fix division by zero
[divverent/nexuiz.git] / data / 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_Note()
8 {
9         float dt;
10
11         dt = time - self.itime2;
12
13         if(self.iflags & IFLAG_PREVALID)
14                 self.iflags |= IFLAG_VALID;
15         else
16                 self.iflags |= IFLAG_PREVALID;
17
18         self.iorigin1 = self.iorigin2;
19         self.iorigin2 = self.origin;
20
21         if(self.iflags & IFLAG_AUTOANGLES)
22                 self.angles = vectoangles(self.iorigin2 - self.iorigin1);
23
24         if(self.iflags & IFLAG_ANGLES)
25         {
26                 fixedmakevectors(self.angles);
27                 self.iforward1 = self.iforward2;
28                 self.iup1 = self.iup2;
29                 self.iforward2 = v_forward;
30                 self.iup2 = v_up;
31         }
32
33         if(self.iflags & IFLAG_VELOCITY)
34         {
35                 self.ivelocity1 = self.ivelocity2;
36                 self.ivelocity2 = self.velocity;
37         }
38
39         if(vlen(self.iorigin2 - self.iorigin1) > 1000)
40         {
41                 self.itime1 = self.itime2 = time; // don't lerp
42         }
43
44         if((self.iflags & IFLAG_VELOCITY) && (vlen(self.ivelocity2 - self.ivelocity1) > 1000))
45         {
46                 self.itime1 = self.itime2 = time; // don't lerp
47         }
48
49         if(dt < 0.2)
50         {
51                 self.itime1 = serverprevtime;
52                 self.itime2 = time;
53         }
54         else
55         {
56                 // don't lerp
57                 self.itime1 = self.itime2 = time;
58         }
59 }
60 void InterpolateOrigin_Do()
61 {
62         vector forward, up;
63         if(self.itime1 && self.itime2 && self.itime1 != self.itime2)
64         {
65                 float f;
66                 f = bound(0, (time - self.itime1) / (self.itime2 - self.itime1), 1);
67                 self.origin = (1 - f) * self.iorigin1 + f * self.iorigin2;
68                 if(self.iflags & IFLAG_ANGLES)
69                 {
70                         forward = (1 - f) * self.iforward1 + f * self.iforward2;
71                         up = (1 - f) * self.iup1 + f * self.iup2;
72                         self.angles = fixedvectoangles2(forward, up);
73                 }
74                 if(self.iflags & IFLAG_VELOCITY)
75                         self.velocity = (1 - f) * self.ivelocity1 + f * self.ivelocity2;
76         }
77 }
78 void InterpolateOrigin_Undo()
79 {
80         self.origin = self.iorigin2;
81         if(self.iflags & IFLAG_ANGLES)
82                 self.angles = fixedvectoangles2(self.iforward2, self.iup2);
83         if(self.iflags & IFLAG_VELOCITY)
84                 self.velocity = self.ivelocity2;
85 }
86