Advertisement
hevohevo

h2turtle API version0.2

Jul 2nd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.81 KB | None | 0 0
  1. local H2Turtle = {}
  2. setmetatable(H2Turtle,{__index = turtle})
  3.  
  4. --[[
  5. turtle.select( slotNum )
  6. turtle.getSelectedSlot()
  7. turtle.getItemCount( [slotNum] )
  8. turtle.getItemSpace( [slotNum] )
  9. turtle.equipLeft()
  10. turtle.equipRight()
  11. turtle.dig( [toolSide] )
  12. turtle.digUp( [toolSide] )
  13. turtle.digDown( [toolSide] )
  14. turtle.place()
  15. turtle.placeUp()
  16. turtle.placeDown()
  17. turtle.attack( [toolSide] )
  18. turtle.attackUp( [toolSide] )
  19. turtle.attackDown( [toolSide] )
  20. turtle.detect()
  21. turtle.detectUp()
  22. turtle.detectDown()
  23. turtle.compare()
  24. turtle.compareUp()
  25. turtle.compareDown()
  26. turtle.compareTo( slotNum )
  27. turtle.transferTo( slotNum, [quantity] )
  28. turtle.drop( [quantity] )
  29. turtle.dropUp( [quantity] )
  30. turtle.dropDown( [quantity] )
  31. turtle.suck( [quantity] )
  32. turtle.suckUp( [quantity] )
  33. turtle.suckDown( [quantity] )
  34. turtle.getFuelLevel()
  35. turtle.getFuelLimit()
  36. turtle.refuel( [quantity] )
  37. turtle.craft( [quantity] ) (requires Crafty Turtle)
  38. --]]
  39.  
  40. H2Turtle.new = function()
  41.   local obj = {}
  42.   obj.direction = 0 -- 0-3, 0: start dir., 1: turn right, 2: two times, 3: three times
  43.   obj.x = 0 -- side
  44.   obj.y = 0 -- height
  45.   obj.z = 0 -- depth
  46.   return setmetatable(obj,{__index = H2Turtle})
  47. end
  48.  
  49. H2Turtle.getPos = function(self)
  50.   return self.x, self.y, self.z, self.direction
  51. end
  52.  
  53. H2Turtle.printPos = function(self)
  54.   local tmp_tbl = {}
  55.   print(string.format("x:%d, y:%d, z:%d, dir:%d", self.x, self.y, self.z, self.direction))
  56.   return self.x, self.y, self.z, self.direction
  57. end
  58.  
  59.  
  60. H2Turtle.turnRight = function(self, _n)
  61.   _n = _n or 1
  62.   if type(_n)=="number" and _n > 0 then
  63.     for i=1,_n do
  64.       turtle.turnRight()
  65.     end
  66.     self.direction = (self.direction + _n)%4
  67.   elseif type(_n)=="number" and _n == 0 then
  68.     -- do nothing.
  69.   else
  70.     assert(false, "Illegal argument: turnRight(n)")
  71.   end
  72.   return self.direction
  73. end
  74.  
  75. H2Turtle.turnLeft = function(self, _n)
  76.   _n = _n or 1
  77.   if type(_n)=="number" and _n > 0 then
  78.     for i=1,_n do
  79.       turtle.turnLeft()
  80.     end
  81.     self.direction = (self.direction + (4-_n))%4
  82.   elseif type(_n)=="number" and _n == 0 then
  83.     -- do nothing.
  84.   else
  85.     assert(false, "Illegal argument: turnLeft(n)")
  86.   end
  87.   return self.direction
  88. end
  89.  
  90. H2Turtle.forward = function(self, n)
  91.   local _forward = function(self)
  92.     local _status, _err_msg
  93.     for i=1,3 do
  94.       _status, _err_msg = turtle.forward()
  95.       if _status or (_err_msg ~= "Movement obstructed") then break end
  96.       os.sleep(0.1)
  97.     end
  98.     if _status then
  99.       if self.direction == 0 then
  100.         self.z = self.z+1
  101.       elseif self.direction == 1 then
  102.         self.x = self.x+1
  103.       elseif self.direction == 2 then
  104.         self.z = self.z-1
  105.       elseif self.direction == 3 then
  106.         self.x = self.x-1
  107.       end
  108.     end
  109.     return _status, _err_msg
  110.   end
  111.  
  112.   -- main loop
  113.   n = tonumber(n) or 1
  114.   local status, err_msg
  115.   for i=1, n do
  116.     status, err_msg = _forward(self)
  117.     if not status then break end
  118.   end
  119.   return status, err_msg
  120. end
  121.  
  122. H2Turtle.back = function(self, n)
  123.   local _back = function(self)
  124.     local _status, _err_msg
  125.     for i=1,3 do
  126.       _status, _err_msg = turtle.back()
  127.       if _status or (_err_msg ~= "Movement obstructed") then break end
  128.       os.sleep(0.1)
  129.     end
  130.     if _status then
  131.       if self.direction == 0 then
  132.         self.z = self.z-1
  133.       elseif self.direction == 1 then
  134.         self.x = self.x-1
  135.       elseif self.direction == 2 then
  136.         self.z = self.z+1
  137.       elseif self.direction == 3 then
  138.         self.x = self.x+1
  139.       end
  140.     end
  141.     return _status, _err_msg
  142.   end
  143.  
  144.   -- main loop
  145.   n = tonumber(n) or 1
  146.   local status, err_msg
  147.   for i=1, n do
  148.     status, err_msg = _back(self)
  149.     if not status then break end
  150.   end
  151.   return status, err_msg  
  152. end
  153.  
  154. H2Turtle.up = function(self, n)
  155.   local _up = function(self)
  156.     local _status, _err_msg
  157.     for i=1,3 do
  158.       _status, _err_msg = turtle.up()
  159.       if _status or (_err_msg ~= "Movement obstructed") then break end
  160.       os.sleep(0.1)
  161.     end
  162.     if _status then
  163.         self.y = self.y+1
  164.     end
  165.     return _status, _err_msg
  166.   end
  167.  
  168.   -- main loop
  169.   n = tonumber(n) or 1
  170.   local status, err_msg
  171.   for i=1, n do
  172.     status, err_msg = _up(self)
  173.     if not status then break end
  174.   end
  175.   return status, err_msg
  176. end
  177.  
  178. H2Turtle.down = function(self, n)
  179.   local _down = function(self)
  180.     local _status, _err_msg
  181.     for i=1,3 do
  182.       _status, _err_msg = turtle.down()
  183.       if _status or (_err_msg ~= "Movement obstructed") then break end
  184.       os.sleep(0.1)
  185.     end
  186.     if _status then
  187.         self.y = self.y-1
  188.     end
  189.     return _status, _err_msg
  190.   end
  191.  
  192.   -- main loop
  193.   n = tonumber(n) or 1
  194.   local status, err_msg
  195.   for i=1, n do
  196.     status, err_msg = _down(self)
  197.     if not status then break end
  198.   end
  199.   return status, err_msg
  200. end
  201.  
  202.  
  203. H2Turtle.select = function(self, slot)
  204.   return turtle.select(slot)
  205. end
  206.  
  207. H2Turtle.selectRelative = function(self, _delta)
  208.   _delta = _delta or 0
  209.   if type(_delta)=="number" then
  210.     local n = turtle.getSelectedSlot() + _delta
  211.     n = ((n-1) % 16) + 1 -- 1 < n < 16
  212.     return self:select(n)
  213.   end
  214. end
  215.  
  216. local function makeSelectedFunc(func)
  217.   return function(self, ...)
  218.     local arg1 = ...
  219.     if type(arg1)=="number" and args[1]>0 and args[1]<17 then
  220.       self:selct(args[1])
  221.       return func(select(2,...))
  222.     end
  223.     return func(...)
  224.   end
  225. end
  226. H2Turtle.place = makeSelectedFunc(turtle.place)
  227. H2Turtle.placeUp = makeSelectedFunc(turtle.placeUp)
  228. H2Turtle.placeDown = makeSelectedFunc(turtle.placeDown)
  229. H2Turtle.dig = makeSelectedFunc(turtle.dig)
  230. H2Turtle.digUp = makeSelectedFunc(turtle.digUp)
  231. H2Turtle.digDown = makeSelectedFunc(turtle.digDown)
  232.  
  233. local function makeSurelyDigFunc(func, sleep_time)
  234.   return function()
  235.     local status, err = func()
  236.     repeat
  237.       if sleep_time then os.sleep(sleep_time) end
  238.     until not (status and func())
  239.     return status, err
  240.   end
  241. end
  242. H2Turtle.surelyDig = makeSelectedFunc(makeSurelyDigFunc(turtle.dig))
  243. H2Turtle.surelyDigUp = makeSelectedFunc(makeSurelyDigFunc(turtle.digUp, 0.4))
  244. H2Turtle.surelyDigDown = makeSelectedFunc(turtle.digDown)
  245.  
  246. H2Turtle.surelyForward = function(self, n)
  247.   local fwd = function()
  248.     while not self:forward() do
  249.       while turtle.detect() do self:dig() end
  250.       if self:forward() then break end
  251.       if not turtle.attack() then
  252.         os.sleep(5)
  253.         if self:forward() then break end
  254.       else
  255.         while turtle.attack() do end
  256.       end
  257.     end
  258.   end
  259.  
  260.   for i=1,n do
  261.     fwd()
  262.   end
  263. end
  264.  
  265. -- shortened name
  266. --[[
  267. H2Turtle.fwd = H2Turle.forward
  268. H2Turtle.tL = H2Turtle.turnLeft
  269. H2Turtle.tR = H2Turtle.turnRight
  270. H2Turtle.sel = H2Turtle.select
  271. H2Turtle.selRel = H2Turtle.selectRelative
  272. --]]
  273.  
  274. -- ##################################
  275. -- API Functions
  276. function new()
  277.   return H2Turtle.new()
  278. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement