dcearth/map/cities.lua

174 lines
4.0 KiB
Lua
Raw Normal View History

2023-04-28 17:35:52 +00:00
--Load and save the fixed width plaintext data used by DEFCON.
2023-07-28 23:23:08 +00:00
local t = {}
2023-04-28 17:35:52 +00:00
local io = io
local math = math
2023-04-28 17:35:52 +00:00
local table = table
local tonumber = tonumber
local lfs = love.filesystem
local lg = love.graphics
local locationQuery = require 'map.locationQuery'
2023-04-28 17:35:52 +00:00
local cities
local points = {}
local caps = {}
2023-09-04 00:49:02 +00:00
t.selected = nil
t.selectionLocked = false
local invisible = 10000 --sentinel value outside the draw rectangle
2023-09-04 00:49:02 +00:00
function t.lockSelection()
t.selectionLocked = true
end
function t.unlockSelection()
t.selectionLocked = false
end
2023-04-28 17:35:52 +00:00
function t.draw()
return lg.points( points )
2023-04-28 17:35:52 +00:00
end
function t.drawSelected( )
2023-09-04 00:49:02 +00:00
local c = t.selected
if not c then return end
lg.circle( "line", c.x, c.y, 1.0 )
end
2023-04-28 17:35:52 +00:00
function t.drawCapitals()
2023-09-04 00:49:02 +00:00
if cities.visible then lg.points( caps ) end
2023-04-28 17:35:52 +00:00
end
function t.selectNearest( cities, x, y )
return cities:getClosestPoint(x, y) --defer to locationQuery
end
local city = {}
local citymt = {__index = city}
function city:formatDisplayInfo()
return (
[[
2024-04-29 01:21:32 +00:00
NAME: %s
COUNTRY: %s
LONGITUDE: %3.2f
LATITUDE: %3.2f
POP: %d
CAPITAL: %s]]):format( self.name, self.country, self.x, self.y, self.pop, tostring(self.capital) )
end
2024-07-20 03:09:53 +00:00
function city:delete()
print( "deleting city:", self.name )
self.deleted = true
if self.capital then
caps[ self.caps ] = invisible
caps[ self.caps + 1] = invisible
end
points[ self.points ] = invisible
points[ self.points + 1] = invisible
2024-07-20 03:09:53 +00:00
end
function city:add()
local n = #cities + 1
cities[ n ] = self
self.n = n
2024-07-20 03:09:53 +00:00
local idxPoints = #points + 1
self.points = idxPoints
points[ idxPoints ], points[ idxPoints + 1 ] = self.x, self.y
end
function city:moveTo(x, y)
self.x, self.y = x, y
if self.points then
points[ self.points ] = x
points[ self.points + 1 ] = y
end
if self.capital then
caps[ self.caps ] = x
caps[ self.caps + 1 ] = y
end
end
function city:toggleCapital()
if self.capital then
self.capital = false
caps[ self.caps ] = invisible
caps[ self.caps + 1 ] = invisible
else
self.capital = true
local idx = #caps + 1
caps[ idx ] = self.x
caps[ idx + 1 ] = self.y
self.caps = idx
end
2024-07-20 03:09:53 +00:00
end
function t.newCity( tbl )
return setmetatable({
name = "",
country = "",
x = 0,
y = 0,
pop = 0,
capital = false,
2024-07-20 03:09:53 +00:00
}, citymt )
end
2023-04-28 17:35:52 +00:00
function t.load( filename )
print( "=== LOADING CITIES. ===" )
2023-04-28 17:35:52 +00:00
2024-04-29 01:21:32 +00:00
cities = { visible = true, active = false, filename = filename }
local n = 1
2023-04-28 17:35:52 +00:00
local idxPts = 1
local idxCaps = 1
points = {}
caps = {}
2023-04-28 17:35:52 +00:00
for line in assert( lfs.lines( filename ), "Error: could not open cities.dat" ) do
2023-04-28 17:35:52 +00:00
local _, _, x, y, pop, capital = line:sub( 83 ):find( "(%g+)%s+(%g+)%s+(%g+)%s+(%g+)" )
if capital then --check against empty or malformed line
x, y, pop, capital = tonumber( x ), tonumber( y ), tonumber( pop ), ( tonumber( capital ) > 0)
local city = setmetatable({
name = line:sub( 1, 39 ):gsub("%s+$",""),
country = line:sub( 42, 82 ):gsub("%s+$",""),
x = x, y = y, pop = pop, capital = capital,
n = n, points = idxPts, caps = capital and idxCaps
}, citymt )
cities[n] = city
n = n + 1
points[idxPts], points[idxPts + 1] = x, y
idxPts = idxPts + 2
if capital then
caps[idxCaps], caps[idxCaps + 1] = x, y
idxCaps = idxCaps + 2
end
else
print( "CITIES: malformed line:", line )
2023-04-28 17:35:52 +00:00
end
end
--Multiple inheritance.
cities = locationQuery.New( cities )
setmetatable( getmetatable( cities ).__index, {__index = t } )
print( "=== CITIES LOADED:", filename, n, "===" )
return cities
2023-04-28 17:35:52 +00:00
end
2024-04-29 01:21:32 +00:00
function t.save( cities )
2023-04-28 17:35:52 +00:00
local str = {}
local i = 1
for _, city in ipairs( cities ) do
if not city.deleted then
str[i] = ("%-41s%-41s%-14f%-14f%-19d %d"):format(
city.name, city.country, city.x, city.y, city.pop, city.capital and 1 or 0 )
i = i + 1
end
2023-04-28 17:35:52 +00:00
end
return assert(table.concat( str, "\n" ))
2023-04-28 17:35:52 +00:00
end
return t