diff --git a/pool.lua b/pool.lua index 3753b44..9fdfee7 100644 --- a/pool.lua +++ b/pool.lua @@ -14,18 +14,11 @@ local hasThreads = config.NeatConfig.Threads > 1 -- Only the parent should manage ticks! --- I'm not terribly thrilled with this logic -if hasThreads then - callback.register('timer', function() - Promise.update() - set_timer_timeout(1) - end) +callback.register('timer', function() + Promise.update() set_timer_timeout(1) -else - callback.register('input', function() - Promise.update() - end) -end +end) +set_timer_timeout(1) local Runner = nil if hasThreads then @@ -453,7 +446,8 @@ local function loadFile(filename) return end local contents = file:read("*all") - local ok, obj = serpent.load(libDeflate:DecompressDeflate(contents:sub(11, #contents - 8))) + local decomp, _ = libDeflate:DecompressDeflate(contents:sub(11, #contents - 8)) + local ok, obj = serpent.load(decomp) if not ok then message("Error parsing pool file", 0x00990000) return @@ -465,8 +459,9 @@ end local function savePool() local filename = _M.saveLoadFile - writeFile(filename) - message(string.format("Saved \"%s\"!", filename:sub(#filename - 50)), 0x00009900) + return writeFile(filename):next(function() + message(string.format("Saved \"%s\"!", filename:sub(#filename - 50)), 0x00009900) + end) end local function loadPool() @@ -716,7 +711,7 @@ local function mainLoop(currentSpecies, topGenome) if saveRequested then saveRequested = false - savePool() + return savePool() end if topRequested then diff --git a/runner-process.lua b/runner-process.lua index 4a63647..b164db8 100644 --- a/runner-process.lua +++ b/runner-process.lua @@ -4,9 +4,12 @@ local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1") local Promise = dofile(base.."/promise.lua") -- Only the parent should manage ticks! -callback.register('input', function() - Promise.update() +callback.register('timer', function() + Promise.update() + set_timer_timeout(1) end) +set_timer_timeout(1) + local Runner = dofile(base.."/runner.lua") local serpent = dofile(base.."/serpent.lua") diff --git a/runner.lua b/runner.lua index 2d87a1d..794df0b 100644 --- a/runner.lua +++ b/runner.lua @@ -379,7 +379,20 @@ local function evaluateNetwork(_M, network, inputs, inputDeltas) return outputs end -local controller = nil +local controller = {} +local function updateController() + for b=0,#config.ButtonNames - 1,1 do + if controller[b] then + input.set(0, b, 1) + else + input.set(0, b, 0) + end + end +end + +local frame = 0 +local lastFrame = 0 + local function evaluateCurrent(_M) local genome = _M.currentSpecies.genomes[_M.currentGenomeIndex] @@ -396,14 +409,6 @@ local function evaluateCurrent(_M) controller[4] = false controller[5] = false end - - for b=0,#config.ButtonNames - 1,1 do - if controller[b] then - input.set(0, b, 1) - else - input.set(0, b, 0) - end - end end local function fitnessAlreadyMeasured(_M) @@ -421,9 +426,6 @@ local function rewind() return promise end -local frame = 0 -local lastFrame = 0 - local function rewound() frame = 0 lastFrame = 0 @@ -547,14 +549,6 @@ local function mainLoop(_M, genome) evaluateCurrent(_M) end - for b=0,#config.ButtonNames - 1,1 do - if controller[b] then - input.set(0, b, 1) - else - input.set(0, b, 0) - end - end - game.getPositions() local timeoutConst = 0 if game.vertical then @@ -861,6 +855,7 @@ local function run(_M, species, generationIdx, genomeCallback) end) register(_M, 'input', function() frame = frame + 1 + updateController() processFrameAdvanced(_M) saveLoadInput(_M) end)