le_Fish

Movement api v6

Apr 27th, 2020
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.66 KB | None | 0 0
  1. Movement = {
  2.     pos = vector.new(0, 0, 0),
  3.     dir = "n"
  4. }
  5.  
  6. function Movement:new(pos, dir)
  7.     if type(pos) ~= "table" then
  8.         error("new pos expects to be a vector, currently is " .. type(pos), 2)
  9.     elseif type(dir) ~= "string" then
  10.         error("new dir expects to be a string, currently is " .. type(dir), 2)
  11.     elseif dir:lower() ~= "n" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "e" then
  12.         error("dir expects to be n, s, w or e, currently is " .. dir, 2)
  13.     end
  14.     setmetatable({}, Movement)
  15.     self.pos = pos
  16.     self.dir = dir:lower()
  17.     return self
  18. end
  19.  
  20. function Movement:whitelistCheck(whitelist, direction)
  21.     local whitelist = whitelist
  22.     local direction = direction or nil
  23.     if type(whitelist) ~= "table" then
  24.         error("check expects whitelist to be a table, currently is " .. type(whitelist), 2)
  25.     elseif direction ~= nil and type(direction) ~= "string" then
  26.         error("check expects direction to be a string or nil, currently is " .. type(direction), 2)
  27.     end
  28.     local success, t, d
  29.     if not direction or string.find(direction:lower(), "f") then
  30.         success, t = turtle.inspect()
  31.         d = "u"
  32.     elseif string.find(direction:lower(), "u") then
  33.         success, t = turtle.inspectUp()
  34.         d = "f"
  35.     elseif string.find(direction:lower(), "d") then
  36.         success, t = turtle.inspectDown()
  37.         d = "f"
  38.     else
  39.         error("check direction is not nil, f, u or d, currently is " .. direction, 2)
  40.     end
  41.     if success then
  42.         for i = 1, #whitelist do
  43.             if string.find(t.name:lower(), whitelist[i]:lower()) then
  44.                 local t = {0, 1, 1, 2, 2, 3, 3, 4, 5}
  45.                 local sleepTime = t[tonumber(math.random(1, #t))]
  46.                 sleep(tonumber(sleepTime))
  47.                 if d == "u" then
  48.                     self:up()
  49.                 elseif d == "d" then
  50.                     self:down()
  51.                 elseif d == "f" then
  52.                     self:forward()
  53.                 end
  54.                 break
  55.             end
  56.         end
  57.     else
  58.         sleep(0.5)
  59.     end
  60. end
  61. function Movement:forward(times)
  62.     times = times or 1
  63.     if (type(times) ~= "number") then
  64.         error("forward expects a number, currently is " .. type(times), 2)
  65.     end
  66.     for i = 1, times do
  67.         if turtle.forward() then
  68.             if self.dir == "n" then
  69.                 self.pos.y = self.pos.y - 1
  70.             elseif self.dir == "s" then
  71.                 self.pos.y = self.pos.y + 1
  72.             elseif self.dir == "e" then
  73.                 self.pos.x = self.pos.x + 1
  74.             elseif self.dir == "w" then
  75.                 self.pos.x = self.pos.x - 1
  76.             end
  77.         end
  78.     end
  79. end
  80. function Movement:attackForward(times)
  81.     times = times or 1
  82.     if (type(times) ~= "number") then
  83.         error("attackForward expects a number, currently is " .. type(times), 2)
  84.     end
  85.     for i = 1, times do
  86.         while not turtle.forward() do
  87.             turtle.attack()
  88.             sleep(1)
  89.         end
  90.         if self.dir == "n" then
  91.             self.pos.y = self.pos.y - 1
  92.         elseif self.dir == "s" then
  93.             self.pos.y = self.pos.y + 1
  94.         elseif self.dir == "e" then
  95.             self.pos.x = self.pos.x + 1
  96.         elseif self.dir == "w" then
  97.             self.pos.x = self.pos.x - 1
  98.         end
  99.     end
  100.     return true
  101. end
  102. function Movement:digForward(times, whitelist)
  103.     local whitelist = whitelist or nil
  104.     local times = times or 1
  105.     if (type(times) ~= "number") then
  106.         error("digForward expects a number, currently is " .. type(times), 2)
  107.     elseif type(whitelist) ~= "table" and whitelist ~= nil then
  108.         error("digForward expects whitelist to be a table, currently is " .. type(whitelist), 2)
  109.     end
  110.     for i = 1, times do
  111.         while not turtle.forward() do
  112.             if whitelist ~= nil and #whitelist > 0 then
  113.                 self:whitelistCheck(whitelist, "f")
  114.             end
  115.             turtle.dig()
  116.             sleep(1)
  117.         end
  118.         if self.dir == "n" then
  119.             self.pos.y = self.pos.y - 1
  120.         elseif self.dir == "s" then
  121.             self.pos.y = self.pos.y + 1
  122.         elseif self.dir == "e" then
  123.             self.pos.x = self.pos.x + 1
  124.         elseif self.dir == "w" then
  125.             self.pos.x = self.pos.x - 1
  126.         end
  127.     end
  128.     return true
  129. end
  130. function Movement:digAttackForward(times, whitelist)
  131.     local times = times or 1
  132.     local whitelist = whitelist or nil
  133.     if (type(times) ~= "number") then
  134.         error("digAttackForward expects a number, currently is " .. type(times), 2)
  135.     end
  136.     for i = 1, times do
  137.         while not turtle.forward() do
  138.             if whitelist ~= nil and #whitelist > 0 then
  139.                 self:whitelistCheck(whitelist, "f")
  140.             end
  141.             turtle.dig()
  142.             turtle.attack()
  143.             turtle.attack()
  144.             sleep(1)
  145.         end
  146.         if self.dir == "n" then
  147.             self.pos.y = self.pos.y - 1
  148.         elseif self.dir == "s" then
  149.             self.pos.y = self.pos.y + 1
  150.         elseif self.dir == "e" then
  151.             self.pos.x = self.pos.x + 1
  152.         elseif self.dir == "w" then
  153.             self.pos.x = self.pos.x - 1
  154.         end
  155.     end
  156.     return true
  157. end
  158.  
  159. function Movement:back(times)
  160.     local times = times or 1
  161.     if (type(times) ~= "number") then
  162.         error("back expects a number, currently is " .. type(times), 2)
  163.     end
  164.     for i = 1, times do
  165.         if turtle.back() then
  166.             if self.dir == "n" then
  167.                 self.pos.y = self.pos.y + 1
  168.             elseif self.dir == "s" then
  169.                 self.pos.y = self.pos.y - 1
  170.             elseif self.dir == "e" then
  171.                 self.pos.x = self.pos.x - 1
  172.             elseif self.dir == "w" then
  173.                 self.pos.x = self.pos.x + 1
  174.             end
  175.         end
  176.     end
  177. end
  178. function Movement:attackBack(times)
  179.     local times = times or 1
  180.     if (type(times) ~= "number") then
  181.         error("attackBack expects a number, currently is " .. type(times), 2)
  182.     end
  183.     for i = 1, times do
  184.         while not turtle.back() do
  185.             self:left(2)
  186.             turtle.attack()
  187.             turtle.attack()
  188.             self:left(2)
  189.             sleep(1)
  190.         end
  191.  
  192.         if self.dir == "n" then
  193.             self.pos.y = self.pos.y - 1
  194.         elseif self.dir == "s" then
  195.             self.pos.y = self.pos.y + 1
  196.         elseif self.dir == "e" then
  197.             self.pos.x = self.pos.x + 1
  198.         elseif self.dir == "w" then
  199.             self.pos.x = self.pos.x - 1
  200.         end
  201.     end
  202.     return true
  203. end
  204.  
  205. function Movement:up(times)
  206.     local times = times or 1
  207.     if (type(times) ~= "number") then
  208.         error("up expects a number, currently is " .. type(times), 2)
  209.     end
  210.     for i = 1, times do
  211.         while not turtle.up() do
  212.             sleep(1)
  213.         end
  214.         self.pos.z = self.pos.z + 1
  215.     end
  216.     return true
  217. end
  218. function Movement:digUp(times, whitelist)
  219.     local times = times or 1
  220.     local whitelist = whitelist or nil
  221.     if (type(times) ~= "number") then
  222.         error("digUp expects a number, currently is " .. type(times), 2)
  223.     end
  224.     for i = 1, times do
  225.         while not turtle.up() do
  226.             if whitelist ~= nil and #whitelist > 0 then
  227.                 self:whitelistCheck(whitelist, "u")
  228.             end
  229.             turtle.digUp()
  230.             sleep(1)
  231.         end
  232.         self.pos.z = self.pos.z + 1
  233.     end
  234.     return true
  235. end
  236. function Movement:attackUp(times)
  237.     local times = times or 1
  238.     if (type(times) ~= "number") then
  239.         error("attackUp expects a number, currently is " .. type(times), 2)
  240.     end
  241.     for i = 1, times do
  242.         while not turtle.up() do
  243.             turtle.attackUp()
  244.             sleep(1)
  245.         end
  246.         self.pos.z = self.pos + 1
  247.     end
  248.     return true
  249. end
  250. function Movement:digAttackUp(times, whitelist)
  251.     local times = times or 1
  252.     local whitelist = whitelist or nil
  253.     if (type(times) ~= "number") then
  254.         error("digAttackUp expects a number, currently is " .. type(times), 2)
  255.     end
  256.     for i = 1, times do
  257.         while not turtle.up() do
  258.             if whitelist ~= nil and #whitelist > 0 then
  259.                 self:whitelistCheck(whitelist, "u")
  260.             end
  261.             turtle.digUp()
  262.             turtle.attackUp()
  263.             turtle.attackUp()
  264.             sleep(1)
  265.         end
  266.         self.pos.z = self.pos + 1
  267.     end
  268.     return true
  269. end
  270.  
  271. function Movement:downOnce()
  272.     if turtle.down() then
  273.         self.pos.z = self.pos.z - 1
  274.         return true
  275.     end
  276.     return false
  277. end
  278. function Movement:down(times)
  279.     local times = times or 1
  280.     if (type(times) ~= "number") then
  281.         error("down expects a number, currently is " .. type(times), 2)
  282.     end
  283.     for i = 1, times do
  284.         while not turtle.down() do
  285.             sleep(1)
  286.         end
  287.         self.pos.z = self.pos.z - 1
  288.     end
  289.     return true
  290. end
  291. function Movement:digDown(time, whitelists)
  292.     local times = times or 1
  293.     local whitelist = whitelist or nil
  294.     if (type(times) ~= "number") then
  295.         error("digDown expects a number, currently is " .. type(times), 2)
  296.     end
  297.     for i = 1, times do
  298.         while not turtle.down() do
  299.             if whitelist ~= nil and #whitelist > 0 then
  300.                 self:whitelistCheck(whitelist, "d")
  301.             end
  302.             turtle.digDown()
  303.         end
  304.         self.pos.z = self.pos.z - 1
  305.     end
  306.     return true
  307. end
  308. function Movement:attackDown(times)
  309.     local times = times or 1
  310.     if (type(times) ~= "number") then
  311.         error("attackDown expects a number, currently is " .. type(times), 2)
  312.     end
  313.     for i = 1, times do
  314.         while not turtle.down() do
  315.             turtle.attackDown()
  316.             sleep(1)
  317.         end
  318.         self.pos.z = self.pos - 1
  319.     end
  320.     return true
  321. end
  322. function Movement:digAttackDown(times, whitelist)
  323.     local times = times or 1
  324.     local whitelist = whitelist or nil
  325.     if (type(times) ~= "number") then
  326.         error("digAttackDown expects a number, currently is " .. type(times), 2)
  327.     end
  328.     for i = 1, times do
  329.         while not turtle.down() do
  330.             if whitelist ~= nil and #whitelist > 0 then
  331.                 self:whitelistCheck(whitelist, "d")
  332.             end
  333.             turtle.digDown()
  334.             turtle.attackDown()
  335.             turtle.attackDown()
  336.             sleep(1)
  337.         end
  338.         self.pos.z = self.pos - 1
  339.     end
  340.     return true
  341. end
  342.  
  343. function Movement:left(times)
  344.     local times = times or 1
  345.     if (type(times) ~= "number") then
  346.         error("left expects a number, currently is " .. type(times), 2)
  347.     end
  348.     for i = 1, times do
  349.         if turtle.turnLeft() then
  350.             if self.dir == "n" then
  351.                 self.dir = "w"
  352.             elseif self.dir == "w" then
  353.                 self.dir = "s"
  354.             elseif self.dir == "s" then
  355.                 self.dir = "e"
  356.             elseif self.dir == "e" then
  357.                 self.dir = "n"
  358.             end
  359.         end
  360.     end
  361. end
  362. function Movement:right(times)
  363.     local times = times or 1
  364.     if (type(times) ~= "number") then
  365.         error("right expects a number, currently is " .. type(times), 2)
  366.     end
  367.     for i = 1, times do
  368.         if turtle.turnRight() then
  369.             if self.dir == "n" then
  370.                 self.dir = "e"
  371.             elseif self.dir == "e" then
  372.                 self.dir = "s"
  373.             elseif self.dir == "s" then
  374.                 self.dir = "w"
  375.             elseif self.dir == "w" then
  376.                 self.dir = "n"
  377.             end
  378.         end
  379.     end
  380. end
  381. function Movement:uTurn(side)
  382.     local side = side:lower() or "left"
  383.     if type(side) ~= "string" then
  384.         error("uTurn expects a string, currently is " .. type(side), 2)
  385.     elseif side ~= "left" and side ~= "right" then
  386.         error("uTurn expects string to be left or right, currently is " .. side, 2)
  387.     end
  388.     if side == "right" then
  389.         self:right()
  390.         self:forward()
  391.         self:right()
  392.     else
  393.         self:left()
  394.         self:forward()
  395.         self:left()
  396.     end
  397. end
  398. function Movement:face(dir)
  399.     if type(dir) ~= "string" then
  400.         error("turn expects a string, currently is " .. type(dir), 2)
  401.     elseif dir:lower() ~= "e" and dir:lower() ~= "s" and dir:lower() ~= "w" and dir:lower() ~= "n" then
  402.         error("quaryChunk expects a dir (N,E,S,W), currently is " .. type(dir) .. ", " .. dir, 2)
  403.     end
  404.     local dir = dir:lower()
  405.     local degree = 0
  406.     local selfDegree = 0
  407.     local dif
  408.  
  409.     if dir == "s" then
  410.         degree = 90
  411.     elseif dir == "w" then
  412.         degree = 180
  413.     elseif dir == "n" then
  414.         degree = 270
  415.     end
  416.     if self.dir == "s" then
  417.         selfDegree = 90
  418.     elseif self.dir == "w" then
  419.         selfDegree = 180
  420.     elseif self.dir == "n" then
  421.         selfDegree = 270
  422.     end
  423.  
  424.     dif = degree - selfDegree
  425.     if dif < 0 then
  426.         dif = dif + 360
  427.     end
  428.     if dif > 180 then
  429.         while self.dir ~= dir do
  430.             self:left()
  431.         end
  432.     else
  433.         while self.dir ~= dir do
  434.             self:right()
  435.         end
  436.     end
  437. end
  438.  
  439. function Movement:goToX(position, digType, whitelist)
  440.     local position = position
  441.     local digType = digType or "normal"
  442.     local whitelist = whitelist or nil
  443.     local vector = vector.new(0, 0, 0)
  444.     if type(position) ~= "table" and type(position) ~= "number" then
  445.         error("goToX expects a vector or number, currently is " .. type(position), 2)
  446.     elseif type(digType) ~= "string" then
  447.         error("goToX expects digType to be a string, currently is " .. type(digType), 2)
  448.     elseif digType ~= "normal" and digType ~= "dig" and digType ~= "attack" and digType ~= "digAttack" then
  449.         error("goToX type expects type to be 'normal', 'dig', 'attack' or 'digAttack'", 2)
  450.     elseif type(whitelist) ~= "table" and whitelist ~= nil then
  451.         error("goToX whitelist expects to be a table or nil, currently is " .. type(whitelist), 2)
  452.     elseif type(position) == "number" then
  453.         vector.x = position
  454.     else
  455.         vector = position
  456.     end
  457.     if self.pos.x ~= vector.x then
  458.         if self.pos.x > vector.x then
  459.             self:face("w")
  460.         elseif self.pos.x < vector.x then
  461.             self:face("e")
  462.         end
  463.         while self.pos.x ~= vector.x do
  464.             if digType == "dig" then
  465.                 self:digForward(1, whitelist)
  466.             elseif digType == "attack" then
  467.                 self:attackForward()
  468.             elseif digType == "digAttack" then
  469.                 self:digAttackForward(1, whitelist)
  470.             else
  471.                 self:forward()
  472.             end
  473.         end
  474.     end
  475. end
  476. function Movement:goToY(position, digType, whitelist)
  477.     local position = position
  478.     local digType = digType or "normal"
  479.     local whitelist = whitelist or nil
  480.     local yVector = vector.new(0, 0, 0)
  481.     if type(position) ~= "table" and type(position) ~= "number" then
  482.         error("goToY expects a vector or number, currently is " .. type(position), 2)
  483.     elseif type(digType) ~= "string" then
  484.         error("goToY expects digType to be a string, currently is " .. type(digType), 2)
  485.     elseif digType ~= "normal" and digType ~= "dig" and digType ~= "attack" and digType ~= "digAttack" then
  486.         error("goToY digType expects to be 'normal', 'dig', 'attack' or 'digAttack'", 2)
  487.     elseif type(whitelist) ~= "table" and whitelist ~= nil then
  488.         error("goToY whitelist expects to be a table or nil, currently is " .. type(whitelist), 2)
  489.     elseif type(position) == "number" then
  490.         yVector.y = position
  491.     else
  492.         yVector = position
  493.     end
  494.     if self.pos.y ~= yVector.y then
  495.         if self.pos.y < yVector.y then
  496.             self:face("s")
  497.         elseif self.pos.y > yVector.y then
  498.             self:face("n")
  499.         end
  500.         while self.pos.y ~= yVector.y do
  501.             if digType == "dig" then
  502.                 self:digForward(1, whitelist)
  503.             elseif digType == "attack" then
  504.                 self:attackForward()
  505.             elseif digType == "digAttack" then
  506.                 self:digAttackForward(1, whitelist)
  507.             else
  508.                 self:forward()
  509.             end
  510.         end
  511.     end
  512. end
  513. function Movement:goToZ(position, digType, whitelist)
  514.     local position = position
  515.     local digType = digType or "normal"
  516.     local whitelist = whitelist or nil
  517.     local vector = vector.new(0, 0, 0)
  518.     if type(position) ~= "table" and type(position) ~= "number" then
  519.         error("goToZ expects a vector or number, currently is " .. type(position), 2)
  520.     elseif type(digType) ~= "string" then
  521.         error("goToZ expects digType to be a string, currently is " .. type(digType), 2)
  522.     elseif digType ~= "normal" and digType ~= "dig" and digType ~= "attack" and digType ~= "digAttack" then
  523.         error("goToZ type expects type to be 'normal', 'dig', 'attack' or 'digAttack'", 2)
  524.     elseif type(whitelist) ~= "table" and whitelist ~= nil then
  525.         error("goToZ whitelist expects to be a table or nil, currently is " .. type(whitelist), 2)
  526.     elseif type(position) == "number" then
  527.         vector.z = position
  528.     else
  529.         vector = position
  530.     end
  531.     if self.pos.z ~= vector.z then
  532.         if self.pos.z > vector.z then
  533.             while self.pos.z ~= vector.z do
  534.                 if digType == "dig" then
  535.                     self:digDown(1, whitelist)
  536.                 elseif digType == "attack" then
  537.                     self:attackDown()
  538.                 elseif digType == "digAttack" then
  539.                     self:digAttackDown(1, whitelist)
  540.                 else
  541.                     self:down()
  542.                 end
  543.             end
  544.         elseif self.pos.z < vector.z then
  545.             while self.pos.z ~= vector.z do
  546.                 if digType == "dig" then
  547.                     self:digUp(1, whitelist)
  548.                 elseif digType == "attack" then
  549.                     self:attackUp()
  550.                 elseif digType == "digAttack" then
  551.                     self:digAttackUp(1, whitelist)
  552.                 else
  553.                     self:up()
  554.                 end
  555.             end
  556.         end
  557.     end
  558. end
  559. function Movement:goTo(vector, digType)
  560.     local digType = digType or "normal"
  561.     if (type(vector) ~= "table") then
  562.         error("goTo expects a vector or number, currently is " .. type(vector), 2)
  563.     elseif type(digType) ~= "string" then
  564.         error("goTo expects digType to be a string, currently is " .. type(digType), 2)
  565.     elseif digType ~= "normal" and digType ~= "dig" and digType ~= "attack" and digType ~= "digAttack" then
  566.         error("goTo digType expects to be 'normal', 'dig', 'attack' or 'digAttack'", 2)
  567.     else
  568.         self:goToX(vector, digType)
  569.         self:goToY(vector, digType)
  570.         self:goToZ(vector, digType)
  571.     end
  572. end
  573.  
  574. function Movement:readData()
  575.     if not fs.exists("data/movement/position") then
  576.         self:createData()
  577.     else
  578.         local file = fs.open("data/movement/position", "r")
  579.         self.pos.y = file.readLine()
  580.         self.pos.x = file.readLine()
  581.         self.pos.z = file.readLine()
  582.         self.dir = file.readLine()
  583.         h.close()
  584.         return true
  585.     end
  586. end
  587. function Movement:saveData()
  588.     if not fs.exists("data/movement/position") then
  589.         self:createData()
  590.     else
  591.         local file = fs.open("data/movement/position", "w")
  592.         file.writeLine(self.pos.x)
  593.         file.writeLine(self.pos.y)
  594.         file.writeLine(self.pos.z)
  595.         file.writeLine(self.dir)
  596.         file.close()
  597.         return true
  598.     end
  599. end
  600. function Movement:createData()
  601.     io.write("(Save) Couldn't find the data/movement/position file, make one? Y/N")
  602.     answer = io.read()
  603.     if answer:lower() == "y" then
  604.         print("creating data/movement/position file")
  605.         local file = fs.open("data/movement/position", "w")
  606.         file.writeLine(self.pos.x)
  607.         file.writeLine(self.pos.y)
  608.         file.writeLine(self.pos.z)
  609.         file.writeLine(self.dir)
  610.         file.close()
  611.         h.close()
  612.     elseif answer:lower() == "n" then
  613.         error("Couldn't find the data/movement/position and didn't want to make one", 2)
  614.     end
  615. end
Advertisement
Add Comment
Please, Sign In to add comment