1 void SUB_NullThink(void) { }
3 void() SUB_CalcMoveDone;
4 void() SUB_CalcAngleMoveDone;
5 //void() SUB_UseTargets;
11 Applies some friction to self
15 void SUB_Friction (void)
17 self.SUB_NEXTTHINK = time;
18 if(self.SUB_FLAGS & FL_ONGROUND)
19 self.SUB_VELOCITY = self.SUB_VELOCITY * (1 - frametime * self.friction);
26 Makes client invisible or removes non-client
29 void SUB_VanishOrRemove (entity ent)
48 void SUB_SetFade_Think (void)
52 self.SUB_THINK = SUB_SetFade_Think;
53 self.SUB_NEXTTHINK = time;
54 self.alpha -= frametime * self.fade_rate;
55 if (self.alpha < 0.01)
56 SUB_VanishOrRemove(self);
58 self.SUB_NEXTTHINK = time;
65 Fade 'ent' out when time >= 'when'
68 void SUB_SetFade (entity ent, float when, float fading_time)
70 ent.fade_rate = 1/fading_time;
71 ent.SUB_THINK = SUB_SetFade_Think;
72 ent.SUB_NEXTTHINK = when;
79 calculate self.SUB_VELOCITY and self.SUB_NEXTTHINK to reach dest from
80 self.SUB_ORIGIN traveling at speed
83 void SUB_CalcMoveDone (void)
85 // After moving, set origin to exact final destination
87 SUB_SETORIGIN (self, self.finaldest);
88 self.SUB_VELOCITY = '0 0 0';
89 self.SUB_NEXTTHINK = -1;
94 .float platmovetype_turn;
95 void SUB_CalcMove_controller_think (void)
106 delta = self.destvec;
107 delta2 = self.destvec2;
108 if(time < self.animstate_endtime)
110 nexttick = time + PHYS_INPUT_FRAMETIME;
112 traveltime = self.animstate_endtime - self.animstate_starttime;
113 phasepos = (nexttick - self.animstate_starttime) / traveltime; // range: [0, 1]
114 phasepos = cubic_speedfunc(self.platmovetype_start, self.platmovetype_end, phasepos);
115 nextpos = self.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
116 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
118 if(self.owner.platmovetype_turn)
121 destangle = delta + 2 * delta2 * phasepos;
122 destangle = vectoangles(destangle);
123 destangle_x = -destangle_x; // flip up / down orientation
125 // take the shortest distance for the angles
126 vector v = SUB_ANGLES(self.owner);
127 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
128 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
129 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
130 SUB_ANGLES(self.owner) = v;
131 angloc = destangle - SUB_ANGLES(self.owner);
132 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
133 self.owner.SUB_AVELOCITY = angloc;
135 if(nexttick < self.animstate_endtime)
136 veloc = nextpos - self.owner.SUB_ORIGIN;
138 veloc = self.finaldest - self.owner.SUB_ORIGIN;
139 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
141 self.owner.SUB_VELOCITY = veloc;
142 self.nextthink = nexttick;
146 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
148 self.owner.SUB_THINK = self.think1;
155 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
157 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
158 // 2 * control * t - 2 * control * t * t + destin * t * t
159 // 2 * control * t + (destin - 2 * control) * t * t
161 setorigin(controller, org);
165 controller.destvec = 2 * control; // control point
166 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
167 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
170 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
172 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
173 // 2 * control * t - 2 * control * t * t + destin * t * t
174 // 2 * control * t + (destin - 2 * control) * t * t
176 setorigin(controller, org);
179 controller.destvec = destin; // end point
180 controller.destvec2 = '0 0 0';
183 float TSPEED_TIME = -1;
184 float TSPEED_LINEAR = 0;
185 float TSPEED_START = 1;
186 float TSPEED_END = 2;
189 void SUB_CalcMove_Bezier (vector tcontrol, vector tdest, float tspeedtype, float tspeed, void() func)
195 objerror ("No speed is defined!");
198 self.finaldest = tdest;
199 self.SUB_THINK = SUB_CalcMoveDone;
205 traveltime = 2 * vlen(tcontrol - self.SUB_ORIGIN) / tspeed;
208 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
211 traveltime = vlen(tdest - self.SUB_ORIGIN) / tspeed;
218 if (traveltime < 0.1) // useless anim
220 self.SUB_VELOCITY = '0 0 0';
221 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
225 controller = spawn();
226 controller.classname = "SUB_CalcMove_controller";
227 controller.owner = self;
228 controller.platmovetype = self.platmovetype;
229 controller.platmovetype_start = self.platmovetype_start;
230 controller.platmovetype_end = self.platmovetype_end;
231 SUB_CalcMove_controller_setbezier(controller, self.SUB_ORIGIN, tcontrol, tdest);
232 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
233 controller.animstate_starttime = time;
234 controller.animstate_endtime = time + traveltime;
235 controller.think = SUB_CalcMove_controller_think;
236 controller.think1 = self.SUB_THINK;
238 // the thinking is now done by the controller
239 self.SUB_THINK = SUB_NullThink; // for PushMove
240 self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
248 void SUB_CalcMove (vector tdest, float tspeedtype, float tspeed, void() func)
254 objerror ("No speed is defined!");
257 self.finaldest = tdest;
258 self.SUB_THINK = SUB_CalcMoveDone;
260 if (tdest == self.SUB_ORIGIN)
262 self.SUB_VELOCITY = '0 0 0';
263 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
267 delta = tdest - self.SUB_ORIGIN;
275 traveltime = vlen (delta) / tspeed;
282 // Very short animations don't really show off the effect
283 // of controlled animation, so let's just use linear movement.
284 // Alternatively entities can choose to specify non-controlled movement.
285 // The only currently implemented alternative movement is linear (value 1)
286 if (traveltime < 0.15 || (self.platmovetype_start == 1 && self.platmovetype_end == 1)) // is this correct?
288 self.SUB_VELOCITY = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
289 self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
293 // now just run like a bezier curve...
294 SUB_CalcMove_Bezier((self.SUB_ORIGIN + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
297 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void() func)
299 WITH(entity, self, ent, SUB_CalcMove(tdest, tspeedtype, tspeed, func));
306 calculate self.SUB_AVELOCITY and self.SUB_NEXTTHINK to reach destangle from
309 The calling function should make sure self.SUB_THINK is valid
312 void SUB_CalcAngleMoveDone (void)
314 // After rotating, set angle to exact final angle
315 self.angles = self.finalangle;
316 self.SUB_AVELOCITY = '0 0 0';
317 self.SUB_NEXTTHINK = -1;
322 // FIXME: I fixed this function only for rotation around the main axes
323 void SUB_CalcAngleMove (vector destangle, float tspeedtype, float tspeed, void() func)
329 objerror ("No speed is defined!");
331 // take the shortest distance for the angles
332 self.angles_x -= 360 * floor((self.angles_x - destangle_x) / 360 + 0.5);
333 self.angles_y -= 360 * floor((self.angles_y - destangle_y) / 360 + 0.5);
334 self.angles_z -= 360 * floor((self.angles_z - destangle_z) / 360 + 0.5);
335 delta = destangle - self.angles;
343 traveltime = vlen (delta) / tspeed;
351 self.finalangle = destangle;
352 self.SUB_THINK = SUB_CalcAngleMoveDone;
354 if (traveltime < 0.1)
356 self.SUB_AVELOCITY = '0 0 0';
357 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
361 self.SUB_AVELOCITY = delta * (1 / traveltime);
362 self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
365 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void() func)
367 WITH(entity, self, ent, SUB_CalcAngleMove (destangle, tspeedtype, tspeed, func));