Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- require("turtle")
- local target = 0
- local width = tonumber(arg[1])
- local length = tonumber(arg[2])
- local DIRECTS = {
- FRONT = 1,
- RIGHT = 2,
- BACK = 3,
- LEFT = 4
- }
- ObjTurtle = {
- direct = DIRECTS.FRONT,
- pos = {x = 0,y = 0},
- }
- local function direct_rotate_180(direct)
- local oDirect = direct +2
- oDirect = oDirect > 4 and oDirect - 4 or oDirect
- return oDirect
- end
- local function printDirect(direct)
- if direct == DIRECTS.FRONT then
- print("FRONT")
- elseif direct == DIRECTS.RIGHT then
- print("RIGHT")
- elseif direct == DIRECTS.BACK then
- print("BACK")
- elseif direct == DIRECTS.LEFT then
- print("LEFT")
- end
- end
- local function xor(conditionA,conditionB)
- if conditionA then
- if conditionB then
- return false
- else
- return true
- end
- else
- if conditionB then
- return true
- else
- return false
- end
- end
- end
- local bkslot = 2
- local emptyFlag =false
- local function turtle_SelfCheck()
- -- check fuel
- -- check fuel
- if(turtle.getFuelLevel() < 100) then
- turtle.select(1)
- turtle.refuel(1)
- end
- -- check space
- turtle.select(bkslot)
- if(emptyFlag == false) then
- while (turtle.getItemCount() == 0) do
- bkslot = bkslot + 1
- if(turtle.getItemCount(2) ~= 0) then
- bkslot = 2
- break
- end
- if(bkslot > 16) then
- os.shutdown()
- end
- turtle.select(bkslot)
- end
- end
- end
- function ObjTurtle:toDirect(targetDirect)
- local gap = targetDirect - self.direct
- local absGap = math.abs(gap)
- local cb_turn
- if xor(absGap < 3, gap < 0) then
- cb_turn = function ()
- turtle.turnRight()
- self.direct = self.direct + 1
- if self.direct > 4 then
- self.direct = 1
- end
- end
- else
- cb_turn = function ()
- turtle.turnLeft()
- self.direct = self.direct - 1
- if self.direct < 1 then
- self.direct = 4
- end
- end
- end
- while self.direct ~= targetDirect do
- cb_turn()
- end
- end
- function ObjTurtle:recordMove(targetDirect)
- if targetDirect == DIRECTS.FRONT then
- self.pos.y = self.pos.y + 1
- elseif targetDirect == DIRECTS.BACK then
- self.pos.y = self.pos.y - 1
- elseif targetDirect == DIRECTS.RIGHT then
- self.pos.x = self.pos.x + 1
- elseif targetDirect == DIRECTS.LEFT then
- self.pos.x = self.pos.x - 1
- end
- -- print(self.pos.x.." .. "..self.pos.y)
- end
- local function get_mainDirect(route)
- local mainDirect = {
- first = DIRECTS.FRONT,
- second = DIRECTS.RIGHT
- }
- if math.abs(route.x) > math.abs(route.y) then
- if route.x < 0 then
- mainDirect.first = DIRECTS.LEFT
- else
- mainDirect.first = DIRECTS.RIGHT
- end
- if route.y < 0 then
- mainDirect.second = DIRECTS.BACK
- else
- mainDirect.second = DIRECTS.FRONT
- end
- else
- if route.y < 0 then
- mainDirect.first = DIRECTS.BACK
- else
- mainDirect.first = DIRECTS.FRONT
- end
- if route.x < 0 then
- mainDirect.second = DIRECTS.LEFT
- else
- mainDirect.second = DIRECTS.RIGHT
- end
- end
- return mainDirect
- end
- function ObjTurtle:tryMove(targetDirect)
- turtle_SelfCheck()
- if self.direct == targetDirect then
- turtle.forward()
- else
- self:toDirect(targetDirect)
- turtle.forward()
- end
- -- 定制功能
- turtle.placeDown()
- self:recordMove(targetDirect)
- end
- local EDGE_STATES = {
- NOT = 0,
- FINAL = 1,
- EDGE = 2,
- NEEDBACK = 3,
- }
- local function edge_check(selfPos,targetPos,mainDirect)
- -- 检查边缘
- if mainDirect == DIRECTS.FRONT or mainDirect == DIRECTS.BACK then
- -- 判断 y
- local isDouble = false
- if targetPos.x % 2 == 0 then
- isDouble = true
- end
- if isDouble then
- if selfPos.y == targetPos.y and selfPos.x == targetPos.x then
- return EDGE_STATES.FINAL
- elseif selfPos.y == targetPos.y or selfPos.y == 0 then
- return EDGE_STATES.EDGE
- else
- return EDGE_STATES.NOT
- end
- else
- if selfPos.y == targetPos.y and selfPos.x == 0 then
- return EDGE_STATES.FINAL
- elseif selfPos.y == targetPos.y or selfPos.x == 0 then
- return EDGE_STATES.EDGE
- else
- return EDGE_STATES.NOT
- end
- end
- else
- -- 判断 x 左右移动
- local isDouble = false
- if targetPos.y % 2 == 0 then
- isDouble = true
- end
- if isDouble then
- if selfPos.y == targetPos.y and selfPos.x == targetPos.x then
- return EDGE_STATES.FINAL
- elseif selfPos.x == targetPos.x or selfPos.x == 0 then
- return EDGE_STATES.EDGE
- else
- return EDGE_STATES.NOT
- end
- else
- if selfPos.y == targetPos.y and selfPos.x == 0 then
- return EDGE_STATES.FINAL
- elseif selfPos.x == targetPos.x or selfPos.x == 0 then
- return EDGE_STATES.EDGE
- else
- return EDGE_STATES.NOT
- end
- end
- end
- end
- function ObjTurtle:goTo(route)
- -- self-check
- local mainDirect = get_mainDirect(route)
- local currentDirect = {
- first = mainDirect.first,
- second = mainDirect.second
- }
- local cnt = 500
- while true do
- if cnt < 0 then
- print("error")
- return
- end
- cnt = cnt -1
- self:tryMove(currentDirect.first)
- check_ret = edge_check(self.pos,route,mainDirect.first)
- if check_ret == EDGE_STATES.FINAL then
- -- self:tryMove(mainDirect.second)
- break
- elseif check_ret == EDGE_STATES.EDGE then
- currentDirect.first = direct_rotate_180(currentDirect.first)
- self:tryMove(currentDirect.second)
- elseif check_ret == EDGE_STATES.NEEDBACK then
- break
- end
- end
- print("final")
- end
- local tst_route = {
- x = 12,
- y = 4,
- }
- local tst_route2 = {
- x = width,
- y = length,
- }
- -- ObjTurtle:tryMove(DIRECTS.FRONT)
- ObjTurtle:goTo(tst_route2)
- -- ObjTurtle:toDirect(DIRECTS.FRONT)
- -- 根据主要方向进行移动和计算
- ---[[
- --- 海龟需要一个对象去持有面向方向和当前相对坐标
- --- 海龟可以移动和旋转
- --- 根据路线记录主要前进方向,根据前进方向初始化姿态
- --- 移动时的输入办法: 传入实际方向 -> 根据海龟面向方向进行实际判断运动逻辑
- --- 定位办法: 记录海龟实际相对坐标 -> 比对实际坐标进行判断是否到达目标点(需要根据主要前进方向进行不同的判断)
- ---
- ---]]
- ---
- ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement