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-28 18:54:39 -04:00
|
|
|
|
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-28 18:54:39 -04:00
|
|
|
local inputFilePath = os.getenv("RUNNER_INPUT_FILE")
|
|
|
|
local outputFilePath = os.getenv("RUNNER_OUTPUT_FILE")
|
2021-04-09 15:06:55 -04:00
|
|
|
|
|
|
|
local outContents = {}
|
|
|
|
|
|
|
|
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-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-04-09 15:06:55 -04:00
|
|
|
table.insert(
|
|
|
|
outContents,
|
2021-04-23 15:39:11 -04:00
|
|
|
serpent.dump({
|
2021-04-09 15:06:55 -04:00
|
|
|
type = 'onMessage',
|
2021-04-24 01:18:06 -04:00
|
|
|
speciesId = speciesId,
|
2021-04-09 15:06:55 -04:00
|
|
|
msg = msg,
|
|
|
|
color = color,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
table.insert(
|
|
|
|
outContents,
|
2021-04-23 15:39:11 -04:00
|
|
|
serpent.dump({
|
2021-04-09 15:06:55 -04:00
|
|
|
type = 'onSave',
|
2021-04-09 17:47:08 -04:00
|
|
|
filename = filename,
|
2021-04-24 01:18:06 -04:00
|
|
|
speciesId = speciesId,
|
2021-04-09 15:06:55 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
runner.onLoad(function(filename)
|
|
|
|
table.insert(
|
|
|
|
outContents,
|
2021-04-23 15:39:11 -04:00
|
|
|
serpent.dump({
|
2021-04-09 15:06:55 -04:00
|
|
|
type = 'onLoad',
|
2021-04-09 17:47:08 -04:00
|
|
|
filename = filename,
|
2021-04-24 01:18:06 -04:00
|
|
|
speciesId = speciesId,
|
2021-04-09 15:06:55 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
local function waitLoop()
|
2021-04-28 23:18:26 -04:00
|
|
|
local inputData = nil
|
|
|
|
local ok = false
|
|
|
|
while not ok or inputData == nil or speciesId == inputData[1].id do
|
|
|
|
local inputFile = io.open(inputFilePath, 'r')
|
|
|
|
ok, inputData = serpent.load(inputFile:read('*a'))
|
|
|
|
inputFile:close()
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-04-28 23:18:26 -04:00
|
|
|
if not ok then
|
|
|
|
print("Deserialization error")
|
|
|
|
end
|
2021-04-09 15:06:55 -04:00
|
|
|
end
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-04-29 07:22:08 -04:00
|
|
|
print('Received input from master process')
|
|
|
|
|
2021-04-28 18:54:39 -04:00
|
|
|
species = inputData[1]
|
|
|
|
|
|
|
|
speciesId = species.id
|
|
|
|
|
|
|
|
generationIndex = inputData[2]
|
|
|
|
|
|
|
|
outContents = {}
|
|
|
|
|
|
|
|
print('Running')
|
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
return runner.run(
|
2021-04-28 18:54:39 -04:00
|
|
|
species,
|
|
|
|
generationIndex,
|
|
|
|
function(genome, index)
|
|
|
|
table.insert(
|
|
|
|
outContents,
|
|
|
|
serpent.dump({
|
|
|
|
type = 'onGenome',
|
|
|
|
genome = genome,
|
|
|
|
genomeIndex = index,
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
|
|
|
)
|
2021-05-01 19:39:35 -04:00
|
|
|
end
|
|
|
|
):next(function()
|
|
|
|
table.insert(
|
|
|
|
outContents,
|
|
|
|
serpent.dump({
|
|
|
|
type = 'onFinish',
|
|
|
|
speciesId = speciesId,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
-- Truncate the input file to reduce the amount of time
|
|
|
|
-- wasted if we reopen it too early
|
|
|
|
local inputFile = io.open(inputFilePath, "w")
|
|
|
|
inputFile:close()
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
local waiter = nil
|
|
|
|
if util.isWin then
|
|
|
|
waiter = Promise.new()
|
|
|
|
waiter:resolve()
|
|
|
|
else
|
|
|
|
waiter = util.waitForFiles(inputFilePath)
|
|
|
|
end
|
2021-04-28 23:29:43 -04:00
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
-- Write the result
|
|
|
|
local outFile = io.open(outputFilePath, "w")
|
|
|
|
outFile:write(table.concat(outContents, "\n"))
|
|
|
|
outFile:close()
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
return waiter
|
|
|
|
end):next(waitLoop)
|
|
|
|
end
|
2021-04-28 18:54:39 -04:00
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
local waiter = nil
|
|
|
|
if util.isWin then
|
|
|
|
waiter = Promise.new()
|
|
|
|
waiter:resolve()
|
|
|
|
else
|
|
|
|
waiter = util.waitForFiles(inputFilePath)
|
2021-04-28 18:54:39 -04:00
|
|
|
end
|
|
|
|
|
2021-05-01 19:39:35 -04:00
|
|
|
local sec, usec = utime()
|
|
|
|
local ts = sec * 1000000 + usec
|
|
|
|
|
|
|
|
local outFile = io.open(outputFilePath, "w")
|
|
|
|
outFile:write(serpent.dump({ type = 'onInit', ts = ts }))
|
|
|
|
outFile:close()
|
|
|
|
|
|
|
|
print(string.format('Wrote init to output at %d', ts))
|
|
|
|
|
|
|
|
waiter:next(waitLoop):catch(function(error)
|
|
|
|
print('ERROR: '..error)
|
2021-05-01 20:50:13 -04:00
|
|
|
end)
|