Xonotic
sv_dynamic_handicap.qc
Go to the documentation of this file.
1 #include "sv_dynamic_handicap.qh"
7 
8 //======================= Global variables ====================================
9 
20 
21 //====================== Forward declarations =================================
22 
26 float DynamicHandicap_ClampHandicap(float handicap);
27 
28 //========================= Free functions ====================================
29 
33 {
34  float total_score = 0;
35  float totalplayers = 0;
37  {
38  total_score += PlayerScore_Get(it, SP_SCORE);
39  ++totalplayers;
40  });
41  float mean_score = total_score / totalplayers;
42  FOREACH_CLIENT(true,
43  {
44  float score = PlayerScore_Get(it, SP_SCORE);
45  float handicap = fabs((score - mean_score) *
47  handicap = handicap ** autocvar_g_dynamic_handicap_exponent;
48  if (score < mean_score)
49  {
50  handicap = -handicap;
51  }
52  if (handicap >= 0)
53  {
54  handicap += 1;
55  }
56  else
57  {
58  handicap = 1 / (fabs(handicap) + 1);
59  }
60  handicap = DynamicHandicap_ClampHandicap(handicap);
61  Handicap_SetForcedHandicap(it, handicap);
62  });
63 }
64 
65 float DynamicHandicap_ClampHandicap(float handicap)
66 {
67  if ((autocvar_g_dynamic_handicap_min >= 0) && (handicap <
69  {
71  }
72  if ((autocvar_g_dynamic_handicap_max > 0) && (handicap >
74  {
76  }
77  return handicap;
78 }
79 
80 //============================= Hooks ========================================
81 
83 
84 MUTATOR_HOOKFUNCTION(dynamic_handicap, BuildMutatorsString)
85 {
86  M_ARGV(0, string) = strcat(M_ARGV(0, string), ":handicap");
87 }
88 
89 MUTATOR_HOOKFUNCTION(dynamic_handicap, BuildMutatorsPrettyString)
90 {
91  M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Dynamic handicap");
92 }
93 
95 {
97 }
98 
100 {
102 }
103 
104 MUTATOR_HOOKFUNCTION(dynamic_handicap, MakePlayerObserver)
105 {
107 }
108 
109 MUTATOR_HOOKFUNCTION(dynamic_handicap, AddedPlayerScore)
110 {
111  if (M_ARGV(0, entity) != SP_SCORE)
112  {
113  return;
114  }
116 }
#define PlayerScore_Get(player, scorefield)
Returns the player&#39;s score.
Definition: scores.qh:43
void DynamicHandicap_UpdateHandicap()
Updates the handicap of all players.
entity() spawn
#define FOREACH_CLIENT(cond, body)
Definition: utils.qh:49
int autocvar_g_dynamic_handicap
Whether to enable dynamic handicap.
ClientDisconnect(this)
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
REGISTER_MUTATOR(dynamic_handicap, autocvar_g_dynamic_handicap)
#define M_ARGV(x, type)
Definition: events.qh:17
float autocvar_g_dynamic_handicap_max
The maximum value of the handicap.
float autocvar_g_dynamic_handicap_exponent
The exponent used to calculate handicap.
float DynamicHandicap_ClampHandicap(float handicap)
Clamps the value of the handicap.
PutClientInServer(this)
float autocvar_g_dynamic_handicap_min
The minimum value of the handicap.
float autocvar_g_dynamic_handicap_scale
The scale of the handicap.
#define IS_PLAYER(v)
Definition: utils.qh:9
MUTATOR_HOOKFUNCTION(dynamic_handicap, BuildMutatorsString)
void Handicap_SetForcedHandicap(entity player, float value)
Sets the forced handicap of the player.
Definition: handicap.qc:28