From 0cb3108f7e66a897a1bd81f32f499cdbfa3d378d Mon Sep 17 00:00:00 2001 From: terencehill Date: Wed, 2 Apr 2025 20:01:29 +0200 Subject: [PATCH] Keyhunt: fix #2344 "KeyHunt: Unfair scoring when key is destroyed when playing with 4 teams" Fixed by saving total player score internally as a float, distributing the score to players truly evenly, by using the new version of DistributeEvenly_Get that doesn't round the score and updating the actual score to the rounded value of the exact score. The version of DistributeEvenly_Get that rounds is now called DistributeEvenly_GetRounded (currently unused) Also clean up DistributeEvenly_Init and DistributeEvenly_GetRandomized code (no logic changes) --- .../gamemodes/gamemode/keyhunt/sv_keyhunt.qc | 5 +-- qcsrc/lib/random.qc | 31 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/qcsrc/common/gamemodes/gamemode/keyhunt/sv_keyhunt.qc b/qcsrc/common/gamemodes/gamemode/keyhunt/sv_keyhunt.qc index b764c588f..922418b40 100644 --- a/qcsrc/common/gamemodes/gamemode/keyhunt/sv_keyhunt.qc +++ b/qcsrc/common/gamemodes/gamemode/keyhunt/sv_keyhunt.qc @@ -175,6 +175,7 @@ void kh_Controller_Think(entity this) // called a lot // frags f: take from cvar * f // frags 0: no frags +.float float_score; void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner) // update the score when a key is captured { string s; @@ -182,10 +183,10 @@ void kh_Scores_Event(entity player, entity key, string what, float frags_player, return; if(frags_player) - GameRules_scoring_add_team(player, SCORE, frags_player); + GameRules_scoring_add_team_float2int(player, SCORE, frags_player, float_score, 1); if(key && key.owner && frags_owner) - GameRules_scoring_add_team(key.owner, SCORE, frags_owner); + GameRules_scoring_add_team_float2int(key.owner, SCORE, frags_owner, float_score, 1); if(!autocvar_sv_eventlog) //output extra info to the console or text file return; diff --git a/qcsrc/lib/random.qc b/qcsrc/lib/random.qc index de78f4027..02dbb5636 100644 --- a/qcsrc/lib/random.qc +++ b/qcsrc/lib/random.qc @@ -42,20 +42,29 @@ ERASEABLE void DistributeEvenly_Init(float amount, float totalweight) { if (DistributeEvenly_amount) - { - LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), " for ", ftos(DistributeEvenly_totalweight), " left!)"); - } - if (totalweight == 0) DistributeEvenly_amount = 0; - else DistributeEvenly_amount = amount; + LOG_TRACE("DistributeEvenly_Init: UNFINISHED DISTRIBUTION (", ftos(DistributeEvenly_amount), + " for ", ftos(DistributeEvenly_totalweight), " left!)"); + DistributeEvenly_amount = (totalweight) ? amount : 0; DistributeEvenly_totalweight = totalweight; } ERASEABLE float DistributeEvenly_Get(float weight) { - float f; - if (weight <= 0) return 0; - f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); + if (weight <= 0) + return 0; + float f = DistributeEvenly_amount * weight / DistributeEvenly_totalweight; + DistributeEvenly_totalweight -= weight; + DistributeEvenly_amount -= f; + return f; +} + +ERASEABLE +float DistributeEvenly_GetRounded(float weight) +{ + if (weight <= 0) + return 0; + float f = floor(0.5 + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); DistributeEvenly_totalweight -= weight; DistributeEvenly_amount -= f; return f; @@ -64,9 +73,9 @@ float DistributeEvenly_Get(float weight) ERASEABLE float DistributeEvenly_GetRandomized(float weight) { - float f; - if (weight <= 0) return 0; - f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); + if (weight <= 0) + return 0; + float f = floor(random() + DistributeEvenly_amount * weight / DistributeEvenly_totalweight); DistributeEvenly_totalweight -= weight; DistributeEvenly_amount -= f; return f; -- 2.39.5