Step 1. Comment out existing co-routine stuff
Code: Select all
-- Simple coroutine management functions
-- local co, pairs, assert = coroutine, pairs, assert
-- local long_tasks_in_progress = {}
-- function StartLongTask(f)
-- long_tasks_in_progress[co.create(f)] = true
-- end
-- function WorkOnLongTasks(...)
-- for c in pairs(long_tasks_in_progress) do
-- if co.status(c) == "dead" then
-- long_tasks_in_progress[c] = nil
-- else
-- assert(co.resume(c, ...))
-- end
-- end
-- end
-- YieldLongTask = co.yield
Step 2 - paste in new Joshua coroutine code at top of main.lua (I only use 1 LUA for the moment)
Code: Select all
local allottedTime
local startTime
local tasks = {}
Multithreading = {}
Multithreading.YieldLongTask = function()
if (os.clock() - startTime >= allottedTime) then
coroutine.yield()
end
--coroutine.yield()
end
Multithreading.StartLongTask = function(f)
tasks[coroutine.create(f)] = true
end
Multithreading.WorkOnLongTasks = function(allotted)
allottedTime = allotted or 0.0005
for task, value in pairs(tasks) do
if coroutine.status(task) == "dead" then
tasks[task] = nil
else
startTime = os.clock()
assert(coroutine.resume(task))
allottedTime = allottedTime - (os.clock() - startTime)
end
end
end
Step 3. Excepting the coroutine code just recently pasted in, search and replace:
YieldLongTask -> Multithreading.YieldLongTask
StartLongTask -> Multithreading.StartLongTask
WorkonLongTasks -> Multithreading.WorkonLongTasks
Doing this, it just hangs instantly. The following block is how my main.lua starts:
Code: Select all
function OnInit()
RequestGameSpeed(10)
SendChat "/name [Bot]ROFLbot"
SendChat("I am ROFLbot, a LUA Defcon API bot")
SendChat("Author: ROFLamingo (scallbe1@hotmail.com) or IV forums")
end
function OnTickReal()
Multithreading.WorkOnLongTasks()
end
function OnShutdown()
end
function OnTick()
if (DefconLevel ~= GetDefconLevel()) then
DefconLevel = GetDefconLevel()
SendChat("Defcon " .. DefconLevel)
if (DefconLevel == 5) then
OnFirstTickD5()
elseif (DefconLevel == 4) then
OnFirstTickD4()
elseif (DefconLevel == 3) then
OnFirstTickD3()
elseif( DefconLevel == 2) then
OnFirstTickD2()
elseif (DefconLevel == 1) then
OnFirstTickD1()
end
end
if (DefconLevel == 5) then
TickD5()
elseif (DefconLevel == 4) then
TickD4()
elseif (DefconLevel == 3) then
TickD3()
elseif (DefconLevel == 2) then
TickD2()
elseif (DefconLevel == 1) then
TickD1()
end
Multithreading.WorkOnLongTasks()
end
function OnFirstTickD5()
Multithreading.StartLongTask( function()
SendChat("Things to do at D5")
SendChat("DONE: Determine who are we, who are they, enumeration activities")
SendChat("DONE: Determine how many forward radar to place, and try to pick good places")
SendChat("Determine how many scout battleships to place, and place them")
SendChat("Determine how many aggressive subs to place, and send them on their way(?!)")
What am I doing wrong?