]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/accuracy.qc
Merge branch 'master' into LegendaryGuard/cyber
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / accuracy.qc
1 #include "accuracy.qh"
2
3 #include <common/constants.qh>
4 #include <common/net_linked.qh>
5 #include <common/teams.qh>
6 #include <common/util.qh>
7 #include <common/weapons/_all.qh>
8 #include <server/client.qh>
9 #include <server/damage.qh>
10 #include <server/mutators/_mod.qh>
11 #include <server/player.qh>
12 #include <server/world.qh>
13
14 int accuracy_byte(float n, float d)
15 {
16         if (n <= 0) return 0;
17         if (n > d) return 255;
18         return 1 + rint(n * 100.0 / d);
19 }
20
21 bool accuracy_send(entity this, entity to, int sf)
22 {
23         WriteHeader(MSG_ENTITY, ENT_CLIENT_ACCURACY);
24
25         entity a = this.owner;
26         if (IS_SPEC(a)) a = a.enemy;
27         a = CS(a).accuracy;
28         
29         // z411 send entity number
30         if(g_duel)
31                 WriteByte(MSG_ENTITY, etof(a.owner));
32         else
33                 WriteByte(MSG_ENTITY, 0);
34
35         if (to != a.owner)
36                 if (!autocvar_sv_accuracy_data_share && !CS_CVAR(a.owner).cvar_cl_accuracy_data_share)
37                         sf = 0;
38         // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
39         WriteInt24_t(MSG_ENTITY, sf);
40         if (sf == 0) return true;
41         
42         // note: we know that client and server agree about SendFlags...
43         int f = 1;
44         for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
45                 if (sf & f) {
46                         if(g_duel) {
47                                 WriteByte(MSG_ENTITY, a.accuracy_frags[w]);
48                                 WriteShort(MSG_ENTITY, a.accuracy_hit[w]);
49                                 WriteShort(MSG_ENTITY, a.accuracy_cnt_hit[w]);
50                                 WriteShort(MSG_ENTITY, a.accuracy_cnt_fired[w]);
51                         } else {
52                                 WriteByte(MSG_ENTITY, accuracy_byte(a.accuracy_hit[w], a.accuracy_fired[w]));
53                         }
54                 }
55                 f = (f == 0x800000) ? 1 : f * 2;
56         }
57         return true;
58 }
59
60 // init/free
61 void accuracy_init(entity e)
62 {
63         entity a = CS(e).accuracy = new_pure(accuracy);
64         e.roundaccuracy = new_pure(accuracy);
65         a.owner = e;
66         if(!g_duel) // z411
67                 a.drawonlytoclient = e;
68         Net_LinkEntity(a, false, 0, accuracy_send);
69 }
70
71 void accuracy_free(entity e)
72 {
73         delete(CS(e).accuracy);
74         delete(e.roundaccuracy);
75 }
76
77 void accuracy_reset(entity e)
78 {
79         entity a = CS(e).accuracy;
80         if (!a) return;
81
82         for (int i = 0; i < REGISTRY_MAX(Weapons); i++)
83         {
84                 a.accuracy_frags[i] = 0;
85                 a.accuracy_hit[i] = 0;
86                 a.accuracy_fired[i] = 0;
87                 a.accuracy_cnt_hit[i] = 0;
88                 a.accuracy_cnt_fired[i] = 0;
89         }
90 }
91
92 // force a resend of a player's accuracy stats
93 void accuracy_resend(entity e)
94 {
95         CS(e).accuracy.SendFlags = 0xFFFFFF;
96 }
97
98 // update accuracy stats
99 //.float hit_time;
100 .float fired_time;
101
102 void roundaccuracy_clear(entity this)
103 {
104         if (IS_INDEPENDENT_PLAYER(this)) return;
105         entity ra = this.roundaccuracy;
106         
107         for (int w = 0; w <= WEP_LAST - WEP_FIRST; ++w) {
108                 ra.accuracy_frags[w] = 0;
109                 ra.accuracy_hit[w] = 0;
110                 ra.accuracy_fired[w] = 0;
111                 ra.accuracy_cnt_hit[w] = 0;
112                 ra.accuracy_cnt_fired[w] = 0;
113         }
114 }
115
116 void accuracy_add(entity this, Weapon w, float fired, float hit)
117 {
118         if (IS_INDEPENDENT_PLAYER(this)) return;
119         entity a = CS(this).accuracy;
120         entity ra = this.roundaccuracy;
121         if (!a) return;
122         if (!hit && !fired) return;
123         if (w == WEP_Null) return;
124         int wepid = w.m_id;
125         wepid -= WEP_FIRST;
126         int b = accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid]);
127         if (hit) {
128                 a.accuracy_hit[wepid] += hit;
129                 ra.accuracy_hit[wepid] += hit;
130         }
131         if (fired) {
132                 a.accuracy_fired[wepid] += fired;
133                 ra.accuracy_fired[wepid] += fired;
134         }
135
136     if (hit && STAT(HIT_TIME, a) != time) { // only run this once per frame
137         a.accuracy_cnt_hit[wepid] += 1;
138                 ra.accuracy_cnt_hit[wepid] += 1;
139         STAT(HIT_TIME, a) = time;
140     }
141
142     if (fired && a.fired_time != time) { // only run this once per frame
143         a.accuracy_cnt_fired[wepid] += 1;
144                 ra.accuracy_cnt_fired[wepid] += 1;
145         a.fired_time = time;
146     }
147
148         if (!g_duel && b == accuracy_byte(a.accuracy_hit[wepid], a.accuracy_fired[wepid])) return; // no change
149         int sf = 1 << (wepid % 24);
150         a.SendFlags |= sf;
151         
152         if(!g_duel)
153                 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == this, { CS(it).accuracy.SendFlags |= sf; });
154 }
155
156 bool accuracy_isgooddamage(entity attacker, entity targ)
157 {
158         int mutator_check = MUTATOR_CALLHOOK(AccuracyTargetValid, attacker, targ);
159
160         if (warmup_stage || game_stopped) return false;
161
162         // damage to dead/frozen players is good only if it happens in the frame they get killed / frozen
163         // so that stats for weapons that shoot multiple projectiles per shot are properly counted
164         if (IS_DEAD(targ) && time > targ.death_time) return false;
165         if (STAT(FROZEN, targ) && time > targ.freeze_time) return false;
166         if (SAME_TEAM(attacker, targ)) return false;
167
168         if (mutator_check == MUT_ACCADD_INVALID) return true;
169
170         if (mutator_check != MUT_ACCADD_VALID) return false;
171         if (!IS_CLIENT(targ) || !IS_CLIENT(attacker)) return false;
172
173         return true;
174 }
175
176 bool accuracy_canbegooddamage(entity attacker)
177 {
178         return !warmup_stage && IS_CLIENT(attacker);
179 }