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 their 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 void SUB_CalcMovePause(entity this)
107 if (!this.move_controller)
109 this.move_controller.animstate_starttime += frametime;
110 this.move_controller.animstate_endtime += frametime;
113 .float platmovetype_turn;
114 void SUB_CalcMove_controller_think (entity this)
124 delta = this.destvec;
125 delta2 = this.destvec2;
126 if(time < this.animstate_endtime)
128 nexttick = time + PHYS_INPUT_FRAMETIME;
130 traveltime = this.animstate_endtime - this.animstate_starttime;
131 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
132 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
133 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
134 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
136 if(this.owner.platmovetype_turn)
139 destangle = delta + 2 * delta2 * phasepos;
140 destangle = vectoangles(destangle);
141 destangle_x = -destangle_x; // flip up / down orientation
143 // take the shortest distance for the angles
144 vector v = this.owner.angles;
145 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
146 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
147 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
148 this.owner.angles = v;
149 angloc = destangle - this.owner.angles;
150 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
151 this.owner.avelocity = angloc;
153 if(nexttick < this.animstate_endtime)
154 veloc = nextpos - this.owner.origin;
156 veloc = this.finaldest - this.owner.origin;
157 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
159 this.owner.velocity = veloc;
160 this.nextthink = nexttick;
164 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
165 entity own = this.owner;
166 setthink(own, this.think1);
167 // set the owner's reference to this entity to NULL
168 own.move_controller = NULL;
174 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
176 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
177 // 2 * control * t - 2 * control * t * t + destin * t * t
178 // 2 * control * t + (destin - 2 * control) * t * t
180 //setorigin(controller, org); // don't link to the world
181 controller.origin = org;
185 controller.destvec = 2 * control; // control point
186 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
187 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
190 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
192 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
193 // 2 * control * t - 2 * control * t * t + destin * t * t
194 // 2 * control * t + (destin - 2 * control) * t * t
196 //setorigin(controller, org); // don't link to the world
197 controller.origin = org;
200 controller.destvec = destin; // end point
201 controller.destvec2 = '0 0 0';
204 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
210 objerror (this, "No speed is defined!");
213 this.finaldest = tdest;
214 setthink(this, SUB_CalcMoveDone);
220 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
223 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
226 traveltime = vlen(tdest - this.origin) / tspeed;
233 if (traveltime < 0.1) // useless anim
235 this.velocity = '0 0 0';
236 this.nextthink = this.ltime + 0.1;
240 // delete the previous controller, otherwise changing movement midway is glitchy
241 if (this.move_controller != NULL)
243 delete(this.move_controller);
245 controller = new_pure(SUB_CalcMove_controller);
246 set_movetype(controller, MOVETYPE_NONE); // mark the entity as physics driven so that thinking is handled by QC
247 controller.owner = this;
248 this.move_controller = controller;
249 controller.platmovetype = this.platmovetype;
250 controller.platmovetype_start = this.platmovetype_start;
251 controller.platmovetype_end = this.platmovetype_end;
252 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
253 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
254 controller.animstate_starttime = time;
255 controller.animstate_endtime = time + traveltime;
256 setthink(controller, SUB_CalcMove_controller_think);
257 controller.think1 = getthink(this);
259 // the thinking is now done by the controller
260 setthink(this, SUB_NullThink); // for PushMove
261 this.nextthink = this.ltime + traveltime;
264 getthink(controller)(controller);
267 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
273 objerror (this, "No speed is defined!");
276 this.finaldest = tdest;
277 setthink(this, SUB_CalcMoveDone);
279 if (tdest == this.origin)
281 this.velocity = '0 0 0';
282 this.nextthink = this.ltime + 0.1;
286 delta = tdest - this.origin;
294 traveltime = vlen (delta) / tspeed;
301 // Very short animations don't really show off the effect
302 // of controlled animation, so let's just use linear movement.
303 // Alternatively entities can choose to specify non-controlled movement.
304 // The only currently implemented alternative movement is linear (value 1)
305 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
307 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
308 this.nextthink = this.ltime + traveltime;
312 // now just run like a bezier curve...
313 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
316 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
318 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
325 calculate this.avelocity and this.nextthink to reach destangle from
328 The calling function should make sure this.setthink is valid
331 void SUB_CalcAngleMoveDone(entity this)
333 // After rotating, set angle to exact final angle
334 this.angles = this.finalangle;
335 this.avelocity = '0 0 0';
337 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
341 // FIXME: I fixed this function only for rotation around the main axes
342 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
345 objerror (this, "No speed is defined!");
347 // take the shortest distance for the angles
348 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
349 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
350 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
351 vector delta = destangle - this.angles;
360 traveltime = vlen (delta) / tspeed;
368 this.finalangle = destangle;
369 setthink(this, SUB_CalcAngleMoveDone);
371 if (traveltime < 0.1)
373 this.avelocity = '0 0 0';
374 this.nextthink = this.ltime + 0.1;
378 this.avelocity = delta * (1 / traveltime);
379 this.nextthink = this.ltime + traveltime;
382 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
384 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
388 void ApplyMinMaxScaleAngles(entity e)
390 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
392 e.maxs = '1 1 1' * vlen(
393 '1 0 0' * max(-e.mins.x, e.maxs.x) +
394 '0 1 0' * max(-e.mins.y, e.maxs.y) +
395 '0 0 1' * max(-e.mins.z, e.maxs.z)
399 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
402 '1 0 0' * max(-e.mins.x, e.maxs.x) +
403 '0 1 0' * max(-e.mins.y, e.maxs.y)
406 e.mins_x = -e.maxs.x;
407 e.mins_y = -e.maxs.x;
410 setsize(e, RoundPerfectVector(e.mins * e.scale), RoundPerfectVector(e.maxs * e.scale));
412 setsize(e, e.mins, e.maxs);
415 void SetBrushEntityModel(entity this, bool with_lod)
417 // Ensure .solid is set correctly before calling this (for area grid linking/unlinking)
421 precache_model(this.model);
422 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
424 vector mi = this.mins;
425 vector ma = this.maxs;
426 _setmodel(this, this.model); // no precision needed
427 setsize(this, mi, ma);
430 _setmodel(this, this.model); // no precision needed
432 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
434 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
435 this.angles_y = anglemods(this.angles_y - 180);
437 setorigin(this, this.origin);
438 ApplyMinMaxScaleAngles(this);
441 bool LOD_customize(entity this, entity client)
443 if(autocvar_loddebug)
445 int d = autocvar_loddebug;
447 this.modelindex = this.lodmodelindex0;
448 else if(d == 2 || !this.lodmodelindex2)
449 this.modelindex = this.lodmodelindex1;
451 this.modelindex = this.lodmodelindex2;
455 // TODO csqc network this so it only gets sent once
456 vector near_point = NearestPointOnBox(this, client.origin);
457 if(vdist(near_point - client.origin, <, this.loddistance1))
458 this.modelindex = this.lodmodelindex0;
459 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
460 this.modelindex = this.lodmodelindex1;
462 this.modelindex = this.lodmodelindex2;
467 void LOD_uncustomize(entity this)
469 this.modelindex = this.lodmodelindex0;
472 void LODmodel_attach(entity this)
476 if(!this.loddistance1)
477 this.loddistance1 = 1000;
478 if(!this.loddistance2)
479 this.loddistance2 = 2000;
480 this.lodmodelindex0 = this.modelindex;
482 if(this.lodtarget1 != "")
484 e = find(NULL, targetname, this.lodtarget1);
487 this.lodmodel1 = e.model;
491 if(this.lodtarget2 != "")
493 e = find(NULL, targetname, this.lodtarget2);
496 this.lodmodel2 = e.model;
501 if(autocvar_loddebug < 0)
503 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
506 if(this.lodmodel1 != "" && fexists(this.lodmodel1))
512 precache_model(this.lodmodel1);
513 _setmodel(this, this.lodmodel1);
514 this.lodmodelindex1 = this.modelindex;
516 if(this.lodmodel2 != "" && fexists(this.lodmodel2))
518 precache_model(this.lodmodel2);
519 _setmodel(this, this.lodmodel2);
520 this.lodmodelindex2 = this.modelindex;
523 this.modelindex = this.lodmodelindex0;
524 setsize(this, mi, ma);
527 if(this.lodmodelindex1)
528 if (!getSendEntity(this))
529 SetCustomizer(this, LOD_customize, LOD_uncustomize);
538 void SetMovedir(entity this)
540 if(this.movedir != '0 0 0')
541 this.movedir = normalize(this.movedir);
544 makevectors(this.angles);
545 this.movedir = v_forward;
548 this.angles = '0 0 0';
551 void InitTrigger(entity this)
553 // trigger angles are used for one-way touches. An angle of 0 is assumed
554 // to mean no restrictions, so use a yaw of 360 instead.
556 this.solid = SOLID_TRIGGER;
557 SetBrushEntityModel(this, false);
558 set_movetype(this, MOVETYPE_NONE);
563 void InitSolidBSPTrigger(entity this)
565 // trigger angles are used for one-way touches. An angle of 0 is assumed
566 // to mean no restrictions, so use a yaw of 360 instead.
568 this.solid = SOLID_BSP;
569 SetBrushEntityModel(this, false);
570 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
571 // this.modelindex = 0;
575 bool InitMovingBrushTrigger(entity this)
577 // trigger angles are used for one-way touches. An angle of 0 is assumed
578 // to mean no restrictions, so use a yaw of 360 instead.
579 this.solid = SOLID_BSP;
580 SetBrushEntityModel(this, true);
581 set_movetype(this, MOVETYPE_PUSH);
582 if(this.modelindex == 0)
584 objerror(this, "InitMovingBrushTrigger: no brushes found!");