Fix reset functionality
This commit is contained in:
parent
fda82e0cd7
commit
0a59ab8b21
4 changed files with 60 additions and 2 deletions
17
pool.lua
17
pool.lua
|
@ -668,6 +668,10 @@ local function newGeneration()
|
|||
writeFile(_M.saveLoadFile .. ".gen" .. pool.generation .. ".pool")
|
||||
end
|
||||
|
||||
local function reset()
|
||||
return _M.run(true)
|
||||
end
|
||||
|
||||
local runner = Runner(Promise)
|
||||
runner.onMessage(function(msg, color)
|
||||
message(msg, color)
|
||||
|
@ -678,6 +682,9 @@ end)
|
|||
runner.onLoad(function(filename)
|
||||
_M.requestLoad(filename)
|
||||
end)
|
||||
runner.onReset(function(filename)
|
||||
_M.requestReset()
|
||||
end)
|
||||
runner.onRenderForm(function(form)
|
||||
processRenderForm(form)
|
||||
end)
|
||||
|
@ -687,6 +694,7 @@ local topRequested = false
|
|||
|
||||
local loadRequested = false
|
||||
local saveRequested = false
|
||||
local resetRequested = false
|
||||
local function mainLoop(currentSpecies, topGenome)
|
||||
if currentSpecies == nil then
|
||||
currentSpecies = 1
|
||||
|
@ -706,6 +714,11 @@ local function mainLoop(currentSpecies, topGenome)
|
|||
return savePool()
|
||||
end
|
||||
|
||||
if resetRequested then
|
||||
resetRequested = false
|
||||
return reset()
|
||||
end
|
||||
|
||||
if topRequested then
|
||||
topRequested = false
|
||||
return playTop()
|
||||
|
@ -780,6 +793,10 @@ function _M.requestSave(filename)
|
|||
saveRequested = true
|
||||
end
|
||||
|
||||
function _M.requestReset()
|
||||
resetRequested = true
|
||||
end
|
||||
|
||||
function _M.onMessage(handler)
|
||||
table.insert(_M.onMessageHandler, handler)
|
||||
end
|
||||
|
|
|
@ -86,6 +86,14 @@ runner.onLoad(function(filename)
|
|||
})
|
||||
end)
|
||||
|
||||
runner.onReset(function(filename)
|
||||
writeResponse({
|
||||
type = 'onReset',
|
||||
filename = filename,
|
||||
speciesId = speciesId,
|
||||
})
|
||||
end)
|
||||
|
||||
local function waitLoop(inputLine)
|
||||
return util.promiseWrap(function()
|
||||
local ok, inputData = serpent.load(inputLine)
|
||||
|
|
|
@ -45,6 +45,16 @@ local function onLoad(_M, handler)
|
|||
table.insert(_M.onLoadHandler, handler)
|
||||
end
|
||||
|
||||
local function reset(_M)
|
||||
for i=#_M.onResetHandler,1,-1 do
|
||||
_M.onResetHandler[i]()
|
||||
end
|
||||
end
|
||||
|
||||
local function onReset(_M, handler)
|
||||
table.insert(_M.onResetHandler, handler)
|
||||
end
|
||||
|
||||
local function onMessage(_M, handler)
|
||||
table.insert(_M.onMessageHandler, handler)
|
||||
end
|
||||
|
@ -109,6 +119,7 @@ return function(promise)
|
|||
|
||||
local _M = {
|
||||
onMessageHandler = {},
|
||||
onResetHandler = {},
|
||||
onSaveHandler = {},
|
||||
onLoadHandler = {},
|
||||
poppets = {},
|
||||
|
@ -143,6 +154,10 @@ return function(promise)
|
|||
onLoad(_M, handler)
|
||||
end
|
||||
|
||||
_M.onReset = function(handler)
|
||||
onReset(_M, handler)
|
||||
end
|
||||
|
||||
_M.run = function(speciesSlice, generationIdx, genomeCallback)
|
||||
local promise = Promise.new()
|
||||
promise:resolve()
|
||||
|
@ -180,6 +195,8 @@ return function(promise)
|
|||
load(_M, obj.filename)
|
||||
elseif obj.type == 'onSave' then
|
||||
save(_M, obj.filename)
|
||||
elseif obj.type == 'onReset' then
|
||||
reset(_M)
|
||||
elseif obj.type == 'onGenome' then
|
||||
for i=1,#speciesSlice,1 do
|
||||
local s = speciesSlice[i]
|
||||
|
|
20
runner.lua
20
runner.lua
|
@ -732,6 +732,18 @@ local function onLoad(_M, handler)
|
|||
table.insert(_M.onLoadHandler, handler)
|
||||
end
|
||||
|
||||
local function reset(_M)
|
||||
for i=#_M.onResetHandler,1,-1 do
|
||||
_M.onResetHandler[i]()
|
||||
end
|
||||
|
||||
message(_M, "Will be reset once all currently active threads finish", 0x00990000)
|
||||
end
|
||||
|
||||
local function onReset(_M, handler)
|
||||
table.insert(_M.onResetHandler, handler)
|
||||
end
|
||||
|
||||
local function keyhook (_M, key, state)
|
||||
if state.value == 1 then
|
||||
if key == "tab" then
|
||||
|
@ -753,9 +765,8 @@ local function keyhook (_M, key, state)
|
|||
_M.helddown = key
|
||||
load(_M)
|
||||
elseif key == "9" then
|
||||
-- FIXME Event inversion
|
||||
_M.helddown = key
|
||||
pool.run(true)
|
||||
reset(_M)
|
||||
end
|
||||
elseif state.value == 0 then
|
||||
_M.helddown = nil
|
||||
|
@ -904,6 +915,7 @@ return function(promise)
|
|||
onMessageHandler = {},
|
||||
onSaveHandler = {},
|
||||
onLoadHandler = {},
|
||||
onResetHandler = {},
|
||||
onRenderFormHandler = {},
|
||||
}
|
||||
|
||||
|
@ -923,6 +935,10 @@ return function(promise)
|
|||
onLoad(_M, handler)
|
||||
end
|
||||
|
||||
_M.onReset = function(handler)
|
||||
onReset(_M, handler)
|
||||
end
|
||||
|
||||
_M.run = function(species, generationIdx, genomeCallback)
|
||||
return run(_M, species, generationIdx, genomeCallback)
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue