Fix extra life powerup giving infinite lives in lms & minstagib
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / accuracy.qc
1 .float accuracy_hit[WEP_MAXCOUNT];
2 .float accuracy_fired[WEP_MAXCOUNT];
3 .float accuracy_cnt_hit[WEP_MAXCOUNT];
4 .float accuracy_cnt_fired[WEP_MAXCOUNT];
5
6 float accuracy_byte(float n, float d)
7 {
8         //print(sprintf("accuracy: %d / %d\n", n, d));
9         if(n <= 0)
10                 return 0;
11         if(n > d)
12                 return 255;
13         return 1 + rint(n * 100.0 / d);
14 }
15
16 float accuracy_send(entity to, float sf)
17 {
18         float w, f;
19         entity a;
20         WriteByte(MSG_ENTITY, ENT_CLIENT_ACCURACY);
21
22         a = self.owner;
23         if(a.classname == "spectator")
24                 a = a.enemy;
25         a = a.accuracy;
26
27         if(to != a.owner)
28                 if not(self.owner.cvar_cl_accuracy_data_share && autocvar_sv_accuracy_data_share)
29                         sf = 0;
30         // note: zero sendflags can never be sent... so we can use that to say that we send no accuracy!
31         WriteInt24_t(MSG_ENTITY, sf);
32         if(sf == 0)
33                 return TRUE;
34         // note: we know that client and server agree about SendFlags...
35         for(w = 0, f = 1; w <= WEP_LAST - WEP_FIRST; ++w)
36         {
37                 if(sf & f)
38                         WriteByte(MSG_ENTITY, accuracy_byte(self.(accuracy_hit[w]), self.(accuracy_fired[w])));
39                 if(f == 0x800000)
40                         f = 1;
41                 else
42                         f *= 2;
43         }
44         return TRUE;
45 }
46
47 // init/free
48 void accuracy_init(entity e)
49 {
50         e.accuracy = spawn();
51         e.accuracy.owner = e;
52         e.accuracy.classname = "accuracy";
53         e.accuracy.drawonlytoclient = e;
54         Net_LinkEntity(e.accuracy, FALSE, 0, accuracy_send);
55 }
56
57 void accuracy_free(entity e)
58 {
59         remove(e.accuracy);
60 }
61
62 // force a resend of a player's accuracy stats
63 void accuracy_resend(entity e)
64 {
65         e.accuracy.SendFlags = 0xFFFFFF;
66 }
67
68 // update accuracy stats
69 .float hit_time;
70 .float fired_time;
71
72 void accuracy_add(entity e, float w, float fired, float hit)
73 {
74         entity a;
75         float b;
76         if(IS_INDEPENDENT_PLAYER(e))
77                 return;
78         a = e.accuracy;
79         if(!a || !(hit || fired))
80                 return;
81         w -= WEP_FIRST;
82         b = accuracy_byte(a.(accuracy_hit[w]), a.(accuracy_fired[w]));
83         if(hit)
84                 a.(accuracy_hit[w]) += hit;
85         if(fired)
86                 a.(accuracy_fired[w]) += fired;
87
88     if(hit && a.hit_time != time) // only run this once per frame
89     {
90         a.(accuracy_cnt_hit[w]) += 1;
91         a.hit_time = time;
92     }
93
94     if(fired && a.fired_time != time) // only run this once per frame
95     {
96         a.(accuracy_cnt_fired[w]) += 1;
97         a.fired_time = time;
98     }
99
100         if(b == accuracy_byte(a.(accuracy_hit[w]), a.(accuracy_fired[w])))
101                 return;
102         w = pow(2, mod(w, 24));
103         a.SendFlags |= w;
104         FOR_EACH_CLIENT(a)
105                 if(a.classname == "spectator")
106                         if(a.enemy == e)
107                                 a.SendFlags |= w;
108 }
109
110 float accuracy_isgooddamage(entity attacker, entity targ)
111 {
112         if(!inWarmupStage)
113         if(targ.flags & FL_CLIENT)
114         if(targ.deadflag == DEAD_NO)
115         if(IsDifferentTeam(attacker, targ))
116                 return TRUE;
117         return FALSE;
118 }
119
120 float accuracy_canbegooddamage(entity attacker)
121 {
122         if(!inWarmupStage)
123                 return TRUE;
124         return FALSE;
125 }