Display sailable border.

This commit is contained in:
wan-may 2023-09-03 21:49:02 -03:00
parent f30f27bfb1
commit c6890d5ef5
5 changed files with 137 additions and 26 deletions

View File

@ -11,25 +11,34 @@ local cities
local points = {} local points = {}
local caps = {} local caps = {}
t.selectedCity = nil t.selected = nil
t.selectionLocked = false
function t.lockSelection()
t.selectionLocked = true
end
function t.unlockSelection()
t.selectionLocked = false
end
function t.draw() function t.draw()
if cities.visible then lg.points( points ) end if cities.visible then lg.points( points ) end
end end
function t.drawSelected( r ) function t.drawSelected( r )
if not t.visible then return end if not cities.visible then return end
local c = t.selectedCity local c = t.selected
if not c then return end if not c then return end
lg.circle( "fill", c.x, c.y, r ) lg.circle( "fill", c.x, c.y, r )
end end
function t.drawCapitals() function t.drawCapitals()
if t.visible then lg.points( caps ) end if cities.visible then lg.points( caps ) end
end end
function t.selectNearestCity(x, y) function t.selectNearestCity(x, y)
t.selectedCity = cities:getClosestPoint(x, y) if not t.selectionLocked then t.selected = cities:getClosestPoint(x, y) end
end end
function t.load( filename ) function t.load( filename )

View File

@ -50,14 +50,30 @@ 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.selectedCity then if map.cities.selected then
local c = map.cities.selectedCity 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 )
love.graphics.setColor( 1, 1, 1, 1 ) love.graphics.setColor( 1, 1, 1, 1 )
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
local c = map.travelnodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
love.graphics.setColor( 1, 1, 1, 1 )
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
local c = map.ainodes.selected
love.graphics.setColor( 0.2, 0.1, 0.1, 0.5 )
love.graphics.rectangle( "fill", 0, 0, 150 ,100 )
love.graphics.setColor( 1, 1, 1, 1 )
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\noffensive: %s"):format(c.number, c.x, c.y, c.attack) )
end end
end end

15
map.lua
View File

@ -49,11 +49,20 @@ function map.draw()
end end
if map.sailable.visible then map.sailable:draw() end if map.sailable.visible then map.sailable:draw() end
lg.setBlendMode( "alpha" ) lg.setBlendMode( "alpha" )
lg.setColor( 1, 1, 1, 1 )
end
do --borders
lg.setColor( 1,1,1,1)
lg.setLineJoin( "none" )
lg.setLineWidth( 1 / Camera.zoom )
map.sailable:drawBorder()
end 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 )
lg.replaceTransform( Camera.tf ) lg.replaceTransform( Camera.tf )
do --points do --points
@ -66,8 +75,8 @@ function map.draw()
lg.setPointSize( 1.0 * Camera.zoom ) lg.setPointSize( 1.0 * Camera.zoom )
map.cities.drawCapitals() map.cities.drawCapitals()
lg.setColor( 1, 0, 1, 0.5 ) lg.setColor( 1, 1, 1, 0.5 )
map.cities.drawSelected( 22.0 / Camera.zoom ) map.cities.drawSelected( 15.0 / Camera.zoom )
map.ainodes:draw() map.ainodes:draw()
end end

View File

@ -25,6 +25,17 @@ local function isConnected( startNode, endNode )
return true return true
end end
function t.getClosest( nodes, x, y )
local d = math.huge
local closestNode
for _, node in pairs( nodes.nodes ) do
local nx, ny = node.x, node.y
local nd = (nx - x) * (nx - x) + (ny - y) * (ny - y)
if nd < d then d = nd; closestNode = node end
end
return closestNode
end
function t.load( filename, sailable ) function t.load( filename, sailable )
isSailable = sailable isSailable = sailable

View File

@ -3,14 +3,14 @@ local bmp = require 'bmp'
local lg = assert( love.graphics ) local lg = assert( love.graphics )
local colours = { local colours = {
africa = {1,0,0,0.5}, africa = {1,0,0,0.5},
europe = {0,1,0,0.5}, europe = {0,1,0,0.5},
northamerica = {0,0,1,0.5}, northamerica = {0,0,1,0.5},
russia = {0,1,1,0.5}, russia = {0,1,1,0.5},
southamerica = {1,0,1,0.5}, southamerica = {1,0,1,0.5},
southasia = {1,1,0,0.5}, southasia = {1,1,0,0.5},
sailable = {1, 1, 1, 0.2}, sailable = {1, 1, 1, 0.2},
} }
function t.load( filename, name ) function t.load( filename, name )
local img, imgd = assert( bmp.load( filename ) ) local img, imgd = assert( bmp.load( filename ) )
@ -18,17 +18,30 @@ function t.load( filename, name )
visible = true, visible = true,
name = name, name = name,
colour = colours[name], colour = colours[name],
border = {},
img = img, img = img,
imgd = imgd imgd = imgd
} }
if name == "sailable" then t.sailable = territory end if name == "sailable" then
t.sailable = territory
t.computeBorder( territory, 20 / 255 )
end
return setmetatable( territory, {__index = t } ) return setmetatable( territory, {__index = t } )
end end
--World space coordinate. --World space coordinate.
local function ToWorld( x, y )
return x * 360 / 512 - 180, y * 200 / 285 - 100
end
local function ToPixel( x, y )
return (x + 180) * 512 / 360, 285 - ( y + 100 ) * 285 / 200
end
function t.isValid( x, y ) function t.isValid( x, y )
end end
function t.getPixel( territory, x, y ) function t.getPixel( territory, x, y )
@ -39,7 +52,7 @@ function t.getPixel( territory, x, y )
end end
function t.toggleVisibility( territory ) function t.toggleVisibility( territory )
end end
--[[ --[[
@ -59,11 +72,17 @@ function t.isSailable( x, y )
end end
function t.canPlaceShip( territory, x, y ) function t.canPlaceShip( territory, x, y )
local water = assert( t.sailable )
if water:getPixel( x, y ) > 60 / 255
and territory:getPixel( x, y ) > 60 / 255 then return true end
return false
end end
function t.canPlaceLand( territory, x, y ) function t.canPlaceLand( territory, x, y )
local water = assert( t.sailable )
if water:getPixel( x, y ) < 60 / 255
and territory:getPixel( x, y ) > 130 / 255 then return true end
return false
end end
function t.draw( territory ) function t.draw( territory )
@ -71,12 +90,59 @@ function t.draw( territory )
lg.draw( territory.img ) lg.draw( territory.img )
end end
function t.drawBorder( territory )
--lg.setColor( territory.colour )
for _, poly in ipairs( territory.border ) do
lg.line( poly )
end
end
function t.computeBorder( territory, threshold )
territory.border = {}
local border = territory.border
local n = 1
for x = 0, 511 do
for y = 0, 284 do
--Bottom left, bottom right, and top right of pixel in image coordinates:
local blx, bly = x, y + 1
local brx, bry = x + 1, y + 1
local trx, try = x + 1, y
--Sample image and detect edge:
local curValue = territory.imgd:getPixel( x, y )
local leftValue = territory.imgd:getPixel( math.min( x + 1, 511 ), y )
local downValue = territory.imgd:getPixel( x, math.min( 284, y + 1 ) )
local isLeftEdge =
((curValue >= threshold) and (leftValue < threshold)) or
((curValue <= threshold) and (leftValue > threshold))
local isDownEdge =
((curValue >= threshold) and (downValue < threshold)) or
((curValue <= threshold) and (downValue > threshold))
if isLeftEdge then
--print( "Left edge:", brx, bry, trx, try )
border[n] = { brx, bry, trx, try }
n = n + 1
end
if isDownEdge then
--print( "Down edge:", blx, bly, brx, bry )
border[n] = { blx, bly, brx, bry }
n = n + 1
end
end
end
end
function t.drawConnections( nodes ) function t.drawConnections( nodes )
end end
function t.save( nodes, filename ) function t.save( nodes, filename )
end end
return t return t