]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/accuracy.qc
Some more defs.qh cleanup, update gameplay hash (again)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / accuracy.qc
1 #include "accuracy.qh"
2
3 #include <server/client.qh>
4 #include <server/mutators/_mod.qh>
5 #include <server/g_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
13 int accuracy_byte(float n, float d)
14 {
15         if (n <= 0) return 0;
16         if (n > d) return 255;
17         return 1 + rint(n * 100.0 / d);
18 }
19
20 bool accuracy_send(entity this, entity to, int sf)
21 {
22         WriteHeader(MSG_ENTITY, ENT_CLIENT_ACCURACY);
23
24         entity a = this.owner;
25         if (IS_SPEC(a)) a = a.enemy;
26         a = CS(a).accuracy;
27
28         if (to != a.owner)
29                 if (!autocvar_sv_accuracy_data_share && !CS(a.owner).cvar_cl_accuracy_data_share)
30                         sf = 0;
31         // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
32         WriteInt24_t(MSG_ENTITY, sf);
33         if (sf == 0) return true;
34         // note: we know that client and server agree about SendFlags...
35         int f = 1;
36         for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
37                 if (sf & f) WriteByte(MSG_ENTITY, accuracy_byte(a.accuracy_hit[w], a.accuracy_fired[w]));
38                 f = (f == 0x800000) ? 1 : f * 2;
39         }
40         return true;
41 }
42
43 // init/free
44 void accuracy_init(entity e)
45 {
46         entity a = CS(e).accuracy = new_pure(accuracy);
47         a.owner = e;
48         a.drawonlytoclient = e;
49         Net_LinkEntity(a, false, 0, accuracy_send);
50 }
51
52 void accuracy_free(entity e)
53 {
54         delete(CS(e).accuracy);
55 }
56
57 // force a resend of a player's accuracy stats
58 void accuracy_resend(entity e)
59 {
60         CS(e).accuracy.SendFlags = 0xFFFFFF;
61 }
62
63 // update accuracy stats
64 //.float hit_time;
65 .float fired_time;
66
67 void accuracy_add(entity this, Weapon w, int fired, int hit)
68 {
69         if (IS_INDEPENDENT_PLAYER(this)) return;
70         entity a = CS(this).accuracy;
71         if (!a) return;
72         if (!hit && !fired) return;
73         if (w == WEP_Null) return;
74         int wepid = w.m_id;
75         wepid -= WEP_FIRST;
76         int b = accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid]);
77         if (hit)    a.accuracy_hit  [wepid] += hit;
78         if (fired)  a.accuracy_fired[wepid] += fired;
79
80     if (hit && STAT(HIT_TIME, a) != time) { // only run this once per frame
81         a.accuracy_cnt_hit[wepid] += 1;
82         STAT(HIT_TIME, a) = time;
83     }
84
85     if (fired && a.fired_time != time) { // only run this once per frame
86         a.accuracy_cnt_fired[wepid] += 1;
87         a.fired_time = time;
88     }
89
90         if (b == accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid])) return; // no change
91         int sf = 1 << (wepid % 24);
92         a.SendFlags |= sf;
93         FOREACH_CLIENT(IS_SPEC(it) && it.enemy == this, { CS(it).accuracy.SendFlags |= sf; });
94 }
95
96 bool accuracy_isgooddamage(entity attacker, entity targ)
97 {
98         int mutator_check = MUTATOR_CALLHOOK(AccuracyTargetValid, attacker, targ);
99
100         if (warmup_stage) return false;
101         if (game_stopped) return false;
102
103         // damage to dead/frozen players is good only if it happens in the frame they get killed / frozen
104         // so that stats for weapons that shoot multiple projectiles per shot are properly counted
105         if (IS_DEAD(targ) && time > targ.death_time) return false;
106         if (STAT(FROZEN, targ) && time > targ.freeze_time) return false;
107         if (SAME_TEAM(attacker, targ)) return false;
108
109         if (mutator_check == MUT_ACCADD_INVALID) return true;
110
111         if (mutator_check != MUT_ACCADD_VALID) return false;
112         if (!IS_CLIENT(targ) || !IS_CLIENT(attacker)) return false;
113
114         return true;
115 }
116
117 bool accuracy_canbegooddamage(entity attacker)
118 {
119         return !warmup_stage && IS_CLIENT(attacker);
120 }