dcearth/map/ai.lua

97 lines
2.1 KiB
Lua

--Manage the AI nodes used by DEFCON.
local bmp = require 'map.bmp'
local lg = assert( love.graphics )
local locationQuery = require 'map.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 aiNode:add()
end
function aiNode:moveTo( x, y )
end
function t.load( filename )
local img, imgd = bmp.load( filename )
local nodes = {
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
function t.save( nodes )
return bmp.ai( nodes.all )
end
function t.newNode( isAttacking )
end
return t