Guest User

Not Working Drone Script

a guest
Jun 3rd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. drone.lua:
  2.  
  3. targetx=0
  4. targety=0
  5. targetz=0
  6.  
  7. local navi=component.proxy(component.list('navigation')())
  8. local xyz={navi.getPosition()}
  9. local x=math.floor(xyz[1])
  10. local y=math.floor(xyz[2])
  11. local z=math.floor(xyz[3])
  12. local modem=component.proxy(component.list('modem')())
  13. modem.open(1337)
  14.  
  15. local function respond(...)
  16.   local args=table.pack(...)
  17.   pcall(function() modem.broadcast(1337, table.unpack(args)) end)
  18. end
  19.  
  20. local function receive()
  21.   while true do
  22.     local e, _, _, _, _, cmd=computer.pullSignal()
  23.     if e == "modem_message" then return load(cmd) end
  24.   end
  25. end
  26.  
  27. function getDroneXYZ()
  28.   pcall(function() modem.broadcast(1337, navi.getPosition()) end)
  29. end
  30.  
  31. function getDroneX()
  32.   pcall(function() modem.broadcast(1337, x) end)
  33. end
  34.  
  35. function geDronetY()
  36.   pcall(function() modem.broadcast(1337, y) end)
  37. end
  38.  
  39. function getDroneZ()
  40.   pcall(function() modem.broadcast(1337, z) end)
  41. end
  42.  
  43. while true do
  44.   if targetx ~= 0 or targety ~= 0 or targetz ~= 0 then
  45.     nx = targetx - x
  46.     ny = targety - y
  47.     nz = targetz - z
  48.     coords = nx .. ", " .. ny .. ", " .. nz
  49.     pcall(function() modem.broadcast(1337, "coords=" .. coords) end)
  50.     --drone.setStatusText(coords)
  51.     drone.move(nx, ny, nz)
  52.     targetx = 0
  53.     targety = 0
  54.     targetz = 0
  55.   end
  56.   local result, reason = pcall(function()
  57.     local result, reason = receive()
  58.     if not result then return respond(reason) end
  59.     respond(result())
  60.   end)
  61.   if not result then respond(reason) end
  62. end
  63.  
  64.  
  65.  
  66. tablet.lua:
  67.  
  68. local component = require('component')
  69. local modem = component.modem
  70. local event = require("event")
  71. local term = require("term")
  72.  
  73. coords=""
  74.  
  75. modem.broadcast(1337, "drone=component.proxy(component.list('drone')())")
  76.  
  77. function printblockinfo(eventname, data)
  78.   modem.broadcast(1337, "targetx=" .. data['posX'])
  79.   modem.broadcast(1337, "targety=" .. data['posY'])
  80.   modem.broadcast(1337, "targetz=" .. data['posZ'])
  81.   term.clear()
  82.   print("X: " .. data['posX'])
  83.   print("Y: " .. data['posY'])
  84.   print("Z: " .. data['posZ'])
  85. end
  86.  
  87. --print("listening for tablet_use events")
  88. event.listen("tablet_use", printblockinfo)
  89.  
  90. function transmit()
  91.   term.clear()
  92.   print("-----Transmit-----")
  93.   while true do
  94.     msg=io.read()
  95.     if msg == "leave" then
  96.       break
  97.     else
  98.       pcall(function() modem.broadcast(1337, msg) end)
  99.       print(select(6, event.pull(6, "modem_message")))
  100.     end
  101.   end
  102.   event.pull("interrupted")
  103.   event.ignore("tablet_use", printblockinfo)
  104. end
  105.  
  106. function main()
  107.   print("send")
  108.   print("leave")
  109.   while true do
  110.     if coords ~= "" then
  111.       modem.broadcast(1337, "drone.move(" .. coords .. ")")
  112.     end
  113.     input=io.read()
  114.     if input == "send" then
  115.       transmit()
  116.     elseif input == "leave" then
  117. --      event.pull("interrupted")
  118. --      event.ignore("tablet_use", printblockinfo)
  119. --      print("done")
  120.       break
  121.     end
  122.   end
  123. end
  124.  
  125. main()
  126.  
  127. event.pull("interrupted")
  128. event.ignore("tablet_use", printblockinfo)
  129. print("done")
Advertisement
Add Comment
Please, Sign In to add comment