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