Guest User

fly.lua

a guest
May 28th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. local a = peripheral.wrap("top")
  2. local modem = peripheral.wrap("right")
  3. local entity = peripheral.wrap("left")
  4. modem.open(70)
  5.  
  6. local p = true
  7.  
  8.  
  9. local curX, curY, curZ = gps.locate()
  10. function fly()
  11.     while true do
  12.         a.launch(0,270,0.320)
  13.         sleep(0.2)
  14.     end
  15. end
  16. function trim(val, max)
  17.     if val > max then
  18.         return max
  19.     end
  20.     return val
  21. end
  22. parallel.waitForAll(
  23.     fly,
  24.     function()
  25.         while true do
  26.             if p then
  27.                 local x, y, z = gps.locate()
  28.                 local xDev = curX - x
  29.                 local yDev = curY - y
  30.                 local zDev = curZ - z
  31.                 if yDev+0.1 < 0 then
  32.                     local power = trim(math.abs(yDev) / 6 ,0.1)
  33.                     print(power, "down", y, curY, power, yDev)
  34.                     a.launch(0,90, power)
  35.                 end
  36.                 if yDev-0.1 > 0 then
  37.                     local power = trim(math.abs(yDev) / 6 ,0.1)
  38.                     print(power, "up", y, curY, power, yDev)
  39.                     a.launch(0,270, power)
  40.                 end
  41.                 if xDev+0.1 < 0 then
  42.                     local power = trim(math.abs(xDev) / 8 ,0.33)
  43.                     print(power, "west", x, curX, power, xDev)
  44.                     a.launch(90,0, power)
  45.                     sleep(0.1)
  46.                     a.launch(270,0, power * 0.75)
  47.                 end
  48.                 if xDev-0.1 > 0 then
  49.                     local power = trim(math.abs(xDev) / 8 ,0.33)
  50.                     print(power, "east", x, curX, power, xDev)
  51.                     a.launch(270,0, power)
  52.                     sleep(0.1)
  53.                     a.launch(90,0, power  * 0.75)
  54.                 end
  55.                 if zDev+0.1 < 0 then
  56.                     local power = trim(math.abs(zDev) / 8 ,0.33)
  57.                     print(power, "north", z, curZ, power, zDev)
  58.                     a.launch(180,0, power)
  59.                     sleep(0.1)
  60.                     a.launch(0,0, power * 0.75)
  61.                 end
  62.                 if zDev-0.1 > 0 then
  63.                     local power = trim(math.abs(zDev) / 8 ,0.33)
  64.                     print(power, "south", z, curZ, power, zDev)
  65.                     a.launch(0,0, power)
  66.                     sleep(0.1)
  67.                     a.launch(180,0, power  * 0.75)
  68.                 end
  69.             end
  70.             sleep(2)
  71.         end
  72.     end,
  73.     function()
  74.         while true do
  75.             event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  76.             p = false
  77.             if message == "up" then
  78.                 curY = curY + 1
  79.                 a.launch(0,270,0.4)
  80.                 sleep(0.4)
  81.                 a.launch(0,90, 0.3)
  82.             elseif message == "down" then
  83.                 curY = curY - 1
  84.                 a.launch(0,90, 0.4)
  85.                 sleep(0.4)
  86.                 a.launch(0,270,0.3)
  87.             elseif message == "north" then
  88.                 curZ = curZ - 1
  89.                 a.launch(180,0, 0.15)
  90.                 sleep(0.4)
  91.                 a.launch(0,0, 0.1)
  92.             elseif message == "south" then
  93.                 curZ = curZ + 1
  94.                 a.launch(0,0, 0.15)
  95.                 sleep(0.4)
  96.                 a.launch(180,0, 0.1)
  97.             elseif message == "west" then
  98.                 curX = curX - 1
  99.                 a.launch(90,0, 0.15)
  100.                 sleep(0.4)
  101.                 a.launch(270,0, 0.1)
  102.             elseif message == "east" then
  103.                 curX = curX + 1
  104.                 a.launch(270,0, 0.15)
  105.                 sleep(0.4)
  106.                 a.launch(90,0, 0.1)
  107.             end
  108.             p = true
  109.         end
  110.     end
  111. )
Add Comment
Please, Sign In to add comment