dcearth/territory.lua

151 lines
4.1 KiB
Lua
Raw Permalink Normal View History

local t = {}
local bmp = require 'bmp'
2023-07-28 23:23:08 +00:00
local lg = assert( love.graphics )
local colours = {
2023-09-04 00:49:02 +00:00
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 ) )
2024-04-21 14:10:53 +00:00
img:setWrap( "repeat" )
2023-07-28 23:23:08 +00:00
local territory = {
visible = true,
name = name,
2024-04-29 01:21:32 +00:00
filename = filename,
2023-07-28 23:23:08 +00:00
colour = colours[name],
2023-09-04 00:49:02 +00:00
border = {},
img = img,
imgd = imgd
2023-07-28 23:23:08 +00:00
}
2023-09-04 00:49:02 +00:00
if name == "sailable" then
t.sailable = territory
t.computeBorder( territory, 20 / 255, "sailable" )
t.computeBorder( territory, 60 / 255, "placeable" )
else
t.computeBorder( territory, 60 / 255, "sea" )
t.computeBorder( territory, 130 / 255, "land" )
2023-09-04 00:49:02 +00:00
end
return setmetatable( territory, {__index = t } )
end
--World space coordinate.
2023-09-04 00:49:02 +00:00
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 )
2023-09-04 00:49:02 +00:00
end
function t.getPixel( territory, x, y )
--ZERO INDEXED
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
--[[
0
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
130 -- if territory.bmp is brighter than this and sailable is darker than 60, structures are placeable.
SO:
SAILABLE: 0 (not), 21 (traverse not place), 61 ( traverse and place )
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 )
2023-09-04 00:49:02 +00:00
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 )
2023-09-04 00:49:02 +00:00
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
2023-07-28 23:23:08 +00:00
function t.draw( territory )
lg.setColor( territory.colour )
lg.draw( territory.img )
end
function t.drawBorder( territory, key )
key = key or "border"
for _, poly in ipairs( territory[key] ) do
2023-09-04 00:49:02 +00:00
lg.line( poly )
end
end
function t.computeBorder( territory, threshold, key )
key = key or "border"
territory[key] = {}
local border = territory[key]
2023-09-04 00:49:02 +00:00
local n = 1
local w, h = territory.imgd:getWidth() - 1, territory.imgd:getHeight() - 1
2024-05-02 00:32:07 +00:00
w, h = math.min( 512, w ), math.min( 285, h )
for x = 0, w do
for y = 0, h do
2023-09-04 00:49:02 +00:00
--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 rightValue = territory.imgd:getPixel( math.min( x + 1, 511 ), y )
2023-09-04 00:49:02 +00:00
local downValue = territory.imgd:getPixel( x, math.min( 284, y + 1 ) )
local isRightEdge =
((curValue >= threshold) and (rightValue < threshold)) or
((curValue < threshold) and (rightValue >= threshold))
2023-09-04 00:49:02 +00:00
local isDownEdge =
((curValue >= threshold) and (downValue < threshold)) or
((curValue < threshold) and (downValue >= threshold))
2023-09-04 00:49:02 +00:00
if isRightEdge then
2023-09-04 00:49:02 +00:00
--print( "Left edge:", brx, bry, trx, try )
border[n] = { x + 1, y, x + 1, y + 1 }
2023-09-04 00:49:02 +00:00
n = n + 1
end
if isDownEdge then
--print( "Down edge:", blx, bly, brx, bry )
border[n] = { x, y + 1, x + 1, y + 1 }
2023-09-04 00:49:02 +00:00
n = n + 1
end
end
end
end
2024-04-28 01:38:23 +00:00
function t.save( territory )
local fmt = (territory.name == "sailable") and "sailable" or "territory"
print( "saving bitmap: ", territory.name, fmt )
return bmp[fmt]( territory.imgd )
end
return t