dcearth/ai.lua

85 lines
2.0 KiB
Lua
Raw Permalink Normal View History

--Manage the AI nodes used by DEFCON.
local bmp = require 'bmp'
local lg = assert( love.graphics )
local locationQuery = require 'locationQuery'
local t = setmetatable( {}, {__index = locationQuery } )
local print = print
local aiNode = {}
local mtAiNode = { __index = aiNode }
function aiNode:formatDisplayInfo()
return ([[AI NODE: %d
LONGITUDE: %3.2f
LATITUDE: %3.2f
ATTACKING: %s
]]):format( self.idx, self.x, self.y, tostring(self.attacking) )
end
function t.load( filename )
local img, imgd = bmp.load( filename )
local nodes = {
2024-04-29 01:21:32 +00:00
filename = filename,
visible = true,
all = {},
att = {},
ptsAtt = {},
def = {},
ptsDef = {},
img = img,
imgd = imgd }
print( "=== Loading AI Markers: ===" )
local idx = 1
for x = 0, 511 do
for y = 0, 284 do
local r, g = imgd:getPixel( x, 284 - y )
if r > 0.5 or g > 0.5 then
local long = x * (360 / imgd:getWidth()) - 180
local lat = y * (200 / img:getHeight()) - 100
local attacking = (r > 0.5)
local set = attacking and nodes.att or nodes.def
local node = setmetatable( {x = long, y = lat, attacking = attacking, idx = idx}, mtAiNode )
nodes.all[ idx ] = node
set[#set + 1] = node
idx = idx + 1
end
end
end
do
local k = 1
for i, point in ipairs( nodes.att ) do
nodes.ptsAtt[k], nodes.ptsAtt[k + 1] = point.x, point.y
k = k + 2
end
k = 1
for j, point in ipairs( nodes.def ) do
nodes.ptsDef[k], nodes.ptsDef[k + 1] = point.x, point.y
k = k + 2
end
end
nodes.all = locationQuery.New( nodes.all )
setmetatable( nodes, {__index = t } )
return nodes
end
function t.selectNearest( nodes, x, y )
return (nodes.all):getClosestPoint( x, y )
end
function t.draw( nodes )
lg.setColor( 1, 0, 0, 0.5 )
lg.points( nodes.ptsAtt )
lg.setColor( 0, 1, 0, 0.5 )
lg.points( nodes.ptsDef )
end
2024-04-29 01:21:32 +00:00
function t.save( nodes )
return bmp.ai( nodes.all )
end
return t