neat-donk/runner-process.lua

173 lines
4 KiB
Lua
Raw Normal View History

local gui, utime = gui, utime
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-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-04-23 18:09:35 -04:00
local util = dofile(base.."/util.lua")
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
2021-04-28 18:54:39 -04:00
local first = false
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
local speciesId = -1
2021-04-28 18:54:39 -04:00
local generationIndex = nil
2021-04-09 15:06:55 -04:00
local runner = Runner()
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',
speciesId = speciesId,
2021-04-09 15:06:55 -04:00
msg = msg,
color = color,
})
)
end)
local guiHeight = 0
local guiWidth = 0
runner.onRenderForm(function(form)
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-04-24 06:00:28 -04:00
gui.rectangle(0, guiHeight - 20, 0, 20, 1, 0x00000000, statusColor)
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,
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,
speciesId = speciesId,
2021-04-09 15:06:55 -04:00
})
)
end)
2021-04-29 08:51:03 -04:00
local waiter = nil
if not util.isWin then
2021-04-29 08:51:03 -04:00
waiter = util.startWaiting(inputFilePath)
end
2021-04-28 18:54:39 -04:00
local function waitLoop()
if not first then
local sec, usec = utime()
local ts = sec * 1000000 + usec
local outFile = io.open(outputFilePath, "w")
outFile:write(serpent.dump({ type = 'onInit', ts = ts }))
2021-04-09 15:06:55 -04:00
outFile:close()
2021-04-28 18:54:39 -04:00
print(string.format('Wrote init to output at %d', ts))
2021-04-28 18:54:39 -04:00
first = true
end
print('Waiting for input from master process')
if not util.isWin then
2021-04-29 08:51:03 -04:00
util.finishWaiting(waiter)
end
2021-04-28 18:54:39 -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
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
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')
runner.run(
species,
generationIndex,
function(genome, index)
table.insert(
outContents,
serpent.dump({
type = 'onGenome',
genome = genome,
genomeIndex = index,
speciesId = speciesId,
})
)
end,
function()
table.insert(
outContents,
serpent.dump({
type = 'onFinish',
speciesId = speciesId,
})
)
2021-04-28 23:29:43 -04:00
-- 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()
if not util.isWin then
2021-04-29 08:51:03 -04:00
waiter = util.startWaiting(inputFilePath)
end
2021-04-28 18:54:39 -04:00
2021-04-28 23:29:43 -04:00
-- Write the result
2021-04-28 18:54:39 -04:00
local outFile = io.open(outputFilePath, "w")
outFile:write(table.concat(outContents, "\n"))
outFile:close()
waitLoop()
end
)
end
waitLoop()