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>
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);
151 float antilag_getlag(entity e)
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)
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.
169 void tracebox_antilag_force_wz (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag, float wz)
171 // check whether antilagged traces are enabled
174 if (!IS_REAL_CLIENT(forent))
175 lag = 0; // only antilag for clients
177 // change shooter to SOLID_BBOX so the shot can hit corpses
178 int oldsolid = source.dphitcontentsmask;
180 source.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
183 antilag_takeback_all(forent, lag);
187 WarpZone_TraceBox (v1, mi, ma, v2, nomonst, forent);
189 tracebox (v1, mi, ma, v2, nomonst, forent);
191 // restore players to current positions
193 antilag_restore_all(forent);
195 // restore shooter solid type
197 source.dphitcontentsmask = oldsolid;
199 void traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
201 tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, false);
203 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
205 bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
206 if (autocvar_g_antilag != 2 || noantilag)
208 traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
210 void tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
212 bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
213 if (autocvar_g_antilag != 2 || noantilag)
215 tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, false);
217 void WarpZone_traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
219 tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, true);
221 void WarpZone_traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
223 bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
224 if (autocvar_g_antilag != 2 || noantilag)
226 WarpZone_traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
228 void WarpZone_tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
230 bool noantilag = ((IS_CLIENT(source)) ? CS_CVAR(source).cvar_cl_noantilag : false);
231 if (autocvar_g_antilag != 2 || noantilag)
233 tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, true);