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 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 controller.owner = this;
245 this.move_controller = controller;
246 controller.platmovetype = this.platmovetype;
247 controller.platmovetype_start = this.platmovetype_start;
248 controller.platmovetype_end = this.platmovetype_end;
249 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
250 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
251 controller.animstate_starttime = time;
252 controller.animstate_endtime = time + traveltime;
253 setthink(controller, SUB_CalcMove_controller_think);
254 controller.think1 = getthink(this);
256 // the thinking is now done by the controller
257 setthink(this, SUB_NullThink); // for PushMove
258 this.nextthink = this.ltime + traveltime;
261 getthink(controller)(controller);
264 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
270 objerror (this, "No speed is defined!");
273 this.finaldest = tdest;
274 setthink(this, SUB_CalcMoveDone);
276 if (tdest == this.origin)
278 this.velocity = '0 0 0';
279 this.nextthink = this.ltime + 0.1;
283 delta = tdest - this.origin;
291 traveltime = vlen (delta) / tspeed;
298 // Very short animations don't really show off the effect
299 // of controlled animation, so let's just use linear movement.
300 // Alternatively entities can choose to specify non-controlled movement.
301 // The only currently implemented alternative movement is linear (value 1)
302 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
304 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
305 this.nextthink = this.ltime + traveltime;
309 // now just run like a bezier curve...
310 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
313 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
315 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
322 calculate this.avelocity and this.nextthink to reach destangle from
325 The calling function should make sure this.setthink is valid
328 void SUB_CalcAngleMoveDone(entity this)
330 // After rotating, set angle to exact final angle
331 this.angles = this.finalangle;
332 this.avelocity = '0 0 0';
334 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
338 // FIXME: I fixed this function only for rotation around the main axes
339 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
342 objerror (this, "No speed is defined!");
344 // take the shortest distance for the angles
345 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
346 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
347 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
348 vector delta = destangle - this.angles;
357 traveltime = vlen (delta) / tspeed;
365 this.finalangle = destangle;
366 setthink(this, SUB_CalcAngleMoveDone);
368 if (traveltime < 0.1)
370 this.avelocity = '0 0 0';
371 this.nextthink = this.ltime + 0.1;
375 this.avelocity = delta * (1 / traveltime);
376 this.nextthink = this.ltime + traveltime;
379 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
381 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
385 void ApplyMinMaxScaleAngles(entity e)
387 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
389 e.maxs = '1 1 1' * vlen(
390 '1 0 0' * max(-e.mins.x, e.maxs.x) +
391 '0 1 0' * max(-e.mins.y, e.maxs.y) +
392 '0 0 1' * max(-e.mins.z, e.maxs.z)
396 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
399 '1 0 0' * max(-e.mins.x, e.maxs.x) +
400 '0 1 0' * max(-e.mins.y, e.maxs.y)
403 e.mins_x = -e.maxs.x;
404 e.mins_y = -e.maxs.x;
407 setsize(e, RoundPerfectVector(e.mins * e.scale), RoundPerfectVector(e.maxs * e.scale));
409 setsize(e, e.mins, e.maxs);
412 void SetBrushEntityModel(entity this, bool with_lod)
416 precache_model(this.model);
417 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
419 vector mi = this.mins;
420 vector ma = this.maxs;
421 _setmodel(this, this.model); // no precision needed
422 setsize(this, mi, ma);
425 _setmodel(this, this.model); // no precision needed
427 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
429 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
430 this.angles_y = anglemods(this.angles_y - 180);
432 setorigin(this, this.origin);
433 ApplyMinMaxScaleAngles(this);
436 bool LOD_customize(entity this, entity client)
438 if(autocvar_loddebug)
440 int d = autocvar_loddebug;
442 this.modelindex = this.lodmodelindex0;
443 else if(d == 2 || !this.lodmodelindex2)
444 this.modelindex = this.lodmodelindex1;
446 this.modelindex = this.lodmodelindex2;
450 // TODO csqc network this so it only gets sent once
451 vector near_point = NearestPointOnBox(this, client.origin);
452 if(vdist(near_point - client.origin, <, this.loddistance1))
453 this.modelindex = this.lodmodelindex0;
454 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
455 this.modelindex = this.lodmodelindex1;
457 this.modelindex = this.lodmodelindex2;
462 void LOD_uncustomize(entity this)
464 this.modelindex = this.lodmodelindex0;
467 void LODmodel_attach(entity this)
471 if(!this.loddistance1)
472 this.loddistance1 = 1000;
473 if(!this.loddistance2)
474 this.loddistance2 = 2000;
475 this.lodmodelindex0 = this.modelindex;
477 if(this.lodtarget1 != "")
479 e = find(NULL, targetname, this.lodtarget1);
482 this.lodmodel1 = e.model;
486 if(this.lodtarget2 != "")
488 e = find(NULL, targetname, this.lodtarget2);
491 this.lodmodel2 = e.model;
496 if(autocvar_loddebug < 0)
498 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
501 if(this.lodmodel1 != "")
507 precache_model(this.lodmodel1);
508 _setmodel(this, this.lodmodel1);
509 this.lodmodelindex1 = this.modelindex;
511 if(this.lodmodel2 != "")
513 precache_model(this.lodmodel2);
514 _setmodel(this, this.lodmodel2);
515 this.lodmodelindex2 = this.modelindex;
518 this.modelindex = this.lodmodelindex0;
519 setsize(this, mi, ma);
522 if(this.lodmodelindex1)
523 if (!getSendEntity(this))
524 SetCustomizer(this, LOD_customize, LOD_uncustomize);
533 void SetMovedir(entity this)
535 if(this.movedir != '0 0 0')
536 this.movedir = normalize(this.movedir);
539 makevectors(this.angles);
540 this.movedir = v_forward;
543 this.angles = '0 0 0';
546 void InitTrigger(entity this)
548 // trigger angles are used for one-way touches. An angle of 0 is assumed
549 // to mean no restrictions, so use a yaw of 360 instead.
551 this.solid = SOLID_TRIGGER;
552 SetBrushEntityModel(this, false);
553 set_movetype(this, MOVETYPE_NONE);
558 void InitSolidBSPTrigger(entity this)
560 // trigger angles are used for one-way touches. An angle of 0 is assumed
561 // to mean no restrictions, so use a yaw of 360 instead.
563 this.solid = SOLID_BSP;
564 SetBrushEntityModel(this, false);
565 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
566 // this.modelindex = 0;
570 bool InitMovingBrushTrigger(entity this)
572 // trigger angles are used for one-way touches. An angle of 0 is assumed
573 // to mean no restrictions, so use a yaw of 360 instead.
574 this.solid = SOLID_BSP;
575 SetBrushEntityModel(this, true);
576 set_movetype(this, MOVETYPE_PUSH);
577 if(this.modelindex == 0)
579 objerror(this, "InitMovingBrushTrigger: no brushes found!");