Note I use the assert(param1,param2)as shorthand for writing if <param1> == false then error(param2) end
When I get some time I'll modify my original program in this style. This was the first turtle program I wanted to make, to learn coroutines. My real project is a turtle autorun to upload arbitrary programs to the turtle... which I think I am in a better position to tackle now.
Thanks for the help,
function main(cmds)
local routines = {}
local eventdata = {}
local filter = {}
local status
local n
routines[1] = coroutine.create(go)
routines[2] = coroutine.create(fuelManager)
while true do
for i,r in ipairs(routines) do
if filter[r] == nil or filter[r] == eventdata[1] or eventdata[1] == "terminate" then
status, param = coroutine.resume(r, unpack(eventdata))
assert(status,paraam)
filter[r] = param
if coroutine.status(r) == "dead" then
return i
end
end
end
for i,r in ipairs(routines) do
if coroutine.status(r) == "dead" then
return i
end
end
os.queueEvent("docmds", unpack(cmds))
eventdata = { coroutine.yield() }
end
end
function go()
local tcmds = { }
tcmds = { coroutine.yield("docmds") }
local eventID = tcmds[1]
table.remove(tcmds,1)
if eventID == "docmds" then
for i,v in ipairs(tcmds) do
print(v)
sleep(1)
end
end
end
function fuelManager()
FuelOnHand = 40
while FuelOnHand > 10 do
FuelOnHand = FuelOnHand - 5
print("Fuel: " .. FuelOnHand)
sleep(1)
end
end
local targs = { ... }
local func = ""
local i = 0
local func = main(targs)
if func == 1 then
print("Commands Completed")
elseif func == 2 then
print("Out of Gas")
else
print("Not sure what happened ... " .. func)
end



