2012-04-22 19:10:29 +00:00
|
|
|
-- INTERNAL
|
2011-09-20 19:12:24 +00:00
|
|
|
-- definitions of BUILD and game types for the Lunatic Interpreter
|
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
local require = require
|
2011-09-20 19:12:24 +00:00
|
|
|
local ffi = require("ffi")
|
2012-08-06 20:00:23 +00:00
|
|
|
local ffiC = ffi.C
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2013-05-28 19:52:48 +00:00
|
|
|
-- Lua C API functions.
|
|
|
|
local CF = CF
|
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
local bit = bit
|
2013-06-20 18:31:47 +00:00
|
|
|
local coroutine = coroutine
|
2012-10-07 15:25:52 +00:00
|
|
|
local string = string
|
|
|
|
local table = table
|
2012-12-03 18:24:25 +00:00
|
|
|
local math = math
|
2012-12-09 13:24:28 +00:00
|
|
|
|
|
|
|
local assert = assert
|
|
|
|
local error = error
|
2013-04-05 17:53:06 +00:00
|
|
|
local getfenv = getfenv
|
2013-03-24 18:53:28 +00:00
|
|
|
local getmetatable = getmetatable
|
2012-12-09 13:24:28 +00:00
|
|
|
local ipairs = ipairs
|
|
|
|
local loadstring = loadstring
|
|
|
|
local pairs = pairs
|
|
|
|
local rawget = rawget
|
|
|
|
local rawset = rawset
|
2013-03-24 18:53:28 +00:00
|
|
|
local select = select
|
2012-12-09 13:24:28 +00:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local setfenv = setfenv
|
|
|
|
local tonumber = tonumber
|
|
|
|
local tostring = tostring
|
2012-12-25 16:13:41 +00:00
|
|
|
local type = type
|
2012-12-09 13:24:28 +00:00
|
|
|
|
2013-06-30 20:38:35 +00:00
|
|
|
-- Create a new module for passing stuff to other modules.
|
|
|
|
local lprivate = {}
|
|
|
|
require("package").loaded.lprivate = lprivate
|
|
|
|
|
2013-04-05 17:53:03 +00:00
|
|
|
require("jit.opt").start("maxmcode=10240") -- in KiB
|
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
-- The "gv" global will provide access to C global *scalars* and safe functions.
|
2013-01-19 18:28:40 +00:00
|
|
|
-- NOTE: This exposes C library functions from e.g. the global C namespace, but
|
|
|
|
-- without their declarations, they will be sitting there like a stone.
|
|
|
|
local gv_ = {}
|
2013-01-19 18:28:55 +00:00
|
|
|
-- [key]=<boolean> forbids, [key]=<non-boolean (e.g. table, function)> overrides
|
2013-01-19 18:28:40 +00:00
|
|
|
local gv_access = {}
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2012-07-20 21:57:44 +00:00
|
|
|
-- This is for declarations of arrays or pointers which should not be
|
2012-10-07 15:26:13 +00:00
|
|
|
-- accessible through the "gv" global. The "defs_common" module will
|
2012-10-07 15:25:52 +00:00
|
|
|
-- use this function.
|
2012-10-07 15:26:13 +00:00
|
|
|
--
|
2013-02-28 17:30:04 +00:00
|
|
|
-- Notes: do not declare multiple scalars on one line (this is bad:
|
|
|
|
-- "int32_t a, b"). Do not name array arguments (or add a space
|
2012-11-25 13:18:52 +00:00
|
|
|
-- between the identifier and the '[' instead).
|
2013-02-21 18:53:57 +00:00
|
|
|
function decl(str, ...)
|
2012-11-25 13:18:52 +00:00
|
|
|
-- NOTE that the regexp also catches non-array/non-function identifiers
|
|
|
|
-- like "user_defs ud;"
|
2012-06-26 19:49:59 +00:00
|
|
|
for varname in string.gmatch(str, "([%a_][%w_]*)[[(;]") do
|
2013-01-19 18:28:40 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
print("FORBID "..varname)
|
|
|
|
end
|
|
|
|
gv_access[varname] = true
|
2012-06-26 19:49:59 +00:00
|
|
|
end
|
|
|
|
|
2013-02-21 18:53:57 +00:00
|
|
|
ffi.cdef(str, ...)
|
2012-06-26 19:49:59 +00:00
|
|
|
end
|
|
|
|
|
2013-11-11 16:48:23 +00:00
|
|
|
lprivate.decl = decl
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
-- Load the definitions common to the game's and editor's Lua interface.
|
2012-10-07 15:25:52 +00:00
|
|
|
local defs_c = require("defs_common")
|
2012-12-09 13:24:28 +00:00
|
|
|
local cansee = defs_c.cansee
|
2012-11-30 18:57:50 +00:00
|
|
|
local strip_const = defs_c.strip_const
|
2012-12-03 18:24:25 +00:00
|
|
|
local setmtonce = defs_c.setmtonce
|
|
|
|
|
2012-12-25 16:13:41 +00:00
|
|
|
-- Must be after loading "defs_common" which redefines "print" to use
|
|
|
|
-- OSD_Printf()
|
2013-04-05 17:53:06 +00:00
|
|
|
local print, printf = print, defs_c.printf
|
2012-12-25 16:13:41 +00:00
|
|
|
|
2012-06-22 21:40:01 +00:00
|
|
|
|
2012-10-07 15:25:52 +00:00
|
|
|
---=== EDuke32 game definitions ===---
|
2012-02-04 21:34:39 +00:00
|
|
|
|
2013-03-15 16:56:19 +00:00
|
|
|
local INV_NAMES = {
|
|
|
|
"STEROIDS",
|
|
|
|
"SHIELD",
|
|
|
|
"SCUBA",
|
|
|
|
"HOLODUKE",
|
|
|
|
"JETPACK",
|
|
|
|
"DUMMY1",
|
|
|
|
"ACCESS",
|
|
|
|
"HEATS",
|
|
|
|
"DUMMY2",
|
|
|
|
"FIRSTAID",
|
|
|
|
"BOOTS",
|
|
|
|
}
|
|
|
|
|
|
|
|
local WEAPON_NAMES = {
|
|
|
|
"KNEE",
|
|
|
|
"PISTOL",
|
|
|
|
"SHOTGUN",
|
|
|
|
"CHAINGUN",
|
|
|
|
"RPG",
|
|
|
|
"HANDBOMB",
|
|
|
|
"SHRINKER",
|
|
|
|
"DEVISTATOR",
|
|
|
|
"TRIPBOMB",
|
|
|
|
"FREEZE",
|
|
|
|
"HANDREMOTE",
|
|
|
|
"GROW",
|
|
|
|
}
|
|
|
|
|
2012-02-04 21:34:39 +00:00
|
|
|
---- game structs ----
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2013-08-23 17:01:09 +00:00
|
|
|
lprivate.GET = defs_c.conststruct(INV_NAMES)
|
|
|
|
lprivate.WEAPON = defs_c.conststruct(WEAPON_NAMES)
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2013-08-23 17:01:09 +00:00
|
|
|
ffi.cdef([[
|
2012-06-26 19:49:59 +00:00
|
|
|
enum {
|
2013-08-23 17:01:09 +00:00
|
|
|
GET_MAX = 11,
|
|
|
|
MAX_WEAPONS = 12,
|
2012-06-26 19:49:59 +00:00
|
|
|
MAXPLAYERS = 16,
|
2013-07-04 19:38:42 +00:00
|
|
|
GTICSPERSEC = 30, // The real number of movement updates per second
|
2012-06-26 19:49:59 +00:00
|
|
|
};
|
2013-03-15 16:56:19 +00:00
|
|
|
]])
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
ffi.cdef[[
|
2012-08-06 20:00:29 +00:00
|
|
|
struct action {
|
|
|
|
int16_t startframe, numframes;
|
|
|
|
int16_t viewtype, incval, delay;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct move {
|
|
|
|
int16_t hvel, vvel;
|
|
|
|
};
|
|
|
|
|
2013-02-07 21:01:03 +00:00
|
|
|
#pragma pack(push,1)
|
2012-08-06 20:00:29 +00:00
|
|
|
typedef struct { int32_t id; struct move mv; } con_move_t;
|
|
|
|
typedef struct { int32_t id; struct action ac; } con_action_t;
|
2013-02-07 21:01:03 +00:00
|
|
|
#pragma pack(pop)
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2012-08-08 19:32:35 +00:00
|
|
|
typedef struct {
|
|
|
|
int32_t id;
|
|
|
|
con_action_t act;
|
|
|
|
con_move_t mov;
|
|
|
|
int32_t movflags;
|
|
|
|
} con_ai_t;
|
2012-11-25 13:18:52 +00:00
|
|
|
]]
|
2012-08-08 19:32:35 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
defs_c.bitint_new_struct_type("int16_t", "SBit16")
|
2013-04-05 17:53:10 +00:00
|
|
|
defs_c.bitint_new_struct_type("int32_t", "SBit32")
|
2013-07-04 19:38:42 +00:00
|
|
|
defs_c.bitint_new_struct_type("uint32_t", "UBit32")
|
2013-04-05 17:53:10 +00:00
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
-- Struct template for actor_t. It already has 'const' fields (TODO: might need
|
|
|
|
-- to make more 'const'), but still has array members exposed, so is unsuited
|
|
|
|
-- for external exposure.
|
|
|
|
local ACTOR_STRUCT = [[
|
2013-07-19 12:49:08 +00:00
|
|
|
struct {
|
2013-06-09 16:37:13 +00:00
|
|
|
const int32_t t_data[10];
|
2012-08-08 19:32:35 +00:00
|
|
|
const struct move mv;
|
|
|
|
const struct action ac;
|
2013-06-30 20:38:52 +00:00
|
|
|
const uint16_t actiontics;
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2013-04-05 17:53:10 +00:00
|
|
|
]]..defs_c.bitint_member("SBit32", "flags")..[[
|
2012-12-23 14:00:08 +00:00
|
|
|
vec3_t bpos; //12b
|
2012-12-09 13:24:46 +00:00
|
|
|
int32_t floorz,ceilingz,lastvx,lastvy; //16b
|
|
|
|
int32_t lasttransport; //4b
|
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
const int16_t picnum;
|
|
|
|
int16_t ang, extra;
|
|
|
|
const int16_t owner;
|
2013-07-04 19:38:42 +00:00
|
|
|
// NOTE: not to be confused with .movflags:
|
|
|
|
]]..defs_c.bitint_member("SBit16", "_movflag")..[[
|
|
|
|
int16_t tempang, timetosleep;
|
2011-12-21 18:43:08 +00:00
|
|
|
|
2013-06-12 17:49:55 +00:00
|
|
|
int16_t stayputsect;
|
2012-12-03 18:24:25 +00:00
|
|
|
const int16_t dispicnum;
|
2013-06-30 20:38:52 +00:00
|
|
|
// Movement flags, sprite[i].hitag in C-CON.
|
|
|
|
// XXX: more research where it was used in EDuke32's C code? (also .lotag <-> actiontics)
|
|
|
|
// XXX: what if CON code knew of the above implementation detail?
|
|
|
|
]]..defs_c.bitint_member("UBit16", "movflags")..[[
|
|
|
|
int16_t cgg;
|
2013-03-15 16:55:59 +00:00
|
|
|
|
|
|
|
const int16_t lightId, lightcount, lightmaxrange;
|
2013-07-04 19:38:53 +00:00
|
|
|
// NOTE: on 32-bit, C's lightptr+filler <=> this dummy:
|
2013-03-15 16:55:59 +00:00
|
|
|
const union { intptr_t ptr; uint64_t dummy; } _light;
|
2012-11-25 13:18:52 +00:00
|
|
|
}
|
2012-06-26 19:49:59 +00:00
|
|
|
]]
|
|
|
|
|
2013-01-26 17:07:44 +00:00
|
|
|
local bcarray = require("bcarray")
|
|
|
|
|
2013-02-01 13:04:56 +00:00
|
|
|
local bcheck = require("bcheck")
|
|
|
|
local check_sector_idx, check_tile_idx = bcheck.sector_idx, bcheck.tile_idx
|
2013-02-25 15:31:19 +00:00
|
|
|
local check_sprite_idx = bcheck.sprite_idx
|
2013-02-01 13:04:56 +00:00
|
|
|
local check_weapon_idx, check_inventory_idx = bcheck.weapon_idx, bcheck.inventory_idx
|
|
|
|
local check_sound_idx = bcheck.sound_idx
|
2013-06-13 17:11:09 +00:00
|
|
|
local check_number = bcheck.number
|
2013-07-13 21:04:59 +00:00
|
|
|
local check_type = bcheck.type
|
2013-02-01 13:04:56 +00:00
|
|
|
|
2013-03-15 16:56:19 +00:00
|
|
|
bcarray.new("int16_t", 64, "loogie", "int16_x_64") -- TODO: randomize member names
|
|
|
|
bcarray.new("int16_t", ffiC.MAX_WEAPONS, "weapon", "int16_x_MAX_WEAPONS", WEAPON_NAMES)
|
|
|
|
bcarray.new("int16_t", ffiC.GET_MAX, "inventory", "int16_x_GET_MAX", INV_NAMES)
|
2013-01-26 17:07:44 +00:00
|
|
|
|
2013-02-28 17:30:04 +00:00
|
|
|
-- NOTE: writing e.g. "ps.jetpack_on" in Lua when "ps.jetpack_on~=0" was meant
|
|
|
|
-- is probably one of the most commonly committed errors, so we make it a bool
|
|
|
|
-- type instead of uint8_t. The only issue is that if CON coders used these
|
|
|
|
-- fields to store more than just one bit, we're in trouble.
|
|
|
|
-- This will need to be documented and frozen for release.
|
2012-11-25 13:18:52 +00:00
|
|
|
local DUKEPLAYER_STRUCT = [[
|
2013-02-03 12:48:25 +00:00
|
|
|
__attribute__((packed)) struct {
|
2012-06-26 19:49:59 +00:00
|
|
|
vec3_t pos, opos, vel, npos;
|
|
|
|
int32_t bobposx, bobposy;
|
|
|
|
int32_t truefz, truecz, player_par;
|
|
|
|
int32_t randomflamex, exitx, exity;
|
|
|
|
int32_t runspeed, max_player_health, max_shield_amount;
|
2012-11-03 19:32:43 +00:00
|
|
|
int32_t autostep, autostep_sbw;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
|
|
|
uint32_t interface_toggle_flag;
|
|
|
|
|
2013-01-19 18:28:55 +00:00
|
|
|
int32_t pipebombControl, pipebombLifetime, pipebombLifetimeVar;
|
|
|
|
int32_t tripbombControl, tripbombLifetime, tripbombLifetimeVar;
|
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
int32_t zrange;
|
|
|
|
int16_t angrange, autoaimang;
|
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
uint16_t max_actors_killed, actors_killed;
|
|
|
|
uint16_t gotweapon, zoom;
|
2012-11-25 13:18:52 +00:00
|
|
|
|
2013-01-26 17:07:44 +00:00
|
|
|
int16_x_64 loogiex;
|
|
|
|
int16_x_64 loogiey;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t sbs, sound_pitch;
|
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
int16_t ang, oang, angvel;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<S> int16_t cursectnum;
|
2012-12-03 18:24:25 +00:00
|
|
|
int16_t look_ang, last_extra, subweapon;
|
2013-01-26 17:07:44 +00:00
|
|
|
int16_x_MAX_WEAPONS max_ammo_amount;
|
|
|
|
int16_x_MAX_WEAPONS ammo_amount;
|
|
|
|
int16_x_GET_MAX inv_amount;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<I-> int16_t wackedbyactor;
|
2013-04-07 15:20:37 +00:00
|
|
|
int16_t pyoff, opyoff;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
|
|
|
int16_t horiz, horizoff, ohoriz, ohorizoff;
|
2012-12-03 18:24:25 +00:00
|
|
|
const int16_t newowner;
|
|
|
|
int16_t jumping_counter, airleft;
|
2013-02-18 16:07:32 +00:00
|
|
|
int16_t fta;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<Q> int16_t ftq;
|
|
|
|
const int16_t access_wallnum, access_spritenum;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t got_access, weapon_ang, visibility;
|
2012-11-30 18:57:55 +00:00
|
|
|
int16_t somethingonplayer, on_crane;
|
|
|
|
const int16_t i;
|
2012-12-03 18:24:25 +00:00
|
|
|
const int16_t one_parallax_sectnum;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t random_club_frame, one_eighty_count;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<I-> int16_t dummyplayersprite;
|
2012-12-03 18:24:30 +00:00
|
|
|
int16_t extra_extra8;
|
|
|
|
int16_t actorsqu, timebeforeexit;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<X-> int16_t customexitsound;
|
2012-12-03 18:24:30 +00:00
|
|
|
int16_t last_pissed_time;
|
2012-11-25 13:18:52 +00:00
|
|
|
|
2013-01-26 17:07:44 +00:00
|
|
|
int16_x_MAX_WEAPONS weaprecs;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t weapon_sway, crack_time, bobcounter;
|
|
|
|
|
|
|
|
int16_t orotscrnang, rotscrnang, dead_flag; // JBF 20031220: added orotscrnang
|
|
|
|
int16_t holoduke_on, pycount;
|
2012-08-02 10:52:17 +00:00
|
|
|
int16_t transporter_hold;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
|
|
|
uint8_t max_secret_rooms, secret_rooms;
|
|
|
|
uint8_t frag, fraggedself, quick_kick, last_quick_kick;
|
2013-02-28 17:30:04 +00:00
|
|
|
uint8_t return_to_center;
|
|
|
|
bool reloading;
|
2013-02-01 13:04:52 +00:00
|
|
|
const uint8_t weapreccnt;
|
2012-06-26 19:49:59 +00:00
|
|
|
uint8_t aim_mode, auto_aim, weaponswitch, movement_lock, team;
|
2013-04-07 15:20:37 +00:00
|
|
|
uint8_t tipincs, hbomb_hold_delay;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<P> uint8_t frag_ps;
|
2013-04-07 15:20:37 +00:00
|
|
|
uint8_t kickback_pic;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2013-02-28 17:30:04 +00:00
|
|
|
uint8_t gm;
|
|
|
|
bool on_warping_sector;
|
|
|
|
uint8_t footprintcount, hurt_delay;
|
|
|
|
bool hbomb_on, jumping_toggle, rapid_fire_hold, on_ground;
|
2013-02-24 16:05:22 +00:00
|
|
|
// NOTE: there's array indexing with inven_icon, but always after a
|
|
|
|
// bound check:
|
2013-02-28 17:30:04 +00:00
|
|
|
uint8_t inven_icon, buttonpalette;
|
|
|
|
bool over_shoulder_on;
|
|
|
|
uint8_t show_empty_weapon;
|
|
|
|
|
|
|
|
bool jetpack_on, spritebridge;
|
|
|
|
uint8_t lastrandomspot; // unused
|
|
|
|
bool scuba_on;
|
|
|
|
uint8_t footprintpal;
|
|
|
|
bool heat_on;
|
|
|
|
uint8_t invdisptime;
|
|
|
|
|
|
|
|
bool holster_weapon;
|
|
|
|
uint8_t falling_counter, footprintshade;
|
2013-04-07 15:20:37 +00:00
|
|
|
uint8_t refresh_inventory;
|
2013-05-26 18:42:53 +00:00
|
|
|
const<W> uint8_t last_full_weapon;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2013-11-28 21:18:02 +00:00
|
|
|
const uint8_t toggle_key_flag;
|
2013-02-28 17:30:04 +00:00
|
|
|
uint8_t knuckle_incs, knee_incs, access_incs;
|
2012-06-26 19:49:59 +00:00
|
|
|
uint8_t walking_snd_toggle, palookup, hard_landing, fist_incs;
|
|
|
|
|
2013-11-28 21:18:02 +00:00
|
|
|
int8_t numloogs, loogcnt;
|
|
|
|
const int8_t scream_voice;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<W-> int8_t last_weapon;
|
2013-11-28 21:18:02 +00:00
|
|
|
const int8_t cheat_phase;
|
|
|
|
int8_t weapon_pos;
|
2013-04-07 15:20:41 +00:00
|
|
|
const<W-> int8_t wantweaponfire;
|
|
|
|
const<W> int8_t curr_weapon;
|
2012-06-26 19:49:59 +00:00
|
|
|
|
2013-01-02 22:33:22 +00:00
|
|
|
uint8_t palette;
|
2013-02-01 13:04:52 +00:00
|
|
|
palette_t _pals;
|
2013-06-20 18:31:50 +00:00
|
|
|
int8_t _palsfadespeed, _palsfadenext, _palsfadeprio, _padding2;
|
2012-11-25 13:18:52 +00:00
|
|
|
|
2013-01-19 18:28:43 +00:00
|
|
|
// NOTE: In C, the struct type has no name. We only have it here to define
|
|
|
|
// a metatype later.
|
|
|
|
const weaponaccess_t weapon;
|
|
|
|
const int8_t _padding;
|
2012-11-25 13:18:52 +00:00
|
|
|
}
|
2012-06-26 19:49:59 +00:00
|
|
|
]]
|
2012-08-02 10:52:17 +00:00
|
|
|
|
2013-02-10 16:23:59 +00:00
|
|
|
local PROJECTILE_STRUCT = [[
|
|
|
|
struct {
|
|
|
|
int32_t workslike, cstat;
|
|
|
|
int32_t hitradius, range, flashcolor;
|
|
|
|
const int16_t spawns;
|
2013-06-09 16:37:16 +00:00
|
|
|
const int16_t sound, isound;
|
|
|
|
int16_t vel;
|
2013-02-10 16:23:59 +00:00
|
|
|
const int16_t decal, trail;
|
|
|
|
int16_t tnum, drop;
|
2013-06-09 16:37:16 +00:00
|
|
|
int16_t offset, bounces;
|
|
|
|
const int16_t bsound;
|
2013-02-10 16:23:59 +00:00
|
|
|
int16_t toffset;
|
|
|
|
int16_t extra, extra_rand;
|
|
|
|
int8_t sxrepeat, syrepeat, txrepeat, tyrepeat;
|
|
|
|
int8_t shade, xrepeat, yrepeat, pal;
|
|
|
|
int8_t movecnt;
|
|
|
|
uint8_t clipdist;
|
2013-06-09 16:37:18 +00:00
|
|
|
int8_t filler[2];
|
|
|
|
int32_t userdata;
|
2013-02-10 16:23:59 +00:00
|
|
|
}
|
|
|
|
]]
|
|
|
|
|
2013-03-18 22:05:26 +00:00
|
|
|
-- KEEPINSYNC weapondata_mt below.
|
|
|
|
local WEAPONDATA_STRUCT = "struct {"..table.concat(con_lang.wdata_members, ';').."; }"
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
local randgen = require("randgen")
|
|
|
|
|
|
|
|
local ma_rand = randgen.new(true) -- initialize to "random" (time-based) seed
|
|
|
|
local ma_count = nil
|
|
|
|
|
|
|
|
local function ma_replace_array(typestr, neltstr)
|
|
|
|
local nelts = tonumber(neltstr)
|
|
|
|
if (nelts==nil) then
|
|
|
|
nelts = ffiC[neltstr]
|
|
|
|
assert(type(nelts)=="number")
|
|
|
|
end
|
|
|
|
|
|
|
|
local strtab = { "const ", typestr.." " }
|
|
|
|
for i=1,nelts do
|
|
|
|
local ch1 = 97 + (ma_rand:getu32() % 25) -- 'a'..'z'
|
2013-01-26 17:07:44 +00:00
|
|
|
strtab[i+2] = string.format("_%c%x%s", ch1, ma_count, (i<nelts) and "," or ";")
|
2012-11-25 13:18:52 +00:00
|
|
|
ma_count = ma_count+1
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat(strtab)
|
|
|
|
end
|
|
|
|
|
2013-04-07 15:20:41 +00:00
|
|
|
---=== Protection of scalars in (currently only) DukePlayer_t struct. ===---
|
|
|
|
-- This is more convenient than writing dozens of set-member methods.
|
|
|
|
local prot_scalar_chkfunc = {
|
|
|
|
S = check_sector_idx,
|
|
|
|
I = check_sprite_idx,
|
|
|
|
|
|
|
|
P = bcheck.player_idx,
|
|
|
|
W = check_weapon_idx,
|
|
|
|
X = check_sound_idx,
|
|
|
|
Q = bcheck.quote_idx,
|
|
|
|
}
|
|
|
|
|
|
|
|
local DukePlayer_prot_allowneg = {} -- [<member name>] = true if setting <0 allowed
|
|
|
|
local DukePlayer_prot_chkfunc = {} -- [<member name>] = <checking function>
|
|
|
|
|
|
|
|
local function ma_replace_scalar(what, typestr, membname)
|
|
|
|
DukePlayer_prot_chkfunc[membname] = assert(prot_scalar_chkfunc[what:sub(1,1)])
|
|
|
|
DukePlayer_prot_allowneg[membname] = (what:sub(2)=="-")
|
|
|
|
return ma_replace_array(typestr, 1)
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
-- Converts a template struct definition to an external one, in which arrays
|
|
|
|
-- have been substituted by randomly named scalar fields.
|
2013-04-07 15:20:41 +00:00
|
|
|
-- <also_scalars>: also handle protected scalars like "const<W-> ..." etc.
|
|
|
|
local function mangle_arrays(structstr, also_scalars)
|
2012-11-25 13:18:52 +00:00
|
|
|
ma_count = 0
|
|
|
|
-- NOTE: regexp only works for non-nested arrays and for one array per line.
|
2013-04-07 15:20:41 +00:00
|
|
|
structstr = structstr:gsub("const%s+([%w_]+)[^\n]+%[([%w_]+)%];", ma_replace_array)
|
|
|
|
|
|
|
|
if (also_scalars) then
|
|
|
|
-- One protected scalar per line, too.
|
|
|
|
structstr = structstr:gsub("const<(.-)>%s+([%w_]-)%s+([%w_]-);", ma_replace_scalar)
|
|
|
|
end
|
|
|
|
|
|
|
|
return structstr
|
2012-11-25 13:18:52 +00:00
|
|
|
end
|
|
|
|
|
2013-04-07 15:20:41 +00:00
|
|
|
--print(mangle_arrays(DUKEPLAYER_STRUCT, true))
|
2012-11-25 13:18:52 +00:00
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
--- default defines etc.
|
|
|
|
local con_lang = require("con_lang")
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
ffi.cdef([[
|
2013-01-19 18:28:43 +00:00
|
|
|
typedef struct { int32_t _p; } weaponaccess_t;
|
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
typedef struct {
|
2013-07-04 19:38:42 +00:00
|
|
|
]]..defs_c.bitint_member("UBit32", "bits")..[[
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t fvel, svel; // 4b
|
|
|
|
int8_t avel, horz; // 2b
|
|
|
|
int8_t extbits, filler; // 2b
|
|
|
|
} input_t;
|
|
|
|
|
2013-02-03 12:48:25 +00:00
|
|
|
typedef
|
|
|
|
]].. mangle_arrays(ACTOR_STRUCT) ..[[
|
|
|
|
actor_t;
|
|
|
|
|
|
|
|
typedef
|
2013-04-07 15:20:41 +00:00
|
|
|
]].. mangle_arrays(DUKEPLAYER_STRUCT, true) ..[[
|
2013-02-03 12:48:25 +00:00
|
|
|
DukePlayer_t;
|
|
|
|
|
2013-02-07 21:01:03 +00:00
|
|
|
typedef __attribute__((packed)) struct {
|
2012-06-26 19:49:59 +00:00
|
|
|
DukePlayer_t *ps;
|
|
|
|
input_t *sync;
|
|
|
|
|
|
|
|
int32_t netsynctime;
|
|
|
|
int16_t ping, filler;
|
|
|
|
int32_t pcolor, pteam;
|
|
|
|
uint8_t frags[MAXPLAYERS], wchoice[MAX_WEAPONS];
|
|
|
|
|
2012-11-03 19:32:43 +00:00
|
|
|
char vote, gotvote, pingcnt, playerquitflag, ready;
|
2012-06-26 19:49:59 +00:00
|
|
|
char user_name[32];
|
|
|
|
uint32_t revision;
|
|
|
|
} playerdata_t;
|
2012-12-09 13:24:39 +00:00
|
|
|
|
2013-01-19 18:28:48 +00:00
|
|
|
typedef struct {
|
|
|
|
int32_t cur, count;
|
|
|
|
int32_t gunposx, lookhalfang;
|
|
|
|
int32_t gunposy, lookhoriz;
|
|
|
|
int32_t shade;
|
|
|
|
} hudweapon_t;
|
|
|
|
|
2013-03-21 10:20:20 +00:00
|
|
|
typedef
|
|
|
|
]].. WEAPONDATA_STRUCT ..[[
|
|
|
|
weapondata_t;
|
|
|
|
|
2013-02-10 16:23:59 +00:00
|
|
|
typedef
|
|
|
|
]].. PROJECTILE_STRUCT ..[[
|
|
|
|
projectile_t;
|
2012-12-09 13:24:39 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2013-07-09 18:23:41 +00:00
|
|
|
uint32_t _flags; // XXX: do we want to have this accessible at game time?
|
2013-03-15 16:56:11 +00:00
|
|
|
const int32_t _cacherange;
|
|
|
|
const projectile_t _defproj;
|
2012-12-09 13:24:39 +00:00
|
|
|
} tiledata_t;
|
2012-01-28 14:36:34 +00:00
|
|
|
|
2013-01-19 18:28:32 +00:00
|
|
|
typedef struct {
|
|
|
|
vec3_t pos;
|
|
|
|
int32_t dist, clock;
|
|
|
|
int16_t ang, horiz;
|
2013-06-15 12:52:06 +00:00
|
|
|
int16_t sect; // NOTE: protected in camera_mt's __newindex
|
2013-01-19 18:28:32 +00:00
|
|
|
} camera_t;
|
|
|
|
|
2012-02-08 11:00:48 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAXMOUSEBUTTONS = 10,
|
|
|
|
MAXMOUSEAXES = 2,
|
|
|
|
MAXJOYBUTTONS = (32+4),
|
|
|
|
MAXJOYAXES = 8,
|
|
|
|
NUMGAMEFUNCTIONS = 56,
|
|
|
|
};
|
2012-01-28 14:38:41 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int32_t const_visibility,uw_framerate;
|
|
|
|
int32_t camera_time,folfvel,folavel,folx,foly,fola;
|
|
|
|
int32_t reccnt,crosshairscale;
|
|
|
|
|
|
|
|
int32_t runkey_mode,statusbarscale,mouseaiming,weaponswitch,drawweapon; // JBF 20031125
|
|
|
|
int32_t democams,color,msgdisptime,statusbarmode;
|
|
|
|
int32_t m_noexits,noexits,autovote,automsg,idplayers;
|
|
|
|
int32_t team, viewbob, weaponsway, althud, weaponscale, textscale;
|
|
|
|
|
|
|
|
int32_t entered_name,screen_tilting,shadows,fta_on,executions,auto_run;
|
|
|
|
int32_t coords,tickrate,levelstats,m_coop,coop,screen_size,lockout,crosshair;
|
|
|
|
int32_t playerai,angleinterpolation,obituaries;
|
|
|
|
|
|
|
|
int32_t respawn_monsters,respawn_items,respawn_inventory,recstat,monsters_off,brightness;
|
|
|
|
int32_t m_respawn_items,m_respawn_monsters,m_respawn_inventory,m_recstat,m_monsters_off,detail;
|
|
|
|
int32_t m_ffire,ffire,m_player_skill,m_level_number,m_volume_number,multimode;
|
|
|
|
int32_t player_skill,level_number,volume_number,m_marker,marker,mouseflip;
|
|
|
|
|
|
|
|
int32_t configversion;
|
|
|
|
|
|
|
|
int16_t pause_on,from_bonus;
|
|
|
|
int16_t camerasprite,last_camsprite;
|
|
|
|
int16_t last_level,secretlevel, bgstretch;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int32_t UseJoystick;
|
|
|
|
int32_t UseMouse;
|
|
|
|
int32_t AutoAim;
|
|
|
|
int32_t ShowOpponentWeapons;
|
|
|
|
int32_t MouseDeadZone,MouseBias;
|
|
|
|
int32_t SmoothInput;
|
|
|
|
|
|
|
|
// JBF 20031211: Store the input settings because
|
|
|
|
// (currently) jmact can't regurgitate them
|
2012-02-08 11:00:48 +00:00
|
|
|
int32_t MouseFunctions[MAXMOUSEBUTTONS][2];
|
|
|
|
int32_t MouseDigitalFunctions[MAXMOUSEAXES][2];
|
|
|
|
int32_t MouseAnalogueAxes[MAXMOUSEAXES];
|
|
|
|
int32_t MouseAnalogueScale[MAXMOUSEAXES];
|
|
|
|
int32_t JoystickFunctions[MAXJOYBUTTONS][2];
|
|
|
|
int32_t JoystickDigitalFunctions[MAXJOYAXES][2];
|
|
|
|
int32_t JoystickAnalogueAxes[MAXJOYAXES];
|
|
|
|
int32_t JoystickAnalogueScale[MAXJOYAXES];
|
|
|
|
int32_t JoystickAnalogueDead[MAXJOYAXES];
|
|
|
|
int32_t JoystickAnalogueSaturate[MAXJOYAXES];
|
|
|
|
uint8_t KeyboardKeys[NUMGAMEFUNCTIONS][2];
|
2012-01-28 14:38:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Sound variables
|
|
|
|
//
|
|
|
|
int32_t FXDevice;
|
|
|
|
int32_t MusicDevice;
|
2013-08-11 15:28:48 +00:00
|
|
|
int32_t MasterVolume;
|
2012-01-28 14:38:41 +00:00
|
|
|
int32_t FXVolume;
|
|
|
|
int32_t MusicVolume;
|
|
|
|
int32_t SoundToggle;
|
|
|
|
int32_t MusicToggle;
|
|
|
|
int32_t VoiceToggle;
|
|
|
|
int32_t AmbienceToggle;
|
|
|
|
|
|
|
|
int32_t NumVoices;
|
|
|
|
int32_t NumChannels;
|
|
|
|
int32_t NumBits;
|
|
|
|
int32_t MixRate;
|
|
|
|
|
|
|
|
int32_t ReverseStereo;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Screen variables
|
|
|
|
//
|
|
|
|
|
|
|
|
int32_t ScreenMode;
|
|
|
|
|
|
|
|
int32_t ScreenWidth;
|
|
|
|
int32_t ScreenHeight;
|
|
|
|
int32_t ScreenBPP;
|
|
|
|
|
|
|
|
int32_t ForceSetup;
|
|
|
|
int32_t NoAutoLoad;
|
|
|
|
|
2013-02-24 16:05:22 +00:00
|
|
|
const int32_t scripthandle;
|
2012-01-28 14:38:41 +00:00
|
|
|
int32_t setupread;
|
|
|
|
|
|
|
|
int32_t CheckForUpdates;
|
|
|
|
int32_t LastUpdateCheck;
|
|
|
|
int32_t useprecache;
|
|
|
|
} config;
|
|
|
|
|
|
|
|
char overhead_on,last_overhead,showweapons;
|
|
|
|
char god,warp_on,cashman,eog,showallmap;
|
2012-03-11 17:38:50 +00:00
|
|
|
char show_help,scrollmode,noclip;
|
2012-01-28 14:38:41 +00:00
|
|
|
char ridecule[10][40];
|
|
|
|
char savegame[10][22];
|
|
|
|
char pwlockout[128],rtsname[128];
|
|
|
|
char display_bonus_screen;
|
|
|
|
char show_level_text;
|
|
|
|
} user_defs;
|
2013-02-21 18:53:57 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int32_t partime, designertime;
|
|
|
|
char *name, *filename, *musicfn, *alt_musicfn;
|
|
|
|
void *savedstate;
|
|
|
|
} map_t;
|
2012-06-26 19:49:59 +00:00
|
|
|
]])
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2013-07-04 19:38:50 +00:00
|
|
|
bcarray.new("weapondata_t", ffiC.MAX_WEAPONS, "weapon", "weapondata_x_MAX_WEAPONS", WEAPON_NAMES)
|
2013-10-20 12:35:06 +00:00
|
|
|
bcarray.new("int32_t", con_lang.MAXSESSIONVARS, "sessionvar", "int32_x_MAXSESSIONVARS")
|
2013-07-04 19:38:50 +00:00
|
|
|
|
2013-01-19 18:28:55 +00:00
|
|
|
-- EXTERNALLY EXPOSED GAME VARIABLES
|
|
|
|
ffi.cdef[[
|
|
|
|
const int32_t screenpeek;
|
|
|
|
hudweapon_t hudweap;
|
2013-03-13 10:48:14 +00:00
|
|
|
int32_t g_logoFlags;
|
2013-01-19 18:28:55 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- INTERNAL VARIABLES/FUNCTIONS
|
2013-02-24 16:05:22 +00:00
|
|
|
decl("map_t MapInfo[$*$];", con_lang.MAXVOLUMES+1, con_lang.MAXLEVELS)
|
2013-06-12 17:49:51 +00:00
|
|
|
decl("char EpisodeNames[$][33];", con_lang.MAXVOLUMES)
|
2013-02-21 18:53:57 +00:00
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
decl[[
|
2013-08-23 17:01:10 +00:00
|
|
|
int32_t g_RETURN;
|
2013-05-19 19:29:13 +00:00
|
|
|
int32_t g_elCONSize;
|
|
|
|
char *g_elCON;
|
|
|
|
void El_SetCON(const char *conluacode);
|
2013-10-27 21:12:20 +00:00
|
|
|
void El_OnError(const char *str);
|
2013-05-19 19:29:13 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
char *g_elSavecode;
|
|
|
|
void El_FreeSaveCode(void);
|
2013-10-27 21:12:22 +00:00
|
|
|
const char *(*El_SerializeGamevars)(int32_t *slenptr, int32_t levelnum);
|
|
|
|
int32_t (*El_RestoreGamevars)(const char *savecode);
|
2013-05-20 19:31:42 +00:00
|
|
|
|
2013-02-21 18:53:57 +00:00
|
|
|
const char *s_buildRev;
|
2012-06-26 19:49:59 +00:00
|
|
|
const char *g_sizes_of_what[];
|
|
|
|
int32_t g_sizes_of[];
|
2012-12-25 16:13:45 +00:00
|
|
|
int32_t g_elCallDepth;
|
2013-05-28 19:52:48 +00:00
|
|
|
int32_t block_deletesprite;
|
2013-05-19 19:29:16 +00:00
|
|
|
const char **g_elModules;
|
2013-02-24 16:05:22 +00:00
|
|
|
char g_modDir[];
|
2013-10-20 12:35:06 +00:00
|
|
|
int32_x_MAXSESSIONVARS g_elSessionVar;
|
2012-02-08 11:00:48 +00:00
|
|
|
actor_t actor[MAXSPRITES];
|
2013-01-19 18:28:40 +00:00
|
|
|
camera_t g_camera;
|
2012-01-28 14:38:41 +00:00
|
|
|
user_defs ud;
|
2012-06-26 19:49:59 +00:00
|
|
|
playerdata_t g_player[MAXPLAYERS];
|
2013-02-10 16:24:44 +00:00
|
|
|
DukePlayer_t *g_player_ps[MAXPLAYERS];
|
2013-07-04 19:38:50 +00:00
|
|
|
weapondata_x_MAX_WEAPONS g_playerWeapon[MAXPLAYERS];
|
2013-06-02 14:07:56 +00:00
|
|
|
weapondata_t g_weaponOverridden[MAX_WEAPONS];
|
2013-07-13 21:04:38 +00:00
|
|
|
int16_t WeaponPickupSprites[MAX_WEAPONS];
|
2012-12-29 15:21:32 +00:00
|
|
|
tiledata_t g_tile[MAXTILES];
|
2013-02-07 21:01:03 +00:00
|
|
|
projectile_t ProjectileData[MAXTILES];
|
|
|
|
projectile_t SpriteProjectile[MAXSPRITES];
|
2012-08-19 12:52:18 +00:00
|
|
|
|
2013-05-30 18:10:59 +00:00
|
|
|
int32_t g_noResetVars;
|
|
|
|
void (*A_ResetVars)(int32_t iActor);
|
|
|
|
|
2013-06-02 14:07:52 +00:00
|
|
|
// Used from lunacon.lua for dynamic {tile,sound} remapping:
|
2013-03-17 18:11:38 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
int32_t *dynvalptr;
|
|
|
|
const int16_t staticval;
|
2013-06-02 14:07:52 +00:00
|
|
|
} g_dynTileList[], g_dynSoundList[];
|
2013-03-17 18:11:38 +00:00
|
|
|
|
2013-01-01 15:24:28 +00:00
|
|
|
char *ScriptQuotes[];
|
|
|
|
|
2012-08-19 12:52:18 +00:00
|
|
|
const int32_t playerswhenstarted;
|
2012-12-28 17:18:02 +00:00
|
|
|
int16_t g_spriteDeleteQueueSize;
|
|
|
|
int16_t BlimpSpawnSprites[15];
|
2012-12-29 15:21:24 +00:00
|
|
|
int32_t g_scriptVersion;
|
2013-01-19 18:28:55 +00:00
|
|
|
const int32_t g_currentFrameRate;
|
2013-01-24 20:03:02 +00:00
|
|
|
const int32_t g_currentMenu;
|
2013-02-11 17:16:58 +00:00
|
|
|
uint16_t g_earthquakeTime;
|
2013-07-04 19:38:42 +00:00
|
|
|
uint32_t g_moveThingsCount;
|
2013-02-24 16:05:22 +00:00
|
|
|
char CheatKeys[2];
|
2012-11-30 18:57:46 +00:00
|
|
|
|
2013-02-24 16:05:47 +00:00
|
|
|
// Must not have functions here that may call events directly or
|
|
|
|
// indirectly. See lunatic_game.c.
|
2013-02-24 16:05:42 +00:00
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
int32_t A_IncurDamage(int32_t sn); // not bound-checked!
|
2013-02-16 18:53:04 +00:00
|
|
|
int32_t G_CheckActivatorMotion(int32_t lotag);
|
2012-12-03 18:24:30 +00:00
|
|
|
int32_t A_Dodge(spritetype *s);
|
2013-02-16 18:53:24 +00:00
|
|
|
int32_t A_MoveSprite(int32_t spritenum, const vec3_t *change, uint32_t cliptype);
|
2013-01-01 15:24:28 +00:00
|
|
|
void P_DoQuote(int32_t q, DukePlayer_t *p);
|
2013-02-25 15:31:09 +00:00
|
|
|
void P_SetGamePalette(DukePlayer_t *player, uint8_t palid, int32_t set);
|
2013-02-18 16:07:51 +00:00
|
|
|
void G_AddUserQuote(const char *daquote);
|
2013-01-01 15:24:28 +00:00
|
|
|
void G_ClearCameraView(DukePlayer_t *ps);
|
2013-02-11 17:16:58 +00:00
|
|
|
void G_DrawTileGeneric(int32_t x, int32_t y, int32_t zoom, int32_t tilenum,
|
|
|
|
int32_t shade, int32_t orientation, int32_t p);
|
2013-02-16 18:53:04 +00:00
|
|
|
void G_InitTimer(int32_t ticspersec);
|
|
|
|
void G_GetTimeDate(int32_t *vals);
|
|
|
|
int32_t G_ToggleWallInterpolation(int32_t w, int32_t doset);
|
2013-02-16 18:53:24 +00:00
|
|
|
int32_t G_StartTrack(int32_t level);
|
2013-04-05 17:52:59 +00:00
|
|
|
int32_t VM_CheckSquished2(int32_t i, int32_t snum);
|
2013-04-09 17:34:53 +00:00
|
|
|
void M_ChangeMenu(int32_t cm);
|
2013-01-02 22:33:37 +00:00
|
|
|
|
2013-02-18 16:07:51 +00:00
|
|
|
const char *KB_ScanCodeToString(uint8_t scancode);
|
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
int32_t A_CheckAnySoundPlaying(int32_t i);
|
|
|
|
int32_t S_CheckSoundPlaying(int32_t i, int32_t num);
|
2013-01-05 13:09:19 +00:00
|
|
|
void S_StopEnvSound(int32_t num, int32_t i);
|
2013-01-24 20:03:02 +00:00
|
|
|
int32_t FX_StopAllSounds(void);
|
|
|
|
void S_ChangeSoundPitch(int32_t num, int32_t i, int32_t pitchoffset);
|
2013-02-18 16:07:42 +00:00
|
|
|
|
|
|
|
int32_t minitext_(int32_t x,int32_t y,const char *t,int32_t s,int32_t p,int32_t sb);
|
|
|
|
void G_DrawTXDigiNumZ(int32_t starttile, int32_t x,int32_t y,int32_t n,int32_t s,int32_t pal,
|
|
|
|
int32_t cs,int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t z);
|
|
|
|
int32_t G_PrintGameText(int32_t f, int32_t tile, int32_t x, int32_t y, const char *t,
|
|
|
|
int32_t s, int32_t p, int32_t o,
|
|
|
|
int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t z);
|
2013-06-01 20:09:45 +00:00
|
|
|
typedef struct {
|
|
|
|
int32_t x, y;
|
|
|
|
} vec2_t;
|
|
|
|
vec2_t G_ScreenText(const int32_t font,
|
|
|
|
int32_t x, int32_t y, const int32_t z, const int32_t blockangle, const int32_t charangle,
|
|
|
|
const char *str, const int32_t shade, int32_t pal, int32_t o, const int32_t alpha,
|
|
|
|
int32_t xspace, int32_t yline, int32_t xbetween, int32_t ybetween, const int32_t f,
|
|
|
|
const int32_t x1, const int32_t y1, const int32_t x2, const int32_t y2);
|
|
|
|
vec2_t G_ScreenTextSize(const int32_t font,
|
|
|
|
int32_t x, int32_t y, const int32_t z, const int32_t blockangle,
|
|
|
|
const char *str, const int32_t o,
|
|
|
|
int32_t xspace, int32_t yline, int32_t xbetween, int32_t ybetween,
|
|
|
|
const int32_t f,
|
|
|
|
int32_t x1, int32_t y1, int32_t x2, int32_t y2);
|
|
|
|
|
2013-05-20 19:31:34 +00:00
|
|
|
void G_SaveMapState(void);
|
|
|
|
void G_RestoreMapState(void);
|
2011-12-21 18:43:08 +00:00
|
|
|
]]
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
decl[[
|
|
|
|
int32_t kopen4loadfrommod(const char *filename, char searchfirst);
|
2012-12-29 15:21:24 +00:00
|
|
|
|
2013-01-13 16:40:07 +00:00
|
|
|
char **g_scriptModules;
|
|
|
|
int32_t g_scriptModulesNum;
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
const char *G_ConFile(void);
|
|
|
|
void G_DoGameStartup(const int32_t *params);
|
2013-01-19 18:28:40 +00:00
|
|
|
int32_t C_DefineSound(int32_t sndidx, const char *fn, int32_t args [5]);
|
2013-01-05 13:09:19 +00:00
|
|
|
void C_DefineMusic(int32_t vol, int32_t lev, const char *fn);
|
2013-01-01 15:24:28 +00:00
|
|
|
void C_DefineQuote(int32_t qnum, const char *qstr);
|
2013-01-05 13:09:19 +00:00
|
|
|
void C_DefineVolumeName(int32_t vol, const char *name);
|
2013-01-05 13:09:31 +00:00
|
|
|
void C_DefineSkillName(int32_t skill, const char *name);
|
2013-01-05 13:09:19 +00:00
|
|
|
void C_DefineLevelName(int32_t vol, int32_t lev, const char *fn,
|
|
|
|
int32_t partime, int32_t designertime,
|
|
|
|
const char *levelname);
|
2013-02-07 21:01:03 +00:00
|
|
|
void C_DefineProjectile(int32_t j, int32_t what, int32_t val);
|
2013-02-24 16:05:22 +00:00
|
|
|
void C_DefineGameFuncName(int32_t idx, const char *name);
|
2013-05-30 18:10:49 +00:00
|
|
|
void C_DefineGameType(int32_t idx, int32_t flags, const char *name);
|
2013-02-24 16:05:22 +00:00
|
|
|
int32_t C_SetDefName(const char *name);
|
2013-11-08 18:08:45 +00:00
|
|
|
void C_SetCfgName(const char *cfgname);
|
2013-02-28 17:30:15 +00:00
|
|
|
|
|
|
|
int32_t SCRIPT_GetNumber(int32_t scripthandle, const char *sectionname, const char *entryname, int32_t *number);
|
|
|
|
void SCRIPT_PutNumber(int32_t scripthandle, const char *sectionname, const char *entryname, int32_t number,
|
|
|
|
int32_t hexadecimal, int32_t defaultvalue);
|
2012-07-20 21:57:28 +00:00
|
|
|
]]
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
|
|
|
|
-- http://lua-users.org/wiki/SandBoxes says "potentially unsafe"
|
|
|
|
-- as it allows to see implementations of functions.
|
|
|
|
--local string_dump = string.dump
|
|
|
|
string.dump = nil
|
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
|
|
|
|
-- sanity-check struct type sizes
|
2012-11-03 19:32:43 +00:00
|
|
|
local good = true
|
2013-06-09 16:37:16 +00:00
|
|
|
for i=0,10 do
|
2012-11-03 19:32:43 +00:00
|
|
|
local what = ffi.string(ffiC.g_sizes_of_what[i])
|
|
|
|
local fsz = ffi.sizeof(what)
|
|
|
|
local csz = ffiC.g_sizes_of[i]
|
2013-01-19 18:28:43 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
print(i..": "..what..": C sizeof = "..tostring(csz)..", FFI sizeof = "..tostring(fsz))
|
|
|
|
end
|
2012-11-03 19:32:43 +00:00
|
|
|
if (fsz ~= csz) then
|
|
|
|
good = false;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not good) then
|
|
|
|
error("Some sizes don't match between C and LuaJIT/FFI.")
|
2012-06-26 19:49:59 +00:00
|
|
|
end
|
2012-06-22 21:40:01 +00:00
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
|
2013-02-10 16:24:44 +00:00
|
|
|
--== "player" global, needed by the "control" module ==--
|
|
|
|
|
|
|
|
local player_static_members = {}
|
|
|
|
|
2013-09-05 17:37:42 +00:00
|
|
|
--[[
|
2013-06-01 20:09:42 +00:00
|
|
|
player_static_members._INPUT_BITS = defs_c.conststruct
|
2013-02-10 16:24:44 +00:00
|
|
|
{
|
|
|
|
JUMP = 1,
|
|
|
|
CROUCH = 2,
|
|
|
|
FIRE = 4,
|
|
|
|
AIM_UP = 8,
|
|
|
|
AIM_DOWN = 16,
|
|
|
|
RUNNING = 32,
|
|
|
|
LOOK_LEFT = 64,
|
|
|
|
LOOK_RIGHT = 128,
|
|
|
|
-- weapons...
|
|
|
|
STEROIDS = 4096,
|
|
|
|
LOOK_UP = 8192,
|
|
|
|
LOOK_DOWN = 16384,
|
|
|
|
NIGHTVISION = 32768,
|
|
|
|
MEDKIT = 65536,
|
|
|
|
RESERVED = 131072,
|
|
|
|
CENTER_VIEW = 262144,
|
|
|
|
HOLSTER_WEAPON = 524288,
|
|
|
|
INVENTORY_LEFT = 1048576,
|
|
|
|
PAUSE = 2097152,
|
|
|
|
QUICK_KICK = 4194304,
|
|
|
|
AIM_MODE = 8388608,
|
|
|
|
HOLODUKE = 16777216,
|
|
|
|
JETPACK = 33554432,
|
|
|
|
QUIT = 67108864,
|
|
|
|
INVENTORY_RIGHT = 134217728,
|
|
|
|
TURN_AROUND = 268435456,
|
|
|
|
OPEN = 536870912,
|
|
|
|
INVENTORY = 1073741824,
|
|
|
|
ESC = 2147483648,
|
|
|
|
}
|
|
|
|
|
2013-06-01 20:09:42 +00:00
|
|
|
player_static_members._INPUT_EXT_BITS = defs_c.conststruct
|
2013-02-10 16:24:44 +00:00
|
|
|
{
|
2013-03-24 18:53:28 +00:00
|
|
|
MOVE_FORWARD = 1,
|
|
|
|
MOVE_BACKWARD = 2,
|
|
|
|
STRAFE_LEFT = 4,
|
|
|
|
STRAFE_RIGHT = 8,
|
|
|
|
TURN_LEFT = 16,
|
|
|
|
TURN_RIGHT = 32,
|
2012-12-03 18:24:25 +00:00
|
|
|
}
|
2013-09-05 17:37:42 +00:00
|
|
|
--]]
|
2013-02-10 16:24:44 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
local band = bit.band
|
|
|
|
local lsh = bit.lshift
|
|
|
|
|
|
|
|
do
|
|
|
|
-- player.all() iterator
|
|
|
|
local function iter_allplayers(_nil, pli)
|
|
|
|
if (pli+1 < ffiC.playerswhenstarted) then
|
|
|
|
return pli+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function player_static_members.all()
|
|
|
|
return iter_allplayers, nil, -1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- player.holdskey(pli, keyname)
|
|
|
|
local KEYS = { -- SK_CROUCH etc. -- "sync keys"
|
|
|
|
CROUCH = lsh(1,1),
|
|
|
|
RUN = lsh(1,5),
|
|
|
|
OPEN = lsh(1,29),
|
|
|
|
}
|
|
|
|
|
|
|
|
function player_static_members.holdskey(pli, keyname)
|
|
|
|
bcheck.player_idx(pli)
|
|
|
|
if (KEYS[keyname] == nil) then
|
|
|
|
error("invalid key name: "..tostring(keyname), 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ffiC.g_player[pli].sync.bitsbits:test(KEYS[keyname])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local player_holdskey = player_static_members.holdskey
|
|
|
|
|
2013-03-24 18:53:28 +00:00
|
|
|
-- Actor flags
|
|
|
|
local actor_static_members = {}
|
|
|
|
do
|
|
|
|
local our_SFLAG = {}
|
|
|
|
local ext_SFLAG = con_lang.labels[4] -- external actor flags only
|
|
|
|
local USER_MASK = 0
|
|
|
|
|
|
|
|
for name, flag in pairs(ext_SFLAG) do
|
|
|
|
our_SFLAG[name:sub(7)] = flag -- strip "SFLAG_"
|
|
|
|
USER_MASK = bit.bor(USER_MASK, flag)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add a couple of convenience defines.
|
|
|
|
our_SFLAG.enemy = con_lang.SFLAG.SFLAG_BADGUY
|
|
|
|
our_SFLAG.enemystayput = con_lang.SFLAG.SFLAG_BADGUY + con_lang.SFLAG.SFLAG_BADGUYSTAYPUT
|
|
|
|
our_SFLAG.rotfixed = con_lang.SFLAG.SFLAG_ROTFIXED
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
-- Callback function chaining control flags.
|
2013-05-30 18:10:54 +00:00
|
|
|
our_SFLAG.replace_hard = 0x08000000 -- Replace actor code and flags
|
|
|
|
our_SFLAG.replace_soft = 0x10000000 -- Replace actor code, bitwise OR flags
|
2013-06-12 17:49:53 +00:00
|
|
|
our_SFLAG.replace = 0x08000000 -- Should only be used for gameevent
|
2013-05-30 18:10:54 +00:00
|
|
|
our_SFLAG.chain_beg = 0x20000000
|
|
|
|
our_SFLAG.chain_end = 0x40000000
|
2013-06-12 17:49:53 +00:00
|
|
|
our_SFLAG._CHAIN_MASK_ACTOR = 0x78000000
|
|
|
|
our_SFLAG._CHAIN_MASK_EVENT = 0x68000000
|
2013-03-31 18:58:04 +00:00
|
|
|
|
2013-03-24 18:53:28 +00:00
|
|
|
-- XXX: CON doesn't export BADGUYSTAYPUT or ROTFIXED SFLAGs, but they are considered
|
|
|
|
-- external for Lunatic.
|
|
|
|
our_SFLAG.USER_MASK = bit.bor(USER_MASK, our_SFLAG.enemystayput, our_SFLAG.rotfixed)
|
|
|
|
|
|
|
|
actor_static_members.FLAGS = defs_c.conststruct(our_SFLAG)
|
2013-06-07 14:26:30 +00:00
|
|
|
|
|
|
|
-- Sprite status numbers. Kept in 'actor', because it's more of a game-side
|
|
|
|
-- concept (event though status lists are implemented in the engine), and
|
|
|
|
-- to prevent confusion with sprite.CSTAT.
|
|
|
|
local our_STAT = {}
|
|
|
|
|
|
|
|
for name, statnum in pairs(con_lang.STAT) do
|
|
|
|
-- Strip 'STAT_'.
|
|
|
|
our_STAT[name:sub(6)] = statnum
|
|
|
|
end
|
|
|
|
|
|
|
|
actor_static_members.STAT = defs_c.conststruct(our_STAT)
|
2013-06-15 12:52:06 +00:00
|
|
|
|
2013-06-30 20:38:41 +00:00
|
|
|
actor_static_members.MOVFLAGS = defs_c.conststruct
|
|
|
|
{
|
|
|
|
-- NOTE: no underscores, like in DEFS.CON.
|
|
|
|
faceplayer = 1,
|
|
|
|
geth = 2,
|
|
|
|
getv = 4,
|
|
|
|
randomangle = 8,
|
|
|
|
faceplayerslow = 16,
|
|
|
|
spin = 32,
|
|
|
|
faceplayersmart = 64,
|
|
|
|
fleeenemy = 128,
|
|
|
|
jumptoplayer = 257,
|
|
|
|
seekplayer = 512,
|
|
|
|
furthestdir = 1024,
|
|
|
|
dodgebullet = 4096,
|
|
|
|
}
|
2013-03-24 18:53:28 +00:00
|
|
|
end
|
|
|
|
|
2013-07-04 19:38:32 +00:00
|
|
|
function actor_static_members.fall(i)
|
|
|
|
check_sprite_idx(i)
|
|
|
|
CF.VM_FallSprite(i)
|
|
|
|
end
|
|
|
|
|
2013-05-28 19:52:48 +00:00
|
|
|
-- Delete sprite with index <i>.
|
|
|
|
function actor_static_members.delete(i)
|
|
|
|
check_sprite_idx(i)
|
|
|
|
|
|
|
|
if (ffiC.sprite[i].statnum == ffiC.MAXSTATUS) then
|
|
|
|
error("Attempt to delete a sprite already not in the game world", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ffiC.block_deletesprite ~= 0) then
|
|
|
|
error("Attempt to delete sprite in EVENT_EGS", 2)
|
|
|
|
end
|
|
|
|
|
2013-05-30 18:10:49 +00:00
|
|
|
-- TODO_MP
|
2013-05-28 19:52:48 +00:00
|
|
|
if (ffiC.g_player_ps[0].i == i) then
|
|
|
|
error("Attempt to delete player 0's APLAYER sprite", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
CF.A_DeleteSprite(i)
|
|
|
|
end
|
|
|
|
|
2013-03-15 16:56:11 +00:00
|
|
|
local tile_static_members = {}
|
2013-03-24 18:53:28 +00:00
|
|
|
do
|
|
|
|
tile_static_members.sizx = defs_c.creategtab(ffiC.tilesizx, ffiC.MAXTILES, "tilesizx[]")
|
|
|
|
tile_static_members.sizy = defs_c.creategtab(ffiC.tilesizy, ffiC.MAXTILES, "tilesizy[]")
|
|
|
|
end
|
2013-03-15 16:56:11 +00:00
|
|
|
|
2013-02-28 17:29:58 +00:00
|
|
|
-- XXX: error message will say "g_player_ps"
|
2013-02-10 16:24:44 +00:00
|
|
|
player = setmtonce({}, defs_c.GenStructMetatable("g_player_ps", "playerswhenstarted", player_static_members))
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2012-12-29 15:21:32 +00:00
|
|
|
-- needed by "control"
|
2013-03-24 18:53:28 +00:00
|
|
|
actor = setmtonce({}, defs_c.GenStructMetatable("actor", "MAXSPRITES", actor_static_members))
|
2013-05-30 18:10:54 +00:00
|
|
|
-- Some bitwise NOTs of various actor flag masks.
|
|
|
|
local BNOT = {
|
|
|
|
USER_MASK = bit.bnot(actor.FLAGS.USER_MASK),
|
2013-06-12 17:49:53 +00:00
|
|
|
CHAIN_MASK_ACTOR = bit.bnot(actor.FLAGS._CHAIN_MASK_ACTOR),
|
|
|
|
CHAIN_MASK_EVENT = bit.bnot(actor.FLAGS._CHAIN_MASK_EVENT),
|
2013-05-30 18:10:54 +00:00
|
|
|
}
|
2012-12-29 15:21:32 +00:00
|
|
|
|
2013-02-07 21:01:03 +00:00
|
|
|
local projectile = defs_c.creategtab(ffiC.ProjectileData, ffiC.MAXTILES, "projectile[]")
|
2013-03-15 16:56:11 +00:00
|
|
|
local g_tile = setmtonce({}, defs_c.GenStructMetatable("g_tile", "MAXTILES", tile_static_members))
|
2013-02-07 21:01:03 +00:00
|
|
|
|
2012-08-06 20:00:29 +00:00
|
|
|
--== Custom operations for BUILD data structures ==--
|
2012-12-29 15:21:32 +00:00
|
|
|
-- Among other things, declares struct action and struct move, and their
|
|
|
|
-- ID-wrapped types con_action_t and con_move_t
|
2012-08-08 19:32:45 +00:00
|
|
|
local con = require("control")
|
2013-06-15 12:52:06 +00:00
|
|
|
local isenemytile = con.isenemytile
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2013-01-23 19:36:48 +00:00
|
|
|
-- Add game-side metamethods to "spritetype" and register it with "metatype"
|
2013-02-01 13:05:20 +00:00
|
|
|
local spr_mt_index_add = {
|
|
|
|
isenemy = function(s)
|
2013-06-15 12:52:06 +00:00
|
|
|
return isenemytile(s.picnum)
|
2013-02-01 13:05:20 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
defs_c.finish_spritetype(spr_mt_index_add)
|
2013-01-23 19:36:48 +00:00
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
-- Check a literal numeric action or move value.
|
2012-08-19 12:52:18 +00:00
|
|
|
local function check_literal_am(am, typename)
|
2012-08-08 19:32:35 +00:00
|
|
|
if (type(am) ~= "number") then
|
2012-08-19 12:52:18 +00:00
|
|
|
error("bad argument: expected number or "..typename, 3)
|
2012-08-08 19:32:35 +00:00
|
|
|
end
|
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
-- Negative values are generated as con.action/con.move IDs.
|
2012-08-08 19:32:35 +00:00
|
|
|
if (not (am >= 0 and am <= 32767)) then
|
|
|
|
error("bad argument: expected number in [0 .. 32767]", 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-26 17:07:53 +00:00
|
|
|
-- An unrestricted actor_t pointer, for internal use:
|
2013-02-03 12:48:25 +00:00
|
|
|
local actor_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(ACTOR_STRUCT)))
|
|
|
|
local player_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(DUKEPLAYER_STRUCT)))
|
2013-02-10 16:23:59 +00:00
|
|
|
local projectile_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(PROJECTILE_STRUCT)))
|
2013-07-13 21:04:59 +00:00
|
|
|
-- An unrestricted weapondata_t pointer, but with the member names stripped of
|
|
|
|
-- the leading underscore, too:
|
2013-03-21 10:20:20 +00:00
|
|
|
local weapondata_ptr_ct = ffi.typeof("$ *", ffi.typeof((strip_const(WEAPONDATA_STRUCT):gsub(" _"," "))))
|
2013-06-30 20:38:39 +00:00
|
|
|
|
|
|
|
local con_action_ct = ffi.typeof("const con_action_t")
|
|
|
|
local con_move_ct = ffi.typeof("const con_move_t")
|
|
|
|
local con_ai_ct = ffi.typeof("const con_ai_t")
|
|
|
|
|
|
|
|
-- All-zero bare action and move.
|
|
|
|
local nullac, nullmv = ffi.new("const struct action"), ffi.new("const struct move")
|
|
|
|
-- All-zero action and move with IDs. Mostly for CON support.
|
|
|
|
local literal_act = { [0]=con_action_ct(0), [1]=con_action_ct(1) }
|
|
|
|
local literal_mov = { [0]=con_move_ct(0), [1]=con_move_ct(1) }
|
2012-08-10 19:11:43 +00:00
|
|
|
|
2013-02-07 21:01:18 +00:00
|
|
|
local function get_actor_idx(a)
|
|
|
|
local i = ffi.cast(actor_ptr_ct, a)-ffi.cast(actor_ptr_ct, ffiC.actor)
|
2013-07-19 12:49:02 +00:00
|
|
|
-- assert(not (i >= ffiC.MAXSPRITES+0ULL))
|
2013-02-07 21:01:18 +00:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:29 +00:00
|
|
|
local actor_mt = {
|
|
|
|
__index = {
|
2012-08-08 19:32:35 +00:00
|
|
|
-- action
|
2012-08-06 20:00:29 +00:00
|
|
|
set_action = function(a, act)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2013-06-30 20:38:39 +00:00
|
|
|
|
2012-08-10 19:11:43 +00:00
|
|
|
if (ffi.istype(con_action_ct, act)) then
|
2012-08-06 20:00:29 +00:00
|
|
|
a.t_data[4] = act.id
|
|
|
|
a.ac = act.ac
|
|
|
|
else
|
2012-08-19 12:52:18 +00:00
|
|
|
check_literal_am(act, "action")
|
2012-08-06 20:00:29 +00:00
|
|
|
a.t_data[4] = act
|
|
|
|
a.ac = nullac
|
|
|
|
end
|
2012-08-08 19:32:35 +00:00
|
|
|
|
|
|
|
a.t_data[2] = 0
|
|
|
|
a.t_data[3] = 0
|
|
|
|
end,
|
|
|
|
|
|
|
|
has_action = function(a, act)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2013-06-30 20:38:39 +00:00
|
|
|
|
2012-08-10 19:11:43 +00:00
|
|
|
if (ffi.istype(con_action_ct, act)) then
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[4]==act.id)
|
|
|
|
else
|
2012-08-19 12:52:18 +00:00
|
|
|
check_literal_am(act, "action")
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[4]==act)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
-- count
|
2013-06-01 20:09:42 +00:00
|
|
|
set_count = function(a, count)
|
|
|
|
ffi.cast(actor_ptr_ct, a).t_data[0] = count
|
2012-08-08 19:32:35 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
get_count = function(a)
|
2012-08-10 19:11:43 +00:00
|
|
|
return ffi.cast(actor_ptr_ct, a).t_data[0]
|
2012-08-08 19:32:35 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
-- action count
|
|
|
|
reset_acount = function(a)
|
2012-08-10 19:11:43 +00:00
|
|
|
ffi.cast(actor_ptr_ct, a).t_data[2] = 0
|
2012-08-06 20:00:29 +00:00
|
|
|
end,
|
|
|
|
|
2012-08-08 19:32:35 +00:00
|
|
|
get_acount = function(a)
|
2012-08-10 19:11:43 +00:00
|
|
|
return ffi.cast(actor_ptr_ct, a).t_data[2]
|
2012-08-08 19:32:35 +00:00
|
|
|
end,
|
|
|
|
|
2013-06-30 20:38:44 +00:00
|
|
|
-- Override action delay. The action ID is kept.
|
|
|
|
set_action_delay = function(a, delay)
|
|
|
|
ffi.cast(actor_ptr_ct, a).ac.delay = delay
|
|
|
|
end,
|
|
|
|
|
2012-08-08 19:32:35 +00:00
|
|
|
-- move
|
|
|
|
set_move = function(a, mov, movflags)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2013-06-30 20:38:39 +00:00
|
|
|
|
2012-08-10 19:11:43 +00:00
|
|
|
if (ffi.istype(con_move_ct, mov)) then
|
2012-08-08 19:32:35 +00:00
|
|
|
a.t_data[1] = mov.id
|
2012-08-06 20:00:29 +00:00
|
|
|
a.mv = mov.mv
|
|
|
|
else
|
2012-08-19 12:52:18 +00:00
|
|
|
check_literal_am(mov, "move")
|
2012-08-08 19:32:35 +00:00
|
|
|
a.t_data[1] = mov
|
2012-08-06 20:00:29 +00:00
|
|
|
a.mv = nullmv
|
|
|
|
end
|
2012-08-08 19:32:35 +00:00
|
|
|
|
|
|
|
a.t_data[0] = 0
|
2013-06-30 20:38:52 +00:00
|
|
|
a.movflags = movflags or 0
|
2013-06-15 12:52:06 +00:00
|
|
|
local spr = ffiC.sprite[get_actor_idx(a)]
|
2012-08-08 19:32:35 +00:00
|
|
|
|
2013-06-15 12:52:06 +00:00
|
|
|
if (not spr:isenemy() or spr.extra > 0) then
|
2013-06-30 20:38:52 +00:00
|
|
|
if (bit.band(a.movflags, 8) ~= 0) then -- random_angle
|
2013-06-15 12:52:06 +00:00
|
|
|
spr.ang = bit.band(ffiC.krand(), 2047)
|
|
|
|
end
|
|
|
|
end
|
2012-08-08 19:32:35 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
has_move = function(a, mov)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2013-06-30 20:38:39 +00:00
|
|
|
|
2012-08-10 19:11:43 +00:00
|
|
|
if (ffi.istype(con_move_ct, mov)) then
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[1]==mov.id)
|
|
|
|
else
|
2012-08-19 12:52:18 +00:00
|
|
|
check_literal_am(mov, "move")
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[1]==mov)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2013-06-30 20:38:44 +00:00
|
|
|
-- Override velocity, keeping move ID.
|
|
|
|
set_hvel = function(a, hvel)
|
|
|
|
ffi.cast(actor_ptr_ct, a).mv.hvel = hvel
|
|
|
|
end,
|
|
|
|
|
|
|
|
set_vvel = function(a, vvel)
|
|
|
|
ffi.cast(actor_ptr_ct, a).mv.vvel = vvel
|
|
|
|
end,
|
|
|
|
|
2012-08-08 19:32:35 +00:00
|
|
|
-- ai
|
|
|
|
set_ai = function(a, ai)
|
|
|
|
local oa = a
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2012-08-08 19:32:35 +00:00
|
|
|
|
|
|
|
-- TODO: literal number AIs?
|
2012-08-19 12:52:18 +00:00
|
|
|
if (not ffi.istype(con_ai_ct, ai)) then
|
|
|
|
error("bad argument: expected ai", 2)
|
|
|
|
end
|
2012-08-08 19:32:35 +00:00
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
-- NOTE: compare with gameexec.c, "CON_AI:"
|
2012-08-08 19:32:35 +00:00
|
|
|
a.t_data[5] = ai.id
|
|
|
|
|
|
|
|
oa:set_action(ai.act)
|
|
|
|
oa:set_move(ai.mov, ai.movflags)
|
|
|
|
|
2013-07-04 19:38:32 +00:00
|
|
|
-- Already reset with set_move():
|
|
|
|
-- a.t_data[0] = 0
|
2012-08-08 19:32:35 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
has_ai = function(a, ai)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2012-08-08 19:32:35 +00:00
|
|
|
|
2012-08-10 19:11:43 +00:00
|
|
|
if (ffi.istype(con_ai_ct, ai)) then
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[5]==ai.id)
|
|
|
|
else
|
2012-08-19 12:52:18 +00:00
|
|
|
check_literal_am(ai, "ai")
|
2012-08-08 19:32:35 +00:00
|
|
|
return (a.t_data[5]==ai)
|
|
|
|
end
|
2012-08-06 20:00:29 +00:00
|
|
|
end,
|
2012-12-09 13:24:39 +00:00
|
|
|
|
|
|
|
-- Getters/setters.
|
2013-06-30 20:38:48 +00:00
|
|
|
_get_t_data = function(a, idx)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (idx >= 0 and idx < 10)) then
|
2013-06-30 20:38:39 +00:00
|
|
|
error("invalid t_data index "..idx, 2)
|
2012-12-09 13:24:39 +00:00
|
|
|
end
|
|
|
|
return ffi.cast(actor_ptr_ct, a).t_data[idx]
|
|
|
|
end,
|
2013-02-01 13:05:00 +00:00
|
|
|
|
|
|
|
_set_t_data = function(a, idx, val)
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (idx >= 0 and idx < 10)) then
|
2013-06-30 20:38:39 +00:00
|
|
|
error("invalid t_data index "..idx, 2)
|
2013-02-01 13:05:00 +00:00
|
|
|
end
|
|
|
|
ffi.cast(actor_ptr_ct, a).t_data[idx] = val
|
|
|
|
end,
|
2013-02-03 12:48:06 +00:00
|
|
|
|
|
|
|
set_picnum = function(a, picnum)
|
2013-04-07 15:20:28 +00:00
|
|
|
if (not (picnum < 0)) then
|
2013-03-29 15:59:23 +00:00
|
|
|
check_tile_idx(picnum)
|
|
|
|
end
|
2013-02-03 12:48:06 +00:00
|
|
|
ffi.cast(actor_ptr_ct, a).picnum = picnum
|
|
|
|
end,
|
|
|
|
|
|
|
|
set_owner = function(a, owner)
|
|
|
|
-- XXX: is it permissible to set to -1?
|
|
|
|
check_sprite_idx(owner)
|
|
|
|
ffi.cast(actor_ptr_ct, a).owner = owner
|
|
|
|
end,
|
2013-07-04 19:38:42 +00:00
|
|
|
|
|
|
|
--- Custom methods ---
|
|
|
|
|
|
|
|
-- Checkers for whether the movement update made the actor hit
|
|
|
|
-- something.
|
|
|
|
|
|
|
|
checkhit = function(a)
|
|
|
|
-- Check whether we hit *anything*, including ceiling/floor.
|
|
|
|
return a._movflagbits:test(49152)
|
|
|
|
end,
|
|
|
|
|
|
|
|
checkbump = function(a)
|
|
|
|
-- Check whether we bumped into a wall or sprite.
|
|
|
|
return (a._movflagbits:mask(49152) >= 32768)
|
|
|
|
end,
|
|
|
|
|
|
|
|
hitwall = function(a)
|
|
|
|
if (a._movflagbits:mask(49152) == 32768) then
|
|
|
|
return a._movflagbits:mask(32767)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
hitsprite = function(a)
|
|
|
|
if (a._movflagbits:mask(49152) == 49152) then
|
|
|
|
return a._movflagbits:mask(32767)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
-- NOTE: no 'hitsector' or 'hitceiling' / 'hitfloor' for now because
|
|
|
|
-- more research is needed as to what the best way of checking c/f is.
|
2012-08-06 20:00:29 +00:00
|
|
|
},
|
|
|
|
}
|
2013-02-07 21:01:18 +00:00
|
|
|
|
|
|
|
local function actor_index_index(a, key)
|
|
|
|
-- actor[].proj gets an actor's projectile ([gs]etthisprojectile in CON)
|
|
|
|
if (key=="proj") then
|
|
|
|
return ffiC.SpriteProjectile[get_actor_idx(a)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setmtonce(actor_mt.__index, { __index = actor_index_index })
|
2012-08-06 20:00:29 +00:00
|
|
|
ffi.metatype("actor_t", actor_mt)
|
|
|
|
|
|
|
|
|
2013-01-19 18:28:43 +00:00
|
|
|
--- PER-PLAYER WEAPON SETTINGS
|
2013-07-13 21:04:59 +00:00
|
|
|
local wd_sound_member = {}
|
|
|
|
for _, declstr in pairs(con_lang.wdata_members) do
|
|
|
|
local member = declstr:match("const int32_t _(.*sound)$")
|
|
|
|
if (member) then
|
|
|
|
wd_sound_member[member] = true
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
printf("weapondata_t member %s denotes a sound", member)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-19 18:28:43 +00:00
|
|
|
local weapondata_mt = {
|
2013-03-24 18:53:06 +00:00
|
|
|
__index = function(wd, member)
|
|
|
|
-- Handle protected members that are renamed (e.g. shoots/_shoots).
|
2013-04-07 15:20:41 +00:00
|
|
|
return ffi.cast(weapondata_ptr_ct, wd)[0][member]
|
2013-03-24 18:53:06 +00:00
|
|
|
end,
|
|
|
|
|
2013-01-19 18:28:43 +00:00
|
|
|
__newindex = function(wd, member, val)
|
2013-06-02 14:07:56 +00:00
|
|
|
-- Set to 'true' if we set a tile or sound member.
|
|
|
|
local didit = false
|
|
|
|
|
2013-07-13 21:04:59 +00:00
|
|
|
check_type(member, "string") -- MEMBER_IS_STRING
|
2013-06-13 17:11:09 +00:00
|
|
|
check_number(val)
|
|
|
|
|
2013-07-13 21:04:59 +00:00
|
|
|
if (wd_sound_member[member]) then -- XXX: sound2time is a time, not a sound
|
2013-03-18 22:05:26 +00:00
|
|
|
if (val < 0) then
|
2013-07-13 21:04:59 +00:00
|
|
|
val = 0 -- Set to 0 if negative (e.g. CrackDown).
|
2013-03-18 22:05:26 +00:00
|
|
|
else
|
|
|
|
check_sound_idx(val)
|
|
|
|
end
|
2013-06-02 14:07:56 +00:00
|
|
|
didit = true
|
2013-02-07 21:01:03 +00:00
|
|
|
elseif (member=="workslike") then
|
|
|
|
check_weapon_idx(val)
|
2013-03-18 22:05:26 +00:00
|
|
|
elseif (member=="spawn" or member=="shoots") then
|
|
|
|
if (val < 0) then
|
|
|
|
-- Set to 0 if oob (e.g. CrackDown). This is a bit problematic
|
|
|
|
-- for .shoots because it's used unconditionally except in one
|
|
|
|
-- case (see player.c).
|
|
|
|
val = 0
|
|
|
|
else
|
|
|
|
check_tile_idx(val)
|
|
|
|
end
|
2013-06-02 14:07:56 +00:00
|
|
|
didit = true
|
2013-02-07 21:01:03 +00:00
|
|
|
end
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2013-07-13 21:04:59 +00:00
|
|
|
-- DEBUG:
|
|
|
|
-- printf("assigning %s to weapon's %s", tostring(val), member)
|
|
|
|
|
|
|
|
-- NOTE: we're indexing a *pointer* with the user-supplied 'member',
|
|
|
|
-- which could be dangerouns if it could be a number. However, we have
|
|
|
|
-- assured that is is not in MEMBER_IS_STRING above.
|
|
|
|
ffi.cast(weapondata_ptr_ct, wd)[member] = val
|
2013-06-02 14:07:56 +00:00
|
|
|
|
|
|
|
if (didit and ffiC.g_elCallDepth==0) then
|
|
|
|
-- Signal that we overrode this member at CON translation time.
|
|
|
|
|
|
|
|
-- Get weapon index as pointer difference first. PLAYER_0.
|
2013-07-04 19:38:50 +00:00
|
|
|
local wi = ffi.cast(weapondata_ptr_ct, wd)
|
|
|
|
- ffi.cast(weapondata_ptr_ct, ffiC.g_playerWeapon)
|
2013-07-19 12:49:02 +00:00
|
|
|
assert(wi >= 0 and wi < ffiC.MAX_WEAPONS)
|
2013-06-02 14:07:56 +00:00
|
|
|
|
|
|
|
-- Set g_weaponOverridden[wi][member], but without invoking
|
|
|
|
-- weapondata_t's __newindex metamethod (i.e. us)!
|
|
|
|
ffi.cast(weapondata_ptr_ct, ffiC.g_weaponOverridden[wi])[member] = 1
|
|
|
|
end
|
2013-01-19 18:28:43 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
ffi.metatype("weapondata_t", weapondata_mt)
|
|
|
|
|
|
|
|
local weaponaccess_mt = {
|
2013-07-04 19:38:50 +00:00
|
|
|
-- Syntax like "player[0].weapon.KNEE.shoots" possible because
|
|
|
|
-- g_playerWeapon[] is declared as an array of corresponding bcarray types
|
|
|
|
-- for us.
|
2013-01-19 18:28:43 +00:00
|
|
|
__index = function(wa, key)
|
2013-07-04 19:38:50 +00:00
|
|
|
if (type(key)~="number" and type(key)~="string") then
|
|
|
|
error("must access weapon either by number or by name")
|
2013-01-19 18:28:43 +00:00
|
|
|
end
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2013-07-04 19:38:50 +00:00
|
|
|
return ffiC.g_playerWeapon[wa._p][key]
|
2013-01-19 18:28:43 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
ffi.metatype("weaponaccess_t", weaponaccess_mt)
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2013-06-20 18:31:50 +00:00
|
|
|
|
|
|
|
local function clamp(num, min, max)
|
|
|
|
return num < min and min
|
|
|
|
or num > max and max
|
|
|
|
or num
|
|
|
|
end
|
|
|
|
|
|
|
|
local function clamp0to1(num)
|
|
|
|
check_number(num, 4)
|
|
|
|
return clamp(num, 0, 1)
|
|
|
|
end
|
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
local player_mt = {
|
|
|
|
__index = {
|
2012-11-30 18:57:55 +00:00
|
|
|
-- CON-like addammo/addweapon, but without the non-local control flow
|
|
|
|
-- (returns true if weapon's ammo was at the max. instead).
|
2012-12-03 18:24:25 +00:00
|
|
|
addammo = con._addammo,
|
|
|
|
addweapon = con._addweapon,
|
2012-12-03 18:24:30 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
stomp = con._pstomp,
|
2012-12-03 18:24:25 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
holdskey = function(p, keyname)
|
|
|
|
-- XXX: on invalid <keyname>, error will point to this next line:
|
|
|
|
return player_holdskey(p.weapon._p, keyname)
|
|
|
|
end,
|
|
|
|
|
|
|
|
has_weapon = function(p, weap)
|
|
|
|
return (band(p.gotweapon, lsh(1, weap)) ~= 0)
|
2013-01-24 20:03:10 +00:00
|
|
|
end,
|
|
|
|
|
2013-02-01 13:04:52 +00:00
|
|
|
give_weapon = function(p, weap)
|
2013-07-04 19:38:42 +00:00
|
|
|
p.gotweapon = bit.bor(p.gotweapon, lsh(1, weap))
|
2013-02-01 13:04:52 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
take_weapon = function(p, weap)
|
2013-07-04 19:38:42 +00:00
|
|
|
p.gotweapon = band(p.gotweapon, bit.bnot(lsh(1, weap)))
|
2013-02-01 13:04:52 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
-- Give or take weapon, for implementation of CON's .gotweapon access.
|
|
|
|
_gt_weapon = function(p, weap, give_p)
|
|
|
|
if (give_p ~= 0) then
|
|
|
|
p:give_weapon(weap)
|
|
|
|
else
|
|
|
|
p:take_weapon(weap)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2013-07-04 19:38:32 +00:00
|
|
|
whack = function(p, no_return_to_center)
|
2012-12-03 18:24:30 +00:00
|
|
|
p.horiz = p.horiz + 64
|
2013-01-13 16:40:21 +00:00
|
|
|
if (not no_return_to_center) then
|
|
|
|
p.return_to_center = 9
|
|
|
|
end
|
2013-07-04 19:38:42 +00:00
|
|
|
local n = bit.arshift(128-band(ffiC.krand(),255), 1)
|
2012-12-03 18:24:30 +00:00
|
|
|
p.rotscrnang = n
|
|
|
|
p.look_ang = n
|
|
|
|
end,
|
|
|
|
|
2013-06-20 18:31:50 +00:00
|
|
|
-- External, improved 'palfrom'.
|
|
|
|
-- <speed>: possibly fractional speed of tint fading, in pals.f decrements per gametic.
|
|
|
|
-- XXX: exposes internals.
|
|
|
|
-- <prio>: a value from -128 to 127, higher ones trump lower or equal ones
|
|
|
|
fadecol = function(p, fadefrac, r, g, b, speed, prio)
|
|
|
|
-- Validate inargs: clamp f,r,g,b to [0 .. 1] first and multiply by
|
|
|
|
-- 63 for the internal handling.
|
|
|
|
fadefrac = clamp0to1(fadefrac)*63
|
|
|
|
-- NOTE: a fadefrac of now <1 is allowed, e.g. for clearing the tint.
|
|
|
|
r = clamp0to1(r)*63
|
|
|
|
g = clamp0to1(g)*63
|
|
|
|
b = clamp0to1(b)*63
|
|
|
|
|
|
|
|
if (speed ~= nil) then
|
|
|
|
check_number(speed)
|
|
|
|
-- Clamp to sensible values; the speed is stored in an int8_t
|
|
|
|
-- (see below).
|
|
|
|
speed = clamp(speed, 1/128, 127)
|
|
|
|
else
|
|
|
|
speed = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if (prio ~= nil) then
|
|
|
|
check_number(prio)
|
|
|
|
|
|
|
|
if (not (prio >= -128 and prio < 127)) then
|
|
|
|
error("invalid argument #6 (priority): must be in [-128 .. 127]", 2)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
prio = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if a currently active tint has higher priority.
|
|
|
|
if (p._pals.f > 0 and p._palsfadeprio > prio) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- The passed tint can be applied now.
|
|
|
|
p:_palfrom(fadefrac, r, g, b)
|
|
|
|
p._palsfadeprio = prio
|
|
|
|
|
|
|
|
-- Calculate .palsfade{speed,next}
|
|
|
|
if (speed >= 1) then
|
|
|
|
-- Will round to the nearest integer ("banker's
|
|
|
|
-- rounding"). NOTE: This is not correct for all numbers, see
|
|
|
|
-- http://blog.frama-c.com/index.php?post/2013/05/02/nearbyintf1
|
|
|
|
p._palsfadespeed = speed + 0.5
|
|
|
|
p._palsfadenext = 0
|
|
|
|
else
|
|
|
|
-- NOTE: Values that round to 0 have are equivalent behavior to
|
|
|
|
-- passing a <speed> of 1.
|
|
|
|
local negspeedrecip = -((1/speed) + 0.5) -- [-128.5 .. 1/127+0.5]
|
|
|
|
p._palsfadespeed = negspeedrecip
|
|
|
|
p._palsfadenext = negspeedrecip
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
-- INTERNAL and CON-only.
|
2012-12-03 18:24:25 +00:00
|
|
|
_palfrom = function(p, f, r,g,b)
|
2013-02-01 13:04:52 +00:00
|
|
|
local pals = p._pals
|
2013-07-04 19:38:39 +00:00
|
|
|
-- Assume that CON's palfrom starts with prio 0 and speed 0.
|
|
|
|
if (pals.f == 0 or p._palsfadeprio <= 0) then
|
|
|
|
pals.f = f
|
|
|
|
pals.r, pals.g, pals.b = r, g, b
|
|
|
|
p._palsfadespeed, p._palsfadenext = 0, 0
|
|
|
|
end
|
2012-12-03 18:24:25 +00:00
|
|
|
end,
|
2012-11-30 18:57:46 +00:00
|
|
|
},
|
2013-04-07 15:20:41 +00:00
|
|
|
|
|
|
|
__newindex = function(p, key, val)
|
|
|
|
-- Write access to protected player members.
|
|
|
|
|
|
|
|
local allowneg = DukePlayer_prot_allowneg[key]
|
|
|
|
assert(type(allowneg)=="boolean")
|
|
|
|
|
|
|
|
if (allowneg==false or not (val < 0)) then
|
|
|
|
DukePlayer_prot_chkfunc[key](val)
|
|
|
|
end
|
|
|
|
ffi.cast(player_ptr_ct, p)[key] = val
|
|
|
|
end,
|
2012-11-30 18:57:46 +00:00
|
|
|
}
|
2013-02-10 16:24:44 +00:00
|
|
|
|
|
|
|
local function player_index_index(p, key)
|
|
|
|
if (key=="_input") then
|
|
|
|
return ffiC.g_player[p.weapon._p].sync[0]
|
|
|
|
end
|
2013-04-07 15:20:41 +00:00
|
|
|
|
|
|
|
-- Read access to protected player members.
|
|
|
|
return ffi.cast(player_ptr_ct, p)[0][key]
|
2013-02-10 16:24:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
setmtonce(player_mt.__index, { __index = player_index_index })
|
2012-11-30 18:57:46 +00:00
|
|
|
ffi.metatype("DukePlayer_t", player_mt)
|
|
|
|
|
2013-06-09 16:37:16 +00:00
|
|
|
local function GenProjectileSetFunc(Member, checkfunc)
|
|
|
|
return function(self, idx)
|
|
|
|
if (not (idx == -1)) then
|
|
|
|
checkfunc(idx)
|
2013-02-10 16:23:59 +00:00
|
|
|
end
|
2013-06-09 16:37:16 +00:00
|
|
|
ffi.cast(projectile_ptr_ct, self)[Member] = idx
|
2013-02-10 16:23:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local projectile_mt = {
|
|
|
|
__index = {
|
2013-06-09 16:37:16 +00:00
|
|
|
set_spawns = GenProjectileSetFunc("spawns", check_tile_idx),
|
|
|
|
set_decal = GenProjectileSetFunc("decal", check_tile_idx),
|
|
|
|
set_trail = GenProjectileSetFunc("trail", check_tile_idx),
|
|
|
|
|
|
|
|
set_sound = GenProjectileSetFunc("sound", check_sound_idx),
|
|
|
|
set_isound = GenProjectileSetFunc("isound", check_sound_idx),
|
|
|
|
set_bsound = GenProjectileSetFunc("bsound", check_sound_idx),
|
2013-02-10 16:23:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("projectile_t", projectile_mt)
|
|
|
|
|
2013-02-10 16:24:20 +00:00
|
|
|
local user_defs_mt = {
|
|
|
|
__index = {
|
|
|
|
set_screen_size = function(ud, screen_size)
|
|
|
|
-- TODO: modify .statusbarmode accordingly
|
|
|
|
ud.screen_size = screen_size
|
|
|
|
end,
|
|
|
|
|
|
|
|
set_volume_number = function(ud, volume_number)
|
2013-06-01 20:09:42 +00:00
|
|
|
-- NOTE: allow volume_number==MAXVOLUMES.
|
|
|
|
if (not (volume_number==con_lang.MAXVOLUMES)) then
|
|
|
|
bcheck.volume_idx(volume_number)
|
|
|
|
end
|
2013-02-10 16:24:20 +00:00
|
|
|
ud.volume_number = volume_number
|
|
|
|
end,
|
2013-05-30 18:10:49 +00:00
|
|
|
|
|
|
|
set_level_number = function(ud, level_number)
|
|
|
|
bcheck.level_idx(level_number)
|
|
|
|
ud.level_number = level_number
|
|
|
|
end,
|
2013-02-10 16:24:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("user_defs", user_defs_mt)
|
|
|
|
|
2013-01-19 18:28:55 +00:00
|
|
|
--- CUSTOM "gv" VARIABLES
|
2013-01-19 18:28:40 +00:00
|
|
|
local camera_mt = {
|
|
|
|
-- TODO: "set position" method, which also updates the sectnum
|
|
|
|
__index = ffiC.g_camera,
|
|
|
|
__newindex = function(_, key, val)
|
|
|
|
if (key=="sect") then
|
2013-02-01 13:04:56 +00:00
|
|
|
check_sector_idx(val)
|
2013-01-19 18:28:40 +00:00
|
|
|
end
|
|
|
|
ffiC.g_camera[key] = val
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2013-01-26 17:07:44 +00:00
|
|
|
gv_access.cam = setmtonce({}, camera_mt)
|
2013-02-10 16:24:20 +00:00
|
|
|
gv_access._ud = ffiC.ud
|
2013-01-19 18:28:40 +00:00
|
|
|
|
2013-03-24 18:54:56 +00:00
|
|
|
-- Support for some CON global system gamevars. RETURN handled separately.
|
|
|
|
gv_access._csv = ffi.new "struct { int32_t LOTAG, HITAG, TEXTURE; }"
|
2013-02-07 21:01:18 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
gv_access.REND = defs_c.conststruct
|
|
|
|
{
|
|
|
|
CLASSIC = 0,
|
|
|
|
POLYMOST = 3,
|
|
|
|
POLYMER = 4,
|
|
|
|
}
|
|
|
|
|
2013-08-23 17:01:09 +00:00
|
|
|
gv_access.WEAPON = lprivate.WEAPON
|
|
|
|
gv_access.GET = lprivate.GET
|
|
|
|
|
2013-02-10 16:24:32 +00:00
|
|
|
function gv_access._get_yxaspect()
|
|
|
|
return ffiC.yxaspect
|
|
|
|
end
|
|
|
|
|
2013-02-11 17:16:58 +00:00
|
|
|
function gv_access._get_viewingrange()
|
|
|
|
return ffiC.viewingrange
|
|
|
|
end
|
|
|
|
|
2013-01-24 20:03:02 +00:00
|
|
|
function gv_access._currentFramerate()
|
2013-01-19 18:28:55 +00:00
|
|
|
return ffiC.g_currentFrameRate
|
|
|
|
end
|
|
|
|
|
2013-01-24 20:03:02 +00:00
|
|
|
function gv_access._currentMenu()
|
|
|
|
return ffiC.g_currentMenu
|
|
|
|
end
|
|
|
|
|
2013-04-09 17:34:53 +00:00
|
|
|
function gv_access._changeMenu(cm)
|
|
|
|
ffiC.M_ChangeMenu(cm)
|
|
|
|
end
|
|
|
|
|
2013-02-18 16:07:32 +00:00
|
|
|
function gv_access._set_guniqhudid(id)
|
|
|
|
local MAXUNIQHUDID = 256 -- KEEPINSYNC build.h
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (id >= 0 and id < MAXUNIQHUDID)) then
|
2013-02-18 16:07:32 +00:00
|
|
|
error("invalid unique HUD ID "..id)
|
|
|
|
end
|
|
|
|
ffiC.guniqhudid = id
|
|
|
|
end
|
|
|
|
|
2013-06-15 12:52:06 +00:00
|
|
|
-- TODO: make return 1-based index?
|
2013-01-19 18:28:55 +00:00
|
|
|
function gv_access.currentEpisode()
|
|
|
|
return ffiC.ud.volume_number
|
|
|
|
end
|
|
|
|
|
|
|
|
function gv_access.currentLevel()
|
|
|
|
return ffiC.ud.level_number
|
|
|
|
end
|
|
|
|
|
2013-02-11 17:16:58 +00:00
|
|
|
function gv_access.doQuake(gametics, snd)
|
|
|
|
ffiC.g_earthquakeTime = gametics
|
|
|
|
if (snd ~= nil) then
|
|
|
|
con._globalsound(ffiC.screenpeek, snd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
-- Declare all con_lang.labels constants in the global FFI namespace.
|
2012-08-06 20:00:23 +00:00
|
|
|
for i=1,#con_lang.labels do
|
2013-03-24 18:53:28 +00:00
|
|
|
if (getmetatable(con_lang.labels[i]) ~= "noffiC") then
|
|
|
|
local strbuf = {"enum {"}
|
|
|
|
for label, val in pairs(con_lang.labels[i]) do
|
|
|
|
strbuf[#strbuf+1] = string.format("%s = %d,", label, val)
|
|
|
|
end
|
|
|
|
strbuf[#strbuf+1] = "};"
|
2012-06-17 19:45:33 +00:00
|
|
|
|
2013-03-24 18:53:28 +00:00
|
|
|
ffi.cdef(table.concat(strbuf))
|
2012-06-17 19:45:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-11 16:53:15 +00:00
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
---=== Set up restricted global environment ===---
|
|
|
|
|
2012-07-13 18:20:38 +00:00
|
|
|
local allowed_modules = {
|
|
|
|
coroutine=coroutine, bit=bit, table=table, math=math, string=string,
|
2012-08-02 10:52:21 +00:00
|
|
|
|
|
|
|
os = {
|
2013-07-07 20:59:05 +00:00
|
|
|
clock = function() return gv_.gethiticks()*0.001 end,
|
2012-08-02 10:52:21 +00:00
|
|
|
},
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
randgen = randgen,
|
2012-08-02 10:52:32 +00:00
|
|
|
stat = require("stat"),
|
2012-08-02 10:52:44 +00:00
|
|
|
bitar = require("bitar"),
|
2013-02-16 18:53:15 +00:00
|
|
|
xmath = require("xmath"),
|
2013-11-11 16:48:23 +00:00
|
|
|
fs = require("fs"),
|
2012-08-06 20:00:29 +00:00
|
|
|
|
|
|
|
con = con,
|
2012-07-13 18:20:38 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 20:38:35 +00:00
|
|
|
do
|
|
|
|
local ctype_cansave = {}
|
|
|
|
|
|
|
|
-- Register as "serializeable" the type of cdata object <v>.
|
|
|
|
local function reg_serializable_cv(v)
|
|
|
|
assert(type(v)=="cdata")
|
|
|
|
assert(type(v._serialize)=="function")
|
|
|
|
-- NOTE: tonumber() on a ctype cdata object gets its LuaJIT-internal
|
|
|
|
-- ID, the one that would be shown with tostring(), e.g.
|
|
|
|
-- ctype<struct 95>
|
|
|
|
ctype_cansave[tonumber(ffi.typeof(v))] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function lprivate.cansave_cdata(v)
|
|
|
|
return type(v)=="cdata" and ctype_cansave[tonumber(ffi.typeof(v))]
|
|
|
|
end
|
|
|
|
|
|
|
|
reg_serializable_cv(allowed_modules.xmath.vec3())
|
|
|
|
reg_serializable_cv(allowed_modules.xmath.ivec3())
|
|
|
|
end
|
|
|
|
|
2013-03-24 18:53:28 +00:00
|
|
|
-- Protect base modules.
|
2013-06-30 20:38:41 +00:00
|
|
|
local function basemod_newidx()
|
2013-06-12 17:49:47 +00:00
|
|
|
error("modifying base module table forbidden", 2)
|
|
|
|
end
|
2012-07-13 18:20:38 +00:00
|
|
|
|
2013-06-12 17:49:47 +00:00
|
|
|
for modname, themodule in pairs(allowed_modules) do
|
|
|
|
local mt = {
|
|
|
|
__index = themodule,
|
2013-06-30 20:38:41 +00:00
|
|
|
__newindex = basemod_newidx,
|
2013-06-12 17:49:47 +00:00
|
|
|
}
|
2013-03-24 18:53:28 +00:00
|
|
|
|
2013-06-12 17:49:47 +00:00
|
|
|
allowed_modules[modname] = setmtonce({}, mt)
|
2012-07-13 18:20:38 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
|
2013-05-20 19:31:42 +00:00
|
|
|
---=== Module stuff ===---
|
|
|
|
|
|
|
|
local package_loaded = {} -- [<modname>] = false/true/table
|
2013-04-05 17:53:06 +00:00
|
|
|
local modname_stack = {} -- [<depth>]=string
|
|
|
|
local module_gamevars = {} -- [<modname>] = { <gvname1>, <gvname2>, ... }
|
2013-06-07 10:18:20 +00:00
|
|
|
local module_gvlocali = {} -- [<modname>] = { <localidx_beg>, <localidx_end> }
|
2013-06-20 18:31:47 +00:00
|
|
|
local module_thread = {} -- [<modname>] = <module_thread>
|
2013-04-05 17:53:06 +00:00
|
|
|
|
|
|
|
local function getcurmodname(thisfuncname)
|
|
|
|
if (#modname_stack == 0) then
|
|
|
|
error("'"..thisfuncname.."' must be called at the top level of a require'd file", 3)
|
|
|
|
-- ... as opposed to "at runtime".
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
return modname_stack[#modname_stack]
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
local function errorf(level, fmt, ...)
|
|
|
|
local errmsg = string.format(fmt, ...)
|
|
|
|
error(errmsg, level+1)
|
|
|
|
end
|
|
|
|
|
2013-05-06 19:43:35 +00:00
|
|
|
local function readintostr_mod(fn)
|
2013-05-06 19:43:42 +00:00
|
|
|
-- TODO: g_loadFromGroupOnly?
|
|
|
|
local fd = ffiC.kopen4loadfrommod(fn, 0)
|
|
|
|
if (fd < 0) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return defs_c.readintostr(fd)
|
2012-08-02 10:52:17 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
|
2013-06-07 10:18:20 +00:00
|
|
|
local debug = require("debug")
|
|
|
|
|
|
|
|
-- Get the number of active locals in the function that calls the function
|
|
|
|
-- calling this one.
|
|
|
|
local function getnumlocals(l)
|
|
|
|
-- 200 is the max. number of locals at one level
|
|
|
|
for i=1,200 do
|
|
|
|
-- level:
|
|
|
|
-- 0 is getlocal() itself.
|
2013-06-20 18:31:47 +00:00
|
|
|
-- 1 is this function (getnumlocals).
|
2013-06-07 10:18:20 +00:00
|
|
|
-- 2 is the function calling getnumlocals()
|
|
|
|
-- 3 is the function calling that one.
|
|
|
|
if (debug.getlocal(3, i) == nil) then
|
|
|
|
return i-1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
local function error_on_nil_read(_, varname)
|
|
|
|
error("attempt to read nil variable '"..varname.."'", 2)
|
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
local required_module_mt = {
|
2013-07-04 19:38:42 +00:00
|
|
|
__index = error_on_nil_read,
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
__newindex = function()
|
|
|
|
error("modifying module table forbidden", 2)
|
|
|
|
end,
|
2013-07-04 19:38:42 +00:00
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
__metatable = true,
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Will contain a function to restore gamevars when running from savegame
|
|
|
|
-- restoration. See SAVEFUNC_ARGS for its arguments.
|
|
|
|
local g_restorefunc = nil
|
|
|
|
|
|
|
|
-- Local gamevar restoration function run from
|
|
|
|
-- our_require('end_gamevars') <- [user module].
|
|
|
|
local function restore_local(li, lval)
|
|
|
|
-- level:
|
|
|
|
-- 0 is getlocal() itself.
|
|
|
|
-- 1 is this function (restore_local).
|
|
|
|
-- 2 is the function calling restore_local(), the savecode.
|
|
|
|
-- 3 is the function calling the savecode, our_require.
|
|
|
|
-- 4 is the function calling our_require, the module function.
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
printf("Restoring index #%d (%s) with value %s",
|
|
|
|
li, debug.getlocal(4, li), tostring(lval))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(debug.setlocal(4, li, lval))
|
|
|
|
end
|
|
|
|
|
2012-07-13 18:20:38 +00:00
|
|
|
-- The "require" function accessible to Lunatic code.
|
|
|
|
-- Base modules in allowed_modules are wrapped so that they cannot be
|
2012-07-20 21:57:28 +00:00
|
|
|
-- modified, user modules are searched in the EDuke32 search
|
2013-01-13 16:40:21 +00:00
|
|
|
-- path. Also, our require
|
|
|
|
-- * never messes with the global environment, it only returns the module.
|
|
|
|
-- * allows passing varargs beyond the name to the module.
|
|
|
|
local function our_require(modname, ...)
|
2013-05-06 19:43:35 +00:00
|
|
|
local ERRLEV = 5
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
-- Check module name is valid first.
|
|
|
|
-- TODO: restrict valid names?
|
2013-05-19 19:29:16 +00:00
|
|
|
if (type(modname) ~= "string" or #modname==0) then
|
|
|
|
error("module name must be a nonempty string", 2)
|
2013-04-05 17:53:06 +00:00
|
|
|
end
|
|
|
|
|
2013-07-19 12:49:06 +00:00
|
|
|
-- For _LUNATIC_DBG
|
|
|
|
if (modname:match("^_LUNATIC") and ffiC._DEBUG_LUNATIC == 0) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
-- Handle the section between module() and require("end_gamevars").
|
|
|
|
if (modname == "end_gamevars") then
|
|
|
|
local thismodname = getcurmodname("require")
|
|
|
|
|
|
|
|
if (module_gamevars[thismodname] ~= nil) then
|
|
|
|
error("\"require 'end_gamevars'\" must be called at most once per require'd file", 2)
|
|
|
|
end
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
local gvnames = {}
|
|
|
|
|
|
|
|
for name in pairs(getfenv(2)) do
|
2013-06-07 10:18:17 +00:00
|
|
|
gvnames[#gvnames+1] = name
|
2013-04-05 17:53:06 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
printf("MODULE %s GAMEVAR %s", thismodname, name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module_gamevars[thismodname] = gvnames
|
2013-06-07 10:18:20 +00:00
|
|
|
local gvmodi = module_gvlocali[thismodname]
|
|
|
|
gvmodi[2] = getnumlocals()
|
|
|
|
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
2013-06-20 18:31:47 +00:00
|
|
|
local numlocals = gvmodi[2]-gvmodi[1]+1
|
|
|
|
if (numlocals > 0) then
|
|
|
|
printf("Module '%s' has %d locals, index %d to %d",
|
|
|
|
thismodname, numlocals, gvmodi[1], gvmodi[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Potentially restore gamevars.
|
|
|
|
if (g_restorefunc) then
|
|
|
|
local modtab = package_loaded[thismodname]
|
|
|
|
assert(type(modtab)=="table")
|
|
|
|
-- SAVEFUNC_ARGS.
|
|
|
|
g_restorefunc(thismodname, modtab, restore_local)
|
2013-06-07 10:18:20 +00:00
|
|
|
end
|
2013-04-05 17:53:06 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Return whether we're NOT running from a savegame restore in the
|
|
|
|
-- second outarg. (Lunatic-private!)
|
|
|
|
return nil, (g_restorefunc==nil)
|
2013-04-05 17:53:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- See whether it's a base module name.
|
2012-07-20 21:57:28 +00:00
|
|
|
if (allowed_modules[modname] ~= nil) then
|
|
|
|
return allowed_modules[modname]
|
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
--- Search user modules...
|
2012-07-13 18:20:38 +00:00
|
|
|
|
2013-06-09 16:37:19 +00:00
|
|
|
if (modname:find("[/\\]")) then
|
|
|
|
error("Module name must not contain directory separators", ERRLEV-1)
|
|
|
|
end
|
|
|
|
-- Instead, dots are translated to directory separators. For EDuke32's
|
2013-06-22 11:31:09 +00:00
|
|
|
-- virtual file system, this is always a forward slash. Keep the original
|
|
|
|
-- module name for passing to the module function.
|
|
|
|
local omodname = modname
|
2013-06-09 16:37:19 +00:00
|
|
|
modname = modname:gsub("%.", "/")
|
|
|
|
|
2013-02-28 17:30:04 +00:00
|
|
|
local omod = package_loaded[modname]
|
|
|
|
if (omod ~= nil) then
|
2013-04-05 17:53:06 +00:00
|
|
|
if (omod==false) then
|
2013-02-28 17:30:04 +00:00
|
|
|
error("Loop while loading modules", ERRLEV-1)
|
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
-- already loaded
|
2013-04-05 17:53:06 +00:00
|
|
|
assert(omod==true or type(omod)=="table")
|
2013-02-28 17:30:04 +00:00
|
|
|
return omod
|
2012-07-13 18:20:38 +00:00
|
|
|
end
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local modfn = modname .. ".lua"
|
2013-05-06 19:43:35 +00:00
|
|
|
local str = readintostr_mod(modfn)
|
2012-12-29 15:21:24 +00:00
|
|
|
if (str == nil) then
|
|
|
|
errorf(ERRLEV-1, "Couldn't open file \"%s\"", modfn)
|
|
|
|
end
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Implant code that yields the module thread just before it would return
|
|
|
|
-- otherwise.
|
|
|
|
str = str.."\nrequire('coroutine').yield()"
|
|
|
|
|
2013-05-24 13:54:25 +00:00
|
|
|
local modfunc, errmsg = loadstring(str, modfn)
|
2012-07-20 21:57:28 +00:00
|
|
|
if (modfunc == nil) then
|
2012-08-02 10:52:28 +00:00
|
|
|
errorf(ERRLEV-1, "Couldn't load \"%s\": %s", modname, errmsg)
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 17:53:06 +00:00
|
|
|
package_loaded[modname] = false -- 'not yet loaded'
|
2012-08-06 20:00:23 +00:00
|
|
|
table.insert(modname_stack, modname)
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Run the module code in a separate Lua thread!
|
|
|
|
local modthread = coroutine.create(modfunc)
|
2013-06-22 11:31:09 +00:00
|
|
|
local ok, retval = coroutine.resume(modthread, omodname, ...)
|
2013-06-20 18:31:47 +00:00
|
|
|
|
|
|
|
if (not ok) then
|
2013-06-30 20:38:39 +00:00
|
|
|
errorf(ERRLEV-1, "Failed running \"%s\": %s\n%s", modname,
|
|
|
|
retval, debug.traceback(modthread))
|
2013-06-20 18:31:47 +00:00
|
|
|
end
|
2012-07-20 21:57:37 +00:00
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
table.remove(modname_stack)
|
2012-07-20 21:57:37 +00:00
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
local modtab = package_loaded[modname]
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
if (type(modtab) ~= "table") then
|
|
|
|
-- The module didn't call our 'module'. Check if it returned a table.
|
|
|
|
-- In that case, the coroutine has finished its main function and has
|
|
|
|
-- not reached our implanted 'yield'.
|
|
|
|
if (coroutine.status(modthread)=="dead" and type(retval)=="table") then
|
|
|
|
modtab = retval
|
|
|
|
package_loaded[modname] = modtab
|
|
|
|
else
|
|
|
|
package_loaded[modname] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
if (type(modtab) == "table") then
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Protect module table in any case (i.e. either if the module used our
|
|
|
|
-- 'module' or if it returned a table).
|
2013-04-05 17:53:06 +00:00
|
|
|
setmetatable(modtab, required_module_mt)
|
2013-06-20 18:31:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local gvmodi = module_gvlocali[modname]
|
|
|
|
|
2013-07-19 12:49:05 +00:00
|
|
|
if (gvmodi and gvmodi[2] and gvmodi[2]>=gvmodi[1]) then
|
2013-06-20 18:31:47 +00:00
|
|
|
if (coroutine.status(modthread)=="suspended") then
|
|
|
|
-- Save off the suspended thread so that we may get its locals later on.
|
|
|
|
-- It is never resumed, but only ever used for debug.getlocal().
|
|
|
|
module_thread[modname] = modthread
|
|
|
|
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
printf("Keeping coroutine for module \"%s\"", modname)
|
|
|
|
end
|
|
|
|
end
|
2012-08-06 20:00:23 +00:00
|
|
|
end
|
2012-07-20 21:57:37 +00:00
|
|
|
|
|
|
|
return modtab
|
2012-07-13 18:20:38 +00:00
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-07-20 21:57:34 +00:00
|
|
|
local module_mt = {
|
2013-07-04 19:38:42 +00:00
|
|
|
__index = error_on_nil_read,
|
2012-07-20 21:57:34 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
-- Our 'module' replacement doesn't get the module name from the function args
|
|
|
|
-- since a malicious user could remove other loaded modules this way.
|
2013-04-05 17:53:06 +00:00
|
|
|
-- Also, our 'module' takes no varargs ("option functions" in Lua).
|
2012-08-06 20:00:23 +00:00
|
|
|
-- TODO: make transactional?
|
|
|
|
local function our_module()
|
2013-04-05 17:53:06 +00:00
|
|
|
if (#modname_stack == 0) then
|
2012-08-06 20:00:23 +00:00
|
|
|
error("'module' must be called at the top level of a require'd file", 2)
|
2013-04-05 17:53:06 +00:00
|
|
|
-- ... as opposed to "at runtime".
|
|
|
|
end
|
|
|
|
|
|
|
|
local modname = getcurmodname("module")
|
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
if (package_loaded[modname]) then
|
2013-04-05 17:53:06 +00:00
|
|
|
error("'module' must be called at most once per require'd file", 2)
|
2012-08-06 20:00:23 +00:00
|
|
|
end
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
local M = setmetatable({}, module_mt)
|
2012-07-20 21:57:28 +00:00
|
|
|
package_loaded[modname] = M
|
|
|
|
-- change the environment of the function which called us:
|
2012-08-06 20:00:23 +00:00
|
|
|
setfenv(2, M)
|
2013-06-07 10:18:20 +00:00
|
|
|
|
|
|
|
module_gvlocali[modname] = { getnumlocals()+1 }
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:31 +00:00
|
|
|
-- overridden 'error' that always passes a string to the base 'error'
|
|
|
|
local function our_error(errmsg, level)
|
|
|
|
if (type(errmsg) ~= "string") then
|
2012-08-06 20:00:23 +00:00
|
|
|
error("error using 'error': error message must be a string", 2)
|
2012-07-20 21:57:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (level) then
|
|
|
|
if (type(level) ~= "number") then
|
2012-08-06 20:00:23 +00:00
|
|
|
error("error using 'error': error level must be a number", 2)
|
2012-07-20 21:57:31 +00:00
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
error(errmsg, level==0 and 0 or level+1)
|
2012-07-20 21:57:31 +00:00
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
error(errmsg, 2)
|
2012-07-20 21:57:31 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
|
|
|
|
-- _G tweaks -- pull in only 'safe' stuff
|
|
|
|
local G_ = {} -- our soon-to-be global environment
|
|
|
|
|
2011-10-11 16:53:15 +00:00
|
|
|
G_.assert = assert
|
2012-07-20 21:57:31 +00:00
|
|
|
G_.error = our_error
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.ipairs = ipairs
|
2011-10-11 16:53:15 +00:00
|
|
|
G_.pairs = pairs
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.pcall = pcall
|
2012-12-25 16:13:41 +00:00
|
|
|
G_.print = print
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.module = our_module
|
2011-10-11 16:53:15 +00:00
|
|
|
G_.next = next
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.require = our_require
|
2011-10-11 16:53:15 +00:00
|
|
|
G_.select = select
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.tostring = tostring
|
|
|
|
G_.tonumber = tonumber
|
2011-10-11 16:53:15 +00:00
|
|
|
G_.type = type
|
2012-08-06 20:00:23 +00:00
|
|
|
G_.unpack = unpack
|
|
|
|
G_.xpcall = xpcall
|
|
|
|
G_._VERSION = _VERSION
|
|
|
|
|
|
|
|
-- Available through our 'require':
|
|
|
|
-- bit, coroutine, math, string, table
|
|
|
|
|
|
|
|
-- Not available:
|
|
|
|
-- collectgarbage, debug, dofile, gcinfo (DEPRECATED), getfenv, getmetatable,
|
|
|
|
-- jit, load, loadfile, loadstring, newproxy (NOT STD?), package, rawequal,
|
|
|
|
-- rawget, rawset, setfenv, setmetatable
|
2011-10-11 16:53:15 +00:00
|
|
|
|
|
|
|
G_._G = G_
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
-- Chain together two functions taking 3 input args.
|
|
|
|
local function chain_func3(func1, func2)
|
|
|
|
if (func1==nil or func2==nil) then
|
|
|
|
return assert(func1 or func2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return a function that runs <func1> first and then tail-calls <func2>.
|
|
|
|
return function(aci, pli, dist)
|
|
|
|
func1(aci, pli, dist)
|
|
|
|
return func2(aci, pli, dist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
-- Determines the last numeric index of a table using *pairs*, so that in
|
|
|
|
-- arg-lists with "holes" (e.g. {1, 2, nil, function() end}) are handled
|
|
|
|
-- properly.
|
2013-06-12 17:49:53 +00:00
|
|
|
local function ourmaxn(tab)
|
|
|
|
local maxi = 0
|
2013-06-30 20:38:39 +00:00
|
|
|
for i in pairs(tab) do
|
|
|
|
if (type(i)=="number") then
|
|
|
|
maxi = math.max(i, maxi)
|
|
|
|
end
|
2013-06-12 17:49:53 +00:00
|
|
|
end
|
|
|
|
return maxi
|
|
|
|
end
|
|
|
|
|
2013-05-30 18:10:54 +00:00
|
|
|
-- Running for the very first time?
|
|
|
|
local g_firstRun = (ffiC.g_elCONSize == 0)
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
-- Actor functions, saved for actor chaining
|
|
|
|
local actor_funcs = {}
|
2013-07-07 20:59:10 +00:00
|
|
|
-- Event functions, saved for event chaining
|
|
|
|
local event_funcs = {}
|
|
|
|
|
|
|
|
-- Per-actor sprite animation callbacks
|
|
|
|
local animsprite_funcs = {}
|
2013-03-31 18:58:04 +00:00
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
local gameactor_internal = gameactor_internal -- included in lunatic.c
|
2013-07-07 20:59:10 +00:00
|
|
|
local gameevent_internal = gameevent_internal -- included in lunatic.c
|
|
|
|
|
|
|
|
local function animate_all_sprites()
|
|
|
|
for i=0,ffiC.spritesortcnt-1 do
|
|
|
|
local tspr = ffiC.tsprite[i]
|
|
|
|
|
|
|
|
if (tspr.owner < ffiC.MAXSPRITES+0ULL) then
|
|
|
|
local spr = tspr:getspr()
|
|
|
|
local animfunc = animsprite_funcs[spr.picnum]
|
|
|
|
|
|
|
|
if (animfunc) then
|
|
|
|
animfunc(tspr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
-- gameactor{tilenum [, flags [, strength [, action [, move [, movflags]]]]], func}
|
|
|
|
-- Every arg may be positional OR key=val (with the name indicated above as key),
|
|
|
|
-- but not both.
|
|
|
|
local function our_gameactor(args)
|
2013-03-03 16:06:37 +00:00
|
|
|
bcheck.top_level("gameactor")
|
2012-12-25 16:13:45 +00:00
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
if (type(args)~="table") then
|
|
|
|
error("invalid gameactor call: must be passed a table")
|
|
|
|
end
|
|
|
|
|
|
|
|
local tilenum = args[1]
|
2012-12-23 19:24:16 +00:00
|
|
|
if (type(tilenum) ~= "number") then
|
|
|
|
error("invalid argument #1 to gameactor: must be a number", 2)
|
|
|
|
end
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (tilenum >= 0 and tilenum < ffiC.MAXTILES)) then
|
2013-03-24 18:53:28 +00:00
|
|
|
error("invalid argument #1 to gameactor: must be a tile number [0..gv.MAXTILES-1]", 2)
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
2013-03-24 18:53:28 +00:00
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
local lastargi = ourmaxn(args)
|
|
|
|
local func = args[lastargi]
|
|
|
|
if (type(func) ~= "function") then
|
|
|
|
func = args.func
|
|
|
|
lastargi = 1/0
|
2012-12-25 16:13:45 +00:00
|
|
|
end
|
2013-06-12 17:49:53 +00:00
|
|
|
if (type(func) ~= "function") then
|
2013-07-04 19:38:42 +00:00
|
|
|
error("invalid gameactor call: must provide a function with last numeric arg or .func", 2)
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
local flags = (lastargi > 2 and args[2]) or args.flags or 0
|
2013-03-24 18:53:28 +00:00
|
|
|
if (type(flags) ~= "number") then
|
2013-06-12 17:49:53 +00:00
|
|
|
error("invalid 'flags' argument (#2) to gameactor: must be a number", 2)
|
2013-03-24 18:53:28 +00:00
|
|
|
end
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
local AF = actor.FLAGS
|
2013-07-04 19:38:42 +00:00
|
|
|
local chainflags = band(flags, AF._CHAIN_MASK_ACTOR)
|
|
|
|
flags = band(flags, BNOT.CHAIN_MASK_ACTOR)
|
2013-03-31 18:58:04 +00:00
|
|
|
|
2013-05-30 18:10:54 +00:00
|
|
|
-- Default chaining behavior: don't, replace the old actor instead, but
|
|
|
|
-- unlike CON, also replace its flags. (CON ORs them instead.)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (chainflags == 0) then
|
2013-05-30 18:10:54 +00:00
|
|
|
chainflags = AF.replace_hard
|
2013-07-04 19:38:42 +00:00
|
|
|
elseif (band(chainflags, chainflags-1) ~= 0) then
|
2013-05-30 18:10:54 +00:00
|
|
|
error("invalid chaining control flags to gameactor", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
|
|
|
|
2013-05-30 18:10:54 +00:00
|
|
|
local replacep = (chainflags==AF.replace_soft or chainflags==AF.replace_hard)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (not replacep and not actor_funcs[tilenum]) then
|
|
|
|
error("attempt to chain code to nonexistent actor tile "..tilenum, 2)
|
|
|
|
end
|
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
local flags_rbits = band(flags, BNOT.USER_MASK)
|
2013-03-24 18:53:28 +00:00
|
|
|
if (flags_rbits ~= 0) then
|
2013-06-12 17:49:53 +00:00
|
|
|
error("invalid 'flags' argument (#2) to gameactor: must not set reserved bits (0x"
|
2013-03-24 18:53:28 +00:00
|
|
|
..(bit.tohex(flags_rbits))..")", 2)
|
|
|
|
end
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
local strength = ((lastargi > 3 and args[3]) or args.strength) or (replacep and 0 or nil)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (replacep or strength~=nil) then
|
|
|
|
if (type(strength) ~= "number") then
|
2013-06-12 17:49:53 +00:00
|
|
|
error("invalid 'strength' argument (#3) to gameactor: must be a number", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
local act = ((lastargi > 4 and args[4]) or args.action) or (replacep and literal_act[0] or nil)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (replacep or act ~= nil) then
|
2013-06-30 20:38:39 +00:00
|
|
|
if (type(act)=="number" and (act==0 or act==1)) then
|
|
|
|
act = literal_act[act]
|
|
|
|
elseif (not ffi.istype(con_action_ct, act)) then
|
|
|
|
error("invalid 'action' argument (#4) to gameactor: must be an action", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2013-06-30 20:38:39 +00:00
|
|
|
local mov = ((lastargi > 5 and args[5]) or args.move) or (replacep and literal_mov[0] or nil)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (replacep or mov ~= nil) then
|
2013-06-30 20:38:39 +00:00
|
|
|
if (type(mov)=="number" and (mov==0 or mov==1)) then
|
|
|
|
mov = literal_mov[mov]
|
|
|
|
elseif (not ffi.istype(con_move_ct, mov)) then
|
|
|
|
error("invalid 'move' argument (#5) to gameactor: must be a move", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
local movflags = ((lastargi > 6 and args[6]) or args.movflags) or (replacep and 0 or nil)
|
2013-03-31 18:58:04 +00:00
|
|
|
if (replacep or movflags ~= nil) then
|
|
|
|
if (type(movflags) ~= "number") then
|
2013-06-12 17:49:53 +00:00
|
|
|
error("invalid 'movflags' argument (#6) to gameactor: must be a number", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2013-07-07 20:59:10 +00:00
|
|
|
-- Register a potentially passed drawn sprite animation callback function.
|
|
|
|
-- TODO: allow registering without main actor execution callback.
|
|
|
|
local animfunc = args.animate
|
|
|
|
if (animfunc ~= nil) then
|
|
|
|
if (type(animfunc) ~= "function") then
|
|
|
|
error("invalid 'animate' argument to gameactor: must be a function", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
animsprite_funcs[tilenum] = replacep and func
|
|
|
|
or (chainflags==AF.chain_beg) and chain_func3(animfunc, animsprite_funcs[tilenum])
|
|
|
|
or (chainflags==AF.chain_end) and chain_func3(animsprite_funcs[tilenum], animfunc)
|
|
|
|
or assert(false)
|
|
|
|
|
|
|
|
-- Register our EVENT_ANIMATEALLSPRITES only now so that it is not
|
|
|
|
-- called if there are no 'animate' definitions.
|
|
|
|
gameevent_internal(95, animate_all_sprites) -- EVENT_ANIMATEALLSPRITES
|
|
|
|
end
|
|
|
|
|
2013-05-30 18:10:54 +00:00
|
|
|
-- All good, set the tile bits in initial run and register the actor!
|
|
|
|
-- XXX: Need to see if some other state living on the C side is clobbered
|
|
|
|
-- by Lua state recreation.
|
|
|
|
if (g_firstRun) then
|
|
|
|
-- NOTE: when chaining, this allows passing different flags which are
|
|
|
|
-- silently ORed. This may or may not be what the user intends, but it
|
|
|
|
-- allows e.g. adding a stayput bit to an already defined enemy.
|
|
|
|
-- Modifying existing behavior is the whole point of chaining after all.
|
|
|
|
-- When soft-replacing, bitwise OR too, emulating CON behavior.
|
|
|
|
local tile = ffiC.g_tile[tilenum]
|
2013-07-09 18:23:41 +00:00
|
|
|
tile._flags = (chainflags==AF.replace_hard) and flags or bit.bor(tile._flags, flags)
|
2013-05-30 18:10:54 +00:00
|
|
|
end
|
2013-03-24 18:53:28 +00:00
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
local newfunc = replacep and func
|
|
|
|
or (chainflags==AF.chain_beg) and chain_func3(func, actor_funcs[tilenum])
|
|
|
|
or (chainflags==AF.chain_end) and chain_func3(actor_funcs[tilenum], func)
|
|
|
|
or assert(false)
|
|
|
|
|
|
|
|
gameactor_internal(tilenum, strength, act, mov, movflags, newfunc)
|
|
|
|
actor_funcs[tilenum] = newfunc
|
2012-12-23 19:24:16 +00:00
|
|
|
end
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
-- gameevent{<event idx or string> [, flags], <event function>}
|
|
|
|
local function our_gameevent(args)
|
2013-06-12 17:49:51 +00:00
|
|
|
bcheck.top_level("gameevent")
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
if (type(args)~="table") then
|
|
|
|
error("invalid gameevent call: must be passed a table")
|
|
|
|
end
|
|
|
|
|
|
|
|
local event = args[1]
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
if (type(event) == "string") then
|
|
|
|
if (event:sub(1,6) ~= "EVENT_") then
|
|
|
|
event = "EVENT_"..event
|
|
|
|
end
|
2013-01-06 18:56:45 +00:00
|
|
|
local eventidx = con_lang.EVENT[event]
|
2012-12-25 16:13:45 +00:00
|
|
|
if (eventidx == nil) then
|
|
|
|
errorf(2, "gameevent: invalid event label %q", event)
|
|
|
|
end
|
|
|
|
event = eventidx
|
|
|
|
end
|
|
|
|
if (type(event) ~= "number") then
|
2013-03-31 18:58:04 +00:00
|
|
|
error("invalid argument #1 to gameevent: must be a number or event label", 2)
|
2012-12-25 16:13:45 +00:00
|
|
|
end
|
2013-07-19 12:49:02 +00:00
|
|
|
if (not (event >= 0 and event < con_lang.MAXEVENTS)) then
|
2012-12-25 16:13:45 +00:00
|
|
|
error("invalid argument #1 to gameevent: must be an event number (0 .. MAXEVENTS-1)", 2)
|
|
|
|
end
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
local AF = actor.FLAGS
|
2012-12-25 16:13:45 +00:00
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
-- Kind of CODEDUP from our_gameactor.
|
|
|
|
local lastargi = ourmaxn(args)
|
|
|
|
local func = args[lastargi]
|
|
|
|
if (type(func) ~= "function") then
|
|
|
|
func = args.func
|
|
|
|
lastargi = 1/0
|
|
|
|
end
|
|
|
|
if (type(func) ~= "function") then
|
|
|
|
error("invalid gameevent call: must provide a function with last numeric arg or .func", 2)
|
|
|
|
end
|
|
|
|
|
2013-03-31 18:58:04 +00:00
|
|
|
-- Event chaining: in Lunatic, chaining at the *end* is the default.
|
2013-06-12 17:49:53 +00:00
|
|
|
local flags = (lastargi > 2 and args[2]) or args.flags or AF.chain_end
|
|
|
|
if (type(flags) ~= "number") then
|
|
|
|
error("invalid 'flags' argument (#2) to gameevent: must be a number", 2)
|
2012-12-25 16:13:45 +00:00
|
|
|
end
|
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
if (band(flags, BNOT.CHAIN_MASK_EVENT) ~= 0) then
|
2013-06-12 17:49:53 +00:00
|
|
|
error("invalid 'flags' argument to gameevent: must not set reserved bits", 2)
|
2013-03-31 18:58:04 +00:00
|
|
|
end
|
|
|
|
|
2013-06-12 17:49:53 +00:00
|
|
|
local newfunc = (flags==AF.replace) and func
|
2013-03-31 18:58:04 +00:00
|
|
|
or (flags==AF.chain_beg) and chain_func3(func, event_funcs[event])
|
|
|
|
or (flags==AF.chain_end) and chain_func3(event_funcs[event], func)
|
|
|
|
or assert(false)
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
gameevent_internal(event, newfunc)
|
|
|
|
event_funcs[event] = newfunc
|
|
|
|
end
|
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
--- non-default data and functions
|
2012-12-25 16:13:45 +00:00
|
|
|
G_.gameevent = our_gameevent
|
2012-12-23 19:24:16 +00:00
|
|
|
G_.gameactor = our_gameactor
|
2013-02-07 21:01:03 +00:00
|
|
|
-- These come from above:
|
|
|
|
G_.player = player
|
|
|
|
G_.actor = actor
|
|
|
|
G_.projectile = projectile
|
2013-03-15 16:56:11 +00:00
|
|
|
G_.g_tile = g_tile
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2013-11-11 16:48:23 +00:00
|
|
|
-- A table that can be used for temporary data when debugging from the OSD.
|
|
|
|
G_.d = {}
|
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2013-05-19 19:29:21 +00:00
|
|
|
---=== Lunatic translator setup ===---
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2013-05-19 19:29:21 +00:00
|
|
|
read_into_string = readintostr_mod -- for lunacon
|
|
|
|
local lunacon = require("lunacon")
|
|
|
|
|
|
|
|
local concode, lineinfo
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2013-05-19 19:29:13 +00:00
|
|
|
--- Get Lua code for CON (+ mutator) code.
|
2013-05-19 19:29:16 +00:00
|
|
|
if (g_firstRun) then
|
2013-05-19 19:29:13 +00:00
|
|
|
-- Compiling CON for the first time.
|
2013-01-13 16:40:07 +00:00
|
|
|
local confn = { ffi.string(ffiC.G_ConFile()) }
|
|
|
|
|
|
|
|
local nummods = ffiC.g_scriptModulesNum
|
|
|
|
if (nummods > 0) then
|
|
|
|
assert(ffiC.g_scriptModules ~= nil)
|
|
|
|
|
|
|
|
for i=1,nummods do
|
|
|
|
confn[i+1] = ffi.string(ffiC.g_scriptModules[i-1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-03 16:06:16 +00:00
|
|
|
concode, lineinfo = lunacon.compile(confn)
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
if (concode == nil) then
|
|
|
|
error("Failure compiling CON code, exiting.", 0)
|
|
|
|
end
|
2013-03-13 10:48:23 +00:00
|
|
|
assert(lineinfo)
|
2013-03-03 16:06:16 +00:00
|
|
|
|
2013-05-19 19:29:13 +00:00
|
|
|
-- Back up the translated code on the C side.
|
|
|
|
assert(type(concode)=="string")
|
|
|
|
ffiC.El_SetCON(concode)
|
|
|
|
else
|
|
|
|
-- CON was already compiled.
|
|
|
|
concode = ffi.string(ffiC.g_elCON, ffiC.g_elCONSize)
|
2013-05-19 19:29:21 +00:00
|
|
|
lineinfo = lunacon.get_lineinfo(concode)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
-- XXX: lineinfo of 2nd up time has one line less.
|
|
|
|
printf("CON line info has %d Lua lines", #lineinfo.llines)
|
2013-05-19 19:29:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
do
|
2013-03-03 16:06:16 +00:00
|
|
|
-- Translate one Lua line number to a CON file name + line number
|
|
|
|
local function transline(lnum)
|
|
|
|
return string.format("%s:%d", lineinfo:getfline(tonumber(lnum)))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Register the function that tweaks an error message, looking out for
|
|
|
|
-- errors from CON and translating the line numbers.
|
|
|
|
local function tweak_traceback_msg(errmsg)
|
|
|
|
return errmsg:gsub('%[string "CON"%]:([0-9]+)', transline)
|
|
|
|
end
|
|
|
|
|
2013-11-08 18:08:42 +00:00
|
|
|
lprivate.tweak_traceback_msg = tweak_traceback_msg
|
|
|
|
|
2013-03-03 16:06:16 +00:00
|
|
|
set_tweak_traceback_internal(tweak_traceback_msg)
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2013-05-20 19:31:42 +00:00
|
|
|
-- XXX: May still be require'd from user code, we don't want that (at least not
|
|
|
|
-- under this name).
|
|
|
|
local CON_MODULE_NAME = "_CON\0"
|
|
|
|
|
|
|
|
-- Set up Lunatic gamevar serialization.
|
|
|
|
do
|
|
|
|
local savegame = require("savegame")
|
|
|
|
|
2013-10-27 21:12:22 +00:00
|
|
|
-- Callback for: const char *(int32_t *slenptr, int32_t levelnum);
|
|
|
|
ffiC.El_SerializeGamevars = function(slenptr, levelnum)
|
2013-05-20 19:31:42 +00:00
|
|
|
local sb = savegame.savebuffer()
|
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Module name, module table, restore_local. See SAVEFUNC_ARGS.
|
|
|
|
sb:addraw("local N,M,F=...")
|
|
|
|
-- A local to temporarily hold module locals.
|
|
|
|
sb:addraw("local L")
|
2013-05-20 19:31:42 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- XXX: System gamevars? Most of them ought to be saved with C data.
|
2013-05-20 19:31:42 +00:00
|
|
|
for modname, modvars in pairs(module_gamevars) do
|
2013-06-28 14:07:39 +00:00
|
|
|
sb:startmod(modname)
|
2013-06-20 18:31:47 +00:00
|
|
|
|
|
|
|
-- Handle global gamevars first.
|
2013-05-20 19:31:42 +00:00
|
|
|
for i=1,#modvars do
|
|
|
|
local varname = modvars[i]
|
2013-11-08 18:08:37 +00:00
|
|
|
local excludedVars = modname==CON_MODULE_NAME and varname=="_V" and
|
|
|
|
package_loaded[CON_MODULE_NAME]._V._IS_NORESET_GAMEVAR or nil
|
|
|
|
|
2013-05-20 19:31:42 +00:00
|
|
|
-- Serialize gamevar named 'varname' from module named 'modname'.
|
|
|
|
-- XXX: May error. This will terminate EDuke32 since this callback
|
|
|
|
-- is run unprotected.
|
2013-11-08 18:08:37 +00:00
|
|
|
if (sb:add("M."..varname, package_loaded[modname][varname], excludedVars)) then
|
2013-05-20 19:31:42 +00:00
|
|
|
-- We couldn't serialize that gamevar.
|
|
|
|
slenptr[0] = -1
|
|
|
|
-- Signal which gamevar that was.
|
|
|
|
return (modname==CON_MODULE_NAME and "<CON>" or modname).."."..varname
|
|
|
|
end
|
|
|
|
end
|
2013-06-20 18:31:47 +00:00
|
|
|
|
|
|
|
local modthread = module_thread[modname]
|
|
|
|
|
|
|
|
if (modthread) then
|
|
|
|
-- Handle local gamevars.
|
|
|
|
local gvmodi = module_gvlocali[modname]
|
|
|
|
|
|
|
|
for li=gvmodi[1],gvmodi[2] do
|
|
|
|
-- Serialize local with index <li>. Get its value first.
|
|
|
|
local lname, lval = debug.getlocal(modthread, 1, li)
|
|
|
|
|
|
|
|
if (sb:add("L", lval)) then
|
|
|
|
-- We couldn't serialize that gamevar.
|
|
|
|
slenptr[0] = -1
|
|
|
|
return "local "..modname.."."..lname
|
|
|
|
end
|
|
|
|
|
2013-10-27 21:12:20 +00:00
|
|
|
-- Emit code to call restore_local.
|
2013-06-20 18:31:47 +00:00
|
|
|
sb:addrawf("F(%d,L)", li)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-28 14:07:39 +00:00
|
|
|
sb:endmod()
|
2013-05-20 19:31:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the whole code as a string.
|
|
|
|
local savecode = sb:getcode()
|
|
|
|
|
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
-- Dump the code if Lunatic debugging is enabled and there is a
|
|
|
|
-- LUNATIC_SAVECODE_FN variable in the environment.
|
|
|
|
local os = require("os")
|
|
|
|
local fn = os.getenv("LUNATIC_SAVECODE_FN")
|
|
|
|
|
|
|
|
if (fn ~= nil) then
|
2013-10-27 21:12:22 +00:00
|
|
|
if (levelnum >= 0) then
|
|
|
|
fn = fn .. levelnum
|
|
|
|
end
|
|
|
|
|
2013-05-20 19:31:42 +00:00
|
|
|
local io = require("io")
|
|
|
|
local f = io.open(fn, "w")
|
|
|
|
|
|
|
|
if (f ~= nil) then
|
|
|
|
f:write(savecode)
|
|
|
|
f:close()
|
|
|
|
printf("Wrote Lunatic gamevar restoration code to \"%s\".", fn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set the size of the code and return the code to C.
|
|
|
|
slenptr[0] = #savecode
|
|
|
|
return savecode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
-- change the environment of this chunk to the table G_
|
2012-07-13 18:20:35 +00:00
|
|
|
-- NOTE: all references to global variables from this point on
|
|
|
|
-- (also in functions created after this point) refer to G_ !
|
2012-05-13 16:04:28 +00:00
|
|
|
setfenv(1, G_)
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
-- Print keys and values of a table.
|
2012-05-13 16:04:28 +00:00
|
|
|
local function printkv(label, table)
|
2012-10-07 15:26:13 +00:00
|
|
|
print("========== Keys and values of "..label.." ("..tostring(table)..")")
|
2012-08-06 20:00:23 +00:00
|
|
|
for k,v in pairs(table) do
|
2012-12-29 15:21:24 +00:00
|
|
|
print(k .. ": " .. tostring(v))
|
2012-05-13 16:04:28 +00:00
|
|
|
end
|
2012-12-29 15:21:24 +00:00
|
|
|
print("----------")
|
2012-05-13 16:04:28 +00:00
|
|
|
end
|
|
|
|
|
2012-05-29 20:01:58 +00:00
|
|
|
--printkv('_G AFTER SETFENV', _G)
|
2012-05-13 16:04:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
---=== Restricted access to C variables from Lunatic ===---
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-01-28 14:38:41 +00:00
|
|
|
-- error(..., 2) is to blame the caller and get its line numbers
|
2012-01-28 14:37:24 +00:00
|
|
|
|
2013-07-04 19:38:42 +00:00
|
|
|
-- Map of 'gv' variable names to C ones.
|
|
|
|
local varnameMap = {
|
|
|
|
gametic = "g_moveThingsCount",
|
2013-08-23 17:01:10 +00:00
|
|
|
RETURN = "g_RETURN",
|
2013-10-20 12:35:06 +00:00
|
|
|
_sessionVar = "g_elSessionVar",
|
2013-07-04 19:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gv_access.gametic = true
|
2013-08-23 17:01:10 +00:00
|
|
|
gv_access.RETURN = true
|
2013-10-20 12:35:06 +00:00
|
|
|
gv_access._sessionVar = true
|
2013-07-04 19:38:42 +00:00
|
|
|
|
2011-09-25 15:11:47 +00:00
|
|
|
local tmpmt = {
|
2013-03-24 18:53:28 +00:00
|
|
|
__index = function(_, key)
|
2013-01-19 18:28:40 +00:00
|
|
|
if (gv_access[key] == nil) then
|
2013-07-04 19:38:42 +00:00
|
|
|
-- Read access allowed.
|
2013-01-19 18:28:40 +00:00
|
|
|
return ffiC[key]
|
2013-07-04 19:38:42 +00:00
|
|
|
elseif (type(gv_access[key])~="boolean") then
|
|
|
|
-- Overridden 'gv' pseudo-member...
|
2013-01-19 18:28:40 +00:00
|
|
|
return gv_access[key]
|
2013-07-04 19:38:42 +00:00
|
|
|
elseif (varnameMap[key]) then
|
|
|
|
-- Variable known under a different name on the C side.
|
|
|
|
return ffiC[varnameMap[key]]
|
2013-01-19 18:28:40 +00:00
|
|
|
end
|
|
|
|
error("access forbidden", 2)
|
|
|
|
end,
|
2013-03-13 10:48:14 +00:00
|
|
|
|
2013-03-24 18:53:28 +00:00
|
|
|
__newindex = function(_, key, val)
|
2013-03-24 18:52:41 +00:00
|
|
|
if (gv_access[key] == nil) then
|
2013-08-23 17:01:09 +00:00
|
|
|
-- Variables declared 'const' are handled by LuaJIT.
|
2013-03-13 10:48:14 +00:00
|
|
|
ffiC[key] = val
|
2013-08-23 17:01:10 +00:00
|
|
|
elseif (varnameMap[key]) then
|
|
|
|
ffiC[varnameMap[key]] = val
|
2013-03-13 10:48:14 +00:00
|
|
|
else
|
|
|
|
error("write access forbidden", 2)
|
|
|
|
end
|
|
|
|
end,
|
2011-09-25 15:11:47 +00:00
|
|
|
}
|
2013-03-24 18:53:28 +00:00
|
|
|
gv = setmtonce(gv_, tmpmt)
|
2011-09-25 15:11:47 +00:00
|
|
|
|
2012-10-07 15:26:13 +00:00
|
|
|
-- This will create 'sprite', 'wall', etc. HERE, i.e. in the environment of this chunk
|
|
|
|
defs_c.create_globals(_G)
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
-- REMOVE this for release
|
2013-07-19 12:49:06 +00:00
|
|
|
if (ffiC._DEBUG_LUNATIC ~= 0) then
|
|
|
|
local DBG_ = {}
|
|
|
|
DBG_.debug = require("debug")
|
|
|
|
DBG_.printkv = printkv
|
|
|
|
DBG_.loadstring = loadstring
|
|
|
|
DBG_.oom = function()
|
|
|
|
local s = "1"
|
|
|
|
for i=1,math.huge do
|
|
|
|
s = s..s
|
|
|
|
end
|
2013-01-06 18:56:45 +00:00
|
|
|
end
|
2012-04-22 19:10:29 +00:00
|
|
|
|
2013-07-19 12:49:06 +00:00
|
|
|
-- Test reading from all struct members
|
|
|
|
DBG_.testmembread = function()
|
|
|
|
for _1, sac in pairs { con_lang.StructAccessCode, con_lang.StructAccessCode2 } do
|
|
|
|
for what, labels in pairs(sac) do
|
|
|
|
if (what~="tspr") then
|
|
|
|
for _3, membaccode in pairs(labels) do
|
|
|
|
if (type(membaccode)=="table") then
|
|
|
|
membaccode = membaccode[1]
|
|
|
|
end
|
|
|
|
if (membaccode) then
|
|
|
|
local codestr = [[
|
2013-04-07 15:20:28 +00:00
|
|
|
do
|
|
|
|
local _bit,_math=require'bit',require'math'
|
|
|
|
local _band,_gv=_bit.band,gv
|
|
|
|
local tmp=]]..
|
2013-02-10 16:24:32 +00:00
|
|
|
membaccode:gsub("%%s","0").." end"
|
2013-07-19 12:49:06 +00:00
|
|
|
local code = assert(loadstring(codestr))
|
|
|
|
code()
|
|
|
|
end
|
2013-02-10 16:24:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-19 12:49:06 +00:00
|
|
|
allowed_modules._LUNATIC_DBG = DBG_
|
|
|
|
end
|
2012-04-22 19:10:29 +00:00
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
---=== Finishing environment setup ===---
|
2011-12-21 18:43:08 +00:00
|
|
|
|
2012-05-29 20:01:58 +00:00
|
|
|
--printkv('_G AFTER DECLS', _G)
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2013-09-05 17:37:42 +00:00
|
|
|
local index_error_mt = {
|
|
|
|
__index = function(_, key)
|
|
|
|
error("attempt to read undeclared variable '"..key.."'", 2)
|
|
|
|
end,
|
|
|
|
|
|
|
|
__metatable = true,
|
|
|
|
}
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2012-02-04 14:29:59 +00:00
|
|
|
-- PiL 14.2 continued
|
2012-05-13 16:04:28 +00:00
|
|
|
-- We need this at the end because we were previously doing just that!
|
2013-09-05 17:37:42 +00:00
|
|
|
setmetatable(G_, index_error_mt)
|
2012-01-28 14:37:24 +00:00
|
|
|
|
2013-07-19 12:49:07 +00:00
|
|
|
local global_mt = {
|
|
|
|
__index = G_,
|
|
|
|
|
|
|
|
__newindex = function()
|
|
|
|
error("attempt to write into the global environment")
|
|
|
|
end,
|
|
|
|
|
|
|
|
__metatable = true,
|
|
|
|
}
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
-- Change the environment of the running Lua thread so that everything
|
|
|
|
-- what we've set up will be available when this chunk is left.
|
2013-02-10 16:24:20 +00:00
|
|
|
-- In particular, we need the globals defined after setting this chunk's
|
2012-05-13 16:04:28 +00:00
|
|
|
-- environment earlier.
|
2013-07-19 12:49:07 +00:00
|
|
|
setfenv(0, setmetatable({}, global_mt))
|
2012-12-29 15:21:24 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
do
|
|
|
|
-- If we're running from a savegame restoration, create the restoration
|
|
|
|
-- function. Must be here, after the above setfenv(), because it must be
|
|
|
|
-- created in this protected ('user') context!
|
|
|
|
local cstr = ffiC.g_elSavecode
|
|
|
|
if (cstr~=nil) then
|
|
|
|
local restorecode = ffi.string(cstr)
|
|
|
|
ffiC.El_FreeSaveCode()
|
|
|
|
|
|
|
|
g_restorefunc = assert(loadstring(restorecode))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-27 21:12:22 +00:00
|
|
|
-- Restore CON gamevars from loadmapstate.
|
|
|
|
-- TODO: non-user-defined gamevars.
|
|
|
|
-- TODO: savegames.
|
|
|
|
-- int32_t El_RestoreGamevars(const char *)
|
|
|
|
ffiC.El_RestoreGamevars = function(savecode)
|
|
|
|
savecode = ffi.string(savecode)
|
|
|
|
local restorefunc = assert(loadstring(savecode))
|
|
|
|
restorefunc(CON_MODULE_NAME, package_loaded[CON_MODULE_NAME], nil)
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2013-05-19 19:29:16 +00:00
|
|
|
-- Run the CON code translated into Lua.
|
2012-12-29 15:21:24 +00:00
|
|
|
if (concode) then
|
|
|
|
local confunc, conerrmsg = loadstring(concode, "CON")
|
|
|
|
if (confunc == nil) then
|
2013-03-13 10:48:23 +00:00
|
|
|
error("Failure loading translated CON code: "..conerrmsg, 0)
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2013-05-20 19:31:42 +00:00
|
|
|
|
2013-06-20 18:31:47 +00:00
|
|
|
-- Emulate our 'require' for the CON module when running it, for
|
|
|
|
-- our_module() which is called from the generated Lua code.
|
|
|
|
table.insert(modname_stack, CON_MODULE_NAME)
|
2013-06-30 20:38:41 +00:00
|
|
|
local conlabels, conaction, conmove, conai = confunc()
|
2013-06-20 18:31:47 +00:00
|
|
|
table.remove(modname_stack)
|
2013-06-12 17:49:47 +00:00
|
|
|
|
2013-06-30 20:38:41 +00:00
|
|
|
local function protect_con_table(tab)
|
2013-09-05 17:37:42 +00:00
|
|
|
-- NOTE: Some of our code specifically excepts the name tables to be
|
|
|
|
-- indexable with nonexistent keys. See e.g. control.c: _A_SpawnGlass()
|
2013-09-05 17:37:47 +00:00
|
|
|
if (ffiC._LUNATIC_STRICT ~= 0) then
|
|
|
|
tab = setmetatable(tab, index_error_mt)
|
|
|
|
end
|
2013-06-30 20:38:41 +00:00
|
|
|
return setmtonce({}, { __index=tab, __newindex=basemod_newidx })
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set up CON.* modules, providing access to diffenrent kinds of labels
|
|
|
|
-- defined in CON from Lua. See CONCODE_RETURN in lunacon.lua.
|
|
|
|
allowed_modules["CON.DEFS"] = protect_con_table(conlabels)
|
|
|
|
allowed_modules["CON.ACTION"] = protect_con_table(conaction)
|
|
|
|
allowed_modules["CON.MOVE"] = protect_con_table(conmove)
|
|
|
|
allowed_modules["CON.AI"] = protect_con_table(conai)
|
2013-06-12 17:49:49 +00:00
|
|
|
|
|
|
|
-- Propagate potentially remapped defines to the control module.
|
|
|
|
con._setuplabels(conlabels)
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2013-05-19 19:29:16 +00:00
|
|
|
|
|
|
|
-- When starting a map, load Lua modules given on the command line.
|
|
|
|
if (not g_firstRun) then
|
|
|
|
local i=0
|
|
|
|
while (ffiC.g_elModules[i] ~= nil) do
|
2013-06-09 16:37:19 +00:00
|
|
|
-- Get the module name and strip the trailing extension.
|
|
|
|
local modname = ffi.string(ffiC.g_elModules[i]):gsub("%.lua$","")
|
|
|
|
|
|
|
|
if (modname:find("%.")) then
|
|
|
|
-- Because they will be replaced by dirseps in our_require().
|
|
|
|
error("Dots are not permitted in module names", 0)
|
|
|
|
end
|
|
|
|
-- Allow forward slashes in module names from the cmdline.
|
|
|
|
our_require((modname:gsub("%.lua$",""):gsub("/",".")))
|
|
|
|
|
2013-05-19 19:29:16 +00:00
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
end
|