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 this.move_controller.animstate_starttime += frametime;
108 this.move_controller.animstate_endtime += frametime;
111 .float platmovetype_turn;
112 void SUB_CalcMove_controller_think (entity this)
122 delta = this.destvec;
123 delta2 = this.destvec2;
124 if(time < this.animstate_endtime)
126 nexttick = time + PHYS_INPUT_FRAMETIME;
128 traveltime = this.animstate_endtime - this.animstate_starttime;
129 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
130 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
131 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
132 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
134 if(this.owner.platmovetype_turn)
137 destangle = delta + 2 * delta2 * phasepos;
138 destangle = vectoangles(destangle);
139 destangle_x = -destangle_x; // flip up / down orientation
141 // take the shortest distance for the angles
142 vector v = this.owner.angles;
143 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
144 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
145 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
146 this.owner.angles = v;
147 angloc = destangle - this.owner.angles;
148 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
149 this.owner.avelocity = angloc;
151 if(nexttick < this.animstate_endtime)
152 veloc = nextpos - this.owner.origin;
154 veloc = this.finaldest - this.owner.origin;
155 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
157 this.owner.velocity = veloc;
158 this.nextthink = nexttick;
162 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
163 entity own = this.owner;
164 setthink(own, this.think1);
165 // set the owner's reference to this entity to NULL
166 own.move_controller = NULL;
172 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
174 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
175 // 2 * control * t - 2 * control * t * t + destin * t * t
176 // 2 * control * t + (destin - 2 * control) * t * t
178 //setorigin(controller, org); // don't link to the world
179 controller.origin = org;
183 controller.destvec = 2 * control; // control point
184 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
185 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
188 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
190 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
191 // 2 * control * t - 2 * control * t * t + destin * t * t
192 // 2 * control * t + (destin - 2 * control) * t * t
194 //setorigin(controller, org); // don't link to the world
195 controller.origin = org;
198 controller.destvec = destin; // end point
199 controller.destvec2 = '0 0 0';
202 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
208 objerror (this, "No speed is defined!");
211 this.finaldest = tdest;
212 setthink(this, SUB_CalcMoveDone);
218 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
221 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
224 traveltime = vlen(tdest - this.origin) / tspeed;
231 if (traveltime < 0.1) // useless anim
233 this.velocity = '0 0 0';
234 this.nextthink = this.ltime + 0.1;
238 // delete the previous controller, otherwise changing movement midway is glitchy
239 if (this.move_controller != NULL)
241 delete(this.move_controller);
243 controller = new_pure(SUB_CalcMove_controller);
244 set_movetype(controller, MOVETYPE_NONE); // mark the entity as physics driven so that thinking is handled by QC
245 controller.owner = this;
246 this.move_controller = controller;
247 controller.platmovetype = this.platmovetype;
248 controller.platmovetype_start = this.platmovetype_start;
249 controller.platmovetype_end = this.platmovetype_end;
250 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
251 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
252 controller.animstate_starttime = time;
253 controller.animstate_endtime = time + traveltime;
254 setthink(controller, SUB_CalcMove_controller_think);
255 controller.think1 = getthink(this);
257 // the thinking is now done by the controller
258 setthink(this, SUB_NullThink); // for PushMove
259 this.nextthink = this.ltime + traveltime;
262 getthink(controller)(controller);
265 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
271 objerror (this, "No speed is defined!");
274 this.finaldest = tdest;
275 setthink(this, SUB_CalcMoveDone);
277 if (tdest == this.origin)
279 this.velocity = '0 0 0';
280 this.nextthink = this.ltime + 0.1;
284 delta = tdest - this.origin;
292 traveltime = vlen (delta) / tspeed;
299 // Very short animations don't really show off the effect
300 // of controlled animation, so let's just use linear movement.
301 // Alternatively entities can choose to specify non-controlled movement.
302 // The only currently implemented alternative movement is linear (value 1)
303 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
305 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
306 this.nextthink = this.ltime + traveltime;
310 // now just run like a bezier curve...
311 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
314 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
316 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
323 calculate this.avelocity and this.nextthink to reach destangle from
326 The calling function should make sure this.setthink is valid
329 void SUB_CalcAngleMoveDone(entity this)
331 // After rotating, set angle to exact final angle
332 this.angles = this.finalangle;
333 this.avelocity = '0 0 0';
335 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
339 // FIXME: I fixed this function only for rotation around the main axes
340 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
343 objerror (this, "No speed is defined!");
345 // take the shortest distance for the angles
346 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
347 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
348 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
349 vector delta = destangle - this.angles;
358 traveltime = vlen (delta) / tspeed;
366 this.finalangle = destangle;
367 setthink(this, SUB_CalcAngleMoveDone);
369 if (traveltime < 0.1)
371 this.avelocity = '0 0 0';
372 this.nextthink = this.ltime + 0.1;
376 this.avelocity = delta * (1 / traveltime);
377 this.nextthink = this.ltime + traveltime;
380 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
382 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
386 void ApplyMinMaxScaleAngles(entity e)
388 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
390 e.maxs = '1 1 1' * vlen(
391 '1 0 0' * max(-e.mins.x, e.maxs.x) +
392 '0 1 0' * max(-e.mins.y, e.maxs.y) +
393 '0 0 1' * max(-e.mins.z, e.maxs.z)
397 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
400 '1 0 0' * max(-e.mins.x, e.maxs.x) +
401 '0 1 0' * max(-e.mins.y, e.maxs.y)
404 e.mins_x = -e.maxs.x;
405 e.mins_y = -e.maxs.x;
408 setsize(e, RoundPerfectVector(e.mins * e.scale), RoundPerfectVector(e.maxs * e.scale));
410 setsize(e, e.mins, e.maxs);
413 void SetBrushEntityModel(entity this, bool with_lod)
415 // Ensure .solid is set correctly before calling this (for area grid linking/unlinking)
419 precache_model(this.model);
420 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
422 vector mi = this.mins;
423 vector ma = this.maxs;
424 _setmodel(this, this.model); // no precision needed
425 setsize(this, mi, ma);
428 _setmodel(this, this.model); // no precision needed
430 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
432 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
433 this.angles_y = anglemods(this.angles_y - 180);
435 setorigin(this, this.origin);
436 ApplyMinMaxScaleAngles(this);
439 bool LOD_customize(entity this, entity client)
441 if(autocvar_loddebug)
443 int d = autocvar_loddebug;
445 this.modelindex = this.lodmodelindex0;
446 else if(d == 2 || !this.lodmodelindex2)
447 this.modelindex = this.lodmodelindex1;
449 this.modelindex = this.lodmodelindex2;
453 // TODO csqc network this so it only gets sent once
454 vector near_point = NearestPointOnBox(this, client.origin);
455 if(vdist(near_point - client.origin, <, this.loddistance1))
456 this.modelindex = this.lodmodelindex0;
457 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
458 this.modelindex = this.lodmodelindex1;
460 this.modelindex = this.lodmodelindex2;
465 void LOD_uncustomize(entity this)
467 this.modelindex = this.lodmodelindex0;
470 void LODmodel_attach(entity this)
474 if(!this.loddistance1)
475 this.loddistance1 = 1000;
476 if(!this.loddistance2)
477 this.loddistance2 = 2000;
478 this.lodmodelindex0 = this.modelindex;
480 if(this.lodtarget1 != "")
482 e = find(NULL, targetname, this.lodtarget1);
485 this.lodmodel1 = e.model;
489 if(this.lodtarget2 != "")
491 e = find(NULL, targetname, this.lodtarget2);
494 this.lodmodel2 = e.model;
499 if(autocvar_loddebug < 0)
501 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
504 if(this.lodmodel1 != "" && fexists(this.lodmodel1))
510 precache_model(this.lodmodel1);
511 _setmodel(this, this.lodmodel1);
512 this.lodmodelindex1 = this.modelindex;
514 if(this.lodmodel2 != "" && fexists(this.lodmodel2))
516 precache_model(this.lodmodel2);
517 _setmodel(this, this.lodmodel2);
518 this.lodmodelindex2 = this.modelindex;
521 this.modelindex = this.lodmodelindex0;
522 setsize(this, mi, ma);
525 if(this.lodmodelindex1)
526 if (!getSendEntity(this))
527 SetCustomizer(this, LOD_customize, LOD_uncustomize);
536 void SetMovedir(entity this)
538 if(this.movedir != '0 0 0')
539 this.movedir = normalize(this.movedir);
542 makevectors(this.angles);
543 this.movedir = v_forward;
546 this.angles = '0 0 0';
549 void InitTrigger(entity this)
551 // trigger angles are used for one-way touches. An angle of 0 is assumed
552 // to mean no restrictions, so use a yaw of 360 instead.
554 this.solid = SOLID_TRIGGER;
555 SetBrushEntityModel(this, false);
556 set_movetype(this, MOVETYPE_NONE);
561 void InitSolidBSPTrigger(entity this)
563 // trigger angles are used for one-way touches. An angle of 0 is assumed
564 // to mean no restrictions, so use a yaw of 360 instead.
566 this.solid = SOLID_BSP;
567 SetBrushEntityModel(this, false);
568 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
569 // this.modelindex = 0;
573 bool InitMovingBrushTrigger(entity this)
575 // trigger angles are used for one-way touches. An angle of 0 is assumed
576 // to mean no restrictions, so use a yaw of 360 instead.
577 this.solid = SOLID_BSP;
578 SetBrushEntityModel(this, true);
579 set_movetype(this, MOVETYPE_PUSH);
580 if(this.modelindex == 0)
582 objerror(this, "InitMovingBrushTrigger: no brushes found!");