Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ***********
- * prototype::move
- *
- * This is a prototype file for turtle's moving.
- *
- * generally, call this by dofile()
- * and use it in a basic object-based coding of lua.
- *
- **********
- ]]
- ----------
- -- a frame of the prototype
- ----------
- Move = {}
- ----------
- -- constructer
- ----------
- function Move.new()
- -- need modem.
- local rightP = peripheral.getType('right')
- local rightL = peripheral.getType('left')
- if rightP ~= 'modem' and rightL ~= 'modem' then
- return false, 'need modem.'
- end
- -- create coordinates.
- local cords = {}
- cords[1] = 0 -- x
- cords[2] = 0 -- y
- cords[3] = 0 -- z
- cords[4] = 0 -- direction. 0 is +z (south). clock. 1 is -x.
- -- create obj.
- local obj = {cords = cords, initialPos = cords}
- -- set metatable.
- local new = setmetatable(obj, {__index = Move})
- -- get current position.
- local flag, opt = new:currentPos()
- --GPS error.
- if flag == false then
- return false, opt
- end
- -- create position file if it doesn't exist.
- if fs.exists("initialPos") == false then
- local h = fs.open("initialPos", "w")
- h.writeLine("initialPos = {"..new.cords[1]..","..new.cords[2]..","..new.cords[3]..","..new.cords[4].."}")
- h.flush()
- h.close()
- end
- -- set initial position.
- dofile("initialPos")
- new.initialPos = initialPos
- return new
- end
- ----------
- -- method for getting the current turtle's position.
- -- this uses GPS system. So, it returns false and error rmessage in non-GPS environment.
- ----------
- function Move:currentPos()
- --get first position.
- local pos1 = {gps.locate(30)}
- if pos1 == nil then
- return false, "Can't connect to GPS, and failed to get pos1 in Move:currentPos()."
- end
- --try to move to get second position.
- local flag1 = true
- for i=1, 4, 1 do
- if turtle.detect() == false then
- flag1 = false
- break
- end
- turtle.turnLeft()
- end
- local flag, msg = self:moveSure()
- if flag == false then
- return false, "Can't move, and failed to get pos2 in Move:currentPos()."
- end
- --get second position.
- local pos2 = {gps.locate(30)}
- if pos2 == nil then
- return false, "Can't connect to GPS, and failed to get pos2 in Move:currentPos()."
- end
- --confirm direction and set cords.
- local distX = pos2[1] - pos1[1]
- local distZ = pos2[3] - pos1[3]
- if distX == 1 then
- self.cords[4] = 3
- elseif distX == -1 then
- self.cords[4] = 1
- elseif distZ == 1 then
- self.cords[4] = 0
- elseif distZ == -1 then
- self.cords[4] = 2
- end
- self.cords[1] = pos2[1]
- self.cords[2] = pos2[2]
- self.cords[3] = pos2[3]
- -- return to first pos.
- self:turnLeft()
- self:turnLeft()
- local flag, msg = self:moveSure()
- if flag == false then
- return false, "Can't move, and failed to return to pos1 in Move:currentPos()."
- end
- self:turnLeft()
- self:turnLeft()
- return true
- end
- ----------
- -- methods for moving.
- ----------
- function Move:turnLeft()
- self.cords[4] = (self.cords[4] - 1) % 4
- if turtle.turnLeft() == false then
- self.cords[4] = (self.cords[4] + 1) % 4
- return false
- end
- -- self:dispCords()
- return true
- end
- function Move:turnRight()
- self.cords[4] = (self.cords[4] + 1) % 4
- if turtle.turnRight() == false then
- self.cords[4] = (self.cords[4] - 1) % 4
- return false
- end
- -- self:dispCords()
- return true
- end
- function Move:forward()
- local sign, way = self:getFacingDir()
- self.cords[way] = self.cords[way] + sign
- if turtle.forward() == false then
- self.cords[way] = self.cords[way] - sign
- return false
- end
- -- self:dispCords()
- return true
- end
- function Move:back()
- local sign, way = self:getFacingDir()
- self.cords[way] = self.cords[way] - sign
- if turtle.back() == false then
- self.cords[way] = self.cords[way] + sign
- return false
- end
- -- self:dispCords()
- return true
- end
- function Move:up()
- self.cords[2] = (self.cords[2] + 1)
- if turtle.up() == false then
- self.cords[2] = (self.cords[2] - 1)
- return false
- end
- -- self:dispCords()
- return true
- end
- function Move:down()
- self.cords[2] = (self.cords[2] - 1)
- if turtle.down() == false then
- self.cords[2] = (self.cords[2] + 1)
- return false
- end
- -- self:dispCords()
- return true
- end
- ----------
- -- method for safe moving.
- ----------
- -- remove mob, and move.
- function Move:moveSafe(dir)
- local moveFunc = self.forward
- local attackFunc = turtle.attack
- if dir == 'up' then
- moveFunc = self.up
- attackFunc = turtle.attackUp
- elseif dir == 'down' then
- moveFunc = self.down
- attackFunc = turtle.attackDown
- end
- local times = 0
- while moveFunc(self) == false do
- attackFunc()
- times = times + 1
- if times == 20 then
- return false, "moving obstructed by unkown."
- end
- end
- return true
- end
- -- remove mob and block, and move.
- function Move:moveSure(dir)
- local moveFunc = self.forward
- local detectFunc = turtle.detect
- local digFunc = turtle.dig
- local attackFunc = turtle.attack
- if dir == 'up' then
- moveFunc = self.up
- detectFunc = turtle.detectUp
- digFunc = turtle.digUp
- attackFunc = turtle.attackUp
- elseif dir == 'down' then
- moveFunc = self.down
- detectFunc = turtle.detectDown
- digFunc = turtle.digDown
- attackFunc = turtle.attackDown
- end
- local times = 0
- while detectFunc() do
- digFunc()
- times = times + 1
- if times == 20 then
- return false, "moving obstructed by block."
- end
- sleep(1)
- end
- times = 0
- while moveFunc(self) == false do
- attackFunc()
- times = times + 1
- if times == 20 then
- return false, "moving obstructed by unkown."
- end
- end
- return true
- end
- ----------
- -- method for getting direction.
- ----------
- function Move:getFacingDir()
- local sign = 1
- if self.cords[4] %3 ~= 0 then
- sign = -1
- end
- local way = 1
- if self.cords[4] % 2 == 0 then
- way = 3
- end
- return sign, way
- end
- ----------
- -- method for rotation.
- ----------
- function Move:rotate(direction)
- if (self.cords[4] - direction) ~= 1 then
- while self.cords[4] ~= direction do
- self:turnRight()
- end
- else
- while self.cords[4] ~= direction do
- self:turnLeft()
- end
- end
- end
- ----------
- -- method for display cords.
- ----------
- function Move:dispCords()
- print(self.cords[1]..","..self.cords[2]..","..self.cords[3]..","..self.cords[4])
- end
- ----------
- -- method for getting distances.
- ----------
- function Move:getDistances(x, y, z)
- local distX = self.cords[1] - x
- local distY = self.cords[2] - y
- local distZ = self.cords[3] - z
- return distX, distY, distZ
- end
- ----------
- -- methods for going Straight.
- ----------
- -- if plus, do 1. if minus, do 3.
- function Move:goStraightX(distX)
- if self:isPlus(distX) then
- self:rotate(1)
- else
- self:rotate(3)
- end
- for i=1, math.abs(distX), 1 do
- self:moveSafe()
- end
- end
- -- if plus, down. if minus, up.
- function Move:goStraightY(distY)
- if self:isPlus(distY) then
- for i=1, math.abs(distY), 1 do
- self:moveSafe("down")
- end
- else
- for i=1, math.abs(distY), 1 do
- self:moveSafe("up")
- end
- end
- end
- -- if puls, go 2. if minus, go 0.
- function Move:goStraightZ(distZ)
- if self:isPlus(distZ) then
- self:rotate(2)
- else
- self:rotate(0)
- end
- for i=1, math.abs(distZ), 1 do
- self:moveSafe()
- end
- end
- ----------
- -- method for return to initial point.
- ----------
- function Move:returnToIniPointSure()
- local distX, distY, distZ = self:getDistances(self.initialPos[1], self.initialPos[2], self.initialPos[3])
- -- resolve Y.
- if self:isPlus(distY) then
- for i=1, math.abs(distY), 1 do
- self:moveSure('down')
- end
- else
- for i=1, math.abs(distY), 1 do
- self:moveSure('up')
- end
- end
- -- resolve X.
- if self:isPlus(distX) then
- self:rotate(1)
- else
- self:rotate(3)
- end
- for i=1, math.abs(distX), 1 do
- self:moveSure()
- end
- -- resolve Z.
- if self:isPlus(distZ) then
- self:rotate(2)
- else
- self:rotate(0)
- end
- for i=1, math.abs(distZ), 1 do
- self:moveSure()
- end
- --resolve dir.
- self:rotate(self.initialPos[4])
- end
- function Move:isPlus(num)
- if num/math.abs(num) == 1 then
- return true
- else
- return false
- end
- end
- --[[
- **********
- * end of the file
- **********
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement