Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERVER_ID = 0
- FUEL_COUNT = -1
- FUEL_INDEX = -1
- ACCEPTABLE_FUEL = {"minecraft:coal"}
- BLOCK_COUNT = -1
- BLOCK_INDEX = -1
- BUILDING_BLOCKS = {"minecraft:cobblestone", "minecraft:dirt", "minecraft:andesite", "minecraft:granite", "minecraft:diorite", "minecraft:cobbled_deepslate", "minecraft:tuff"}
- function arrHasValue(arr, val)
- for index, value in ipairs(arr) do
- if value == val then
- return true
- end
- end
- return false
- end
- function hasFuel()
- if FUEL_INDEX == -1 then
- for i=1, 16 do
- turtle.select(i)
- local slotData = turtle.getItemDetail()
- if slotData ~= nil then
- if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == true then
- FUEL_INDEX = i
- FUEL_COUNT = slotData["count"]
- break
- end
- end
- end
- end
- return FUEL_COUNT > 0
- end
- function shouldRefuel()
- return turtle.getFuelLevel() <= 0
- end
- function refuel()
- turtle.select(FUEL_INDEX)
- turtle.refuel(1)
- FUEL_COUNT = FUEL_COUNT - 1
- end
- function getBlockIndex()
- if BLOCK_COUNT == 0 then
- BLOCK_INDEX = -1
- end
- if BLOCK_INDEX == -1 then
- for i=1, 16 do
- turtle.select(i)
- local slotData = turtle.getItemDetail()
- if slotData ~= nil then
- if arrHasValue(BUILDING_BLOCKS, slotData["name"]) == true then
- BLOCK_INDEX = i
- BLOCK_COUNT = slotData["count"]
- break
- end
- end
- end
- end
- return BLOCK_INDEX
- end
- function dumpDown()
- for i=1, 16 do
- turtle.select(i)
- local slotData = turtle.getItemDetail()
- if slotData ~= nil then
- if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == false then
- turtle.dropDown(slotData["count"])
- end
- end
- end
- end
- -- +x -> east
- -- -x -> west
- -- -z -> north
- -- +z -> south
- function getFacingDirection(x, z, prevX, prevZ)
- if x < prevX then
- return "west"
- elseif x > prevX then
- return "east"
- end
- if z < prevZ then
- return "north"
- elseif z > prevZ then
- return "south"
- end
- end
- -- +x -> east = 0
- -- -z -> north = 1
- -- -x -> west = 2
- -- +z -> south = 3
- function rotateToFace(currentFacing, target)
- local turning = {
- east = 0,
- north = 1,
- west = 2,
- south = 3
- }
- if currentFacing == target then
- return
- end
- local currFacingVal = turning[currentFacing]
- local targFacingVal = turning[target]
- local offset = targFacingVal - currFacingVal
- if offset < 0 then
- offset = offset + 3
- end
- local moveFacing = currentFacing
- for i=0, offset do
- moveFacing = left(moveFacing)
- end
- end
- -- +x -> east
- -- -x -> west
- -- -z -> north
- -- +z -> south
- function incrementByFacing(facing, x, z, increment)
- if facing == "west" then
- return (x - increment), z
- elseif facing == "east" then
- return (x + increment), z
- elseif facing == "north" then
- return x, (z - increment)
- elseif facing == "south" then
- return x, (z + increment)
- end
- end
- function left(facing)
- turtle.turnLeft()
- if facing == "west" then
- return "south"
- elseif facing == "east" then
- return "north"
- elseif facing == "north" then
- return "west"
- elseif facing == "south" then
- return "east"
- end
- end
- function right(facing)
- turtle.turnRight()
- if facing == "west" then
- return "north"
- elseif facing == "east" then
- return "south"
- elseif facing == "north" then
- return "east"
- elseif facing == "south" then
- return "west"
- end
- end
- function moveToward(x, z, facing)
- local currX, currY, currZ = gps.locate()
- -- Change facing direction if we need to
- local newFacing = facing
- if currX < x then
- rotateToFace(facing, "east")
- newFacing = "east"
- elseif currX > x then
- rotateToFace(facing, "west")
- newFacing = "west"
- else
- if currZ < z then
- rotateToFace(facing, "south")
- newFacing = "south"
- elseif currZ > z then
- rotateToFace(facing, "north")
- newFacing = "north"
- end
- end
- -- Dig the block in front
- if turtle.detect() == true then
- turtle.dig()
- end
- -- Move forward
- turtle.forward()
- -- If there is no block below, place one if possible
- if turtle.detectDown() == false then
- local blockIndex = getBlockIndex()
- if blockIndex ~= -1 then
- turtle.select(blockIndex)
- turtle.placeDown()
- end
- end
- -- Dig the block above
- if turtle.detectUp() == true then
- turtle.digUp()
- end
- return newFacing
- end
- function moveBackTo(x, z, facing)
- local currX, currY, currZ = gps.locate()
- local newFacing = facing
- if currZ < z then
- rotateToFace(facing, "south")
- newFacing = "south"
- elseif currZ > z then
- rotateToFace(facing, "north")
- newFacing = "north"
- else
- if currX < x then
- rotateToFace(facing, "east")
- newFacing = "east"
- elseif currX > x then
- rotateToFace(facing, "west")
- newFacing = "west"
- end
- end
- local success = turtle.forward()
- if success == false then
- return ""
- end
- -- Pick up any blocks that may have dropped on the ground (e.g. gravel)
- turtle.suck()
- return newFacing
- end
- function reachedTarget(x, z, targetX, targetZ)
- return x == targetX and z == targetZ
- end
- function loop(targetX, targetZ, facing)
- if shouldRefuel() == true then
- if hasFuel() == true then
- refuel()
- else
- print("Not enough fuel!")
- rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
- return ""
- end
- end
- return moveToward(targetX, targetZ, facing)
- end
- function returnTo(targetX, targetZ, facing)
- if shouldRefuel() == true then
- if hasFuel() == true then
- refuel()
- else
- print("Not enough fuel!")
- rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
- return ""
- end
- end
- return moveBackTo(targetX, targetZ, facing)
- end
- function prepare()
- print("Connecting...")
- rednet.broadcast("turtle", "minenettoserver")
- local id, message = rednet.receive("minenettoclient")
- SERVER_ID = id
- print("Connected!")
- local id, distance = rednet.receive("minenettoclient")
- if shouldRefuel() == true then
- if hasFuel() == true then
- refuel()
- else
- print("Not enough fuel!")
- rednet.send(SERVER_ID, "out_of_fuel", "minenettoserver")
- return nil
- end
- end
- return distance
- end
- function main()
- rednet.open("left")
- if rednet.isOpen("left") ~= true then
- print("Failed to start modem!")
- return
- end
- local distance = prepare()
- if distance ~= nil then
- local currentX, currentY, currentZ = gps.locate()
- local startX, startZ = currentX, currentZ
- if turtle.detect() == true then
- turtle.dig()
- end
- turtle.forward()
- local nextX, nextY, nextZ = gps.locate()
- turtle.back()
- if turtle.detectUp() == true then
- turtle.digUp()
- end
- local facing = getFacingDirection(nextX, nextZ, currentX, currentZ)
- local targetX, targetZ = incrementByFacing(facing, currentX, currentZ, distance)
- print("Heading to target: " .. targetX .. ", " .. targetZ)
- while reachedTarget(currentX, currentZ, targetX, targetZ) ~= true do
- facing = loop(targetX, targetZ, facing)
- currentX, currentY, currentZ = gps.locate()
- if facing == "" then
- break
- end
- end
- rednet.send(SERVER_ID, "returning", "minenettoserver")
- while reachedTarget(currentX, currentZ, startX, startZ) ~= true do
- facing = returnTo(startX, startZ, facing)
- currentX, currentY, currentZ = gps.locate()
- if facing == "" then
- break
- end
- end
- rednet.send(SERVER_ID, "returned", "minenettoserver")
- while reachedTarget(currentX, currentZ, startX, startZ) ~= true do
- facing = returnTo(startX, startZ, facing)
- currentX, currentY, currentZ = gps.locate()
- if facing == "" then
- break
- end
- end
- dumpDown()
- left(facing)
- left(facing)
- rednet.send(SERVER_ID, "dumped", "minenettoserver")
- end
- rednet.close("left")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement