neat-donk/runner-process.lua

116 lines
2.5 KiB
Lua
Raw Normal View History

2021-04-09 15:06:55 -04:00
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
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")
local runnerDataFile = io.open(os.getenv("RUNNER_DATA"), 'r')
local ok, runnerData = serpent.load(runnerDataFile:read('*a'))
2021-04-23 18:09:35 -04:00
runnerDataFile:close()
2021-04-09 15:06:55 -04:00
if not ok then
print("Deserialization error")
2021-04-09 15:06:55 -04:00
return
end
local species = runnerData[1]
2021-04-09 15:06:55 -04:00
local speciesId = species.id
local generationIndex = runnerData[2]
local filename = runnerData[3]
2021-04-09 15:06:55 -04:00
local outFile = io.open(filename, "w")
2021-04-09 15:06:55 -04:00
local outContents = {}
local statusLine = nil
local statusColor = 0x0000ff00
local runner = Runner()
runner.onMessage(function(msg, color)
statusLine = msg
statusColor = color
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)
runner.run(
species,
generationIndex,
2021-04-09 15:06:55 -04:00
function(genome, index)
table.insert(
outContents,
2021-04-23 15:39:11 -04:00
serpent.dump({
2021-04-09 15:06:55 -04:00
type = 'onGenome',
genome = genome,
genomeIndex = index,
speciesId = speciesId,
2021-04-09 15:06:55 -04:00
})
)
end,
function()
table.insert(
outContents,
2021-04-23 15:39:11 -04:00
serpent.dump({
2021-04-09 15:06:55 -04:00
type = 'onFinish',
speciesId = speciesId,
2021-04-09 15:06:55 -04:00
})
)
outFile:write(table.concat(outContents, "\n"))
outFile:close()
if os.getenv("RUNNER_DONT_QUIT") == nil then
exec('quit-emulator')
end
2021-04-09 15:06:55 -04:00
end
)