]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/accuracy.qc
beginning of a SANE accuracy networking system
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / accuracy.qc
1 .entity accuracy;
2 .float accuracy_hit[WEP_MAXCOUNT];
3 .float accuracy_fired[WEP_MAXCOUNT];
4
5 void accuracy_send(entity to, float sf)
6 {
7         float w, f;
8         WriteByte(MSG_ENTITY, ENT_CLIENT_ACCURACY);
9         if(to != self.owner)
10                 if not(self.owner.cvar_cl_accuracy_data_share && autocvar_sv_accuracy_data_share)
11                         sf = 0;
12         // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
13         WriteInt24_t(MSG_ENTITY, sf);
14         if(sf == 0)
15                 return TRUE;
16         // note: we know that client and server agree about SendFlags...
17         for(w = 0, f = 1; w <= WEP_LAST - WEP_FIRST; ++w, f *= 2)
18         {
19                 if(sf & f)
20                 {
21                         if(self.(accuracy_fired[w]))
22                                 WriteByte(MSG_ENTITY, 0);
23                         else
24                                 WriteByte(MSG_ENTITY, 1 + bound(0, (254.0 * self.(accuracy_hit[w])) / self.(accuracy_fired[w]), 254));
25                 }
26         }
27         return TRUE;
28 }
29
30 // init/free
31 void accuracy_init(entity e)
32 {
33         e.accuracy = spawn();
34         e.accuracy.owner = e;
35         e.accuracy.classname = "accuracy";
36         e.accuracy.SendEntity = accuracy_send;
37 }
38
39 void accuracy_free(entity e)
40 {
41         remove(e.accuracy);
42 }
43
44 // force a resend of a player's accuracy stats
45 void accuracy_resend(entity e)
46 {
47         e.accuracy.SendFlags = 0xFFFFFF;
48 }
49
50 // update accuracy stats
51 void accuracy_set(entity e, float w, float hit, float fired)
52 {
53         e = e.accuracy;
54         w -= WEP_FIRST;
55         e.(accuracy_hit[w]) = hit;
56         e.(accuracy_fired[w]) = fired;
57         e.SendFlags |= pow(2, w);
58 }
59
60 void accuracy_add(entity e, float w, float hit, float fired)
61 {
62         e = e.accuracy;
63         w -= WEP_FIRST;
64         e.(accuracy_hit[w]) += hit;
65         e.(accuracy_fired[w]) += fired;
66         e.SendFlags |= pow(2, w);
67 }