2012-10-07 15:25:52 +00:00
|
|
|
|
|
|
|
-- This file contains LuaJIT definitions of stuff that's common to the game and
|
2012-11-08 20:17:19 +00:00
|
|
|
-- editor. The 'decl' function is expected to be defined in the global
|
|
|
|
-- environment.
|
2013-09-05 17:37:44 +00:00
|
|
|
-- See the file "BUILDLIC.TXT" distributed with EDuke32 for license info.
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-05-06 19:43:34 +00:00
|
|
|
-- Will be 'true' if running from engine Lua state:
|
|
|
|
local _LUNATIC_AUX = _LUNATIC_AUX
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
local ffiC = ffi.C
|
2013-02-28 17:30:04 +00:00
|
|
|
local bit = require("bit")
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-09-05 17:37:46 +00:00
|
|
|
local bor = bit.bor
|
2013-10-16 19:43:06 +00:00
|
|
|
local pcall = pcall
|
2013-09-05 17:37:46 +00:00
|
|
|
|
|
|
|
ffi.cdef "const char **g_argv;"
|
|
|
|
|
|
|
|
-- Lunatic debugging options (-Lopts=<opt1>,<opt2>,... from the command line):
|
|
|
|
-- diag: print diagnostic information
|
|
|
|
-- nojit: disable JIT compilation
|
|
|
|
-- traces: load LuaJIT's 'v' module, printing trace info
|
|
|
|
-- (env var: LUAJIT_VERBOSEFILE)
|
|
|
|
-- dump: load LuaJIT's 'dump' module, printing generated IR/machine code
|
|
|
|
-- (env var: LUAJIT_DUMPFILE)
|
2013-10-16 19:43:06 +00:00
|
|
|
-- profile: load LuaJIT's 'jit.p' module for profiling (LuaJIT 2.1 only)
|
|
|
|
-- (env var: LUAJIT_PROFILEFILE)
|
2014-03-10 20:08:27 +00:00
|
|
|
-- strict: catch various conditions that may indicate a logical error
|
2013-09-06 14:43:20 +00:00
|
|
|
-- TODO for strict: actor[], spriteext[], per-actor gamevars
|
2013-09-05 17:37:46 +00:00
|
|
|
local debug_flags = {}
|
|
|
|
local IS_DEBUG_FLAG = {
|
|
|
|
diag=true, nojit=true, traces=true, dump=true,
|
2013-10-16 19:43:06 +00:00
|
|
|
strict=true, profile=true,
|
2013-09-05 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- Handle command-line argument. (Look for -Lopts=...)
|
|
|
|
local function handle_cmdline_arg(str)
|
|
|
|
local opts = str:match("^-Lopts=(.*)")
|
|
|
|
|
|
|
|
if (opts ~= nil) then
|
|
|
|
for opt in opts:gmatch("[^,]+") do
|
|
|
|
if (IS_DEBUG_FLAG[opt]) then
|
|
|
|
debug_flags[opt] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local i=0
|
|
|
|
while (ffiC.g_argv[i] ~= nil) do
|
|
|
|
handle_cmdline_arg(ffi.string(ffiC.g_argv[i]))
|
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Print diagnostic information?
|
|
|
|
ffi.cdef("enum { _DEBUG_LUNATIC="..(debug_flags.diag and 1 or 0).." }")
|
|
|
|
-- Be strict?
|
|
|
|
ffi.cdef("enum { _LUNATIC_STRICT="..(debug_flags.strict and 1 or 0).." }")
|
|
|
|
|
|
|
|
if (debug_flags.nojit) then
|
2013-02-24 16:05:42 +00:00
|
|
|
require("jit").off()
|
|
|
|
end
|
|
|
|
|
2013-05-06 19:43:34 +00:00
|
|
|
if (not _LUNATIC_AUX) then
|
2013-09-05 17:37:46 +00:00
|
|
|
if (debug_flags.dump) then
|
2013-05-06 19:43:34 +00:00
|
|
|
require("dump").on("+rs")
|
2013-09-05 17:37:46 +00:00
|
|
|
elseif (debug_flags.traces) then
|
2013-05-06 19:43:34 +00:00
|
|
|
require("v").on()
|
|
|
|
end
|
2013-10-16 19:43:06 +00:00
|
|
|
|
|
|
|
if (debug_flags.profile) then
|
|
|
|
if (pcall(function() require("jit.p").start() end) == false) then
|
|
|
|
print("Warning: failed enabing profiler. Running LuaJIT 2.0 build?")
|
|
|
|
end
|
|
|
|
end
|
2013-02-28 17:30:04 +00:00
|
|
|
end
|
|
|
|
|
2013-02-16 18:53:04 +00:00
|
|
|
local math = require("math")
|
2012-11-30 18:57:50 +00:00
|
|
|
local string = require("string")
|
2013-02-10 16:23:54 +00:00
|
|
|
local table = require("table")
|
2012-11-30 18:57:50 +00:00
|
|
|
|
2013-02-01 13:04:52 +00:00
|
|
|
local assert = assert
|
2012-10-07 15:25:52 +00:00
|
|
|
local error = error
|
2012-10-07 15:26:13 +00:00
|
|
|
local pairs = pairs
|
2013-02-01 13:04:52 +00:00
|
|
|
local require = require
|
2012-10-07 15:25:52 +00:00
|
|
|
local setmetatable = setmetatable
|
2012-12-25 16:13:45 +00:00
|
|
|
local tostring = tostring
|
2013-02-01 13:05:20 +00:00
|
|
|
local type = type
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-05-06 19:43:34 +00:00
|
|
|
local decl = assert(decl)
|
2012-10-07 15:26:13 +00:00
|
|
|
local getfenv = getfenv
|
|
|
|
|
2012-12-25 16:13:41 +00:00
|
|
|
decl "void OSD_Printf(const char *fmt, ...);"
|
|
|
|
print = function(str)
|
2012-12-25 16:13:45 +00:00
|
|
|
-- our "print" doesn't use the global "tostring", but the initial one
|
2012-12-25 16:13:41 +00:00
|
|
|
str = tostring(str)
|
|
|
|
if (type(str) ~= "string") then
|
|
|
|
error("invalid argument to print: must be convertible to a string")
|
|
|
|
end
|
|
|
|
ffiC.OSD_Printf("%s\n", str)
|
|
|
|
end
|
|
|
|
|
2012-10-07 15:26:13 +00:00
|
|
|
local print=print
|
2012-10-07 15:25:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
module(...)
|
|
|
|
|
|
|
|
|
2013-04-05 17:53:10 +00:00
|
|
|
local band = bit.band
|
|
|
|
local bor = bit.bor
|
|
|
|
local bnot = bit.bnot
|
|
|
|
local lshift = bit.lshift
|
|
|
|
local rshift = bit.rshift
|
|
|
|
local xor = bit.bxor
|
|
|
|
|
|
|
|
|
|
|
|
--== bitint type factory ==--
|
|
|
|
|
|
|
|
-- Metatable for an integer type that is treated as bitfield. The integer
|
|
|
|
-- itself must be named '_v'.
|
|
|
|
local bitint_mt = {
|
|
|
|
__index = {
|
|
|
|
set = function(self, bits)
|
|
|
|
self._v = bor(self._v, bits)
|
|
|
|
end,
|
|
|
|
|
|
|
|
clear = function(self, bits)
|
|
|
|
self._v = band(self._v, bnot(bits))
|
|
|
|
end,
|
|
|
|
|
|
|
|
flip = function(self, bits)
|
|
|
|
self._v = xor(self._v, bits)
|
|
|
|
end,
|
|
|
|
|
|
|
|
test = function(self, bits)
|
|
|
|
return (band(self._v, bits) ~= 0)
|
|
|
|
end,
|
2013-07-04 19:38:42 +00:00
|
|
|
|
|
|
|
mask = function(self, bits)
|
|
|
|
return band(self._v, bits)
|
|
|
|
end,
|
2013-04-05 17:53:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
__metatable = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
local bitint_to_base_type = {}
|
|
|
|
|
|
|
|
function bitint_new_struct_type(basetypename, newtypename)
|
|
|
|
assert(bitint_to_base_type[newtypename] == nil)
|
|
|
|
assert(type(basetypename)=="string")
|
|
|
|
assert(type(newtypename)=="string")
|
|
|
|
|
|
|
|
local bitint_struct_t = ffi.typeof("struct { $ _v; }", ffi.typeof(basetypename))
|
|
|
|
ffi.metatype(bitint_struct_t, bitint_mt)
|
|
|
|
ffi.cdef("typedef $ $;", bitint_struct_t, newtypename)
|
|
|
|
|
|
|
|
bitint_to_base_type[newtypename] = basetypename
|
|
|
|
end
|
|
|
|
|
|
|
|
function bitint_member(bitint_struct_typename, membname)
|
2013-06-09 16:37:13 +00:00
|
|
|
return string.format("union { %s %s; %s %sbits; };",
|
2013-04-05 17:53:10 +00:00
|
|
|
bitint_to_base_type[bitint_struct_typename], membname,
|
|
|
|
bitint_struct_typename, membname)
|
|
|
|
end
|
|
|
|
|
|
|
|
bitint_new_struct_type("uint8_t", "UBit8")
|
|
|
|
bitint_new_struct_type("uint16_t", "UBit16")
|
|
|
|
|
2013-12-20 18:31:35 +00:00
|
|
|
-- Converts a template struct definition to an internal, unrestricted one.
|
|
|
|
-- NOTE: "[^ ]*" for const decorations in defs.ilua.
|
|
|
|
function strip_const(structstr)
|
|
|
|
return (string.gsub(structstr, "const[^ ]* ", ""));
|
|
|
|
end
|
|
|
|
|
|
|
|
local function maybe_strip_const(str)
|
|
|
|
return _LUNATIC_AUX and strip_const(str) or str
|
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:10 +00:00
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
--== Core engine structs ==--
|
2013-04-05 17:53:10 +00:00
|
|
|
|
2013-06-01 20:09:43 +00:00
|
|
|
local CF_MEMBERS = [[
|
|
|
|
const int16_t ~picnum;
|
|
|
|
int16_t ~heinum;
|
|
|
|
const int16_t ~bunch;
|
|
|
|
]]..bitint_member("UBit16", "~stat")..[[
|
|
|
|
int32_t ~z;
|
|
|
|
int8_t ~shade;
|
|
|
|
uint8_t ~pal, ~xpanning, ~ypanning;
|
|
|
|
]]
|
|
|
|
|
|
|
|
ffi.cdef("typedef struct { "..CF_MEMBERS:gsub("~","").." } ceiling_or_floor_t;")
|
|
|
|
|
2013-12-20 18:31:35 +00:00
|
|
|
local hplane_ptr_ct = ffi.typeof("struct { "..strip_const(CF_MEMBERS:gsub("~","")).." } *")
|
|
|
|
|
2013-02-01 13:05:00 +00:00
|
|
|
local SECTOR_STRUCT = [[
|
|
|
|
struct {
|
|
|
|
const int16_t wallptr, wallnum;
|
2013-06-01 20:09:43 +00:00
|
|
|
]]..
|
|
|
|
string.format([[
|
|
|
|
union {
|
|
|
|
struct { ceiling_or_floor_t ceiling, floor; };
|
|
|
|
struct { %s %s };
|
|
|
|
};
|
|
|
|
]], CF_MEMBERS:gsub("~","ceiling"), CF_MEMBERS:gsub("~","floor"))
|
|
|
|
..[[
|
2014-04-05 11:28:10 +00:00
|
|
|
uint8_t visibility, fogpal;
|
2013-05-06 19:43:28 +00:00
|
|
|
int16_t lotag, hitag; // NOTE: signed for Lunatic
|
|
|
|
int16_t extra;
|
2013-02-01 13:05:00 +00:00
|
|
|
}]]
|
|
|
|
|
2012-11-30 18:57:50 +00:00
|
|
|
local SPRITE_STRUCT = [[
|
2013-02-01 13:05:00 +00:00
|
|
|
struct {
|
2013-07-07 20:59:10 +00:00
|
|
|
// TODO: transparent union with vec3_t pos?
|
2012-11-30 18:57:50 +00:00
|
|
|
int32_t x, y, z;
|
2013-04-05 17:53:10 +00:00
|
|
|
]]..bitint_member("UBit16", "cstat")..[[
|
2012-11-30 18:57:50 +00:00
|
|
|
const int16_t picnum;
|
|
|
|
int8_t shade;
|
2014-02-08 14:37:51 +00:00
|
|
|
uint8_t pal, clipdist, blend;
|
2012-11-30 18:57:50 +00:00
|
|
|
uint8_t xrepeat, yrepeat;
|
|
|
|
int8_t xoffset, yoffset;
|
|
|
|
const int16_t sectnum, statnum;
|
2012-12-03 18:24:25 +00:00
|
|
|
int16_t ang;
|
2013-02-03 12:48:06 +00:00
|
|
|
|
|
|
|
const int16_t owner;
|
|
|
|
int16_t xvel;
|
|
|
|
// NOTE: yvel is often used as player index in game code.
|
|
|
|
const int16_t yvel;
|
|
|
|
int16_t zvel;
|
|
|
|
|
2012-11-30 18:57:50 +00:00
|
|
|
int16_t lotag, hitag, extra;
|
2013-02-01 13:05:00 +00:00
|
|
|
}]]
|
|
|
|
|
|
|
|
local WALL_STRUCT = [[
|
|
|
|
struct {
|
|
|
|
int32_t x, y;
|
|
|
|
const int16_t point2, nextwall, nextsector;
|
2013-04-09 17:35:11 +00:00
|
|
|
const int16_t upwall, dnwall;
|
2013-04-05 17:53:10 +00:00
|
|
|
]]..bitint_member("UBit16", "cstat")..[[
|
2013-02-01 13:05:00 +00:00
|
|
|
const int16_t picnum, overpicnum;
|
|
|
|
int8_t shade;
|
|
|
|
uint8_t pal, xrepeat, yrepeat, xpanning, ypanning;
|
|
|
|
int16_t lotag, hitag, extra;
|
2014-03-10 20:08:27 +00:00
|
|
|
uint8_t blend, _filler;
|
2013-02-01 13:05:00 +00:00
|
|
|
}]]
|
2012-11-30 18:57:50 +00:00
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
-- NOTE for FFI definitions: we're compiling EDuke32 with -funsigned-char, so
|
|
|
|
-- we need to take care to declare chars as unsigned whenever it matters, for
|
|
|
|
-- example if it represents a palette index. (I think it's harmless for stuff
|
|
|
|
-- like passing a function argument, but it should be done there for clarity.)
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2012-11-17 19:46:18 +00:00
|
|
|
-- TODO: provide getters for unsigned {hi,lo}tag?
|
2012-11-30 18:57:50 +00:00
|
|
|
ffi.cdef([[
|
2013-02-01 13:05:00 +00:00
|
|
|
typedef $ sectortype;
|
|
|
|
typedef $ walltype;
|
2013-02-10 16:23:54 +00:00
|
|
|
// NOTE: spritetype and tspritetype are different types with the same data members.
|
2013-02-01 13:05:00 +00:00
|
|
|
typedef $ spritetype;
|
2013-02-10 16:23:54 +00:00
|
|
|
typedef struct { spritetype; } tspritetype;
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-06-12 17:49:55 +00:00
|
|
|
typedef struct {
|
|
|
|
int32_t x, y, z;
|
|
|
|
} vec3_t;
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
typedef struct {
|
|
|
|
const uint32_t mdanimtims;
|
|
|
|
const int16_t mdanimcur;
|
|
|
|
int16_t angoff, pitch, roll;
|
2013-06-12 17:49:55 +00:00
|
|
|
vec3_t mdoff;
|
2013-04-05 17:53:10 +00:00
|
|
|
]]..bitint_member("UBit8", "flags")..[[
|
2012-10-07 15:25:52 +00:00
|
|
|
uint8_t xpanning, ypanning;
|
|
|
|
const uint8_t filler;
|
|
|
|
float alpha;
|
2013-06-30 20:38:54 +00:00
|
|
|
// NOTE: const aggregate fixed with LuaJIT git fe9934feea0a8d580de1
|
|
|
|
// ("FFI: Fix handling of qualified transparent structs/unions.")
|
|
|
|
const union {
|
|
|
|
intptr_t _tspr;
|
|
|
|
struct { int32_t _dummy0, _dummy1; };
|
2013-02-01 13:05:20 +00:00
|
|
|
};
|
2012-10-07 15:25:52 +00:00
|
|
|
} spriteext_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
vec3_t pos;
|
2013-09-22 12:42:06 +00:00
|
|
|
int16_t sprite, wall, sector;
|
2012-10-07 15:25:52 +00:00
|
|
|
} hitdata_t;
|
2013-02-01 13:05:20 +00:00
|
|
|
]],
|
2013-05-06 19:43:34 +00:00
|
|
|
ffi.typeof(maybe_strip_const(SECTOR_STRUCT)),
|
|
|
|
ffi.typeof(maybe_strip_const(WALL_STRUCT)),
|
|
|
|
ffi.typeof(maybe_strip_const(SPRITE_STRUCT)))
|
|
|
|
|
|
|
|
if (not _LUNATIC_AUX) then
|
|
|
|
-- Define the "palette_t" type, which for us has .{r,g,b} fields and a
|
|
|
|
-- bound-checking array of length 3 overlaid.
|
2013-12-31 11:51:55 +00:00
|
|
|
require("bcarray").new("uint8_t", 3, "RGB array", "palette_t",
|
|
|
|
{ "r", "g", "b", "f" })
|
2013-05-06 19:43:34 +00:00
|
|
|
assert(ffi.alignof("palette_t")==1)
|
|
|
|
end
|
2013-02-01 13:04:52 +00:00
|
|
|
|
2013-06-28 14:07:41 +00:00
|
|
|
local vec3_ct = ffi.typeof("vec3_t") -- will be metatype'd in xmath.lua:
|
2013-05-06 19:43:34 +00:00
|
|
|
|
|
|
|
if (not _LUNATIC_AUX) then
|
2013-06-28 14:07:41 +00:00
|
|
|
require("xmath")
|
2013-05-06 19:43:34 +00:00
|
|
|
end
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-09-22 12:42:00 +00:00
|
|
|
-- TODO: 'isceiling' and 'isfloor' methods or similar?
|
2013-05-06 19:43:35 +00:00
|
|
|
local hitdata_ct = ffi.typeof("hitdata_t")
|
|
|
|
|
2013-02-28 17:30:04 +00:00
|
|
|
decl[[
|
|
|
|
const int32_t engine_main_arrays_are_static;
|
|
|
|
const int32_t engine_v8;
|
|
|
|
]]
|
2012-10-07 15:25:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
--== Engine data and functions ==--
|
|
|
|
|
|
|
|
|
|
|
|
-- NOTE TO SELF: This is not C, never EVER write
|
|
|
|
-- if (x)
|
|
|
|
-- when checking a C variable x for 'thuthiness'
|
|
|
|
if (ffiC.engine_main_arrays_are_static ~= 0) then
|
|
|
|
decl[[
|
|
|
|
sectortype sector[];
|
|
|
|
walltype wall[];
|
|
|
|
spritetype sprite[];
|
2013-02-01 13:05:20 +00:00
|
|
|
tspritetype tsprite[];
|
2012-10-07 15:25:52 +00:00
|
|
|
spriteext_t spriteext[];
|
|
|
|
]]
|
|
|
|
else
|
|
|
|
decl[[
|
|
|
|
sectortype *sector;
|
|
|
|
walltype *wall;
|
|
|
|
spritetype *sprite;
|
2013-02-01 13:05:20 +00:00
|
|
|
tspritetype *tsprite;
|
2012-10-07 15:25:52 +00:00
|
|
|
spriteext_t *spriteext;
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ffiC.engine_v8 == 0) then
|
|
|
|
-- V7
|
|
|
|
ffi.cdef[[
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAXSECTORS = 1024,
|
|
|
|
MAXWALLS = 8192,
|
|
|
|
MAXSPRITES = 4096,
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
else
|
|
|
|
-- V8
|
|
|
|
ffi.cdef[[
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAXSECTORS = 4096,
|
|
|
|
MAXWALLS = 16384,
|
|
|
|
MAXSPRITES = 16384,
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
|
|
|
|
ffi.cdef[[
|
|
|
|
enum {
|
|
|
|
MAXSTATUS = 1024,
|
|
|
|
MAXTILES = 30720,
|
2013-02-01 13:05:20 +00:00
|
|
|
MAXSPRITESONSCREEN = 4096,
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-04-12 11:59:32 +00:00
|
|
|
MAXBUNCHES = 512,
|
2013-12-12 19:22:14 +00:00
|
|
|
CEILING = 0, // must be 0
|
|
|
|
FLOOR = 1, // must be 1
|
|
|
|
BOTH_CF = 2,
|
2012-10-07 15:25:52 +00:00
|
|
|
|
|
|
|
CLIPMASK0 = (1<<16)+1, // blocking
|
|
|
|
CLIPMASK1 = (256<<16)+64, // hittable
|
|
|
|
};
|
|
|
|
]]
|
|
|
|
|
2013-05-06 19:43:42 +00:00
|
|
|
ffi.cdef(maybe_strip_const("const int16_t numsectors, numwalls;"))
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
ffi.cdef[[
|
2014-05-18 09:55:42 +00:00
|
|
|
const int32_t Numsprites;
|
2013-01-19 18:28:40 +00:00
|
|
|
const int32_t numyaxbunches; // XXX
|
2012-10-07 15:25:52 +00:00
|
|
|
const int32_t totalclock;
|
2013-01-19 18:28:55 +00:00
|
|
|
int32_t randomseed; // DEPRECATED
|
2012-10-07 15:25:52 +00:00
|
|
|
const int32_t xdim, ydim;
|
2013-01-19 18:28:55 +00:00
|
|
|
const int32_t windowx1, windowy1, windowx2, windowy2;
|
2013-05-06 19:43:42 +00:00
|
|
|
]]
|
2013-05-06 19:43:35 +00:00
|
|
|
|
2013-05-06 19:43:42 +00:00
|
|
|
decl[[
|
2013-05-06 19:43:35 +00:00
|
|
|
int32_t kopen4load(const char *filename, char searchfirst);
|
|
|
|
int32_t kfilelength(int32_t handle);
|
|
|
|
void kclose(int32_t handle);
|
|
|
|
int32_t kread(int32_t handle, void *buffer, int32_t leng);
|
|
|
|
int32_t klseek(int32_t handle, int32_t offset, int32_t whence);
|
2013-05-06 19:43:42 +00:00
|
|
|
|
|
|
|
int32_t sectorofwall_noquick(int16_t theline);
|
2012-10-07 15:25:52 +00:00
|
|
|
]]
|
|
|
|
|
2013-05-06 19:43:42 +00:00
|
|
|
-- Reads the whole file given by the k* file descriptor into a Lua string.
|
|
|
|
-- Always closes the file descriptor.
|
|
|
|
function readintostr(fd, kopen4load_func)
|
2013-05-06 19:43:35 +00:00
|
|
|
-- XXX: this is pretty much the same as the code in L_RunOnce()
|
|
|
|
|
|
|
|
local sz = ffiC.kfilelength(fd)
|
|
|
|
if (sz == 0) then
|
|
|
|
ffiC.kclose(fd)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
if (sz < 0) then
|
|
|
|
ffi.kclose(fd)
|
|
|
|
error("INTERNAL ERROR: kfilelength() returned negative length")
|
|
|
|
end
|
|
|
|
|
|
|
|
local str = ffi.new("char [?]", sz)
|
|
|
|
local readlen = ffiC.kread(fd, str, sz)
|
|
|
|
|
|
|
|
ffiC.kclose(fd); fd=-1
|
|
|
|
|
|
|
|
if (readlen ~= sz) then
|
2013-06-30 20:38:35 +00:00
|
|
|
error("INTERNAL ERROR: couldn't read file wholly")
|
2013-05-06 19:43:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ffi.string(str, sz)
|
|
|
|
end
|
|
|
|
|
2013-05-06 19:43:34 +00:00
|
|
|
if (_LUNATIC_AUX) then
|
2013-05-06 19:43:35 +00:00
|
|
|
-- XXX: The global doesn't show up in 'engine_maptext'.
|
|
|
|
-- I guess I still haven't fully grokked globals in Lua.
|
|
|
|
string.readintostr = readintostr
|
2013-05-06 19:43:34 +00:00
|
|
|
require "engine_maptext"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
ffi.cdef "const int32_t rendmode;"
|
2013-05-06 19:43:34 +00:00
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
decl[[
|
2013-02-28 17:30:04 +00:00
|
|
|
int32_t yxaspect;
|
|
|
|
int32_t viewingrange;
|
2013-02-01 13:05:20 +00:00
|
|
|
int32_t spritesortcnt;
|
2013-02-18 16:07:32 +00:00
|
|
|
int32_t guniqhudid;
|
2012-10-07 15:25:52 +00:00
|
|
|
const int16_t headspritesect[MAXSECTORS+1], headspritestat[MAXSTATUS+1];
|
|
|
|
const int16_t prevspritesect[MAXSPRITES], prevspritestat[MAXSPRITES];
|
|
|
|
const int16_t nextspritesect[MAXSPRITES], nextspritestat[MAXSPRITES];
|
2013-03-15 16:56:11 +00:00
|
|
|
const int16_t tilesizx[MAXTILES], tilesizy[MAXTILES];
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-04-07 15:20:28 +00:00
|
|
|
uint8_t show2dsector[(MAXSECTORS+7)>>3];
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
const int16_t headsectbunch[2][MAXBUNCHES], nextsectbunch[2][MAXSECTORS];
|
|
|
|
|
|
|
|
int16_t yax_getbunch(int16_t i, int16_t cf);
|
|
|
|
|
2012-12-28 17:18:02 +00:00
|
|
|
int32_t getceilzofslopeptr(const sectortype *sec, int32_t dax, int32_t day);
|
|
|
|
int32_t getflorzofslopeptr(const sectortype *sec, int32_t dax, int32_t day);
|
|
|
|
void getzsofslopeptr(const sectortype *sec, int32_t dax, int32_t day,
|
|
|
|
int32_t *ceilz, int32_t *florz);
|
2013-07-04 19:38:42 +00:00
|
|
|
int32_t spriteheightofsptr(const spritetype *spr, int32_t *height, int32_t alsotileyofs);
|
2012-12-28 17:18:02 +00:00
|
|
|
|
2013-02-10 16:24:32 +00:00
|
|
|
int32_t changespritesect(int16_t spritenum, int16_t newsectnum);
|
|
|
|
int32_t changespritestat(int16_t spritenum, int16_t newstatnum);
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
int32_t hitscan(const vec3_t *sv, int16_t sectnum, int32_t vx, int32_t vy, int32_t vz,
|
|
|
|
hitdata_t *hitinfo, uint32_t cliptype);
|
2012-11-30 18:57:46 +00:00
|
|
|
int32_t cansee(int32_t x1, int32_t y1, int32_t z1, int16_t sect1,
|
|
|
|
int32_t x2, int32_t y2, int32_t z2, int16_t sect2);
|
2012-12-03 18:24:30 +00:00
|
|
|
void neartag(int32_t xs, int32_t ys, int32_t zs, int16_t sectnum, int16_t ange, int16_t *neartagsector, int16_t *neartagwall,
|
|
|
|
int16_t *neartagsprite, int32_t *neartaghitdist, int32_t neartagrange, uint8_t tagsearch,
|
|
|
|
int32_t (*blacklist_sprite_func)(int32_t));
|
2013-05-24 13:54:37 +00:00
|
|
|
void dragpoint(int16_t pointhighlight, int32_t dax, int32_t day, uint8_t flags);
|
2013-02-25 15:31:13 +00:00
|
|
|
void getzrange(const vec3_t *pos, int16_t sectnum,
|
|
|
|
int32_t *ceilz, int32_t *ceilhit, int32_t *florz, int32_t *florhit,
|
|
|
|
int32_t walldist, uint32_t cliptype);
|
2013-05-24 13:54:37 +00:00
|
|
|
int32_t clipmovex(vec3_t *pos, int16_t *sectnum, int32_t xvect, int32_t yvect,
|
|
|
|
int32_t walldist, int32_t ceildist, int32_t flordist, uint32_t cliptype,
|
|
|
|
uint8_t noslidep);
|
2012-11-30 18:57:46 +00:00
|
|
|
|
|
|
|
int32_t ldist(const spritetype *s1, const spritetype *s2);
|
|
|
|
int32_t dist(const spritetype *s1, const spritetype *s2);
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2012-12-03 18:24:30 +00:00
|
|
|
int32_t inside(int32_t x, int32_t y, int16_t sectnum);
|
2012-10-07 15:26:29 +00:00
|
|
|
void updatesector(int32_t x, int32_t y, int16_t *sectnum);
|
|
|
|
void updatesectorbreadth(int32_t x, int32_t y, int16_t *sectnum);
|
|
|
|
void updatesectorz(int32_t x, int32_t y, int32_t z, int16_t *sectnum);
|
|
|
|
|
2013-03-25 04:33:03 +00:00
|
|
|
void rotatesprite_(int32_t sx, int32_t sy, int32_t z, int16_t a, int16_t picnum,
|
2014-03-05 21:12:54 +00:00
|
|
|
int8_t dashade, unsigned char dapalnum, int32_t dastat, uint8_t alpha, uint8_t dablend,
|
|
|
|
int32_t cx1, int32_t cy1, int32_t cx2, int32_t cy2);
|
2013-02-18 16:07:42 +00:00
|
|
|
|
|
|
|
void setaspect(int32_t daxrange, int32_t daaspect);
|
2012-10-07 15:25:52 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- misc. functions
|
|
|
|
ffi.cdef[[
|
2013-02-11 17:16:58 +00:00
|
|
|
uint32_t getticks(void);
|
2013-07-07 20:59:05 +00:00
|
|
|
double gethiticks(void);
|
2012-11-30 18:57:46 +00:00
|
|
|
|
|
|
|
int32_t krand(void);
|
|
|
|
int32_t ksqrt(uint32_t num);
|
2012-12-03 18:24:30 +00:00
|
|
|
int32_t __fastcall getangle(int32_t xvect, int32_t yvect);
|
2013-02-16 18:53:15 +00:00
|
|
|
int32_t Mulscale(int32_t a, int32_t b, int32_t sh);
|
2012-10-07 15:25:52 +00:00
|
|
|
]]
|
|
|
|
|
2013-02-01 13:04:56 +00:00
|
|
|
local bcheck = require("bcheck")
|
|
|
|
local check_sector_idx = bcheck.sector_idx
|
2013-05-24 13:54:37 +00:00
|
|
|
local check_wall_idx = bcheck.wall_idx
|
2013-02-01 13:05:20 +00:00
|
|
|
local check_sprite_idx = bcheck.sprite_idx
|
2013-02-01 13:05:00 +00:00
|
|
|
local check_tile_idx = bcheck.tile_idx
|
2013-02-01 13:04:56 +00:00
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
local wallsofsec -- fwd-decl
|
|
|
|
|
2013-02-01 13:05:00 +00:00
|
|
|
local sectortype_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(SECTOR_STRUCT)))
|
|
|
|
|
2013-02-25 15:31:13 +00:00
|
|
|
local function get_sector_idx(sec)
|
|
|
|
local i = ffi.cast(sectortype_ptr_ct, sec)-ffi.cast(sectortype_ptr_ct, ffiC.sector)
|
2013-07-19 12:49:02 +00:00
|
|
|
-- assert(i >= 0 and i < ffiC.numsectors)
|
2013-02-25 15:31:13 +00:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
local zret = ffi.new("int32_t [4]")
|
|
|
|
local zret_t = ffi.typeof[[const struct {
|
|
|
|
struct {
|
|
|
|
bool spritep;
|
|
|
|
int32_t num; // number of sector or sprite
|
|
|
|
int32_t z;
|
|
|
|
} c, f;
|
|
|
|
}]]
|
|
|
|
|
2012-12-28 17:18:02 +00:00
|
|
|
local sectortype_mt = {
|
|
|
|
__index = {
|
2013-02-01 13:05:00 +00:00
|
|
|
--- Setters
|
|
|
|
set_ceilingpicnum = function(s, picnum)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
ffi.cast(sectortype_ptr_ct, s).ceilingpicnum = picnum
|
|
|
|
end,
|
|
|
|
|
|
|
|
set_floorpicnum = function(s, picnum)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
ffi.cast(sectortype_ptr_ct, s).floorpicnum = picnum
|
|
|
|
end,
|
|
|
|
|
|
|
|
--- Other methods
|
2012-12-28 17:18:02 +00:00
|
|
|
ceilingzat = function(s, pos)
|
2013-01-01 15:24:22 +00:00
|
|
|
return ffiC.getceilzofslopeptr(s, pos.x, pos.y)
|
2012-12-28 17:18:02 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
floorzat = function(s, pos)
|
2013-01-01 15:24:22 +00:00
|
|
|
return ffiC.getflorzofslopeptr(s, pos.x, pos.y)
|
2012-12-28 17:18:02 +00:00
|
|
|
end,
|
2013-01-05 13:09:31 +00:00
|
|
|
|
2013-02-25 15:31:13 +00:00
|
|
|
-- getzrange() interface
|
|
|
|
zrangeat = function(s, pos, walldist, cliptype)
|
|
|
|
local sectnum = get_sector_idx(s)
|
|
|
|
local ipos = vec3_ct(pos.x, pos.y, pos.z)
|
|
|
|
walldist = walldist or 128
|
|
|
|
cliptype = cliptype or ffiC.CLIPMASK0
|
|
|
|
|
|
|
|
ffiC.getzrange(ipos, sectnum, zret+0, zret+1, zret+2, zret+3,
|
|
|
|
walldist, cliptype)
|
|
|
|
local ceilz, ceilhit, florz, florhit = zret[0], zret[1], zret[2], zret[3]
|
|
|
|
|
|
|
|
return zret_t({ ceilhit>=49152, bit.band(ceilhit,16383), ceilz },
|
|
|
|
{ florhit>=49152, bit.band(florhit,16383), florz })
|
|
|
|
end,
|
|
|
|
|
2013-06-22 11:31:16 +00:00
|
|
|
-- inside() port, OUTOFSYNC with engine.c
|
2013-01-05 13:09:31 +00:00
|
|
|
contains = function(s, pos)
|
|
|
|
local x, y = pos.x, pos.y
|
|
|
|
local cnt = 0
|
|
|
|
|
|
|
|
for w in wallsofsec(s) do
|
|
|
|
local wal2 = ffiC.wall[ffiC.wall[w].point2]
|
|
|
|
local y1, y2 = ffiC.wall[w].y-y, wal2.y-y
|
|
|
|
|
|
|
|
if (xor(y1, y2) < 0) then
|
|
|
|
local x1, x2 = ffiC.wall[w].x-x, wal2.x-x
|
|
|
|
|
|
|
|
if (xor(x1, x2)>=0) then
|
|
|
|
cnt = xor(cnt, x1)
|
|
|
|
else
|
|
|
|
cnt = xor(cnt, xor(x1*y2-x2*y1, y2))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return (cnt < 0)
|
|
|
|
end,
|
2012-12-28 17:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ffi.metatype("sectortype", sectortype_mt)
|
|
|
|
|
2013-12-20 18:31:35 +00:00
|
|
|
local hplane_mt = {
|
|
|
|
__index = {
|
|
|
|
set_picnum = function(hp, picnum)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
ffi.cast(hplane_ptr_ct, hp).picnum = picnum
|
|
|
|
end
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("ceiling_or_floor_t", hplane_mt)
|
|
|
|
|
2013-02-01 13:05:00 +00:00
|
|
|
local walltype_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(WALL_STRUCT)))
|
|
|
|
|
2012-12-09 13:24:39 +00:00
|
|
|
local walltype_mt = {
|
|
|
|
__index = {
|
2013-02-01 13:05:00 +00:00
|
|
|
--- Setters
|
|
|
|
set_picnum = function(w, picnum)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
ffi.cast(walltype_ptr_ct, w).picnum = picnum
|
|
|
|
end,
|
|
|
|
|
|
|
|
set_overpicnum = function(w, picnum)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
ffi.cast(walltype_ptr_ct, w).overpicnum = picnum
|
|
|
|
end,
|
|
|
|
|
|
|
|
_set_nextwall = function(w, nextwall)
|
2013-03-03 16:06:16 +00:00
|
|
|
-- NOTE: Allow setting a wall to white too, but no checking of the
|
|
|
|
-- consistency invariant ".nextwall>=0 iff .nextsector>=0".
|
|
|
|
if (not (nextwall < 0)) then
|
2013-05-24 13:54:37 +00:00
|
|
|
check_wall_idx(nextwall)
|
2013-03-03 16:06:16 +00:00
|
|
|
end
|
2013-02-01 13:05:00 +00:00
|
|
|
ffi.cast(walltype_ptr_ct, w).nextwall = nextwall
|
|
|
|
end,
|
|
|
|
|
|
|
|
_set_nextsector = function(w, nextsector)
|
2013-03-03 16:06:16 +00:00
|
|
|
if (not (nextsector < 0)) then
|
|
|
|
check_sector_idx(nextsector)
|
|
|
|
end
|
2013-02-01 13:05:00 +00:00
|
|
|
ffi.cast(walltype_ptr_ct, w).nextsector = nextsector
|
|
|
|
end,
|
|
|
|
|
|
|
|
--- Predicates
|
2012-12-09 13:24:39 +00:00
|
|
|
isblocking = function(w)
|
2013-02-10 16:23:54 +00:00
|
|
|
return (band(w.cstat, 1)~=0)
|
2012-12-09 13:24:39 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
ismasked = function(w)
|
2013-02-10 16:23:54 +00:00
|
|
|
return (band(w.cstat, 16)~=0)
|
2012-12-09 13:24:39 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
isoneway = function(w)
|
2013-02-10 16:23:54 +00:00
|
|
|
return (band(w.cstat, 32)~=0)
|
2012-12-09 13:24:39 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
ishittable = function(w)
|
2013-02-10 16:23:54 +00:00
|
|
|
return (band(w.cstat, 64)~=0)
|
2012-12-09 13:24:39 +00:00
|
|
|
end,
|
2013-06-12 17:49:56 +00:00
|
|
|
|
|
|
|
-- Indexing a wall with 'z' gets 0, so that you can e.g. use a wall as
|
|
|
|
-- RHS to vec3_t addition.
|
|
|
|
z = 0,
|
2012-12-09 13:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ffi.metatype("walltype", walltype_mt)
|
|
|
|
|
2013-02-01 13:05:20 +00:00
|
|
|
local spriteext_mt = {
|
|
|
|
__index = {
|
|
|
|
-- Enable EVENT_ANIMATESPRITES for this sprite.
|
2013-07-07 20:59:10 +00:00
|
|
|
-- XXX: unused?
|
2013-02-01 13:05:20 +00:00
|
|
|
make_animated = function(sx)
|
2013-02-10 16:23:54 +00:00
|
|
|
sx.flags = bor(sx.flags, 16)
|
2013-02-01 13:05:20 +00:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("spriteext_t", spriteext_mt)
|
|
|
|
|
2013-02-01 13:05:00 +00:00
|
|
|
local spritetype_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(SPRITE_STRUCT)))
|
2013-02-10 16:23:54 +00:00
|
|
|
-- NOTE: this is the *protected* tspritetype pointer.
|
|
|
|
local tspritetype_ptr_ct = ffi.typeof("$ *", ffi.typeof("tspritetype"))
|
2012-11-30 18:57:50 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
local intarg = ffi.new("int32_t[1]")
|
|
|
|
|
2013-12-28 17:04:39 +00:00
|
|
|
-- XXX: We ought to be using the dynamic value, but users remapping that tile
|
|
|
|
-- are likely somewhat confused anyway. Also, it would be more proper if this
|
|
|
|
-- lived on game-side Lunatic.
|
|
|
|
local APLAYER = 1405
|
|
|
|
|
2013-02-01 13:05:20 +00:00
|
|
|
local spritetype_mt = {
|
2012-12-03 18:24:25 +00:00
|
|
|
__pow = function(s, zofs)
|
2013-03-21 10:20:27 +00:00
|
|
|
return vec3_ct(s.x, s.y, s.z-zofs)
|
2012-12-03 18:24:25 +00:00
|
|
|
end,
|
|
|
|
|
2012-11-30 18:57:50 +00:00
|
|
|
__index = {
|
2013-02-03 12:48:06 +00:00
|
|
|
--- Setters
|
2012-11-30 18:57:50 +00:00
|
|
|
set_picnum = function(s, tilenum)
|
2013-12-28 17:04:39 +00:00
|
|
|
if (s.picnum == APLAYER or tilenum == APLAYER) then
|
|
|
|
error("setting picnum to or on an APLAYER sprite forbidden")
|
|
|
|
end
|
2013-02-01 13:05:00 +00:00
|
|
|
check_tile_idx(tilenum)
|
2012-11-30 18:57:50 +00:00
|
|
|
ffi.cast(spritetype_ptr_ct, s).picnum = tilenum
|
2013-01-01 15:24:22 +00:00
|
|
|
end,
|
|
|
|
|
2013-12-28 17:04:39 +00:00
|
|
|
set_yvel = function(s, yvel)
|
|
|
|
-- XXX: A malicious user might still find some backdoor way to
|
|
|
|
-- inject a bad yvel value, but this together with the above
|
|
|
|
-- set_picnum() check should at least prevent naive attempts.
|
|
|
|
if (s.picnum == APLAYER) then
|
2013-02-10 16:24:32 +00:00
|
|
|
error("setting yvel on an APLAYER sprite forbidden", 2)
|
|
|
|
end
|
2013-01-01 15:24:22 +00:00
|
|
|
ffi.cast(spritetype_ptr_ct, s).yvel = yvel
|
|
|
|
end,
|
2013-02-10 16:23:54 +00:00
|
|
|
|
2013-02-10 16:24:32 +00:00
|
|
|
_set_owner = function(s, owner)
|
2013-03-17 18:11:58 +00:00
|
|
|
-- XXX: AMC TC sets owner to -1 in the cutscene.
|
2013-02-10 16:24:32 +00:00
|
|
|
check_sprite_idx(owner)
|
|
|
|
ffi.cast(spritetype_ptr_ct, s).owner = owner
|
|
|
|
end,
|
2013-07-04 19:38:42 +00:00
|
|
|
|
|
|
|
--- Custom methods ---
|
|
|
|
|
|
|
|
getheightofs = function(s)
|
|
|
|
-- XXX: better reimplement in Lua?
|
|
|
|
local zofs = ffiC.spriteheightofsptr(s, intarg, 0)
|
|
|
|
return intarg[0], zofs
|
|
|
|
end,
|
2012-11-30 18:57:50 +00:00
|
|
|
},
|
|
|
|
}
|
2013-02-01 13:05:20 +00:00
|
|
|
|
|
|
|
local function deep_copy(tab)
|
|
|
|
local ntab = {}
|
|
|
|
for key, val in pairs(tab) do
|
|
|
|
if (type(val)=="table") then
|
|
|
|
ntab[key] = deep_copy(val)
|
|
|
|
else
|
|
|
|
assert(type(val)=="function")
|
|
|
|
ntab[key] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ntab
|
|
|
|
end
|
|
|
|
|
|
|
|
local tspritetype_mt = deep_copy(spritetype_mt)
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- Get the sprite index of a sprite reference.
|
|
|
|
-- This is relatively slow if the code doesn't get compiled, see related
|
|
|
|
-- discussion here:
|
|
|
|
-- http://www.freelists.org/post/luajit/FFI-versus-Lua-C-API-in-purely-interpreted-mode
|
2013-02-18 16:07:42 +00:00
|
|
|
local function get_sprite_idx(spr)
|
|
|
|
local i = ffi.cast(spritetype_ptr_ct, spr)-ffi.cast(spritetype_ptr_ct, ffiC.sprite)
|
2013-07-19 12:49:02 +00:00
|
|
|
-- assert(i >= 0 and i < ffiC.MAXSPRITES)
|
2013-02-18 16:07:42 +00:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
2013-07-07 20:59:10 +00:00
|
|
|
---=== Methods that are specific to sprites ===---
|
|
|
|
|
2013-07-07 20:59:14 +00:00
|
|
|
local l_updatesector -- fwd-decl
|
2013-07-13 21:04:57 +00:00
|
|
|
local l_changesect -- fwd-decl
|
2013-07-07 20:59:14 +00:00
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- The 'setpos' method is available for sprite and tsprite objects.
|
|
|
|
-- spr:setpos(pos [, newsect]),
|
|
|
|
-- where <newsect> is taken to mean "set the sprite's sector number to <newsect>";
|
|
|
|
-- don't run update/search routines or anything like this.
|
|
|
|
function spritetype_mt.__index.setpos(spr, pos, newsect)
|
2013-02-18 16:07:42 +00:00
|
|
|
spr.x, spr.y, spr.z = pos.x, pos.y, pos.z
|
2013-07-13 21:04:57 +00:00
|
|
|
if (newsect ~= nil) then
|
|
|
|
spr:changesect(newsect)
|
|
|
|
end
|
|
|
|
return spr
|
|
|
|
end
|
2013-02-18 16:07:42 +00:00
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- spr:changesect(newsect)
|
|
|
|
-- changes the sector number of <spr> in an 'unforgiving' fashion (<newsect> of
|
|
|
|
-- -1 is error)
|
|
|
|
function spritetype_mt.__index.changesect(spr, newsect)
|
|
|
|
if (newsect ~= spr.sectnum) then
|
|
|
|
l_changesect(get_sprite_idx(spr), newsect)
|
2013-03-15 16:56:03 +00:00
|
|
|
end
|
2013-07-13 21:04:57 +00:00
|
|
|
end
|
2013-07-07 20:59:10 +00:00
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- spr:updatesect(flags)
|
|
|
|
-- updates <spr>'s sectnum; if no matching sector is found, no-op
|
|
|
|
-- returns the updated sector number
|
|
|
|
function spritetype_mt.__index.updatesect(spr, flags)
|
|
|
|
local newsect = l_updatesector(spr, spr.sectnum, flags)
|
|
|
|
if (newsect ~= -1) then
|
|
|
|
spr:changesect(newsect)
|
|
|
|
end
|
2013-07-07 20:59:10 +00:00
|
|
|
return newsect
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
---=== Methods that are specific to tsprites ===---
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- This ought to be called 'set_sectnum', but 'changesect' is for consistency
|
|
|
|
-- with the sprite method.
|
|
|
|
function tspritetype_mt.__index.changesect(tspr, sectnum)
|
|
|
|
if (tspr.sectnum ~= sectnum) then
|
|
|
|
check_sector_idx(sectnum)
|
|
|
|
ffi.cast(spritetype_ptr_ct, tspr).sectnum = sectnum
|
|
|
|
end
|
2013-07-07 20:59:10 +00:00
|
|
|
end
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
function tspritetype_mt.__index.setpos(tspr, pos, newsect)
|
2013-07-07 20:59:10 +00:00
|
|
|
tspr.x, tspr.y, tspr.z = pos.x, pos.y, pos.z
|
2013-07-13 21:04:57 +00:00
|
|
|
if (newsect ~= nil) then
|
2013-07-19 12:49:02 +00:00
|
|
|
tspr:changesect(newsect)
|
2013-07-07 20:59:10 +00:00
|
|
|
end
|
2013-07-13 21:04:57 +00:00
|
|
|
return tspr
|
|
|
|
end
|
2013-07-07 20:59:10 +00:00
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
function tspritetype_mt.__index.updatesect(tspr, flags)
|
|
|
|
local newsect = l_updatesector(tspr, tspr.sectnum, flags)
|
|
|
|
if (newsect ~= -1 and newsect ~= tspr.sectnum) then
|
2013-07-19 12:49:02 +00:00
|
|
|
tspr:changesect(newsect)
|
2013-07-13 21:04:57 +00:00
|
|
|
end
|
2013-02-18 16:07:42 +00:00
|
|
|
return newsect
|
|
|
|
end
|
|
|
|
|
2013-02-01 13:05:20 +00:00
|
|
|
function tspritetype_mt.__index.dup(tspr)
|
|
|
|
if (ffiC.spritesortcnt >= ffiC.MAXSPRITESONSCREEN+0ULL) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
local newtspr = ffiC.tsprite[ffiC.spritesortcnt]
|
2013-02-01 13:05:20 +00:00
|
|
|
ffi.copy(newtspr, tspr, ffi.sizeof(tspr))
|
|
|
|
ffiC.spritesortcnt = ffiC.spritesortcnt+1
|
|
|
|
|
|
|
|
return newtspr
|
|
|
|
end
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
function tspritetype_mt.__index.getspr(tspr)
|
2013-07-07 20:59:10 +00:00
|
|
|
check_sprite_idx(tspr.owner)
|
|
|
|
return ffiC.sprite[tspr.owner]
|
2013-02-10 16:23:54 +00:00
|
|
|
end
|
|
|
|
|
2013-07-07 20:59:10 +00:00
|
|
|
---======---
|
2013-02-10 16:23:54 +00:00
|
|
|
|
2013-02-01 13:05:20 +00:00
|
|
|
-- The user of this module can insert additional "spritetype" index
|
|
|
|
-- methods and register them with "ffi.metatype".
|
|
|
|
function finish_spritetype(mt_index)
|
|
|
|
for name, func in pairs(mt_index) do
|
|
|
|
spritetype_mt.__index[name] = func
|
|
|
|
tspritetype_mt.__index[name] = func
|
|
|
|
end
|
|
|
|
ffi.metatype("spritetype", spritetype_mt)
|
|
|
|
ffi.metatype("tspritetype", tspritetype_mt)
|
|
|
|
end
|
2012-11-30 18:57:50 +00:00
|
|
|
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
---=== Restricted access to C variables from Lunatic ===---
|
|
|
|
|
|
|
|
-- set metatable and forbid setting it further
|
|
|
|
function setmtonce(tab, mt)
|
|
|
|
mt.__metatable = true
|
|
|
|
return setmetatable(tab, mt)
|
|
|
|
end
|
|
|
|
|
|
|
|
---- indirect C array access ----
|
|
|
|
|
2013-02-18 16:07:32 +00:00
|
|
|
-- create a safe indirection for an ffi.C array
|
|
|
|
function creategtab(ctab, maxidx, name)
|
|
|
|
local tab = {}
|
|
|
|
local tmpmt = {
|
|
|
|
__index = function(tab, key)
|
|
|
|
if (key>=0 and key < maxidx) then
|
|
|
|
return ctab[key]
|
|
|
|
end
|
|
|
|
error('out-of-bounds '..name..' read access', 2)
|
|
|
|
end,
|
|
|
|
__newindex = function()
|
|
|
|
error('cannot write directly to '..name, 2)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
return setmtonce(tab, tmpmt)
|
|
|
|
end
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
-- Construct const struct from table
|
2013-02-10 16:24:44 +00:00
|
|
|
function conststruct(tab)
|
2013-08-23 17:01:04 +00:00
|
|
|
local strtab = { "struct {" }
|
2013-02-10 16:23:54 +00:00
|
|
|
|
2013-08-23 17:01:09 +00:00
|
|
|
if (tab[1] ~= nil) then
|
|
|
|
-- e.g. { "KNEE", "PISTOL", ... } provided
|
|
|
|
for i=1,#tab do
|
|
|
|
strtab[#strtab+1] = "static const int "..tab[i].."="..(i-1)..";"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- e.g. { KNEE=0, PISTOL=1, ... } provided
|
|
|
|
for member, val in pairs(tab) do
|
|
|
|
strtab[#strtab+1] = "static const int "..member.."="..val..";"
|
|
|
|
end
|
2013-02-10 16:23:54 +00:00
|
|
|
end
|
|
|
|
strtab[#strtab+1] = "}"
|
|
|
|
|
2013-08-23 17:01:04 +00:00
|
|
|
return ffi.new(table.concat(strtab))
|
2013-02-10 16:23:54 +00:00
|
|
|
end
|
|
|
|
|
2013-12-24 09:44:19 +00:00
|
|
|
do
|
|
|
|
local smt_mt = {
|
|
|
|
__index = function()
|
|
|
|
error("invalid access to static data", 2)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
function static_members_tab()
|
|
|
|
return setmtonce({}, smt_mt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
-- Static, non-instance members. Used to hold constants, for example
|
2013-06-06 11:22:35 +00:00
|
|
|
-- sprite.CSTAT.TRANS1
|
2013-12-24 09:44:19 +00:00
|
|
|
local static_members = {
|
|
|
|
sector = static_members_tab(),
|
|
|
|
wall = static_members_tab(),
|
|
|
|
sprite = static_members_tab(),
|
|
|
|
}
|
2013-02-10 16:23:54 +00:00
|
|
|
|
|
|
|
static_members.sector.STAT = conststruct
|
|
|
|
{
|
2013-04-05 17:53:10 +00:00
|
|
|
PARALLAX = 1,
|
2013-06-06 11:22:35 +00:00
|
|
|
SLOPE = 2,
|
|
|
|
SWAPXY = 4,
|
2013-06-01 20:09:43 +00:00
|
|
|
SMOOSH = 8,
|
2013-06-06 11:22:35 +00:00
|
|
|
FLIPX = 16,
|
|
|
|
FLIPY = 32,
|
2013-04-05 17:53:10 +00:00
|
|
|
RELATIVE = 64,
|
2013-06-06 11:22:35 +00:00
|
|
|
MASK = 128,
|
2013-02-10 16:23:54 +00:00
|
|
|
-- NOTE the reversed order
|
2013-06-06 11:22:35 +00:00
|
|
|
TRANS2 = 128,
|
|
|
|
TRANS1 = 256,
|
|
|
|
BLOCK = 512,
|
2013-12-20 18:31:33 +00:00
|
|
|
HITSCAN = 2048,
|
2013-06-06 11:22:35 +00:00
|
|
|
|
|
|
|
FLIP_BITMASK = 16+32,
|
|
|
|
ORIENT_BITMASK = 4+16+32,
|
|
|
|
TRANS_BITMASK = 128+256,
|
2012-10-07 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 12:52:06 +00:00
|
|
|
static_members.sector.NEARTAG_FLAGS = conststruct
|
|
|
|
{
|
|
|
|
LOTAG = 1,
|
|
|
|
HITAG = 2,
|
|
|
|
NOSPRITES = 4,
|
|
|
|
}
|
|
|
|
|
2013-06-28 14:07:43 +00:00
|
|
|
static_members.sector.UPDATE_FLAGS = conststruct
|
|
|
|
{
|
|
|
|
BREADTH = 1,
|
2013-07-13 21:04:57 +00:00
|
|
|
Z = 2,
|
2013-06-28 14:07:43 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
static_members.wall.CSTAT = conststruct
|
|
|
|
{
|
2013-06-06 11:22:35 +00:00
|
|
|
BLOCK = 1,
|
2013-06-01 20:09:43 +00:00
|
|
|
BOTTOMSWAP = 2,
|
2013-06-06 11:22:35 +00:00
|
|
|
ALIGNBOTTOM = 4,
|
|
|
|
FLIPX = 8,
|
|
|
|
MASK = 16,
|
2013-04-05 17:53:10 +00:00
|
|
|
ONEWAY = 32,
|
|
|
|
HITSCAN = 64,
|
2013-06-06 11:22:35 +00:00
|
|
|
TRANS1 = 128,
|
|
|
|
FLIPY = 256,
|
|
|
|
TRANS2 = 512,
|
|
|
|
|
|
|
|
FLIP_BITMASK = 8+256,
|
|
|
|
TRANS_BITMASK = 128+512,
|
2013-02-10 16:23:54 +00:00
|
|
|
}
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
static_members.sprite.CSTAT = conststruct
|
|
|
|
{
|
2013-06-06 11:22:35 +00:00
|
|
|
BLOCK = 1,
|
|
|
|
TRANS1 = 2,
|
2013-04-05 17:53:10 +00:00
|
|
|
XFLIP = 4,
|
|
|
|
YFLIP = 8,
|
2013-06-06 11:22:35 +00:00
|
|
|
ALIGNWALL = 16,
|
|
|
|
ALIGNFLOOR = 32,
|
|
|
|
ONESIDE = 64,
|
|
|
|
CENTER = 128,
|
2013-04-05 17:53:10 +00:00
|
|
|
HITSCAN = 256,
|
2013-06-06 11:22:35 +00:00
|
|
|
TRANS2 = 512,
|
|
|
|
|
|
|
|
ALIGN_BITMASK = 16+32,
|
|
|
|
TRANS_BITMASK = 2+512,
|
2013-12-20 18:31:33 +00:00
|
|
|
|
|
|
|
INVISIBLE = 32768,
|
2013-02-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 15:20:28 +00:00
|
|
|
local bitar = require("bitar")
|
|
|
|
-- XXX: bitar uses int32_t arrays, while show2dsector[] is a uint8_t
|
|
|
|
-- array. Potential unaligned access. Also, only works on little-endian
|
|
|
|
-- machines. This sucks.
|
|
|
|
static_members.sector.showbitmap = bitar.new(ffiC.MAXSECTORS-1, ffi.cast("int32_t *", ffiC.show2dsector))
|
|
|
|
|
2013-12-20 18:31:33 +00:00
|
|
|
static_members.sector.DAMAGEHPLANE = conststruct
|
|
|
|
{
|
|
|
|
SUPPRESS = -1,
|
|
|
|
DEFAULT = 0,
|
|
|
|
GLASSBREAK = 2^20,
|
|
|
|
}
|
|
|
|
|
|
|
|
function static_members.sector.damagehplane_whatsect(RETURN)
|
|
|
|
local what = (band(RETURN, 65536)~=0) and "ceiling" or "floor"
|
|
|
|
local sectnum = band(RETURN, ffiC.MAXSECTORS-1)
|
|
|
|
return what, sectnum
|
|
|
|
end
|
|
|
|
|
2013-09-05 17:37:42 +00:00
|
|
|
local function iter_allsprites(_, curi)
|
|
|
|
for i=curi+1,ffiC.MAXSPRITES-1 do
|
|
|
|
if (ffiC.sprite[i].statnum ~= ffiC.MAXSTATUS) then
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function static_members.sprite.all()
|
|
|
|
return iter_allsprites, nil, -1
|
|
|
|
end
|
|
|
|
|
2013-02-18 16:07:32 +00:00
|
|
|
local sms = static_members.sprite
|
|
|
|
sms._headspritesect = creategtab(ffiC.headspritesect, ffiC.MAXSECTORS, 'headspritesect[]')
|
|
|
|
-- NOTE: don't allow freelist access
|
|
|
|
sms._headspritestat = creategtab(ffiC.headspritestat, ffiC.MAXSTATUS, 'headspritestat[]')
|
|
|
|
sms._nextspritesect = creategtab(ffiC.nextspritesect, ffiC.MAXSPRITES, 'nextspritesect[]')
|
|
|
|
sms._nextspritestat = creategtab(ffiC.nextspritestat, ffiC.MAXSPRITES, 'nextspritestat[]')
|
|
|
|
sms._prevspritesect = creategtab(ffiC.prevspritesect, ffiC.MAXSPRITES, 'prevspritesect[]')
|
|
|
|
sms._prevspritestat = creategtab(ffiC.prevspritestat, ffiC.MAXSPRITES, 'prevspritestat[]')
|
|
|
|
|
2013-05-24 13:54:37 +00:00
|
|
|
function static_members.wall.dragto(wallnum, pos)
|
|
|
|
check_wall_idx(wallnum)
|
|
|
|
|
|
|
|
-- TODO: some kind of validation of the position?
|
|
|
|
ffiC.dragpoint(wallnum, pos.x, pos.y, 0)
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- Functions changing the sector/status number of a sprite, without asking.
|
|
|
|
|
|
|
|
-- Changes sector number of sprite with index <spritenum> to <sectnum>,
|
|
|
|
-- unconditionally and "unforgiving" (oob <sectnum> gives error).
|
|
|
|
-- <noerr> is for CON compatibility and prevents error on *sprite not in the
|
|
|
|
-- game world* if true.
|
2013-03-24 18:53:48 +00:00
|
|
|
function static_members.sprite.changesect(spritenum, sectnum, noerr)
|
2013-02-10 16:24:32 +00:00
|
|
|
check_sprite_idx(spritenum)
|
|
|
|
check_sector_idx(sectnum)
|
2013-03-24 18:53:48 +00:00
|
|
|
if (ffiC.changespritesect(spritenum, sectnum)==-1 and not noerr) then
|
2013-02-10 16:24:32 +00:00
|
|
|
error("cannot change sector number of sprite not in the game world", 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
l_changesect = static_members.sprite.changesect
|
|
|
|
|
2013-03-24 18:53:48 +00:00
|
|
|
function static_members.sprite.changestat(spritenum, statnum, noerr)
|
2013-06-30 20:38:48 +00:00
|
|
|
-- TODO: see gameexec.c's CON_CHANGESPRITESTAT.
|
2013-02-10 16:24:32 +00:00
|
|
|
check_sprite_idx(spritenum)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (statnum >= 0 and statnum < ffiC.MAXSTATUS)) then
|
2013-02-10 16:24:32 +00:00
|
|
|
error("invalid status number "..statnum, 2)
|
|
|
|
end
|
2013-03-24 18:53:48 +00:00
|
|
|
if (ffiC.changespritestat(spritenum, statnum)==-1 and not noerr) then
|
2013-02-10 16:24:32 +00:00
|
|
|
error("cannot change status number of sprite not in the game world", 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:04:57 +00:00
|
|
|
-- Update a sprite's sector number from its current position and sector number.
|
|
|
|
function static_members.sprite.updatesect(spritenum, flags)
|
|
|
|
check_sprite_idx(spritenum)
|
|
|
|
local spr = ffiC.sprite[spritenum]
|
|
|
|
|
|
|
|
local newsect = l_updatesector(spr, spr.sectnum, flags)
|
|
|
|
if (newsect ~= -1 and newsect ~= spr.sectnum) then
|
|
|
|
l_changesect(spritenum, newsect)
|
|
|
|
end
|
|
|
|
return newsect
|
|
|
|
end
|
|
|
|
|
2013-09-05 17:37:46 +00:00
|
|
|
local strictp = debug_flags.strict
|
|
|
|
|
2013-02-10 16:24:44 +00:00
|
|
|
function GenStructMetatable(Structname, Boundname, StaticMembersTab)
|
|
|
|
StaticMembersTab = StaticMembersTab or static_members[Structname]
|
|
|
|
|
2013-09-05 17:37:46 +00:00
|
|
|
-- If we're running with the 'strict' option, disallow accesses to void
|
|
|
|
-- sprites.
|
|
|
|
local index_func = (strictp and Structname=="sprite") and
|
|
|
|
|
|
|
|
-- Mostly CODEDUP of lower function, ...
|
|
|
|
function(tab, key)
|
2013-02-10 16:23:54 +00:00
|
|
|
if (type(key)=="number") then
|
|
|
|
if (key >= 0 and key < ffiC[Boundname]) then
|
2013-09-05 17:37:46 +00:00
|
|
|
-- ... except this.
|
|
|
|
-- (Inlining into the other function did slow things down.)
|
|
|
|
if (ffiC.sprite[key].statnum == ffiC.MAXSTATUS) then
|
|
|
|
error("attempt to access void sprite with index "..key, 2)
|
|
|
|
end
|
2013-02-10 16:23:54 +00:00
|
|
|
return ffiC[Structname][key]
|
|
|
|
end
|
2013-03-03 16:06:16 +00:00
|
|
|
error("out-of-bounds "..Structname.."[] read access with index "..key, 2)
|
2013-02-10 16:23:54 +00:00
|
|
|
elseif (type(key)=="string") then
|
2013-02-10 16:24:44 +00:00
|
|
|
return StaticMembersTab[key]
|
2013-02-10 16:23:54 +00:00
|
|
|
end
|
2013-09-05 17:37:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
function(tab, key)
|
|
|
|
if (type(key)=="number") then
|
|
|
|
if (key >= 0 and key < ffiC[Boundname]) then
|
|
|
|
return ffiC[Structname][key]
|
|
|
|
end
|
|
|
|
error("out-of-bounds "..Structname.."[] read access with index "..key, 2)
|
|
|
|
elseif (type(key)=="string") then
|
|
|
|
return StaticMembersTab[key]
|
|
|
|
end
|
|
|
|
end
|
2013-02-10 16:23:54 +00:00
|
|
|
|
2013-09-05 17:37:46 +00:00
|
|
|
return {
|
|
|
|
__index = index_func,
|
2013-02-10 16:23:54 +00:00
|
|
|
__newindex = function() error("cannot write directly to "..Structname.."[]", 2) end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local sector_mt = GenStructMetatable("sector", "numsectors")
|
|
|
|
local wall_mt = GenStructMetatable("wall", "numwalls")
|
|
|
|
local sprite_mt = GenStructMetatable("sprite", "MAXSPRITES")
|
|
|
|
|
2013-02-01 13:05:20 +00:00
|
|
|
local atsprite_mt = {
|
|
|
|
__index = function(tab, idx)
|
|
|
|
check_sprite_idx(idx)
|
|
|
|
|
2013-02-10 16:23:54 +00:00
|
|
|
local tspr = ffi.cast(tspritetype_ptr_ct, ffiC.spriteext[idx]._tspr)
|
2013-02-01 13:05:20 +00:00
|
|
|
if (tspr == nil) then
|
|
|
|
error("tsprite of actor "..idx.." unavailable", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return a reference to a tsprite[] element.
|
|
|
|
return tspr[0]
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function() error('cannot write directly to atsprite[]', 2) end,
|
2012-10-07 15:25:52 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 15:26:13 +00:00
|
|
|
|
|
|
|
local vars_to_ignore = {}
|
|
|
|
for varname,_ in pairs(getfenv(1)) do
|
2013-01-19 18:28:40 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
print("IGNORE "..varname)
|
|
|
|
end
|
2012-10-07 15:26:13 +00:00
|
|
|
vars_to_ignore[varname] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
--== ALL GLOBALS FROM HERE ON ARE EXPORTED UPWARDS (see create_globals() below) ==--
|
|
|
|
|
|
|
|
sector = setmtonce({}, sector_mt)
|
2013-07-07 20:59:14 +00:00
|
|
|
local sector = sector
|
2012-10-07 15:26:13 +00:00
|
|
|
wall = setmtonce({}, wall_mt)
|
2013-02-10 16:23:54 +00:00
|
|
|
sprite = setmtonce({}, sprite_mt)
|
2013-02-01 13:05:20 +00:00
|
|
|
spriteext = creategtab(ffiC.spriteext, ffiC.MAXSPRITES, 'spriteext[]')
|
2013-07-07 20:59:10 +00:00
|
|
|
_atsprite = setmtonce({}, atsprite_mt)
|
2012-10-07 15:26:13 +00:00
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
local function iter_wallsofsec(endwall, w)
|
|
|
|
w = w+1
|
|
|
|
if (w < endwall) then
|
|
|
|
return w
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
wallsofsec = function(sec) -- local
|
|
|
|
return iter_wallsofsec, sec.wallptr+sec.wallnum, sec.wallptr-1
|
|
|
|
end
|
|
|
|
|
|
|
|
function wallsofsect(sect)
|
|
|
|
check_sector_idx(sect)
|
|
|
|
return iter_wallsofsec, sector[sect].wallptr+sector[sect].wallnum, sector[sect].wallptr-1
|
|
|
|
end
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
--== Per-sector/per-statnum sprite iterators ==--
|
|
|
|
local function iter_spritesofsect(sect, i)
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headspritesect[sect]
|
|
|
|
else
|
|
|
|
i = ffiC.nextspritesect[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then return i end
|
|
|
|
end
|
|
|
|
|
2013-03-15 16:56:03 +00:00
|
|
|
-- sprites of sectnum iterator that allows deleting the iterated sprite
|
|
|
|
local function iter_spritesofsect_safe(tab, i)
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headspritesect[-i]
|
|
|
|
else
|
|
|
|
i = tab[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then
|
|
|
|
tab[1] = ffiC.nextspritesect[i]
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function spritesofsect(sect, maydelete)
|
2012-12-03 18:24:30 +00:00
|
|
|
check_sector_idx(sect)
|
2013-03-15 16:56:03 +00:00
|
|
|
|
|
|
|
if (maydelete) then
|
|
|
|
return iter_spritesofsect_safe, { -1 }, -sect
|
|
|
|
else
|
|
|
|
return iter_spritesofsect, sect, -1
|
|
|
|
end
|
2012-10-07 15:25:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function iter_spritesofstat(stat, i)
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headspritestat[stat]
|
|
|
|
else
|
|
|
|
i = ffiC.nextspritestat[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then return i end
|
|
|
|
end
|
|
|
|
|
2013-03-15 16:56:03 +00:00
|
|
|
-- sprites of statnum iterator that allows deleting the iterated sprite
|
|
|
|
local function iter_spritesofstat_safe(tab, i)
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headspritestat[-i]
|
|
|
|
else
|
|
|
|
i = tab[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then
|
|
|
|
tab[1] = ffiC.nextspritestat[i]
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function spritesofstat(stat, maydelete)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (stat >= 0 and stat < ffiC.MAXSTATUS)) then
|
2012-10-07 15:25:52 +00:00
|
|
|
error("passed invalid statnum to spritesofstat iterator", 2)
|
|
|
|
end
|
|
|
|
|
2013-03-15 16:56:03 +00:00
|
|
|
if (maydelete) then
|
|
|
|
return iter_spritesofstat_safe, { -1 }, -stat
|
|
|
|
else
|
|
|
|
return iter_spritesofstat, stat, -1
|
|
|
|
end
|
2012-10-07 15:25:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--== TROR iterators ==--
|
|
|
|
local function iter_sectorsofbunch(cf, i)
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headsectbunch[cf][-i-1];
|
|
|
|
else
|
|
|
|
i = ffiC.nextsectbunch[cf][i];
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then return i end
|
|
|
|
end
|
|
|
|
|
2013-12-12 19:22:14 +00:00
|
|
|
local function iter_sectorsofbunch_both(cftab, i)
|
|
|
|
local cf = cftab[1]
|
|
|
|
|
|
|
|
if (i < 0) then
|
|
|
|
i = ffiC.headsectbunch[cf][-i-1];
|
|
|
|
else
|
|
|
|
i = ffiC.nextsectbunch[cf][i];
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i < 0 and cf==0) then
|
|
|
|
cftab[1] = 1
|
|
|
|
i = ffiC.headsectbunch[1][cftab[2]]
|
|
|
|
assert(i >= 0, "TROR bunch lists corrupt")
|
|
|
|
end
|
|
|
|
|
|
|
|
if (i >= 0) then
|
|
|
|
return i, cftab[1]==0 and "ceiling" or "floor"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
function sectorsofbunch(bunchnum, cf)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (bunchnum >= 0 and bunchnum < ffiC.numyaxbunches)) then
|
2012-10-07 15:25:52 +00:00
|
|
|
error("passed invalid bunchnum to sectorsofbunch iterator", 2)
|
|
|
|
end
|
|
|
|
|
2013-12-12 19:22:14 +00:00
|
|
|
if (cf == ffiC.BOTH_CF) then
|
|
|
|
return iter_sectorsofbunch_both, { 0, bunchnum }, -bunchnum-1
|
|
|
|
else
|
|
|
|
if (not (cf == 0 or cf == 1)) then
|
|
|
|
error("passed invalid 'cf' to sectorsofbunch iterator, must be 0 or 1", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
return iter_sectorsofbunch, cf, -bunchnum-1
|
|
|
|
end
|
2012-10-07 15:25:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
---=== Engine functions, wrapped for Lua convenience ===---
|
2012-12-03 18:24:30 +00:00
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
-- returns a hitdata_ct
|
2013-07-07 20:59:10 +00:00
|
|
|
-- TODO: make cliptype optional? What should be the default?
|
2013-09-13 20:23:54 +00:00
|
|
|
function hitscan(pos, sectnum, ray, cliptype)
|
2012-12-03 18:24:30 +00:00
|
|
|
check_sector_idx(sectnum)
|
2012-10-07 15:26:24 +00:00
|
|
|
local vec = vec3_ct(pos.x, pos.y, pos.z)
|
2012-10-07 15:25:52 +00:00
|
|
|
local hitdata = hitdata_ct()
|
|
|
|
|
2013-09-13 20:23:54 +00:00
|
|
|
ffiC.hitscan(vec, sectnum, ray.x, ray.y, ray.z, hitdata, cliptype)
|
2012-10-07 15:25:52 +00:00
|
|
|
return hitdata
|
|
|
|
end
|
2012-10-07 15:26:13 +00:00
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
function cansee(pos1,sect1, pos2,sect2)
|
2013-02-16 18:53:15 +00:00
|
|
|
check_sector_idx(sect1)
|
|
|
|
check_sector_idx(sect2)
|
2012-11-30 18:57:46 +00:00
|
|
|
|
|
|
|
local ret = ffiC.cansee(pos1.x,pos1.y,pos1.z, sect1,
|
|
|
|
pos2.x,pos2.y,pos2.z, sect2)
|
2012-11-30 18:57:55 +00:00
|
|
|
return (ret~=0)
|
2012-11-30 18:57:46 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:03 +00:00
|
|
|
local neartag_ret_ct = ffi.typeof[[
|
|
|
|
const struct {
|
2012-12-03 18:24:30 +00:00
|
|
|
int32_t sector, wall, sprite;
|
|
|
|
int32_t dist;
|
2013-04-05 17:53:03 +00:00
|
|
|
}
|
2012-12-03 18:24:30 +00:00
|
|
|
]]
|
|
|
|
|
2013-02-16 18:53:24 +00:00
|
|
|
local function newar() return ffi.new("int16_t [1]") end
|
2013-06-15 12:52:06 +00:00
|
|
|
-- NOTE: <tagsearch> flags are in sector.NEARTAG_FLAGS
|
2012-12-03 18:24:30 +00:00
|
|
|
function neartag(pos, sectnum, ang, range, tagsearch)
|
|
|
|
check_sector_idx(sectnum)
|
|
|
|
local a, b, c, d = newar(), newar(), newar(), ffi.new("int32_t [1]")
|
|
|
|
ffiC.neartag(pos.x, pos.y, pos.z, sectnum, ang, a, b, c, d, range, tagsearch, nil)
|
|
|
|
return neartag_ret_ct(a[0], b[0], c[0], d[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
function inside(pos, sectnum)
|
|
|
|
check_sector_idx(sectnum)
|
|
|
|
return (ffiC.inside(pos.x, pos.y, sectnum)==1)
|
|
|
|
end
|
|
|
|
|
2013-03-13 10:48:23 +00:00
|
|
|
local us_retsect = ffi.new("int16_t [1]")
|
2013-06-28 14:07:43 +00:00
|
|
|
local USF = sector.UPDATE_FLAGS
|
2013-03-13 10:48:23 +00:00
|
|
|
|
2013-06-28 14:07:43 +00:00
|
|
|
function updatesector(pos, sectnum, flags)
|
|
|
|
if (sectnum ~= -1) then
|
|
|
|
check_sector_idx(sectnum)
|
|
|
|
end
|
2012-10-07 15:26:29 +00:00
|
|
|
|
2013-03-13 10:48:23 +00:00
|
|
|
us_retsect[0] = sectnum
|
2013-06-28 14:07:43 +00:00
|
|
|
|
|
|
|
if (flags==nil or flags==0) then
|
|
|
|
ffiC.updatesector(pos.x, pos.y, us_retsect)
|
|
|
|
elseif (flags==USF.BREADTH) then
|
|
|
|
ffiC.updatesectorbreadth(pos.x, pos.y, us_retsect)
|
2013-07-13 21:04:57 +00:00
|
|
|
elseif (flags==USF.Z) then
|
|
|
|
-- Same as updatesectorz, but useful if we are called from
|
|
|
|
-- e.g. sprite.updatesect().
|
|
|
|
ffiC.updatesectorz(pos.x, pos.y, pos.z, us_retsect)
|
2013-06-28 14:07:43 +00:00
|
|
|
else
|
|
|
|
error("invalid argument #3 (flags)", 2)
|
|
|
|
end
|
|
|
|
|
2013-03-13 10:48:23 +00:00
|
|
|
return us_retsect[0]
|
2012-10-07 15:26:29 +00:00
|
|
|
end
|
|
|
|
|
2013-07-07 20:59:14 +00:00
|
|
|
l_updatesector = updatesector
|
|
|
|
|
2013-06-28 14:07:43 +00:00
|
|
|
function updatesectorz(pos, sectnum, flags)
|
|
|
|
if (sectnum ~= -1) then
|
|
|
|
check_sector_idx(sectnum)
|
|
|
|
end
|
|
|
|
if (flags ~= nil) then
|
|
|
|
error("invalid argument #3 (flags)", 2)
|
|
|
|
end
|
|
|
|
|
2013-03-13 10:48:23 +00:00
|
|
|
us_retsect[0] = sectnum
|
|
|
|
ffiC.updatesectorz(pos.x, pos.y, pos.z, us_retsect)
|
|
|
|
return us_retsect[0]
|
2012-10-07 15:26:29 +00:00
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
function printf(fmt, ...)
|
|
|
|
print(string.format(fmt, ...))
|
|
|
|
end
|
|
|
|
|
2012-10-07 15:26:13 +00:00
|
|
|
|
|
|
|
-- This is supposed to be run from the file that 'require's this module to take
|
2013-01-05 13:09:31 +00:00
|
|
|
-- over the non-local variables from here into its global environment.
|
2012-10-07 15:26:13 +00:00
|
|
|
function create_globals(_G_their)
|
|
|
|
local _G_our = getfenv(1)
|
|
|
|
vars_to_ignore["create_globals"] = true
|
|
|
|
|
|
|
|
for varname,obj in pairs(_G_our) do
|
|
|
|
if (not vars_to_ignore[varname]) then
|
2013-01-19 18:28:40 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
print("EXPORT "..varname)
|
|
|
|
end
|
2012-10-07 15:26:13 +00:00
|
|
|
_G_their[varname] = obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|