Advertisement
bueddl

Untitled

Jun 4th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 0 0
  1. MiningTurtle = {
  2.     position = {
  3.         x = 0, -- WEST/EAST
  4.         y = 0, -- UP/DOWN
  5.         z = 0  -- SOUTH/NORTH
  6.     },
  7.     direction = 0
  8.     -- 0 = SOUTH -> Z++
  9.     -- 1 = WEST -> X--
  10.     -- 2 = NORTH -> Z--
  11.     -- 3 = EAST -> X++
  12. }
  13.  
  14. -- Constructor
  15.  
  16. function MiningTurtle:new()
  17.  
  18.     local object = { }
  19.    
  20.     setmetatable(object, { __index = MiningTurtle })
  21.     return object
  22. end
  23.  
  24. -- Check if the Turtle needs to be refueled ( <= refuelLevel )
  25. -- if yes, try to refuel it taking the contents of the inventory
  26. -- if this is not possible or not enough, go back to base
  27. function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel )
  28.     local slot
  29.  
  30.     if refuelLevel == nil then
  31.         refuelLevel         = 25
  32.     end
  33.    
  34.     if requiredFuelLevel == nil then
  35.         requiredFuelLevel   = 500
  36.     end
  37.    
  38.     -- should we refuel?
  39.     if turtle.getFuelLevel() <= refuelLevel then
  40.    
  41.         print("Turtle needs refuel!!!")
  42.        
  43.         -- iterate through our inventory
  44.         for slot = 1,16 do
  45.            
  46.             -- select slot
  47.             turtle.select(slot)
  48.            
  49.             -- refuel from active slot...
  50.             if turtle.refuel( turtle.getItemCount( slot ) ) then
  51.                
  52.             end
  53.            
  54.             -- ...and check again
  55.             if turtle.getFuelLevel() >= requiredFuelLevel then
  56.                 return true
  57.             end
  58.            
  59.         end
  60.        
  61.         -- check if we did not refueled enough alreay
  62.         if not (turtle.getFuelLevel() >= requiredFuelLevel) then
  63.             -- if yes, go back to base
  64.            
  65.             MiningTurtle:toBase()
  66.             -- refuel: to be implemented!!!!
  67.         end
  68.        
  69.         return false
  70.     end
  71.    
  72.     return true
  73. end
  74.  
  75. -- Go back to the base of the MiningTurtle
  76. function MiningTurtle:toBase( )
  77.  
  78. end
  79.    
  80. -- Go to ground
  81. function MiningTurtle:toGround( )
  82.  
  83.     while not turtle.detectDown() do
  84.         MiningTurtle:down()
  85.     end
  86.    
  87. end
  88.  
  89. -- Go the our "base" Ground (z = 0)
  90. function MiningTurtle:toBaseLevel( )
  91.  
  92.     while self.position.y > 0 do
  93.         MiningTurtle:saveDown()
  94.     end
  95.  
  96. end
  97.  
  98. -- forward
  99. function MiningTurtle:forward()
  100.     local ret
  101.    
  102.     ret = turtle.forward()
  103.    
  104.     if ret then
  105.        
  106.        
  107.         if self.direction == 0 then
  108.             -- 0 = SOUTH -> Z++
  109.             self.position.z = self.position.z + 1
  110.            
  111.         elseif self.direction == 1 then
  112.             -- 1 = WEST -> X--
  113.             self.position.x = self.position.x - 1
  114.        
  115.         elseif self.direction == 2 then
  116.             -- 2 = NORTH -> Z--
  117.             self.position.z = self.position.z -  1
  118.        
  119.         elseif self.direction == 3 then
  120.             -- 3 = EAST -> X++
  121.             self.position.x = self.position.x + 1
  122.        
  123.         end
  124.        
  125.     end
  126.    
  127.     MiningTurtle:tick()
  128.    
  129.     return ret
  130. end
  131.  
  132. -- back
  133. function MiningTurtle:back()
  134.     local ret
  135.    
  136.     ret = turtle.back()
  137.    
  138.     if ret then
  139.        
  140.         -- all directions reverse!!! (moving back)
  141.  
  142.         if self.direction == 0 then
  143.             -- 0 = SOUTH -> Z++
  144.             self.position.z = self.position.z - 1
  145.            
  146.         elseif self.direction == 1 then
  147.             -- 1 = WEST -> X--
  148.             self.position.x = self.position.x + 1
  149.        
  150.         elseif self.direction == 2 then
  151.             -- 2 = NORTH -> Z--
  152.             self.position.z = self.position.z +  1
  153.        
  154.         elseif self.direction == 3 then
  155.             -- 3 = EAST -> X++
  156.             self.position.x = self.position.x - 1
  157.        
  158.         end
  159.        
  160.     end
  161.    
  162.     MiningTurtle:tick()
  163.    
  164.     return ret
  165. end
  166.  
  167. -- up
  168. function MiningTurtle:up()
  169.     local ret
  170.    
  171.     ret = turtle.up()
  172.    
  173.     if ret then
  174.         self.position.y = self.position.y + 1
  175.     end
  176.    
  177.     MiningTurtle:tick()
  178.    
  179.     return ret
  180. end
  181.  
  182. -- down
  183. function MiningTurtle:down()
  184.     local ret
  185.    
  186.     ret = turtle.down()
  187.    
  188.     if ret then
  189.         self.position.y = self.position.y - 1
  190.     end
  191.    
  192.                
  193.     MiningTurtle:tick()
  194.    
  195.     return ret
  196. end
  197.  
  198. -- turn Left
  199. function MiningTurtle:turnLeft()
  200.     local ret
  201.    
  202.     ret = turtle.turnLeft()
  203.    
  204.     if ret then
  205.         self.direction = self.direction + 1
  206.        
  207.         while self.direction >= 4 do
  208.             self.direction = self.direction - 4
  209.         end
  210.     end
  211.    
  212.     MiningTurtle:outPos()
  213.    
  214.     return ret
  215. end
  216.  
  217. -- turn Right
  218. function MiningTurtle:turnRight()
  219.     local ret
  220.    
  221.     ret = turtle.turnRight()
  222.    
  223.     if ret then
  224.         self.direction = self.direction - 1
  225.        
  226.         while self.direction < 0 do
  227.             self.direction = self.direction - 4
  228.         end
  229.     end
  230.    
  231.     MiningTurtle:outPos()
  232.    
  233.     return ret
  234. end
  235.  
  236. -- Save forward
  237. function MiningTurtle:saveForward()
  238.  
  239.     while not MiningTurtle:forward() do
  240.         turtle.dig()
  241.     end
  242. end
  243.  
  244. -- Save up
  245. function MiningTurtle:saveUp()
  246.  
  247.     while not MiningTurtle:up() do
  248.         turtle.digUp()
  249.     end
  250. end
  251.  
  252. -- Save down
  253. function MiningTurtle:saveDown()
  254.  
  255.     while not MiningTurtle:down() do
  256.         turtle.digDown()
  257.     end
  258. end
  259.  
  260. -- 180 degree turn
  261. function MiningTurtle:turn()
  262.  
  263.     MiningTurtle:turnRight()
  264.     MiningTurtle:turnRight()
  265. end
  266.  
  267. -- output pos data
  268. function MiningTurtle:outPos()
  269.  
  270.     print("Pos: " .. self.position.x .. "|" .. self.position.z .. "|" .. self.position.y .. " o: " .. self.direction .. " f: " .. turtle.getFuelLevel() )
  271.    
  272. end
  273.  
  274. -- Turn to direction
  275. function MiningTurtle:turnTo( newOrientation )
  276.     while self.direction ~= newOrientation do
  277.         if math.abs(self.direction - newOrientation) < 180 then
  278.             MiningTurtle:turnRight()
  279.         else
  280.             MiningTurtle:turnLeft()
  281.         end
  282.     end
  283. end
  284.  
  285. -- Go to
  286. function MiningTurtle:goTo( newPosition )
  287.     -- 0 = SOUTH -> Z++
  288.     -- 1 = WEST -> X--
  289.     -- 2 = NORTH -> Z--
  290.     -- 3 = EAST -> X++
  291.  
  292.     while not ( (newPosition.x - self.position.x) == 0 and (newPosition.z - self.position.z) == 0 and (newPosition.y - self.position.y) == 0 ) do
  293.         print("Was: " .. self.position.x .. "|" .. self.position.z .. "|" .. self.position.y)
  294.        
  295.         if (newPosition.x - self.position.x) < 0 then
  296.             MiningTurtle:turnTo(1)
  297.             MiningTurtle:saveForward()
  298.         elseif (newPosition.x - self.position.x) > 0 then
  299.             MiningTurtle:turnTo(3)
  300.             MiningTurtle:saveForward()
  301.         end
  302.  
  303.         if (newPosition.z - self.position.z) < 0 then
  304.             MiningTurtle:turnTo(2)
  305.             MiningTurtle:saveForward()
  306.         elseif (newPosition.z - self.position.z) > 0 then
  307.             MiningTurtle:turnTo(0)
  308.             MiningTurtle:saveForward()
  309.         end
  310.  
  311.         if (newPosition.y - self.position.y) < 0 then
  312.             MiningTurtle:saveDown()
  313.         elseif (newPosition.y - self.position.y) > 0 then
  314.             MiningTurtle:saveUp()
  315.         end
  316.         print("Is: " .. self.position.x .. "|" .. self.position.z .. "|" .. self.position.y)
  317.        
  318.     end
  319.  
  320. end
  321.  
  322. -- Make a tunnel
  323. function MiningTurtle:tunnel( length, height, width, ret )
  324.  
  325.     -- default values for parameters, only length is required (!)
  326.     if height == nil then
  327.         height  = 2
  328.     end
  329.    
  330.     if width == nil then
  331.         width   = 1
  332.     end
  333.    
  334.     if ret == nil then
  335.         ret     = false
  336.     end
  337.  
  338.     -- Save our current positon back for later return
  339.    
  340.     returnPos = {}
  341.     returnPos.x = self.position.x
  342.     returnPos.z = self.position.z
  343.     returnPos.y = self.position.y
  344.     returnDirection = self.direction
  345.  
  346.     -- Place the turtle at the lower left corner of the new tunnel (so that we were starting from the middle of the new tunnel)
  347.     -- We only need this, if we have a tonnel with a width > 2 (<=2 means we are already on the right position)
  348.    
  349.     if width > 2 then
  350.  
  351.         MiningTurtle:turnLeft()
  352.  
  353.         for i = 0,math.floor( width / 2 ) do
  354.             MiningTurtle:saveForward()
  355.         end
  356.  
  357.         MiningTurtle:turnRight()
  358.     end
  359.    
  360.     -- Length loop
  361.    
  362.     for i = 0,length-1 do
  363.  
  364.         MiningTurtle:saveForward()
  365.    
  366.         if i % 2 == 0 then
  367.             -- even
  368.            
  369.             -- only change direction if we mine a tunnel wider than 1 block
  370.             if width > 1 then
  371.                 MiningTurtle:turnRight()
  372.             end
  373.  
  374.             for x = 0,width-1 do
  375.  
  376.                 for y = 0,height-3 do
  377.                
  378.                     if x % 2 == 0 then
  379.                         MiningTurtle:saveUp()                      
  380.                     else
  381.                         MiningTurtle:saveDown()        
  382.                     end
  383.                    
  384.                 end
  385.  
  386.                 if height > 1 then
  387.                     turtle.digUp()
  388.                 end
  389.                
  390.                 if x < width-1 then
  391.                     MiningTurtle:saveForward()
  392.                 end
  393.             end
  394.            
  395.             -- same as above
  396.             if width > 1 then
  397.                 MiningTurtle:turnLeft()
  398.             end
  399.        
  400.         else
  401.             -- odd
  402.            
  403.             if width > 1 then
  404.                 MiningTurtle:turnLeft()
  405.             end
  406.  
  407.             for x = 0,width-1 do
  408.                
  409.                 for y = 0,height-3 do
  410.                
  411.                     if x % 2 == 1 or width % 2 == 0 then
  412.                         MiningTurtle:saveUp()                      
  413.                     else
  414.                         MiningTurtle:saveDown()        
  415.                     end
  416.                    
  417.                 end
  418.  
  419.                 if height > 1 then
  420.                     turtle.digUp()
  421.                 end
  422.                
  423.                 if x < width-1 then
  424.                     MiningTurtle:saveForward()
  425.                 end
  426.             end
  427.            
  428.             if width > 1 then
  429.                 MiningTurtle:turnRight()
  430.             end
  431.         end
  432.    
  433.     end
  434.    
  435.     if ret then
  436.  
  437.         -- Go up to not remove torches on the floow
  438.         MiningTurtle:saveUp()
  439.    
  440.         MiningTurtle:goTo( returnPos )
  441.  
  442.         MiningTurtle:turnTo( returnDirection )
  443.  
  444.     end
  445.    
  446. end
  447.  
  448. function MiningTurtle:tick()
  449.     MiningTurtle:outPos()
  450.  
  451.     -- refuel loop
  452.     if not MiningTurtle:requiredRefuel( 20, 50 ) then
  453.         write(".")
  454.         sleep(1)
  455.     end
  456. end
  457.  
  458. --
  459. function MiningTurtle:matrix( rows, cols )
  460.  
  461.     MiningTurtle:tick()
  462.  
  463.     --MiningTurtle:toBaseLevel()
  464.     MiningTurtle:toGround()
  465.  
  466.     --MiningTurtle:tunnel(5, 2, 2, true)
  467.     MiningTurtle:goTo( { x=0, y=-5, z=-5 } )
  468.  
  469. end
  470.  
  471.  
  472. -- main program
  473.  
  474. baby = MiningTurtle:new()
  475.  
  476. baby:matrix( 10, 10 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement