Lunatic: player stuff (protect some members etc.)

git-svn-id: https://svn.eduke32.com/eduke32@3249 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-11-30 18:57:55 +00:00
parent b850d04f8d
commit 5b07f67627
6 changed files with 112 additions and 33 deletions

View file

@ -742,7 +742,8 @@ dead:
else vm.g_sp->shade += (sector[vm.g_sp->sectnum].floorshade-vm.g_sp->shade)>>1; 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)) if ((ps->weaponswitch & 1) && (ps->weaponswitch & 4))
{ {
@ -773,12 +774,12 @@ void addweapon_maybeswitch(DukePlayer_t *ps, int32_t weap)
P_AddWeaponNoSwitch(ps, 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); P_AddAmmo(weap, ps, amount);
if (ps->curr_weapon == KNEE_WEAPON && (ps->gotweapon & (1 << weap))) 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) 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) 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]) 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; return 2;
} }
addweapon_addammo_common(ps, weap, amount); P_AddWeaponAmmoCommon(ps, weap, amount);
return 0; return 0;
} }
@ -1409,7 +1410,7 @@ skip_check:
break; break;
} }
addweapon_addammo_common(ps, weap, amount); P_AddWeaponAmmoCommon(ps, weap, amount);
continue; continue;
} }

View file

@ -4,12 +4,15 @@ local ffi = require("ffi")
local ffiC = ffi.C local ffiC = ffi.C
local bit = require("bit") local bit = require("bit")
local math = require("math")
local setmetatable = setmetatable local setmetatable = setmetatable
local error = error local error = error
local type = type local type = type
local player = player
module(...) module(...)
@ -123,3 +126,55 @@ end
function rnd(x) function rnd(x)
return (bit.rshift(ffiC.krand(), 8) >= (255-x)) return (bit.rshift(ffiC.krand(), 8) >= (255-x))
end 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

View file

@ -177,7 +177,9 @@ local DUKEPLAYER_STRUCT = [[
int16_t newowner, jumping_counter, airleft; int16_t newowner, jumping_counter, airleft;
int16_t fta, ftq, access_wallnum, access_spritenum; int16_t fta, ftq, access_wallnum, access_spritenum;
int16_t got_access, weapon_ang, visibility; 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 random_club_frame, one_eighty_count;
int16_t dummyplayersprite, extra_extra8; int16_t dummyplayersprite, extra_extra8;
int16_t actorsqu, timebeforeexit, customexitsound, last_pissed_time; 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; uint8_t walking_snd_toggle, palookup, hard_landing, fist_incs;
int8_t numloogs, loogcnt, scream_voice; 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; palette_t pals;
@ -413,6 +416,7 @@ playerdata_t g_player[MAXPLAYERS];
const int32_t playerswhenstarted; const int32_t playerswhenstarted;
int32_t A_IncurDamage(int32_t sn); // not bound-checked! int32_t A_IncurDamage(int32_t sn); // not bound-checked!
void P_AddWeaponMaybeSwitch(DukePlayer_t *ps, int32_t weap);
]] ]]
-- functions -- functions
@ -478,6 +482,7 @@ local function check_literal_am(am, typename)
end end
local actor_ptr_ct = ffi.typeof("actor_u_t *") -- an unrestricted actor_t pointer 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_action_ct = ffi.typeof("con_action_t")
local con_move_ct = ffi.typeof("con_move_t") local con_move_ct = ffi.typeof("con_move_t")
local con_ai_ct = ffi.typeof("con_ai_t") local con_ai_ct = ffi.typeof("con_ai_t")
@ -614,8 +619,46 @@ ffi.metatype("actor_t", actor_mt)
--- default defines etc. --- default defines etc.
local con_lang = require("con_lang") 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 = { local player_mt = {
__index = { __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) cansee = function(p, otherpos, othersect)
return cansee(p.pos, sprite[p.i].sectnum, otherpos, othersect) return cansee(p.pos, sprite[p.i].sectnum, otherpos, othersect)
end, end,
@ -700,7 +743,7 @@ local function readintostr(fn)
if (sz < 0) then if (sz < 0) then
ffi.kclose(fd) ffi.kclose(fd)
error("INTERNAL ERROR: kfilelength() returned negative length", 5) error("INTERNAL ERROR: kfilelength() returned negative length", ERRLEV)
end end
local str = ffi.new("char [?]", sz) -- XXX: what does it do on out of mem? local str = ffi.new("char [?]", sz) -- XXX: what does it do on out of mem?

View file

@ -395,7 +395,7 @@ function cansee(pos1,sect1, pos2,sect2)
local ret = ffiC.cansee(pos1.x,pos1.y,pos1.z, sect1, local ret = ffiC.cansee(pos1.x,pos1.y,pos1.z, sect1,
pos2.x,pos2.y,pos2.z, sect2) pos2.x,pos2.y,pos2.z, sect2)
return (ret~=0) and true or false return (ret~=0)
end end
-- TODO: should these rather be one function, and the specific kind of updating -- TODO: should these rather be one function, and the specific kind of updating

View file

@ -58,8 +58,6 @@ g_player;
playerswhenstarted; playerswhenstarted;
A_IncurDamage;
luaJIT_BC_lunacon; luaJIT_BC_lunacon;
luaJIT_BC_con_lang; luaJIT_BC_con_lang;
luaJIT_BC_geom; luaJIT_BC_geom;
@ -71,4 +69,7 @@ luaJIT_BC_control;
rand_jkiss_u32; rand_jkiss_u32;
rand_jkiss_dbl; rand_jkiss_dbl;
md4once; md4once;
A_IncurDamage;
P_AddWeaponMaybeSwitch;
}; };

View file

@ -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_labeldef = {} -- Lua numbers for numbers, strings for composites
local g_labeltype = {} local g_labeltype = {}
local TEMP_numlookups = {}
local function reset_labels() local function reset_labels()
TEMP_numlookups = {}
-- NO is also a valid `move', `ai' or `action', but they are handled -- NO is also a valid `move', `ai' or `action', but they are handled
-- separately in lookup_composite(). -- separately in lookup_composite().
g_labeldef = { NO=0 } g_labeldef = { NO=0 }
@ -240,12 +237,6 @@ local function lookup_composite(labeltype, pos, identifier)
-- Generate a quoted identifier name. -- Generate a quoted identifier name.
val = format("%q", identifier) val = format("%q", identifier)
if (TEMP_numlookups[identifier]) then
TEMP_numlookups[identifier] = TEMP_numlookups[identifier]+1
else
TEMP_numlookups[identifier] = 1
end
return val return val
end end
@ -1570,18 +1561,6 @@ if (not _EDUKE32_LUNATIC) then
print(msg) print(msg)
end 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 local file = io.stdout
for filename,codetab in pairs(g_file_code) do for filename,codetab in pairs(g_file_code) do