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..8c42451719 100644 --- a/game/neo/scripts/game_sounds_weapons.txt +++ b/game/neo/scripts/game_sounds_weapons.txt @@ -12,6 +12,18 @@ "wave" "weapons/milso/milso_reload.wav" } +// MilSO reload in per-step pieces (sv_neo_reload_experimental): only the load step has a sound; the slide release +// (3_cycle) is silent in the shipped wav. NEO TODO (sunzenshen): placeholder wav from the research branch. +"weapon_milso.reload_2_load" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "CompatibilityAttenuation" "1.0" + "pitch" "PITCH_NORM" + "wave" "weapons/milso/milso_reload_2_load.wav" +} + //------------------ "weapon_milso.npc_reload" @@ -586,6 +598,47 @@ "wave" "weapons/m41/m41_reload.wav" } +// M41 reload in per-step pieces (sv_neo_reload_experimental), named after the step; _2 and _3 are a step's second and +// third sounds (seat rattle, tap, slap). The ZR68 entries above use ")" in front of the wave; these do not, as +// weapon_m41.reload does not. NEO TODO (sunzenshen): the wavs are placeholders from the research branch; re-cut from +// m41_reload.wav as the ZR68 pieces were (zr68c-sound-align.md). + +"weapon_m41.reload_2_load" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" "weapons/m41/m41_reload_2_load.wav" +} + +"weapon_m41.reload_2_load_2" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" "weapons/m41/m41_reload_2_load_2.wav" +} + +"weapon_m41.reload_2_load_3" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" "weapons/m41/m41_reload_2_load_3.wav" +} + +"weapon_m41.reload_3_cycle" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" "weapons/m41/m41_reload_3_cycle.wav" +} + //------------------- "weapon_m41.npc_reload" @@ -759,6 +812,36 @@ "wave" "weapons/zr68/zr68_reload.wav" } +// ZR68 reload in per-step pieces (sv_neo_reload_experimental), named after the step; _2 is a step's second sound. +// Each file starts just before its first transient + +"weapon_zr68.reload_2_load" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_2_load.wav" +} + +"weapon_zr68.reload_2_load_2" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_2_load_2.wav" +} + +"weapon_zr68.reload_3_cycle" +{ + "channel" "CHAN_ITEM" + "soundlevel" "SNDLVL_NORM" + "volume" "1.0" + "pitch" "PITCH_NORM" + "wave" ")weapons/zr68/zr68_reload_3_cycle.wav" +} + //------------------- "weapon_zr68.npc_reload" diff --git a/game/neo/scripts/weapon_m41.txt b/game/neo/scripts/weapon_m41.txt index b3d02a4cfb..dc14b7a9dd 100644 --- a/game/neo/scripts/weapon_m41.txt +++ b/game/neo/scripts/weapon_m41.txt @@ -1,3 +1,7 @@ +#base "weapon_mosok41_reload_phases.txt" +// NEO TODO (sunzenshen): M41 (one reload, no 30+1 rule): decide the fire point (clip end or ReloadReadyBeforeEnd) and whether the resume keeps +// the _empty name; resume after 2_load and the 3 sounds in 2_load are not done; see the plan's Open TODO list. + // M41 WeaponData @@ -16,6 +20,11 @@ WeaponData "Penetration" "32.0" "CycleTime" "0.166666" // time between shots "ReloadReadyBeforeEnd" "0.393" // 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 + // NEO TODO (sunzenshen): with the flag on, the magazine loads at the 2_load ammo time, not at the end minus + // ReloadReadyBeforeEnd above (user decision U1); decide whether the M41's fire point moves with it (U2). + "ReloadPhases" {} "TPMuzzleFlashScale" "0.5" diff --git a/game/neo/scripts/weapon_milso.txt b/game/neo/scripts/weapon_milso.txt index 5e220ac1f1..def71448e7 100644 --- a/game/neo/scripts/weapon_milso.txt +++ b/game/neo/scripts/weapon_milso.txt @@ -1,3 +1,7 @@ +#base "weapon_milso_reload_phases.txt" +// NEO TODO (sunzenshen): MilSO: resume after 2_load (dry slide release) needs a resume that keeps the clip; tactical resume must keep the +// chambered round; ReloadReadyBeforeEnd 0.467 s vs the new fire point; see the plan's Open TODO list. + // Milso WeaponData { @@ -14,7 +18,13 @@ WeaponData "Penetration" "12.0" "CycleTime" "0.212121" "ReloadReadyBeforeEnd" "0.467" // 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. + // The pistol ships two reloads (reload, reloadempty); the phases file has a table for each and a resume for each. + // NEO TODO (sunzenshen): the same U1/U2 question as the M41: with the flag on, the magazine loads at the 2_load ammo + // time, not at the end minus ReloadReadyBeforeEnd above. + "ReloadPhases" {} + "TPMuzzleFlashScale" "0.5" "clip_size" "12" 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/neo/weapons/neo_weapon_parse.cpp b/src/game/shared/neo/weapons/neo_weapon_parse.cpp index bafe4597b6..77872fa0eb 100644 --- a/src/game/shared/neo/weapons/neo_weapon_parse.cpp +++ b/src/game/shared/neo/weapons/neo_weapon_parse.cpp @@ -24,6 +24,7 @@ CNEOWeaponInfo::CNEOWeaponInfo() m_flPenetration = 0.f; m_bDropOnDeath = true; iAimFOV = 0; + m_iNumReloadTables = 0; m_flVMFov = m_flVMAimFov = 0.f; m_vecVMPosOffset = m_vecVMAimPosOffset = vec3_origin; @@ -31,6 +32,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_iNumReloadTables = 0; + 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_iNumReloadTables < RELOAD_PHASE_TABLES_MAX; pSeq = pSeq->GetNextTrueSubKey()) + { + ReloadPhaseTable_t &table = m_reloadTables[m_iNumReloadTables++]; + 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_iNumReloadTables; ++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 +114,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..dc10c7d7ca 100644 --- a/src/game/shared/neo/weapons/neo_weapon_parse.h +++ b/src/game/shared/neo/weapons/neo_weapon_parse.h @@ -12,6 +12,54 @@ #include "hl2mp_weapon_parse.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 +73,8 @@ class CNEOWeaponInfo : public CHL2MPSWeaponInfo int m_iBullets; float m_flCycleTime; float m_flReloadReadyBeforeEnd; + int m_iNumReloadTables; // 0: the weapon reloads without phases + ReloadPhaseTable_t m_reloadTables[RELOAD_PHASE_TABLES_MAX]; 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 +89,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..1a1dfe093b 100644 --- a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp +++ b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.cpp @@ -42,6 +42,11 @@ BEGIN_NETWORK_TABLE( CNEOBaseCombatWeapon, DT_NEOBaseCombatWeapon ) RecvPropInt(RECVINFO(m_nNumShotsFired)), RecvPropBool(RECVINFO(m_bTriggerReset)), RecvPropTime(RECVINFO(m_flReloadReadyTime)), + RecvPropBool(RECVINFO(m_bReloadExperimentalEnabled)), + 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 +55,11 @@ BEGIN_NETWORK_TABLE( CNEOBaseCombatWeapon, DT_NEOBaseCombatWeapon ) SendPropInt(SENDINFO(m_nNumShotsFired)), SendPropBool(SENDINFO(m_bTriggerReset)), SendPropTime(SENDINFO(m_flReloadReadyTime)), + SendPropBool(SENDINFO(m_bReloadExperimentalEnabled)), + 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 +73,11 @@ 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_bReloadExperimentalEnabled, FIELD_BOOLEAN, 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 +91,19 @@ BEGIN_DATADESC( CNEOBaseCombatWeapon ) DEFINE_FIELD(m_nNumShotsFired, FIELD_INTEGER), DEFINE_FIELD(m_bTriggerReset, FIELD_BOOLEAN), DEFINE_FIELD(m_flReloadReadyTime, FIELD_TIME), + DEFINE_FIELD(m_bReloadExperimentalEnabled, FIELD_BOOLEAN), + 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. Set before the map loads.", + true, 0.0f, true, 1.0f); const char *GetWeaponByLoadoutId(int id) { @@ -232,7 +255,12 @@ static const WeaponHandlingInfo_t handlingTable[] = { CNEOBaseCombatWeapon::CNEOBaseCombatWeapon( void ) { + m_bReloadExperimentalEnabled = sv_neo_reload_experimental.GetBool(); m_bTriggerReset = true; + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; + m_flReloadStart = 0.0f; + m_nReloadSoundsDone = 0; } const CNEOWeaponInfo &CNEOBaseCombatWeapon::GetNEOWpnData() const @@ -418,6 +446,8 @@ void CNEOBaseCombatWeapon::Equip(CBaseCombatCharacter* pOwner) } +// NEO TODO (sunzenshen): Partial reload (chamber live, skip the cycle) and the chamber model are not here; see sketch-zr68c-full. +// Bots: Clip1() >= GetMaxClip1() treats 30 of 31 as full; revisit the threshold later. bool CNEOBaseCombatWeapon::Reload( void ) { const bool bEmptyReload = UsesClipsForAmmo1() && m_iClip1 <= 0 && @@ -441,6 +471,14 @@ 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); + if (IsReloadPhaseSystemEnabled()) + { + // the phases are those of the sequence the reload plays, such as reload or reloadempty + CBaseViewModel *pVM = ReloadViewModel(); + m_nReloadPhase = RELOAD_PHASE_NONE; + StartReloadPhases(pVM ? GetNEOWpnData().FindReloadTable(pVM->GetSequenceName(pVM->GetSequence())) : -1); + } + return true; #if(0) @@ -471,6 +509,219 @@ 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 m_bReloadExperimentalEnabled && GetNEOWpnData().m_iNumReloadTables > 0; +} + +// 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); + } +} + +// NEO TODO (sunzenshen): RELOAD_END: stop the third-person reload gesture on a cut-short reload (U5b); not decided. +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 +// NEO TODO (sunzenshen): Resume after 2_load keeps the loaded magazine; only a resume after 1_unload zeroes the clip. +// A tactical reload also needs the chambered round kept (chamber model). +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; + + SetSequence(iSequence); + SendViewModelAnim(iSequence); + + // the draw is part of the motion: the full reload gesture fits the resume + ToHL2MPPlayer(GetOwner())->DoAnimationEvent(PLAYERANIMEVENT_RELOAD); + + const float flEnd = gpGlobals->curtime + ReloadViewModel()->SequenceDuration(iSequence); + 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 +// NEO TODO (sunzenshen): Gun dropped after the load but before the cycle: the next owner's draw plays the dry resume (U4a). +// Fallback for a lost resume (full magazine, empty chamber): let Reload() play the dry reload. +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 +735,12 @@ void CNEOBaseCombatWeapon::FinishReload(void) } m_flReloadReadyTime = 0.0f; + // the magazine is in: the phases up to the load are done, and a switch from here leaves nothing to resume + if (IsReloadPhaseSystemEnabled()) + { + m_nReloadPhase = Max(static_cast(m_nReloadPhase.Get()), RELOAD_PHASE_LOADED); + } + CBaseCombatCharacter* pOwner = GetOwner(); if (pOwner) @@ -533,7 +790,9 @@ 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; + // a reload interrupted by a switch goes on in the draw + const bool bResumed = IsReloadPhaseSystemEnabled() && m_nReloadPhase != RELOAD_PHASE_NONE && ResumeReload(); + 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 +859,17 @@ float CNEOBaseCombatWeapon::GetPenetration() const bool CNEOBaseCombatWeapon::Holster(CBaseCombatWeapon* pSwitchingTo) { + // Only a phase that a sequence resumes from outlasts the switch, with the table that names the resume + if (IsReloadPhaseSystemEnabled()) + { + StopReloadSoundPieces(); + if (!FindResumeSequence()) + { + m_nReloadPhase = RELOAD_PHASE_NONE; + m_nReloadTable = -1; + } + } + return BaseClass::Holster(pSwitchingTo); } @@ -655,6 +925,11 @@ void CNEOBaseCombatWeapon::ItemBusyFrame(void) BaseClass::ItemBusyFrame(); UpdateInaccuracy(); + if (IsReloadPhaseSystemEnabled()) + { + RunReloadPhases(); + } + // 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..77efdc22b2 100644 --- a/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h +++ b/src/game/shared/neo/weapons/weapon_neobasecombatweapon.h @@ -264,6 +264,7 @@ class CNEOBaseCombatWeapon : public CBaseHL2MPCombatWeapon virtual float GetFireRate() override final; float GetPenetration() const; + void StopReloadOnDrop(); #ifdef CLIENT_DLL float m_flTemperature; int m_spawnflags; @@ -282,6 +283,7 @@ class CNEOBaseCombatWeapon : public CBaseHL2MPCombatWeapon virtual float GetMaxAccuracyPenalty() const { return 1.0; } virtual float GetAccuracyPenaltyDecay(); virtual float GetFastestDryRefireTime() const { Assert(false); return 0; } // Should never call this base class; implement in children. + bool IsReloadPhaseSystemEnabled() const; protected: CNetworkVar(float, m_flSoonestAttack); @@ -294,10 +296,32 @@ class CNEOBaseCombatWeapon : public CBaseHL2MPCombatWeapon // When the reload in progress loads the magazine, 0 once it is loaded CNetworkVar(float, m_flReloadReadyTime); + // The server's pre-game reload setting, snapshotted for prediction + CNetworkVar(bool, m_bReloadExperimentalEnabled); + + // Reload phases (weapons whose script has a ReloadPhases block): 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: CNEOBaseCombatWeapon(const CNEOBaseCombatWeapon &other); bool HasEmptyIdle(); + 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