Advertisement
Nokiyen

move

Jun 14th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.05 KB | None | 0 0
  1. --[[
  2. ***********
  3.  * prototype::move
  4.  *
  5.  * This is a prototype file for turtle's moving.
  6.  *
  7.  * generally, call this by dofile()
  8.  * and use it in a basic object-based coding of lua.
  9.  *
  10. **********
  11. ]]
  12.  
  13.  
  14.  
  15. ----------
  16. -- a frame of the prototype
  17. ----------
  18. Move = {}
  19.  
  20.  
  21. ----------
  22. -- constructer
  23. ----------
  24. function Move.new()
  25.     -- need modem.
  26.     local rightP = peripheral.getType('right')
  27.     local rightL = peripheral.getType('left')
  28.     if rightP ~= 'modem' and rightL ~= 'modem' then
  29.         return false, 'need modem.'
  30.     end
  31.  
  32.     -- create coordinates.
  33.     local cords = {}
  34.     cords[1] = 0    -- x
  35.     cords[2] = 0    -- y
  36.     cords[3] = 0    -- z
  37.     cords[4] = 0    -- direction. 0 is +z (south). clock. 1 is -x.
  38.  
  39.     -- create obj.
  40.     local obj = {cords = cords, initialPos = cords}
  41.     -- set metatable.
  42.     local new = setmetatable(obj, {__index = Move})
  43.     -- get current position.
  44.     local flag, opt = new:currentPos()
  45.    
  46.     --GPS error.
  47.     if flag == false then
  48.         return false, opt
  49.     end
  50.    
  51.     -- create position file if it doesn't exist.
  52.     if fs.exists("initialPos") == false then
  53.         local h = fs.open("initialPos", "w")
  54.         h.writeLine("initialPos = {"..new.cords[1]..","..new.cords[2]..","..new.cords[3]..","..new.cords[4].."}")
  55.         h.flush()
  56.         h.close()
  57.     end
  58.  
  59.     -- set initial position.   
  60.     dofile("initialPos")
  61.     new.initialPos = initialPos
  62.  
  63.     return new
  64. end
  65.  
  66.  
  67. ----------
  68. -- method for getting the current turtle's position.
  69. -- this uses GPS system. So, it returns false and error rmessage in non-GPS environment.
  70. ----------
  71. function Move:currentPos()
  72.     --get first position.
  73.     local pos1 = {gps.locate(30)}
  74.     if pos1 == nil then
  75.         return false, "Can't connect to GPS, and failed to get pos1 in Move:currentPos()."
  76.     end
  77.  
  78.     --try to move to get second position.
  79.     local flag1 = true
  80.     for i=1, 4, 1 do
  81.         if turtle.detect() == false then
  82.             flag1 = false
  83.             break
  84.         end
  85.         turtle.turnLeft()
  86.     end
  87.     local flag, msg = self:moveSure()
  88.     if flag == false then
  89.         return false, "Can't move, and failed to get pos2 in Move:currentPos()."
  90.     end
  91.  
  92.     --get second position.
  93.     local pos2 = {gps.locate(30)}
  94.     if pos2 == nil then
  95.         return false, "Can't connect to GPS, and failed to get pos2 in Move:currentPos()."
  96.     end
  97.  
  98.     --confirm direction and set cords.
  99.     local distX = pos2[1] - pos1[1]
  100.     local distZ = pos2[3] - pos1[3]
  101.     if distX == 1 then
  102.         self.cords[4] = 3
  103.     elseif distX == -1 then
  104.         self.cords[4] = 1
  105.     elseif distZ == 1 then
  106.         self.cords[4] = 0
  107.     elseif distZ == -1 then
  108.         self.cords[4] = 2
  109.     end
  110.     self.cords[1] = pos2[1]
  111.     self.cords[2] = pos2[2]
  112.     self.cords[3] = pos2[3]
  113.    
  114.     -- return to first pos.
  115.     self:turnLeft()
  116.     self:turnLeft()
  117.     local flag, msg = self:moveSure()
  118.     if flag == false then
  119.         return false, "Can't move, and failed to return to  pos1 in Move:currentPos()."
  120.     end
  121.     self:turnLeft()
  122.     self:turnLeft()
  123.    
  124.     return true
  125. end
  126.  
  127. ----------
  128. -- methods for moving.
  129. ----------
  130. function Move:turnLeft()
  131.     self.cords[4] = (self.cords[4] - 1) % 4
  132.  
  133.     if turtle.turnLeft() == false then
  134.         self.cords[4] = (self.cords[4] + 1) % 4
  135.         return false
  136.     end
  137. --  self:dispCords()
  138.     return true
  139. end
  140.  
  141.  
  142. function Move:turnRight()
  143.     self.cords[4] = (self.cords[4] + 1) % 4
  144.  
  145.     if turtle.turnRight() == false then
  146.         self.cords[4] = (self.cords[4] - 1) % 4
  147.         return false
  148.     end
  149. --  self:dispCords()
  150.     return true
  151. end
  152.  
  153.  
  154. function Move:forward()
  155.     local sign, way = self:getFacingDir()
  156.  
  157.     self.cords[way] = self.cords[way] + sign
  158.     if turtle.forward() == false then
  159.         self.cords[way] = self.cords[way] - sign
  160.         return false
  161.     end
  162. --  self:dispCords()
  163.     return true
  164. end
  165.  
  166.  
  167. function Move:back()
  168.     local sign, way = self:getFacingDir()
  169.  
  170.     self.cords[way] = self.cords[way] - sign
  171.     if turtle.back() == false then
  172.         self.cords[way] = self.cords[way] + sign
  173.         return false
  174.     end
  175. --  self:dispCords()
  176.     return true
  177. end
  178.  
  179.  
  180. function Move:up()
  181.     self.cords[2] = (self.cords[2] + 1)
  182.  
  183.     if turtle.up() == false then
  184.         self.cords[2] = (self.cords[2] - 1)
  185.         return false
  186.     end
  187. --  self:dispCords()
  188.     return true
  189. end
  190.  
  191.  
  192. function Move:down()
  193.     self.cords[2] = (self.cords[2] - 1)
  194.  
  195.     if turtle.down() == false then
  196.         self.cords[2] = (self.cords[2] + 1)
  197.         return false
  198.     end
  199. --  self:dispCords()
  200.     return true
  201. end
  202.  
  203. ----------
  204. -- method for safe moving.
  205. ----------
  206. -- remove mob, and move.
  207. function Move:moveSafe(dir)
  208.     local moveFunc = self.forward
  209.     local attackFunc = turtle.attack
  210.    
  211.     if dir == 'up' then
  212.         moveFunc = self.up
  213.         attackFunc = turtle.attackUp
  214.     elseif dir == 'down' then
  215.         moveFunc = self.down
  216.         attackFunc = turtle.attackDown
  217.     end
  218.    
  219.     local times = 0
  220.     while moveFunc(self) == false do
  221.         attackFunc()
  222.         times = times + 1
  223.         if times == 20 then
  224.             return false, "moving obstructed by unkown."
  225.         end
  226.     end
  227.    
  228.     return true
  229. end
  230.  
  231. -- remove mob and block, and move.
  232. function Move:moveSure(dir)
  233.     local moveFunc = self.forward
  234.     local detectFunc = turtle.detect
  235.     local digFunc = turtle.dig
  236.     local attackFunc = turtle.attack
  237.    
  238.     if dir == 'up' then
  239.         moveFunc = self.up
  240.         detectFunc = turtle.detectUp
  241.         digFunc = turtle.digUp
  242.         attackFunc = turtle.attackUp
  243.     elseif dir == 'down' then
  244.         moveFunc = self.down
  245.         detectFunc = turtle.detectDown
  246.         digFunc = turtle.digDown
  247.         attackFunc = turtle.attackDown
  248.     end
  249.    
  250.     local times = 0
  251.     while detectFunc() do
  252.         digFunc()
  253.         times = times + 1
  254.         if times == 20 then
  255.             return false, "moving obstructed by block."
  256.         end
  257.         sleep(1)
  258.     end
  259.     times = 0
  260.     while moveFunc(self) == false do
  261.         attackFunc()
  262.         times = times + 1
  263.         if times == 20 then
  264.             return false, "moving obstructed by unkown."
  265.         end
  266.     end
  267.    
  268.     return true
  269. end
  270.  
  271. ----------
  272. -- method for getting direction.
  273. ----------
  274. function Move:getFacingDir()
  275.     local sign = 1
  276.     if self.cords[4] %3 ~= 0 then
  277.         sign = -1
  278.     end
  279.  
  280.     local way = 1
  281.     if self.cords[4] % 2 == 0 then
  282.         way = 3
  283.     end
  284.  
  285.     return sign, way
  286. end
  287.  
  288. ----------
  289. -- method for rotation.
  290. ----------
  291. function Move:rotate(direction)
  292.     if (self.cords[4] - direction) ~= 1 then
  293.         while self.cords[4] ~= direction do
  294.             self:turnRight()
  295.         end
  296.     else
  297.         while self.cords[4] ~= direction do
  298.             self:turnLeft()
  299.         end
  300.     end
  301. end
  302.  
  303. ----------
  304. -- method for display cords.
  305. ----------
  306. function Move:dispCords()
  307.     print(self.cords[1]..","..self.cords[2]..","..self.cords[3]..","..self.cords[4])
  308. end
  309.  
  310. ----------
  311. -- method for getting distances.
  312. ----------
  313. function Move:getDistances(x, y, z)
  314.     local distX = self.cords[1] - x
  315.     local distY = self.cords[2] - y
  316.     local distZ = self.cords[3] - z
  317.     return distX, distY, distZ
  318. end
  319.  
  320. ----------
  321. -- methods for going Straight.
  322. ----------
  323. -- if plus, do 1. if minus, do 3.
  324. function Move:goStraightX(distX)
  325.     if self:isPlus(distX) then
  326.         self:rotate(1)
  327.     else
  328.         self:rotate(3)
  329.     end
  330.     for i=1, math.abs(distX), 1 do
  331.         self:moveSafe()
  332.     end
  333. end
  334.  
  335. -- if plus, down. if minus, up.
  336. function Move:goStraightY(distY)
  337.     if self:isPlus(distY) then
  338.         for i=1, math.abs(distY), 1 do
  339.             self:moveSafe("down")
  340.         end
  341.     else
  342.         for i=1, math.abs(distY), 1 do
  343.             self:moveSafe("up")
  344.         end
  345.     end
  346. end
  347.  
  348. -- if puls, go 2. if minus, go 0.
  349. function Move:goStraightZ(distZ)
  350.     if self:isPlus(distZ) then
  351.         self:rotate(2)
  352.     else
  353.         self:rotate(0)
  354.     end
  355.     for i=1, math.abs(distZ), 1 do
  356.         self:moveSafe()
  357.     end
  358. end
  359.  
  360. ----------
  361. -- method for return to initial point.
  362. ----------
  363. function Move:returnToIniPointSure()
  364.     local distX, distY, distZ = self:getDistances(self.initialPos[1], self.initialPos[2], self.initialPos[3])
  365.    
  366.     -- resolve Y.
  367.     if self:isPlus(distY) then
  368.         for i=1, math.abs(distY), 1 do
  369.             self:moveSure('down')
  370.         end
  371.     else
  372.         for i=1, math.abs(distY), 1 do
  373.             self:moveSure('up')
  374.         end
  375.     end
  376.     -- resolve X.
  377.     if self:isPlus(distX) then
  378.         self:rotate(1)
  379.     else
  380.         self:rotate(3)
  381.     end
  382.     for i=1, math.abs(distX), 1 do
  383.         self:moveSure()
  384.     end
  385.     -- resolve Z.
  386.     if self:isPlus(distZ) then
  387.         self:rotate(2)
  388.     else
  389.         self:rotate(0)
  390.     end
  391.     for i=1, math.abs(distZ), 1 do
  392.         self:moveSure()
  393.     end
  394.     --resolve dir.
  395.     self:rotate(self.initialPos[4])
  396. end
  397.  
  398. function Move:isPlus(num)
  399.     if num/math.abs(num) == 1 then
  400.         return true
  401.     else
  402.         return false
  403.     end
  404. end
  405.  
  406.  
  407. --[[
  408. **********
  409.  * end of the file
  410. **********
  411. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement