3 #include <common/constants.qh>
4 #include <common/net_linked.qh>
5 #include <common/teams.qh>
6 #include <common/gamemodes/gamemode/duel/duel.qh>
7 #include <common/util.qh>
8 #include <common/weapons/_all.qh>
9 #include <server/client.qh>
10 #include <server/damage.qh>
11 #include <server/mutators/_mod.qh>
12 #include <server/player.qh>
13 #include <server/world.qh>
15 int accuracy_byte(float n, float d)
18 if (n > d) return 255;
19 return 1 + rint(n * 100.0 / d);
22 bool accuracy_send(entity this, entity to, int sf)
24 WriteHeader(MSG_ENTITY, ENT_CLIENT_ACCURACY);
26 entity a = this.owner;
27 if (IS_SPEC(a)) a = a.enemy;
30 // z411 send entity number
32 WriteByte(MSG_ENTITY, etof(a.owner));
34 WriteByte(MSG_ENTITY, 0);
37 if (!autocvar_sv_accuracy_data_share && !CS_CVAR(a.owner).cvar_cl_accuracy_data_share)
39 // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
40 WriteInt24_t(MSG_ENTITY, sf);
41 if (sf == 0) return true;
43 // note: we know that client and server agree about SendFlags...
45 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
48 WriteByte(MSG_ENTITY, a.accuracy_frags[w]);
49 WriteShort(MSG_ENTITY, a.accuracy_hit[w]);
50 WriteShort(MSG_ENTITY, a.accuracy_cnt_hit[w]);
51 WriteShort(MSG_ENTITY, a.accuracy_cnt_fired[w]);
53 WriteByte(MSG_ENTITY, accuracy_byte(a.accuracy_hit[w], a.accuracy_fired[w]));
56 f = (f == 0x800000) ? 1 : f * 2;
62 void accuracy_init(entity e)
64 entity a = CS(e).accuracy = new_pure(accuracy);
65 e.roundaccuracy = new_pure(accuracy);
68 a.drawonlytoclient = e;
69 Net_LinkEntity(a, false, 0, accuracy_send);
72 void accuracy_free(entity e)
74 delete(CS(e).accuracy);
75 delete(e.roundaccuracy);
78 void accuracy_reset(entity e)
80 entity a = CS(e).accuracy;
83 for (int i = 0; i < REGISTRY_MAX(Weapons); ++i)
85 a.accuracy_frags[i] = 0;
86 a.accuracy_hit[i] = 0;
87 a.accuracy_fired[i] = 0;
88 a.accuracy_cnt_hit[i] = 0;
89 a.accuracy_cnt_fired[i] = 0;
93 // force a resend of a player's accuracy stats
94 void accuracy_resend(entity e)
96 CS(e).accuracy.SendFlags = 0xFFFFFF;
99 // update accuracy stats
103 void roundaccuracy_clear(entity this)
105 if (IS_INDEPENDENT_PLAYER(this)) return;
106 entity ra = this.roundaccuracy;
108 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
109 ra.accuracy_frags[w] = 0;
110 ra.accuracy_hit[w] = 0;
111 ra.accuracy_fired[w] = 0;
112 ra.accuracy_cnt_hit[w] = 0;
113 ra.accuracy_cnt_fired[w] = 0;
117 void accuracy_add(entity this, Weapon w, float fired, float hit)
119 if (IS_INDEPENDENT_PLAYER(this)) return;
120 entity a = CS(this).accuracy;
121 entity ra = this.roundaccuracy;
123 if (!hit && !fired) return;
124 if (w == WEP_Null) return;
127 int b = accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid]);
129 a.accuracy_hit[wepid] += hit;
130 ra.accuracy_hit[wepid] += hit;
133 a.accuracy_fired[wepid] += fired;
134 ra.accuracy_fired[wepid] += fired;
137 if (hit && STAT(HIT_TIME, a) != time) { // only run this once per frame
138 a.accuracy_cnt_hit[wepid] += 1;
139 ra.accuracy_cnt_hit[wepid] += 1;
140 STAT(HIT_TIME, a) = time;
143 if (fired && a.fired_time != time) { // only run this once per frame
144 a.accuracy_cnt_fired[wepid] += 1;
145 ra.accuracy_cnt_fired[wepid] += 1;
149 if (!g_duel && b == accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid])) return; // no change
150 int sf = 1 << (wepid % 24);
154 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == this, { CS(it).accuracy.SendFlags |= sf; });
157 bool accuracy_isgooddamage(entity attacker, entity targ)
159 int mutator_check = MUTATOR_CALLHOOK(AccuracyTargetValid, attacker, targ);
161 if (warmup_stage || game_stopped) return false;
163 // damage to dead/frozen players is good only if it happens in the frame they get killed / frozen
164 // so that stats for weapons that shoot multiple projectiles per shot are properly counted
165 if (IS_DEAD(targ) && time > targ.death_time) return false;
166 if (STAT(FROZEN, targ) && time > targ.freeze_time) return false;
167 if (SAME_TEAM(attacker, targ)) return false;
169 if (mutator_check == MUT_ACCADD_INVALID) return true;
171 if (mutator_check != MUT_ACCADD_VALID) return false;
172 if (!IS_CLIENT(targ) || !IS_CLIENT(attacker)) return false;
177 bool accuracy_canbegooddamage(entity attacker)
179 return !warmup_stage && IS_CLIENT(attacker);
182 REPLICATE(cvar_cl_accuracy_data_share, bool, "cl_accuracy_data_share");
183 REPLICATE(cvar_cl_accuracy_data_receive, bool, "cl_accuracy_data_receive");