Render travel node adjacency; render markers as points; turn off texture filtering

This commit is contained in:
wan-may 2023-08-28 20:12:43 -03:00
parent ee7a32af21
commit f30f27bfb1
6 changed files with 118 additions and 34 deletions

46
ai.lua
View File

@ -2,29 +2,59 @@
local t = {} local t = {}
local bmp = require 'bmp' local bmp = require 'bmp'
local lg = assert( love.graphics ) local lg = assert( love.graphics )
local print = print
function t.load( filename ) function t.load( filename )
local img, imgd = bmp.load( filename ) local img, imgd = bmp.load( filename )
local nodes = { local nodes = {
visible = true, visible = true,
att = {}, att = {},
ptsAtt = {},
def = {}, def = {},
ptsDef = {},
img = img, img = img,
imgd = imgd } imgd = imgd }
print( "PIXEL: ", imgd:getPixel( 1, 1 ) ) print( "=== Loading AI Markers: ===" )
for x = 0, 511 do
return setmetatable( nodes, {__index = t } ) for y = 0, 284 do
local r, g = imgd:getPixel( x, 284 - y )
if r > 0.5 or g > 0.5 then
local long = x * (360 / imgd:getWidth()) - 180
local lat = y * (200 / img:getHeight()) - 100
local set = (r > 0.5) and nodes.att or nodes.def
set[#set + 1] = {x = long, y = lat}
print( #set, long, lat )
end
end
end
do
local k = 1
for i, point in ipairs( nodes.att ) do
nodes.ptsAtt[k], nodes.ptsAtt[k + 1] = point.x, point.y
k = k + 2
end
k = 1
for j, point in ipairs( nodes.def ) do
nodes.ptsDef[k], nodes.ptsDef[k + 1] = point.x, point.y
k = k + 2
end
end
return setmetatable( nodes, {__index = t } )
end end
function t.draw( nodes ) function t.draw( nodes )
lg.points( nodes.att ) lg.setColor( 1, 0, 0, 0.5 )
lg.points( nodes.def ) lg.points( nodes.ptsAtt )
lg.draw( nodes.img ) lg.setColor( 0, 1, 0, 0.5 )
lg.points( nodes.ptsDef )
end end
function t.save( nodes, filename ) function t.save( nodes, filename )
end end
return t return t

View File

@ -10,7 +10,9 @@ local bit = require 'bit'
function t.load( filename ) function t.load( filename )
local imgd = love.image.newImageData( filename ) local imgd = love.image.newImageData( filename )
print( "LOADING BITMAP: ", filename, imgd:getSize(), imgd:getFormat(), imgd:getDimensions() ) print( "LOADING BITMAP: ", filename, imgd:getSize(), imgd:getFormat(), imgd:getDimensions() )
return love.graphics.newImage( imgd ), imgd local img = love.graphics.newImage( imgd )
img:setFilter( "nearest", "nearest" )
return img, imgd
end end
function t.save( data, format ) function t.save( data, format )

View File

@ -48,7 +48,7 @@ function t.Edit( points, point, x, y )
end end
function t.Add( points ) function t.Add( points, x, y )
end end

15
map.lua
View File

@ -29,25 +29,18 @@ 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" )
map.international = Lines.load( "data/earth/international.dat" ) map.international = Lines.load( "data/earth/international.dat" )
map.travelnodes = Nodes.load( "data/earth/travel_nodes.bmp" ) map.sailable = Territory.load( "data/earth/sailable.bmp", "sailable" )
map.travelnodes = Nodes.load( "data/earth/travel_nodes.bmp", map.sailable.isSailable ) --travel node adjacency matrix depends on sailable bitmap
map.ainodes = AI.load( "data/earth/ai_markers.bmp" ) map.ainodes = AI.load( "data/earth/ai_markers.bmp" )
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.sailable = Territory.load( "data/earth/sailable.bmp", "sailable" )
end end
function map.draw() function map.draw()
lg.clear( 0, 0, 0, 1 ) lg.clear( 0, 0, 0, 1 )
do -- ai nodes
lg.replaceTransform( Camera.tfTerritory )
map.ainodes:draw()
end
do --territory do --territory
lg.replaceTransform( Camera.tfTerritory ) lg.replaceTransform( Camera.tfTerritory )
lg.setBlendMode( "add" ) lg.setBlendMode( "add" )
@ -75,6 +68,8 @@ function map.draw()
lg.setColor( 1, 0, 1, 0.5 ) lg.setColor( 1, 0, 1, 0.5 )
map.cities.drawSelected( 22.0 / Camera.zoom ) map.cities.drawSelected( 22.0 / Camera.zoom )
map.ainodes:draw()
end end
do --line stuff do --line stuff

View File

@ -5,25 +5,58 @@ local t = {}
local bmp = require 'bmp' local bmp = require 'bmp'
local lg = assert( love.graphics ) local lg = assert( love.graphics )
function t.load( filename ) local isSailable
local function isConnected( startNode, endNode )
local ix, iy, fx, fy = startNode.x, startNode.y, endNode.x, endNode.y
if fx < -180 then fx = fx + 180 end
if fx > 180 then fx = fx - 180 end
local dx, dy = fx - ix, fy - iy
local mag = math.sqrt( dx * dx + dy * dy )
local n = 2 * math.floor( mag )
dx, dy = 0.5 * dx / mag, 0.5 * dy / mag
for i = 1, n do
ix, iy = ix + dx, iy + dy
if not( isSailable( ix, iy ) ) then return nil end
end
return true
end
function t.load( filename, sailable )
isSailable = sailable
local img, imgd = bmp.load( filename ) local img, imgd = bmp.load( filename )
local nodes = { visible = true, points = {}, connections = {}, img = img } local nodes = { visible = true, nodes = {}, points = {}, connections = {}, img = img }
print( "=== Loading Nodes: ===" ) print( "=== Loading Nodes: ===" )
local n = 1
for x = 0, 799 do for x = 0, 799 do
for y = 0, 399 do for y = 0, 399 do
if imgd:getPixel( x, 399 - y ) > 0 then if imgd:getPixel( x, 399 - y ) > 0 then
local long = 360 * ( x - 800 ) / 800 - 360 / 2 + 360 local long = 360 * ( x - 800 ) / 800 - 360 / 2 + 360
local lat = 360 * ( 600 / 800 ) * ( 600 - y ) / 600 - 180 local lat = 360 * ( 600 / 800 ) * ( 600 - y ) / 600 - 180
local n = #nodes.points nodes.nodes[n] = {x = long, y = lat}
nodes.points[n + 1] = long nodes.points[ 2 * n - 1 ] = long
nodes.points[n + 2] = lat nodes.points[ 2 * n ] = lat
print( long, lat ) print( n, long, lat )
n = n + 1
end end
end end
end end
for i, srcNode in ipairs( nodes.nodes ) do
local adjacent = {}
for j, destNode in ipairs( nodes.nodes ) do
adjacent[j] = isConnected( srcNode, destNode )
end
nodes.connections[i] = adjacent
end
print( "=== Nodes Loaded ===" ) print( "=== Nodes Loaded ===" )
return setmetatable( nodes, {__index = t } ) return setmetatable( nodes, {__index = t } )
end end
@ -36,6 +69,13 @@ function t.draw( nodes )
lg.setPointSize( 10 ) lg.setPointSize( 10 )
lg.setColor( 1, 1, 1, 0.5 ) lg.setColor( 1, 1, 1, 0.5 )
lg.points( nodes.points ) lg.points( nodes.points )
for i, connection in pairs( nodes.connections ) do
for j in pairs( connection ) do
local ix, iy, fx, fy = nodes.nodes[i].x, nodes.nodes[i].y, nodes.nodes[j].x, nodes.nodes[j].y
lg.line( ix, iy, fx, fy )
end
end
end end
function t.drawConnections( nodes ) function t.drawConnections( nodes )

View File

@ -21,6 +21,8 @@ function t.load( filename, name )
img = img, img = img,
imgd = imgd imgd = imgd
} }
if name == "sailable" then t.sailable = territory end
return setmetatable( territory, {__index = t } ) return setmetatable( territory, {__index = t } )
end end
@ -31,7 +33,9 @@ end
function t.getPixel( territory, x, y ) function t.getPixel( territory, x, y )
--ZERO INDEXED --ZERO INDEXED
return territory.img[math.floor( 512 * ( x + 180 ) / 360 ) ][ math.floor( 285 * ( y + 100 ) / 200 ) ] local imgx, imgy = math.floor( 512 * ( x + 180 ) / 360 ), math.floor( 285 * ( y + 100 ) / 200 )
--print( territory.name, imgx, imgy )
return territory.imgd:getPixel( imgx, imgy )
end end
function t.toggleVisibility( territory ) function t.toggleVisibility( territory )
@ -39,15 +43,28 @@ function t.toggleVisibility( territory )
end end
--[[ --[[
0 0
20 -- once sailable.bmp is brighter than this, the area is traversable by ships 20 -- once sailable.bmp is brighter than this, the area is traversable by ships
60 -- once sailable.bmp and territory.bmp are brighter than this, ships can be placed here 60 -- once sailable.bmp and territory.bmp are brighter than this, ships can be placed here
130 -- if territory.bmp is brighter than this and sailable is darker than 60, structures are placeable. 130 -- if territory.bmp is brighter than this and sailable is darker than 60, structures are placeable.
SO: SO:
SAILABLE: 0 (not), 21 (traverse not place), 61 ( traverse and place ) SAILABLE: 0 (not), 21 (traverse not place), 61 ( traverse and place )
TERRITORY: 131 ( place land if sailable <= 60 ), 61 ( place sea ), 0 TERRITORY: 131 ( place land if sailable <= 60 ), 61 ( place sea ), 0
]] ]]
function t.isSailable( x, y )
local water = assert( t.sailable )
if water:getPixel( x, y ) > 20 / 255 then return true end
return false
end
function t.canPlaceShip( territory, x, y )
end
function t.canPlaceLand( territory, x, y )
end
function t.draw( territory ) function t.draw( territory )
lg.setColor( territory.colour ) lg.setColor( territory.colour )