From b5c4ab8c47796d5e5e6a5ad2ddfaee73f6e7a438 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 25 Nov 2018 08:17:37 +0100 Subject: [PATCH] - removed all direct access to AWeapon's members to prepare moving this class fully to the script side. Disregarding UI-side and setup-related calls there's unfortunately still 6 places in the native game code which require direct access. --- src/b_think.cpp | 6 ++-- src/ct_chat.cpp | 4 +-- src/d_dehacked.cpp | 36 ++++++++++++--------- src/g_game.cpp | 6 ++-- src/g_inventory/a_weapons.cpp | 2 ++ src/g_inventory/a_weapons.h | 34 ++++++++----------- src/g_shared/shared_hud.cpp | 11 ++++--- src/g_statusbar/sbarinfo.cpp | 18 +++++------ src/g_statusbar/sbarinfo_commands.cpp | 2 +- src/g_statusbar/shared_sbar.cpp | 2 +- src/namedef.h | 18 +++++++++++ src/p_conversation.cpp | 2 +- src/p_enemy.cpp | 2 +- src/p_map.cpp | 2 +- src/p_mobj.cpp | 2 +- src/p_pspr.cpp | 6 ++-- src/scripting/thingdef_properties.cpp | 10 +++--- wadsrc/static/zscript/inventory/weapons.txt | 8 ++--- 18 files changed, 95 insertions(+), 76 deletions(-) diff --git a/src/b_think.cpp b/src/b_think.cpp index 32dd8e6b1..2099c9045 100644 --- a/src/b_think.cpp +++ b/src/b_think.cpp @@ -369,8 +369,10 @@ void DBot::WhatToGet (AActor *item) { if (!weapgiveammo) return; - if ((heldWeapon->Ammo1 == NULL || heldWeapon->Ammo1->Amount >= heldWeapon->Ammo1->MaxAmount) && - (heldWeapon->Ammo2 == NULL || heldWeapon->Ammo2->Amount >= heldWeapon->Ammo2->MaxAmount)) + auto ammo1 = heldWeapon->PointerVar(NAME_Ammo1); + auto ammo2 = heldWeapon->PointerVar(NAME_Ammo2); + if ((ammo1 == NULL || ammo1->Amount >= ammo1->MaxAmount) && + (ammo2 == NULL || ammo2->Amount >= ammo2->MaxAmount)) { return; } diff --git a/src/ct_chat.cpp b/src/ct_chat.cpp index c32a67303..9f131ab8a 100644 --- a/src/ct_chat.cpp +++ b/src/ct_chat.cpp @@ -375,8 +375,8 @@ static bool DoSubstitution (FString &out, const char *in) { player_t *player = &players[consoleplayer]; AWeapon *weapon = player->ReadyWeapon; - auto ammo1 = weapon ? weapon->Ammo1 : nullptr; - auto ammo2 = weapon ? weapon->Ammo2 : nullptr; + auto ammo1 = weapon ? weapon->PointerVar(NAME_Ammo1) : nullptr; + auto ammo2 = weapon ? weapon->PointerVar(NAME_Ammo2) : nullptr; const char *a, *b; a = in; diff --git a/src/d_dehacked.cpp b/src/d_dehacked.cpp index a04ab2240..92b3630cd 100644 --- a/src/d_dehacked.cpp +++ b/src/d_dehacked.cpp @@ -1583,13 +1583,15 @@ static int PatchAmmo (int ammoNum) else if (type->IsDescendantOf (RUNTIME_CLASS(AWeapon))) { AWeapon *defWeap = (AWeapon *)GetDefaultByType (type); - if (defWeap->AmmoType1 == ammoType) + if (defWeap->PointerVar(NAME_AmmoType1) == ammoType) { - defWeap->AmmoGive1 = Scale (defWeap->AmmoGive1, *per, oldclip); + auto &AmmoGive1 = defWeap->IntVar(NAME_AmmoGive1); + AmmoGive1 = Scale (AmmoGive1, *per, oldclip); } - if (defWeap->AmmoType2 == ammoType) + if (defWeap->PointerVar(NAME_AmmoType2) == ammoType) { - defWeap->AmmoGive2 = Scale (defWeap->AmmoGive2, *per, oldclip); + auto &AmmoGive2 = defWeap->IntVar(NAME_AmmoGive2); + AmmoGive2 = Scale (AmmoGive2, *per, oldclip); } } } @@ -1655,13 +1657,15 @@ static int PatchWeapon (int weapNum) { val = 5; } - info->AmmoType1 = AmmoNames[val]; - if (info->AmmoType1 != NULL) + auto &AmmoType = info->PointerVar(NAME_AmmoType1); + AmmoType = AmmoNames[val]; + if (AmmoType != nullptr) { - info->AmmoGive1 = ((AInventory*)GetDefaultByType (info->AmmoType1))->Amount * 2; - if (info->AmmoUse1 == 0) + info->IntVar(NAME_AmmoGive1) = ((AInventory*)GetDefaultByType (AmmoType))->Amount * 2; + auto &AmmoUse = info->IntVar(NAME_AmmoUse1); + if (AmmoUse == 0) { - info->AmmoUse1 = 1; + AmmoUse = 1; } } } @@ -1685,12 +1689,12 @@ static int PatchWeapon (int weapNum) } else if (stricmp (Line1, "Ammo use") == 0 || stricmp (Line1, "Ammo per shot") == 0) { - info->AmmoUse1 = val; + info->IntVar(NAME_AmmoUse1) = val; info->flags6 |= MF6_INTRYMOVE; // flag the weapon for postprocessing (reuse a flag that can't be set by external means) } else if (stricmp (Line1, "Min ammo") == 0) { - info->MinSelAmmo1 = val; + info->IntVar(NAME_MinSelAmmo1) = val; } else { @@ -1698,9 +1702,9 @@ static int PatchWeapon (int weapNum) } } - if (info->AmmoType1 == NULL) + if (info->PointerVar(NAME_AmmoType1) == nullptr) { - info->AmmoUse1 = 0; + info->IntVar(NAME_AmmoUse1) = 0; } if (patchedStates) @@ -3095,8 +3099,8 @@ void FinishDehPatch () } else { - weap->bDehAmmo = true; - weap->AmmoUse1 = 0; + weap->BoolVar(NAME_bDehAmmo) = true; + weap->IntVar(NAME_AmmoUse1) = 0; // to allow proper checks in CheckAmmo we have to find the first attack pointer in the Fire sequence // and set its default ammo use as the weapon's AmmoUse1. @@ -3123,7 +3127,7 @@ void FinishDehPatch () found = true; int use = AmmoPerAttacks[j].ammocount; if (use < 0) use = deh.BFGCells; - weap->AmmoUse1 = use; + weap->IntVar(NAME_AmmoUse1) = use; break; } } diff --git a/src/g_game.cpp b/src/g_game.cpp index eff1b7a3c..1c4304f51 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -799,7 +799,7 @@ static int LookAdjust(int look) if (players[consoleplayer].playerstate != PST_DEAD && // No adjustment while dead. players[consoleplayer].ReadyWeapon != NULL) // No adjustment if no weapon. { - auto scale = players[consoleplayer].ReadyWeapon->FOVScale; + auto scale = players[consoleplayer].ReadyWeapon->FloatVar(NAME_FOVScale); if (scale > 0) // No adjustment if it is non-positive. { look = int(look * scale); @@ -1301,8 +1301,8 @@ void G_PlayerFinishLevel (int player, EFinishLevelType mode, int flags) item = next; } if (p->ReadyWeapon != NULL && - p->ReadyWeapon->WeaponFlags&WIF_POWERED_UP && - p->PendingWeapon == p->ReadyWeapon->SisterWeapon) + p->ReadyWeapon->IntVar(NAME_WeaponFlags) & WIF_POWERED_UP && + p->PendingWeapon == p->ReadyWeapon->PointerVar(NAME_SisterWeapon)) { // Unselect powered up weapons if the unpowered counterpart is pending p->ReadyWeapon=p->PendingWeapon; diff --git a/src/g_inventory/a_weapons.cpp b/src/g_inventory/a_weapons.cpp index 94e65a33c..86819f75e 100644 --- a/src/g_inventory/a_weapons.cpp +++ b/src/g_inventory/a_weapons.cpp @@ -55,6 +55,7 @@ IMPLEMENT_POINTERS_START(AWeapon) IMPLEMENT_POINTER(SisterWeapon) IMPLEMENT_POINTERS_END +/* DEFINE_FIELD(AWeapon, AmmoType1) DEFINE_FIELD(AWeapon, AmmoType2) DEFINE_FIELD(AWeapon, AmmoGive1) @@ -85,6 +86,7 @@ DEFINE_FIELD(AWeapon, GivenAsMorphWeapon) DEFINE_FIELD(AWeapon, bAltFire) DEFINE_FIELD(AWeapon, WeaponFlags) DEFINE_FIELD(AWeapon, bDehAmmo) +*/ //=========================================================================== // diff --git a/src/g_inventory/a_weapons.h b/src/g_inventory/a_weapons.h index 39179999f..8fb67f8d5 100644 --- a/src/g_inventory/a_weapons.h +++ b/src/g_inventory/a_weapons.h @@ -139,7 +139,7 @@ public: int MinAmmo1, MinAmmo2; // Minimum ammo needed to switch to this weapon int AmmoUse1, AmmoUse2; // How much ammo to use with each shot int Kickback; - float YAdjust; // For viewing the weapon fullscreen (visual only so no need to be a double) + double YAdjust; // For viewing the weapon fullscreen (visual only so no need to be a double) FSoundIDNoInit UpSound, ReadySound; // Sounds when coming up and idle PClassActor *SisterWeaponType; // Another weapon to pick up with this one int SelectionOrder; // Lower-numbered weapons get picked first @@ -152,32 +152,24 @@ public: // In-inventory instance variables TObjPtr Ammo1, Ammo2; TObjPtr SisterWeapon; - float FOVScale; + double FOVScale; int Crosshair; // 0 to use player's crosshair - bool GivenAsMorphWeapon; // *** only accessed from ZScript. + bool GivenAsMorphWeapon; - bool bAltFire; // *** only accessed from ZScript. Set when this weapon's alternate fire is used. + bool bAltFire; bool bDehAmmo; void Serialize(FSerializer &arc) override; +}; - enum - { - PrimaryFire, - AltFire, - EitherFire - }; - - enum - { - BobNormal, - BobInverse, - BobAlpha, - BobInverseAlpha, - BobSmooth, - BobInverseSmooth - }; - +enum class EBobStyle +{ + BobNormal, + BobInverse, + BobAlpha, + BobInverseAlpha, + BobSmooth, + BobInverseSmooth }; enum diff --git a/src/g_shared/shared_hud.cpp b/src/g_shared/shared_hud.cpp index 88fc1ecbf..92b664baa 100644 --- a/src/g_shared/shared_hud.cpp +++ b/src/g_shared/shared_hud.cpp @@ -512,7 +512,7 @@ static void AddAmmoToList(AWeapon * weapdef) for (int i = 0; i < 2; i++) { - auto ti = i == 0 ? weapdef->AmmoType1 : weapdef->AmmoType2; + auto ti = weapdef->PointerVar(i == 0 ? NAME_AmmoType1 : NAME_AmmoType2); if (ti) { auto ammodef = (AInventory*)GetDefaultByType(ti); @@ -643,7 +643,7 @@ static int DrawAmmo(player_t *CPlayer, int x, int y) FTextureID icon = !AltIcon.isNull()? AltIcon : inv->Icon; if (!icon.isValid()) continue; - double trans= (wi && (type==wi->AmmoType1 || type==wi->AmmoType2)) ? 0.75 : 0.375; + double trans= (wi && (type==wi->PointerVar(NAME_AmmoType1) || type==wi->PointerVar(NAME_AmmoType2))) ? 0.75 : 0.375; int maxammo = inv->MaxAmount; int ammo = ammoitem? ammoitem->Amount : 0; @@ -741,13 +741,14 @@ static void DrawOneWeapon(player_t * CPlayer, int x, int & y, AWeapon * weapon) double trans; // Powered up weapons and inherited sister weapons are not displayed. - if (weapon->WeaponFlags & WIF_POWERED_UP) return; - if (weapon->SisterWeapon && weapon->IsKindOf(weapon->SisterWeapon->GetClass())) return; + if (weapon->IntVar(NAME_WeaponFlags) & WIF_POWERED_UP) return; + auto SisterWeapon = weapon->PointerVar(NAME_SisterWeapon); + if (SisterWeapon && weapon->IsKindOf(SisterWeapon->GetClass())) return; trans=0.4; if (CPlayer->ReadyWeapon) { - if (weapon==CPlayer->ReadyWeapon || weapon==CPlayer->ReadyWeapon->SisterWeapon) trans = 0.85; + if (weapon==CPlayer->ReadyWeapon || SisterWeapon == CPlayer->ReadyWeapon) trans = 0.85; } FTextureID picnum = GetInventoryIcon(weapon, DI_ALTICONFIRST); diff --git a/src/g_statusbar/sbarinfo.cpp b/src/g_statusbar/sbarinfo.cpp index e0dc3e294..147f32439 100644 --- a/src/g_statusbar/sbarinfo.cpp +++ b/src/g_statusbar/sbarinfo.cpp @@ -1039,22 +1039,22 @@ public: } wrapper->ForceHUDScale(script->huds[hud]->ForceScaled()); - if (CPlayer->ReadyWeapon != NULL) + if (CPlayer->ReadyWeapon != nullptr) { - ammo1 = CPlayer->ReadyWeapon->Ammo1; - ammo2 = CPlayer->ReadyWeapon->Ammo2; - if (ammo1 == NULL) + ammo1 = CPlayer->ReadyWeapon->PointerVar(NAME_Ammo1); + ammo2 = CPlayer->ReadyWeapon->PointerVar(NAME_Ammo2); + if (ammo1 == nullptr) { ammo1 = ammo2; - ammo2 = NULL; + ammo2 = nullptr; } } else { - ammo1 = ammo2 = NULL; + ammo1 = ammo2 = nullptr; } - ammocount1 = ammo1 != NULL ? ammo1->Amount : 0; - ammocount2 = ammo2 != NULL ? ammo2->Amount : 0; + ammocount1 = ammo1 != nullptr ? ammo1->Amount : 0; + ammocount2 = ammo2 != nullptr ? ammo2->Amount : 0; //prepare ammo counts armor = CPlayer->mo->FindInventory(NAME_BasicArmor); @@ -1470,7 +1470,7 @@ public: PClassActor *AmmoType(int no) const { auto w = StatusBar->CPlayer->ReadyWeapon; - return w == nullptr ? nullptr : (no == 1 ? w->AmmoType1 : w->AmmoType2); + return w == nullptr ? nullptr : (w->PointerVar(no == 1 ? NAME_AmmoType1 : NAME_AmmoType2)); } AInventory *ammo1, *ammo2; diff --git a/src/g_statusbar/sbarinfo_commands.cpp b/src/g_statusbar/sbarinfo_commands.cpp index d28298667..2c94970f5 100644 --- a/src/g_statusbar/sbarinfo_commands.cpp +++ b/src/g_statusbar/sbarinfo_commands.cpp @@ -245,7 +245,7 @@ class CommandDrawImage : public SBarInfoCommandFlowControl else if(type == AMMO1) { auto ammo = statusBar->ammo1; - if(ammo != NULL) + if(ammo != NULL) GetIcon(ammo); } else if(type == AMMO2) diff --git a/src/g_statusbar/shared_sbar.cpp b/src/g_statusbar/shared_sbar.cpp index e3430f2cf..52e280d83 100644 --- a/src/g_statusbar/shared_sbar.cpp +++ b/src/g_statusbar/shared_sbar.cpp @@ -175,7 +175,7 @@ void ST_LoadCrosshair(bool alwaysload) players[consoleplayer].camera->player != NULL && players[consoleplayer].camera->player->ReadyWeapon != NULL) { - num = players[consoleplayer].camera->player->ReadyWeapon->Crosshair; + num = players[consoleplayer].camera->player->ReadyWeapon->IntVar(NAME_Crosshair); } if (num == 0) { diff --git a/src/namedef.h b/src/namedef.h index 6dc22c113..c40b16ca6 100644 --- a/src/namedef.h +++ b/src/namedef.h @@ -988,3 +988,21 @@ xx(snd_resampler) xx(ScriptUtil) xx(SetMarineWeapon) xx(SetMarineSprite) + +// Weapon member fields that need direct access +xx(Ammo1) +xx(Ammo2) +xx(AmmoType1) +xx(AmmoType2) +xx(AmmoGive1) +xx(AmmoGive2) +xx(AmmoUse1) +xx(SisterWeapon) +xx(BobStyle) +xx(Kickback) +xx(MinSelAmmo1) +xx(bDehAmmo) +xx(FOVScale) +xx(YAdjust) +xx(Crosshair) +xx(WeaponFlags) \ No newline at end of file diff --git a/src/p_conversation.cpp b/src/p_conversation.cpp index c577a4c2c..d0e67ca7a 100644 --- a/src/p_conversation.cpp +++ b/src/p_conversation.cpp @@ -965,7 +965,7 @@ static void HandleReply(player_t *player, bool isconsole, int nodenum, int reply if (item->GetClass()->TypeName == NAME_FlameThrower) { // The flame thrower gives less ammo when given in a dialog - static_cast(item)->AmmoGive1 = 40; + item->IntVar(NAME_AmmoGive1) = 40; } item->flags |= MF_DROPPED; if (!item->CallTryPickup(player->mo)) diff --git a/src/p_enemy.cpp b/src/p_enemy.cpp index 7f6f376a6..a54603d8f 100644 --- a/src/p_enemy.cpp +++ b/src/p_enemy.cpp @@ -1029,7 +1029,7 @@ void P_NewChaseDir(AActor * actor) { // melee range of player weapon is a parameter of the action function and cannot be checked here. // Add a new weapon property? - ismeleeattacker = ((target->player->ReadyWeapon->WeaponFlags & WIF_MELEEWEAPON) && dist < 192); + ismeleeattacker = ((target->player->ReadyWeapon->IntVar(NAME_WeaponFlags) & WIF_MELEEWEAPON) && dist < 192); } if (ismeleeattacker) { diff --git a/src/p_map.cpp b/src/p_map.cpp index c1d50d453..e6b2e33c9 100644 --- a/src/p_map.cpp +++ b/src/p_map.cpp @@ -4459,7 +4459,7 @@ DAngle P_AimLineAttack(AActor *t1, DAngle angle, double distance, FTranslatedLin { // [BB] Disable autoaim on weapons with WIF_NOAUTOAIM. AWeapon *weapon = t1->player->ReadyWeapon; - if (weapon && (weapon->WeaponFlags & WIF_NOAUTOAIM)) + if ((weapon && (weapon->IntVar(NAME_WeaponFlags) & WIF_NOAUTOAIM)) && !(flags & ALF_NOWEAPONCHECK)) { vrange = 0.5; } diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp index 56b19a5de..5e555e1db 100644 --- a/src/p_mobj.cpp +++ b/src/p_mobj.cpp @@ -7284,7 +7284,7 @@ AActor *P_SpawnPlayerMissile (AActor *source, double x, double y, double z, DAngle vrange = nofreeaim ? 35. : 0.; if (!pLineTarget) pLineTarget = &scratch; - if (source->player && source->player->ReadyWeapon && ((source->player->ReadyWeapon->WeaponFlags & WIF_NOAUTOAIM) || noautoaim)) + if (!(flags & ALF_NOWEAPONCHECK) && source->player && source->player->ReadyWeapon && ((source->player->ReadyWeapon->IntVar(NAME_WeaponFlags) & WIF_NOAUTOAIM) || noautoaim)) { // Keep exactly the same angle and pitch as the player's own aim an = angle; diff --git a/src/p_pspr.cpp b/src/p_pspr.cpp index 88ffca329..82434f148 100644 --- a/src/p_pspr.cpp +++ b/src/p_pspr.cpp @@ -1150,16 +1150,16 @@ float DPSprite::GetYAdjust(bool fullscreen) AWeapon *weapon = dyn_cast(GetCaller()); if (weapon != nullptr) { - float fYAd = weapon->YAdjust; + auto fYAd = weapon->FloatVar(NAME_YAdjust); if (fYAd != 0) { if (fullscreen) { - return fYAd; + return (float)fYAd; } else { - return (float)StatusBar->GetDisplacement() * fYAd; + return (float)(StatusBar->GetDisplacement() * fYAd); } } } diff --git a/src/scripting/thingdef_properties.cpp b/src/scripting/thingdef_properties.cpp index 8701f8f4e..0c5f836b8 100644 --- a/src/scripting/thingdef_properties.cpp +++ b/src/scripting/thingdef_properties.cpp @@ -1181,7 +1181,7 @@ DEFINE_CLASS_PROPERTY(pickupannouncerentry, S, Inventory) //========================================================================== DEFINE_CLASS_PROPERTY(defaultkickback, 0, Weapon) { - defaults->Kickback = gameinfo.defKickback; + defaults->IntVar(NAME_Kickback) = gameinfo.defKickback; } //========================================================================== @@ -1190,9 +1190,9 @@ DEFINE_CLASS_PROPERTY(defaultkickback, 0, Weapon) DEFINE_CLASS_PROPERTY(bobstyle, S, Weapon) { static const char *names[] = { "Normal", "Inverse", "Alpha", "InverseAlpha", "Smooth", "InverseSmooth", NULL }; - static const int styles[] = { AWeapon::BobNormal, - AWeapon::BobInverse, AWeapon::BobAlpha, AWeapon::BobInverseAlpha, - AWeapon::BobSmooth, AWeapon::BobInverseSmooth, }; + static const EBobStyle styles[] = { EBobStyle::BobNormal, + EBobStyle::BobInverse, EBobStyle::BobAlpha, EBobStyle::BobInverseAlpha, + EBobStyle::BobSmooth, EBobStyle::BobInverseSmooth, }; PROP_STRING_PARM(id, 0); int match = MatchString(id, names); if (match < 0) @@ -1200,7 +1200,7 @@ DEFINE_CLASS_PROPERTY(bobstyle, S, Weapon) I_Error("Unknown bobstyle %s", id); match = 0; } - defaults->BobStyle = styles[match]; + defaults->IntVar(NAME_BobStyle) = (int)styles[match]; } //========================================================================== diff --git a/wadsrc/static/zscript/inventory/weapons.txt b/wadsrc/static/zscript/inventory/weapons.txt index b03edbde7..e98dba8e8 100644 --- a/wadsrc/static/zscript/inventory/weapons.txt +++ b/wadsrc/static/zscript/inventory/weapons.txt @@ -10,13 +10,13 @@ class Weapon : StateProvider native const ZOOM_INSTANT = 1; const ZOOM_NOSCALETURNING = 2; - native uint WeaponFlags; + deprecated("3.7") native uint WeaponFlags; // not to be used directly. native class AmmoType1, AmmoType2; // Types of ammo used by self weapon native int AmmoGive1, AmmoGive2; // Amount of each ammo to get when picking up weapon - native int MinAmmo1, MinAmmo2; // not used anywhere. + deprecated("3.7") native int MinAmmo1, MinAmmo2; // not used anywhere and thus deprecated. native int AmmoUse1, AmmoUse2; // How much ammo to use with each shot native int Kickback; - native float YAdjust; // For viewing the weapon fullscreen (visual only so no need to be a double) + native double YAdjust; // For viewing the weapon fullscreen native sound UpSound, ReadySound; // Sounds when coming up and idle native class SisterWeaponType; // Another weapon to pick up with self one native int SelectionOrder; // Lower-numbered weapons get picked first @@ -27,7 +27,7 @@ class Weapon : StateProvider native native float BobRangeX, BobRangeY; // [XA] Bobbing range. Defines how far a weapon bobs in either direction. native Ammo Ammo1, Ammo2; // In-inventory instance variables native Weapon SisterWeapon; - native float FOVScale; + native double FOVScale; native int Crosshair; // 0 to use player's crosshair native bool GivenAsMorphWeapon; native bool bAltFire; // Set when this weapon's alternate fire is used.