5 #include <server/defs.qh>
6 #include <common/state.qh>
7 #include <common/vehicles/all.qh>
8 #include <lib/warpzone/common.qh>
12 const int ANTILAG_MAX_ORIGINS = 64;
13 .vector antilag_origins[ANTILAG_MAX_ORIGINS];
14 .float antilag_times[ANTILAG_MAX_ORIGINS];
16 .vector antilag_saved_origin;
17 .float antilag_takenback;
21 void antilag_record(entity e, entity store, float t)
24 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
25 antilag_record(e.vehicle, e.vehicle, t);
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;
35 if (store.antilag_debug)
36 te_spark(antilag_takebackorigin(e, store, t - store.antilag_debug), '0 0 0', 32);
39 // finds the index BEFORE t
40 float antilag_find(entity e, entity store, float t)
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)
47 if(store.antilag_times[0] >= t)
48 if(store.antilag_times[ANTILAG_MAX_ORIGINS - 1] < t)
49 return ANTILAG_MAX_ORIGINS - 1;
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)
56 // if we get here, t is sandwiched nowhere, so let's assume it's in the present
60 vector antilag_takebackorigin(entity e, entity store, float t)
62 int i0 = antilag_find(e, store, t);
66 if(store.antilag_takenback)
67 return store.antilag_saved_origin;
72 if (i1 >= ANTILAG_MAX_ORIGINS)
75 return lerpv(store.antilag_times[i0], store.antilag_origins[i0], store.antilag_times[i1], store.antilag_origins[i1], t);
78 vector antilag_takebackavgvelocity(entity e, entity store, float t0, float t1)
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));
86 void antilag_takeback(entity e, entity store, float t)
89 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
90 antilag_takeback(e.vehicle, e.vehicle, t);
93 if (!store.antilag_takenback)
94 store.antilag_saved_origin = e.origin;
96 vector org = antilag_takebackorigin(e, store, t);
98 store.antilag_takenback = true;
101 void antilag_restore(entity e, entity store)
104 if (e.vehicle.vehicle_flags == VHF_PLAYERSLOT) return;
105 antilag_restore(e.vehicle, e.vehicle);
108 if (!store.antilag_takenback) return;
110 setorigin(e, store.antilag_saved_origin);
111 store.antilag_takenback = false;
114 void antilag_clear(entity e, entity store)
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;
121 store.antilag_index = ANTILAG_MAX_ORIGINS - 1; // next one is 0
124 // TODO: use a single intrusive list across all antilagged entities
125 void antilag_takeback_all(entity ignore, float lag)
127 FOREACH_CLIENT(IS_PLAYER(it) && it != ignore, antilag_takeback(it, CS(it), time - lag));
128 IL_EACH(g_monsters, it != ignore,
130 antilag_takeback(it, it, time - lag);
132 IL_EACH(g_projectiles, it != ignore && it.classname == "nade",
134 antilag_takeback(it, it, time - lag);
138 void antilag_restore_all(entity ignore)
140 FOREACH_CLIENT(IS_PLAYER(it) && it != ignore, antilag_restore(it, CS(it)));
141 IL_EACH(g_monsters, it != ignore,
143 antilag_restore(it, it);
145 IL_EACH(g_projectiles, it != ignore && it.classname == "nade",
147 antilag_restore(it, it);
155 A version of traceline that must be used by SOLID_SLIDEBOX things that want to hit SOLID_CORPSE things with a trace attack
156 Additionally it moves players back into the past before the trace and restores them afterward.
159 void tracebox_antilag_force_wz (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag, float wz)
161 // check whether antilagged traces are enabled
164 if (!IS_REAL_CLIENT(forent))
165 lag = 0; // only antilag for clients
167 // change shooter to SOLID_BBOX so the shot can hit corpses
168 int oldsolid = source.dphitcontentsmask;
170 source.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
173 antilag_takeback_all(forent, lag);
177 WarpZone_TraceBox (v1, mi, ma, v2, nomonst, forent);
179 tracebox (v1, mi, ma, v2, nomonst, forent);
181 // restore players to current positions
183 antilag_restore_all(forent);
185 // restore shooter solid type
187 source.dphitcontentsmask = oldsolid;
189 void traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
191 tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, false);
193 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
195 bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
196 if (autocvar_g_antilag != 2 || noantilag)
198 traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
200 void tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
202 bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
203 if (autocvar_g_antilag != 2 || noantilag)
205 tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, false);
207 void WarpZone_traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
209 tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, true);
211 void WarpZone_traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
213 bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
214 if (autocvar_g_antilag != 2 || noantilag)
216 WarpZone_traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
218 void WarpZone_tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
220 bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
221 if (autocvar_g_antilag != 2 || noantilag)
223 tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, true);