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 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()
if cities.visible then lg.points( points ) end
end
function t.drawSelected( r )
if not t.visible then return end
local c = t.selectedCity
if not cities.visible then return end
local c = t.selected
if not c then return end
lg.circle( "fill", c.x, c.y, r )
end
function t.drawCapitals()
if t.visible then lg.points( caps ) end
if cities.visible then lg.points( caps ) end
end
function t.selectNearestCity(x, y)
t.selectedCity = cities:getClosestPoint(x, y)
if not t.selectionLocked then t.selected = cities:getClosestPoint(x, y) end
end
function t.load( filename )

View File

@ -50,14 +50,30 @@ function love.draw()
--Edit box.
love.graphics.rectangle( "line", love.graphics.getWidth() / 2, h, love.graphics.getWidth() / 2, 30 )
if map.cities.selectedCity then
local c = map.cities.selectedCity
if 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 )
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( ("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

15
map.lua
View File

@ -49,11 +49,20 @@ function map.draw()
end
if map.sailable.visible then map.sailable:draw() end
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
do --all this stuff is drawn in world coordinates, ( -180, 180 ) x ( -100, 100 )
lg.replaceTransform( Camera.tf )
do --points
@ -66,8 +75,8 @@ function map.draw()
lg.setPointSize( 1.0 * Camera.zoom )
map.cities.drawCapitals()
lg.setColor( 1, 0, 1, 0.5 )
map.cities.drawSelected( 22.0 / Camera.zoom )
lg.setColor( 1, 1, 1, 0.5 )
map.cities.drawSelected( 15.0 / Camera.zoom )
map.ainodes:draw()
end

View File

@ -25,6 +25,17 @@ local function isConnected( startNode, endNode )
return true
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 )
isSailable = sailable

View File

@ -3,14 +3,14 @@ local bmp = require 'bmp'
local lg = assert( love.graphics )
local colours = {
africa = {1,0,0,0.5},
europe = {0,1,0,0.5},
northamerica = {0,0,1,0.5},
russia = {0,1,1,0.5},
southamerica = {1,0,1,0.5},
southasia = {1,1,0,0.5},
sailable = {1, 1, 1, 0.2},
}
africa = {1,0,0,0.5},
europe = {0,1,0,0.5},
northamerica = {0,0,1,0.5},
russia = {0,1,1,0.5},
southamerica = {1,0,1,0.5},
southasia = {1,1,0,0.5},
sailable = {1, 1, 1, 0.2},
}
function t.load( filename, name )
local img, imgd = assert( bmp.load( filename ) )
@ -18,17 +18,30 @@ function t.load( filename, name )
visible = true,
name = name,
colour = colours[name],
border = {},
img = img,
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 } )
end
--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 )
end
function t.getPixel( territory, x, y )
@ -39,7 +52,7 @@ function t.getPixel( territory, x, y )
end
function t.toggleVisibility( territory )
end
--[[
@ -59,11 +72,17 @@ function t.isSailable( x, y )
end
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
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
function t.draw( territory )
@ -71,12 +90,59 @@ function t.draw( territory )
lg.draw( territory.img )
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 )
end
function t.save( nodes, filename )
end
return t