Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- -------------------------------
- -- my turtle(API)
- -- -------------------------------
- -- ---------------
- -- Move/Dig Functions
- -- ---------------
- function surelyUp()
- while not turtle.up() do turtle.digUp() end
- end
- function surelyDown()
- while not turtle.down() do turtle.digDown() end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDigUp()
- while turtle.digUp() do
- os.sleep(0.4)
- end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyDig()
- while turtle.dig() do end
- end
- -- http://hevohevo.hatenablog.com/entry/2014/07/14/213109
- function surelyFwd()
- for i=1,4 do
- local status, err = turtle.forward()
- if status then
- return true -- success!
- elseif err=="Out of fuel" then
- return status, err
- end
- surelyDig() -- face to a normal block or a sand(gravel) hill
- if turtle.detect() and not turtle.dig() then
- return false, "bedrock!"
- end
- if turtle.forward() then return true end -- success!
- if turtle.attack() then
- -- face to monster-mob
- while turtle.attack() do end
- else
- -- face to sand-blocks which is dropping long time
- os.sleep(5) -- probably, adjustment is required
- end
- end
- return turtle.forward()
- end
- -- ---------------
- -- vacuum
- -- ---------------
- function vacuum(slotNo)
- local result = false
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
- turtle.select(i)
- turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
- result = true
- if 0 == turtle.getItemSpace(slotNo) then
- break
- end
- turtle.select(slotNo)
- end
- end
- turtle.select(slotNo)
- return result
- end
- -- ---------------
- -- refuelPrompt
- -- ---------------
- function refuelPrompt(moveFunc)
- local firstTime = true;
- while true do
- local rslt, msg;
- if nil ~= moveFunc then
- rslt, msg = moveFunc();
- else
- if firstTime then
- firstTime = false;
- rslt = false;
- msg = "Out of fuel";
- else
- return true;
- end
- end
- if not rslt and msg == "Out of fuel" then
- print("Out of fuel. please select [r]efuel or [q]uit.");
- print("(r/q)");
- local ch = read();
- if "q" == ch then
- print("Are you sure you want to stop the script?");
- print("(y/n)");
- ch = read();
- if "y" == ch then
- error("out of fuel.");
- end
- elseif "r" == ch then
- local selSlot = 1;
- while true do
- print("Please type current selected slot number.");
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- selSlot = num;
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- while true do
- print("please set fuel item and type slot number.");
- print("When you finish refueling, Please type q.");
- print("FuelLevel = " .. turtle.getFuelLevel());
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- turtle.select(num);
- local fRslt, fMsg = turtle.refuel();
- if not fRslt then
- print(fMsg);
- end
- elseif "q" == ch then
- turtle.select(selSlot);
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- end
- else
- return rslt, msg;
- end
- end
- end
- -- ---------------
- -- (common) direction code
- -- ---------------
- DIR_FORWARD = 1
- DIR_RIGHT = 2
- DIR_BACK = 3
- DIR_LEFT = 4
- DIR_NOT = 5
- -- ---------------
- -- (common) turn code
- -- ---------------
- TURN_NOT = 1
- TURN_LEFT = 2
- TURN_RIGHT = 3
- TURN_OPPOSIT = 4
- -- ---------------
- -- (common) turn operation
- -- ---------------
- TURN_OPERATION = {
- [DIR_FORWARD] = { [TURN_LEFT] = DIR_LEFT , [TURN_RIGHT] = DIR_RIGHT , [TURN_OPPOSIT] = DIR_BACK },
- [DIR_LEFT ] = { [TURN_LEFT] = DIR_BACK , [TURN_RIGHT] = DIR_FORWARD, [TURN_OPPOSIT] = DIR_RIGHT },
- [DIR_RIGHT ] = { [TURN_LEFT] = DIR_FORWARD, [TURN_RIGHT] = DIR_BACK , [TURN_OPPOSIT] = DIR_LEFT },
- [DIR_BACK ] = { [TURN_LEFT] = DIR_RIGHT , [TURN_RIGHT] = DIR_LEFT , [TURN_OPPOSIT] = DIR_FORWARD }
- }
- -- ---------------
- -- MyTurtle
- -- ---------------
- MyTurtle = {}
- MyTurtle.new = function(initialY)
- local obj = {}
- obj.turtleX = 0
- obj.turtleZ = 0
- obj.turtleY = initialY
- obj.turtleDir = DIR_FORWARD
- return setmetatable(obj, {__index = MyTurtle})
- end
- MyTurtle.copy = function(myTSrc)
- local obj = {}
- obj.turtleX = myTSrc.turtleX
- obj.turtleZ = myTSrc.turtleZ
- obj.turtleY = myTSrc.turtleY
- obj.turtleDir = myTSrc.turtleDir
- return setmetatable(obj, {__index = MyTurtle})
- end
- MyTurtle.turnTo = function(self, direction)
- local TURN_RULES = { -- dir_forward, dir_right, dir_back, dir_left
- [DIR_FORWARD] = { TURN_NOT , TURN_RIGHT , TURN_OPPOSIT, TURN_LEFT },
- [DIR_RIGHT ] = { TURN_LEFT , TURN_NOT , TURN_RIGHT , TURN_OPPOSIT },
- [DIR_BACK ] = { TURN_OPPOSIT, TURN_LEFT , TURN_NOT , TURN_RIGHT },
- [DIR_LEFT ] = { TURN_RIGHT , TURN_OPPOSIT, TURN_LEFT , TURN_NOT }}
- local turn = TURN_RULES[self.turtleDir][direction]
- if TURN_LEFT == turn then
- if not SIMULATION then
- turtle.turnLeft()
- end
- elseif TURN_RIGHT == turn then
- if not SIMULATION then
- turtle.turnRight()
- end
- elseif TURN_OPPOSIT == turn then
- if not SIMULATION then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- self.turtleDir = direction
- end
- MyTurtle.adjustX = function(self, aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- if self.turtleX < aimX and DIR_RIGHT ~= self.turtleDir then
- self:turnTo(DIR_RIGHT)
- elseif aimX < self.turtleX and DIR_LEFT ~= self.turtleDir then
- self:turnTo(DIR_LEFT)
- end
- local function sign(val)
- if val == 0 then
- return 0
- else
- return math.abs(val) / val
- end
- end
- while self.turtleX ~= aimX do
- if not SIMULATION then
- surelyFwd()
- if withDigUp then
- surelyDigUp()
- end
- end
- self.turtleX = self.turtleX + sign(aimX - self.turtleX)
- end
- end
- MyTurtle.adjustZ = function(self, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- if self.turtleZ < aimZ and DIR_FORWARD ~= self.turtleDir then
- self:turnTo(DIR_FORWARD)
- elseif aimZ < self.turtleZ and DIR_BACK ~= self.turtleDir then
- self:turnTo(DIR_BACK)
- end
- local function sign(val)
- if val == 0 then
- return 0
- else
- return math.abs(val) / val
- end
- end
- while self.turtleZ ~= aimZ do
- if not SIMULATION then
- surelyFwd()
- if withDigUp then
- surelyDigUp()
- end
- end
- self.turtleZ = self.turtleZ + sign(aimZ - self.turtleZ)
- end
- end
- MyTurtle.moveTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- end
- MyTurtle.backTo = function(self, aimX, aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustX(aimX, withDigUp, beforeEvHandlers, afterEvHandlers)
- self:adjustZ(aimZ, withDigUp, beforeEvHandlers, afterEvHandlers)
- end
- MyTurtle.adjustY = function(self, aimY)
- if self.turtleY < aimY then
- while self.turtleY ~= aimY do
- surelyDigUp()
- surelyUp()
- self.turtleY = self.turtleY + 1
- end
- elseif aimY < self.turtleY then
- while self.turtleY ~= aimY do
- turtle.digDown()
- surelyDown()
- self.turtleY = self.turtleY - 1
- end
- end
- end
- MyTurtle.pos = function(self)
- return { y = self.turtleY, x = self.turtleX, z = self.turtleZ }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement