fix a couple of travel node saving bugs

This commit is contained in:
wan-may 2024-05-24 23:43:39 -03:00
parent 9635e0ea2f
commit 03f1a96112
6 changed files with 28 additions and 50 deletions

29
bmp.lua
View File

@ -10,7 +10,7 @@ local bit = require 'bit'
local function getHeader( filename )
local offset = love.data.unpack( "<I4", assert(love.filesystem.read( "data", filename, 14 )), 11 )
local header, size = assert( love.filesystem.read( filename, offset ) )
print( "BMP HEADER", filename, size, "\n", header )
print( "BMP HEADER", filename, size, "\n", string.byte( header, 1, size ) )
return header
end
@ -33,8 +33,8 @@ local formats = {
header = getHeader( "data/earth/travel_nodes.bmp" ),
encode = function( r ) return math.floor( r * 255 / 16 ) end,
bytesPerPixel = 0.5,
w = 512,
h = 285,
w = 800,
h = 400,
}
}
@ -65,8 +65,9 @@ end
local function packTwentyFour( bytes )
local twentyFour = { bytes[1] }
for j = 2, #bytes do
bytes[j] = string.char( bytes[j], bytes[j], bytes[j] )
twentyFour[j] = string.char( bytes[j], bytes[j], bytes[j] )
end
return twentyFour
end
function t.save( data, format )
@ -75,20 +76,19 @@ function t.save( data, format )
local bytes = { format.header }
format.byte = 0
local i = 2
for x = 0, w - 1 do
for y = 0, h - 1 do
for y = h - 1, 0, -1 do
for x = 0, w - 1 do
bytes[i] = format.encode( data:getPixel(x, y) )
i = i + 1
end
end
if format.bytesPerPixel < 1 then bytes = foldByteArray( bytes )
else packTwentyFour( bytes ) end
else bytes = packTwentyFour( bytes ) end
return table.concat( bytes )
end
--takes an array of world-space points in [-180, 180] x [-100, 100]
--e.g { { x = 0.5, y = 0.5 }, { x = 0.4, y = 0.6 }, }, etc.
--and a function which maps points to colours
function t.savePoints( points, format )
format = assert( formats[format] )
@ -102,11 +102,14 @@ function t.savePoints( points, format )
if format.bytesPerPixel < 1 then
for i, point in ipairs( points ) do
local wx, wy = point.x, point.y
--get bitmap coordinates
local x, y = math.floor(format.w * (wx + 180) / 360), math.floor(format.h * (1.0 - (wy + 100) / 200))
-- get bitmap coordinates:
-- N.B., y's divided by 190 rather than 200 because of an idiosyncracy in the travel node image
local x, y = math.floor(format.w * (wx + 180) / 360), math.floor(format.h * (wy + 100) / 190)
--get index into byte array
local idx = 2 + x * format.h + y
bitmap[idx] = 0x0f --0b00001111
local idx = 2 + y * format.w + x
print( "SAVING POINT:", wx, wy, x, format.h - y - 1, idx )
bitmap[idx] = 1 --white: this header uses an indexed palette
end
--now map pixels to 4-bit strings, slap on the header, and pass it back up
return table.concat( foldByteArray( bitmap ) )
@ -133,7 +136,7 @@ function t.savePoints( points, format )
for j = 2, size do
if bitmap[j] == 0
then bitmap[j] = "\0\0\0"
else bitmap[j] = string.char( bitmap[j].attack and 0xff or 0, bitmap[j].place and 0xff or 0, 0 )
else bitmap[j] = string.char( 0, bitmap[j].place and 0xff or 0, bitmap[j].attack and 0xff or 0 ) --bgr24
end
end

View File

@ -19,35 +19,13 @@ function t.contains( button, x, y )
and y < button.y + button.h and y > button.y
end
local function debugLoop()
local i = 0
local j = 0
local a = t
repeat
i = i + 1
--print( "BUTTON", i, tostring( a ) )
a = a.next
until a == t or i > 100
a = t
repeat
j = j + 1
--print( "BUTTON", i, tostring( a ) )
a = a.prev
until a == t or j > 100
print( i, j, "BUTTONS" )
end
local k = 1
function t.new( b )
b = setmetatable( b or {}, t )
b.next = t
t.prev.next = b
b.prev = t.prev
t.prev = b
--nonsense
k = k + 1
print( "ADD BUTTON", k, tostring( b ) )
debugLoop()
return b
end

View File

@ -87,9 +87,9 @@ function t.selectNearest( lines, wx, wy )
end
function t.save( lines )
local str = {}
local str = { "b" }
for i, poly in ipairs( lines ) do
str[i] = table.concat( poly, " " )
str[i + 1] = table.concat( poly, " " )
end
str = table.concat( str, "\nb\n" ):gsub("(%S+) (%S+) ", "%1 %2\n")
return str

View File

@ -23,10 +23,10 @@ end
function love.update( dt )
local tx, ty = 0, 0
local moveCamera = false
if love.keyboard.isScancodeDown( "w" ) then moveCamera = true; ty = ty + 30 * dt end
if love.keyboard.isScancodeDown( "a" ) then moveCamera = true; tx = tx - 30 * dt end
if love.keyboard.isScancodeDown( "s" ) then moveCamera = true; ty = ty - 30 * dt end
if love.keyboard.isScancodeDown( "d" ) then moveCamera = true; tx = tx + 30 * dt end
if love.keyboard.isScancodeDown( "w" ) then moveCamera = true; ty = ty + dt * 150 / Camera.zoom end
if love.keyboard.isScancodeDown( "a" ) then moveCamera = true; tx = tx - dt * 150 / Camera.zoom end
if love.keyboard.isScancodeDown( "s" ) then moveCamera = true; ty = ty - dt * 150 / Camera.zoom end
if love.keyboard.isScancodeDown( "d" ) then moveCamera = true; tx = tx + dt * 150 / Camera.zoom end
if love.keyboard.isScancodeDown( "q" ) then Camera.Zoom( dt * 400 ) end
if love.keyboard.isScancodeDown( "e" ) then Camera.Zoom( -dt* 400 ) end
if moveCamera then Camera.Translate( tx, ty ) end

View File

@ -179,16 +179,15 @@ function map.draw()
end
local function write( filename, string )
print( "Pretending to write", string:len(), "bytes to", filename )
--[[ os.rename( filename, filename..".bak" ) --just in case :^)
print( "Writing", string:len(), "bytes to", filename )
os.rename( filename, filename..".bak" ) --just in case :^)
local file = assert( io.open( filename, "w+" ) )
assert( file:write( string ) )
file:close()]]
file:close()
end
function map.save()
for k, layer in pairs( layers ) do
print( "SAVING:", k, tostring( layer.filename ) )
write( map.path..tostring( layer.filename ), assert( layer:save() ) )
end
end

View File

@ -57,10 +57,6 @@ function t.getPixel( territory, x, y )
return territory.imgd:getPixel( imgx, imgy )
end
function t.toggleVisibility( territory )
end
--[[
0
20 -- once sailable.bmp is brighter than this, the area is traversable by ships
@ -147,7 +143,9 @@ function t.computeBorder( territory, threshold, key )
end
function t.save( territory )
return bmp.save( territory.imgd, "512rgb24" )
local fmt = (territory.name == "sailable") and "512r4" or "512rgb24"
print( "saving bitmap: ", territory.name, fmt )
return bmp.save( territory.imgd, fmt )
end
return t