diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index baa3fd8a2..a9ccc311b 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -10413,7 +10413,7 @@ int32_t app_main(int32_t argc, const char **argv) { if (!g_player[i].ps) g_player[i].ps = (DukePlayer_t *)Bcalloc(1, sizeof(DukePlayer_t)); #ifdef LUNATIC - g_player[i].ps->idx = i; + g_player[i].ps->wa.idx = i; #endif if (!g_player[i].sync) g_player[i].sync = (input_t *)Bcalloc(1, sizeof(input_t)); } diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 558f223e8..2e2185bc1 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -231,7 +231,10 @@ local DUKEPLAYER_STRUCT = [[ const char name[32]; - const int8_t idx; + // 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; } ]] @@ -282,6 +285,8 @@ typedef struct ]].. strip_const(ACTOR_STRUCT).. [[ actor_u_t; // The _u_t versions are unrestricted variants for internal use. +typedef struct { int32_t _p; } weaponaccess_t; + typedef struct ]].. mangle_arrays(DUKEPLAYER_STRUCT) ..[[ DukePlayer_t; @@ -536,7 +541,9 @@ for i=0,7 do local what = ffi.string(ffiC.g_sizes_of_what[i]) local fsz = ffi.sizeof(what) local csz = ffiC.g_sizes_of[i] --- print(i..": "..what..": C sizeof = "..tostring(csz)..", FFI sizeof = "..tostring(fsz)) + if (ffiC._DEBUG_LUNATIC ~= 0) then + print(i..": "..what..": C sizeof = "..tostring(csz)..", FFI sizeof = "..tostring(fsz)) + end if (fsz ~= csz) then good = false; end @@ -758,20 +765,9 @@ local function check_tile_idx(tilenum) end end -local weapondata_accessor = {} -local function get_weapondata_accessor(pli, weap) - assert(pli < ffiC.MAXPLAYERS+0ULL) - - local wdaidx = pli*1024 + weap - - if (weapondata_accessor[wdaidx] ~= nil) then - return weapondata_accessor[wdaidx] - end - - local weapondata_mt = { - __index = ffiC.g_playerWeapon[pli][weap], - - __newindex = function(_, member, val) +--- PER-PLAYER WEAPON SETTINGS +local weapondata_mt = { + __newindex = function(wd, member, val) if (string.match(member, "sound")) then -- TODO: set to 0 if oob? (e.g. CrackDown) check_sound_idx(val) @@ -782,17 +778,23 @@ local function get_weapondata_accessor(pli, weap) check_tile_idx(val) end - ffiC.g_playerWeapon[pli][weap][member] = val - end, + wd[member] = val + end, +} +ffi.metatype("weapondata_t", weapondata_mt) - __metatable = true, - } +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 - local tab = setmetatable({}, weapondata_mt) - weapondata_accessor[wdaidx] = tab - - return tab -end + return ffiC.g_playerWeapon[wa._p][ffiC[key.."_WEAPON"]] + end, +} +ffi.metatype("weaponaccess_t", weaponaccess_mt) local player_mt = { __index = { @@ -839,13 +841,6 @@ local player_mt = { ffi.cast(player_ptr_ct, p).customexitsound = soundnum end, - -- Access to weapon data (g_playerWeapon) - -- TODO: Make syntax e.g. "player[0].weapon.KNEE.shoots" possible - weapon = function(p, weap) - check_weapon_idx(weap) - return get_weapondata_accessor(p.idx, weap) - end, - -- CON-like addammo/addweapon, but without the non-local control flow -- (returns true if weapon's ammo was at the max. instead). addammo = con._addammo, diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index e67dda80a..e10bba47e 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -176,7 +176,7 @@ local function new_initial_gvartab() local member = wmembers[i]:gsub(".* ","") -- strip e.g. "int32_t " local name = format("WEAPON%d_%s", w, member:upper()) - local code = format(PLS":weapon(%d).%s", w, member) + local code = format(PLS".weapon[%d].%s", w, member) gamevar[name] = { name=code, flags=GVFLAG.PERPLAYER+GVFLAG.SYSTEM } end diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 843db986a..da9ae6dec 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -205,7 +205,8 @@ gameevent("JUMP", function(actori, playeri, dist) print("I'm first!") -- DBG_.oom() - player[playeri]:weapon(gv.PISTOL_WEAPON).shoots = 2605 -- RPG + player[playeri].weapon.PISTOL.shoots = 2605 -- RPG + player[playeri].weapon[gv.PISTOL_WEAPON].firesound = 351 -- thunder end ) diff --git a/polymer/eduke32/source/player.h b/polymer/eduke32/source/player.h index ae936d429..cc6bf9725 100644 --- a/polymer/eduke32/source/player.h +++ b/polymer/eduke32/source/player.h @@ -221,11 +221,10 @@ typedef struct { #ifdef LUNATIC // The player index. Always valid since we have no loose DukePlayer_t's - // anywhere (like with spritetype_t): g_player[i].ps->idx == i. - int8_t idx; -#else - int8_t padding_; + // anywhere (like with spritetype_t): g_player[i].ps->wa.idx == i. + struct { int32_t idx; } wa; #endif + int8_t padding_; } DukePlayer_t; typedef struct {