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()
- -- create position file if it doesn't exists.
- if fs.exists("position") == false then
- local h = fs.open("position", "w")
- h.writeLine("position = {0,0,0,0}")
- h.flush()
- h.close()
- end
- -- get position data.
- dofile("position")
- local cords = {}
- cords[1] = position[1] -- x
- cords[2] = position[2] -- y
- cords[3] = position[3] -- z
- cords[4] = position[4] -- front. 1 is +z.
- local obj = {cords = cords}
- -- set metatable.
- return setmetatable(obj, {__index = Move})
- end
- ----------
- -- methods for moving.
- ----------
- function Move:turnLeft()
- self.cords[4] = (self.cords[4] - 1) % 4
- self:save()
- if turtle.turnLeft() == false then
- self.cords[4] = (self.cords[4] + 1) % 4
- self:save()
- return false
- else
- return true
- end
- end
- function Move:turnRight()
- self.cords[4] = (self.cords[4] + 1) % 4
- self:save()
- if turtle.turnRight() == false then
- self.cords[4] = (self.cords[4] - 1) % 4
- self:save()
- return false
- else
- return true
- end
- end
- function Move:forward()
- if turtle.detect() == false then
- local sign, way = self:getFacingDir()
- local time = 1
- local flag = false
- while time < 11 do
- self.cords[way] = self.cords[way] + sign
- self:save()
- flag = turtle.forward()
- if flag == true then
- break
- elseif
- self.cords[way] = self.cords[way] - sign
- self:save()
- end
- time = time + 1
- end
- if flag == true then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- -- always return true unless there are no mobs in facing direction
- -- because turtle can't use detectBack().
- -- so, be careful for using this method!
- function Move:back()
- local sign, way = self:getFacingDir()
- local time = 1
- local flag = false
- while time < 11 do
- self.cords[way] = self.cords[way] - sign
- self:save()
- flag = turtle.back()
- if flag == true then
- break
- elseif
- self.cords[way] = self.cords[way] + sign
- self:save()
- end
- time = time + 1
- end
- if flag == true then
- return true
- else
- return false
- end
- end
- function Move:up()
- if turtle.detectUp() == false then
- local time = 1
- local flag = false
- while time < 11 do
- self.cords[2] = (self.cords[2] + 1)
- self:save()
- flag = turtle.up()
- if flag == true then
- break
- elseif
- self.cords[2] = (self.cords[2] - 1)
- self:save()
- end
- time = time + 1
- end
- if flag == true then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function Move:down()
- if turtle.detectDown() == false then
- local time = 1
- local flag = false
- while time < 11 do
- self.cords[2] = (self.cords[2] - 1)
- self:save()
- flag = turtle.down()
- if flag == true then
- break
- elseif
- self.cords[2] = (self.cords[2] + 1)
- self:save()
- end
- time = time + 1
- end
- if flag == true then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- ----------
- -- method for saving positions
- ----------
- function Move:getFacingDir()
- local sign = 1
- if self.cords[4] > 1
- sign = -1
- end
- local way = 1
- if self.cords[4] % 2 == 0
- 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 saving positions
- ----------
- function Move:save()
- local new_line = "position = {"..table.concat(self.cords, ",").."}"
- local new = fs.open("position", "w")
- new.writeLine(new_line)
- new.flush()
- new.close()
- end
- ----------
- -- method for getting distances.
- ----------
- function Move:getDistances(x, y, z)
- local dist_x = x - self.cords[1]
- local dist_y = y - self.cords[2]
- local dist_z = z - self.cords[3]
- return dist_x, dist_y, dist_z
- end
- --[[
- **********
- * end of the file
- **********
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement