Make digging gravel safe. Report fuel values

This commit is contained in:
Dominic Zimmer 2020-07-05 12:11:24 +02:00
parent e077be9e95
commit 7a61b20e63
2 changed files with 43 additions and 54 deletions

View File

@ -39,22 +39,25 @@ function ithSpot(i)
end
end
-- function digTo(x, z)
-- print("Digging to ("..tostring(x)..", "..tostring(z)..")")
-- while z > 0 do
-- turtle.dig()
-- turtle.forward()
-- turtle.digUp()
-- z = z - 1
-- end
-- turtle.turnRight()
-- while x > 0 do
-- turtle.dig()
-- turtle.forward()
-- turtle.digUp()
-- x = x - 1
-- end
-- end
-- move safely into the direction, not hurting other turtles, but freeing the path if needed.
function moveSafe(moveAction, inspectAction, digAction)
while true do
local status, block = inspectAction()
if status then
if block["name"]:lower():find("turtle") then
-- turtle in front of me. lets wait for it to move
sleep(0.6)
else
digAction()
end
else
-- nothing in the way, try to move
if moveAction() then
break
end
end
end
end
-- If the block in front of the turtle is desired, take it.
function grabOres()
@ -142,43 +145,22 @@ end
function digTo(x, z)
print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")")
while z > 0 do
turtle.dig()
turtle.forward()
turtle.digUp()
moveSafe(turtle.forward, turtle.inspect, turtle.dig)
z = z - 1
repeat sleep(0.6)
until (not turtle.digUp())
end
turtle.turnRight()
while x > 0 do
turtle.dig()
turtle.forward()
turtle.digUp()
moveSafe(turtle.forward, turtle.inspect, turtle.dig)
x = x - 1
end
end
-- move safely into the direction, not hurting other turtles, but freeing the path if needed.
function moveSafe(moveAction, inspectAction, digAction)
while true do
local status, block = inspectAction()
if status then
if block["name"]:lower():find("turtle") then
-- turtle in front of me. lets wait for it to move
sleep(0.6)
else
digAction()
end
else
-- nothing in the way, try to move
if moveAction() then
break
end
end
repeat sleep(0.6)
until (not turtle.digUp())
end
end
function goToSpawn(x, z)
turtle.digUp()
turtle.up()
moveSafe(turtle.up, turtle.inspectUp, turtle.digUp)
while z > 0 do
moveSafe(turtle.forward, turtle.inspect, turtle.dig)
z = z - 1
@ -240,7 +222,7 @@ function leaveBarrel(path)
end
end
function reportDuty(id)
function reportDuty(id, fuelconsumed)
moveSafe(turtle.down, turtle.inspectDown, noop)
moveSafe(turtle.down, turtle.inspectDown, noop)
moveSafe(turtle.down, turtle.inspectDown, noop)
@ -254,6 +236,8 @@ function reportDuty(id)
moveSafeForward()
rednet.open("bottom")
rednet.broadcast(tostring(id), "jobs")
sleep(0.6)
rednet.broadcast(tostring(fuelconsumed), "fuel")
while true do
sender, message, proto = rednet.receive("jobcomplete")
if message == "thanks" then
@ -299,18 +283,17 @@ function enqueueTurtle()
end
function doRefuel()
moveSafeForward()
turtle.turnRight()
while turtle.getFuelLevel() < 1500 do
turtle.suck(4)
selectFuel()
turtle.refuel()
end
turtle.turnLeft()
end
function goToJob()
moveSafeForward()
turtle.turnRight()
doRefuel()
turtle.turnLeft()
moveSafeForward()
moveSafeForward()
moveSafeForward()
@ -344,8 +327,9 @@ function emptyToBarrel()
end
while true do
fuel = turtle.getFuelLevel()
thejob = waitForJob()
doRefuel()
fuel = turtle.getFuelLevel()
goToJob()
x, z = ithSpot(thejob)
digTo(x,z)
@ -355,8 +339,8 @@ while true do
goToBarrel(path)
emptyToBarrel()
leaveBarrel(path)
reportDuty(thejob)
enqueueTurtle()
fuelconsumed = fuel - turtle.getFuelLevel()
print("Consumed "..tostring(fuelconsumed).." fuel")
reportDuty(thejob, fuelconsumed)
print("I consumed "..tostring(fuelconsumed).." fuel")
enqueueTurtle()
end

View File

@ -30,7 +30,12 @@ while true do
else
-- numberic job is complete
completejob = tonumber(message)
print("Job "..tostring(completejob).." was completed")
fuelused = "??"
fuelsender, fuelmessage, fuelproto = rednet.receive("fuel")
if fuelmessage then
fuelused = fuelmessage
end
print("Job "..tostring(completejob).." was completed, "..fu elused.." fuel was used")
rednet.broadcast("thanks", "jobcomplete")
end
end