Advertisement
Guest User

macgyver.lua

a guest
Dec 20th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. sides = {'back','front','top','bottom','left','right'}
  2. dhd = false
  3. monitor = false
  4.  
  5. locations = {}
  6. function closePortal()
  7.   local result = false
  8.   repeat
  9.     result,value = pcall(dhd.terminate)
  10.     os.sleep(0.3)
  11.   until result == true
  12. end
  13.  
  14. function dialLocation(i)
  15.     print(dhd.getStoredName(2))
  16.     outputLocations(locations[i])
  17.     closePortal()
  18.     local index = i - 1
  19.     local res = false
  20.     repeat
  21.       res,v = pcall(dhd.dialStored, index)
  22.       os.sleep(0.3)
  23.    
  24.     until res == true
  25. end
  26.  
  27. function generateLocations()
  28.     for i=0, address_count - 1  do
  29.         locations[i + 1] = dhd.getStoredName(i)
  30.     end
  31. end
  32.  
  33. function outputLocations(dialled)
  34.     monitor.clear()
  35.     term.clear()
  36.     monitor.setCursorPos(1,1)
  37.     term.setCursorPos(1,1)
  38.     for i,v in ipairs(locations) do
  39.         monitor.write(i..". "..v)
  40.         monitor.setCursorPos(1, i + 1)
  41.     end
  42.     if( not addressDialled == false) then
  43.         monitor.write("Last Location Dialled: "..dialled)
  44.     end
  45. end
  46.  
  47. -- wrap peripherals automagically
  48. for i, side in ipairs(sides) do
  49.     if(peripheral.isPresent(side)) then
  50.         if(peripheral.getType(side) == 'ep_dialling_device') then
  51.             dhd = peripheral.wrap(side)
  52.             --print methods (debug)
  53.             --for i,v in ipairs(peripheral.getMethods(side)) do print(i..". "..v) end
  54.         elseif(peripheral.getType(side) == 'monitor') then
  55.             monitor = peripheral.wrap(side)
  56.         end
  57.     end
  58. end
  59.  
  60. if(not dhd or not monitor) then
  61.     error('No portal or monitor connected to computer')
  62. end
  63.  
  64. address_count = dhd.getStoredCount()
  65. generateLocations()
  66. outputLocations(false)
  67.  
  68. --monitor bs
  69. while true do
  70.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  71.     --if address touched
  72.     if(yPos <= address_count) then
  73.         dialLocation(yPos)
  74.     end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement