From 454416e71719767a370f0232395246405d957c1c Mon Sep 17 00:00:00 2001 From: terencehill Date: Wed, 16 Feb 2022 17:52:48 +0100 Subject: [PATCH] Move code adding score as int to a dedicated function that can be used by other game modes --- .../gamemode/clanarena/sv_clanarena.qc | 18 +----------------- qcsrc/common/gamemodes/sv_rules.qc | 19 +++++++++++++++++++ qcsrc/common/gamemodes/sv_rules.qh | 3 +++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc b/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc index 800c26f12..3b823dfdf 100644 --- a/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc +++ b/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc @@ -263,11 +263,6 @@ MUTATOR_HOOKFUNCTION(ca, PlayerDies) return true; } -MUTATOR_HOOKFUNCTION(ca, ClientConnect) -{ - entity player = M_ARGV(0, entity); - player.ca_damage_counter = 0; -} MUTATOR_HOOKFUNCTION(ca, ClientDisconnect) { @@ -409,18 +404,7 @@ MUTATOR_HOOKFUNCTION(ca, PlayerDamage_SplitHealthArmor) } if (scorer) - { - // assign damage score in units (rounded) to avoid bugs with float score - scorer.ca_damage_counter += scorer_damage; - float score_counter = scorer.ca_damage_counter / autocvar_g_ca_damage2score; - if (score_counter >= -0.5 && score_counter < 0.5) - return; - // NOTE: this code works for subtracting score too - int points = floor(score_counter + 0.5); - GameRules_scoring_add(scorer, SCORE, points); - - scorer.ca_damage_counter -= points * autocvar_g_ca_damage2score; - } + GameRules_scoring_add_float2int(scorer, SCORE, scorer_damage, ca_damage_counter, autocvar_g_ca_damage2score); } MUTATOR_HOOKFUNCTION(ca, CalculateRespawnTime) diff --git a/qcsrc/common/gamemodes/sv_rules.qc b/qcsrc/common/gamemodes/sv_rules.qc index 898ef3a4f..84d89909f 100644 --- a/qcsrc/common/gamemodes/sv_rules.qc +++ b/qcsrc/common/gamemodes/sv_rules.qc @@ -104,6 +104,25 @@ bool GameRules_scoring_is_vip(entity player) return player.m_GameRules_scoring_vip; } +// Uses client.float_field to accumulate and consume float score and adds score to the player as int (rounded) +// only when at least one unit of score has been accumulated. It works with negative score too +// Float scores can't be used as score because they aren't supported by the QC score networking system +// and online server browsers (e.g. qstat) +float _GameRules_scoring_add_float2int(entity client, entity sp, float value, .float float_field, float score_factor) +{ + client.(float_field) += value; + float score_counter = client.(float_field) / score_factor; + if (score_counter >= -0.5 && score_counter < 0.5) + return 0; + + // NOTE: this code works for subtracting score too + int points = floor(score_counter + 0.5); + client.(float_field) -= points * score_factor; + if (!points) + return 0; + return PlayerScore_Add(client, sp, points); +} + float _GameRules_scoring_add(entity client, entity sp, float value) { return PlayerScore_Add(client, sp, value); diff --git a/qcsrc/common/gamemodes/sv_rules.qh b/qcsrc/common/gamemodes/sv_rules.qh index 4fabea39c..2efcf1973 100644 --- a/qcsrc/common/gamemodes/sv_rules.qh +++ b/qcsrc/common/gamemodes/sv_rules.qh @@ -72,6 +72,9 @@ void _GameRules_scoring_end(); void GameRules_scoring_vip(entity player, bool value); bool GameRules_scoring_is_vip(entity player); +#define GameRules_scoring_add_float2int(client, fld, value, float_field, score_factor) \ + _GameRules_scoring_add_float2int(client, SP_##fld, value, float_field, score_factor) +float _GameRules_scoring_add_float2int(entity client, entity sp, float value, .float field, float score_factor); #define GameRules_scoring_add(client, fld, value) _GameRules_scoring_add(client, SP_##fld, value) float _GameRules_scoring_add(entity client, entity sp, float value); #define GameRules_scoring_add_team(client, fld, value) _GameRules_scoring_add_team(client, SP_##fld, ST_##fld, value) -- 2.39.2