3 #include <server/client.qh>
4 #include <server/mutators/_mod.qh>
5 #include <server/damage.qh>
6 #include <common/constants.qh>
7 #include <common/net_linked.qh>
8 #include <server/player.qh>
9 #include <common/teams.qh>
10 #include <common/util.qh>
11 #include <common/weapons/_all.qh>
12 #include <server/world.qh>
14 int accuracy_byte(float n, float d)
17 if (n > d) return 255;
18 return 1 + rint(n * 100.0 / d);
21 bool accuracy_send(entity this, entity to, int sf)
23 WriteHeader(MSG_ENTITY, ENT_CLIENT_ACCURACY);
25 entity a = this.owner;
26 if (IS_SPEC(a)) a = a.enemy;
30 if (!autocvar_sv_accuracy_data_share && !CS(a.owner).cvar_cl_accuracy_data_share)
32 // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
33 WriteInt24_t(MSG_ENTITY, sf);
34 if (sf == 0) return true;
35 // note: we know that client and server agree about SendFlags...
37 for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
38 if (sf & f) WriteByte(MSG_ENTITY, accuracy_byte(a.accuracy_hit[w], a.accuracy_fired[w]));
39 f = (f == 0x800000) ? 1 : f * 2;
45 void accuracy_init(entity e)
47 entity a = CS(e).accuracy = new_pure(accuracy);
49 a.drawonlytoclient = e;
50 Net_LinkEntity(a, false, 0, accuracy_send);
53 void accuracy_free(entity e)
55 delete(CS(e).accuracy);
58 // force a resend of a player's accuracy stats
59 void accuracy_resend(entity e)
61 CS(e).accuracy.SendFlags = 0xFFFFFF;
64 // update accuracy stats
68 void accuracy_add(entity this, Weapon w, int fired, int hit)
70 if (IS_INDEPENDENT_PLAYER(this)) return;
71 entity a = CS(this).accuracy;
73 if (!hit && !fired) return;
74 if (w == WEP_Null) return;
77 int b = accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid]);
78 if (hit) a.accuracy_hit [wepid] += hit;
79 if (fired) a.accuracy_fired[wepid] += fired;
81 if (hit && STAT(HIT_TIME, a) != time) { // only run this once per frame
82 a.accuracy_cnt_hit[wepid] += 1;
83 STAT(HIT_TIME, a) = time;
86 if (fired && a.fired_time != time) { // only run this once per frame
87 a.accuracy_cnt_fired[wepid] += 1;
91 if (b == accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid])) return; // no change
92 int sf = 1 << (wepid % 24);
94 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == this, { CS(it).accuracy.SendFlags |= sf; });
97 bool accuracy_isgooddamage(entity attacker, entity targ)
99 int mutator_check = MUTATOR_CALLHOOK(AccuracyTargetValid, attacker, targ);
101 if (warmup_stage) return false;
102 if (game_stopped) return false;
104 // damage to dead/frozen players is good only if it happens in the frame they get killed / frozen
105 // so that stats for weapons that shoot multiple projectiles per shot are properly counted
106 if (IS_DEAD(targ) && time > targ.death_time) return false;
107 if (STAT(FROZEN, targ) && time > targ.freeze_time) return false;
108 if (SAME_TEAM(attacker, targ)) return false;
110 if (mutator_check == MUT_ACCADD_INVALID) return true;
112 if (mutator_check != MUT_ACCADD_VALID) return false;
113 if (!IS_CLIENT(targ) || !IS_CLIENT(attacker)) return false;
118 bool accuracy_canbegooddamage(entity attacker)
120 return !warmup_stage && IS_CLIENT(attacker);