From: terencehill Date: Wed, 16 Feb 2022 16:52:48 +0000 (+0100) Subject: Move code adding score as int to a dedicated function that can be used by other game... X-Git-Tag: xonotic-v0.8.5~198 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=454416e71719767a370f0232395246405d957c1c;p=xonotic%2Fxonotic-data.pk3dir.git Move code adding score as int to a dedicated function that can be used by other game modes --- 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)