2022-09-11 04:23:52 +00:00
|
|
|
-- based on
|
2022-09-11 04:56:27 +00:00
|
|
|
-- https://healeycodes.com/virtual-ants
|
2022-09-11 04:23:52 +00:00
|
|
|
-- https://github.com/healeYcodes/virtual-ants/blob/main/index.html
|
|
|
|
|
|
|
|
require("util")
|
|
|
|
|
|
|
|
local function round2even(num)
|
|
|
|
return 2 * math.floor(num / 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function rotate(direction, clockwise)
|
|
|
|
local match
|
|
|
|
if clockwise then
|
|
|
|
match = {
|
|
|
|
u = "r",
|
|
|
|
r = "d",
|
|
|
|
d = "l",
|
|
|
|
l = "u",
|
|
|
|
}
|
|
|
|
else
|
|
|
|
match = {
|
|
|
|
u = "l",
|
|
|
|
l = "d",
|
|
|
|
d = "r",
|
|
|
|
r = "u"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return match[direction]
|
|
|
|
end
|
|
|
|
|
|
|
|
local function initColors(rules)
|
|
|
|
rules = rules:lower()
|
|
|
|
local colorbrewer = require("colorbrewer")
|
2022-09-11 13:18:20 +00:00
|
|
|
-- set a specific random seed to avoid picking the same palette every time
|
|
|
|
math.randomseed(os.time())
|
2022-09-11 16:08:09 +00:00
|
|
|
local scheme = colorbrewer.random(#rules)
|
2022-09-11 04:23:52 +00:00
|
|
|
COLORS = {}
|
2022-09-11 16:08:09 +00:00
|
|
|
for i, color in pairs(scheme) do
|
2022-09-11 04:23:52 +00:00
|
|
|
COLORS[i] = {
|
|
|
|
clockwise = rules:sub(i, i) == "r",
|
2022-09-11 16:08:09 +00:00
|
|
|
rgba = color
|
2022-09-11 04:23:52 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-11 04:38:08 +00:00
|
|
|
local function initScreen()
|
|
|
|
local width, height = love.graphics.getDimensions()
|
2022-09-11 04:23:52 +00:00
|
|
|
WIDTH, HEIGHT = round2even(width), round2even(height)
|
|
|
|
CANVAS = love.graphics.newCanvas(WIDTH, HEIGHT)
|
|
|
|
CANVAS:renderTo(function()
|
|
|
|
local color = COLORS[1].rgba
|
|
|
|
color[4] = 0.9
|
|
|
|
love.graphics.setColor(color)
|
|
|
|
love.graphics.rectangle("fill", 0, 0, WIDTH, HEIGHT)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function initFont()
|
2022-09-11 18:39:25 +00:00
|
|
|
FONT = love.graphics.newFont(16)
|
2022-09-11 16:08:09 +00:00
|
|
|
love.graphics.setFont(FONT)
|
|
|
|
FPS_WIDTH = FONT:getWidth("120 FPS")
|
|
|
|
FPS_HEIGHT = FONT:getHeight()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function spawnAnt(x, y)
|
|
|
|
table.insert(ANTS, {
|
|
|
|
x = x,
|
|
|
|
y = y,
|
|
|
|
direction = "u"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local function spawnAntCentered()
|
|
|
|
spawnAnt(
|
|
|
|
(WIDTH / 2) - ((WIDTH / 2) % UNITS),
|
|
|
|
(HEIGHT / 2) - ((HEIGHT / 2) % UNITS)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function initAnts()
|
|
|
|
ANTS = {}
|
|
|
|
spawnAntCentered()
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function initGrid()
|
|
|
|
GRID = {}
|
|
|
|
GRID.mt = {}
|
|
|
|
setmetatable(GRID, GRID.mt)
|
|
|
|
GRID.mt.__index = function(table, key)
|
|
|
|
table[key] = {}
|
|
|
|
table[key].mt = {}
|
|
|
|
setmetatable(table[key], table[key].mt)
|
|
|
|
table[key].mt.__index = function(innerTable, innerKey)
|
|
|
|
innerTable[innerKey] = 1
|
|
|
|
return innerTable[innerKey]
|
|
|
|
end
|
|
|
|
return table[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function love.load(args)
|
2022-09-11 04:38:08 +00:00
|
|
|
local rules = args[1] or "RL"
|
2022-09-11 16:08:09 +00:00
|
|
|
UNITS = args[2] or 4
|
2022-09-11 13:18:20 +00:00
|
|
|
STEPS_PER_FRAME = args[3] or 5
|
2022-09-11 04:23:52 +00:00
|
|
|
initColors(rules)
|
2022-09-11 04:38:08 +00:00
|
|
|
initScreen()
|
2022-09-11 16:08:09 +00:00
|
|
|
initFont()
|
|
|
|
initAnts()
|
2022-09-11 04:23:52 +00:00
|
|
|
initGrid()
|
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function antColor(ant)
|
|
|
|
return GRID[ant.x][ant.y]
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function setColor(ant, color)
|
|
|
|
GRID[ant.x][ant.y] = color
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function updateColor(ant)
|
|
|
|
local val = antColor(ant)
|
2022-09-11 04:23:52 +00:00
|
|
|
if val == #COLORS then
|
|
|
|
val = 1
|
|
|
|
else
|
|
|
|
val = val + 1
|
|
|
|
end
|
2022-09-11 16:08:09 +00:00
|
|
|
setColor(ant, val)
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function nextColor(ant)
|
|
|
|
local c = COLORS[antColor(ant)]
|
|
|
|
updateColor(ant)
|
2022-09-11 04:23:52 +00:00
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
function love.mousepressed(x, y, _, _)
|
|
|
|
spawnAnt(x, y)
|
|
|
|
end
|
|
|
|
|
2022-09-11 04:23:52 +00:00
|
|
|
function love.update()
|
|
|
|
require("lurker").update()
|
2022-09-11 13:18:20 +00:00
|
|
|
for _ = 1, STEPS_PER_FRAME do
|
2022-09-11 16:08:09 +00:00
|
|
|
for i, ant in pairs(ANTS) do
|
|
|
|
local c = nextColor(ant)
|
|
|
|
ant.direction = rotate(ant.direction, c.clockwise)
|
|
|
|
CANVAS:renderTo(function()
|
|
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
love.graphics.setColor(c.rgba)
|
|
|
|
love.graphics.rectangle("fill", ant.x, ant.y, UNITS, UNITS)
|
|
|
|
end)
|
|
|
|
if ant.direction == "u" then
|
|
|
|
ant.y = ant.y - UNITS
|
|
|
|
elseif ant.direction == "r" then
|
|
|
|
ant.x = ant.x + UNITS
|
|
|
|
elseif ant.direction == "d" then
|
|
|
|
ant.y = ant.y + UNITS
|
|
|
|
else
|
|
|
|
ant.x = ant.x - UNITS
|
|
|
|
end
|
|
|
|
|
|
|
|
if ant.x >= WIDTH then
|
|
|
|
ant.x = 0
|
|
|
|
elseif ant.x < 0 then
|
|
|
|
ant.x = (WIDTH - 1) - ((WIDTH - 1) % UNITS)
|
|
|
|
end
|
|
|
|
if ant.y >= HEIGHT then
|
|
|
|
ant.y = 0
|
|
|
|
elseif ant.y < 0 then
|
|
|
|
ant.y = (HEIGHT - 1) - ((HEIGHT - 1) % UNITS)
|
|
|
|
end
|
|
|
|
ANTS[i] = ant
|
2022-09-11 13:18:20 +00:00
|
|
|
end
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-11 16:08:09 +00:00
|
|
|
local function paintFPS()
|
|
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
local fps = tostring(love.timer.getFPS()) .. " FPS"
|
|
|
|
love.graphics.setColor(COLORS[1].rgba)
|
2022-09-11 18:39:25 +00:00
|
|
|
love.graphics.rectangle("fill", WIDTH - FPS_WIDTH - 10, HEIGHT - FPS_HEIGHT - 20, FPS_WIDTH + 30, FPS_HEIGHT + 10, 5, 5)
|
2022-09-11 16:08:09 +00:00
|
|
|
love.graphics.setColor(COLORS[#COLORS].rgba)
|
|
|
|
love.graphics.print(fps, WIDTH - FPS_WIDTH - 5, HEIGHT - FPS_HEIGHT - 15)
|
|
|
|
end
|
|
|
|
|
2022-09-11 04:23:52 +00:00
|
|
|
function love.draw()
|
2022-09-11 04:56:27 +00:00
|
|
|
love.graphics.setBlendMode("alpha", "premultiplied")
|
2022-09-11 04:23:52 +00:00
|
|
|
love.graphics.setColor(COLORS[1].rgba)
|
|
|
|
love.graphics.draw(CANVAS, 0, 0)
|
2022-09-11 16:08:09 +00:00
|
|
|
paintFPS()
|
2022-09-11 04:23:52 +00:00
|
|
|
end
|