Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
accuracy.qc
Go to the documentation of this file.
1#include "accuracy.qh"
2
3#include <common/constants.qh>
5#include <common/teams.qh>
6#include <common/util.qh>
8#include <server/client.qh>
9#include <server/damage.qh>
11#include <server/player.qh>
12#include <server/world.qh>
13
14// 0: haven't fired the weapon yet
15// 1-101: weapon accuracy + 1%
16// 255: >100% accuracy (see accuracy_add)
17int accuracy_byte(float n, float d)
18{
19 if (n < 0 || d <= 0)
20 return 0;
21 if (n > d)
22 return 255;
23 return 1 + rint(n * 100.0 / d);
24}
25
26bool accuracy_send(entity this, entity to, int sf)
27{
28 WriteHeader(MSG_ENTITY, ENT_CLIENT_ACCURACY);
29
30 entity a = this.owner;
31 if (IS_SPEC(a)) a = a.enemy;
32 a = CS(a).accuracy;
33
34 if (to != a.owner)
35 if (!autocvar_sv_accuracy_data_share && !CS_CVAR(a.owner).cvar_cl_accuracy_data_share)
36 sf = 0;
37 // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
38 WriteInt24_t(MSG_ENTITY, sf);
39 if (sf == 0) return true;
40 // note: we know that client and server agree about SendFlags...
41 int f = 1;
42 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
43 if (sf & f) WriteByte(MSG_ENTITY, accuracy_byte(a.accuracy_hit[w], a.accuracy_fired[w]));
44 f = (f == 0x800000) ? 1 : f * 2;
45 }
46 return true;
47}
48
51{
52 entity a = CS(e).accuracy = new_pure(accuracy);
53 a.owner = e;
54 a.drawonlytoclient = e;
55 Net_LinkEntity(a, false, 0, accuracy_send);
56}
57
59{
60 delete(CS(e).accuracy);
61}
62
64{
65 entity a = CS(e).accuracy;
66 if (!a) return;
67
68 for (int i = 0; i < REGISTRY_MAX(Weapons); ++i)
69 {
70 a.accuracy_real[i] = 0;
71 a.accuracy_frags[i] = 0;
72 a.accuracy_hit[i] = 0;
73 a.accuracy_fired[i] = 0;
74 a.accuracy_cnt_hit[i] = 0;
75 a.accuracy_cnt_fired[i] = 0;
76 }
77}
78
81{
82 CS(e).accuracy.SendFlags = 0xFFFFFF;
83}
84
85//.float hit_time;
87/* per-weapon accuracy stats:
88 * fired: total damage that all fired shots could have dealt
89 * hit: total damage dealt, including damage after death
90 * real: total damage dealt, excluding damage after death
91 * cnt-hit: amount of shots that actually hit
92 * cnt-fired: amount of shots fired
93 * hit is important to have, to calculate accuracy (hit / fired * 100%), since real / fired would give low accuracy for shooting a 1 hp player with Vortex.
94 * accuracy_fired is usually added to only when a weapon is fired.
95 * accuracy_real is only added to when a player finally receives damage in PlayerDamage.
96 * accuracy_hit is added to at other points before excess damage is removed.
97 * accuracy_hit can be higher than accuracy_fired (accuracy_fired doesn't account for e.g. Vortex shots hitting 2 players), this is networked as 100% accuracy.
98 * adding to accuracy_real doesn't need to add to accuracy_cnt_hit, since accuracy_hit would've already updated it.
99 * we count only damage dealt to players, not any other entities.
100 */
102void accuracy_add(entity this, Weapon w, float fired, float hit, float real)
103{
104 if (IS_INDEPENDENT_PLAYER(this)) return;
105 entity a = CS(this).accuracy;
106 if (!a) return;
107 if (!hit && !fired && !real) return;
108 if (w == WEP_Null) return;
109 int wepid = w.m_id - WEP_FIRST;
110 int b = accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid]);
111 if (real) a.accuracy_real [wepid] += real;
112 if (hit) a.accuracy_hit [wepid] += hit;
113 if (fired) a.accuracy_fired[wepid] += fired;
114
115 if (hit && STAT(HIT_TIME, a) != time) { // only run this once per frame
116 a.accuracy_cnt_hit[wepid] += 1;
117 STAT(HIT_TIME, a) = time;
118 }
119
120 if (fired && a.fired_time != time) { // only run this once per frame
121 a.accuracy_cnt_fired[wepid] += 1;
122 a.fired_time = time;
123 }
124
125 if (b == accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid])) return; // no change
126 int sf = BIT(wepid % 24);
127 a.SendFlags |= sf;
128 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == this, { CS(it).accuracy.SendFlags |= sf; });
129}
130
134{
135 int mutator_check = MUTATOR_CALLHOOK(AccuracyTargetValid, attacker, targ);
136
137 if (warmup_stage || game_stopped) return false;
138
139 // damage to dead players is good only if it happens in the frame they get killed
140 // so that stats for weapons that shoot multiple projectiles per shot are properly counted
141 if (IS_DEAD(targ) && time > targ.death_time) return false;
142 if (SAME_TEAM(attacker, targ)) return false;
143
144 if (mutator_check == MUT_ACCADD_INVALID) return true;
145
146 if (mutator_check != MUT_ACCADD_VALID) return false;
147 if (!IS_CLIENT(targ) || !IS_CLIENT(attacker)) return false;
148
149 return true;
150}
151
155{
156 return !warmup_stage && IS_CLIENT(attacker);
157}
158
159REPLICATE(cvar_cl_accuracy_data_share, bool, "cl_accuracy_data_share");
160REPLICATE(cvar_cl_accuracy_data_receive, bool, "cl_accuracy_data_receive");
bool accuracy_send(entity this, entity to, int sf)
Definition accuracy.qc:26
float fired_time
Definition accuracy.qc:86
void accuracy_add(entity this, Weapon w, float fired, float hit, float real)
update accuracy stats
Definition accuracy.qc:102
void accuracy_free(entity e)
Definition accuracy.qc:58
void accuracy_init(entity e)
init/free
Definition accuracy.qc:50
bool accuracy_isgooddamage(entity attacker, entity targ)
does this damage count towards accuracy stats?
Definition accuracy.qc:133
bool accuracy_canbegooddamage(entity attacker)
if damage were to occur, would accuracy_isgooddamage be able to return true?
Definition accuracy.qc:154
void accuracy_reset(entity e)
Definition accuracy.qc:63
void accuracy_resend(entity e)
force a resend of a player's accuracy stats
Definition accuracy.qc:80
int accuracy_byte(float n, float d)
Definition accuracy.qc:17
float autocvar_sv_accuracy_data_share
Weapon Accuracy stats.
Definition accuracy.qh:21
entity accuracy
Definition accuracy.qh:26
#define MUTATOR_CALLHOOK(id,...)
Definition base.qh:143
#define BIT(n)
Only ever assign into the first 24 bits in QC (so max is BIT(23)).
Definition bits.qh:8
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:44
int m_id
Definition weapon.qh:45
entity owner
Definition main.qh:87
bool warmup_stage
Definition main.qh:120
#define IS_CLIENT(s)
Definition player.qh:242
#define IS_DEAD(s)
Definition player.qh:245
float game_stopped
Definition stats.qh:81
float time
Weapons
Definition guide.qh:113
const int MSG_ENTITY
Definition net.qh:115
#define WriteHeader(to, id)
Definition net.qh:221
void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
Definition net.qh:123
#define REPLICATE(...)
Replicates a client cvar into a server field.
Definition replicate.qh:23
#define STAT(...)
Definition stats.qh:82
float rint(float f)
void WriteByte(float data, float dest, float desto)
#define new_pure(class)
purely logical entities (not linked to the area grid)
Definition oo.qh:67
#define REGISTRY_MAX(id)
Definition registry.qh:17
#define IS_INDEPENDENT_PLAYER(e)
Definition client.qh:312
@ MUT_ACCADD_INVALID
Definition events.qh:861
@ MUT_ACCADD_VALID
Definition events.qh:860
#define CS_CVAR(this)
Definition state.qh:51
ClientState CS(Client this)
Definition state.qh:47
#define SAME_TEAM(a, b)
Definition teams.qh:241
#define IS_SPEC(v)
Definition utils.qh:10
#define FOREACH_CLIENT(cond, body)
Definition utils.qh:50
#define WEP_LAST
Definition all.qh:327
const int WEP_FIRST
Definition all.qh:326