add drag and drop, parse lines more robustly

This commit is contained in:
wan-may 2024-04-21 13:07:56 -03:00
parent 688e954c2b
commit ce15bfdcf7
21 changed files with 80 additions and 33234 deletions

View File

@ -51,6 +51,7 @@ function t.load( filename )
for line in assert( lfs.lines( filename ), "Error: could not open cities.dat" ) do for line in assert( lfs.lines( filename ), "Error: could not open cities.dat" ) do
local _, _, x, y, pop, capital = line:sub( 83 ):find( "(%g+)%s+(%g+)%s+(%g+)%s+(%g+)" ) 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) x, y, pop, capital = tonumber( x ), tonumber( y ), tonumber( pop ), ( tonumber( capital ) > 0)
local city = { local city = {
name = line:sub( 1, 39 ):gsub("%s+$",""), name = line:sub( 1, 39 ):gsub("%s+$",""),
@ -67,6 +68,9 @@ function t.load( filename )
caps[idxCaps], caps[idxCaps + 1] = x, y caps[idxCaps], caps[idxCaps + 1] = x, y
idxCaps = idxCaps + 2 idxCaps = idxCaps + 2
end end
else
print( "CITIES: malformed line:", line )
end
end end
--Multiple inheritance. --Multiple inheritance.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 426 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 428 KiB

View File

@ -9,18 +9,27 @@ function t.load( filename )
local n = 0 local n = 0
local k = 1 local k = 1
for line in assert( lfs.lines( filename ) ) do for line in assert( lfs.lines( filename ) ) do
if line == "b" then if line:find "b" then
k = 1 k = 1
n = n + 1 if #poly > 2 then n = n + 1 end
poly = {} poly = {}
polys[n] = poly polys[n] = poly
else else
local _, _, x, y = line:find( "(%g+)%s+(%g+)" ) local _, _, x, y = line:find( "(%g+)%s+(%g+)" )
x, y = assert( tonumber( x ) ), assert( tonumber( y ) ) x, y = tonumber( x ), tonumber( y )
if x and y then
poly[k], poly[ k + 1 ] = x, y poly[k], poly[ k + 1 ] = x, y
k = k + 2 k = k + 2
else
print( "LINES: malformed line:", filename, line )
end end
end end
end
if not polys[n] or (#(polys[n]) < 3) then
polys[n] = nil
n = n - 1
end
print( "LOADED", filename, n ) print( "LOADED", filename, n )
return setmetatable( polys, {__index = t } ) return setmetatable( polys, {__index = t } )

View File

@ -1,4 +1,5 @@
local love = assert( love, "This tool requires LOVE: love2d.org" ) local love = assert( love, "This tool requires LOVE: love2d.org" )
--assert( require('mobdebug') ).start() --remote debugger
local map = require 'map' local map = require 'map'
local button = require 'button' local button = require 'button'
local SAVEDIRECTORY = "out/" local SAVEDIRECTORY = "out/"
@ -11,12 +12,16 @@ function love.load()
lfs.setIdentity( "dcearth", false ) lfs.setIdentity( "dcearth", false )
assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" )) assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" ))
assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" )) assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" ))
map.load()
love.graphics.setNewFont( 12, "mono" ) love.graphics.setNewFont( 12, "mono" )
end end
function love.directorydropped( path )
love.filesystem.mount( path, "" )
return map.load( path )
end
function love.update( dt ) function love.update( dt )
local tx, ty = 0, 0 local tx, ty = 0, 0
local moveCamera = false local moveCamera = false
@ -57,6 +62,10 @@ local layerButtons = {
function love.draw() function love.draw()
if not map.loaded then
return love.graphics.print( "Drag and drop folder to begin.")
end
love.graphics.push( "all" ) love.graphics.push( "all" )
map.draw() map.draw()
love.graphics.pop() love.graphics.pop()
@ -85,7 +94,7 @@ function love.draw()
--Edit box. --Edit box.
love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 ) love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 )
if map.cities.selected then if map.loaded and map.cities.selected then
local c = map.cities.selected local c = map.cities.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 ) love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 ) love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
@ -93,7 +102,7 @@ function love.draw()
love.graphics.rectangle( "line", 0, 0, 150 ,100 ) love.graphics.rectangle( "line", 0, 0, 150 ,100 )
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 ) love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
love.graphics.print( ("NAME: %s\nX: %3.2f\nY: %3.2f\nPOP: %d\nCAPITAL: %s\nCOUNTRY: %s"):format(c.name, c.x, c.y, c.pop, tostring(c.capital), c.country), 0, 0 ) love.graphics.print( ("NAME: %s\nX: %3.2f\nY: %3.2f\nPOP: %d\nCAPITAL: %s\nCOUNTRY: %s"):format(c.name, c.x, c.y, c.pop, tostring(c.capital), c.country), 0, 0 )
elseif map.travelnodes.selected then elseif map.loaded and map.travelnodes.selected then
local c = map.travelnodes.selected local c = map.travelnodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 ) love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 ) love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
@ -101,7 +110,7 @@ function love.draw()
love.graphics.rectangle( "line", 0, 0, 150 ,100 ) love.graphics.rectangle( "line", 0, 0, 150 ,100 )
love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 ) love.graphics.setColor( 1.2, 1.1, 1.1, 1.5 )
love.graphics.print( ("Node: %d\nX: %3.2f\nY: %3.2f\n"):format(c.number, c.x, c.y) ) love.graphics.print( ("Node: %d\nX: %3.2f\nY: %3.2f\n"):format(c.number, c.x, c.y) )
elseif map.ainodes.selectedNode then elseif map.loaded and map.ainodes.selectedNode then
local c = map.ainodes.selected local c = map.ainodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 ) love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 ) love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
@ -122,16 +131,18 @@ end
function love.mousepressed( x, y, button, istouch, presses ) function love.mousepressed( x, y, button, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y ) local wx, wy = Camera.GetWorldCoordinate( x, y )
if map.loaded then
if button == 1 then if button == 1 then
map.cities.lockSelection() map.cities.lockSelection()
else else
map.cities.unlockSelection() map.cities.unlockSelection()
end end
end
print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) ) print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) )
end end
function love.mousemoved( x, y, dx, dy, istouch ) function love.mousemoved( x, y, dx, dy, istouch )
map.cities.selectNearestCity( Camera.GetWorldCoordinate( x, y ) ) if map.loaded and map.cities.visible then map.cities.selectNearestCity( Camera.GetWorldCoordinate( x, y ) ) end
end end
local function ToggleVisibility( layer ) local function ToggleVisibility( layer )
@ -155,7 +166,9 @@ local layerVisibilityKeybinds = {
["8"] = "coastlines", ["8"] = "coastlines",
["9"] = "coastlinesLow", ["9"] = "coastlinesLow",
["0"] = "international", ["0"] = "international",
["-"] = "cities" ["-"] = "cities",
["="] = "travelnodes",
["backspace"] = "ainodes",
} }
function love.keypressed(key) function love.keypressed(key)

22
map.lua
View File

@ -8,6 +8,7 @@ local Camera = require 'camera'
local Territory = require 'territory' local Territory = require 'territory'
local map = { local map = {
loaded = false,
coastlines = false, coastlines = false,
coastlinesLow = false, coastlinesLow = false,
international = false, international = false,
@ -21,11 +22,11 @@ local map = {
}, },
travelnodes = false, travelnodes = false,
sailable = false, sailable = false,
aimarkers = false, ainodes = false,
cities = false cities = false
} }
function map.load() function map.load( )
map.cities = Cities.load( "data/earth/cities.dat" ) map.cities = Cities.load( "data/earth/cities.dat" )
map.coastlines = Lines.load( "data/earth/coastlines.dat" ) map.coastlines = Lines.load( "data/earth/coastlines.dat" )
map.coastlinesLow = Lines.load( "data/earth/coastlines-low.dat" ) map.coastlinesLow = Lines.load( "data/earth/coastlines-low.dat" )
@ -36,10 +37,12 @@ function map.load()
for k, v in pairs(map.territory) do for k, v in pairs(map.territory) do
map.territory[k] = Territory.load( "data/earth/"..k..".bmp", k ) map.territory[k] = Territory.load( "data/earth/"..k..".bmp", k )
end end
map.loaded = true
end end
function map.draw() function map.draw()
lg.clear( 0, 0, 0, 1 ) lg.clear( 0, 0, 0, 1 )
if not map.loaded then return end
do --territory do --territory
lg.setColor( 1,1,1,1) lg.setColor( 1,1,1,1)
@ -55,13 +58,9 @@ function map.draw()
v:drawBorder( "sea" ) v:drawBorder( "sea" )
end end
end end
if map.sailable.visible then map.sailable:draw() end if map.sailable.visible then
lg.setBlendMode( "alpha" ) map.sailable:draw()
lg.setColor( 1, 1, 1, 1 )
end
do --borders
lg.setLineJoin( "none" ) lg.setLineJoin( "none" )
lg.setLineWidth( 1 / Camera.zoom ) lg.setLineWidth( 1 / Camera.zoom )
@ -71,7 +70,10 @@ function map.draw()
lg.setLineWidth( 3 / Camera.zoom ) lg.setLineWidth( 3 / Camera.zoom )
map.sailable:drawBorder( "placeable" ) map.sailable:drawBorder( "placeable" )
end end
lg.setBlendMode( "alpha" )
lg.setColor( 1, 1, 1, 1 )
end
do --all this stuff is drawn in world coordinates, ( -180, 180 ) x ( -100, 100 ) do --all this stuff is drawn in world coordinates, ( -180, 180 ) x ( -100, 100 )
@ -90,7 +92,7 @@ function map.draw()
lg.setColor( 1, 1, 1, 0.5 ) lg.setColor( 1, 1, 1, 0.5 )
map.cities.drawSelected( 15.0 / Camera.zoom ) map.cities.drawSelected( 15.0 / Camera.zoom )
map.ainodes:draw() if map.ainodes.visible then map.ainodes:draw() end
end end
do --line stuff do --line stuff
@ -115,8 +117,10 @@ function map.draw()
do --travel nodes do --travel nodes
lg.replaceTransform( Camera.tfNodes ) lg.replaceTransform( Camera.tfNodes )
if map.travelnodes.visible then
map.travelnodes:draw() map.travelnodes:draw()
end end
end
end end