Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local H2Turtle = {}
- setmetatable(H2Turtle,{__index = turtle})
- --[[
- turtle.select( slotNum )
- turtle.getSelectedSlot()
- turtle.getItemCount( [slotNum] )
- turtle.getItemSpace( [slotNum] )
- turtle.equipLeft()
- turtle.equipRight()
- turtle.dig( [toolSide] )
- turtle.digUp( [toolSide] )
- turtle.digDown( [toolSide] )
- turtle.place()
- turtle.placeUp()
- turtle.placeDown()
- turtle.attack( [toolSide] )
- turtle.attackUp( [toolSide] )
- turtle.attackDown( [toolSide] )
- turtle.detect()
- turtle.detectUp()
- turtle.detectDown()
- turtle.compare()
- turtle.compareUp()
- turtle.compareDown()
- turtle.compareTo( slotNum )
- turtle.transferTo( slotNum, [quantity] )
- turtle.drop( [quantity] )
- turtle.dropUp( [quantity] )
- turtle.dropDown( [quantity] )
- turtle.suck( [quantity] )
- turtle.suckUp( [quantity] )
- turtle.suckDown( [quantity] )
- turtle.getFuelLevel()
- turtle.getFuelLimit()
- turtle.refuel( [quantity] )
- turtle.craft( [quantity] ) (requires Crafty Turtle)
- --]]
- H2Turtle.new = function()
- local obj = {}
- obj.direction = 0 -- 0-3, 0: start dir., 1: turn right, 2: two times, 3: three times
- obj.x = 0 -- side
- obj.y = 0 -- height
- obj.z = 0 -- depth
- return setmetatable(obj,{__index = H2Turtle})
- end
- H2Turtle.getPos = function(self)
- return self.x, self.y, self.z, self.direction
- end
- H2Turtle.printPos = function(self)
- local tmp_tbl = {}
- print(string.format("x:%d, y:%d, z:%d, dir:%d", self.x, self.y, self.z, self.direction))
- return self.x, self.y, self.z, self.direction
- end
- H2Turtle.turnRight = function(self, _n)
- _n = _n or 1
- if type(_n)=="number" and _n > 0 then
- for i=1,_n do
- turtle.turnRight()
- end
- self.direction = (self.direction + _n)%4
- elseif type(_n)=="number" and _n == 0 then
- -- do nothing.
- else
- assert(false, "Illegal argument: turnRight(n)")
- end
- return self.direction
- end
- H2Turtle.turnLeft = function(self, _n)
- _n = _n or 1
- if type(_n)=="number" and _n > 0 then
- for i=1,_n do
- turtle.turnLeft()
- end
- self.direction = (self.direction + (4-_n))%4
- elseif type(_n)=="number" and _n == 0 then
- -- do nothing.
- else
- assert(false, "Illegal argument: turnLeft(n)")
- end
- return self.direction
- end
- H2Turtle.forward = function(self, n)
- local _forward = function(self)
- local _status, _err_msg
- for i=1,3 do
- _status, _err_msg = turtle.forward()
- if _status or (_err_msg ~= "Movement obstructed") then break end
- os.sleep(0.1)
- end
- if _status then
- if self.direction == 0 then
- self.z = self.z+1
- elseif self.direction == 1 then
- self.x = self.x+1
- elseif self.direction == 2 then
- self.z = self.z-1
- elseif self.direction == 3 then
- self.x = self.x-1
- end
- end
- return _status, _err_msg
- end
- -- main loop
- n = tonumber(n) or 1
- local status, err_msg
- for i=1, n do
- status, err_msg = _forward(self)
- if not status then break end
- end
- return status, err_msg
- end
- H2Turtle.back = function(self, n)
- local _back = function(self)
- local _status, _err_msg
- for i=1,3 do
- _status, _err_msg = turtle.back()
- if _status or (_err_msg ~= "Movement obstructed") then break end
- os.sleep(0.1)
- end
- if _status then
- if self.direction == 0 then
- self.z = self.z-1
- elseif self.direction == 1 then
- self.x = self.x-1
- elseif self.direction == 2 then
- self.z = self.z+1
- elseif self.direction == 3 then
- self.x = self.x+1
- end
- end
- return _status, _err_msg
- end
- -- main loop
- n = tonumber(n) or 1
- local status, err_msg
- for i=1, n do
- status, err_msg = _back(self)
- if not status then break end
- end
- return status, err_msg
- end
- H2Turtle.up = function(self, n)
- local _up = function(self)
- local _status, _err_msg
- for i=1,3 do
- _status, _err_msg = turtle.up()
- if _status or (_err_msg ~= "Movement obstructed") then break end
- os.sleep(0.1)
- end
- if _status then
- self.y = self.y+1
- end
- return _status, _err_msg
- end
- -- main loop
- n = tonumber(n) or 1
- local status, err_msg
- for i=1, n do
- status, err_msg = _up(self)
- if not status then break end
- end
- return status, err_msg
- end
- H2Turtle.down = function(self, n)
- local _down = function(self)
- local _status, _err_msg
- for i=1,3 do
- _status, _err_msg = turtle.down()
- if _status or (_err_msg ~= "Movement obstructed") then break end
- os.sleep(0.1)
- end
- if _status then
- self.y = self.y-1
- end
- return _status, _err_msg
- end
- -- main loop
- n = tonumber(n) or 1
- local status, err_msg
- for i=1, n do
- status, err_msg = _down(self)
- if not status then break end
- end
- return status, err_msg
- end
- H2Turtle.select = function(self, slot)
- return turtle.select(slot)
- end
- H2Turtle.selectRelative = function(self, _delta)
- _delta = _delta or 0
- if type(_delta)=="number" then
- local n = turtle.getSelectedSlot() + _delta
- n = ((n-1) % 16) + 1 -- 1 < n < 16
- return self:select(n)
- end
- end
- local function makeSelectedFunc(func)
- return function(self, ...)
- local arg1 = ...
- if type(arg1)=="number" and args[1]>0 and args[1]<17 then
- self:selct(args[1])
- return func(select(2,...))
- end
- return func(...)
- end
- end
- H2Turtle.place = makeSelectedFunc(turtle.place)
- H2Turtle.placeUp = makeSelectedFunc(turtle.placeUp)
- H2Turtle.placeDown = makeSelectedFunc(turtle.placeDown)
- H2Turtle.dig = makeSelectedFunc(turtle.dig)
- H2Turtle.digUp = makeSelectedFunc(turtle.digUp)
- H2Turtle.digDown = makeSelectedFunc(turtle.digDown)
- local function makeSurelyDigFunc(func, sleep_time)
- return function()
- local status, err = func()
- repeat
- if sleep_time then os.sleep(sleep_time) end
- until not (status and func())
- return status, err
- end
- end
- H2Turtle.surelyDig = makeSelectedFunc(makeSurelyDigFunc(turtle.dig))
- H2Turtle.surelyDigUp = makeSelectedFunc(makeSurelyDigFunc(turtle.digUp, 0.4))
- H2Turtle.surelyDigDown = makeSelectedFunc(turtle.digDown)
- H2Turtle.surelyForward = function(self, n)
- local fwd = function()
- while not self:forward() do
- while turtle.detect() do self:dig() end
- if self:forward() then break end
- if not turtle.attack() then
- os.sleep(5)
- if self:forward() then break end
- else
- while turtle.attack() do end
- end
- end
- end
- for i=1,n do
- fwd()
- end
- end
- -- shortened name
- --[[
- H2Turtle.fwd = H2Turle.forward
- H2Turtle.tL = H2Turtle.turnLeft
- H2Turtle.tR = H2Turtle.turnRight
- H2Turtle.sel = H2Turtle.select
- H2Turtle.selRel = H2Turtle.selectRelative
- --]]
- -- ##################################
- -- API Functions
- function new()
- return H2Turtle.new()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement