Fix status line

This commit is contained in:
Empathic Qubit 2021-05-01 20:50:13 -04:00
parent 6149f72d98
commit 836fc09ce8
4 changed files with 55 additions and 9 deletions

View file

@ -4,12 +4,30 @@ local gui = gui
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
local pool = dofile(base.."/pool.lua")
local util = dofile(base.."/util.lua")()
local statusLine = nil
local statusColor = 0x0000ff00
pool.onMessage(function(msg, color)
print(msg)
local color = util.nearestColor(color, {
-- Red
['91'] = { r = 255, g = 0 , b = 0 },
-- Green
['92'] = { r = 0 , g = 255, b = 0 },
-- Yellow
['93'] = { r = 255, g = 255, b = 0 },
-- Blue
['94'] = { r = 0 , g = 0 , b = 255},
-- Magenta
['95'] = { r = 255, g = 0 , b = 255},
-- Cyan
['96'] = { r = 0 , g = 255, b = 255},
-- White
['97'] = { r = 255, g = 255, b = 255},
})
io.stderr:write('\x1b['..color..'m'..msg..'\x1b[0m\n')
statusLine = msg
statusColor = color
end)
@ -25,7 +43,7 @@ pool.onRenderForm(function(form)
form:draw(-500, 0)
if statusLine ~= nil then
gui.rectangle(-500, guiHeight - 20, 0, 20, 1, 0x00000000, statusColor)
gui.rectangle(-500, guiHeight - 20, guiWidth, 20, 1, 0x00000000, statusColor)
gui.text(-500, guiHeight - 20, statusLine, 0x00000000)
end
end)
@ -35,4 +53,4 @@ pool.run():next(function()
end):catch(function(error)
io.stderr:write(string.format("There was a problem running the pool: %s", error))
print(string.format("There was a problem running the pool: %s", error))
end)
end)

View file

@ -42,10 +42,8 @@ runner.onMessage(function(msg, color)
)
end)
local guiHeight = 0
local guiWidth = 0
runner.onRenderForm(function(form)
guiWidth, guiHeight = gui.resolution()
local guiWidth, guiHeight = gui.resolution()
gui.left_gap(0)
gui.top_gap(0)
gui.bottom_gap(0)
@ -53,7 +51,7 @@ runner.onRenderForm(function(form)
form:draw(0, 0)
if statusLine ~= nil then
gui.rectangle(0, guiHeight - 20, 0, 20, 1, 0x00000000, statusColor)
gui.rectangle(0, guiHeight - 20, guiWidth, 20, 1, 0x00000000, statusColor)
gui.text(0, guiHeight - 20, statusLine, 0x00000000)
end
@ -171,4 +169,4 @@ print(string.format('Wrote init to output at %d', ts))
waiter:next(waitLoop):catch(function(error)
print('ERROR: '..error)
end)
end)

View file

@ -274,6 +274,7 @@ local function displayForm(_M)
end
if form ~= nil and _M.drawFrame % 10 ~= 0 then
gui.renderctx.setnull()
for i=#_M.onRenderFormHandler,1,-1 do
_M.onRenderFormHandler[i](form)
end
@ -630,6 +631,10 @@ local function mainLoop(_M, genome)
_M.timeout = _M.timeout - 1
if lastFrame ~= frame then
message(_M, string.format("We missed %d frames", frame - lastFrame), 0x00990000)
end
-- Continue if we haven't timed out
local timeoutBonus = _M.currentFrame / 4
if _M.timeout + timeoutBonus > 0 then

View file

@ -1,4 +1,4 @@
local utime = utime
local utime, bit = utime, bit
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
@ -214,7 +214,32 @@ function _M.file_exists(name)
if f~=nil then io.close(f) return true else return false end
end
function _M.nearestColor(needle, colors)
local opacity = bit.band(needle, 0xff000000)
local needle = {
r = bit.lrshift(bit.band(needle, 0x00ff0000), 4),
g = bit.lrshift(bit.band(needle, 0x0000ff00), 2),
b = bit.band(needle, 0x000000ff),
}
local minDistanceSq = 0x7fffffff
local value = nil
for name,color in pairs(colors) do
local distanceSq = (
math.pow(needle.r - color.r, 2) +
math.pow(needle.g - color.g, 2) +
math.pow(needle.b - color.b, 2)
)
if distanceSq < minDistanceSq then
minDistanceSq = distanceSq
value = name
end
end
return value
end
return function(promise)
Promise = promise
return _M
end
end