G2A Many GEOs
SHARE
TWEET

myturtle(API)

kssr3951 Mar 2nd, 2015 (edited) 223 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- -------------------------------
  2. -- my turtle(API)
  3. -- -------------------------------
  4. -- ---------------
  5. -- Move/Dig Functions
  6. -- ---------------
  7. function surelyUp()
  8.   while not turtle.up() do turtle.digUp() end
  9. end
  10. function surelyDown()
  11.   while not turtle.down() do turtle.digDown() end
  12. end
  13. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  14. function surelyDigUp()
  15.   while turtle.digUp() do
  16.     os.sleep(0.4)
  17.   end
  18. end
  19. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  20. function surelyDig()
  21.   while turtle.dig() do end
  22. end
  23. -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
  24. function surelyFwd()
  25.   for i=1,4 do
  26.     local status, err = turtle.forward()
  27.     if status then
  28.       return true  -- success!
  29.     elseif err=="Out of fuel" then
  30.       return status, err
  31.     end
  32.  
  33.     surelyDig() -- face to a normal block or a sand(gravel) hill
  34.  
  35.     if turtle.detect() and not turtle.dig() then
  36.       return false, "bedrock!"
  37.     end
  38.  
  39.     if turtle.forward() then return true end -- success!
  40.  
  41.     if turtle.attack() then
  42.       -- face to monster-mob
  43.       while turtle.attack() do end
  44.     else
  45.       -- face to sand-blocks which is dropping long time
  46.       os.sleep(5) -- probably, adjustment is required
  47.     end
  48.   end
  49.   return turtle.forward()
  50. end
  51. -- ---------------
  52. -- vacuum
  53. -- ---------------
  54. function vacuum(slotNo)
  55.   local result = false
  56.   turtle.select(slotNo)
  57.   for i = 1, 16 do
  58.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  59.       turtle.select(i)
  60.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  61.       result = true
  62.       if 0 == turtle.getItemSpace(slotNo) then
  63.         break
  64.       end
  65.       turtle.select(slotNo)
  66.     end
  67.   end
  68.   turtle.select(slotNo)
  69.   return result
  70. end
  71. -- ---------------
  72. -- refuelPrompt
  73. -- ---------------
  74. function refuelPrompt(moveFunc)
  75.   local firstTime = true;
  76.   while true do
  77.     local rslt, msg;
  78.     if nil ~= moveFunc then
  79.       rslt, msg = moveFunc();
  80.     else
  81.       if firstTime then
  82.         firstTime = false;
  83.         rslt = false;
  84.         msg  = "Out of fuel";
  85.       else
  86.         return true;
  87.       end
  88.     end
  89.     if not rslt and msg == "Out of fuel" then
  90.       print("Out of fuel. please select [r]efuel or [q]uit.");
  91.       print("(r/q)");
  92.       local ch = read();
  93.       if "q" == ch then
  94.         print("Are you sure you want to stop the script?");
  95.         print("(y/n)");
  96.         ch = read();
  97.         if "y" == ch then
  98.           error("out of fuel.");
  99.         end
  100.       elseif "r" == ch then
  101.         local selSlot = 1;
  102.         while true do
  103.           print("Please type current selected slot number.");
  104.           ch = read();
  105.           local num = tonumber(ch);
  106.           if nil ~= num and 1 <= num and num <= 16 then
  107.             selSlot = num;
  108.             break;
  109.           else
  110.             print("Wrong input. Please retry.");
  111.           end
  112.         end
  113.         while true do
  114.           print("please set fuel item and type slot number.");
  115.           print("When you finish refueling, Please type q.");
  116.           print("FuelLevel = " .. turtle.getFuelLevel());
  117.           ch = read();
  118.           local num = tonumber(ch);
  119.           if nil ~= num and 1 <= num and num <= 16 then
  120.             turtle.select(num);
  121.             local fRslt, fMsg = turtle.refuel();
  122.             if not fRslt then
  123.               print(fMsg);
  124.             end
  125.           elseif "q" == ch then
  126.             turtle.select(selSlot);
  127.             break;
  128.           else
  129.             print("Wrong input. Please retry.");
  130.           end
  131.         end
  132.       end
  133.     else
  134.       return rslt, msg;
  135.     end
  136.   end
  137. end
  138. -- ---------------
  139. -- (common) direction code
  140. -- ---------------
  141. DIR_FORWARD = 1
  142. DIR_RIGHT   = 2
  143. DIR_BACK    = 3
  144. DIR_LEFT    = 4
  145. DIR_NOT     = 5
  146. -- ---------------
  147. -- (common) turn code
  148. -- ---------------
  149. TURN_NOT     = 1
  150. TURN_LEFT    = 2
  151. TURN_RIGHT   = 3
  152. TURN_OPPOSIT = 4
  153. -- ---------------
  154. -- (common) turn operation
  155. -- ---------------
  156. TURN_OPERATION = {
  157.   [DIR_FORWARD] = { [TURN_LEFT] = DIR_LEFT   , [TURN_RIGHT] = DIR_RIGHT  , [TURN_OPPOSIT] = DIR_BACK    },
  158.   [DIR_LEFT   ] = { [TURN_LEFT] = DIR_BACK   , [TURN_RIGHT] = DIR_FORWARD, [TURN_OPPOSIT] = DIR_RIGHT   },
  159.   [DIR_RIGHT  ] = { [TURN_LEFT] = DIR_FORWARD, [TURN_RIGHT] = DIR_BACK   , [TURN_OPPOSIT] = DIR_LEFT    },
  160.   [DIR_BACK   ] = { [TURN_LEFT] = DIR_RIGHT  , [TURN_RIGHT] = DIR_LEFT   , [TURN_OPPOSIT] = DIR_FORWARD }
  161. }
  162. -- ---------------
  163. -- MyTurtle
  164. -- ---------------
  165. MyTurtle = {}
  166. MyTurtle.new = function(initialY)
  167.     local obj = {}
  168.     obj.turtleX = 0
  169.     obj.turtleZ = 0
  170.     obj.turtleY = initialY
  171.     obj.turtleDir = DIR_FORWARD
  172.     return setmetatable(obj, {__index = MyTurtle})
  173.   end
  174. MyTurtle.copy = function(myTSrc)
  175.     local obj = {}
  176.     obj.turtleX = myTSrc.turtleX
  177.     obj.turtleZ = myTSrc.turtleZ
  178.     obj.turtleY = myTSrc.turtleY
  179.     obj.turtleDir = myTSrc.turtleDir
  180.     return setmetatable(obj, {__index = MyTurtle})
  181.   end
  182. MyTurtle.turnTo = function(self, direction)
  183.     local TURN_RULES = { -- dir_forward, dir_right, dir_back, dir_left
  184.       [DIR_FORWARD] = { TURN_NOT    , TURN_RIGHT  , TURN_OPPOSIT, TURN_LEFT    },
  185.       [DIR_RIGHT  ] = { TURN_LEFT   , TURN_NOT    , TURN_RIGHT  , TURN_OPPOSIT },
  186.       [DIR_BACK   ] = { TURN_OPPOSIT, TURN_LEFT   , TURN_NOT    , TURN_RIGHT   },
  187.       [DIR_LEFT   ] = { TURN_RIGHT  , TURN_OPPOSIT, TURN_LEFT   , TURN_NOT     }}
  188.     local turn = TURN_RULES[self.turtleDir][direction]
  189.     if TURN_LEFT == turn then
  190.       if not SIMULATION then
  191.         turtle.turnLeft()
  192.       end
  193.     elseif TURN_RIGHT == turn then
  194.       if not SIMULATION then
  195.         turtle.turnRight()
  196.       end
  197.     elseif TURN_OPPOSIT == turn then
  198.       if not SIMULATION then
  199.         turtle.turnLeft()
  200.         turtle.turnLeft()
  201.       end
  202.     end
  203.     self.turtleDir = direction
  204.   end
  205. MyTurtle.adjustX = function(self, aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  206.     if self.turtleX < aimX and DIR_RIGHT ~= self.turtleDir then
  207.       self:turnTo(DIR_RIGHT)
  208.     elseif aimX < self.turtleX and DIR_LEFT ~= self.turtleDir then
  209.       self:turnTo(DIR_LEFT)
  210.     end
  211.     local function sign(val)
  212.       if val == 0 then
  213.         return 0
  214.       else
  215.         return math.abs(val) / val
  216.       end
  217.     end
  218.     while self.turtleX ~= aimX do
  219.       if not SIMULATION then
  220.         surelyFwd()
  221.         if withDigUp then
  222.           surelyDigUp()
  223.         end
  224.       end
  225.       self.turtleX = self.turtleX + sign(aimX - self.turtleX)
  226.     end
  227.   end
  228. MyTurtle.adjustZ = function(self, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  229.     if self.turtleZ < aimZ and DIR_FORWARD ~= self.turtleDir then
  230.       self:turnTo(DIR_FORWARD)
  231.     elseif aimZ < self.turtleZ and DIR_BACK ~= self.turtleDir then
  232.       self:turnTo(DIR_BACK)
  233.     end
  234.     local function sign(val)
  235.       if val == 0 then
  236.         return 0
  237.       else
  238.         return math.abs(val) / val
  239.       end
  240.     end
  241.     while self.turtleZ ~= aimZ do
  242.       if not SIMULATION then
  243.         surelyFwd()
  244.         if withDigUp then
  245.           surelyDigUp()
  246.         end
  247.       end
  248.       self.turtleZ = self.turtleZ + sign(aimZ - self.turtleZ)
  249.     end
  250.   end
  251. MyTurtle.moveTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  252.     self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  253.     self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  254.   end
  255. MyTurtle.backTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  256.     self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
  257.     self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
  258.   end
  259. MyTurtle.adjustY = function(self, aimY)
  260.     if self.turtleY < aimY then
  261.       while self.turtleY ~= aimY do
  262.         surelyDigUp()
  263.         surelyUp()
  264.         self.turtleY = self.turtleY + 1
  265.       end
  266.     elseif aimY < self.turtleY then
  267.       while self.turtleY ~= aimY do
  268.         turtle.digDown()
  269.         surelyDown()
  270.         self.turtleY = self.turtleY - 1
  271.       end
  272.     end
  273.   end
  274. MyTurtle.pos = function(self)
  275.     return { y = self.turtleY, x = self.turtleX, z = self.turtleZ }
  276.   end
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top