Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MiningTurtle = {
- position = {
- x = 0,
- y = 0,
- z = 0
- },
- direction = 0
- -- 0 = SOUTH -> Z++
- -- 1 = WEST -> X--
- -- 2 = NORTH -> Z--
- -- 3 = EAST -> X++
- }
- -- Constructor
- function MiningTurtle:new()
- local object = { }
- setmetatable(object, { __index = MiningTurtle })
- return object
- end
- -- Check if the Turtle needs to be refueled ( <= refuelLevel )
- -- if yes, try to refuel it taking the contents of the inventory
- -- if this is not possible or not enough, go back to base
- function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel )
- local slot
- if refuelLevel == nil then
- refuelLevel = 25
- end
- if requiredFuelLevel == nil then
- requiredFuelLevel = 500
- end
- -- should we refuel?
- if turtle.getFuelLevel() <= refuelLevel then
- -- iterate through our inventory
- for slot = 1,16 do
- -- select slot
- turtle.select(slot)
- -- refuel from active slot...
- if turtle.refuel( turtle.getItemCount( slot ) ) then
- end
- -- ...and check again
- if turtle.getFuelLevel() >= requiredFuelLevel then
- return true
- end
- end
- -- check if we did not refueled enough alreay
- if not (turtle.getFuelLevel() >= requiredFuelLevel) then
- -- if yes, go back to base
- MiningTurtle:toBase()
- -- refuel: to be implemented!!!!
- end
- return false
- end
- return true
- end
- -- Go back to the base of the MiningTurtle
- function MiningTurtle:toBase( )
- end
- -- Go to ground
- function MiningTurtle:toGround( )
- while not turtle.detectDown() do
- MiningTurtle:down()
- end
- end
- -- Go the our "base" Ground (z = 0)
- function MiningTurtle:toBaseLevel( )
- while self.position.y > 0 do
- MiningTurtle:saveDown()
- end
- end
- -- forward
- function MiningTurtle:forward()
- local ret
- ret = turtle.forward()
- if ret then
- if self.direction == 0 then
- -- 0 = SOUTH -> Z++
- self.position.z = self.position.z + 1
- elseif self.direction == 1 then
- -- 1 = WEST -> X--
- self.position.x = self.position.x - 1
- elseif self.direction == 2 then
- -- 2 = NORTH -> Z--
- self.position.z = self.position.z - 1
- elseif self.direction == 3 then
- -- 3 = EAST -> X++
- self.position.x = self.position.x + 1
- end
- end
- MiningTurtle:outPos()
- return ret
- end
- -- back
- function MiningTurtle:back()
- local ret
- ret = turtle.back()
- if ret then
- -- all directions reverse!!! (moving back)
- if self.direction == 0 then
- -- 0 = SOUTH -> Z++
- self.position.z = self.position.z - 1
- elseif self.direction == 1 then
- -- 1 = WEST -> X--
- self.position.x = self.position.x + 1
- elseif self.direction == 2 then
- -- 2 = NORTH -> Z--
- self.position.z = self.position.z + 1
- elseif self.direction == 3 then
- -- 3 = EAST -> X++
- self.position.x = self.position.x - 1
- end
- end
- MiningTurtle:outPos()
- return ret
- end
- -- up
- function MiningTurtle:up()
- local ret
- ret = turtle.up()
- if ret then
- self.position.y = self.position.y + 1
- end
- MiningTurtle:outPos()
- return ret
- end
- -- down
- function MiningTurtle:down()
- local ret
- ret = turtle.down()
- if ret then
- self.position.y = self.position.y - 1
- end
- MiningTurtle:outPos()
- return ret
- end
- -- turn Left
- function MiningTurtle:turnLeft()
- local ret
- ret = turtle.turnLeft()
- if ret then
- self.direction = self.direction + 1
- while self.direction >= 4 do
- self.direction = self.direction - 4
- end
- end
- MiningTurtle:outPos()
- return ret
- end
- -- turn Right
- function MiningTurtle:turnRight()
- local ret
- ret = turtle.turnRight()
- if ret then
- self.direction = self.direction - 1
- while self.direction < 0 do
- self.direction = self.direction - 4
- end
- end
- MiningTurtle:outPos()
- return ret
- end
- -- Save forward
- function MiningTurtle:saveForward()
- while not MiningTurtle:forward() do
- turtle.dig()
- end
- end
- -- Save up
- function MiningTurtle:saveUp()
- while not MiningTurtle:up() do
- turtle.digUp()
- end
- end
- -- Save down
- function MiningTurtle:saveDown()
- while not MiningTurtle:down() do
- turtle.digDown()
- end
- end
- -- 180 degree turn
- function MiningTurtle:turn()
- MiningTurtle:turnRight()
- MiningTurtle:turnRight()
- end
- -- output pos data
- function MiningTurtle:outPos()
- print("Pos: " .. tostring(self.position.x) .. "|" .. tostring(self.position.z) .. "|" .. tostring(self.position.y) .. " o: " .. tostring(self.direction) .. " f: " .. tostring(turtle.getFuelLevel()) )
- end
- -- Turn to direction
- function MiningTurtle:turnTo( newOrientation )
- while self.direction ~= newOrientation do
- if math.abs(self.direction - newOrientation) < 180 then
- MiningTurtle:turnRight()
- else
- MiningTurtle:turnLeft()
- end
- end
- end
- -- Go to
- function MiningTurtle:goTo( newPosition )
- print("GoTo: " .. newPosition.x .. "|" .. newPosition.z .. "|" .. newPosition.y)
- -- 0 = SOUTH -> Z++
- -- 1 = WEST -> X--
- -- 2 = NORTH -> Z--
- -- 3 = EAST -> X++
- while not ( (newPosition.x - self.position.x) == 0 and (newPosition.z - self.position.z) == 0 and (newPosition.y - self.position.y) == 0 ) do
- if (newPosition.x - self.position.x) < 0 then
- MiningTurtle:turnTo(1)
- MiningTurtle:saveForward()
- elseif (newPosition.x - self.position.x) > 0 then
- MiningTurtle:turnTo(3)
- MiningTurtle:saveForward()
- end
- if (newPosition.z - self.position.z) < 0 then
- MiningTurtle:turnTo(2)
- MiningTurtle:saveForward()
- elseif (newPosition.z - self.position.z) > 0 then
- MiningTurtle:turnTo(0)
- MiningTurtle:saveForward()
- end
- if (newPosition.y - self.position.y) < 0 then
- MiningTurtle:saveDown()
- elseif (newPosition.y - self.position.y) > 0 then
- MiningTurtle:saveUp()
- end
- end
- end
- -- Make a tunnel
- function MiningTurtle:tunnel( length, height, width, ret )
- -- default values for parameters, only length is required (!)
- if height == nil then
- height = 2
- end
- if width == nil then
- width = 1
- end
- if ret == nil then
- ret = false
- end
- -- Save our current positon back for later return
- returnPos = {}
- returnPos.x = self.position.x
- returnPos.z = self.position.z
- returnPos.y = self.position.y
- returnDirection = self.direction
- -- Place the turtle at the lower left corner of the new tunnel (so that we were starting from the middle of the new tunnel)
- -- We only need this, if we have a tonnel with a width > 2 (<=2 means we are already on the right position)
- if width > 2 then
- MiningTurtle:turnLeft()
- for i = 0,math.floor( width / 2 ) do
- MiningTurtle:saveForward()
- end
- MiningTurtle:turnRight()
- end
- -- Length loop
- for i = 0,length-1 do
- MiningTurtle:saveForward()
- if i % 2 == 0 then
- -- even
- -- only change direction if we mine a tunnel wider than 1 block
- if width > 1 then
- MiningTurtle:turnRight()
- end
- for x = 0,width-1 do
- MiningTurtle:tick()
- for y = 0,height-3 do
- if x % 2 == 0 then
- MiningTurtle:saveUp()
- else
- MiningTurtle:saveDown()
- end
- end
- if height > 1 then
- turtle.digUp()
- end
- if x < width-1 then
- MiningTurtle:saveForward()
- end
- end
- -- same as above
- if width > 1 then
- MiningTurtle:turnLeft()
- end
- else
- -- odd
- if width > 1 then
- MiningTurtle:turnLeft()
- end
- for x = 0,width-1 do
- MiningTurtle:tick()
- for y = 0,height-3 do
- if x % 2 == 1 or width % 2 == 0 then
- MiningTurtle:saveUp()
- else
- MiningTurtle:saveDown()
- end
- end
- if height > 1 then
- turtle.digUp()
- end
- if x < width-1 then
- MiningTurtle:saveForward()
- end
- end
- if width > 1 then
- MiningTurtle:turnRight()
- end
- end
- end
- if ret then
- -- Go up to not remove torches on the floow
- MiningTurtle:saveUp()
- MiningTurtle:goTo( returnPos )
- MiningTurtle:turnTo( returnDirection )
- end
- end
- function MiningTurtle:tick()
- -- refuel loop
- while not MiningTurtle:requiredRefuel( 20, 50 ) do
- sleep(1)
- end
- end
- --
- function MiningTurtle:matrix( rows, cols )
- MiningTurtle:tick()
- --MiningTurtle:toBaseLevel()
- MiningTurtle:tunnel(5, 2, 1, true)
- end
- -- main program
- baby = MiningTurtle:new()
- baby:matrix( 10, 10 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement