From 7a61b20e6307e688ec5c59869258b5c988aa8a7f Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Sun, 5 Jul 2020 12:11:24 +0200 Subject: [PATCH] Make digging gravel safe. Report fuel values --- dominic/canemine.lua | 90 ++++++++++++++++++-------------------------- dominic/server.lua | 7 +++- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index 2d04456..2fbc20a 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -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 diff --git a/dominic/server.lua b/dominic/server.lua index 47fc763..309ee89 100644 --- a/dominic/server.lua +++ b/dominic/server.lua @@ -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