]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/antilag.qc
#includes: cleanup
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / antilag.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4     #include "../common/vehicles/all.qh"
5     #include "antilag.qh"
6 #endif
7
8 const int ANTILAG_MAX_ORIGINS = 64;
9 .vector antilag_origins[ANTILAG_MAX_ORIGINS];
10 .float antilag_times[ANTILAG_MAX_ORIGINS];
11 .int antilag_index;
12 .vector antilag_saved_origin;
13 .float antilag_takenback;
14
15 .float antilag_debug;
16
17 void antilag_record(entity e, float t)
18 {
19     if (e.vehicle && e.vehicle.vehicle_flags == VHF_PLAYERSLOT)
20         return;
21
22     if(e.vehicle)
23         antilag_record(e.vehicle, t);
24
25         if(time < e.(antilag_times[e.antilag_index]))
26                 return;
27         e.antilag_index = e.antilag_index + 1;
28         if(e.antilag_index >= ANTILAG_MAX_ORIGINS)
29                 e.antilag_index = 0;
30         e.(antilag_times[e.antilag_index]) = t;
31         e.(antilag_origins[e.antilag_index]) = e.origin;
32
33         if(e.antilag_debug)
34                 te_spark(antilag_takebackorigin(e, t - e.antilag_debug), '0 0 0', 32);
35
36 }
37
38 // finds the index BEFORE t
39 float antilag_find(entity e, float t)
40 {
41         for(int i = e.antilag_index; i > 0; --i)
42                 if(e.(antilag_times[i]) >= t)
43                         if(e.(antilag_times[i - 1]) < t)
44                                 return i - 1;
45
46         if(e.(antilag_times[0]) >= t)
47                 if(e.(antilag_times[ANTILAG_MAX_ORIGINS - 1]) < t)
48                         return ANTILAG_MAX_ORIGINS - 1;
49
50         for(int i = ANTILAG_MAX_ORIGINS - 1; i > e.antilag_index + 1; --i)
51                 if(e.(antilag_times[i]) >= t)
52                         if(e.(antilag_times[i - 1]) < t)
53                                 return i - 1;
54
55         // if we get here, t is sandwiched nowhere, so let's assume it's in the present
56         return -1;
57 }
58
59 vector antilag_takebackorigin(entity e, float t)
60 {
61         int i0 = antilag_find(e, t);
62         if (i0 < 0)
63         {
64                 // IN THE PRESENT
65                 if(e.antilag_takenback)
66                         return e.antilag_saved_origin;
67                 else
68                         return e.origin;
69         }
70         int i1 = i0 + 1;
71         if (i1 >= ANTILAG_MAX_ORIGINS)
72                 i1 = 0;
73
74         return lerpv(e.(antilag_times[i0]), e.(antilag_origins[i0]), e.(antilag_times[i1]), e.(antilag_origins[i1]), t);
75 }
76
77 vector antilag_takebackavgvelocity(entity e, float t0, float t1)
78 {
79         vector o0, o1;
80
81         if(t0 >= t1)
82                 return '0 0 0';
83         o0 = antilag_takebackorigin(e, t0);
84         o1 = antilag_takebackorigin(e, t1);
85         return (o1 - o0) * (1 / (t1 - t0));
86 }
87
88 void antilag_takeback(entity e, float t)
89 {
90
91     if (e.vehicle && e.vehicle.vehicle_flags == VHF_PLAYERSLOT)
92         return;
93
94         if(e.vehicle)
95                 antilag_takeback(e.vehicle, t);
96
97         if(!e.antilag_takenback)
98                 e.antilag_saved_origin = e.origin;
99
100         setorigin(e, antilag_takebackorigin(e, t));
101         e.antilag_takenback = true;
102 }
103
104 void antilag_restore(entity e)
105 {
106     if (e.vehicle && e.vehicle.vehicle_flags == VHF_PLAYERSLOT)
107         return;
108
109         if(e.vehicle)
110                 antilag_restore(e.vehicle);
111
112         if(!e.antilag_takenback)
113                 return;
114
115         setorigin(e, e.antilag_saved_origin);
116         e.antilag_takenback = false;
117 }
118
119 void antilag_clear(entity e)
120 {
121         antilag_restore(e);
122         for (int i = 0; i < ANTILAG_MAX_ORIGINS; ++i)
123         {
124                 e.(antilag_times[i]) = -2342;
125                 e.(antilag_origins[i]) = e.origin;
126         }
127         e.antilag_index = ANTILAG_MAX_ORIGINS - 1; // next one is 0
128 }