Advertisement
Nokiyen

Move

Aug 6th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 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.     -- create position file if it doesn't exists.
  26.     if fs.exists("position") == false then
  27.         local h = fs.open("position", "w")
  28.         h.writeLine("position = {0,0,0,0}")
  29.  
  30.         h.flush()
  31.         h.close()
  32.     end
  33.  
  34.     -- get position data.
  35.     dofile("position")
  36.  
  37.     local cords = {}
  38.     cords[1] = position[1]  -- x
  39.     cords[2] = position[2]  -- y
  40.     cords[3] = position[3]  -- z
  41.     cords[4] = position[4]  -- front. 1 is +z.
  42.  
  43.     local obj = {cords = cords}
  44.  
  45.     -- set metatable.
  46.     return setmetatable(obj, {__index = Move})
  47. end
  48.  
  49.  
  50. ----------
  51. -- methods for moving.
  52. ----------
  53. function Move:turnLeft()
  54.     self.cords[4] = (self.cords[4] - 1) % 4
  55.     self:save()
  56.  
  57.     if turtle.turnLeft() == false then
  58.         self.cords[4] = (self.cords[4] + 1) % 4
  59.         self:save()
  60.  
  61.         return false
  62.     else
  63.         return true
  64.     end
  65. end
  66.  
  67.  
  68. function Move:turnRight()
  69.     self.cords[4] = (self.cords[4] + 1) % 4
  70.     self:save()
  71.  
  72.     if turtle.turnRight() == false then
  73.         self.cords[4] = (self.cords[4] - 1) % 4
  74.         self:save()
  75.  
  76.         return false
  77.     else
  78.         return true
  79.     end
  80. end
  81.  
  82.  
  83. function Move:forward()
  84.     if turtle.detect() == false then
  85.         local sign, way = self:getFacingDir()
  86.  
  87.         local time = 1
  88.         local flag = false
  89.         while time < 11 do
  90.             self.cords[way] = self.cords[way] + sign
  91.             self:save()
  92.  
  93.             flag = turtle.forward()
  94.             if flag == true then
  95.                 break
  96.             elseif
  97.                 self.cords[way] = self.cords[way] - sign
  98.                 self:save()
  99.             end
  100.             time = time + 1
  101.         end
  102.  
  103.         if flag == true then
  104.             return true
  105.         else
  106.             return false
  107.         end
  108.     else
  109.         return false
  110.     end
  111. end
  112.  
  113.  
  114. -- always return true unless there are no mobs in facing direction
  115. -- because turtle can't use detectBack().
  116. -- so, be careful for using this method!
  117. function Move:back()
  118.     local sign, way = self:getFacingDir()
  119.  
  120.     local time = 1
  121.     local flag = false
  122.     while time < 11 do
  123.         self.cords[way] = self.cords[way] - sign
  124.         self:save()
  125.        
  126.         flag = turtle.back()
  127.         if flag == true then
  128.             break
  129.         elseif
  130.             self.cords[way] = self.cords[way] + sign
  131.             self:save()
  132.         end
  133.         time = time + 1
  134.     end
  135.  
  136.     if flag == true then
  137.         return true
  138.     else
  139.         return false
  140.     end
  141. end
  142.  
  143.  
  144. function Move:up()
  145.     if turtle.detectUp() == false then
  146.         local time = 1
  147.         local flag = false
  148.  
  149.         while time < 11 do
  150.             self.cords[2] = (self.cords[2] + 1)
  151.             self:save()
  152.  
  153.             flag = turtle.up()
  154.             if flag == true then
  155.                 break
  156.             elseif
  157.                 self.cords[2] = (self.cords[2] - 1)
  158.                 self:save()
  159.             end
  160.             time = time + 1
  161.         end
  162.  
  163.         if flag == true then
  164.             return true
  165.         else
  166.             return false
  167.         end
  168.     else
  169.         return false
  170.     end
  171. end
  172.  
  173.  
  174. function Move:down()
  175.     if turtle.detectDown() == false then
  176.         local time = 1
  177.         local flag = false
  178.  
  179.         while time < 11 do
  180.             self.cords[2] = (self.cords[2] - 1)
  181.             self:save()
  182.  
  183.             flag = turtle.down()
  184.             if flag == true then
  185.                 break
  186.             elseif
  187.                 self.cords[2] = (self.cords[2] + 1)
  188.                 self:save()
  189.             end
  190.             time = time + 1
  191.         end
  192.  
  193.         if flag == true then
  194.             return true
  195.         else
  196.             return false
  197.         end
  198.     else
  199.         return false
  200.     end
  201. end
  202.  
  203.  
  204. ----------
  205. -- method for saving positions
  206. ----------
  207. function Move:getFacingDir()
  208.     local sign = 1
  209.     if self.cords[4] > 1
  210.         sign = -1
  211.     end
  212.  
  213.     local way = 1
  214.     if self.cords[4] % 2 == 0
  215.         way = 3
  216.     end
  217.  
  218.     return sign, way
  219. end
  220.  
  221.  
  222. ----------
  223. -- method for rotation.
  224. ----------
  225. function Move:rotate(direction)
  226.     if (self.cords[4] - direction) ~= 1 then
  227.         while self.cords[4] ~= direction do
  228.             self:turnRight()
  229.         end
  230.     else
  231.         while self.cords[4] ~= direction do
  232.             self:turnLeft()
  233.         end
  234.     end
  235. end
  236.  
  237.  
  238. ----------
  239. -- method for saving positions
  240. ----------
  241. function Move:save()
  242.     local new_line = "position = {"..table.concat(self.cords, ",").."}"
  243.     local new = fs.open("position", "w")
  244.     new.writeLine(new_line)
  245.     new.flush()
  246.     new.close()
  247. end
  248.  
  249.  
  250. ----------
  251. -- method for getting distances.
  252. ----------
  253. function Move:getDistances(x, y, z)
  254.     local dist_x = x - self.cords[1]
  255.     local dist_y = y - self.cords[2]
  256.     local dist_z = z - self.cords[3]
  257.     return dist_x, dist_y, dist_z
  258. end
  259.  
  260.  
  261.  
  262. --[[
  263. **********
  264.  * end of the file
  265. **********
  266. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement