Advertisement
Lanzr

new placer

Nov 23rd, 2024 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.28 KB | None | 0 0
  1. -- require("turtle")
  2. local target = 0
  3.  
  4. local width = tonumber(arg[1])
  5. local length = tonumber(arg[2])
  6.  
  7.  
  8. local DIRECTS = {
  9.     FRONT = 1,
  10.     RIGHT = 2,
  11.     BACK = 3,
  12.     LEFT = 4
  13. }
  14. ObjTurtle = {
  15.     direct = DIRECTS.FRONT,
  16.     pos = {x = 0,y = 0},
  17. }
  18. local function direct_rotate_180(direct)
  19.     local oDirect = direct +2
  20.     oDirect = oDirect > 4 and oDirect - 4 or oDirect
  21.     return oDirect
  22. end
  23. local function printDirect(direct)
  24.     if direct == DIRECTS.FRONT then
  25.         print("FRONT")
  26.     elseif direct == DIRECTS.RIGHT then
  27.         print("RIGHT")
  28.     elseif direct == DIRECTS.BACK then
  29.         print("BACK")
  30.     elseif direct == DIRECTS.LEFT then
  31.         print("LEFT")
  32.     end
  33. end
  34.  
  35. local function xor(conditionA,conditionB)
  36.     if conditionA then
  37.         if conditionB then
  38.             return false
  39.         else
  40.             return true
  41.         end
  42.     else
  43.         if conditionB then
  44.             return true
  45.         else
  46.             return false
  47.         end
  48.     end
  49.    
  50. end
  51. local bkslot = 2
  52. local emptyFlag =false
  53. local function turtle_SelfCheck()
  54.     -- check fuel
  55.     -- check fuel
  56.     if(turtle.getFuelLevel() < 100) then
  57.         turtle.select(1)
  58.         turtle.refuel(1)
  59.     end
  60.     -- check space
  61.     turtle.select(bkslot)
  62.     if(emptyFlag == false) then
  63.         while (turtle.getItemCount() == 0) do
  64.             bkslot = bkslot + 1
  65.             if(turtle.getItemCount(2) ~= 0) then
  66.                 bkslot = 2
  67.                 break
  68.             end
  69.             if(bkslot > 16) then
  70.                 os.shutdown()
  71.             end
  72.             turtle.select(bkslot)
  73.         end
  74.     end
  75.    
  76. end
  77. function ObjTurtle:toDirect(targetDirect)
  78.     local gap =  targetDirect - self.direct
  79.     local absGap = math.abs(gap)
  80.     local cb_turn
  81.  
  82.     if xor(absGap < 3, gap < 0) then
  83.         cb_turn = function ()
  84.             turtle.turnRight()
  85.             self.direct = self.direct + 1
  86.             if self.direct > 4 then
  87.                 self.direct = 1
  88.             end
  89.         end
  90.     else
  91.         cb_turn = function ()
  92.             turtle.turnLeft()
  93.             self.direct = self.direct - 1
  94.             if self.direct < 1 then
  95.                 self.direct = 4
  96.             end
  97.         end
  98.     end
  99.    
  100.     while self.direct ~= targetDirect do
  101.         cb_turn()
  102.     end
  103. end
  104.  
  105. function ObjTurtle:recordMove(targetDirect)
  106.     if targetDirect == DIRECTS.FRONT then
  107.         self.pos.y = self.pos.y + 1
  108.     elseif targetDirect == DIRECTS.BACK then
  109.         self.pos.y = self.pos.y - 1
  110.     elseif targetDirect == DIRECTS.RIGHT then
  111.         self.pos.x = self.pos.x + 1
  112.     elseif targetDirect == DIRECTS.LEFT then
  113.         self.pos.x = self.pos.x - 1
  114.     end
  115.     -- print(self.pos.x.." .. "..self.pos.y)
  116. end
  117.  
  118.  
  119.  
  120. local function get_mainDirect(route)
  121.     local mainDirect = {
  122.         first = DIRECTS.FRONT,
  123.         second = DIRECTS.RIGHT
  124.     }
  125.    
  126.     if math.abs(route.x) > math.abs(route.y) then
  127.         if route.x < 0 then
  128.             mainDirect.first = DIRECTS.LEFT
  129.         else
  130.             mainDirect.first = DIRECTS.RIGHT
  131.         end
  132.         if route.y < 0 then
  133.             mainDirect.second = DIRECTS.BACK
  134.         else
  135.             mainDirect.second = DIRECTS.FRONT
  136.         end
  137.     else
  138.         if route.y < 0 then
  139.             mainDirect.first = DIRECTS.BACK
  140.         else
  141.             mainDirect.first = DIRECTS.FRONT
  142.         end
  143.         if route.x < 0 then
  144.             mainDirect.second = DIRECTS.LEFT
  145.         else
  146.             mainDirect.second = DIRECTS.RIGHT
  147.         end
  148.     end
  149.     return mainDirect
  150. end
  151.  
  152.  
  153. function ObjTurtle:tryMove(targetDirect)
  154.     turtle_SelfCheck()
  155.     if self.direct == targetDirect then
  156.         turtle.forward()
  157.     else
  158.         self:toDirect(targetDirect)
  159.         turtle.forward()
  160.     end
  161.     -- 定制功能
  162.     turtle.placeDown()
  163.     self:recordMove(targetDirect)
  164. end
  165. local EDGE_STATES = {
  166.     NOT = 0,
  167.     FINAL = 1,
  168.     EDGE = 2,
  169.     NEEDBACK = 3,
  170. }
  171. local function edge_check(selfPos,targetPos,mainDirect)
  172.     -- 检查边缘
  173.     if mainDirect == DIRECTS.FRONT or mainDirect == DIRECTS.BACK then
  174.         -- 判断 y
  175.         local isDouble = false
  176.         if targetPos.x % 2 == 0 then
  177.             isDouble = true
  178.         end
  179.         if isDouble then
  180.             if selfPos.y == targetPos.y and selfPos.x == targetPos.x then
  181.                 return EDGE_STATES.FINAL
  182.             elseif selfPos.y == targetPos.y or selfPos.y == 0 then
  183.                 return EDGE_STATES.EDGE
  184.             else
  185.                 return EDGE_STATES.NOT
  186.             end
  187.         else
  188.             if selfPos.y == targetPos.y and selfPos.x == 0 then
  189.                 return EDGE_STATES.FINAL
  190.             elseif selfPos.y == targetPos.y or selfPos.x == 0 then
  191.                 return EDGE_STATES.EDGE
  192.             else
  193.                 return EDGE_STATES.NOT                
  194.             end
  195.         end
  196.     else
  197.         -- 判断 x 左右移动
  198.         local isDouble = false
  199.         if targetPos.y % 2 == 0 then
  200.             isDouble = true
  201.         end
  202.  
  203.         if isDouble then
  204.             if selfPos.y == targetPos.y and selfPos.x == targetPos.x then
  205.                 return EDGE_STATES.FINAL
  206.             elseif selfPos.x == targetPos.x or selfPos.x == 0 then
  207.                 return EDGE_STATES.EDGE
  208.             else
  209.                 return EDGE_STATES.NOT
  210.             end
  211.         else
  212.             if selfPos.y == targetPos.y and selfPos.x == 0 then
  213.                 return EDGE_STATES.FINAL
  214.             elseif selfPos.x == targetPos.x or selfPos.x == 0 then
  215.                 return EDGE_STATES.EDGE
  216.             else
  217.                 return EDGE_STATES.NOT                
  218.             end
  219.         end
  220.     end
  221. end
  222.  
  223. function ObjTurtle:goTo(route)
  224.     -- self-check
  225.     local mainDirect = get_mainDirect(route)
  226.     local currentDirect = {
  227.         first = mainDirect.first,
  228.         second = mainDirect.second
  229.     }
  230.     local cnt = 500
  231.     while true do
  232.         if cnt < 0 then
  233.             print("error")
  234.             return
  235.         end
  236.         cnt = cnt -1
  237.         self:tryMove(currentDirect.first)
  238.         check_ret = edge_check(self.pos,route,mainDirect.first)
  239.         if check_ret == EDGE_STATES.FINAL then
  240.             -- self:tryMove(mainDirect.second)
  241.             break
  242.         elseif check_ret == EDGE_STATES.EDGE then                    
  243.             currentDirect.first = direct_rotate_180(currentDirect.first)
  244.             self:tryMove(currentDirect.second)
  245.         elseif check_ret == EDGE_STATES.NEEDBACK then
  246.             break
  247.         end
  248.        
  249.     end
  250.     print("final")
  251. end
  252.  
  253. local tst_route = {
  254.     x = 12,
  255.     y = 4,
  256. }
  257. local tst_route2 = {
  258.     x = width,
  259.     y = length,
  260. }
  261. -- ObjTurtle:tryMove(DIRECTS.FRONT)
  262. ObjTurtle:goTo(tst_route2)
  263.  
  264. -- ObjTurtle:toDirect(DIRECTS.FRONT)
  265. -- 根据主要方向进行移动和计算
  266. ---[[
  267. --- 海龟需要一个对象去持有面向方向和当前相对坐标
  268. --- 海龟可以移动和旋转
  269. --- 根据路线记录主要前进方向,根据前进方向初始化姿态
  270. --- 移动时的输入办法: 传入实际方向 -> 根据海龟面向方向进行实际判断运动逻辑
  271. --- 定位办法: 记录海龟实际相对坐标 -> 比对实际坐标进行判断是否到达目标点(需要根据主要前进方向进行不同的判断)
  272. ---
  273. ---]]
  274. ---
  275. ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement