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
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
local bit = bit
|
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
|
|
|
|
local ipairs = ipairs
|
|
|
|
local loadstring = loadstring
|
|
|
|
local pairs = pairs
|
|
|
|
local rawget = rawget
|
|
|
|
local rawset = rawset
|
|
|
|
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
|
|
|
|
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
|
|
|
--
|
2012-11-25 13:18:52 +00:00
|
|
|
-- Notes: do not declare multiple pointers on one line (this is bad:
|
|
|
|
-- "int32_t *a, *b"). Do not name array arguments (or add a space
|
|
|
|
-- between the identifier and the '[' instead).
|
2012-10-07 15:25:52 +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
|
|
|
|
|
|
|
|
ffi.cdef(str)
|
|
|
|
end
|
|
|
|
|
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()
|
|
|
|
local print = print
|
|
|
|
|
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
|
|
|
|
|
|
|
---- game structs ----
|
2011-12-21 18:43:08 +00:00
|
|
|
ffi.cdef[[
|
2012-06-26 19:49:59 +00:00
|
|
|
enum dukeinv_t {
|
|
|
|
GET_STEROIDS,
|
|
|
|
GET_SHIELD,
|
|
|
|
GET_SCUBA,
|
|
|
|
GET_HOLODUKE,
|
|
|
|
GET_JETPACK,
|
|
|
|
GET_DUMMY1,
|
|
|
|
GET_ACCESS,
|
|
|
|
GET_HEATS,
|
|
|
|
GET_DUMMY2,
|
|
|
|
GET_FIRSTAID,
|
|
|
|
GET_BOOTS,
|
|
|
|
GET_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum dukeweapon_t {
|
|
|
|
KNEE_WEAPON,
|
|
|
|
PISTOL_WEAPON,
|
|
|
|
SHOTGUN_WEAPON,
|
|
|
|
CHAINGUN_WEAPON,
|
|
|
|
RPG_WEAPON,
|
|
|
|
HANDBOMB_WEAPON,
|
|
|
|
SHRINKER_WEAPON,
|
|
|
|
DEVISTATOR_WEAPON,
|
|
|
|
TRIPBOMB_WEAPON,
|
|
|
|
FREEZE_WEAPON,
|
|
|
|
HANDREMOTE_WEAPON,
|
|
|
|
GROW_WEAPON,
|
|
|
|
MAX_WEAPONS
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
MAXPLAYERS = 16,
|
|
|
|
};
|
|
|
|
]]
|
|
|
|
|
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
|
|
|
|
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-02-03 12:48:25 +00:00
|
|
|
__attribute__((packed)) struct {
|
2012-11-25 13:18:52 +00:00
|
|
|
const int32_t t_data[10]; // 56b sometimes used to hold offsets to con code
|
2012-08-08 19:32:35 +00:00
|
|
|
const struct move mv;
|
|
|
|
const struct action ac;
|
2012-11-25 13:18:52 +00:00
|
|
|
const int16_t _padding[1];
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2012-12-23 14:00:08 +00:00
|
|
|
int32_t flags; //4b
|
|
|
|
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;
|
2011-12-21 18:43:08 +00:00
|
|
|
int16_t movflag,tempang,timetosleep; //6b
|
|
|
|
|
|
|
|
const int16_t lightId, lightcount, lightmaxrange, cgg; //8b
|
2012-12-03 18:24:25 +00:00
|
|
|
int16_t actorstayput;
|
|
|
|
const int16_t dispicnum;
|
|
|
|
int16_t shootzvel;
|
2012-11-25 13:18:52 +00:00
|
|
|
const int8_t _do_not_use[8];
|
|
|
|
}
|
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
|
|
|
|
local check_weapon_idx, check_inventory_idx = bcheck.weapon_idx, bcheck.inventory_idx
|
|
|
|
local check_sound_idx = bcheck.sound_idx
|
|
|
|
|
2013-01-26 17:07:44 +00:00
|
|
|
-- TODO: randomize member names
|
|
|
|
bcarray.new("int16_t", 64, "loogie", "int16_x_64")
|
|
|
|
bcarray.new("int16_t", ffiC.MAX_WEAPONS, "weapon", "int16_x_MAX_WEAPONS")
|
|
|
|
bcarray.new("int16_t", ffiC.GET_MAX, "inventory", "int16_x_GET_MAX")
|
|
|
|
|
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;
|
|
|
|
const int16_t cursectnum;
|
|
|
|
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;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t wackedbyactor, pyoff, opyoff;
|
|
|
|
|
|
|
|
int16_t horiz, horizoff, ohoriz, ohorizoff;
|
2012-12-03 18:24:25 +00:00
|
|
|
const int16_t newowner;
|
|
|
|
int16_t jumping_counter, airleft;
|
2012-06-26 19:49:59 +00:00
|
|
|
int16_t fta, ftq, access_wallnum, access_spritenum;
|
|
|
|
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;
|
2012-12-03 18:24:30 +00:00
|
|
|
const int16_t dummyplayersprite;
|
|
|
|
int16_t extra_extra8;
|
|
|
|
int16_t actorsqu, timebeforeexit;
|
|
|
|
const int16_t customexitsound;
|
|
|
|
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-01 13:04:52 +00:00
|
|
|
uint8_t return_to_center, reloading;
|
|
|
|
const uint8_t weapreccnt;
|
2012-06-26 19:49:59 +00:00
|
|
|
uint8_t aim_mode, auto_aim, weaponswitch, movement_lock, team;
|
|
|
|
uint8_t tipincs, hbomb_hold_delay, frag_ps, kickback_pic;
|
|
|
|
|
|
|
|
uint8_t gm, on_warping_sector, footprintcount, hurt_delay;
|
|
|
|
uint8_t hbomb_on, jumping_toggle, rapid_fire_hold, on_ground;
|
|
|
|
uint8_t inven_icon, buttonpalette, over_shoulder_on, show_empty_weapon;
|
|
|
|
|
|
|
|
uint8_t jetpack_on, spritebridge, lastrandomspot;
|
|
|
|
uint8_t scuba_on, footprintpal, heat_on, invdisptime;
|
|
|
|
|
|
|
|
uint8_t holster_weapon, falling_counter, footprintshade;
|
|
|
|
uint8_t refresh_inventory, last_full_weapon;
|
|
|
|
|
|
|
|
uint8_t toggle_key_flag, knuckle_incs, knee_incs, access_incs;
|
|
|
|
uint8_t walking_snd_toggle, palookup, hard_landing, fist_incs;
|
|
|
|
|
2012-08-02 10:52:17 +00:00
|
|
|
int8_t numloogs, loogcnt, scream_voice;
|
2012-11-30 18:57:55 +00:00
|
|
|
int8_t last_weapon, cheat_phase, weapon_pos, wantweaponfire;
|
2013-01-24 20:03:10 +00:00
|
|
|
const 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;
|
2012-11-25 13:18:52 +00:00
|
|
|
|
|
|
|
const char name[32];
|
|
|
|
|
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;
|
|
|
|
int16_t sound, isound, vel;
|
|
|
|
const int16_t decal, trail;
|
|
|
|
int16_t tnum, drop;
|
|
|
|
int16_t offset, bounces, bsound;
|
|
|
|
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;
|
|
|
|
int8_t filler[6];
|
|
|
|
}
|
|
|
|
]]
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
local randgen = require("randgen")
|
2012-11-30 18:57:46 +00:00
|
|
|
local geom = require("geom")
|
2012-11-25 13:18:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
-- Converts a template struct definition to an external one, in which arrays
|
|
|
|
-- have been substituted by randomly named scalar fields.
|
|
|
|
local function mangle_arrays(structstr)
|
|
|
|
ma_count = 0
|
|
|
|
-- NOTE: regexp only works for non-nested arrays and for one array per line.
|
|
|
|
return (string.gsub(structstr, "const%s+([%w_]+)[^\n]+%[([%w_]+)%];", ma_replace_array))
|
|
|
|
end
|
|
|
|
|
|
|
|
--print(mangle_arrays(ACTOR_STRUCT))
|
|
|
|
|
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 {
|
|
|
|
uint32_t bits; // 4b
|
|
|
|
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
|
|
|
|
]].. mangle_arrays(DUKEPLAYER_STRUCT) ..[[
|
|
|
|
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-02-10 16:23:59 +00:00
|
|
|
typedef
|
|
|
|
]].. PROJECTILE_STRUCT ..[[
|
|
|
|
projectile_t;
|
2012-12-09 13:24:39 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t flags;
|
2013-02-03 12:48:25 +00:00
|
|
|
int32_t cacherange;
|
2012-12-09 13:24:39 +00:00
|
|
|
projectile_t defproj;
|
|
|
|
} 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;
|
|
|
|
int16_t sect;
|
|
|
|
} camera_t;
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
typedef struct {
|
|
|
|
]] .. table.concat(con_lang.wdata_members, ';')..';' .. [[
|
|
|
|
} weapondata_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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
int32_t scripthandle;
|
|
|
|
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;
|
2012-06-26 19:49:59 +00:00
|
|
|
]])
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2013-01-19 18:28:55 +00:00
|
|
|
-- EXTERNALLY EXPOSED GAME VARIABLES
|
|
|
|
ffi.cdef[[
|
|
|
|
const int32_t screenpeek;
|
|
|
|
hudweapon_t hudweap;
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- INTERNAL VARIABLES/FUNCTIONS
|
2012-06-26 19:49:59 +00:00
|
|
|
decl[[
|
|
|
|
const char *g_sizes_of_what[];
|
|
|
|
int32_t g_sizes_of[];
|
2012-12-25 16:13:45 +00:00
|
|
|
int32_t g_elCallDepth;
|
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-01-13 16:40:32 +00:00
|
|
|
weapondata_t g_playerWeapon[MAXPLAYERS][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-01-01 15:24:28 +00:00
|
|
|
char *ScriptQuotes[];
|
|
|
|
|
2012-08-19 12:52:18 +00:00
|
|
|
const int32_t playerswhenstarted;
|
2012-12-09 13:24:39 +00:00
|
|
|
int32_t lastvisinc;
|
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;
|
2012-11-30 18:57:46 +00:00
|
|
|
|
|
|
|
int32_t A_IncurDamage(int32_t sn); // not bound-checked!
|
2012-11-30 18:57:55 +00:00
|
|
|
void P_AddWeaponMaybeSwitch(DukePlayer_t *ps, int32_t weap);
|
2013-02-07 21:01:12 +00:00
|
|
|
int32_t A_ShootWithZvel(int32_t i, int32_t atwith, int32_t override_zvel);
|
2012-12-03 18:24:25 +00:00
|
|
|
int32_t A_IncurDamage(int32_t sn);
|
|
|
|
int32_t A_Spawn(int32_t j, int32_t pn);
|
|
|
|
void VM_FallSprite(int32_t i);
|
|
|
|
int32_t VM_ResetPlayer2(int32_t snum);
|
2012-12-03 18:24:30 +00:00
|
|
|
void A_RadiusDamage(int32_t i, int32_t r, int32_t, int32_t, int32_t, int32_t);
|
|
|
|
void G_OperateSectors(int32_t sn, int32_t ii);
|
|
|
|
int32_t A_Dodge(spritetype *s);
|
2012-12-28 17:18:02 +00:00
|
|
|
int32_t A_InsertSprite(int32_t whatsect,int32_t s_x,int32_t s_y,int32_t s_z,int32_t s_pn,int32_t s_s,
|
|
|
|
int32_t s_xr,int32_t s_yr,int32_t s_a,int32_t s_ve,int32_t s_zv,int32_t s_ow,int32_t s_ss);
|
|
|
|
int32_t A_Spawn(int32_t j, int32_t pn);
|
|
|
|
void A_AddToDeleteQueue(int32_t i);
|
2013-01-01 15:24:28 +00:00
|
|
|
void P_DoQuote(int32_t q, DukePlayer_t *p);
|
|
|
|
void G_ClearCameraView(DukePlayer_t *ps);
|
2013-01-02 22:33:37 +00:00
|
|
|
|
|
|
|
int32_t A_CheckAnySoundPlaying(int32_t i);
|
|
|
|
int32_t A_PlaySound(uint32_t num, 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);
|
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);
|
|
|
|
int32_t kfilelength(int32_t handle);
|
|
|
|
void kclose(int32_t handle);
|
|
|
|
int32_t kread(int32_t handle, void *buffer, int32_t leng);
|
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);
|
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-02-01 13:05:20 +00:00
|
|
|
for i=0,8 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
|
|
|
-- "player" global, needed by the "control" module
|
|
|
|
local tmpmt = {
|
|
|
|
__index = function(tab, key)
|
|
|
|
if (key >= 0 and key < ffiC.playerswhenstarted) then
|
|
|
|
return ffiC.g_player[key].ps[0]
|
|
|
|
end
|
|
|
|
error('out-of-bounds player[] read access', 2)
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function(tab, key, val) error('cannot write directly to player[] struct', 2) end,
|
|
|
|
}
|
|
|
|
player = setmtonce({}, tmpmt)
|
2012-08-06 20:00:29 +00:00
|
|
|
|
2012-12-29 15:21:32 +00:00
|
|
|
-- needed by "control"
|
|
|
|
actor = defs_c.creategtab(ffiC.actor, ffiC.MAXSPRITES, "actor[]")
|
|
|
|
|
2013-02-07 21:01:03 +00:00
|
|
|
local projectile = defs_c.creategtab(ffiC.ProjectileData, ffiC.MAXTILES, "projectile[]")
|
|
|
|
|
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")
|
2012-11-25 13:18:46 +00:00
|
|
|
local MV, AC, AI = con.MV, con.AC, con.AI
|
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)
|
|
|
|
return con.isenemytile(s.picnum)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
defs_c.finish_spritetype(spr_mt_index_add)
|
2013-01-23 19:36:48 +00:00
|
|
|
|
2012-08-06 20:00:29 +00:00
|
|
|
-- All-zero action and move
|
|
|
|
local nullac, nullmv = ffi.new("const struct action"), ffi.new("const struct move")
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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)))
|
2012-08-10 19:11:43 +00:00
|
|
|
local con_action_ct = ffi.typeof("con_action_t")
|
|
|
|
local con_move_ct = ffi.typeof("con_move_t")
|
|
|
|
local con_ai_ct = ffi.typeof("con_ai_t")
|
|
|
|
|
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)
|
|
|
|
assert(not (i >= ffiC.MAXSPRITES+0ULL))
|
|
|
|
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)
|
2012-11-25 13:18:46 +00:00
|
|
|
if (type(act)=="string") then
|
|
|
|
act = AC[act];
|
|
|
|
end
|
|
|
|
-- TODO: disallow passing the FFI types altogether, in favor of
|
|
|
|
-- strings (also move, ai)?
|
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)
|
2012-11-25 13:18:46 +00:00
|
|
|
if (type(act)=="string") then
|
|
|
|
act = AC[act];
|
|
|
|
end
|
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
|
|
|
|
set_count = function(a)
|
2012-08-10 19:11:43 +00:00
|
|
|
ffi.cast(actor_ptr_ct, a).t_data[0] = 0
|
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,
|
|
|
|
|
|
|
|
-- move
|
|
|
|
set_move = function(a, mov, movflags)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2012-11-25 13:18:46 +00:00
|
|
|
if (type(mov)=="string") then
|
|
|
|
mov = MV[mov];
|
|
|
|
end
|
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-02-07 21:01:18 +00:00
|
|
|
local i = get_actor_idx(a)
|
2012-08-08 19:32:35 +00:00
|
|
|
ffiC.sprite[i].hitag = movflags or 0
|
|
|
|
|
|
|
|
-- TODO: random angle moveflag
|
|
|
|
end,
|
|
|
|
|
|
|
|
has_move = function(a, mov)
|
2012-08-10 19:11:43 +00:00
|
|
|
a = ffi.cast(actor_ptr_ct, a)
|
2012-11-25 13:18:46 +00:00
|
|
|
if (type(mov)=="string") then
|
|
|
|
mov = MV[mov];
|
|
|
|
end
|
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,
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
if (type(ai)=="string") then
|
2012-11-25 13:18:46 +00:00
|
|
|
ai = AI[ai];
|
|
|
|
end
|
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
|
|
|
|
|
|
|
-- NOTE: compare with gameexec.c
|
|
|
|
a.t_data[5] = ai.id
|
|
|
|
|
|
|
|
oa:set_action(ai.act)
|
|
|
|
oa:set_move(ai.mov, ai.movflags)
|
|
|
|
|
|
|
|
a.t_data[0] = 0
|
|
|
|
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-12-09 13:24:28 +00:00
|
|
|
if (type(ai)=="string") then
|
2012-11-25 13:18:46 +00:00
|
|
|
ai = AI[ai];
|
|
|
|
end
|
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-02-01 13:05:00 +00:00
|
|
|
-- TODO: make a bcarray instead.
|
2012-12-09 13:24:39 +00:00
|
|
|
get_t_data = function(a, idx)
|
|
|
|
if (idx >= 10ULL) then
|
|
|
|
error("Invalid t_data index "..idx, 2)
|
|
|
|
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)
|
|
|
|
if (idx >= 10ULL) then
|
|
|
|
error("Invalid t_data index "..idx, 2)
|
|
|
|
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)
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
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,
|
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
|
|
|
|
local weapondata_mt = {
|
|
|
|
__newindex = function(wd, member, val)
|
2013-02-07 21:01:03 +00:00
|
|
|
if (string.match(member, "sound")) then
|
|
|
|
-- TODO: set to 0 if oob? (e.g. CrackDown)
|
|
|
|
check_sound_idx(val)
|
|
|
|
elseif (member=="workslike") then
|
|
|
|
check_weapon_idx(val)
|
|
|
|
elseif (member=="shoots" or member=="spawn") then
|
|
|
|
-- TODO: set to 0 if oob? (e.g. AMC TC)
|
|
|
|
check_tile_idx(val)
|
|
|
|
end
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2013-02-07 21:01:03 +00:00
|
|
|
wd[member] = val
|
2013-01-19 18:28:43 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
ffi.metatype("weapondata_t", weapondata_mt)
|
|
|
|
|
|
|
|
local weaponaccess_mt = {
|
|
|
|
-- Make syntax like "player[0].weapon.KNEE.shoots" possible
|
|
|
|
__index = function(wa, key)
|
|
|
|
if (type(key)=="number") then
|
|
|
|
check_weapon_idx(key)
|
|
|
|
return ffiC.g_playerWeapon[wa._p][key]
|
|
|
|
end
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2013-01-19 18:28:43 +00:00
|
|
|
return ffiC.g_playerWeapon[wa._p][ffiC[key.."_WEAPON"]]
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
ffi.metatype("weaponaccess_t", weaponaccess_mt)
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
local player_mt = {
|
|
|
|
__index = {
|
2012-11-30 18:57:55 +00:00
|
|
|
--- Getters/setters
|
|
|
|
get_ammo_amount = function(p, weap)
|
2013-01-26 17:07:44 +00:00
|
|
|
return p.ammo_amount[weap]
|
2012-11-30 18:57:55 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
set_ammo_amount = function(p, weap, amount)
|
2013-01-26 17:07:44 +00:00
|
|
|
p.ammo_amount[weap] = amount
|
2012-11-30 18:57:55 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
get_max_ammo_amount = function(p, weap)
|
2013-01-26 17:07:44 +00:00
|
|
|
return p.max_ammo_amount[weap]
|
2012-11-30 18:57:55 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
set_max_ammo_amount = function(p, weap, amount)
|
2013-01-26 17:07:44 +00:00
|
|
|
p.max_ammo_amount[weap] = amount
|
2012-11-30 18:57:55 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
set_curr_weapon = function(p, weap)
|
2013-02-01 13:04:52 +00:00
|
|
|
check_weapon_idx(weap)
|
2013-01-26 17:07:44 +00:00
|
|
|
p.curr_weapon = weap
|
2012-11-30 18:57:55 +00:00
|
|
|
end,
|
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
get_inv_amount = function(p, inv)
|
2013-01-26 17:07:44 +00:00
|
|
|
return p.inv_amount[inv]
|
2012-12-03 18:24:25 +00:00
|
|
|
end,
|
|
|
|
|
|
|
|
set_inv_amount = function(p, inv, amount)
|
2013-01-26 17:07:44 +00:00
|
|
|
p.inv_amount[inv] = amount
|
2012-12-03 18:24:25 +00:00
|
|
|
end,
|
|
|
|
|
2012-12-03 18:24:30 +00:00
|
|
|
set_customexitsound = function(p, soundnum)
|
|
|
|
if (soundnum >= con_lang.MAXSOUNDS+0ULL) then
|
|
|
|
error("Invalid sound number "..soundnum, 2)
|
|
|
|
end
|
|
|
|
ffi.cast(player_ptr_ct, p).customexitsound = soundnum
|
|
|
|
end,
|
|
|
|
|
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-01-24 20:03:10 +00:00
|
|
|
have_weapon = function(p, weap)
|
|
|
|
return (bit.band(p.gotweapon, bit.lshift(1, weap)) ~= 0)
|
|
|
|
end,
|
|
|
|
|
2013-02-01 13:04:52 +00:00
|
|
|
give_weapon = function(p, weap)
|
|
|
|
p.gotweapon = bit.bor(p.gotweapon, bit.lshift(1, weap))
|
|
|
|
end,
|
|
|
|
|
|
|
|
take_weapon = function(p, weap)
|
|
|
|
p.gotweapon = bit.band(p.gotweapon, bit.bnot(bit.lshift(1, weap)))
|
|
|
|
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-01-13 16:40:21 +00:00
|
|
|
-- XXX: is the correct spelling "whack"?
|
|
|
|
wack = 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
|
2012-12-03 18:24:30 +00:00
|
|
|
local n = bit.arshift(128-bit.band(ffiC.krand(),255), 1)
|
|
|
|
p.rotscrnang = n
|
|
|
|
p.look_ang = n
|
|
|
|
end,
|
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
--- Not fully specified, off-limits to users. Might disappear, change
|
|
|
|
--- signature, etc...
|
2013-02-01 13:04:52 +00:00
|
|
|
-- TODO: need to think about external API: 0-255 based? 0-1 based?
|
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
|
2012-12-03 18:24:25 +00:00
|
|
|
pals.f = f
|
|
|
|
pals.r, pals.g, pals.b = r, g, b
|
|
|
|
end,
|
2012-11-30 18:57:46 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("DukePlayer_t", player_mt)
|
|
|
|
|
2013-02-10 16:23:59 +00:00
|
|
|
local function GenProjectileSetFunc(Member)
|
|
|
|
return function(self, picnum)
|
|
|
|
if (picnum >= 0) then
|
|
|
|
check_tile_idx(picnum)
|
|
|
|
end
|
|
|
|
ffi.cast(projectile_ptr_ct, self)[Member] = picnum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local projectile_mt = {
|
|
|
|
__index = {
|
|
|
|
set_spawns = GenProjectileSetFunc "spawns",
|
|
|
|
set_decal = GenProjectileSetFunc "decal",
|
|
|
|
set_trail = GenProjectileSetFunc "trail",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ffi.metatype("projectile_t", projectile_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-01-19 18:28:40 +00:00
|
|
|
|
2013-02-07 21:01:18 +00:00
|
|
|
-- Support for some CON global system gamevars
|
|
|
|
gv_access._csv = ffi.new "struct { int32_t RETURN; }"
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
function gv_access.currentRenderMode()
|
|
|
|
-- TODO: USE_OPENGL=0 build
|
|
|
|
return ffiC.rendmode
|
|
|
|
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
|
2012-06-17 19:45:33 +00:00
|
|
|
local strbuf = {"enum {"}
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
for label, val in pairs(con_lang.labels[i]) do
|
2012-06-17 19:45:33 +00:00
|
|
|
strbuf[#strbuf+1] = string.format("%s = %d,", label, val)
|
|
|
|
end
|
|
|
|
strbuf[#strbuf+1] = "};"
|
|
|
|
|
|
|
|
ffi.cdef(table.concat(strbuf))
|
|
|
|
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 = {
|
2012-08-06 20:00:23 +00:00
|
|
|
clock = function() return gv_.gethitickms()*0.001 end,
|
2012-08-02 10:52:21 +00:00
|
|
|
},
|
|
|
|
|
2012-11-25 13:18:52 +00:00
|
|
|
randgen = randgen,
|
2012-11-30 18:57:46 +00:00
|
|
|
geom = geom,
|
2012-08-02 10:52:32 +00:00
|
|
|
stat = require("stat"),
|
2012-08-02 10:52:44 +00:00
|
|
|
bitar = require("bitar"),
|
2012-08-06 20:00:29 +00:00
|
|
|
|
|
|
|
con = con,
|
2012-07-13 18:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for modname, themodule in pairs(allowed_modules) do
|
|
|
|
local mt = {
|
|
|
|
__index = themodule,
|
|
|
|
__newindex = function(tab,idx,val)
|
2012-08-06 20:00:23 +00:00
|
|
|
error("modifying base module table forbidden", 2)
|
2012-07-13 18:20:38 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
2012-07-20 21:57:37 +00:00
|
|
|
-- Comment out to make base modules not protected:
|
2013-01-26 17:07:44 +00:00
|
|
|
allowed_modules[modname] = setmtonce({}, mt)
|
2012-07-13 18:20:38 +00:00
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
local function check_valid_modname(modname, errlev)
|
2012-07-20 21:57:28 +00:00
|
|
|
if (type(modname) ~= "string") then
|
2012-08-06 20:00:23 +00:00
|
|
|
error("module name must be a string", errlev+1)
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: restrict valid names?
|
|
|
|
end
|
|
|
|
|
|
|
|
local function errorf(level, fmt, ...)
|
|
|
|
local errmsg = string.format(fmt, ...)
|
|
|
|
error(errmsg, level+1)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
local package_loaded = {}
|
|
|
|
local modname_stack = {}
|
|
|
|
local ERRLEV = 5
|
2012-08-02 10:52:17 +00:00
|
|
|
|
|
|
|
local function readintostr(fn)
|
2012-12-29 15:21:24 +00:00
|
|
|
-- XXX: this is pretty much the same as the code in L_RunOnce()
|
2012-08-02 10:52:17 +00:00
|
|
|
|
|
|
|
local fd = ffiC.kopen4loadfrommod(fn, 0) -- TODO: g_loadFromGroupOnly
|
|
|
|
if (fd < 0) then
|
2012-12-29 15:21:24 +00:00
|
|
|
return nil
|
2012-08-02 10:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local sz = ffiC.kfilelength(fd)
|
|
|
|
if (sz == 0) then
|
|
|
|
ffiC.kclose(fd)
|
2012-12-29 15:21:24 +00:00
|
|
|
return ""
|
2012-08-02 10:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (sz < 0) then
|
|
|
|
ffi.kclose(fd)
|
2012-11-30 18:57:55 +00:00
|
|
|
error("INTERNAL ERROR: kfilelength() returned negative length", ERRLEV)
|
2012-08-02 10:52:17 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
local str = ffi.new("char [?]", sz)
|
2012-08-02 10:52:17 +00:00
|
|
|
local readlen = ffiC.kread(fd, str, sz)
|
|
|
|
|
|
|
|
ffiC.kclose(fd); fd=-1
|
|
|
|
|
|
|
|
if (readlen ~= sz) then
|
|
|
|
errorf(ERRLEV, "INTERNAL ERROR: couldn't read \"%s\" wholly", fn)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ffi.string(str, sz)
|
|
|
|
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, ...)
|
2012-08-06 20:00:23 +00:00
|
|
|
check_valid_modname(modname, 2)
|
2012-07-20 21:57:28 +00:00
|
|
|
|
|
|
|
-- see whether it's a base module name first
|
|
|
|
if (allowed_modules[modname] ~= nil) then
|
|
|
|
return allowed_modules[modname]
|
|
|
|
end
|
|
|
|
|
|
|
|
--- search user modules
|
2012-07-13 18:20:38 +00:00
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
if (package_loaded[modname] ~= nil) then
|
|
|
|
-- already loaded
|
|
|
|
return package_loaded[modname]
|
2012-07-13 18:20:38 +00:00
|
|
|
end
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local modfn = modname .. ".lua"
|
|
|
|
local str = readintostr(modfn)
|
|
|
|
if (str == nil) then
|
|
|
|
errorf(ERRLEV-1, "Couldn't open file \"%s\"", modfn)
|
|
|
|
end
|
2012-07-20 21:57:28 +00:00
|
|
|
|
|
|
|
local modfunc, errmsg = loadstring(str)
|
|
|
|
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
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
package_loaded[modname] = true
|
|
|
|
table.insert(modname_stack, modname)
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
-- Run the module code!
|
2013-01-13 16:40:21 +00:00
|
|
|
modfunc(modname, ...) -- TODO: call protected and report errors here later
|
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
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
if (type(modtab) == "table") then
|
|
|
|
-- Protect module table if there is one...
|
|
|
|
local mt = {
|
|
|
|
__index = modtab,
|
|
|
|
__newindex = function(tab,idx,val)
|
|
|
|
error("modifying module table forbidden", 2)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(modtab, mt)
|
|
|
|
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 = {
|
|
|
|
__index = function (_, n)
|
|
|
|
error("attempt to read undeclared variable '"..n.."'", 2)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
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-01-13 16:40:32 +00:00
|
|
|
-- Also, our 'module' has no varargs ("option functions" in Lua).
|
2012-08-06 20:00:23 +00:00
|
|
|
-- TODO: make transactional?
|
|
|
|
local function our_module()
|
|
|
|
local modname = modname_stack[#modname_stack]
|
|
|
|
if (type(modname) ~= "string") then
|
|
|
|
error("'module' must be called at the top level of a require'd file", 2)
|
|
|
|
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)
|
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_
|
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
local gameactor_internal = gameactor_internal -- included in lunatic.c
|
|
|
|
-- gameactor(tilenum [, strength [, act [, mov [, movflags]]]], actor_func)
|
|
|
|
local function our_gameactor(tilenum, ...)
|
2012-12-25 16:13:45 +00:00
|
|
|
if (ffiC.g_elCallDepth > 0) then
|
|
|
|
error("Invalid use of gameactor: must be called from top level", 2)
|
|
|
|
end
|
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
local args = {...}
|
|
|
|
if (type(tilenum) ~= "number") then
|
|
|
|
error("invalid argument #1 to gameactor: must be a number", 2)
|
|
|
|
end
|
|
|
|
if (tilenum >= ffiC.MAXTILES+0ULL) then
|
|
|
|
error("invalid argument #1 to gameactor: must be a tile number (0 .. MAXTILES-1)", 2)
|
|
|
|
end
|
|
|
|
if (#args == 0) then
|
|
|
|
error("invalid call to gameactor: must have at least two arguments (tilenum, func)", 2)
|
|
|
|
end
|
2013-01-26 17:07:44 +00:00
|
|
|
if (#args > 5) then
|
2012-12-25 16:13:45 +00:00
|
|
|
error("invalid call to gameactor: must have at most six arguments", 2)
|
|
|
|
end
|
2012-12-23 19:24:16 +00:00
|
|
|
if (type(args[#args]) ~= "function") then
|
|
|
|
error("invalid last argument to gameactor: must be a function", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local strength = (#args >= 2) and args[1] or 0
|
|
|
|
if (type(strength) ~= "number") then
|
|
|
|
error("invalid 'strength' argument to gameactor: must be a number", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: literal number action other than 0?
|
|
|
|
local act = (#args >= 3) and args[2] or "NO"
|
|
|
|
if (type(act)=="string") then
|
|
|
|
act = AC[act]
|
|
|
|
end
|
|
|
|
if (not ffi.istype(con_action_ct, act)) then
|
|
|
|
error("invalid 'act' argument to gameactor: must be a string or action", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: literal number move other than 0?
|
|
|
|
local mov = (#args >= 4) and args[3] or "NO"
|
|
|
|
if (type(mov)=="string") then
|
|
|
|
mov = MV[mov]
|
|
|
|
end
|
|
|
|
if (not ffi.istype(con_move_ct, mov)) then
|
|
|
|
error("invalid 'mov' argument to gameactor: must be a string or move", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local movflags = (#args >= 5) and args[4] or 0
|
|
|
|
if (type(movflags) ~= "number") then
|
|
|
|
error("invalid 'movflags' argument to gameactor: must be a number", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
gameactor_internal(tilenum, strength, act, mov, movflags, args[#args])
|
|
|
|
end
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
-- Event functions, saved for event chaining
|
|
|
|
local event_funcs = {}
|
|
|
|
|
|
|
|
local gameevent_internal = gameevent_internal -- included in lunatic.c
|
|
|
|
-- gameevent(<event idx or string>, <event function>)
|
|
|
|
local function our_gameevent(event, func)
|
|
|
|
if (ffiC.g_elCallDepth > 0) then
|
|
|
|
error("Invalid use of gameevent: must be called from top level", 2)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
error("invalid argument #1 to gameevent: must be a number", 2)
|
|
|
|
end
|
|
|
|
if (event >= con_lang.MAXEVENTS+0ULL) then
|
|
|
|
error("invalid argument #1 to gameevent: must be an event number (0 .. MAXEVENTS-1)", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (type(func) ~= "function") then
|
|
|
|
error("invalid argument #2 to gameevent: must be a function", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local newfunc = func
|
|
|
|
local oldfunc = event_funcs[event]
|
|
|
|
-- event chaining: events defined later come first
|
|
|
|
if (oldfunc ~= nil) then
|
|
|
|
newfunc = function(aci, pli, dist)
|
|
|
|
func(aci, pli, dist)
|
2013-01-05 13:09:31 +00:00
|
|
|
return oldfunc(aci, pli, dist)
|
2012-12-25 16:13:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2012-05-13 16:05:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
---=== Lunatic interpreter setup ===---
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local concode
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
--- Compile CONs
|
|
|
|
do
|
|
|
|
read_into_string = readintostr -- for lunacon
|
|
|
|
local lunacon = require("lunacon")
|
|
|
|
|
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
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
concode = lunacon.compile(confn)
|
2013-01-13 16:40:21 +00:00
|
|
|
if (concode == nil) then
|
|
|
|
error("Failure compiling CON code, exiting.", 0)
|
|
|
|
end
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
|
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
|
|
|
|
2012-06-26 19:49:59 +00:00
|
|
|
gv = gv_
|
2011-09-25 15:11:47 +00:00
|
|
|
local tmpmt = {
|
2013-01-19 18:28:40 +00:00
|
|
|
__index = function(gv, key)
|
|
|
|
if (gv_access[key] == nil) then
|
|
|
|
return ffiC[key]
|
|
|
|
end
|
2013-01-19 18:28:55 +00:00
|
|
|
if (type(gv_access[key])~="boolean") then
|
2013-01-19 18:28:40 +00:00
|
|
|
-- overridden...
|
|
|
|
return gv_access[key]
|
|
|
|
end
|
|
|
|
error("access forbidden", 2)
|
|
|
|
end,
|
|
|
|
__newindex = function() error("write access forbidden", 2) end,
|
2011-09-25 15:11:47 +00:00
|
|
|
}
|
2012-10-07 15:26:13 +00:00
|
|
|
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-01-28 14:38:41 +00:00
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
---=== Game variables ===---
|
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
-- gamevarNames[name] is true if that gamevar was declared
|
|
|
|
local gamevarNames = {}
|
|
|
|
|
2012-03-25 22:01:36 +00:00
|
|
|
-- code for prohibiting initial assignments to create new variables,
|
|
|
|
-- based on 14.2 of PiL
|
2012-01-28 14:38:41 +00:00
|
|
|
function gamevar(name, initval) -- aka 'declare'
|
2012-03-25 22:01:36 +00:00
|
|
|
if (type(name) ~= "string") then
|
2012-07-20 21:57:34 +00:00
|
|
|
error("First argument to 'gamevar' must be a string", 2)
|
2012-03-25 22:01:36 +00:00
|
|
|
end
|
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
if (not string.match(name, "^[%a_][%w_]*$")) then
|
2012-07-20 21:57:34 +00:00
|
|
|
error("First argument to 'gamevar' must be a valid identifier", 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (string.match(name, "^_")) then
|
|
|
|
error("Identifier names starting with an underscore are reserved", 2)
|
2012-03-25 22:01:36 +00:00
|
|
|
end
|
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
if (gamevarNames[name]) then
|
2012-03-25 22:01:36 +00:00
|
|
|
error(string.format("Duplicate declaration of identifier '%s'", name), 2)
|
|
|
|
end
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
if (rawget(G_, name) ~= nil) then
|
2012-03-25 22:01:36 +00:00
|
|
|
error(string.format("Identifier name '%s' is already used in the global environment", name), 2)
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
gamevarNames[name] = true
|
2012-08-06 20:00:23 +00:00
|
|
|
rawset(G_, name, initval or false)
|
2011-09-25 15:11:47 +00:00
|
|
|
end
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2011-10-11 16:53:15 +00:00
|
|
|
|
2012-04-22 19:10:29 +00:00
|
|
|
---=== Serialization, from PiL with modifications ===---
|
|
|
|
local function basicSerialize(o)
|
|
|
|
if (type(o) == "number") then
|
|
|
|
if (o ~= o) then return "0/0" end -- nan
|
|
|
|
if (o == 1/0) then return "1/0" end -- inf
|
|
|
|
if (o == -1/0) then return "-1/0" end -- -inf
|
|
|
|
|
|
|
|
return tostring(o)
|
2012-06-17 19:45:37 +00:00
|
|
|
elseif (type(o) == "boolean") then
|
|
|
|
return tostring(o)
|
|
|
|
elseif (type(o) == "string") then
|
2012-04-22 19:10:29 +00:00
|
|
|
return string.format("%q", o)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- will contain all gamevar tables as keys (and true as value)
|
|
|
|
local tmpgvtabs = {}
|
|
|
|
|
|
|
|
local function save(name, value, saved, strings)
|
|
|
|
saved = saved or {} -- initial value
|
|
|
|
|
|
|
|
strings[#strings+1] = name .. "="
|
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
local str = basicSerialize(value)
|
|
|
|
|
|
|
|
if (str ~= nil) then
|
|
|
|
strings[#strings+1] = str .. "\n"
|
2012-04-22 19:10:29 +00:00
|
|
|
elseif (type(value) == "table") then
|
|
|
|
if (saved[value]) then -- value already saved?
|
|
|
|
strings[#strings+1] = saved[value] .. "\n" -- use its previous name
|
|
|
|
else
|
|
|
|
saved[value] = name -- save name for next time
|
|
|
|
strings[#strings+1] = "{}\n" -- create a new table
|
|
|
|
|
|
|
|
for k,v in pairs(value) do -- save its fields
|
2012-06-17 19:45:37 +00:00
|
|
|
local keystr = basicSerialize(k)
|
|
|
|
if (keystr == nil) then
|
|
|
|
error("cannot save a " .. type(k) .. " as key of a table", 2);
|
|
|
|
end
|
|
|
|
local fieldname = string.format("%s[%s]", name, keystr)
|
2012-04-22 19:10:29 +00:00
|
|
|
if (type(v)=="table" and not tmpgvtabs[v]) then
|
|
|
|
error("cannot save \""..name..
|
|
|
|
"\": gamevar tables may only contain tables that are also gamevars", 2)
|
|
|
|
end
|
|
|
|
save(fieldname, v, saved, strings)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error("cannot save \""..name.."\", a " .. type(value), 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function serializeGamevars()
|
|
|
|
local saved = {}
|
|
|
|
local strings = {}
|
|
|
|
|
|
|
|
for gvname,_ in pairs(gamevarNames) do
|
|
|
|
if (type(G_[gvname])=="table") then
|
|
|
|
tmpgvtabs[G_[gvname]] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: catch errors in save()
|
|
|
|
for gvname,_ in pairs(gamevarNames) do
|
|
|
|
save(gvname, G_[gvname], saved, strings)
|
|
|
|
end
|
|
|
|
strings[#strings+1] = "\n"
|
|
|
|
|
|
|
|
tmpgvtabs = {}
|
|
|
|
|
|
|
|
return table.concat(strings)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function loadGamevarsString(string)
|
2012-06-17 19:45:33 +00:00
|
|
|
--[=[
|
|
|
|
for gvname,_ in pairs(gamevarNames) do
|
|
|
|
G_[gvname] = nil;
|
|
|
|
end
|
|
|
|
gamevarNames = {}; -- clear gamevars
|
|
|
|
--]=]
|
|
|
|
|
2012-08-06 20:00:23 +00:00
|
|
|
assert(loadstring(string))()
|
2012-04-22 19:10:29 +00:00
|
|
|
end
|
|
|
|
|
2012-05-13 16:04:28 +00:00
|
|
|
|
|
|
|
-- REMOVE this for release
|
|
|
|
DBG_ = {}
|
|
|
|
DBG_.printkv = printkv
|
2012-08-06 20:00:23 +00:00
|
|
|
DBG_.loadstring = loadstring
|
2012-04-22 19:10:29 +00:00
|
|
|
DBG_.serializeGamevars = serializeGamevars
|
|
|
|
DBG_.loadGamevarsString = loadGamevarsString
|
2013-01-06 18:56:45 +00:00
|
|
|
DBG_.oom = function()
|
|
|
|
local s = "1"
|
|
|
|
for i=1,math.huge do
|
|
|
|
s = s..s
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
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!
|
2012-08-06 20:00:23 +00:00
|
|
|
setmetatable(
|
2012-01-28 14:37:24 +00:00
|
|
|
G_, {
|
2012-07-13 18:20:35 +00:00
|
|
|
__newindex = function (_1, n, _2)
|
|
|
|
error("attempt to write to undeclared variable '"..n.."'", 2)
|
2012-01-28 14:37:24 +00:00
|
|
|
end,
|
|
|
|
__index = function (_, n)
|
2012-07-13 18:20:35 +00:00
|
|
|
error("attempt to read undeclared variable '"..n.."'", 2)
|
2012-01-28 14:37:24 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
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.
|
|
|
|
-- In particular, we need the functions defined after setting this chunk's
|
|
|
|
-- environment earlier.
|
2012-08-06 20:00:23 +00:00
|
|
|
setfenv(0, _G)
|
2012-12-29 15:21:24 +00:00
|
|
|
|
|
|
|
-- Finally, run the CON code translated into Lua.
|
|
|
|
if (concode) then
|
|
|
|
local confunc, conerrmsg = loadstring(concode, "CON")
|
|
|
|
if (confunc == nil) then
|
|
|
|
error("Failure loading translated CON code: "..conerrmsg)
|
|
|
|
end
|
|
|
|
confunc()
|
|
|
|
end
|