Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
handicap.qc
Go to the documentation of this file.
1#include "handicap.qh"
2
7
8#include <common/state.qh>
9#include <common/ent_cs.qh> // for .handicap_level
10#include <lib/replicate.qh> // for REPLICATE_APPLYCHANGE
11#include <server/client.qh>
12
15
17{
18 // forced handicap defaults
19 CS(player).m_handicap_give = 1;
20 CS(player).m_handicap_take = 1;
21
23}
24
25float Handicap_GetVoluntaryHandicap(entity player, bool receiving)
26{
28 return 1;
29#if 0
30 if (receiving)
31 return bound(1.0, CS_CVAR(player).cvar_cl_handicap_damage_taken, 10.0);
32 else
33 return bound(1.0, CS_CVAR(player).cvar_cl_handicap_damage_given, 10.0);
34#else
35 // TODO: remove the else vector branch after 0.9 release
36 // Forwards compatibility for old clients on new servers. `cl_handicap 2`
37 // ( '2 0 0' ) is treated the same as `cl_handicap_damage_{given,taken} 2`.
38 // The x is give and y is take.
39 // '2 0 0' gives and takes x2
40 // '2 2 0' gives and takes x2
41 // '2 1 0' only gives x2
42 // '1 2 0' only takes x2
43 // z is wasted
44
45 int handicap_value;
46
47 // First read if the new cvars have a valid value,
48 // if they don't then read old cvar, checking if the old cvar has
49 // separate give and take values or we should use the first value for both
50 if (receiving)
51 {
52 if (CS_CVAR(player).cvar_cl_handicap_damage_taken > 1)
53 handicap_value = CS_CVAR(player).cvar_cl_handicap_damage_taken;
54 else if (CS_CVAR(player).cvar_cl_handicap.y > 0)
55 handicap_value = CS_CVAR(player).cvar_cl_handicap.y;
56 else
57 handicap_value = CS_CVAR(player).cvar_cl_handicap.x;
58 }
59 else
60 {
61 if (CS_CVAR(player).cvar_cl_handicap_damage_given > 1)
62 handicap_value = CS_CVAR(player).cvar_cl_handicap_damage_given;
63 else
64 handicap_value = CS_CVAR(player).cvar_cl_handicap.x;
65 }
66
67 return bound(1.0, handicap_value, 10.0);
68#endif
69}
70
71float Handicap_GetForcedHandicap(entity player, bool receiving)
72{
74 return 1;
75 if (receiving)
76 return (CS(player)) ? CS(player).m_handicap_take : 1;
77 else
78 return (CS(player)) ? CS(player).m_handicap_give : 1;
79}
80
81void Handicap_SetForcedHandicap(entity player, float value, bool receiving)
82{
84 return;
85 if (value <= 0)
86 error("Handicap_SetForcedHandicap: Invalid handicap value.");
87
88 if (receiving)
89 CS(player).m_handicap_take = value;
90 else
91 CS(player).m_handicap_give = value;
92
94}
95
96float Handicap_GetTotalHandicap(entity player, bool receiving)
97{
99 return 1;
100 return Handicap_GetForcedHandicap(player, receiving) *
101 Handicap_GetVoluntaryHandicap(player, receiving);
102}
103
105{
106 if (HANDICAP_DISABLED())
107 {
108 player.handicap_level = 0;
109 return;
110 }
111
112 float handicap_total = (Handicap_GetTotalHandicap(player, true) +
113 Handicap_GetTotalHandicap(player, false)) / 2;
114 player.handicap_level = floor(map_bound_ranges(handicap_total, 1, HANDICAP_MAX_LEVEL_EQUIVALENT, 0, 16));
115}
116
117REPLICATE_APPLYCHANGE("cl_handicap_damage_given", { Handicap_UpdateHandicapLevel(this); });
118REPLICATE_APPLYCHANGE("cl_handicap_damage_taken", { Handicap_UpdateHandicapLevel(this); });
119
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
void Handicap_SetForcedHandicap(entity player, float value, bool receiving)
Sets the forced handicap of the player.
Definition handicap.qc:81
void Handicap_Initialize(entity player)
Initializes handicap to its default value.
Definition handicap.qc:16
float Handicap_GetTotalHandicap(entity player, bool receiving)
Returns the total handicap of the player.
Definition handicap.qc:96
float Handicap_GetForcedHandicap(entity player, bool receiving)
Returns the forced handicap of the player.
Definition handicap.qc:71
float m_handicap_give
Holds the forced handicap value.
Definition handicap.qc:13
void Handicap_UpdateHandicapLevel(entity player)
Updates .handicap_level for the player.
Definition handicap.qc:104
float m_handicap_take
Holds the forced handicap value.
Definition handicap.qc:14
float Handicap_GetVoluntaryHandicap(entity player, bool receiving)
Returns the voluntary handicap of the player.
Definition handicap.qc:25
Header file that describes the handicap system.
#define HANDICAP_DISABLED()
Definition handicap.qh:57
#define HANDICAP_MAX_LEVEL_EQUIVALENT
Definition handicap.qh:66
#define REPLICATE_APPLYCHANGE(var, ApplyChange_code)
Allows setting code that will be executed on cvar value changes.
Definition replicate.qh:36
ERASEABLE float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
Same as map_ranges except that values outside the source range are clamped to min or max.
Definition math.qh:372
float bound(float min, float value, float max)
float floor(float f)
#define error
Definition pre.qh:6
#define CS_CVAR(this)
Definition state.qh:51
ClientState CS(Client this)
Definition state.qh:47