2023-04-28 17:35:52 +00:00
|
|
|
--Manage the pathfinding nodes used by DEFCON.
|
|
|
|
--This is important for a mapping tool because the DEFCON client will not load a map unless
|
|
|
|
--the pathfinding nodes form a connected graph.
|
2023-07-28 05:17:00 +00:00
|
|
|
local t = {}
|
|
|
|
local bmp = require 'bmp'
|
2023-07-28 23:23:08 +00:00
|
|
|
local lg = assert( love.graphics )
|
2023-04-28 17:35:52 +00:00
|
|
|
|
2023-08-28 23:12:43 +00:00
|
|
|
local isSailable
|
|
|
|
|
|
|
|
local function isConnected( startNode, endNode )
|
|
|
|
local ix, iy, fx, fy = startNode.x, startNode.y, endNode.x, endNode.y
|
|
|
|
if fx < -180 then fx = fx + 180 end
|
|
|
|
if fx > 180 then fx = fx - 180 end
|
|
|
|
|
|
|
|
local dx, dy = fx - ix, fy - iy
|
|
|
|
local mag = math.sqrt( dx * dx + dy * dy )
|
|
|
|
local n = 2 * math.floor( mag )
|
|
|
|
dx, dy = 0.5 * dx / mag, 0.5 * dy / mag
|
|
|
|
|
|
|
|
for i = 1, n do
|
|
|
|
ix, iy = ix + dx, iy + dy
|
|
|
|
if not( isSailable( ix, iy ) ) then return nil end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-09-04 00:49:02 +00:00
|
|
|
function t.getClosest( nodes, x, y )
|
|
|
|
local d = math.huge
|
|
|
|
local closestNode
|
|
|
|
for _, node in pairs( nodes.nodes ) do
|
|
|
|
local nx, ny = node.x, node.y
|
|
|
|
local nd = (nx - x) * (nx - x) + (ny - y) * (ny - y)
|
|
|
|
if nd < d then d = nd; closestNode = node end
|
|
|
|
end
|
|
|
|
return closestNode
|
|
|
|
end
|
|
|
|
|
2023-08-28 23:12:43 +00:00
|
|
|
function t.load( filename, sailable )
|
2023-07-28 05:17:00 +00:00
|
|
|
|
2023-08-28 23:12:43 +00:00
|
|
|
isSailable = sailable
|
2023-08-26 21:05:48 +00:00
|
|
|
local img, imgd = bmp.load( filename )
|
2023-08-28 23:12:43 +00:00
|
|
|
local nodes = { visible = true, nodes = {}, points = {}, connections = {}, img = img }
|
2023-08-26 21:05:48 +00:00
|
|
|
|
|
|
|
print( "=== Loading Nodes: ===" )
|
2023-08-28 23:12:43 +00:00
|
|
|
local n = 1
|
2023-08-26 21:05:48 +00:00
|
|
|
for x = 0, 799 do
|
|
|
|
for y = 0, 399 do
|
|
|
|
if imgd:getPixel( x, 399 - y ) > 0 then
|
|
|
|
local long = 360 * ( x - 800 ) / 800 - 360 / 2 + 360
|
|
|
|
local lat = 360 * ( 600 / 800 ) * ( 600 - y ) / 600 - 180
|
2023-08-28 23:12:43 +00:00
|
|
|
nodes.nodes[n] = {x = long, y = lat}
|
|
|
|
nodes.points[ 2 * n - 1 ] = long
|
|
|
|
nodes.points[ 2 * n ] = lat
|
|
|
|
print( n, long, lat )
|
|
|
|
n = n + 1
|
2023-08-26 21:05:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-28 23:12:43 +00:00
|
|
|
for i, srcNode in ipairs( nodes.nodes ) do
|
|
|
|
local adjacent = {}
|
|
|
|
|
|
|
|
for j, destNode in ipairs( nodes.nodes ) do
|
|
|
|
adjacent[j] = isConnected( srcNode, destNode )
|
|
|
|
end
|
|
|
|
|
|
|
|
nodes.connections[i] = adjacent
|
|
|
|
end
|
|
|
|
|
2023-08-26 21:05:48 +00:00
|
|
|
print( "=== Nodes Loaded ===" )
|
2023-07-28 05:17:00 +00:00
|
|
|
return setmetatable( nodes, {__index = t } )
|
|
|
|
end
|
2023-04-28 17:35:52 +00:00
|
|
|
|
2024-04-21 14:10:53 +00:00
|
|
|
--Determine if graph has more than one connected component.
|
2023-07-28 05:17:00 +00:00
|
|
|
function t.isConnected( nodes )
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.draw( nodes )
|
2023-08-26 21:05:48 +00:00
|
|
|
lg.setPointSize( 10 )
|
|
|
|
lg.setColor( 1, 1, 1, 0.5 )
|
2023-07-28 23:23:08 +00:00
|
|
|
lg.points( nodes.points )
|
2023-08-28 23:12:43 +00:00
|
|
|
|
|
|
|
for i, connection in pairs( nodes.connections ) do
|
|
|
|
for j in pairs( connection ) do
|
|
|
|
local ix, iy, fx, fy = nodes.nodes[i].x, nodes.nodes[i].y, nodes.nodes[j].x, nodes.nodes[j].y
|
|
|
|
lg.line( ix, iy, fx, fy )
|
|
|
|
end
|
|
|
|
end
|
2023-07-28 05:17:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.drawConnections( nodes )
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function t.save( nodes, filename )
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|