From 5b07f676274d101eea0afebeacab5b25049b6c31 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 30 Nov 2012 18:57:55 +0000 Subject: [PATCH] Lunatic: player stuff (protect some members etc.) git-svn-id: https://svn.eduke32.com/eduke32@3249 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/gameexec.c | 13 +++-- polymer/eduke32/source/lunatic/control.lua | 55 +++++++++++++++++++ polymer/eduke32/source/lunatic/defs.ilua | 49 ++++++++++++++++- .../eduke32/source/lunatic/defs_common.lua | 2 +- polymer/eduke32/source/lunatic/dynsymlist | 5 +- polymer/eduke32/source/lunatic/lunacon.lua | 21 ------- 6 files changed, 112 insertions(+), 33 deletions(-) diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 9e6f3305f..2bf860913 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -742,7 +742,8 @@ dead: else vm.g_sp->shade += (sector[vm.g_sp->sectnum].floorshade-vm.g_sp->shade)>>1; } -void addweapon_maybeswitch(DukePlayer_t *ps, int32_t weap) +// NOTE: Used from Lunatic +void P_AddWeaponMaybeSwitch(DukePlayer_t *ps, int32_t weap) { if ((ps->weaponswitch & 1) && (ps->weaponswitch & 4)) { @@ -773,12 +774,12 @@ void addweapon_maybeswitch(DukePlayer_t *ps, int32_t weap) P_AddWeaponNoSwitch(ps, weap); } -static void addweapon_addammo_common(DukePlayer_t *ps, int32_t weap, int32_t amount) +static void P_AddWeaponAmmoCommon(DukePlayer_t *ps, int32_t weap, int32_t amount) { P_AddAmmo(weap, ps, amount); if (ps->curr_weapon == KNEE_WEAPON && (ps->gotweapon & (1 << weap))) - addweapon_maybeswitch(ps, weap); + P_AddWeaponMaybeSwitch(ps, weap); } static int32_t VM_AddWeapon(int32_t weap, int32_t amount, DukePlayer_t *ps) @@ -791,7 +792,7 @@ static int32_t VM_AddWeapon(int32_t weap, int32_t amount, DukePlayer_t *ps) if ((ps->gotweapon & (1 << weap)) == 0) { - addweapon_maybeswitch(ps, weap); + P_AddWeaponMaybeSwitch(ps, weap); } else if (ps->ammo_amount[weap] >= ps->max_ammo_amount[weap]) { @@ -799,7 +800,7 @@ static int32_t VM_AddWeapon(int32_t weap, int32_t amount, DukePlayer_t *ps) return 2; } - addweapon_addammo_common(ps, weap, amount); + P_AddWeaponAmmoCommon(ps, weap, amount); return 0; } @@ -1409,7 +1410,7 @@ skip_check: break; } - addweapon_addammo_common(ps, weap, amount); + P_AddWeaponAmmoCommon(ps, weap, amount); continue; } diff --git a/polymer/eduke32/source/lunatic/control.lua b/polymer/eduke32/source/lunatic/control.lua index a0a86da73..2fcaddbd6 100644 --- a/polymer/eduke32/source/lunatic/control.lua +++ b/polymer/eduke32/source/lunatic/control.lua @@ -4,12 +4,15 @@ local ffi = require("ffi") local ffiC = ffi.C local bit = require("bit") +local math = require("math") local setmetatable = setmetatable local error = error local type = type +local player = player + module(...) @@ -123,3 +126,55 @@ end function rnd(x) return (bit.rshift(ffiC.krand(), 8) >= (255-x)) end + + +---=== Weapon stuff + + +--- Helper functions (might be exported later) + +local function have_weapon(ps, weap) + return (bit.band(ps.gotweapon, bit.lshift(1, weap)) ~= 0) +end + +local function have_ammo_at_max(ps, weap) + return (ps:get_ammo_amount(weap) >= ps:get_max_ammo_amount(weap)) +end + +local function P_AddAmmo(ps, weap, amount) + if (not have_ammo_at_max(ps, weap)) then + -- NOTE: no clamping towards the bottom + ps:set_ammo_amount(weap, math.min(curamount+amount, maxamount)) + end +end + +local function P_AddWeaponAmmoCommon(ps, weap, amount) + P_AddAmmo(ps, weap, amount) + + if (ps.curr_weapon==KNEE_WEAPON and have_weapon(weap)) then + ffiC.P_AddWeaponMaybeSwitch(ps, weap); + end +end + + +--- Exported functions + +-- The return value is true iff the ammo was at the weapon's max. +-- In that case, no action is taken. +function addammo(ps, weapon, amount) + return have_ammo_at_max(ps, weap) or P_AddWeaponAmmoCommon(ps, weap, amount) +end + +function addweapon(ps, weapon, amount) + if (weap >= ffiC.MAX_WEAPONS+0ULL) then + error("Invalid weapon ID "..weap, 2) + end + + if (not have_weapon(ps, weap)) then + ffiC.P_AddWeaponMaybeSwitch(ps, weap); + elseif (have_ammo_at_max(ps, weap)) then + return true + end + + P_AddWeaponAmmoCommon(ps, weap, amount) +end diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 4014c2515..707f21503 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -177,7 +177,9 @@ local DUKEPLAYER_STRUCT = [[ int16_t newowner, jumping_counter, airleft; int16_t fta, ftq, access_wallnum, access_spritenum; int16_t got_access, weapon_ang, visibility; - int16_t somethingonplayer, on_crane, i, one_parallax_sectnum; + int16_t somethingonplayer, on_crane; + const int16_t i; + int16_t one_parallax_sectnum; int16_t random_club_frame, one_eighty_count; int16_t dummyplayersprite, extra_extra8; int16_t actorsqu, timebeforeexit, customexitsound, last_pissed_time; @@ -209,7 +211,8 @@ local DUKEPLAYER_STRUCT = [[ uint8_t walking_snd_toggle, palookup, hard_landing, fist_incs; int8_t numloogs, loogcnt, scream_voice; - int8_t last_weapon, cheat_phase, weapon_pos, wantweaponfire, curr_weapon; + int8_t last_weapon, cheat_phase, weapon_pos, wantweaponfire; + int8_t const curr_weapon; palette_t pals; @@ -413,6 +416,7 @@ playerdata_t g_player[MAXPLAYERS]; const int32_t playerswhenstarted; int32_t A_IncurDamage(int32_t sn); // not bound-checked! +void P_AddWeaponMaybeSwitch(DukePlayer_t *ps, int32_t weap); ]] -- functions @@ -478,6 +482,7 @@ local function check_literal_am(am, typename) end local actor_ptr_ct = ffi.typeof("actor_u_t *") -- an unrestricted actor_t pointer +local player_ptr_ct = ffi.typeof("DukePlayer_u_t *") 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") @@ -614,8 +619,46 @@ ffi.metatype("actor_t", actor_mt) --- default defines etc. local con_lang = require("con_lang") +local function check_weapon_idx(weap) + if (weap >= ffiC.MAX_WEAPONS+0ULL) then + error("Invalid weapon ID "..weap, 3) + end +end + local player_mt = { __index = { + --- Getters/setters + get_ammo_amount = function(p, weap) + check_weapon_idx(weap) + return ffi.cast(player_ptr_ct, p).ammo_amount[weap] + end, + + set_ammo_amount = function(p, weap, amount) + check_weapon_idx(weap) + ffi.cast(player_ptr_ct, p).ammo_amount[weap] = amount + end, + + get_max_ammo_amount = function(p, weap) + check_weapon_idx(weap) + return ffi.cast(player_ptr_ct, p).max_ammo_amount[weap] + end, + + set_max_ammo_amount = function(p, weap, amount) + check_weapon_idx(weap) + ffi.cast(player_ptr_ct, p).max_ammo_amount[weap] = amount + end, + + set_curr_weapon = function(p, weap) + check_weapon_idx(weap) + ffi.cast(player_ptr_ct, p).curr_weapon = 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, + addweapon = con.addweapon, + + --- Convenient wrappers around engine functions cansee = function(p, otherpos, othersect) return cansee(p.pos, sprite[p.i].sectnum, otherpos, othersect) end, @@ -700,7 +743,7 @@ local function readintostr(fn) if (sz < 0) then ffi.kclose(fd) - error("INTERNAL ERROR: kfilelength() returned negative length", 5) + error("INTERNAL ERROR: kfilelength() returned negative length", ERRLEV) end local str = ffi.new("char [?]", sz) -- XXX: what does it do on out of mem? diff --git a/polymer/eduke32/source/lunatic/defs_common.lua b/polymer/eduke32/source/lunatic/defs_common.lua index ea56304fc..6b7c9a3a7 100644 --- a/polymer/eduke32/source/lunatic/defs_common.lua +++ b/polymer/eduke32/source/lunatic/defs_common.lua @@ -395,7 +395,7 @@ function cansee(pos1,sect1, pos2,sect2) local ret = ffiC.cansee(pos1.x,pos1.y,pos1.z, sect1, pos2.x,pos2.y,pos2.z, sect2) - return (ret~=0) and true or false + return (ret~=0) end -- TODO: should these rather be one function, and the specific kind of updating diff --git a/polymer/eduke32/source/lunatic/dynsymlist b/polymer/eduke32/source/lunatic/dynsymlist index 51e54eb82..09042ab48 100644 --- a/polymer/eduke32/source/lunatic/dynsymlist +++ b/polymer/eduke32/source/lunatic/dynsymlist @@ -58,8 +58,6 @@ g_player; playerswhenstarted; -A_IncurDamage; - luaJIT_BC_lunacon; luaJIT_BC_con_lang; luaJIT_BC_geom; @@ -71,4 +69,7 @@ luaJIT_BC_control; rand_jkiss_u32; rand_jkiss_dbl; md4once; + +A_IncurDamage; +P_AddWeaponMaybeSwitch; }; diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index ed1324acd..57dc0ef50 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -151,11 +151,8 @@ local LABEL_FUNCNAME = { [2]="move", [3]="ai", [5]="action" } local g_labeldef = {} -- Lua numbers for numbers, strings for composites local g_labeltype = {} -local TEMP_numlookups = {} local function reset_labels() - TEMP_numlookups = {} - -- NO is also a valid `move', `ai' or `action', but they are handled -- separately in lookup_composite(). g_labeldef = { NO=0 } @@ -240,12 +237,6 @@ local function lookup_composite(labeltype, pos, identifier) -- Generate a quoted identifier name. val = format("%q", identifier) - if (TEMP_numlookups[identifier]) then - TEMP_numlookups[identifier] = TEMP_numlookups[identifier]+1 - else - TEMP_numlookups[identifier] = 1 - end - return val end @@ -1570,18 +1561,6 @@ if (not _EDUKE32_LUNATIC) then print(msg) end - -- TEMP - local n, numl = 0, 0 - local ll = {[1]=0, [2]=0, [3]=0} - for id,numlookups in pairs(TEMP_numlookups) do - numl = numl+numlookups - n = n+1 - if (numlookups<=3) then - ll[numlookups] = ll[numlookups]+1 - end - end - printf("avg. lookups: %f (%d %d %d)", numl/n, ll[1],ll[2],ll[3]) - --[[ local file = io.stdout for filename,codetab in pairs(g_file_code) do