]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/antilag.qc
Merge branch 'terencehill/min_spec_time' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / antilag.qc
1 #include "antilag.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include <server/defs.qh>
6     #include <common/state.qh>
7     #include <common/vehicles/all.qh>
8         #include <lib/warpzone/common.qh>
9     #include "antilag.qh"
10 #endif
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 /*
152 ==================
153 traceline_antilag
154
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.
157 ==================
158 */
159 void tracebox_antilag_force_wz (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag, float wz)
160 {
161         // check whether antilagged traces are enabled
162         if (lag < 0.001)
163                 lag = 0;
164         if (!IS_REAL_CLIENT(forent))
165                 lag = 0; // only antilag for clients
166
167         // change shooter to SOLID_BBOX so the shot can hit corpses
168         int oldsolid = source.dphitcontentsmask;
169         if(source)
170                 source.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
171
172         if (lag)
173                 antilag_takeback_all(forent, lag);
174
175         // do the trace
176         if(wz)
177                 WarpZone_TraceBox (v1, mi, ma, v2, nomonst, forent);
178         else
179                 tracebox (v1, mi, ma, v2, nomonst, forent);
180
181         // restore players to current positions
182         if (lag)
183                 antilag_restore_all(forent);
184
185         // restore shooter solid type
186         if(source)
187                 source.dphitcontentsmask = oldsolid;
188 }
189 void traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
190 {
191         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, false);
192 }
193 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
194 {
195         bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
196         if (autocvar_g_antilag != 2 || noantilag)
197                 lag = 0;
198         traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
199 }
200 void tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
201 {
202         bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
203         if (autocvar_g_antilag != 2 || noantilag)
204                 lag = 0;
205         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, false);
206 }
207 void WarpZone_traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
208 {
209         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, true);
210 }
211 void WarpZone_traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
212 {
213         bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
214         if (autocvar_g_antilag != 2 || noantilag)
215                 lag = 0;
216         WarpZone_traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
217 }
218 void WarpZone_tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
219 {
220         bool noantilag = ((IS_CLIENT(source)) ? CS(source).cvar_cl_noantilag : false);
221         if (autocvar_g_antilag != 2 || noantilag)
222                 lag = 0;
223         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, true);
224 }