3 void SUB_NullThink(entity this) { }
5 void SUB_CalcMoveDone(entity this);
6 void SUB_CalcAngleMoveDone(entity this);
12 // if anything breaks, tell the mapper to fix his map! info_null is meant to remove itself immediately.
20 Applies some friction to this
24 void SUB_Friction (entity this)
26 this.nextthink = time;
28 this.velocity = this.velocity * (1 - frametime * this.friction);
35 Makes client invisible or removes non-client
38 void SUB_VanishOrRemove (entity ent)
57 void SUB_SetFade_Think (entity this)
61 setthink(this, SUB_SetFade_Think);
62 this.nextthink = time;
63 this.alpha -= frametime * this.fade_rate;
64 if (this.alpha < 0.01)
65 SUB_VanishOrRemove(this);
67 this.nextthink = time;
74 Fade ent out when time >= vanish_time
77 void SUB_SetFade(entity ent, float vanish_time, float fading_time)
81 ent.fade_rate = 1/fading_time;
82 setthink(ent, SUB_SetFade_Think);
83 ent.nextthink = vanish_time;
90 calculate this.velocity and this.nextthink to reach dest from
91 this.origin traveling at speed
94 void SUB_CalcMoveDone(entity this)
96 // After moving, set origin to exact final destination
98 setorigin (this, this.finaldest);
99 this.velocity = '0 0 0';
101 if (this.think1 && this.think1 != SUB_CalcMoveDone)
105 .float platmovetype_turn;
106 void SUB_CalcMove_controller_think (entity this)
116 delta = this.destvec;
117 delta2 = this.destvec2;
118 if(time < this.animstate_endtime)
120 nexttick = time + PHYS_INPUT_FRAMETIME;
122 traveltime = this.animstate_endtime - this.animstate_starttime;
123 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
124 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
125 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
126 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
128 if(this.owner.platmovetype_turn)
131 destangle = delta + 2 * delta2 * phasepos;
132 destangle = vectoangles(destangle);
133 destangle_x = -destangle_x; // flip up / down orientation
135 // take the shortest distance for the angles
136 vector v = this.owner.angles;
137 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
138 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
139 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
140 this.owner.angles = v;
141 angloc = destangle - this.owner.angles;
142 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
143 this.owner.avelocity = angloc;
145 if(nexttick < this.animstate_endtime)
146 veloc = nextpos - this.owner.origin;
148 veloc = this.finaldest - this.owner.origin;
149 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
151 this.owner.velocity = veloc;
152 this.nextthink = nexttick;
156 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
157 entity own = this.owner;
158 setthink(own, this.think1);
159 // set the owner's reference to this entity to NULL
160 own.move_controller = NULL;
166 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
168 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
169 // 2 * control * t - 2 * control * t * t + destin * t * t
170 // 2 * control * t + (destin - 2 * control) * t * t
172 setorigin(controller, org);
176 controller.destvec = 2 * control; // control point
177 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
178 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
181 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
183 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
184 // 2 * control * t - 2 * control * t * t + destin * t * t
185 // 2 * control * t + (destin - 2 * control) * t * t
187 setorigin(controller, org);
190 controller.destvec = destin; // end point
191 controller.destvec2 = '0 0 0';
194 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
200 objerror (this, "No speed is defined!");
203 this.finaldest = tdest;
204 setthink(this, SUB_CalcMoveDone);
210 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
213 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
216 traveltime = vlen(tdest - this.origin) / tspeed;
223 if (traveltime < 0.1) // useless anim
225 this.velocity = '0 0 0';
226 this.nextthink = this.ltime + 0.1;
230 // delete the previous controller, otherwise changing movement midway is glitchy
231 if (this.move_controller != NULL)
233 delete(this.move_controller);
235 controller = new(SUB_CalcMove_controller);
236 controller.owner = this;
237 this.move_controller = controller;
238 controller.platmovetype = this.platmovetype;
239 controller.platmovetype_start = this.platmovetype_start;
240 controller.platmovetype_end = this.platmovetype_end;
241 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
242 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
243 controller.animstate_starttime = time;
244 controller.animstate_endtime = time + traveltime;
245 setthink(controller, SUB_CalcMove_controller_think);
246 controller.think1 = getthink(this);
248 // the thinking is now done by the controller
249 setthink(this, SUB_NullThink); // for PushMove
250 this.nextthink = this.ltime + traveltime;
253 getthink(controller)(controller);
256 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
262 objerror (this, "No speed is defined!");
265 this.finaldest = tdest;
266 setthink(this, SUB_CalcMoveDone);
268 if (tdest == this.origin)
270 this.velocity = '0 0 0';
271 this.nextthink = this.ltime + 0.1;
275 delta = tdest - this.origin;
283 traveltime = vlen (delta) / tspeed;
290 // Very short animations don't really show off the effect
291 // of controlled animation, so let's just use linear movement.
292 // Alternatively entities can choose to specify non-controlled movement.
293 // The only currently implemented alternative movement is linear (value 1)
294 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
296 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
297 this.nextthink = this.ltime + traveltime;
301 // now just run like a bezier curve...
302 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
305 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
307 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
314 calculate this.avelocity and this.nextthink to reach destangle from
317 The calling function should make sure this.setthink is valid
320 void SUB_CalcAngleMoveDone(entity this)
322 // After rotating, set angle to exact final angle
323 this.angles = this.finalangle;
324 this.avelocity = '0 0 0';
326 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
330 // FIXME: I fixed this function only for rotation around the main axes
331 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
334 objerror (this, "No speed is defined!");
336 // take the shortest distance for the angles
337 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
338 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
339 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
340 vector delta = destangle - this.angles;
349 traveltime = vlen (delta) / tspeed;
357 this.finalangle = destangle;
358 setthink(this, SUB_CalcAngleMoveDone);
360 if (traveltime < 0.1)
362 this.avelocity = '0 0 0';
363 this.nextthink = this.ltime + 0.1;
367 this.avelocity = delta * (1 / traveltime);
368 this.nextthink = this.ltime + traveltime;
371 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
373 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
377 void ApplyMinMaxScaleAngles(entity e)
379 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
381 e.maxs = '1 1 1' * vlen(
382 '1 0 0' * max(-e.mins.x, e.maxs.x) +
383 '0 1 0' * max(-e.mins.y, e.maxs.y) +
384 '0 0 1' * max(-e.mins.z, e.maxs.z)
388 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
391 '1 0 0' * max(-e.mins.x, e.maxs.x) +
392 '0 1 0' * max(-e.mins.y, e.maxs.y)
395 e.mins_x = -e.maxs.x;
396 e.mins_y = -e.maxs.x;
399 setsize(e, e.mins * e.scale, e.maxs * e.scale);
401 setsize(e, e.mins, e.maxs);
404 void SetBrushEntityModel(entity this, bool with_lod)
408 precache_model(this.model);
409 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
411 vector mi = this.mins;
412 vector ma = this.maxs;
413 _setmodel(this, this.model); // no precision needed
414 setsize(this, mi, ma);
417 _setmodel(this, this.model); // no precision needed
419 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
421 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
422 this.angles_y = anglemods(this.angles_y - 180);
424 setorigin(this, this.origin);
425 ApplyMinMaxScaleAngles(this);
428 bool LOD_customize(entity this, entity client)
430 if(autocvar_loddebug)
432 int d = autocvar_loddebug;
434 this.modelindex = this.lodmodelindex0;
435 else if(d == 2 || !this.lodmodelindex2)
436 this.modelindex = this.lodmodelindex1;
438 this.modelindex = this.lodmodelindex2;
442 // TODO csqc network this so it only gets sent once
443 vector near_point = NearestPointOnBox(this, client.origin);
444 if(vdist(near_point - client.origin, <, this.loddistance1))
445 this.modelindex = this.lodmodelindex0;
446 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
447 this.modelindex = this.lodmodelindex1;
449 this.modelindex = this.lodmodelindex2;
454 void LOD_uncustomize(entity this)
456 this.modelindex = this.lodmodelindex0;
459 void LODmodel_attach(entity this)
463 if(!this.loddistance1)
464 this.loddistance1 = 1000;
465 if(!this.loddistance2)
466 this.loddistance2 = 2000;
467 this.lodmodelindex0 = this.modelindex;
469 if(this.lodtarget1 != "")
471 e = find(NULL, targetname, this.lodtarget1);
474 this.lodmodel1 = e.model;
478 if(this.lodtarget2 != "")
480 e = find(NULL, targetname, this.lodtarget2);
483 this.lodmodel2 = e.model;
488 if(autocvar_loddebug < 0)
490 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
493 if(this.lodmodel1 != "")
499 precache_model(this.lodmodel1);
500 _setmodel(this, this.lodmodel1);
501 this.lodmodelindex1 = this.modelindex;
503 if(this.lodmodel2 != "")
505 precache_model(this.lodmodel2);
506 _setmodel(this, this.lodmodel2);
507 this.lodmodelindex2 = this.modelindex;
510 this.modelindex = this.lodmodelindex0;
511 setsize(this, mi, ma);
514 if(this.lodmodelindex1)
515 if (!getSendEntity(this))
516 SetCustomizer(this, LOD_customize, LOD_uncustomize);
525 void SetMovedir(entity this)
527 if(this.movedir != '0 0 0')
528 this.movedir = normalize(this.movedir);
531 makevectors(this.angles);
532 this.movedir = v_forward;
535 this.angles = '0 0 0';
538 void InitTrigger(entity this)
540 // trigger angles are used for one-way touches. An angle of 0 is assumed
541 // to mean no restrictions, so use a yaw of 360 instead.
543 this.solid = SOLID_TRIGGER;
544 SetBrushEntityModel(this, false);
545 set_movetype(this, MOVETYPE_NONE);
550 void InitSolidBSPTrigger(entity this)
552 // trigger angles are used for one-way touches. An angle of 0 is assumed
553 // to mean no restrictions, so use a yaw of 360 instead.
555 this.solid = SOLID_BSP;
556 SetBrushEntityModel(this, false);
557 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
558 // this.modelindex = 0;
562 bool InitMovingBrushTrigger(entity this)
564 // trigger angles are used for one-way touches. An angle of 0 is assumed
565 // to mean no restrictions, so use a yaw of 360 instead.
566 this.solid = SOLID_BSP;
567 SetBrushEntityModel(this, true);
568 set_movetype(this, MOVETYPE_PUSH);
569 if(this.modelindex == 0)
571 objerror(this, "InitMovingBrushTrigger: no brushes found!");