dcearth/map/lines.lua

124 lines
2.9 KiB
Lua
Raw Normal View History

2023-04-28 17:35:52 +00:00
--Load and save plaintext line mesh format for DEFCON coastlines etc.
local t = {}
local lfs = love.filesystem
local lg = love.graphics
2024-04-28 01:38:23 +00:00
local polygon = { x = 180, X = -180, y = 100, Y = -100 } --default empty bounding box
local polymt = { __index = polygon }
function polygon:formatDisplayInfo()
return ([[
2024-04-29 01:21:32 +00:00
x: %f
y: %f
X: %f
Y: %f
N: %d]]):format( self.x, self.y, self.X, self.Y, #self )
2024-04-28 01:38:23 +00:00
end
2024-05-02 00:32:07 +00:00
function polygon:drawDirection()
local a,b,c,d = self[1], self[2], self[3], self[4]
local bx, by = (c + a) / 2, (b + d) / 2
local dx, dy = c - a, d - b
local r = math.max( math.sqrt( dy * dy + dx * dx ), 0.0001 )
dx, dy = dx / r, dy / r
lg.polygon( "fill",
a + dx, b + dy,
a - 0.4 * dy, b + 0.4 * dx,
a + 0.4 * dy, b - 0.4 * dx )
end
2023-04-28 17:35:52 +00:00
function t.load( filename )
2024-04-29 01:21:32 +00:00
local polys = { visible = true, filename = filename }
2023-04-28 17:35:52 +00:00
local poly = {}
local n = 1
2023-04-28 17:35:52 +00:00
local k = 1
for line in assert( lfs.lines( filename ) ) do
if line:find "b" then
2023-04-28 17:35:52 +00:00
k = 1
if #poly > 2 then n = n + 1 end
2024-04-28 01:38:23 +00:00
poly = setmetatable({}, polymt) --axis-aligned bounding box
2023-04-28 17:35:52 +00:00
polys[n] = poly
else
local _, _, x, y = line:find( "(%g+)%s+(%g+)" )
x, y = tonumber( x ), tonumber( y )
if x and y then
poly[k], poly[ k + 1 ] = x, y
k = k + 2
2024-04-28 01:38:23 +00:00
if x < poly.x then poly.x = x end
if x > poly.X then poly.X = x end
if y < poly.y then poly.y = y end
if y > poly.Y then poly.Y = y end
else
print( "LINES: malformed line:", filename, line )
end
2023-04-28 17:35:52 +00:00
end
end
2024-04-28 01:38:23 +00:00
if not polys[n] or (#(polys[n]) < 3) then
polys[n] = nil
n = n - 1
end
2023-04-28 17:35:52 +00:00
print( "LOADED", filename, n )
return setmetatable( polys, {__index = t } )
end
2024-04-28 01:38:23 +00:00
function t.selectNearest( lines, wx, wy )
local d = math.huge
local nearest
for i, poly in ipairs( lines ) do
if poly.x - 5 < wx and
poly.X + 5 > wx and
poly.y - 5 < wy and
poly.Y + 5 > wy then
for k = 1, #poly, 2 do
local x, y = poly[k], poly[k + 1]
local r = ( x - wx ) * ( x - wx ) + ( y - wy ) * ( y - wy )
if r < d then
d = r
nearest = poly
end
end
end
end
return nearest
end
2023-04-28 17:35:52 +00:00
2024-04-29 01:21:32 +00:00
function t.save( lines )
local str = { "b" } --initial B
2024-04-28 01:38:23 +00:00
for i, poly in ipairs( lines ) do
str[i + 1] = table.concat( poly, " " )
2024-04-28 01:38:23 +00:00
end
--concatenate into one big string, one line per polygon
--then put each pair of numbers on their own line (without concatenating)
--we use CRLF line breaks here because that's what's in the original game files
str = table.concat( str, "\13\nb\13\n" ):gsub("(%S+) (%S+) ", "%1 %2\13\n")
2024-04-28 01:38:23 +00:00
return str
2023-07-28 05:42:16 +00:00
end
function t.newPolygon( x, y )
2024-04-28 01:38:23 +00:00
2023-07-28 05:42:16 +00:00
end
function t.addToCurrentPolygon( x, y )
2024-04-28 01:38:23 +00:00
2023-07-28 05:42:16 +00:00
end
2023-07-28 23:23:08 +00:00
function t.deletePolygon( poly )
2024-04-28 01:38:23 +00:00
2023-07-28 23:23:08 +00:00
end
function t.drawPolygonEditHandles( poly )
2024-04-28 01:38:23 +00:00
2023-04-28 17:35:52 +00:00
end
function t.draw( lines )
2023-07-28 05:42:16 +00:00
if not lines.visible then return end
2023-04-28 17:35:52 +00:00
for _, poly in ipairs( lines ) do
lg.line( poly )
end
end
return t