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