2 void SUB_NullThink(entity this) { }
4 void SUB_CalcMoveDone(entity this);
5 void SUB_CalcAngleMoveDone(entity this);
11 // if anything breaks, tell the mapper to fix his map! info_null is meant to remove itself immediately.
19 Applies some friction to this
23 void SUB_Friction (entity this)
25 this.nextthink = time;
27 this.velocity = this.velocity * (1 - frametime * this.friction);
34 Makes client invisible or removes non-client
37 void SUB_VanishOrRemove (entity ent)
56 void SUB_SetFade_Think (entity this)
60 setthink(this, SUB_SetFade_Think);
61 this.nextthink = time;
62 this.alpha -= frametime * this.fade_rate;
63 if (this.alpha < 0.01)
64 SUB_VanishOrRemove(this);
66 this.nextthink = time;
73 Fade ent out when time >= vanish_time
76 void SUB_SetFade(entity ent, float vanish_time, float fading_time)
80 ent.fade_rate = 1/fading_time;
81 setthink(ent, SUB_SetFade_Think);
82 ent.nextthink = vanish_time;
89 calculate this.velocity and this.nextthink to reach dest from
90 this.origin traveling at speed
93 void SUB_CalcMoveDone(entity this)
95 // After moving, set origin to exact final destination
97 setorigin (this, this.finaldest);
98 this.velocity = '0 0 0';
100 if (this.think1 && this.think1 != SUB_CalcMoveDone)
104 .float platmovetype_turn;
105 void SUB_CalcMove_controller_think (entity this)
115 delta = this.destvec;
116 delta2 = this.destvec2;
117 if(time < this.animstate_endtime)
119 nexttick = time + PHYS_INPUT_FRAMETIME;
121 traveltime = this.animstate_endtime - this.animstate_starttime;
122 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
123 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
124 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
125 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
127 if(this.owner.platmovetype_turn)
130 destangle = delta + 2 * delta2 * phasepos;
131 destangle = vectoangles(destangle);
132 destangle_x = -destangle_x; // flip up / down orientation
134 // take the shortest distance for the angles
135 vector v = this.owner.angles;
136 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
137 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
138 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
139 this.owner.angles = v;
140 angloc = destangle - this.owner.angles;
141 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
142 this.owner.avelocity = angloc;
144 if(nexttick < this.animstate_endtime)
145 veloc = nextpos - this.owner.origin;
147 veloc = this.finaldest - this.owner.origin;
148 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
150 this.owner.velocity = veloc;
151 this.nextthink = nexttick;
155 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
156 entity own = this.owner;
157 setthink(own, this.think1);
158 // set the owner's reference to this entity to NULL
159 own.move_controller = NULL;
165 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
167 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
168 // 2 * control * t - 2 * control * t * t + destin * t * t
169 // 2 * control * t + (destin - 2 * control) * t * t
171 setorigin(controller, org);
175 controller.destvec = 2 * control; // control point
176 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
177 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
180 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
182 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
183 // 2 * control * t - 2 * control * t * t + destin * t * t
184 // 2 * control * t + (destin - 2 * control) * t * t
186 setorigin(controller, org);
189 controller.destvec = destin; // end point
190 controller.destvec2 = '0 0 0';
193 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
199 objerror (this, "No speed is defined!");
202 this.finaldest = tdest;
203 setthink(this, SUB_CalcMoveDone);
209 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
212 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
215 traveltime = vlen(tdest - this.origin) / tspeed;
222 if (traveltime < 0.1) // useless anim
224 this.velocity = '0 0 0';
225 this.nextthink = this.ltime + 0.1;
229 // delete the previous controller, otherwise changing movement midway is glitchy
230 if (this.move_controller != NULL)
232 delete(this.move_controller);
234 controller = new(SUB_CalcMove_controller);
235 controller.owner = this;
236 this.move_controller = controller;
237 controller.platmovetype = this.platmovetype;
238 controller.platmovetype_start = this.platmovetype_start;
239 controller.platmovetype_end = this.platmovetype_end;
240 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
241 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
242 controller.animstate_starttime = time;
243 controller.animstate_endtime = time + traveltime;
244 setthink(controller, SUB_CalcMove_controller_think);
245 controller.think1 = getthink(this);
247 // the thinking is now done by the controller
248 setthink(this, SUB_NullThink); // for PushMove
249 this.nextthink = this.ltime + traveltime;
252 getthink(controller)(controller);
255 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
261 objerror (this, "No speed is defined!");
264 this.finaldest = tdest;
265 setthink(this, SUB_CalcMoveDone);
267 if (tdest == this.origin)
269 this.velocity = '0 0 0';
270 this.nextthink = this.ltime + 0.1;
274 delta = tdest - this.origin;
282 traveltime = vlen (delta) / tspeed;
289 // Very short animations don't really show off the effect
290 // of controlled animation, so let's just use linear movement.
291 // Alternatively entities can choose to specify non-controlled movement.
292 // The only currently implemented alternative movement is linear (value 1)
293 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
295 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
296 this.nextthink = this.ltime + traveltime;
300 // now just run like a bezier curve...
301 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
304 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
306 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
313 calculate this.avelocity and this.nextthink to reach destangle from
316 The calling function should make sure this.setthink is valid
319 void SUB_CalcAngleMoveDone(entity this)
321 // After rotating, set angle to exact final angle
322 this.angles = this.finalangle;
323 this.avelocity = '0 0 0';
325 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
329 // FIXME: I fixed this function only for rotation around the main axes
330 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
333 objerror (this, "No speed is defined!");
335 // take the shortest distance for the angles
336 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
337 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
338 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
339 vector delta = destangle - this.angles;
348 traveltime = vlen (delta) / tspeed;
356 this.finalangle = destangle;
357 setthink(this, SUB_CalcAngleMoveDone);
359 if (traveltime < 0.1)
361 this.avelocity = '0 0 0';
362 this.nextthink = this.ltime + 0.1;
366 this.avelocity = delta * (1 / traveltime);
367 this.nextthink = this.ltime + traveltime;
370 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
372 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
376 void ApplyMinMaxScaleAngles(entity e)
378 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
380 e.maxs = '1 1 1' * vlen(
381 '1 0 0' * max(-e.mins.x, e.maxs.x) +
382 '0 1 0' * max(-e.mins.y, e.maxs.y) +
383 '0 0 1' * max(-e.mins.z, e.maxs.z)
387 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
390 '1 0 0' * max(-e.mins.x, e.maxs.x) +
391 '0 1 0' * max(-e.mins.y, e.maxs.y)
394 e.mins_x = -e.maxs.x;
395 e.mins_y = -e.maxs.x;
398 setsize(e, e.mins * e.scale, e.maxs * e.scale);
400 setsize(e, e.mins, e.maxs);
403 void SetBrushEntityModel(entity this, bool with_lod)
407 precache_model(this.model);
408 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
410 vector mi = this.mins;
411 vector ma = this.maxs;
412 _setmodel(this, this.model); // no precision needed
413 setsize(this, mi, ma);
416 _setmodel(this, this.model); // no precision needed
418 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
420 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
421 this.angles_y = anglemods(this.angles_y - 180);
423 setorigin(this, this.origin);
424 ApplyMinMaxScaleAngles(this);
427 bool LOD_customize(entity this, entity client)
429 if(autocvar_loddebug)
431 int d = autocvar_loddebug;
433 this.modelindex = this.lodmodelindex0;
434 else if(d == 2 || !this.lodmodelindex2)
435 this.modelindex = this.lodmodelindex1;
437 this.modelindex = this.lodmodelindex2;
441 // TODO csqc network this so it only gets sent once
442 vector near_point = NearestPointOnBox(this, client.origin);
443 if(vdist(near_point - client.origin, <, this.loddistance1))
444 this.modelindex = this.lodmodelindex0;
445 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
446 this.modelindex = this.lodmodelindex1;
448 this.modelindex = this.lodmodelindex2;
453 void LOD_uncustomize(entity this)
455 this.modelindex = this.lodmodelindex0;
458 void LODmodel_attach(entity this)
462 if(!this.loddistance1)
463 this.loddistance1 = 1000;
464 if(!this.loddistance2)
465 this.loddistance2 = 2000;
466 this.lodmodelindex0 = this.modelindex;
468 if(this.lodtarget1 != "")
470 e = find(NULL, targetname, this.lodtarget1);
473 this.lodmodel1 = e.model;
477 if(this.lodtarget2 != "")
479 e = find(NULL, targetname, this.lodtarget2);
482 this.lodmodel2 = e.model;
487 if(autocvar_loddebug < 0)
489 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
492 if(this.lodmodel1 != "")
498 precache_model(this.lodmodel1);
499 _setmodel(this, this.lodmodel1);
500 this.lodmodelindex1 = this.modelindex;
502 if(this.lodmodel2 != "")
504 precache_model(this.lodmodel2);
505 _setmodel(this, this.lodmodel2);
506 this.lodmodelindex2 = this.modelindex;
509 this.modelindex = this.lodmodelindex0;
510 setsize(this, mi, ma);
513 if(this.lodmodelindex1)
514 if (!getSendEntity(this))
515 SetCustomizer(this, LOD_customize, LOD_uncustomize);
524 void SetMovedir(entity this)
526 if(this.movedir != '0 0 0')
527 this.movedir = normalize(this.movedir);
530 makevectors(this.angles);
531 this.movedir = v_forward;
534 this.angles = '0 0 0';
537 void InitTrigger(entity this)
539 // trigger angles are used for one-way touches. An angle of 0 is assumed
540 // to mean no restrictions, so use a yaw of 360 instead.
542 this.solid = SOLID_TRIGGER;
543 SetBrushEntityModel(this, false);
544 set_movetype(this, MOVETYPE_NONE);
549 void InitSolidBSPTrigger(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_BSP;
555 SetBrushEntityModel(this, false);
556 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
557 // this.modelindex = 0;
561 bool InitMovingBrushTrigger(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.
565 this.solid = SOLID_BSP;
566 SetBrushEntityModel(this, true);
567 set_movetype(this, MOVETYPE_PUSH);
568 if(this.modelindex == 0)
570 objerror(this, "InitMovingBrushTrigger: no brushes found!");