From 836fc09ce823caa132ad88366fbe21e9d0c90a03 Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Sat, 1 May 2021 20:50:13 -0400 Subject: [PATCH] Fix status line --- neat-donk.lua | 22 ++++++++++++++++++++-- runner-process.lua | 8 +++----- runner.lua | 5 +++++ util.lua | 29 +++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/neat-donk.lua b/neat-donk.lua index cddc956..e19debe 100644 --- a/neat-donk.lua +++ b/neat-donk.lua @@ -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) \ No newline at end of file +end) diff --git a/runner-process.lua b/runner-process.lua index 420e360..474d6d0 100644 --- a/runner-process.lua +++ b/runner-process.lua @@ -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) \ No newline at end of file +end) diff --git a/runner.lua b/runner.lua index 3cca936..d6e97c5 100644 --- a/runner.lua +++ b/runner.lua @@ -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 diff --git a/util.lua b/util.lua index 8afab99..2ed9661 100644 --- a/util.lua +++ b/util.lua @@ -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 \ No newline at end of file +end +