Implement proto
This commit is contained in:
parent
bcbaba516c
commit
bdf9d150a6
@ -123,12 +123,9 @@ function grabOres()
|
|||||||
local status, block = turtle.inspect()
|
local status, block = turtle.inspect()
|
||||||
if status and block["name"]:lower():find("ore") then
|
if status and block["name"]:lower():find("ore") then
|
||||||
ore = block["name"]:lower()
|
ore = block["name"]:lower()
|
||||||
print("digging"..tostring(ore))
|
|
||||||
if turtlestate["ores"][ore] then
|
if turtlestate["ores"][ore] then
|
||||||
print("+1")
|
|
||||||
turtlestate["ores"][ore] = turtlestate["ores"][ore] + 1
|
turtlestate["ores"][ore] = turtlestate["ores"][ore] + 1
|
||||||
else
|
else
|
||||||
print("=1")
|
|
||||||
turtlestate["ores"][ore] = 1
|
turtlestate["ores"][ore] = 1
|
||||||
end
|
end
|
||||||
turtle.dig()
|
turtle.dig()
|
||||||
@ -503,13 +500,18 @@ function enqueueForJob2()
|
|||||||
end
|
end
|
||||||
turtle.turnLeft()
|
turtle.turnLeft()
|
||||||
rednet.open("bottom")
|
rednet.open("bottom")
|
||||||
|
|
||||||
local retrying = false
|
local retrying = false
|
||||||
while true do
|
while true do
|
||||||
rednet.broadcast("gibjob", "jobs")
|
rednet.broadcast("syn")
|
||||||
sender, message, proto = rednet.receive("newjob", 2)
|
server, message, proto = rednet.receive(2) -- ACK
|
||||||
|
if not message then
|
||||||
|
retrying = true
|
||||||
|
else
|
||||||
|
rednet.send(server, "gibjob")
|
||||||
|
_, job, _ = rednet.receive()
|
||||||
if message then
|
if message then
|
||||||
job = tonumber(message)
|
return tonumber(job)
|
||||||
return job
|
|
||||||
else
|
else
|
||||||
if not retrying then
|
if not retrying then
|
||||||
retrying = true
|
retrying = true
|
||||||
@ -520,6 +522,7 @@ function enqueueForJob2()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function faceDir(dir)
|
function faceDir(dir)
|
||||||
if ((direction - dir) % 4 == 2) then
|
if ((direction - dir) % 4 == 2) then
|
||||||
@ -658,25 +661,27 @@ function reportDuty2()
|
|||||||
id = turtlestate["job"]
|
id = turtlestate["job"]
|
||||||
fuelconsumed = startfuel - turtle.getFuelLevel()
|
fuelconsumed = startfuel - turtle.getFuelLevel()
|
||||||
turtlestate["fuel"] = fuelconsumed
|
turtlestate["fuel"] = fuelconsumed
|
||||||
rednet.broadcast(tostring(id), "jobs")
|
|
||||||
sleep(0.6)
|
rednet.broadcast("syn")
|
||||||
--print("Proto: Sending fuel status")
|
server, _, _ = rednet.receive() -- ACK
|
||||||
rednet.broadcast(tostring(fuelconsumed), "fuel")
|
rednet.send(server, "report")
|
||||||
while true do
|
rednet.send(server, tostring(id))
|
||||||
--print("Proto: Awaiting thanks")
|
rednet.send(server, tostring(fuelconsumed))
|
||||||
updateState({ state = "Waiting for thanks..." })
|
--while true do
|
||||||
sender, message, proto = rednet.receive("jobcomplete", 2)
|
-- updateState({ state = "Waiting for thanks..." })
|
||||||
if message == "thanks" then
|
-- sender, message, proto = rednet.receive("jobcomplete", 2)
|
||||||
break
|
-- if message == "thanks" then
|
||||||
else
|
-- break
|
||||||
sleep(1)
|
-- else
|
||||||
end
|
-- sleep(1)
|
||||||
end
|
-- end
|
||||||
|
--end
|
||||||
for k, v in pairs(turtlestate["ores"]) do
|
for k, v in pairs(turtlestate["ores"]) do
|
||||||
print(k,v)
|
rednet.send(server, k)
|
||||||
|
rednet.send(server, v)
|
||||||
end
|
end
|
||||||
exit()
|
rednet.broadcast("done", "ores")
|
||||||
--print("Proto: Received thanks")
|
server, _, _ = rednet.receive() -- thanks
|
||||||
end
|
end
|
||||||
|
|
||||||
function turtleAI2()
|
function turtleAI2()
|
||||||
|
@ -17,30 +17,58 @@ function getNextJob()
|
|||||||
end
|
end
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
sender, message, proto = rednet.receive("jobs")
|
sender, message, proto = rednet.receive()
|
||||||
if not message then
|
if not message then
|
||||||
sleep(0.6)
|
sleep(0.6)
|
||||||
else
|
else
|
||||||
--print("> raw: "..message)
|
if message == "syn" then
|
||||||
|
receiver = sender
|
||||||
|
rednet.send(receiver, "ack")
|
||||||
|
_, message, _ = rednet.receive()
|
||||||
|
if message then
|
||||||
if message == "gibjob" then
|
if message == "gibjob" then
|
||||||
if rs.getInput("top") then
|
if rs.getInput("top") then
|
||||||
job = getNextJob()
|
job = getNextJob()
|
||||||
rednet.broadcast(tostring(job), "newjob")
|
rednet.send(receiver, tostring(job))
|
||||||
print("Found job request. Offering "..tostring(job))
|
print("Found job request. Offering "..tostring(job))
|
||||||
end
|
end
|
||||||
else
|
elseif message == "report" then
|
||||||
-- numberic job is complete
|
_, job, _ = rednet.receive()
|
||||||
completejob = tonumber(message)
|
thejob = tonumber(job)
|
||||||
fuelused = "??"
|
_, fuel, _ = rednet.receive()
|
||||||
fuelsender, fuelmessage, fuelproto = rednet.receive("fuel")
|
thefuel = tonumber(fuel)
|
||||||
if fuelmessage then
|
while true do
|
||||||
fuelused = fuelmessage
|
_, ore, _ = rednet.receive()
|
||||||
|
if ore == "done" then
|
||||||
|
break
|
||||||
end
|
end
|
||||||
print("Job "..tostring(completejob).." was completed, "..fuelused.." fuel was used")
|
_, number, _ = rednet.receive()
|
||||||
rednet.broadcast("thanks", "jobcomplete")
|
--thenumber = tonumber(thenumber)
|
||||||
log = io.open("jobs.log","a")
|
print(tostring(job)..": "..ore.." "..tostring(number).."x")
|
||||||
log:write(tostring(completejob),"\n")
|
end
|
||||||
log:close()
|
rednet.send(receiver, "thanks")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- if message == "gibjob" then
|
||||||
|
-- if rs.getInput("top") then
|
||||||
|
-- job = getNextJob()
|
||||||
|
-- rednet.send(sender, tostring(job), "newjob")
|
||||||
|
-- print("Found job request. Offering "..tostring(job))
|
||||||
|
-- end
|
||||||
|
-- else
|
||||||
|
-- -- numberic job is complete
|
||||||
|
-- completejob = tonumber(message)
|
||||||
|
-- fuelused = "??"
|
||||||
|
-- fuelsender, fuelmessage, fuelproto = rednet.receive("fuel")
|
||||||
|
-- if fuelmessage then
|
||||||
|
-- fuelused = fuelmessage
|
||||||
|
-- end
|
||||||
|
-- print("Job "..tostring(completejob).." was completed, "..fuelused.." fuel was used")
|
||||||
|
-- rednet.broadcast("thanks", "jobcomplete")
|
||||||
|
-- log = io.open("jobs.log","a")
|
||||||
|
-- log:write(tostring(completejob),"\n")
|
||||||
|
-- log:close()
|
||||||
|
-- end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user