Larvix

apiTestProgram

Feb 5th, 2024 (edited)
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.81 KB | None | 0 0
  1. shell.run("clear")
  2.  
  3. maps = require("map")
  4. local map = maps.loadMap()
  5. local tileset = maps.loadTiles(true)
  6. maps.scanner()
  7.  
  8. pf = require("path")
  9. local curPath = {}
  10. local manualMode = false
  11. local update = true
  12.  
  13. imp = require("impTurtle")
  14. imp.setBearing()
  15. local pos = imp.info().position
  16. local center = {x=tonumber(pos.x),y=tonumber(pos.y),z=tonumber(pos.z)}
  17.  
  18. local displayMap = function()
  19.     pos = imp.info().position
  20.     map = maps.loadMap()
  21.     d = maps.draw(map,tileset,9,center.y,center.x,center.z)
  22.     for termX,col in pairs(d) do
  23.         for termY,b in pairs(d[termX]) do
  24.             if b.y == center.y and b.y == pos.y and b.x == pos.x and b.z == pos.z then
  25.                 d[termX][termY].name = "Me!"
  26.                 term.current().setCursorPos(termX,termY)
  27.                 if manualMode then
  28.                     term.current().setTextColor(colors.pink)
  29.                     if imp.info().bearing == "west" then
  30.                         term.current().write("\27")
  31.                     elseif imp.info().bearing == "east" then
  32.                         term.current().write("\26")
  33.                     elseif imp.info().bearing == "north" then
  34.                         term.current().write("\24")
  35.                     elseif imp.info().bearing == "south" then
  36.                         term.current().write("\25")
  37.                     end
  38.                 else
  39.                     term.current().setTextColor(colors.yellow)
  40.                     term.current().write("\2")
  41.                 end
  42.                 term.current().setTextColor(colors.white)
  43.             else
  44.                 for i = 1,#curPath do
  45.                     if i == 1 then
  46.                         curPath[i].colour = colors.green
  47.                     elseif i == 2 or i == 3 then
  48.                         curPath[i].colour = colors.lime
  49.                     elseif i >= 4 and i <= 6 then
  50.                         curPath[i].colour = colors.yellow
  51.                     elseif i >= 7 and i <= 10 then
  52.                         curPath[i].colour = colors.orange
  53.                     elseif i >= 11 and i <= 15 then
  54.                         curPath[i].colour = colors.red
  55.                     elseif i >= 16 and i <= 20 then
  56.                         curPath[i].colour = colors.purple
  57.                     elseif i >= 21 and i <= 30 then
  58.                         curPath[i].colour = colors.magenta
  59.                     elseif i >= 31 and i <= 50 then
  60.                         curPath[i].colour = colors.pink
  61.                     else
  62.                         curPath[i].colour = colors.white
  63.                     end
  64.                     if b.y == center.y and b.y == curPath[i].y and b.x == curPath[i].x and b.z == curPath[i].z then
  65.                         term.current().setCursorPos(termX,termY)
  66.                         term.current().setTextColor(curPath[i].colour)
  67.                         term.current().write(curPath[i].tile)
  68.                         term.current().setTextColor(colors.white)
  69.                     end
  70.                 end
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. while true do
  77.     if turtle and turtle.getFuelLevel() < 1 then
  78.         turtle.refuel()
  79.     end
  80.     if update then
  81.         displayMap()
  82.         update = false
  83.     end
  84.     local evt = {os.pullEvent()}
  85.     if evt[1] == "key" or "mouse_click" then update = true end
  86.     if evt[1] == "key" then
  87.         if evt[2] == keys.leftCtrl or evt[2] == keys.rightCtrl then
  88.             manualMode = not manualMode
  89.             if manualMode then path = {} end
  90.         elseif evt[2] == keys.enter then
  91.             for i = 1,#curPath do
  92.                 local mvmt = curPath[1].dir
  93.                 if mvmt == "w" or mvmt == "n" or mvmt == "e" or mvmt == "s" then
  94.                     local face = string.sub(imp.info().bearing,1,1)
  95.                     while mvmt ~= face do
  96.                         if face == "w" and mvmt == "n" or face == "e" and mvmt == "s" or face == "n" and mvmt == "e" or face == "s" and mvmt == "w" then
  97.                             imp.right()
  98.                         elseif face == "w" and mvmt == "s" or face == "e" and mvmt == "n" or face == "n" and mvmt == "w" or face == "s" and mvmt == "e" then
  99.                             imp.left()
  100.                         else
  101.                             imp.right()
  102.                         end
  103.                         face = string.sub(imp.info().bearing,1,1)
  104.                     end
  105.                     imp.forward()
  106.                 elseif mvmt == "u" then
  107.                     imp.up()
  108.                 elseif mvmt == "d" then
  109.                     imp.down()
  110.                 end
  111.                 pos = imp.info().position
  112.                 center = {x=tonumber(pos.x),y=tonumber(pos.y),z=tonumber(pos.z)}
  113.                 maps.scanner()
  114.                 table.remove(curPath,1)
  115.                 displayMap()
  116.             end
  117.             while turtle.inspect() do imp.left() end
  118.             curPath = {}
  119.         elseif evt[2] == keys.leftShift then
  120.             imp.left()
  121.         elseif evt[2] == keys.rightShift then
  122.             imp.right()
  123.         elseif evt[2] == keys.left then
  124.             if manualMode then
  125.                 imp.left()
  126.             else
  127.                 center.x = center.x - 1
  128.             end
  129.         elseif evt[2] == keys.right then
  130.             if manualMode then
  131.                 imp.right()
  132.             else
  133.                 center.x = center.x + 1
  134.             end
  135.         elseif evt[2] == keys.up then
  136.             if manualMode then
  137.                 imp.forward()
  138.                 maps.scanner()
  139.             else
  140.                 center.z = center.z - 1
  141.             end
  142.         elseif evt[2] == keys.down then
  143.             if manualMode then
  144.                 imp.back()
  145.                 maps.scanner()
  146.             else
  147.                 center.z = center.z + 1
  148.             end
  149.         elseif evt[2] == keys.pageDown then
  150.             if manualMode then
  151.                 imp.down()
  152.                 maps.scanner()
  153.             else
  154.                 center.y = center.y - 1
  155.             end
  156.         elseif evt[2] == keys.pageUp then
  157.             if manualMode then
  158.                 imp.up()
  159.                 maps.scanner()
  160.             else
  161.                 center.y = center.y + 1
  162.             end
  163.         elseif evt[2] == keys.home then
  164.             imp.goToMarker()
  165.         elseif evt[2] == keys.space then
  166.             pos = imp.info().position
  167.             center = {x=tonumber(pos.x),y=tonumber(pos.y),z=tonumber(pos.z)}
  168.         elseif evt[2] == keys.tab then
  169.             shell.run("clear")
  170.             print("Map Controls:")
  171.             print(" - Delete: Delete Current Map")
  172.             print(" - Arrow Keys: Navigate Map X/Z")
  173.             print(" - Page Up/Down: Navigate Map Level")
  174.             print(" - Spacebar: Center Map on Turtle")
  175.             print(" - Left Click: Get Block Info")
  176.             print(" - Right Click: Pathfind to Block")
  177.             print(" - Insert: Add Location Marker")
  178.             print(" - Home: Return to Marker/Start")
  179.             print(" - Left/Right Shift: Rotate Turtle")
  180.             print("Manual Controls: (Ctrl to Switch Mode)")
  181.             print(" - Arrow Keys: Move Turtle F/B/L/R")
  182.             write(" - Page Up/Down: Move Turtle Up/Down")
  183.             os.pullEvent("key")
  184.         elseif evt[2] == keys.insert then
  185.             imp.setMarker()
  186.         elseif evt[2] == keys.delete then
  187.             shell.run("clear")
  188.             print("This will delete the entire map. \nPress enter to confirm.")
  189.             e = {os.pullEvent("key")}
  190.             if e[2] == keys.enter and fs.exists("//data/map.txt") then
  191.                 fs.delete("//data/map.txt")
  192.                 map = maps.loadMap()
  193.                 imp.clearPath()
  194.                 print("Map deleted.")
  195.             elseif fs.exists("//data/map.txt") then
  196.                 print("Process terminated.")
  197.             else
  198.                 print("No map found.")
  199.             end
  200.             print("\nPress any key to continue.")
  201.             os.pullEvent("key")
  202.         end
  203.     elseif evt[1] == "mouse_click" and evt[2] == 1 then
  204.         if #curPath > 0 then
  205.             curPath = {}
  206.         else
  207.             local x,y = evt[3], evt[4]
  208.             if d[x] and d[x][y] then
  209.                 pos = imp.info().position
  210.                 shell.run("clear")
  211.                 print("Block:", d[x][y].name)
  212.                 print("Position:", d[x][y].x, d[x][y].y, d[x][y].z)
  213.                 if d[x][y].name ~= "Me!" then
  214.                     local pathable = pf.find(pos.x,pos.y,pos.z,d[x][y].x,d[x][y].y,d[x][y].z)
  215.                     print("Pathable: "..tostring(pathable[1]))
  216.                 end
  217.                 print("\nPress any key to continue")
  218.                 os.pullEvent("key")
  219.             end
  220.         end
  221.     elseif evt[1] == "mouse_click" and evt[2] == 2 and not manualMode then
  222.         curPath = {}
  223.         pos = imp.info().position
  224.         local x,y = evt[3], evt[4]
  225.         if d[x] and d[x][y] then
  226.             pathing = pf.find(pos.x,pos.y,pos.z,d[x][y].x,d[x][y].y,d[x][y].z)
  227.             if pathing[1] then
  228.                 for i = 1,#pathing[2] do
  229.                     curPath[i] = {}
  230.                     if i == 1 then
  231.                         curPath[i].x = pos.x
  232.                         curPath[i].y = pos.y
  233.                         curPath[i].z = pos.z
  234.                     else
  235.                         curPath[i].x = curPath[i-1].x
  236.                         curPath[i].y = curPath[i-1].y
  237.                         curPath[i].z = curPath[i-1].z
  238.                     end
  239.                     curPath[i].dir = string.sub(pathing[2],i,i)
  240.                     if curPath[i].dir == "w" then
  241.                         curPath[i].tile = "\27"
  242.                         curPath[i].x = curPath[i].x - 1
  243.                     elseif curPath[i].dir == "e" then
  244.                         curPath[i].tile = "\26"
  245.                         curPath[i].x = curPath[i].x + 1
  246.                     elseif curPath[i].dir == "n" then
  247.                         curPath[i].tile = "\24"
  248.                         curPath[i].z = curPath[i].z - 1
  249.                     elseif curPath[i].dir == "s" then
  250.                         curPath[i].tile = "\25"
  251.                         curPath[i].z = curPath[i].z + 1
  252.                     elseif curPath[i].dir == "u" then
  253.                         curPath[i].tile = "\30"
  254.                         curPath[i].y = curPath[i].y + 1
  255.                     elseif curPath[i].dir == "d" then
  256.                         curPath[i].tile = "\31"
  257.                         curPath[i].y = curPath[i].y - 1
  258.                     end
  259.                 end
  260.             end
  261.         end
  262.     end
  263. end
Advertisement
Add Comment
Please, Sign In to add comment