Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- for chunky turtles with pickaxes
- Description: This turtle will lay blocks from its current location to a starting location
- Usage:[all parameters are required]
- placeToTarget currentX currentY currentZ targetX targetY targetZ depth
- Directions: The turtle uses reletive position to track its current position,
- It must be placed facing 0,0,0 on the z meaning if you are south of 0,0,0 it must face north
- but if you are north of 0,0,0 you must face south.
- you must load a fuel source in slot 1 and have enough fuel to make the trip to the target
- you must load the block(including wires or redstone ex..) into slots 5-8
- Notes: if your server crashes while the turtle is working you can find the
- position within the world/computercraft/computers/computerId/scriptDirectory/turtle
- @TODO
- * Auto resume if after server reboot
- * Communicate Wirelessly (will require a pair of turtles)
- * Start the turtle wirelessly
- * create a gui for managing the turtles wirelessy
- --]]
- -- set global variables
- startX = 0
- startY = 0
- startZ = 0
- targetX = 0
- targetY = 0
- targetZ = 0
- depth = 0
- currentX = 0
- currentY = 0
- currentZ = 0
- placeItemSlot = 5
- moves = 0
- updateXZ = {
- [1] = function() -- facing north add to z
- currentZ = currentZ - 1
- end,
- [2] = function() -- facing east add to x
- currentX = currentX + 1
- end,
- [3] = function() -- facing south subtract from z
- currentZ = currentZ + 1
- end,
- [4] = function() -- facing west subtract from x
- currentX = currentX - 1
- end
- }
- facing = 1
- --utility functions
- function storeData() -- This will store the turtles current state
- local data = {
- ["currentX"] = currentX,
- ["currentY"] = currentY,
- ["currentZ"] = currentZ,
- ["targetX"] = targetX,
- ["targetY"] = targetY,
- ["targetZ"] = targetZ,
- ["facing"] = facing
- }
- local file = fs.open("turtle","w")
- file.write(textutils.serialize(data))
- file.close()
- end
- function say(message)
- print(message)
- end
- function getDistanceBetween(x,y)
- say("distance between " .. x .. ", " .. y)
- return x - y
- end
- function getAbsoluteDistance(x,y)
- say("absolute distance between " .. x .. ", " .. y)
- return math.abs(x - y)
- end
- function getTotalMoves()
- local xDistance = getAbsoluteDistance(startX , targetX)
- local zDistance = getAbsoluteDistance(startZ , targetZ)
- local yDistance = getAbsoluteDistance(startY , depth)
- local targetYDistance = getAbsoluteDistance(targetY , depth)
- moves = yDistance + xDistance + zDistance + targetYDistance
- say("we need " .. moves .. " fuel To get there")
- return moves
- end
- function refuel()
- say("Refueling System")
- while turtle.getFuelLevel() < moves do
- turtle.select(1)
- turtle.refuel();
- end
- turtle.select(placeItemSlot)
- say("Refueled")
- return true
- end
- --Rotation Functions
- function turnRight(turns)
- turns = turns or 1
- while turns > 0 do
- turns = turns - 1
- turtle.turnRight()
- if facing == 4 then
- facing = 1
- else
- facing = facing + 1
- end
- storeData()
- end
- end
- function turnLeft(turns)
- turns = turns or 1
- while turns > 0 do
- turns = turns - 1
- turtle.turnLeft()
- if facing == 1 then
- facing = 4
- else
- facing = facing - 1
- end
- storeData()
- end
- end
- function faceNorth()
- while facing ~= 1 do
- turnRight()
- end
- end
- function faceTargetZ()
- if targetZ > currentZ then
- while facing ~= 3 do
- turnRight()
- end
- else
- while facing ~= 1 do
- turnRight()
- end
- end
- end
- function faceTargetX()
- if targetX < currentX then
- while facing ~= 4 do
- turnRight()
- end
- else
- while facing ~= 2 do
- turnRight()
- end
- end
- end
- --Movement Functions
- function moveForward()
- if turtle.detect() == false then
- turtle.forward()
- updateXZ[facing]()
- storeData()
- else
- say("I cant move forward")
- end
- end
- function moveDown()
- if turtle.detectDown() == false then
- turtle.down()
- currentY = currentY - 1
- storeData()
- else
- say("I cant move down")
- end
- end
- function moveUp()
- if turtle.detectUp() == false then
- turtle.up()
- currentY = currentY + 1
- storeData()
- else
- say("I cant move Up")
- end
- end
- --actions
- function refilSlotFromRight()
- say("refilling main item slot")
- local fromSlot = placeItemSlot + 1
- local targetSlot = fromSlot - 1
- while targetSlot < placeItemSlot + 4 do
- turtle.select(fromSlot)
- say("transfering to Slot: " .. targetSlot .. " from slot: " .. turtle.getSelectedSlot())
- turtle.transferTo(targetSlot,1)
- fromSlot = fromSlot + 1
- targetSlot = fromSlot - 1
- end
- turtle.select(placeItemSlot)
- end
- function placeItem()
- turnRight(2)
- turtle.place(placeItemSlot)
- refilSlotFromRight()
- turnRight(2)
- end
- --Main Functions
- function moveAndPlace()
- turtle.dig()
- moveForward()
- placeItem()
- end
- --Core Functions
- function goToTargetZ()
- while getAbsoluteDistance(targetZ,currentZ) > 0 do
- say("current Z distance is " .. getAbsoluteDistance(targetZ,currentZ))
- moveAndPlace()
- end
- end
- function goToTargetX()
- while getAbsoluteDistance(targetX,currentX) > 0 do
- say("current X distance is " .. getAbsoluteDistance(targetX,currentX))
- moveAndPlace()
- end
- end
- function goToDepth()
- say("moving to target depth")
- while getAbsoluteDistance(depth,currentY)>0 do
- turtle.digDown()
- turtle.placeUp(placeItemSlot)
- moveDown()
- say("placing item above me from slot " .. placeItemSlot)
- end
- end
- function goToTargetY()
- say("moving to target Y")
- while getAbsoluteDistance(targetY,currentY ) > 0 do
- turtle.digUp()
- moveUp()
- say("placing item below me from slot " .. placeItemSlot)
- turtle.placeDown(placeItemSlot)
- end
- end
- function systemsReady()
- say("doing systems check")
- if #arg >=7 then
- startX = tonumber(arg[1])
- startY = tonumber(arg[2])
- startZ = tonumber(arg[3])
- targetX = tonumber(arg[4])
- targetY = tonumber(arg[5])
- targetZ = tonumber(arg[6])
- depth = tonumber(arg[7])
- currentX = startX
- currentY = startY
- currentZ = startZ
- say("Checking ItemInventory")
- if turtle.getItemCount(placeItemSlot) > moves then
- if refuel() then
- turtle.select(placeItemSlot)
- say("Lets place these bitches")
- return true
- else
- say("Feed me semour")
- end
- else
- say("We Need An Item in Slot " .. placeItemSlot)
- end
- else
- say("Tell me where to go (e.g placeTotarget x y z [optional] depth")
- end
- return false
- end
- function initialize()
- say("initializing")
- local systemIsReady = systemsReady()
- if systemIsReady == true then
- getTotalMoves()
- goToDepth()
- storeData()
- faceTargetZ()
- goToTargetZ()
- faceTargetX()
- goToTargetX()
- goToTargetY()
- faceNorth()
- end
- end
- initialize()
Add Comment
Please, Sign In to add comment