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
|
2024-07-19 02:36:47 +00:00
|
|
|
Length: %d]]):format( self.x, self.y, self.X, self.Y, #self / 4 )
|
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 = {}
|
2024-04-26 16:42:52 +00:00
|
|
|
local n = 1
|
2023-04-28 17:35:52 +00:00
|
|
|
local k = 1
|
|
|
|
for line in assert( lfs.lines( filename ) ) do
|
2024-04-21 16:07:56 +00:00
|
|
|
if line:find "b" then
|
2023-04-28 17:35:52 +00:00
|
|
|
k = 1
|
2024-04-21 16:07:56 +00:00
|
|
|
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+)" )
|
2024-04-21 16:07:56 +00:00
|
|
|
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
|
2024-04-21 16:07:56 +00:00
|
|
|
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
|
|
|
|
2024-04-21 16:07:56 +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
|
2024-07-19 02:36:47 +00:00
|
|
|
for k = 1, #poly - 3, 4 do
|
|
|
|
--find the midpoint of each line segment
|
|
|
|
local x, y = 0.5 * (poly[k] + poly[k + 2]), 0.5 * (poly[k + 1] + poly[k + 3])
|
2024-04-28 01:38:23 +00:00
|
|
|
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 )
|
2024-07-18 20:00:51 +00:00
|
|
|
local str = { "b" } --initial B
|
2024-04-28 01:38:23 +00:00
|
|
|
for i, poly in ipairs( lines ) do
|
2024-05-25 02:43:39 +00:00
|
|
|
str[i + 1] = table.concat( poly, " " )
|
2024-04-28 01:38:23 +00:00
|
|
|
end
|
2024-07-18 20:00:51 +00:00
|
|
|
--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
|