2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
-- Loaders for various BUILD structures for LuaJIT
|
|
|
|
|
|
|
|
|
|
|
|
local ffi = require "ffi"
|
|
|
|
local io = require "io"
|
2012-08-26 22:11:46 +00:00
|
|
|
local bit = require "bit"
|
2012-08-26 22:13:57 +00:00
|
|
|
local string = require "string"
|
|
|
|
local table = require "table"
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
local error = error
|
2012-07-08 21:47:06 +00:00
|
|
|
local assert = assert
|
2013-10-16 19:43:01 +00:00
|
|
|
local pairs = pairs
|
2012-07-08 21:47:06 +00:00
|
|
|
local print = print
|
2012-07-08 21:47:11 +00:00
|
|
|
local setmetatable = setmetatable
|
2012-08-26 22:13:57 +00:00
|
|
|
local tostring = tostring
|
2012-11-18 19:01:39 +00:00
|
|
|
local tonumber = tonumber
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
module(...)
|
|
|
|
|
2013-10-16 19:43:01 +00:00
|
|
|
local STRUCTDEF = {
|
|
|
|
sector = [[
|
2012-07-08 21:47:06 +00:00
|
|
|
int16_t wallptr, wallnum;
|
|
|
|
int32_t ceilingz, floorz;
|
2012-11-17 19:46:18 +00:00
|
|
|
uint16_t ceilingstat, floorstat;
|
2012-07-08 21:47:06 +00:00
|
|
|
int16_t ceilingpicnum, ceilingheinum;
|
|
|
|
int8_t ceilingshade;
|
|
|
|
uint8_t ceilingpal, ceilingxpanning, ceilingypanning;
|
|
|
|
int16_t floorpicnum, floorheinum;
|
|
|
|
int8_t floorshade;
|
|
|
|
uint8_t floorpal, floorxpanning, floorypanning;
|
2014-04-05 11:28:10 +00:00
|
|
|
uint8_t visibility, fogpal;
|
2012-07-08 21:47:06 +00:00
|
|
|
int16_t lotag, hitag, extra;
|
2013-10-16 19:43:01 +00:00
|
|
|
]],
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2013-10-16 19:43:01 +00:00
|
|
|
wall = [[
|
2012-07-08 21:47:06 +00:00
|
|
|
int32_t x, y;
|
|
|
|
int16_t point2, nextwall, nextsector;
|
2012-11-17 19:46:18 +00:00
|
|
|
uint16_t cstat;
|
2012-07-08 21:47:06 +00:00
|
|
|
int16_t picnum, overpicnum;
|
|
|
|
int8_t shade;
|
|
|
|
uint8_t pal, xrepeat, yrepeat, xpanning, ypanning;
|
|
|
|
int16_t lotag, hitag, extra;
|
2013-10-16 19:43:01 +00:00
|
|
|
]],
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2013-10-16 19:43:01 +00:00
|
|
|
sprite = [[
|
2012-07-08 21:47:06 +00:00
|
|
|
int32_t x, y, z;
|
2012-11-17 19:46:18 +00:00
|
|
|
uint16_t cstat;
|
|
|
|
int16_t picnum;
|
2012-07-08 21:47:06 +00:00
|
|
|
int8_t shade;
|
2014-02-08 14:37:51 +00:00
|
|
|
uint8_t pal, clipdist, blend;
|
2012-07-08 21:47:06 +00:00
|
|
|
uint8_t xrepeat, yrepeat;
|
|
|
|
int8_t xoffset, yoffset;
|
|
|
|
int16_t sectnum, statnum;
|
|
|
|
int16_t ang, owner, xvel, yvel, zvel;
|
|
|
|
int16_t lotag, hitag, extra;
|
2013-10-16 19:43:01 +00:00
|
|
|
]],
|
|
|
|
}
|
|
|
|
|
|
|
|
ffi.cdef([[
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
]]..STRUCTDEF.sector..[[
|
|
|
|
} sectortype;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
]]..STRUCTDEF.wall..[[
|
|
|
|
} walltype;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
]]..STRUCTDEF.sprite..[[
|
2012-07-08 21:47:06 +00:00
|
|
|
} spritetype;
|
2013-10-16 19:43:01 +00:00
|
|
|
]])
|
2013-09-05 17:37:40 +00:00
|
|
|
|
|
|
|
ffi.cdef[[
|
|
|
|
size_t fread(void *ptr, size_t size, size_t nmemb, void *stream);
|
2012-07-08 21:47:06 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
local C = ffi.C
|
|
|
|
|
2013-10-16 19:43:01 +00:00
|
|
|
-- [<sector/wall/sprite>].<membername> = true
|
|
|
|
local is_member_tab = {
|
|
|
|
sector = {},
|
|
|
|
wall = {},
|
|
|
|
sprite = {},
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
for what, sdef in pairs(STRUCTDEF) do
|
|
|
|
for membname in string.gmatch(sdef, "([a-z0-9_]+)[,;]") do
|
|
|
|
is_member_tab[what][membname] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ismember(what, membname)
|
|
|
|
return (is_member_tab[what][membname] ~= nil)
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2012-12-23 14:00:00 +00:00
|
|
|
MAX =
|
2012-07-08 21:47:06 +00:00
|
|
|
{
|
|
|
|
SECTORS = { [7]=1024, [8]=4096, [9]=4096 },
|
|
|
|
WALLS = { [7]=8192, [8]=16384, [9]=16384 },
|
|
|
|
SPRITES = { [7]=4096, [8]=16384, [9]=16384 },
|
2012-07-08 21:47:11 +00:00
|
|
|
|
|
|
|
TILES = 30720,
|
2012-07-08 21:47:06 +00:00
|
|
|
}
|
2013-10-16 19:42:59 +00:00
|
|
|
local MAX = MAX
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2013-10-16 19:42:59 +00:00
|
|
|
|
|
|
|
-- <dontclose>: if true, don't close file on error
|
|
|
|
local function doread(fh, basectype, numelts, dontclose)
|
2013-09-06 14:43:17 +00:00
|
|
|
assert(numelts > 0)
|
2013-10-16 19:42:59 +00:00
|
|
|
local typ = ffi.typeof("$ [?]", ffi.typeof(basectype))
|
|
|
|
local cd = ffi.new(typ, numelts)
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2013-09-05 17:37:40 +00:00
|
|
|
if (C.fread(cd, ffi.sizeof(basectype), numelts, fh) ~= numelts) then
|
2013-10-16 19:42:59 +00:00
|
|
|
if (not dontclose) then
|
|
|
|
fh:close()
|
|
|
|
end
|
|
|
|
return nil, "Failed reading"
|
2012-07-08 21:47:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return cd
|
|
|
|
end
|
|
|
|
|
2013-10-16 19:43:03 +00:00
|
|
|
-- Read base palette (i.e. first 768 bytes as R,G,B triplets) from a PALETTE.DAT.
|
2013-12-31 11:51:57 +00:00
|
|
|
-- <noquad>: if true, don't multiply components by 4
|
2013-10-16 19:43:03 +00:00
|
|
|
-- Returns:
|
|
|
|
-- on success: <uint8_t [768]> cdata (palette values scaled by 4)
|
|
|
|
-- on failure: nil, <errmsg>
|
2013-12-31 11:51:57 +00:00
|
|
|
function read_basepal(filename, noquad)
|
2013-10-16 19:43:03 +00:00
|
|
|
local fh, errmsg = io.open(filename, "rb")
|
|
|
|
if (fh == nil) then
|
|
|
|
return nil, errmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
local palette, errmsg = doread(fh, "uint8_t", 768, true)
|
|
|
|
fh:close()
|
|
|
|
|
2013-12-31 11:51:57 +00:00
|
|
|
local f = noquad and 1 or 4
|
|
|
|
|
2013-10-16 19:43:03 +00:00
|
|
|
for i=0,768-1 do
|
2013-12-31 11:51:57 +00:00
|
|
|
palette[i] = f*palette[i]
|
2013-10-16 19:43:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return palette, errmsg
|
|
|
|
end
|
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
local function set_secwalspr_mt(structar, maxidx)
|
|
|
|
local mt = {
|
|
|
|
__index = function(tab, idx)
|
2013-09-05 17:37:40 +00:00
|
|
|
if (not (idx >= 0 and idx < maxidx)) then
|
2012-07-08 21:47:11 +00:00
|
|
|
error("Invalid structure array read access", 2)
|
|
|
|
end
|
|
|
|
return structar[idx]
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function(tab, idx, newval)
|
|
|
|
error('cannot write directly to structure array', 2)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
return setmetatable({}, mt)
|
|
|
|
end
|
|
|
|
|
2012-08-26 22:11:46 +00:00
|
|
|
|
|
|
|
local function get_numyaxbunches(map)
|
|
|
|
if (map.version < 9) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local numbunches = 0
|
2013-04-12 11:59:32 +00:00
|
|
|
local sectsperbunch = { [0]={}, [1]={} }
|
2012-08-26 22:11:46 +00:00
|
|
|
|
|
|
|
for i=0,map.numsectors-1 do
|
|
|
|
for cf=0,1 do
|
|
|
|
local sec = map.sector[i]
|
|
|
|
local stat = (cf==0) and sec.ceilingstat or sec.floorstat
|
|
|
|
local xpan = (cf==0) and sec.ceilingxpanning or sec.floorxpanning
|
|
|
|
|
|
|
|
if (bit.band(stat, 1024) ~= 0) then
|
|
|
|
if (xpan+1 > numbunches) then
|
|
|
|
numbunches = xpan+1
|
|
|
|
end
|
2013-04-12 11:59:32 +00:00
|
|
|
|
|
|
|
if (sectsperbunch[cf][xpan]==nil) then
|
|
|
|
sectsperbunch[cf][xpan] = 1
|
|
|
|
else
|
|
|
|
sectsperbunch[cf][xpan] = sectsperbunch[cf][xpan]+1
|
|
|
|
end
|
2012-08-26 22:11:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-12 11:59:32 +00:00
|
|
|
map.numbunches = numbunches
|
|
|
|
map.sectsperbunch = sectsperbunch
|
2012-08-26 22:11:46 +00:00
|
|
|
end
|
|
|
|
|
2012-08-26 22:13:57 +00:00
|
|
|
--== sprite canonicalizer ==--
|
|
|
|
local function sprite2str(s)
|
|
|
|
local FMT = "%+11d_"
|
|
|
|
-- NOTE: this canonicalization isn't very useful except for debugging
|
|
|
|
-- copy-paste in the editor.
|
|
|
|
-- tostring(s): make sort stable
|
|
|
|
return string.format(FMT:rep(4).."%s", s.x, s.y, s.z, s.ang, tostring(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function canonicalize_sprite_order(map)
|
|
|
|
local numsprites = map.numsprites
|
|
|
|
|
|
|
|
map.spriten2o = {} -- mapping of new to old sprite index
|
|
|
|
|
|
|
|
if (numsprites == 0) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local spriteidx = {}
|
|
|
|
|
|
|
|
for i=0,numsprites-1 do -- 0->1 based indexing
|
|
|
|
spriteidx[i+1] = i
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(spriteidx,
|
|
|
|
function(i1, i2)
|
|
|
|
return sprite2str(map.sprite[i1]) < sprite2str(map.sprite[i2])
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- deep-copied sprite structs
|
|
|
|
local spritedup = {}
|
|
|
|
|
|
|
|
for i=0,numsprites-1 do
|
|
|
|
-- save sorting permutation (0-based -> 0-based)
|
|
|
|
map.spriten2o[i] = assert(spriteidx[i+1])
|
|
|
|
|
|
|
|
-- back up struct
|
|
|
|
spritedup[i] = ffi.new("spritetype")
|
|
|
|
ffi.copy(spritedup[i], map.sprite[i], ffi.sizeof("spritetype"))
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=0,numsprites-1 do -- do the actual rearrangement
|
|
|
|
map.sprite[i] = spritedup[spriteidx[i+1]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-08 21:47:06 +00:00
|
|
|
--== LOADBOARD ==--
|
|
|
|
-- returns:
|
|
|
|
-- on failure, nil, errmsg
|
|
|
|
-- on success, a table
|
|
|
|
-- {
|
|
|
|
-- version = <num>,
|
|
|
|
-- numsectors=<num>, numwalls=<num>, numsprites=<num>,
|
|
|
|
-- sector=<cdata (array of sectortype)>,
|
|
|
|
-- wall=<cdata (array of walltype)>,
|
|
|
|
-- sprite=nil or <cdata> (array of spritetype),
|
|
|
|
-- start =
|
2012-08-26 22:11:46 +00:00
|
|
|
-- { x=<num>, y=<num>, z=<num>, ang=<num>, sectnum=<num> },
|
|
|
|
-- numbunches = <num>,
|
2013-04-12 11:59:32 +00:00
|
|
|
-- sectsperbunch = {
|
|
|
|
-- [0] = { [<bunchnum>]=<number of ceilings> },
|
|
|
|
-- [1] = { [<bunchnum>]=<number of floors> }
|
|
|
|
-- }
|
2012-07-08 21:47:06 +00:00
|
|
|
-- }
|
2012-08-26 22:13:57 +00:00
|
|
|
function loadboard(filename, do_canonicalize_sprite)
|
2013-09-06 14:43:17 +00:00
|
|
|
local fh, errmsg = io.open(filename, "rb")
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
if (fh==nil) then
|
|
|
|
return nil, errmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
local cd = doread(fh, "int32_t", 4)
|
|
|
|
if (cd==nil) then
|
|
|
|
return nil, "Couldn't read header"
|
|
|
|
end
|
|
|
|
|
2012-08-26 22:11:46 +00:00
|
|
|
-- The table we'll return on success
|
|
|
|
local map = {
|
|
|
|
version = cd[0],
|
|
|
|
start = { x=cd[1], y=cd[2], z=cd[3] },
|
|
|
|
}
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
if (map.version < 7 or map.version > 9) then
|
|
|
|
fh:close()
|
|
|
|
return nil, "Invalid map version"
|
|
|
|
end
|
|
|
|
|
|
|
|
cd = doread(fh, "int16_t", 3)
|
|
|
|
if (cd==nil) then
|
|
|
|
return nil, "Couldn't read header (2)"
|
|
|
|
end
|
|
|
|
|
|
|
|
map.start.ang = cd[0]
|
|
|
|
map.start.sectnum = cd[1]
|
|
|
|
|
|
|
|
-- sectors
|
|
|
|
map.numsectors = cd[2]
|
2012-07-08 21:47:11 +00:00
|
|
|
if (map.numsectors == nil) then
|
|
|
|
return nil, "Couldn't read number of sectors"
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
if (map.numsectors <= 0 or map.numsectors > MAX.SECTORS[map.version]) then
|
|
|
|
fh:close()
|
|
|
|
return nil, "Invalid number of sectors"
|
|
|
|
end
|
|
|
|
|
|
|
|
map.sector = doread(fh, "sectortype", map.numsectors)
|
|
|
|
if (map.sector == nil) then
|
|
|
|
return nil, "Couldn't read sectors"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- walls
|
|
|
|
cd = doread(fh, "int16_t", 1)
|
2012-07-08 21:47:11 +00:00
|
|
|
if (cd == nil) then
|
|
|
|
return nil, "Couldn't read number of walls"
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
map.numwalls = cd[0]
|
|
|
|
if (map.numwalls <= 0 or map.numwalls > MAX.WALLS[map.version]) then
|
|
|
|
fh:close()
|
|
|
|
return nil, "Invalid number of walls"
|
|
|
|
end
|
|
|
|
|
|
|
|
map.wall = doread(fh, "walltype", map.numwalls)
|
|
|
|
if (map.wall == nil) then
|
|
|
|
return nil, "Couldn't read walls"
|
|
|
|
end
|
|
|
|
|
|
|
|
-- sprites
|
|
|
|
cd = doread(fh, "int16_t", 1)
|
2012-07-08 21:47:11 +00:00
|
|
|
if (cd == nil) then
|
|
|
|
return nil, "Couldn't read number of sprites"
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
map.numsprites = cd[0]
|
|
|
|
if (map.numsprites < 0 or map.numsprites > MAX.SPRITES[map.version]) then
|
|
|
|
fh:close()
|
|
|
|
return nil, "Invalid number of sprites"
|
|
|
|
end
|
|
|
|
|
2013-09-05 17:37:40 +00:00
|
|
|
if (map.numsprites ~= 0) then
|
|
|
|
map.sprite = doread(fh, "spritetype", map.numsprites)
|
|
|
|
if (map.sprite == nil) then
|
|
|
|
return nil, "Couldn't read sprites"
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
end
|
2012-08-26 22:11:46 +00:00
|
|
|
fh:close()
|
2012-07-08 21:47:06 +00:00
|
|
|
|
2012-08-26 22:13:57 +00:00
|
|
|
if (do_canonicalize_sprite) then
|
|
|
|
-- must do this before setting metatable
|
|
|
|
canonicalize_sprite_order(map)
|
|
|
|
end
|
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
map.sector = set_secwalspr_mt(map.sector, map.numsectors)
|
|
|
|
map.wall = set_secwalspr_mt(map.wall, map.numwalls)
|
|
|
|
map.sprite = set_secwalspr_mt(map.sprite, map.numsprites)
|
|
|
|
|
2013-04-12 11:59:32 +00:00
|
|
|
get_numyaxbunches(map)
|
2012-08-26 22:11:46 +00:00
|
|
|
|
2012-07-08 21:47:06 +00:00
|
|
|
-- done
|
|
|
|
return map
|
|
|
|
end
|
2012-07-08 21:47:11 +00:00
|
|
|
|
|
|
|
|
2013-10-16 19:42:59 +00:00
|
|
|
local picanm_t = ffi.typeof[[
|
|
|
|
struct {
|
|
|
|
uint8_t num; // animate number
|
|
|
|
int8_t xofs, yofs;
|
|
|
|
uint8_t sf; // anim. speed and flags
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
|
|
|
|
local artfile_mt = {
|
|
|
|
__index = {
|
|
|
|
-- Global -> local tile index (-1 if gtile is not in this ART file)
|
|
|
|
toltile = function(self, gtile)
|
|
|
|
if (self.tbeg <= gtile and gtile <= self.tend) then
|
|
|
|
return gtile - self.tbeg
|
|
|
|
end
|
|
|
|
return -1
|
|
|
|
end,
|
|
|
|
|
|
|
|
_check_ltile = function(self, ltile)
|
|
|
|
if (not (ltile >= 0 and ltile < self.numtiles)) then
|
|
|
|
error("Invalid local tile number "..ltile, 3)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
_getofs = function(self, ltile)
|
|
|
|
return self.tiledataofs + self.offs[ltile]
|
|
|
|
end,
|
|
|
|
|
|
|
|
dims = function(self, ltile)
|
2013-10-16 19:43:03 +00:00
|
|
|
self:_check_ltile(ltile)
|
2013-10-16 19:42:59 +00:00
|
|
|
return self.sizx[ltile], self.sizy[ltile]
|
|
|
|
end,
|
|
|
|
|
|
|
|
getpic = function(self, ltile)
|
|
|
|
local sx, sy = self:dims(ltile)
|
|
|
|
if (sx == 0 or sy == 0) then
|
|
|
|
-- Tile nonexistent/empty in this ART file
|
|
|
|
return nil, "Empty tile"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(self.fh:seek("set", self:_getofs(ltile)))
|
|
|
|
return doread(self.fh, "uint8_t", sx*sy, self.grpfh ~= false) -- GRPFH_FALSE
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
|
|
|
|
__metatable = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- af, errmsg = artfile(filename [, grpfh, grpofs])
|
|
|
|
--
|
|
|
|
-- <filename>: File name of the file to get data from, expect if <grpfh>
|
|
|
|
-- passed. Always closed on error. Kept open on success.
|
|
|
|
-- <grpfh>: io.open() file handle to grouping file containing ART
|
|
|
|
-- uncompressed. Never closed, even on error.
|
|
|
|
-- <grpofs>: offset of the ART file in file given by <grpfh>
|
|
|
|
--
|
|
|
|
-- Returns:
|
|
|
|
-- * on error: nil, <errmsg>
|
|
|
|
-- * on success:
|
|
|
|
function artfile(filename, grpfh, grpofs)
|
|
|
|
local ogrpofs
|
|
|
|
local fh
|
|
|
|
|
|
|
|
if (grpfh) then
|
|
|
|
ogrpofs = grpfh:seek()
|
|
|
|
assert(grpfh:seek("set", grpofs))
|
|
|
|
fh = grpfh
|
|
|
|
else
|
|
|
|
local errmsg
|
|
|
|
fh, errmsg = io.open(filename, "rb")
|
|
|
|
if (fh == nil) then
|
|
|
|
return nil, errmsg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Close file on error?
|
|
|
|
local dontclose = (grpfh ~= nil)
|
|
|
|
|
|
|
|
-- Maybe close file handle and return error message <msg>
|
|
|
|
local function err(msg, ...)
|
|
|
|
if (not dontclose) then
|
|
|
|
fh:close()
|
|
|
|
end
|
|
|
|
return nil, string.format(msg, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local hdr = doread(fh, "int32_t", 4, dontclose)
|
|
|
|
-- artversion, numtiles, localtilestart, localtileend
|
|
|
|
if (hdr == nil) then
|
|
|
|
return err("Couldn't read header")
|
|
|
|
end
|
|
|
|
|
|
|
|
local af = {
|
|
|
|
filename = filename,
|
|
|
|
fh = fh,
|
|
|
|
grpfh = grpfh or false, -- GRPFH_FALSE
|
|
|
|
|
|
|
|
tbeg = hdr[2],
|
|
|
|
tend = hdr[3],
|
|
|
|
numtiles = hdr[3]-hdr[2]+1,
|
|
|
|
|
|
|
|
-- Members inserted later:
|
|
|
|
-- sizx, sizy: picanm: arrays of length af.numtiles
|
|
|
|
-- tiledataofs: byte offset in `fh' to beginning of tile data
|
|
|
|
-- offs: local byte offsets of each tile, relative of af.tiledataofs
|
|
|
|
--
|
|
|
|
-- Thus,
|
|
|
|
-- af.tiledataofs + af.offs[localtilenum]
|
|
|
|
-- is the byte offset of global tile
|
|
|
|
-- af.tbeg + localtilenum
|
|
|
|
-- in `fh'.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (af.numtiles <= 0) then
|
|
|
|
return err("Invalid tile start/end or empty ART file")
|
|
|
|
end
|
|
|
|
|
|
|
|
local lasttile = af.tbeg + af.numtiles
|
|
|
|
if (lasttile >= MAX.TILES) then
|
|
|
|
return err("Last tile %d beyond MAXTILES-1 (%d)", lasttile, MAX.TILES-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
af.sizx = doread(fh, "int16_t", af.numtiles, dontclose)
|
|
|
|
af.sizy = doread(fh, "int16_t", af.numtiles, dontclose)
|
|
|
|
|
|
|
|
if (af.sizx==nil or af.sizy==nil) then
|
|
|
|
return err("Couldn't read sizx or sizy arrays")
|
|
|
|
end
|
|
|
|
|
|
|
|
af.picanm = doread(fh, picanm_t, af.numtiles, dontclose)
|
|
|
|
|
|
|
|
if (af.picanm == nil) then
|
|
|
|
return err("Couldn't read picanm array")
|
|
|
|
end
|
|
|
|
|
|
|
|
af.tiledataofs = assert(fh:seek())
|
|
|
|
af.offs = ffi.new("uint32_t [?]", af.numtiles)
|
|
|
|
|
|
|
|
local curofs = 0
|
|
|
|
|
|
|
|
for i=0,af.numtiles-1 do
|
|
|
|
local sx, sy = af.sizx[i], af.sizy[i]
|
|
|
|
|
|
|
|
if (sx > 0 and sy > 0) then
|
|
|
|
af.offs[i] = curofs
|
|
|
|
curofs = curofs + sx*sy
|
|
|
|
elseif (sx < 0 or sy < 0) then
|
|
|
|
-- Let's sanity-check stuff a little
|
|
|
|
return err("Local tile %d has negative x or y size", i)
|
|
|
|
else
|
|
|
|
af.offs[i] = -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(af, artfile_mt)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
local function set_sizarray_mt(sizar)
|
|
|
|
local mt = {
|
|
|
|
__index = function(tab, idx)
|
2013-09-05 17:37:40 +00:00
|
|
|
if (not (idx >= 0 and idx < MAX.TILES)) then
|
2012-07-08 21:47:11 +00:00
|
|
|
error("Invalid tile size array read access", 2)
|
|
|
|
end
|
|
|
|
return sizar[idx]
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function(tab, idx, newval)
|
2013-09-05 17:37:40 +00:00
|
|
|
if (not (idx >= 0 and idx < MAX.TILES)) then
|
2012-07-08 21:47:11 +00:00
|
|
|
error("Invalid tile size array write access", 2)
|
|
|
|
end
|
|
|
|
sizar[idx] = newval
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
return setmetatable({}, mt)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--== LOADARTS (currently tilesizx and tilesizy only) ==--
|
|
|
|
-- filenames: a table with ART file names
|
|
|
|
-- returns:
|
|
|
|
-- on failure, nil, errmsg
|
|
|
|
-- on success, a table
|
|
|
|
-- {
|
|
|
|
-- sizx = <cdata (array of length MAXTILES)>
|
|
|
|
-- sizy = <cdata (array of length MAXTILES)>
|
|
|
|
-- }
|
|
|
|
function loadarts(filenames)
|
|
|
|
local tile = {
|
|
|
|
sizx = ffi.new("int16_t [?]", MAX.TILES),
|
|
|
|
sizy = ffi.new("int16_t [?]", MAX.TILES),
|
|
|
|
}
|
|
|
|
|
|
|
|
for fni=1,#filenames do
|
|
|
|
local fn = filenames[fni]
|
2013-09-06 14:43:17 +00:00
|
|
|
local fh, errmsg = io.open(fn, "rb")
|
2012-07-08 21:47:11 +00:00
|
|
|
|
|
|
|
if (fh==nil) then
|
|
|
|
return nil, errmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
local cd = doread(fh, "int32_t", 4)
|
|
|
|
-- artversion, numtiles, localtilestart, localtileend
|
|
|
|
if (cd==nil) then
|
|
|
|
fh:close()
|
|
|
|
return nil, fn..": Couldn't read header"
|
|
|
|
end
|
|
|
|
|
|
|
|
local localtilestart = cd[2]
|
2012-12-23 14:00:00 +00:00
|
|
|
local numtileshere = cd[3]-localtilestart+1
|
|
|
|
-- print(fn.. ": "..cd[2].. ", "..cd[3])
|
2012-07-08 21:47:11 +00:00
|
|
|
|
|
|
|
if (numtileshere < 0 or localtilestart+numtileshere >= MAX.TILES) then
|
|
|
|
fh:close()
|
|
|
|
return nil, fn..": Invalid tile start/end"
|
|
|
|
end
|
|
|
|
|
|
|
|
if (numtileshere==0) then
|
|
|
|
fh:close()
|
|
|
|
else
|
|
|
|
-- X sizes
|
|
|
|
cd = doread(fh, "int16_t", numtileshere)
|
|
|
|
if (cd == nil) then
|
|
|
|
fh:close()
|
|
|
|
return nil, fn..": Couldn't read tilesizx array"
|
|
|
|
end
|
|
|
|
|
|
|
|
ffi.copy(tile.sizx+localtilestart, cd, numtileshere*2)
|
|
|
|
|
|
|
|
-- Y sizes
|
|
|
|
cd = doread(fh, "int16_t", numtileshere)
|
|
|
|
if (cd == nil) then
|
|
|
|
fh:close()
|
|
|
|
return nil, fn..": Couldn't read tilesizy array"
|
|
|
|
end
|
|
|
|
|
|
|
|
ffi.copy(tile.sizy+localtilestart, cd, numtileshere*2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
tile.sizx = set_sizarray_mt(tile.sizx)
|
|
|
|
tile.sizy = set_sizarray_mt(tile.sizy)
|
|
|
|
|
|
|
|
return tile
|
|
|
|
end
|
2012-11-18 19:01:39 +00:00
|
|
|
|
2014-06-01 20:50:14 +00:00
|
|
|
-- defs [, rdefs] = readdefs(fn [, alsoreverse])
|
|
|
|
function readdefs(fn, alsoreverse)
|
2012-11-18 19:01:39 +00:00
|
|
|
local fh, errmsg = io.open(fn)
|
|
|
|
|
|
|
|
if (fh==nil) then
|
|
|
|
return nil, errmsg
|
|
|
|
end
|
|
|
|
|
2014-06-01 20:50:14 +00:00
|
|
|
local defs, rdefs = {}, nil
|
2012-11-18 19:01:39 +00:00
|
|
|
|
2013-04-12 11:59:32 +00:00
|
|
|
for line in fh:lines() do
|
2012-11-18 19:01:39 +00:00
|
|
|
local defname, numstr = string.match(line, "#?%s*define%s+([%a_][%w_]+)%s+([0-9]+)")
|
|
|
|
if (defname) then
|
|
|
|
defs[defname] = tonumber(numstr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-01 20:50:14 +00:00
|
|
|
if (alsoreverse) then
|
|
|
|
rdefs = {}
|
|
|
|
for defname, num in pairs(defs) do
|
|
|
|
rdefs[num] = defname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-18 19:01:39 +00:00
|
|
|
fh:close()
|
2014-06-01 20:50:14 +00:00
|
|
|
return defs, rdefs
|
2012-11-18 19:01:39 +00:00
|
|
|
end
|