]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/antilag.qc
Merge MR 'Various Q3 and QL map entity features and fixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / antilag.qc
1 #include "antilag.qh"
2
3 #include <common/monsters/sv_monsters.qh>
4 #include <common/state.qh>
5 #include <common/stats.qh>
6 #include <common/vehicles/all.qh>
7 #include <common/weapons/_all.qh>
8 #include <lib/warpzone/common.qh>
9 #include <server/antilag.qh>
10 #include <server/weapons/common.qh>
11
12 const int ANTILAG_MAX_ORIGINS = 64;
13 .vector antilag_origins[ANTILAG_MAX_ORIGINS];
14 .float antilag_times[ANTILAG_MAX_ORIGINS];
15 .int antilag_index;
16 .vector antilag_saved_origin;
17 .float antilag_takenback;
18
19 .float antilag_debug;
20
21 void antilag_record(entity e, entity store, float t)
22 {
23         if (e.vehicle) {
24                 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
25                 antilag_record(e.vehicle, e.vehicle, t);
26         }
27
28         if (time < store.antilag_times[store.antilag_index]) return;
29         store.antilag_index += 1;
30         if (store.antilag_index >= ANTILAG_MAX_ORIGINS)
31                 store.antilag_index = 0;
32         store.antilag_times[store.antilag_index] = t;
33         store.antilag_origins[store.antilag_index] = e.origin;
34
35         if (store.antilag_debug)
36                 te_spark(antilag_takebackorigin(e, store, t - store.antilag_debug), '0 0 0', 32);
37 }
38
39 // finds the index BEFORE t
40 float antilag_find(entity e, entity store, float t)
41 {
42         for(int i = store.antilag_index; i > 0; --i)
43                 if(store.antilag_times[i] >= t)
44                         if(store.antilag_times[i - 1] < t)
45                                 return i - 1;
46
47         if(store.antilag_times[0] >= t)
48                 if(store.antilag_times[ANTILAG_MAX_ORIGINS - 1] < t)
49                         return ANTILAG_MAX_ORIGINS - 1;
50
51         for(int i = ANTILAG_MAX_ORIGINS - 1; i > store.antilag_index + 1; --i)
52                 if(store.antilag_times[i] >= t)
53                         if(store.antilag_times[i - 1] < t)
54                                 return i - 1;
55
56         // if we get here, t is sandwiched nowhere, so let's assume it's in the present
57         return -1;
58 }
59
60 vector antilag_takebackorigin(entity e, entity store, float t)
61 {
62         int i0 = antilag_find(e, store, t);
63         if (i0 < 0)
64         {
65                 // IN THE PRESENT
66                 if(store.antilag_takenback)
67                         return store.antilag_saved_origin;
68                 else
69                         return e.origin;
70         }
71         int i1 = i0 + 1;
72         if (i1 >= ANTILAG_MAX_ORIGINS)
73                 i1 = 0;
74
75         return lerpv(store.antilag_times[i0], store.antilag_origins[i0], store.antilag_times[i1], store.antilag_origins[i1], t);
76 }
77
78 vector antilag_takebackavgvelocity(entity e, entity store, float t0, float t1)
79 {
80         if (t0 >= t1) return '0 0 0';
81         vector o0 = antilag_takebackorigin(e, store, t0);
82         vector o1 = antilag_takebackorigin(e, store, t1);
83         return (o1 - o0) * (1 / (t1 - t0));
84 }
85
86 void antilag_takeback(entity e, entity store, float t)
87 {
88         if (e.vehicle) {
89                 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
90                 antilag_takeback(e.vehicle, e.vehicle, t);
91         }
92
93         if (!store.antilag_takenback)
94                 store.antilag_saved_origin = e.origin;
95
96         vector org = antilag_takebackorigin(e, store, t);
97         setorigin(e, org);
98         store.antilag_takenback = true;
99 }
100
101 void antilag_restore(entity e, entity store)
102 {
103         if (e.vehicle) {
104                 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
105                 antilag_restore(e.vehicle, e.vehicle);
106         }
107
108         if (!store.antilag_takenback) return;
109
110         setorigin(e, store.antilag_saved_origin);
111         store.antilag_takenback = false;
112 }
113
114 void antilag_clear(entity e, entity store)
115 {
116         antilag_restore(e, store);
117         for (int i = 0; i < ANTILAG_MAX_ORIGINS; ++i) {
118                 store.antilag_times[i] = -2342;
119                 store.antilag_origins[i] = e.origin;
120         }
121         store.antilag_index = ANTILAG_MAX_ORIGINS - 1; // next one is 0
122 }
123
124 // TODO: use a single intrusive list across all antilagged entities
125 void antilag_takeback_all(entity ignore, float lag)
126 {
127         FOREACH_CLIENT(IS_PLAYER(it) && it != ignore, antilag_takeback(it, CS(it), time - lag));
128         IL_EACH(g_monsters, it != ignore,
129         {
130                 antilag_takeback(it, it, time - lag);
131         });
132         IL_EACH(g_projectiles, it != ignore && it.classname == "nade",
133         {
134                 antilag_takeback(it, it, time - lag);
135         });
136 }
137
138 void antilag_restore_all(entity ignore)
139 {
140         FOREACH_CLIENT(IS_PLAYER(it) && it != ignore, antilag_restore(it, CS(it)));
141         IL_EACH(g_monsters, it != ignore,
142         {
143                 antilag_restore(it, it);
144         });
145         IL_EACH(g_projectiles, it != ignore && it.classname == "nade",
146         {
147                 antilag_restore(it, it);
148         });
149 }
150
151 float antilag_getlag(entity e)
152 {
153         float lag = ((IS_REAL_CLIENT(e)) ? ANTILAG_LATENCY(e) : 0);
154         bool noantilag = ((IS_CLIENT(e)) ? CS_CVAR(e).cvar_cl_noantilag : false);
155         if(autocvar_g_antilag == 0 || noantilag || lag < 0.001)
156                 lag = 0;
157
158         return lag;
159 }
160
161 /*
162 ==================
163 traceline_antilag
164
165 A version of traceline that must be used by SOLID_SLIDEBOX things that want to hit SOLID_CORPSE things with a trace attack
166 Additionally it moves players back into the past before the trace and restores them afterward.
167 ==================
168 */
169 void tracebox_antilag_force_wz (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag, float wz)
170 {
171         // check whether antilagged traces are enabled
172         if (lag < 0.001)
173                 lag = 0;
174         if (!IS_REAL_CLIENT(forent))
175                 lag = 0; // only antilag for clients
176
177         // change shooter to SOLID_BBOX so the shot can hit corpses
178         int oldsolid = source.dphitcontentsmask;
179         if(source)
180                 source.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
181
182         if (lag)
183                 antilag_takeback_all(forent, lag);
184
185         // do the trace
186         if(wz)
187                 WarpZone_TraceBox (v1, mi, ma, v2, nomonst, forent);
188         else
189                 tracebox (v1, mi, ma, v2, nomonst, forent);
190
191         // restore players to current positions
192         if (lag)
193                 antilag_restore_all(forent);
194
195         // restore shooter solid type
196         if(source)
197                 source.dphitcontentsmask = oldsolid;
198 }
199 void traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
200 {
201         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, false);
202 }
203 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
204 {
205         bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
206         if (autocvar_g_antilag != 2 || noantilag)
207                 lag = 0;
208         traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
209 }
210 void tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
211 {
212         bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
213         if (autocvar_g_antilag != 2 || noantilag)
214                 lag = 0;
215         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, false);
216 }
217 void WarpZone_traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
218 {
219         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, true);
220 }
221 void WarpZone_traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
222 {
223         bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
224         if (autocvar_g_antilag != 2 || noantilag)
225                 lag = 0;
226         WarpZone_traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
227 }
228 void WarpZone_tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
229 {
230         bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
231         if (autocvar_g_antilag != 2 || noantilag)
232                 lag = 0;
233         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, true);
234 }