2021-05-01 19:39:35 -04:00
|
|
|
local gui, utime, callback, set_timer_timeout = gui, utime, callback, set_timer_timeout
|
2021-04-29 20:19:56 -04:00
|
|
|
|
2021-04-09 15:06:55 -04:00
|
|
|
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
local Promise = dofile(base.."/promise.lua")
|
2021-05-01 19:39:35 -04:00
|
|
|
-- Only the parent should manage ticks!
|
2021-05-01 23:17:59 -04:00
|
|
|
callback.register('timer', function()
|
|
|
|
Promise.update()
|
|
|
|
set_timer_timeout(1)
|
2021-05-01 19:39:35 -04:00
|
|
|
end)
|
2021-05-01 23:17:59 -04:00
|
|
|
set_timer_timeout(1)
|
|
|
|
|
2021-04-09 15:06:55 -04:00
|
|
|
local Runner = dofile(base.."/runner.lua")
|
2021-04-23 15:39:11 -04:00
|
|
|
local serpent = dofile(base.."/serpent.lua")
|
2021-05-01 19:39:35 -04:00
|
|
|
local util = dofile(base.."/util.lua")(Promise)
|
2021-04-23 18:09:35 -04:00
|
|
|
|
2021-04-09 15:06:55 -04:00
|
|
|
local statusLine = nil
|
|
|
|
local statusColor = 0x0000ff00
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
local species = nil
|
2021-04-28 23:18:26 -04:00
|
|
|
local speciesId = -1
|
2021-04-28 18:54:39 -04:00
|
|
|
local generationIndex = nil
|
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
local inputPipeName = os.getenv("RUNNER_INPUT_PIPE")
|
|
|
|
local outputFilePath = os.getenv("RUNNER_OUTPUT_FILE")
|
|
|
|
|
|
|
|
print('Opening input pipe '..inputPipeName)
|
|
|
|
local inputPipe = util.openReadPipe(inputPipeName)
|
|
|
|
if inputPipe == nil then
|
|
|
|
error('Error opening input file')
|
|
|
|
end
|
|
|
|
print('Opened input pipe '..inputPipeName)
|
|
|
|
|
|
|
|
print('Opening output file '..outputFilePath)
|
|
|
|
local outputFile = nil
|
|
|
|
while outputFile == nil do
|
|
|
|
outputFile = io.open(outputFilePath, 'w')
|
|
|
|
end
|
|
|
|
print('Opened output file '..outputFilePath)
|
|
|
|
|
|
|
|
local function writeResponse(object)
|
|
|
|
outputFile:write(serpent.dump(object).."\n")
|
|
|
|
outputFile:flush()
|
|
|
|
end
|
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
local runner = Runner(Promise)
|
2021-04-09 15:06:55 -04:00
|
|
|
runner.onMessage(function(msg, color)
|
|
|
|
statusLine = msg
|
|
|
|
statusColor = color
|
2021-04-28 18:54:39 -04:00
|
|
|
print(msg)
|
2021-05-04 08:20:35 -04:00
|
|
|
|
|
|
|
writeResponse({
|
|
|
|
type = 'onMessage',
|
|
|
|
speciesId = speciesId,
|
|
|
|
msg = msg,
|
|
|
|
color = color,
|
|
|
|
})
|
2021-04-09 15:06:55 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
runner.onRenderForm(function(form)
|
2021-05-01 20:50:13 -04:00
|
|
|
local guiWidth, guiHeight = gui.resolution()
|
2021-04-24 06:00:28 -04:00
|
|
|
gui.left_gap(0)
|
2021-04-09 15:06:55 -04:00
|
|
|
gui.top_gap(0)
|
|
|
|
gui.bottom_gap(0)
|
|
|
|
gui.right_gap(0)
|
2021-04-24 06:00:28 -04:00
|
|
|
form:draw(0, 0)
|
2021-04-09 15:06:55 -04:00
|
|
|
|
|
|
|
if statusLine ~= nil then
|
2021-05-01 20:50:13 -04:00
|
|
|
gui.rectangle(0, guiHeight - 20, guiWidth, 20, 1, 0x00000000, statusColor)
|
2021-04-24 06:00:28 -04:00
|
|
|
gui.text(0, guiHeight - 20, statusLine, 0x00000000)
|
2021-04-09 15:06:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
-- This isn't passed up to the parent since we're handling the GUI.
|
|
|
|
end)
|
|
|
|
|
|
|
|
runner.onSave(function(filename)
|
2021-05-04 08:20:35 -04:00
|
|
|
writeResponse({
|
|
|
|
type = 'onSave',
|
|
|
|
filename = filename,
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
2021-04-09 15:06:55 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
runner.onLoad(function(filename)
|
2021-05-04 08:20:35 -04:00
|
|
|
writeResponse({
|
|
|
|
type = 'onLoad',
|
|
|
|
filename = filename,
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
2021-04-09 15:06:55 -04:00
|
|
|
end)
|
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
local function waitLoop(inputLine)
|
|
|
|
return util.promiseWrap(function()
|
|
|
|
local ok, inputData = serpent.load(inputLine)
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
if not ok or inputData == nil or speciesId == inputData[1].id then
|
2021-04-28 23:18:26 -04:00
|
|
|
print("Deserialization error")
|
2021-05-04 08:20:35 -04:00
|
|
|
return
|
2021-04-28 23:18:26 -04:00
|
|
|
end
|
2021-04-29 07:22:08 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
print('Received input from master process')
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
species = inputData[1]
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
speciesId = species.id
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
generationIndex = inputData[2]
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
print('Running')
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
return runner.run(
|
|
|
|
species,
|
|
|
|
generationIndex,
|
|
|
|
function(genome, index)
|
|
|
|
writeResponse({
|
2021-04-28 18:54:39 -04:00
|
|
|
type = 'onGenome',
|
|
|
|
genome = genome,
|
|
|
|
genomeIndex = index,
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
2021-05-04 08:20:35 -04:00
|
|
|
end
|
|
|
|
):next(function()
|
|
|
|
writeResponse({
|
2021-05-01 19:39:35 -04:00
|
|
|
type = 'onFinish',
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
2021-05-04 08:20:35 -04:00
|
|
|
end)
|
|
|
|
end):next(function()
|
|
|
|
return inputPipe:read("*l")
|
2021-05-01 19:39:35 -04:00
|
|
|
end):next(waitLoop)
|
|
|
|
end
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
local sec, usec = utime()
|
|
|
|
local ts = sec * 1000000 + usec
|
|
|
|
|
2021-05-04 08:20:35 -04:00
|
|
|
local waiter = util.promiseWrap(function()
|
|
|
|
return inputPipe:read("*l")
|
|
|
|
end)
|
|
|
|
|
|
|
|
writeResponse({ type = 'onInit', ts = ts })
|
2021-05-01 19:39:35 -04:00
|
|
|
|
|
|
|
print(string.format('Wrote init to output at %d', ts))
|
|
|
|
|
|
|
|
waiter:next(waitLoop):catch(function(error)
|
2021-05-04 08:20:35 -04:00
|
|
|
if type(error) == "table" then
|
|
|
|
error = "\n"..table.concat(error, "\n")
|
|
|
|
end
|
2021-05-03 03:23:04 -04:00
|
|
|
print('Runner process error: '..error)
|
|
|
|
io.stderr:write('Runner process error: '..error..'\n')
|
2021-05-04 08:20:35 -04:00
|
|
|
end)
|