NickSProud

nsp_turtle.lua

Jun 26th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.71 KB | None | 0 0
  1. -- Z X are horizontal, Y is height, vertial.
  2. -- Facing options: N, E, S, W
  3. -- North is -Z East is +X South +Z West -X
  4.  
  5. nsp = require("nsp_core")
  6.  
  7. nsp_turtle = {}
  8. --Primes the turtle, makes sure it has a location var and file--
  9. function nsp_turtle.prime_turtle()
  10.     turtle_location = nsp_core.unserialize("turtle_location")
  11.     if turtle_location == nil then
  12.         nsp_core.serialize(turtle_location_blank, "turtle_location")
  13.         turtle_location = {["x"] = 0,["z"] = 0,["y"] = 0,["f"] = "N",["name"] = os.getComputerLabel()}
  14.     end
  15. end
  16.  
  17. --move forwards, update location file--
  18. function nsp_turtle.fwd()
  19.     if turtle.forward() then
  20.         if turtle_location["f"] == "N" then
  21.             turtle_location["z"] = turtle_location["z"]-1
  22.             nsp_core.serialize(turtle_location, "turtle_location")
  23.             return true
  24.         elseif turtle_location["f"] == "E" then
  25.             turtle_location["x"] = turtle_location["x"]+1
  26.             nsp_core.serialize(turtle_location, "turtle_location")
  27.             return true
  28.         elseif turtle_location["f"] == "S" then
  29.             turtle_location["z"] = turtle_location["z"]+1
  30.             nsp_core.serialize(turtle_location, "turtle_location")
  31.             return true
  32.         elseif turtle_location["f"] == "W" then
  33.             turtle_location["x"] = turtle_location["x"]-1
  34.             nsp_core.serialize(turtle_location, "turtle_location")
  35.             return true
  36.         end
  37.     else
  38.         return false
  39.     end
  40. end
  41.  
  42. --moves backwards, updates location file--
  43. function nsp_turtle.bwd()
  44.     if turtle.forward() then
  45.         if turtle_location["f"] == "N" then
  46.             turtle_location["z"] = turtle_location["z"]+1
  47.             nsp_core.serialize(turtle_location, "turtle_location")
  48.             return true
  49.         elseif turtle_location["f"] == "E" then
  50.             turtle_location["x"] = turtle_location["x"]-1
  51.             nsp_core.serialize(turtle_location, "turtle_location")
  52.             return true
  53.         elseif turtle_location["f"] == "S" then
  54.             turtle_location["z"] = turtle_location["z"]-1
  55.             nsp_core.serialize(turtle_location, "turtle_location")
  56.             return true
  57.         elseif turtle_location["f"] == "W" then
  58.             turtle_location["x"] = turtle_location["x"]+1
  59.             nsp_core.serialize(turtle_location, "turtle_location")
  60.             return true
  61.         end
  62.     else
  63.         return false
  64.     end
  65. end
  66.  
  67. --turns left, updates location--
  68. function nsp_turtle.turn_l()
  69.     if turtle.turnLeft() then
  70.         if turtle_location["f"] == "N" then
  71.             turtle_location["f"] = "W"
  72.             nsp_core.serialize(turtle_location, "turtle_location")
  73.             return true
  74.         elseif turtle_location["f"] == "E" then
  75.             turtle_location["f"] = "N"
  76.             nsp_core.serialize(turtle_location, "turtle_location")
  77.             return true
  78.         elseif turtle_location["f"] == "S" then
  79.             turtle_location["f"] = "E"
  80.             nsp_core.serialize(turtle_location, "turtle_location")
  81.             return true
  82.         elseif turtle_location["f"] == "W" then
  83.             turtle_location["f"] = "S"
  84.             nsp_core.serialize(turtle_location, "turtle_location")
  85.             return true
  86.         end
  87.     else
  88.         return false
  89.     end
  90. end
  91.  
  92. --turns right--
  93. function nsp_turtle.turn_r()
  94.     if turtle.turnRight() then
  95.         if turtle_location["f"] == "N" then
  96.             turtle_location["f"] = "E"
  97.             nsp_core.serialize(turtle_location, "turtle_location")
  98.             return true
  99.         elseif turtle_location["f"] == "E" then
  100.             turtle_location["f"] = "S"
  101.             nsp_core.serialize(turtle_location, "turtle_location")
  102.             return true
  103.         elseif turtle_location["f"] == "S" then
  104.             turtle_location["f"] = "W"
  105.             nsp_core.serialize(turtle_location, "turtle_location")
  106.             return true
  107.         elseif turtle_location["f"] == "W" then
  108.             turtle_location["f"] = "N"
  109.             nsp_core.serialize(turtle_location, "turtle_location")
  110.             return true
  111.         end
  112.     else
  113.         return false
  114.     end
  115. end
  116.  
  117. function nsp_turtle.up()
  118.     if turtle.up() then
  119.         turtle_location["y"] = turtle_location["y"]+1
  120.         nsp_core.serialize(turtle_location, "turtle_location")
  121.         return true
  122.     else
  123.         return false
  124.     end
  125. end
  126.  
  127. function nsp_turtle.down()
  128.     if turtle.down() then
  129.         turtle_location["y"] = turtle_location["y"]-1
  130.         nsp_core.serialize(turtle_location, "turtle_location")
  131.         return true
  132.     else
  133.         return false
  134.     end
  135. end
  136.  
  137. function nsp_turtle.fwds(amount)
  138.     print("Movin:" .. amount)
  139.     --Move forwards amount
  140.     if type(amount) == "number" then
  141.         local moved = 0
  142.         for i = 1, amount do
  143.             if nsp_turtle.fwd() then
  144.                 moved = moved + 1
  145.             else
  146.                 -- couldn't move --
  147.                 return(moved)
  148.             end
  149.         end
  150.         return(moved)
  151.     else
  152.         --not a number, what the fuck?
  153.         return(0)
  154.     end
  155. end
  156.  
  157. function nsp_turtle.bwds(amount)
  158.     print("Movin:" .. amount)
  159.     --Move forwards amount
  160.     if type(amount) == "number" then
  161.         local moved = 0
  162.         for i = 1, amount do
  163.             if nsp_turtle.bwd() then
  164.                 moved = moved + 1
  165.             else
  166.                 -- couldn't move --
  167.                 return(moved)
  168.             end
  169.         end
  170.         return(moved)
  171.     else
  172.         --not a number, what the fuck?
  173.         return(0)
  174.     end
  175. end
  176.  
  177. function nsp_turtle.ups(amount)
  178.     print("Movin:" .. amount)
  179.     --Move forwards amount
  180.     if type(amount) == "number" then
  181.         local moved = 0
  182.         for i = 1, amount do
  183.             if nsp_turtle.up() then
  184.                 moved = moved + 1
  185.             else
  186.                 -- couldn't move --
  187.                 return(moved)
  188.             end
  189.         end
  190.         return(moved)
  191.     else
  192.         --not a number, what the fuck?
  193.         return(0)
  194.     end
  195. end
  196.  
  197. function nsp_turtle.downs(amount)
  198.     print("Movin:" .. amount)
  199.     --Move forwards amount
  200.     if type(amount) == "number" then
  201.         local moved = 0
  202.         for i = 1, amount do
  203.             if nsp_turtle.down() then
  204.                 moved = moved + 1
  205.             else
  206.                 -- couldn't move --
  207.                 return(moved)
  208.             end
  209.         end
  210.         return(moved)
  211.     else
  212.         --not a number, what the fuck?
  213.         return(0)
  214.     end
  215. end
  216. --return home--
  217. function nsp_turtle.setCurrentLocation(name)
  218.     if type(name) == "string" then
  219.         nsp_core.serialize(turtle_location, name)
  220.         return true
  221.     else
  222.         return false
  223.     end
  224. end
  225.  
  226. function nsp_turtle.face(direction)
  227.     if direction == turtle_location["f"] then
  228.         return true
  229.     elseif turtle_location["f"] == "N" then
  230.         if direction== "E" then
  231.             return nsp_turtle.turn_r()
  232.         elseif direction == "S" then
  233.             if nsp_turtle.turn_r() then
  234.                 return nsp_turtle.turn_r()
  235.             else
  236.                 return false
  237.             end
  238.         elseif direction == "W" then
  239.             return nsp_turtle.turn_l()
  240.         end
  241.     elseif  turtle_location["f"] == "E" then
  242.         if direction == "S" then
  243.             return nsp_turtle.turn_r()
  244.         elseif direction == "W" then
  245.             if nsp_turtle.turn_r() then
  246.                 return nsp_turtle.turn_r()
  247.             else
  248.                 return false
  249.             end
  250.         elseif direction == "N" then
  251.             return nsp_turtle.turn_l()
  252.         end
  253.     elseif turtle_location["f"] == "S" then
  254.         if direction == "W" then
  255.             return nsp_turtle.turn_r()
  256.         elseif direction == "N" then
  257.             if nsp_turtle.turn_r() then
  258.                 return nsp_turtle.turn_r()
  259.             else
  260.                 return false
  261.             end
  262.         elseif direction == "E" then
  263.             return nsp_turtle.turn_l()
  264.         end      
  265.     elseif turtle_location["f"] == "W" then
  266.         if direction == "N" then
  267.             return nsp_turtle.turn_r()
  268.         elseif direction == "E" then
  269.             if nsp_turtle.turn_r() then
  270.                 return nsp_turtle.turn_r()
  271.             else
  272.                 return false
  273.             end
  274.         elseif direction == "S" then
  275.             return nsp_turtle.turn_l()
  276.         end
  277.     else
  278.         print("Error: nsp_turtle.face requires string argument N E S W")
  279.     end
  280. end
  281.  
  282. function nsp_turtle.goX(number)
  283.     number = number-turtle_location["x"]
  284.     local negativeNumber
  285.     if type(number) == "number" then
  286.         if number == 0 then
  287.             return 0
  288.         elseif number > 0 then
  289.             negativeNumber = false
  290.             if nsp_turtle.face("E") == false then
  291.                 print("Failed at turning east somehow")
  292.                 return number
  293.             end
  294.         elseif number < 0 then
  295.             negativeNumber = true
  296.             if nsp_turtle.face("W") == false then
  297.                 print("Failed at turning west somehow")
  298.                 return number
  299.             end
  300.         end
  301.         number = math.abs(number)
  302.         number = number - nsp_turtle.fwds(number)
  303.         if negativeNumber then
  304.             nubmer = number * -1
  305.         end
  306.         return number
  307.     else
  308.         print("goX requires a number")
  309.         return 0
  310.     end
  311. end
  312.  
  313. function nsp_turtle.goZ(number)
  314.     number = number-turtle_location["z"]
  315.     local negativeNumber
  316.     if type(number) == "number" then
  317.         if number == 0 then
  318.             return 0
  319.         elseif number > 0 then
  320.             negativeNumber = false
  321.             if nsp_turtle.face("S") == false then
  322.                 print("Failed at turning east somehow")
  323.                 return number
  324.             end
  325.         elseif number < 0 then
  326.             negativeNumber = true
  327.             if nsp_turtle.face("N") == false then
  328.                 print("Failed at turning west somehow")
  329.                 return number
  330.             end
  331.         end
  332.         number = math.abs(number)
  333.         number = number - nsp_turtle.fwds(number)
  334.         if negativeNumber then
  335.             number = number * -1
  336.         end
  337.         return number
  338.     else
  339.         print("goZ requires a number")
  340.         return 0
  341.     end
  342. end
  343.  
  344. function nsp_turtle.goY(number)
  345.     number = number - turtle_location["y"]
  346.     if type(number) == "number" then
  347.         if number == 0 then
  348.             return 0
  349.         elseif number > 0 then
  350.                 number = number - nsp_turtle.ups(number)
  351.         elseif number < 0 then
  352.             number = math.abs(number)
  353.                 number = number - nsp_turtle.downs(number)
  354.             nubmer = -number
  355.         end
  356.         return number
  357.     else
  358.         print("goY requires a number")
  359.         return 0
  360.     end
  361. end
  362.  
  363. function nsp_turtle.goTo(location)
  364.     math.randomseed (os.time("local"))
  365.     turtle_location = nsp_core.unserialize("turtle_location")
  366.     local x_remaining = location["x"] - turtle_location["x"]
  367.     local z_remaining = location["z"] - turtle_location["z"]
  368.     local y_remaining = location["y"] - turtle_location["y"]
  369.  
  370.     local at_location = x_remaining + z_remaining + y_remaining == 0
  371.     local total_changed = 0
  372.     while at_location == false do
  373.         local init_location = turtle_location
  374.        
  375.         x_remaining = nsp_turtle.goX(location["x"])
  376.         turtle_location = nsp_core.unserialize("turtle_location")
  377.         local x_changed = init_location["x"] - turtle_location["x"]
  378.         print("X Changed: ".. x_changed)
  379.  
  380.         z_remaining = nsp_turtle.goZ(location["z"])
  381.         turtle_location = nsp_core.unserialize("turtle_location")
  382.         local z_changed = init_location["z"] - turtle_location["z"]
  383.         print("Z Changed: ".. z_changed)
  384.  
  385.         y_remaining = nsp_turtle.goY(location["y"])
  386.         turtle_location = nsp_core.unserialize("turtle_location")
  387.         local y_changed = init_location["y"] - turtle_location["y"]
  388.         print("Y Changed: ".. y_changed)
  389.  
  390.         total_changed = x_changed + z_changed + y_changed
  391.         print(total_changed)
  392.  
  393.         turtle_location = nsp_core.unserialize("turtle_location")
  394.         x_remaining = math.abs(location["x"] - turtle_location["x"])
  395.         z_remaining = math.abs(location["z"] - turtle_location["z"])
  396.         y_remaining = math.abs(location["y"] - turtle_location["y"])
  397.  
  398.         at_location = x_remaining + z_remaining + y_remaining == 0
  399.     end
  400.     nsp_turtle.face(location["f"])
  401. end
  402.  
  403. function nsp_turtle.dig()
  404.     return turtle.dig()
  405. end
  406.  
  407. function nsp_turtle.dig_down()
  408.     return turtle.digDown()
  409. end
  410.  
  411. function nsp_turtle.dig_up()
  412.     return turtle.digUp()
  413. end
  414.  
  415.  
  416.  
  417. --mine infront--
  418. --mine infront up down--
  419.  
  420.  
  421.  
  422. return nsp_turtle
Add Comment
Please, Sign In to add comment