Larvix

movement

Dec 25th, 2023 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.73 KB | None | 0 0
  1. remoteTurtle = function()
  2. controls = {
  3. "forward/back = \24/\25",
  4. "left/right = \27/\26",
  5. "up/down = pageUp/pageDown",
  6. "break = delete",
  7. "place = insert",
  8. }
  9. bearRight = {
  10.     north = 'east',
  11.     east  = 'south',
  12.     south = 'west',
  13.     west  = 'north',
  14. }
  15. bearLeft = {
  16.     north = 'west',
  17.     east  = 'north',
  18.     south = 'east',
  19.     west  = 'south',
  20. }
  21. movePos = {
  22.     north = { 0, 0,-1},
  23.     east  = { 1, 0, 0},
  24.     south = { 0, 0, 1},
  25.     west  = {-1, 0, 0},
  26. }
  27. inv = {
  28. 00,00,00,00,
  29. 00,00,00,00,
  30. 00,00,00,00,
  31. 00,00,00,00,
  32. }
  33. getBearing = function()
  34.     pos = {x=0,y=0,z=0}
  35.     pos.x, pos.y, pos.z = gps.locate()
  36.    
  37.     turtle.forward()
  38.     diffX, diffY, diffZ = gps.locate()
  39.     turtle.back()
  40.    
  41.     if not diffX then
  42.         print("Unable to triangulate GPS")
  43.     else
  44.         diffX = diffX - pos.x
  45.         diffZ = diffZ - pos.z
  46.         if diffX ~= 0 then
  47.             if diffX > 0 then
  48.                 bearing = "east"
  49.             else bearing = "west" end
  50.         elseif diffZ > 0 then
  51.             bearing = "south"
  52.         else bearing = "north" end
  53.     end
  54. end
  55. left = function()
  56.     if turtle.turnLeft() then
  57.         bearing = bearLeft[bearing]
  58.     end
  59. end
  60. right = function()
  61.     if turtle.turnRight() then
  62.         bearing = bearRight[bearing]
  63.     end
  64. end
  65. fwd = function()
  66.     change = movePos[bearing]
  67.     if turtle.forward() then
  68.         pos.x = pos.x - change[1]
  69.         pos.z = pos.z - change[3]
  70.     end
  71. end
  72. back = function()
  73.     change = movePos[bearing]
  74.     if turtle.back() then
  75.         pos.x = pos.x - change[1]
  76.         pos.z = pos.z - change[3]
  77.     end
  78. end
  79. up = function()
  80.     if turtle.up() then
  81.         pos.y = pos.y + 1
  82.     end
  83. end
  84. down = function()
  85.     if turtle.down() then
  86.         pos.y = pos.y - 1
  87.     end
  88. end
  89. dig = function()
  90.     if turtle.dig() then
  91.    
  92.     end
  93. end
  94. put = function()
  95.     if turtle.place() then
  96.    
  97.     end
  98. end
  99. shell.run("clear")
  100. getBearing()
  101. firstRun = true
  102. while true do
  103.     if not firstRun then
  104.         evt,key,cmd = os.pullEvent("rednet_message")
  105.     else firstRun = false end
  106.     if cmd == keys.enter then
  107.         break
  108.     elseif cmd == keys.left or cmd == "left" then
  109.         left()
  110.     elseif cmd == keys.right or cmd == "right" then
  111.         right()
  112.     elseif cmd == keys.up or cmd == "forward" then
  113.         fwd()
  114.     elseif cmd == keys.down or cmd == "back" then
  115.         back()
  116.     elseif cmd == keys.pageUp or cmd == "up" then
  117.         up()
  118.     elseif cmd == keys.pageDown or cmd == "down" then
  119.         down()
  120.     elseif cmd == keys.delete or cmd == "dig" then
  121.         dig()    
  122.     elseif cmd == keys.insert or cmd == "place" then
  123.         put()
  124.     else
  125.         os.queueEvent("yield")
  126.         os.pullEvent("yield")
  127.     end
  128.     x,y = term.getCursorPos()
  129.     term.setCursorPos(1,1)
  130.     term.clearLine()
  131.     print("Bearing: "..bearing)
  132.     term.setCursorPos(1,2)
  133.     term.clearLine()
  134.     print("Position: "..pos.x,pos.y,pos.z)
  135.     mX,mY = term.getSize()
  136.     for i = #controls,1,-1 do
  137.         term.setCursorPos(1,mY-#controls+i)
  138.         write(controls[i])
  139.     end
  140.     term.setCursorPos(x,y)
  141. end
  142. end
  143.  
  144. remoteMob = function()
  145. controls = {
  146. "forward/back = \24/\25",
  147. "left/right = \27/\26",
  148. "jump = pageUp *WIP",
  149. }
  150. movePos = {
  151.     north = { 0, 0,-1},
  152.     east  = { 1, 0, 0},
  153.     south = { 0, 0, 1},
  154.     west  = {-1, 0, 0},
  155. }
  156.     mobBearing = function()
  157.         pos = {x=0,y=0,z=0}
  158.         pos.x, pos.y, pos.z = gps.locate()
  159.         pos.x = math.floor(pos.x)
  160.         pos.y = math.floor(pos.y)
  161.         pos.z = math.floor(pos.z)
  162.  
  163.         yaw = math.abs(peripheral.wrap("back").getMetaOwner().yaw)
  164.         if math.abs(180-yaw) < 45 then
  165.             bearing = "north"
  166.             mob.look(180,0)
  167.         elseif math.abs(270-yaw) < 45 then
  168.             bearing = "east"
  169.             mob.look(270,0)
  170.         elseif math.abs(0-yaw) < 45 then
  171.             bearing = "south"
  172.             mob.look(0,0)
  173.         elseif math.abs(90-yaw) < 45 then
  174.             bearing = "west"
  175.             mob.look(90,0)
  176.         end
  177.         yaw = math.abs(peripheral.wrap("back").getMetaOwner().yaw)
  178.     end
  179.  
  180.     shell.run("clear")
  181.     firstRun = true
  182.     while true do
  183.         if not firstRun then
  184.             evt = {os.pullEvent("rednet_message")}
  185.             cmd = evt[3]
  186.         else
  187.             firstRun = false
  188.             cmd = nil
  189.         end
  190.         mob = peripheral.wrap("back")
  191.         mobBearing()
  192.         if cmd == false then
  193.             break
  194.         elseif cmd == "left" then
  195.             newYaw = yaw - 90
  196.             if newYaw < 0 then
  197.                 newYaw = 360 + newYaw
  198.             end
  199.             mob.look(newYaw,0)
  200.         elseif cmd == "right" then
  201.             newYaw = yaw + 90
  202.             if newYaw > 360 then
  203.                 newYaw = newYaw - 360
  204.             end
  205.             mob.look(newYaw,0)
  206.         elseif cmd == "forward" then
  207.             change = movePos[bearing]
  208.             if pcall(mob.walk(change[1],change[2],change[3])) then
  209.                 pos.x = pos.x - change[1]
  210.                 pos.z = pos.z - change[3]
  211.                 mob.look(yaw,0)
  212.             end
  213.         elseif cmd == "back" then
  214.             change = movePos[bearing]
  215.             if pcall(mob.walk(-change[1],change[2],-change[3])) then
  216.                 pos.x = pos.x + change[1]
  217.                 pos.z = pos.z + change[3]
  218.                 mob.look(yaw,0)
  219.             end
  220.         elseif cmd == "up" then
  221.             mob.launch(0,-90,1)
  222.         else
  223.             os.queueEvent("yield")
  224.             os.pullEvent("yield")
  225.         end
  226.         mobBearing()
  227.         term.setCursorPos(1,1)
  228.         term.clearLine()
  229.         print("Bearing: "..bearing)
  230.         term.setCursorPos(1,2)
  231.         term.clearLine()
  232.         print("Position: "..pos.x,pos.y,pos.z)
  233.         mX,mY = term.getSize()
  234.         for i = #controls,1,-1 do
  235.             term.setCursorPos(1,mY-#controls+i)
  236.             write(controls[i])
  237.         end
  238.     end
  239. end
  240.  
  241. modem = peripheral.find("modem")
  242. if not modem then
  243.     print("[Error] No modem")
  244. else
  245.     rednet.open(peripheral.getName(modem))
  246.     if not gps.locate() then
  247.         print("[Error] No GPS")
  248.     else
  249.         if turtle then
  250.             remoteTurtle()
  251.         elseif peripheral.getType("back") == "neuralInterface" then
  252.             if peripheral.wrap("back").hasModule("plethora:kinetic") then
  253.                 remoteMob()
  254.             end
  255.         end
  256.     end
  257. end
Advertisement
Add Comment
Please, Sign In to add comment