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
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 = {
name = line:sub( 1, 39 ):gsub("%s+$",""),
@ -67,6 +68,9 @@ function t.load( filename )
caps[idxCaps], caps[idxCaps + 1] = x, y
idxCaps = idxCaps + 2
end
else
print( "CITIES: malformed line:", line )
end
end
--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 k = 1
for line in assert( lfs.lines( filename ) ) do
if line == "b" then
if line:find "b" then
k = 1
n = n + 1
if #poly > 2 then n = n + 1 end
poly = {}
polys[n] = poly
else
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
k = k + 2
else
print( "LINES: malformed line:", filename, line )
end
end
end
if not polys[n] or (#(polys[n]) < 3) then
polys[n] = nil
n = n - 1
end
print( "LOADED", filename, n )
return setmetatable( polys, {__index = t } )

View File

@ -1,4 +1,5 @@
local love = assert( love, "This tool requires LOVE: love2d.org" )
--assert( require('mobdebug') ).start() --remote debugger
local map = require 'map'
local button = require 'button'
local SAVEDIRECTORY = "out/"
@ -11,12 +12,16 @@ function love.load()
lfs.setIdentity( "dcearth", false )
assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" ))
assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" ))
map.load()
love.graphics.setNewFont( 12, "mono" )
end
function love.directorydropped( path )
love.filesystem.mount( path, "" )
return map.load( path )
end
function love.update( dt )
local tx, ty = 0, 0
local moveCamera = false
@ -57,6 +62,10 @@ local layerButtons = {
function love.draw()
if not map.loaded then
return love.graphics.print( "Drag and drop folder to begin.")
end
love.graphics.push( "all" )
map.draw()
love.graphics.pop()
@ -85,7 +94,7 @@ function love.draw()
--Edit box.
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
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
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.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 )
elseif map.travelnodes.selected then
elseif map.loaded and map.travelnodes.selected then
local c = map.travelnodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
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.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) )
elseif map.ainodes.selectedNode then
elseif map.loaded and map.ainodes.selectedNode then
local c = map.ainodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
@ -122,16 +131,18 @@ end
function love.mousepressed( x, y, button, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y )
if map.loaded then
if button == 1 then
map.cities.lockSelection()
else
map.cities.unlockSelection()
end
end
print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) )
end
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
local function ToggleVisibility( layer )
@ -155,7 +166,9 @@ local layerVisibilityKeybinds = {
["8"] = "coastlines",
["9"] = "coastlinesLow",
["0"] = "international",
["-"] = "cities"
["-"] = "cities",
["="] = "travelnodes",
["backspace"] = "ainodes",
}
function love.keypressed(key)

20
map.lua
View File

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