From 658a0d690f64fbed1f2b9747660bf94a3497c8a0 Mon Sep 17 00:00:00 2001 From: Alan Shen Date: Sun, 8 Nov 2026 23:07:12 -0700 Subject: [PATCH 1/2] Resume a reload after the magazine is unloaded Behind sv_neo_reload_experimental, a weapon whose script has a ReloadPhases block (only the ZR68C here) keeps its reload step across a weapon switch: switched after the unload, the next draw plays the resume sequence named in the phases file, loads the magazine and plays the remaining sound pieces. A switch before the unload still cancels the reload, and a dropped weapon cancels it. The parser reads the ReloadPhases block and stores tables only for weapons that have one. Master's Reload, FinishReload, Deploy, Holster and ItemBusyFrame each gain one call into a hook that checks the flag. The flag is read once per map into the game rules. The third-person reload gesture adopts the new weapon on a resumed reload, so NEO's weapon-switch reset does not clear it. The viewmodel reads the weapon data by reference instead of copying it every frame. --- .gitignore | 3 + game/neo/scripts/game_sounds_weapons.txt | 30 ++ game/neo/scripts/weapon_zr68c.txt | 5 + src/game/server/neo/neo_player.cpp | 3 + .../shared/hl2mp/hl2mp_playeranimstate.cpp | 5 + src/game/shared/hl2mp/hl2mp_playeranimstate.h | 3 + src/game/shared/neo/neo_gamerules.cpp | 9 + src/game/shared/neo/neo_gamerules.h | 2 + .../shared/neo/neo_predicted_viewmodel.cpp | 2 +- .../shared/neo/weapons/neo_weapon_parse.cpp | 75 +++++ .../shared/neo/weapons/neo_weapon_parse.h | 56 ++++ .../weapons/weapon_neobasecombatweapon.cpp | 311 +++++++++++++++++- .../neo/weapons/weapon_neobasecombatweapon.h | 33 ++ 13 files changed, 534 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3d541782cc..590e093b19 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,9 @@ out # Match session restore file /game/neo/scripts/match_session_restore_point.txt +# Reload phases copied from neoAssets +/game/neo/scripts/weapon_*_reload_phases.txt + # Shader state debug image dumps /game/neo/*.tga diff --git a/game/neo/scripts/game_sounds_weapons.txt b/game/neo/scripts/game_sounds_weapons.txt index ad9ac75c4f..f07bfa6586 100644 --- a/game/neo/scripts/game_sounds_weapons.txt +++ b/game/neo/scripts/game_sounds_weapons.txt @@ -759,6 +759,36 @@ "wave" "weapons/zr68/zr68_reload.wav" } +// ZR68 reload in per-step pieces (sv_neo_reload_experimental), named reload_; a step's later sounds add _part2, _part3. +// Each file starts just before its first transient + +"weapon_zr68.reload_load" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_load.wav" +} + +"weapon_zr68.reload_load_part2" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_load_part2.wav" +} + +"weapon_zr68.reload_cycle" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_cycle.wav" +} + //------------------- "weapon_zr68.npc_reload" diff --git a/game/neo/scripts/weapon_zr68c.txt b/game/neo/scripts/weapon_zr68c.txt index 38f426b6d0..d7f86f9791 100644 --- a/game/neo/scripts/weapon_zr68c.txt +++ b/game/neo/scripts/weapon_zr68c.txt @@ -1,3 +1,5 @@ +#base "weapon_zr68c_reload_phases.txt" + // ZR68S WeaponData @@ -16,6 +18,9 @@ WeaponData "Penetration" "28.0" "CycleTime" "0.090909" // time between shots "ReloadReadyBeforeEnd" "0.483" // the reload is done at the last reload click, this long before its animation ends + + // Reload phases (sv_neo_reload_experimental): the rows come from neoAssets, timed from its Blender markers + "ReloadPhases" {} "TPMuzzleFlashScale" "0.5" diff --git a/src/game/server/neo/neo_player.cpp b/src/game/server/neo/neo_player.cpp index e09dfbe8b9..6e038c727f 100644 --- a/src/game/server/neo/neo_player.cpp +++ b/src/game/server/neo/neo_player.cpp @@ -2485,6 +2485,8 @@ void CNEO_Player::Weapon_DropOnDeath(CNEOBaseCombatWeapon* pNeoWeapon, Vector da pNeoWeapon->AddEffects(EF_BONEMERGE); } + pNeoWeapon->StopReloadOnDrop(); + Vector playerVelocity = vec3_origin; GetVelocity(&playerVelocity, nullptr); pNeoWeapon->Drop(playerVelocity); @@ -2953,6 +2955,7 @@ void CNEO_Player::Weapon_Drop( CBaseCombatWeapon *pWeapon, return; // Return early to not drop weapon } + neoWeapon->StopReloadOnDrop(); pWeapon->m_bInReload = false; pWeapon->StopWeaponSound(RELOAD_NPC); diff --git a/src/game/shared/hl2mp/hl2mp_playeranimstate.cpp b/src/game/shared/hl2mp/hl2mp_playeranimstate.cpp index 9e441acd87..3d12e77d44 100644 --- a/src/game/shared/hl2mp/hl2mp_playeranimstate.cpp +++ b/src/game/shared/hl2mp/hl2mp_playeranimstate.cpp @@ -343,6 +343,11 @@ void CHL2MPPlayerAnimState::DoAnimationEvent( PlayerAnimEvent_t event, int nData { #ifdef NEO AddToGestureSlot(GESTURE_SLOT_ATTACK_AND_RELOAD, CalcReloadSequence(), true); + if (nData == RELOAD_EVENT_RESUMED) + { + // A reload resumed by the draw starts with the weapon change, which would otherwise clear the gesture + m_hActiveWeapon = GetBasePlayer()->GetActiveWeapon(); + } #else // Weapon reload. if ( GetBasePlayer()->GetFlags() & FL_DUCKING ) diff --git a/src/game/shared/hl2mp/hl2mp_playeranimstate.h b/src/game/shared/hl2mp/hl2mp_playeranimstate.h index 9a05125539..6b59010f62 100644 --- a/src/game/shared/hl2mp/hl2mp_playeranimstate.h +++ b/src/game/shared/hl2mp/hl2mp_playeranimstate.h @@ -47,6 +47,9 @@ class CHL2MPPlayerAnimState : public CMultiPlayerAnimState virtual float GetCurrentMaxGroundSpeed(); #ifdef NEO virtual void ComputeSequences(CStudioHdr* pStudioHdr); + + // Reload event data: the draw resumes an interrupted reload + static constexpr int RELOAD_EVENT_RESUMED = 1; #endif void DebugShowAnimStateFull(int iStartLine); diff --git a/src/game/shared/neo/neo_gamerules.cpp b/src/game/shared/neo/neo_gamerules.cpp index 38e7155f74..45fceb7356 100644 --- a/src/game/shared/neo/neo_gamerules.cpp +++ b/src/game/shared/neo/neo_gamerules.cpp @@ -42,6 +42,7 @@ #include extern ConVar weaponstay; +extern ConVar sv_neo_reload_experimental; #endif ConVar sv_neo_loopback_warmup_round("sv_neo_loopback_warmup_round", "0", FCVAR_REPLICATED, "Allow loopback server to do warmup rounds.", true, 0.0f, true, 1.0f); @@ -306,6 +307,7 @@ BEGIN_NETWORK_TABLE_NOBASE( CNEORules, DT_NEORules ) RecvPropInt(RECVINFO(m_iForcedSkin)), RecvPropInt(RECVINFO(m_iForcedWeapon)), RecvPropBool(RECVINFO(m_bCyberspaceLevel)), + RecvPropBool(RECVINFO(m_bReloadExperimental)), RecvPropString(RECVINFO(m_szNeoJinraiClantag)), RecvPropString(RECVINFO(m_szNeoNSFClantag)), RecvPropInt(RECVINFO(m_iGhosterTeam)), @@ -349,6 +351,7 @@ BEGIN_NETWORK_TABLE_NOBASE( CNEORules, DT_NEORules ) SendPropInt(SENDINFO(m_iForcedSkin)), SendPropInt(SENDINFO(m_iForcedWeapon)), SendPropBool(SENDINFO(m_bCyberspaceLevel)), + SendPropBool(SENDINFO(m_bReloadExperimental)), SendPropString(SENDINFO(m_szNeoJinraiClantag)), SendPropString(SENDINFO(m_szNeoNSFClantag)), SendPropInt(SENDINFO(m_iGhosterTeam), NumBitsForCount(TEAM__TOTAL), SPROP_UNSIGNED), @@ -705,6 +708,12 @@ CNEORules::CNEORules() m_iForcedSkin = -1; m_iForcedWeapon = -1; m_bCyberspaceLevel = false; +#ifdef GAME_DLL + // Read once per map so that every weapon, and the client's prediction of it, sees one value for the whole map + m_bReloadExperimental = sv_neo_reload_experimental.GetBool(); +#else + m_bReloadExperimental = false; +#endif m_iClassLimitRecon = -1; m_iClassLimitAssault = -1; diff --git a/src/game/shared/neo/neo_gamerules.h b/src/game/shared/neo/neo_gamerules.h index 1bdee613ae..32e789c521 100644 --- a/src/game/shared/neo/neo_gamerules.h +++ b/src/game/shared/neo/neo_gamerules.h @@ -244,6 +244,7 @@ class CNEORules : public CHL2MPRules, public CGameEventListener int GetForcedSkin(); int GetForcedWeapon(); bool IsCyberspace(); + bool IsReloadExperimental() const { return m_bReloadExperimental; } virtual const char* GetGameTypeName(void) OVERRIDE; bool CanChangeTeamClassLoadoutWhenAlive(); bool CanRespawnAnyTime(); @@ -555,6 +556,7 @@ class CNEORules : public CHL2MPRules, public CGameEventListener CNetworkVar(int, m_iForcedSkin); CNetworkVar(int, m_iForcedWeapon); CNetworkVar(bool, m_bCyberspaceLevel); + CNetworkVar(bool, m_bReloadExperimental); CNetworkVar(int, m_nGameTypeSelected); CNetworkVar(int, m_iRoundNumber); CNetworkVar(bool, m_bIsMatchPoint); diff --git a/src/game/shared/neo/neo_predicted_viewmodel.cpp b/src/game/shared/neo/neo_predicted_viewmodel.cpp index fd2ff5bed0..4349c215d3 100644 --- a/src/game/shared/neo/neo_predicted_viewmodel.cpp +++ b/src/game/shared/neo/neo_predicted_viewmodel.cpp @@ -487,7 +487,7 @@ void CNEOPredictedViewModel::CalcViewModelView(CBasePlayer *pOwner, return; } - CNEOWeaponInfo data = weapon->GetNEOWpnData(); + const CNEOWeaponInfo &data = weapon->GetNEOWpnData(); Vector vForward, vRight, vUp, newPos, vOffset; QAngle newAng, angOffset; diff --git a/src/game/shared/neo/weapons/neo_weapon_parse.cpp b/src/game/shared/neo/weapons/neo_weapon_parse.cpp index bafe4597b6..eee8e991f1 100644 --- a/src/game/shared/neo/weapons/neo_weapon_parse.cpp +++ b/src/game/shared/neo/weapons/neo_weapon_parse.cpp @@ -31,6 +31,80 @@ CNEOWeaponInfo::CNEOWeaponInfo() } +// Weapon-script names of the reload phases, by ReloadPhase_t +static constexpr const char *RELOAD_PHASE_NAMES[RELOAD_PHASE_COUNT] = { "", "1_unload", "2_load", "3_cycle" }; +// Weapon-script keys of a phase's sound pieces: { sound name, time } +static constexpr const char *RELOAD_SOUND_KEYS[RELOAD_SOUNDS_PER_ROW][2] = { + { "sound_name", "sound" }, { "sound2_name", "sound2" }, { "sound3_name", "sound3" } +}; + +static ReloadPhase_t ReloadPhaseFromName(const char *pszName) +{ + for (int i = RELOAD_PHASE_UNLOADED; i < RELOAD_PHASE_COUNT; ++i) + { + if (V_stricmp(pszName, RELOAD_PHASE_NAMES[i]) == 0) + { + return static_cast(i); + } + } + return RELOAD_PHASE_NONE; +} + +// "ReloadPhases" { "" { "" { "ammo" "s" "sound" "s" "sound_name" "name" "resume" "sequence" } ... } ... } +void CNEOWeaponInfo::ParseReloadPhases(KeyValues *pBlock) +{ + m_reloadTables.Purge(); + if (!pBlock) + { + return; + } + + if (!pBlock->GetFirstTrueSubKey()) + { + Warning("%s: \"ReloadPhases\" is empty (is its neoAssets #base file missing?); reloading without phases\n", szClassName); + return; + } + + for (KeyValues *pSeq = pBlock->GetFirstTrueSubKey(); pSeq && m_reloadTables.Count() < RELOAD_PHASE_TABLES_MAX; pSeq = pSeq->GetNextTrueSubKey()) + { + ReloadPhaseTable_t &table = m_reloadTables[m_reloadTables.AddToTail()]; + V_strncpy(table.szSequence, pSeq->GetName(), sizeof(table.szSequence)); + table.iNumRows = 0; + + for (KeyValues *pRow = pSeq->GetFirstTrueSubKey(); pRow && table.iNumRows < RELOAD_PHASE_ROWS_MAX; pRow = pRow->GetNextTrueSubKey()) + { + const ReloadPhase_t phase = ReloadPhaseFromName(pRow->GetName()); + if (phase == RELOAD_PHASE_NONE) + { + Warning("%s: unknown reload phase \"%s\" in \"%s\"\n", szClassName, pRow->GetName(), table.szSequence); + continue; + } + + ReloadPhaseRow_t &row = table.rows[table.iNumRows++]; + row.phase = phase; + row.flAmmo = pRow->GetFloat("ammo"); + for (int j = 0; j < RELOAD_SOUNDS_PER_ROW; ++j) + { + row.sounds[j].flAt = pRow->GetFloat(RELOAD_SOUND_KEYS[j][1]); + V_strncpy(row.sounds[j].szName, pRow->GetString(RELOAD_SOUND_KEYS[j][0]), sizeof(row.sounds[j].szName)); + } + V_strncpy(row.szResume, pRow->GetString("resume"), sizeof(row.szResume)); + } + } +} + +int CNEOWeaponInfo::FindReloadTable(const char *pszSequence) const +{ + for (int i = 0; i < m_reloadTables.Count(); ++i) + { + if (V_stricmp(m_reloadTables[i].szSequence, pszSequence) == 0) + { + return i; + } + } + return -1; +} + void CNEOWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName ) { BaseClass::Parse( pKeyValuesData, szWeaponName ); @@ -39,6 +113,7 @@ void CNEOWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName m_iBullets = pKeyValuesData->GetInt( "Bullets", 1 ); m_flCycleTime = pKeyValuesData->GetFloat( "CycleTime", 0.15 ); m_flReloadReadyBeforeEnd = pKeyValuesData->GetFloat( "ReloadReadyBeforeEnd", 0.0f ); + ParseReloadPhases(pKeyValuesData->FindKey("ReloadPhases")); const char *notFoundStr = "notfound"; Q_strncpy(szViewModel2, pKeyValuesData->GetString("team2viewmodel", notFoundStr), MAX_WEAPON_STRING); diff --git a/src/game/shared/neo/weapons/neo_weapon_parse.h b/src/game/shared/neo/weapons/neo_weapon_parse.h index 040dc00649..8decaa120e 100644 --- a/src/game/shared/neo/weapons/neo_weapon_parse.h +++ b/src/game/shared/neo/weapons/neo_weapon_parse.h @@ -11,6 +11,55 @@ #endif #include "hl2mp_weapon_parse.h" +#include "utlvector.h" + +// A reload is a linear sequence of phases; a reload sequence finishes each phase at a point of its animation +enum ReloadPhase_t +{ + RELOAD_PHASE_NONE = 0, // no reload in progress, or none pending + RELOAD_PHASE_UNLOADED, // 1_unload: the old magazine is out + RELOAD_PHASE_LOADED, // 2_load: the fresh magazine is in + RELOAD_PHASE_CYCLED, // 3_cycle: the bolt or charging handle chambered a round + + RELOAD_PHASE_COUNT +}; + +// bits to network a phase +static constexpr int RELOAD_PHASE_BITS = 2; +static_assert((1 << RELOAD_PHASE_BITS) >= RELOAD_PHASE_COUNT, "RELOAD_PHASE_BITS cannot hold every phase"); +static constexpr int RELOAD_PHASE_TABLES_MAX = 12; +// bits to network a table index, signed so that -1 (no table) fits +static constexpr int RELOAD_PHASE_TABLE_BITS = 5; +static_assert((1 << (RELOAD_PHASE_TABLE_BITS - 1)) >= RELOAD_PHASE_TABLES_MAX, "RELOAD_PHASE_TABLE_BITS cannot hold every table index"); +static constexpr int RELOAD_PHASE_ROWS_MAX = RELOAD_PHASE_COUNT - 1; +static constexpr int RELOAD_SOUND_NAME_MAX = 64; +static constexpr int RELOAD_SEQUENCE_NAME_MAX = 32; +// a phase may need more than one sound piece, such as loading a magazine and the tap after it +static constexpr int RELOAD_SOUNDS_PER_ROW = 3; + +// A sound piece of a phase +struct ReloadSoundPiece_t +{ + float flAt; // when the piece's first transient plays, seconds from the sequence start + char szName[RELOAD_SOUND_NAME_MAX]; +}; + +// One phase of a reload sequence: when its ammo counts, and the sound pieces that go with it +struct ReloadPhaseRow_t +{ + ReloadPhase_t phase; + float flAmmo; // seconds from the sequence start + ReloadSoundPiece_t sounds[RELOAD_SOUNDS_PER_ROW]; + char szResume[RELOAD_SEQUENCE_NAME_MAX]; // the sequence that resumes a reload interrupted after this phase, or empty +}; + +// The phases one reload sequence carries, in order +struct ReloadPhaseTable_t +{ + char szSequence[RELOAD_SEQUENCE_NAME_MAX]; + int iNumRows; + ReloadPhaseRow_t rows[RELOAD_PHASE_ROWS_MAX]; +}; //-------------------------------------------------------------------------------------------------------- class CNEOWeaponInfo : public CHL2MPSWeaponInfo @@ -25,6 +74,8 @@ class CNEOWeaponInfo : public CHL2MPSWeaponInfo int m_iBullets; float m_flCycleTime; float m_flReloadReadyBeforeEnd; + // Empty: the weapon reloads without phases. Allocated only for scripts with a ReloadPhases block. + CUtlVector m_reloadTables; char szViewModel2[MAX_WEAPON_STRING]; // Team 2 (NSF) wep vm char szBulletCharacter[MAX_BULLET_CHARACTER];// character used to display ammunition in current clip char szDeathIcon[MAX_BULLET_CHARACTER]; @@ -39,6 +90,11 @@ class CNEOWeaponInfo : public CHL2MPSWeaponInfo float m_flVMAimFov; Vector m_vecVMAimPosOffset; QAngle m_angVMAimAngOffset; + + int FindReloadTable(const char *pszSequence) const; + +private: + void ParseReloadPhases(::KeyValues *pBlock); }; diff --git a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp index 979d70f8a7..7d100292a3 100644 --- a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp +++ b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp @@ -22,10 +22,10 @@ extern ConVar weaponstay; #include "in_main.h" #else #include "items.h" -#include "neo_gamerules.h" #endif // CLIENT_DLL #include "basecombatweapon_shared.h" +#include "neo_gamerules.h" #include @@ -42,6 +42,10 @@ BEGIN_NETWORK_TABLE( CNEOBaseCombatWeapon, DT_NEOBaseCombatWeapon ) RecvPropInt(RECVINFO(m_nNumShotsFired)), RecvPropBool(RECVINFO(m_bTriggerReset)), RecvPropTime(RECVINFO(m_flReloadReadyTime)), + RecvPropInt(RECVINFO(m_nReloadPhase)), + RecvPropInt(RECVINFO(m_nReloadTable)), + RecvPropTime(RECVINFO(m_flReloadStart)), + RecvPropInt(RECVINFO(m_nReloadSoundsDone)), RecvPropInt(RECVINFO(m_spawnflags)), #else SendPropTime(SENDINFO(m_flSoonestAttack)), @@ -50,6 +54,10 @@ BEGIN_NETWORK_TABLE( CNEOBaseCombatWeapon, DT_NEOBaseCombatWeapon ) SendPropInt(SENDINFO(m_nNumShotsFired)), SendPropBool(SENDINFO(m_bTriggerReset)), SendPropTime(SENDINFO(m_flReloadReadyTime)), + SendPropInt(SENDINFO(m_nReloadPhase), RELOAD_PHASE_BITS, SPROP_UNSIGNED), + SendPropInt(SENDINFO(m_nReloadTable), RELOAD_PHASE_TABLE_BITS), + SendPropTime(SENDINFO(m_flReloadStart)), + SendPropInt(SENDINFO(m_nReloadSoundsDone), RELOAD_PHASE_ROWS_MAX * RELOAD_SOUNDS_PER_ROW, SPROP_UNSIGNED), SendPropInt(SENDINFO(m_spawnflags)), SendPropExclude("DT_BaseAnimating", "m_nSequence"), #endif @@ -63,6 +71,10 @@ BEGIN_PREDICTION_DATA(CNEOBaseCombatWeapon) DEFINE_PRED_FIELD(m_nNumShotsFired, FIELD_INTEGER, FTYPEDESC_INSENDTABLE), DEFINE_PRED_FIELD(m_bTriggerReset, FIELD_BOOLEAN, FTYPEDESC_INSENDTABLE), DEFINE_PRED_FIELD(m_flReloadReadyTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE), + DEFINE_PRED_FIELD(m_nReloadPhase, FIELD_INTEGER, FTYPEDESC_INSENDTABLE), + DEFINE_PRED_FIELD(m_nReloadTable, FIELD_INTEGER, FTYPEDESC_INSENDTABLE), + DEFINE_PRED_FIELD(m_flReloadStart, FIELD_FLOAT, FTYPEDESC_INSENDTABLE), + DEFINE_PRED_FIELD(m_nReloadSoundsDone, FIELD_INTEGER, FTYPEDESC_INSENDTABLE), END_PREDICTION_DATA() #endif @@ -76,11 +88,18 @@ BEGIN_DATADESC( CNEOBaseCombatWeapon ) DEFINE_FIELD(m_nNumShotsFired, FIELD_INTEGER), DEFINE_FIELD(m_bTriggerReset, FIELD_BOOLEAN), DEFINE_FIELD(m_flReloadReadyTime, FIELD_TIME), + DEFINE_FIELD(m_nReloadPhase, FIELD_INTEGER), + DEFINE_FIELD(m_nReloadTable, FIELD_INTEGER), + DEFINE_FIELD(m_flReloadStart, FIELD_TIME), + DEFINE_FIELD(m_nReloadSoundsDone, FIELD_INTEGER), END_DATADESC() #endif ConVar sv_neo_accuracy_penalty_scale("sv_neo_accuracy_penalty_scale", "1.0", FCVAR_REPLICATED, "Scales the accuracy penalty per shot.", true, 0.0f, true, 2.0f); ConVar sv_neo_dynamic_viewkick("sv_neo_dynamic_viewkick", "0", FCVAR_REPLICATED, "Enables view kick scaling based on current inaccuracy.", true, 0.0f, true, 1.0f); +ConVar sv_neo_reload_experimental("sv_neo_reload_experimental", "0", FCVAR_REPLICATED, + "Lets a switch interrupt a reload after the magazine is out, for weapons that define reload phases. A change applies from the next map.", + true, 0.0f, true, 1.0f); const char *GetWeaponByLoadoutId(int id) { @@ -233,6 +252,10 @@ static const WeaponHandlingInfo_t handlingTable[] = { CNEOBaseCombatWeapon::CNEOBaseCombatWeapon( void ) { m_bTriggerReset = true; + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; + m_flReloadStart = 0.0f; + m_nReloadSoundsDone = 0; } const CNEOWeaponInfo &CNEOBaseCombatWeapon::GetNEOWpnData() const @@ -441,6 +464,8 @@ bool CNEOBaseCombatWeapon::Reload( void ) // the animation and the wait to fire still run to the end m_flReloadReadyTime = Max(gpGlobals->curtime, m_flNextPrimaryAttack - GetNEOWpnData().m_flReloadReadyBeforeEnd); + ReloadPhasesOnReload(); + return true; #if(0) @@ -471,6 +496,281 @@ bool CNEOBaseCombatWeapon::Reload( void ) #endif } +// Each reload sound piece's file starts this long before its first transient +static constexpr float RELOAD_SOUND_LEAD = 0.02f; + +bool CNEOBaseCombatWeapon::IsReloadPhaseSystemEnabled() const +{ + return NEORules() && NEORules()->IsReloadExperimental() && !GetNEOWpnData().m_reloadTables.IsEmpty(); +} + +// The phases are those of the sequence the reload plays, such as reload or reloadempty +void CNEOBaseCombatWeapon::ReloadPhasesOnReload() +{ + if (!IsReloadPhaseSystemEnabled()) + { + return; + } + + CBaseViewModel *pVM = ReloadViewModel(); + m_nReloadPhase = RELOAD_PHASE_NONE; + StartReloadPhases(pVM ? GetNEOWpnData().FindReloadTable(pVM->GetSequenceName(pVM->GetSequence())) : -1); +} + +// The magazine is in: the phases up to the load are done, and a switch from here leaves nothing to resume +void CNEOBaseCombatWeapon::ReloadPhasesOnFinish() +{ + if (!IsReloadPhaseSystemEnabled()) + { + return; + } + + m_nReloadPhase = Max(static_cast(m_nReloadPhase.Get()), RELOAD_PHASE_LOADED); +} + +// A reload interrupted by a switch goes on in the draw; returns whether it resumed +bool CNEOBaseCombatWeapon::ReloadPhasesOnDeploy() +{ + if (!IsReloadPhaseSystemEnabled() || m_nReloadPhase == RELOAD_PHASE_NONE) + { + return false; + } + + return ResumeReload(); +} + +// Only a phase that a sequence resumes from outlasts the switch, with the table that names the resume +void CNEOBaseCombatWeapon::ReloadPhasesOnHolster() +{ + if (!IsReloadPhaseSystemEnabled()) + { + return; + } + + StopReloadSoundPieces(); + if (!FindResumeSequence()) + { + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; + } +} + +void CNEOBaseCombatWeapon::ReloadPhasesOnBusyFrame() +{ + if (!IsReloadPhaseSystemEnabled()) + { + return; + } + + RunReloadPhases(); +} + +// The viewmodel sequence that resumes the reload interrupted now, from the phase table of the playing sequence; +// nullptr: none (resume sequences carry no activity, so they are picked by name) +const char *CNEOBaseCombatWeapon::FindResumeSequence() const +{ + const ReloadPhase_t phase = static_cast(m_nReloadPhase.Get()); + if (phase == RELOAD_PHASE_NONE || m_nReloadTable < 0) + { + return nullptr; + } + + // a sequence that starts after the phase is itself the resume from it, interrupted again before its first phase + const ReloadPhaseTable_t &table = GetNEOWpnData().m_reloadTables[m_nReloadTable]; + if (table.iNumRows > 0 && table.rows[0].phase > phase) + { + return table.szSequence; + } + + for (int i = 0; i < table.iNumRows; ++i) + { + if (table.rows[i].phase == phase) + { + return table.rows[i].szResume[0] ? table.rows[i].szResume : nullptr; + } + } + return nullptr; +} + +// Time the phases of a reload sequence that starts now (-1: a sequence without a table finishes none) +void CNEOBaseCombatWeapon::StartReloadPhases(int iTable) +{ + m_nReloadTable = iTable; + m_flReloadStart = gpGlobals->curtime; + m_nReloadSoundsDone = 0; +} + +// Finish every phase of the playing reload sequence whose time has come, and play its sound pieces +void CNEOBaseCombatWeapon::RunReloadPhases() +{ + if (!m_bInReload || m_nReloadTable < 0) + { + return; + } + + const ReloadPhaseTable_t &table = GetNEOWpnData().m_reloadTables[m_nReloadTable]; + const float flElapsed = gpGlobals->curtime - m_flReloadStart; + + // Sound pieces play on their own times, before or after their phase's ammo time + for (int i = 0; i < table.iNumRows; ++i) + { + for (int j = 0; j < RELOAD_SOUNDS_PER_ROW; ++j) + { + const ReloadSoundPiece_t &piece = table.rows[i].sounds[j]; + const int iBit = 1 << (i * RELOAD_SOUNDS_PER_ROW + j); + if (piece.szName[0] && !(m_nReloadSoundsDone & iBit) && flElapsed >= piece.flAt - RELOAD_SOUND_LEAD) + { + m_nReloadSoundsDone |= iBit; + EmitReloadSoundPiece(piece.szName); + } + } + } + + for (int i = 0; i < table.iNumRows; ++i) + { + const ReloadPhaseRow_t &row = table.rows[i]; + if (row.phase <= m_nReloadPhase) + { + continue; + } + + if (flElapsed < row.flAmmo) + { + break; + } + + FinishReloadPhase(row.phase); + } +} + +void CNEOBaseCombatWeapon::FinishReloadPhase(ReloadPhase_t phase) +{ + // The unload takes no rounds: they are lost only if a switch interrupts the reload after it (ResumeReload), + // so an uninterrupted reload shows the same clip as without phases + if (phase == RELOAD_PHASE_LOADED) + { + // the magazine loads now, as at a reload's ready time (ItemBusyFrame) + m_flReloadReadyTime = gpGlobals->curtime; + } + + m_nReloadPhase = phase; +} + +// Resume, in the draw, a reload interrupted by a switch. A model without the resume sequence keeps the old magazine, +// as a cancelled reload does; returns whether the reload resumed +bool CNEOBaseCombatWeapon::ResumeReload() +{ + const char *pszName = FindResumeSequence(); + const int iTable = pszName ? GetNEOWpnData().FindReloadTable(pszName) : -1; + const int iSequence = (iTable >= 0) ? LookupReloadSequence(pszName) : -1; + if (iSequence < 0) + { + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; + return false; + } + + // the old magazine and its rounds are gone, as when a reload loads at its ready time + m_iClip1 = 0; + + // The weapon entity draws its world model in third person, which has none of the viewmodel's sequences + const int iOwnSequence = LookupSequence(pszName); + if (iOwnSequence >= 0) + { + SetSequence(iOwnSequence); + } + SendViewModelAnim(iSequence); + + ToHL2MPPlayer(GetOwner())->DoAnimationEvent(PLAYERANIMEVENT_RELOAD, CHL2MPPlayerAnimState::RELOAD_EVENT_RESUMED); + + const float flDuration = ReloadViewModel()->SequenceDuration(iSequence); + + const float flEnd = gpGlobals->curtime + flDuration; + GetOwner()->SetNextAttack(flEnd); + m_flNextPrimaryAttack = m_flNextSecondaryAttack = flEnd; + SetWeaponIdleTime(flEnd); + + // the load phase loads the magazine + m_flReloadReadyTime = 0.0f; + StartReloadPhases(iTable); + m_bInReload = true; + return true; +} + +// Reload sequences are looked up on the owner's viewmodel, the entity that plays them. The weapon entity's model is not +// always that viewmodel on the client: CalcOverrideModelIndex gives it the world model whenever the local player does +// not draw its viewmodel +CBaseViewModel *CNEOBaseCombatWeapon::ReloadViewModel() const +{ + CBasePlayer *pOwner = ToBasePlayer(GetOwner()); + return pOwner ? pOwner->GetViewModel(m_nViewModelIndex) : nullptr; +} + +int CNEOBaseCombatWeapon::LookupReloadSequence(const char *pszName) const +{ + CBaseViewModel *pVM = ReloadViewModel(); + return pVM ? pVM->LookupSequence(pszName) : -1; +} + +// Sound pieces play on their own channel entries, so a switch or a drop stops them by name, +// as the player stops the single reload sound +void CNEOBaseCombatWeapon::StopReloadSoundPieces() +{ + CBaseCombatCharacter *pOwner = GetOwner(); + if (!pOwner || m_nReloadTable < 0) + { + return; + } + + const ReloadPhaseTable_t &table = GetNEOWpnData().m_reloadTables[m_nReloadTable]; + for (int i = 0; i < table.iNumRows; ++i) + { + for (int j = 0; j < RELOAD_SOUNDS_PER_ROW; ++j) + { + const char *pszName = table.rows[i].sounds[j].szName; + if (pszName[0]) + { + StopSound(pOwner->entindex(), pszName); + } + } + } +} + +// A dropped weapon cancels its reload: the next owner does not resume it +void CNEOBaseCombatWeapon::StopReloadOnDrop() +{ + if (!IsReloadPhaseSystemEnabled()) + { + return; + } + + StopReloadSoundPieces(); + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; +} + +// Play a reload sound piece from the owner, as WeaponSound does: predicted for the owner, from the server for the rest +void CNEOBaseCombatWeapon::EmitReloadSoundPiece(const char *pszSound) +{ + CBaseCombatCharacter *pOwner = GetOwner(); + if (!pOwner) + { + return; + } + + CSoundParameters params; + if (!GetParametersForSound(pszSound, params, NULL)) + { + return; + } + + CPASAttenuationFilter filter(pOwner, params.soundlevel); + if (IsPredicted() && CBaseEntity::GetPredictionPlayer()) + { + filter.UsePredictionRules(); + } + EmitSound(filter, pOwner->entindex(), pszSound); +} //----------------------------------------------------------------------------- // Purpose: Reload has finished. @@ -484,6 +784,8 @@ void CNEOBaseCombatWeapon::FinishReload(void) } m_flReloadReadyTime = 0.0f; + ReloadPhasesOnFinish(); + CBaseCombatCharacter* pOwner = GetOwner(); if (pOwner) @@ -533,7 +835,8 @@ bool CNEOBaseCombatWeapon::Deploy(void) // An empty weapon draws with its slide held back where the model has that draw, // which carries no activity, so it is picked by name static constexpr const char *VM_SEQ_DRAW_EMPTY = "drawempty"; - const int iDrawEmpty = (UsesClipsForAmmo1() && m_iClip1 <= 0) ? LookupSequence(VM_SEQ_DRAW_EMPTY) : ACTIVITY_NOT_AVAILABLE; + const bool bResumed = ReloadPhasesOnDeploy(); + const int iDrawEmpty = (!bResumed && UsesClipsForAmmo1() && m_iClip1 <= 0) ? LookupSequence(VM_SEQ_DRAW_EMPTY) : ACTIVITY_NOT_AVAILABLE; if (iDrawEmpty != ACTIVITY_NOT_AVAILABLE) { SetSequence(iDrawEmpty); @@ -600,6 +903,8 @@ float CNEOBaseCombatWeapon::GetPenetration() const bool CNEOBaseCombatWeapon::Holster(CBaseCombatWeapon* pSwitchingTo) { + ReloadPhasesOnHolster(); + return BaseClass::Holster(pSwitchingTo); } @@ -655,6 +960,8 @@ void CNEOBaseCombatWeapon::ItemBusyFrame(void) BaseClass::ItemBusyFrame(); UpdateInaccuracy(); + ReloadPhasesOnBusyFrame(); + // The owner stays busy until the reload animation ends, but the magazine loads at the reload's ready time if (m_bInReload && m_flReloadReadyTime > 0.0f && gpGlobals->curtime >= m_flReloadReadyTime) { diff --git a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h index a772edda17..a197043f2e 100644 --- a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h +++ b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h @@ -298,6 +298,39 @@ class CNEOBaseCombatWeapon : public CBaseHL2MPCombatWeapon CNEOBaseCombatWeapon(const CNEOBaseCombatWeapon &other); bool HasEmptyIdle(); + // Reload phases (sv_neo_reload_experimental, weapons whose script has a ReloadPhases block). + // Each hook checks IsReloadPhaseSystemEnabled itself, so with the flag off a hook call does nothing +public: + void StopReloadOnDrop(); + +protected: + bool IsReloadPhaseSystemEnabled() const; + + // the last phase finished, kept across a switch + CNetworkVar(int, m_nReloadPhase); + // the phase table of the reload sequence started last, -1 none + CNetworkVar(int, m_nReloadTable); + // when the reload sequence started; phase times are offsets from it + CNetworkVar(float, m_flReloadStart); + // the sound pieces of the playing sequence already started, one bit per piece + CNetworkVar(int, m_nReloadSoundsDone); + +private: + void ReloadPhasesOnReload(); + void ReloadPhasesOnFinish(); + bool ReloadPhasesOnDeploy(); + void ReloadPhasesOnHolster(); + void ReloadPhasesOnBusyFrame(); + + void StartReloadPhases(int iTable); + void RunReloadPhases(); + void FinishReloadPhase(ReloadPhase_t phase); + const char *FindResumeSequence() const; + bool ResumeReload(); + void StopReloadSoundPieces(); + void EmitReloadSoundPiece(const char *pszSound); + CBaseViewModel *ReloadViewModel() const; + int LookupReloadSequence(const char *pszName) const; }; #endif // WEAPON_NEO_BASECOMBATWEAPON_SHARED_H From 86d98ed9aa46738a1021f7f90e9da6e5b69a2bac Mon Sep 17 00:00:00 2001 From: Alan Shen Date: Sun, 8 Nov 2026 23:07:12 -0700 Subject: [PATCH 2/2] Enable experimental reloads by default for review Reviewers can test the resume without setting the cvar. The last PR of the partial-reload stack sets the default back to 0. --- src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp index 7d100292a3..28cd82411b 100644 --- a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp +++ b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp @@ -97,7 +97,9 @@ END_DATADESC() ConVar sv_neo_accuracy_penalty_scale("sv_neo_accuracy_penalty_scale", "1.0", FCVAR_REPLICATED, "Scales the accuracy penalty per shot.", true, 0.0f, true, 2.0f); ConVar sv_neo_dynamic_viewkick("sv_neo_dynamic_viewkick", "0", FCVAR_REPLICATED, "Enables view kick scaling based on current inaccuracy.", true, 0.0f, true, 1.0f); -ConVar sv_neo_reload_experimental("sv_neo_reload_experimental", "0", FCVAR_REPLICATED, +// NEO TODO: Set the default back to 0 before the partial-reload PRs merge. +// It is 1 while they are reviewed, so reviewers can test without setting it. +ConVar sv_neo_reload_experimental("sv_neo_reload_experimental", "1", FCVAR_REPLICATED, "Lets a switch interrupt a reload after the magazine is out, for weapons that define reload phases. A change applies from the next map.", true, 0.0f, true, 1.0f);