Merge remote branch 'refs/remotes/origin/fruitiex/racefixes'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / interpolate.qc
1 // FIXME make this generic code, to be used for other entities too?
2 .vector iorigin1, iorigin2;
3 .vector ivelocity1, ivelocity2;
4 .vector iforward1, iforward2;
5 .vector iup1, iup2;
6 .float itime1, itime2;
7 void InterpolateOrigin_Reset()
8 {
9         self.iflags &~= IFLAG_INTERNALMASK;
10         self.itime1 = self.itime2 = 0;
11 }
12 void InterpolateOrigin_Note()
13 {
14         float dt;
15         float f0;
16
17         dt = time - self.itime2;
18
19         f0 = self.iflags;
20         if(self.iflags & IFLAG_PREVALID)
21                 self.iflags |= IFLAG_VALID;
22         else
23                 self.iflags |= IFLAG_PREVALID;
24
25         self.iorigin1 = self.iorigin2;
26         self.iorigin2 = self.origin;
27
28         if(self.iflags & IFLAG_AUTOANGLES)
29                 if(self.iorigin2 != self.iorigin1)
30                         self.angles = vectoangles(self.iorigin2 - self.iorigin1);
31
32         if(self.iflags & IFLAG_ANGLES)
33         {
34                 fixedmakevectors(self.angles);
35                 if(f0 & IFLAG_VALID)
36                 {
37                         self.iforward1 = self.iforward2;
38                         self.iup1 = self.iup2;
39                 }
40                 else
41                 {
42                         self.iforward1 = v_forward;
43                         self.iup1 = v_up;
44                 }
45                 self.iforward2 = v_forward;
46                 self.iup2 = v_up;
47         }
48
49         if(self.iflags & IFLAG_VELOCITY)
50         {
51                 self.ivelocity1 = self.ivelocity2;
52                 self.ivelocity2 = self.velocity;
53         }
54
55         if(vlen(self.iorigin2 - self.iorigin1) > 1000)
56         {
57                 self.itime1 = self.itime2 = time; // don't lerp
58         }
59
60         if((self.iflags & IFLAG_VELOCITY) && (vlen(self.ivelocity2 - self.ivelocity1) > 1000))
61         {
62                 self.itime1 = self.itime2 = time; // don't lerp
63         }
64
65         if(dt < 0.2)
66         {
67                 self.itime1 = serverprevtime;
68                 self.itime2 = time;
69         }
70         else
71         {
72                 // don't lerp
73                 self.itime1 = self.itime2 = time;
74         }
75 }
76 void InterpolateOrigin_Do()
77 {
78         vector forward, up;
79         if(self.itime1 && self.itime2 && self.itime1 != self.itime2)
80         {
81                 float f;
82                 f = bound(0, (time - self.itime1) / (self.itime2 - self.itime1), 1);
83                 self.origin = (1 - f) * self.iorigin1 + f * self.iorigin2;
84                 if(self.iflags & IFLAG_ANGLES)
85                 {
86                         forward = (1 - f) * self.iforward1 + f * self.iforward2;
87                         up = (1 - f) * self.iup1 + f * self.iup2;
88                         self.angles = fixedvectoangles2(forward, up);
89                 }
90                 if(self.iflags & IFLAG_VELOCITY)
91                         self.velocity = (1 - f) * self.ivelocity1 + f * self.ivelocity2;
92         }
93 }
94 void InterpolateOrigin_Undo()
95 {
96         self.origin = self.iorigin2;
97         if(self.iflags & IFLAG_ANGLES)
98                 self.angles = fixedvectoangles2(self.iforward2, self.iup2);
99         if(self.iflags & IFLAG_VELOCITY)
100                 self.velocity = self.ivelocity2;
101 }
102