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); // don't link to the world
173 controller.origin = org;
177 controller.destvec = 2 * control; // control point
178 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
179 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
182 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
184 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
185 // 2 * control * t - 2 * control * t * t + destin * t * t
186 // 2 * control * t + (destin - 2 * control) * t * t
188 //setorigin(controller, org); // don't link to the world
189 controller.origin = org;
192 controller.destvec = destin; // end point
193 controller.destvec2 = '0 0 0';
196 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
202 objerror (this, "No speed is defined!");
205 this.finaldest = tdest;
206 setthink(this, SUB_CalcMoveDone);
212 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
215 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
218 traveltime = vlen(tdest - this.origin) / tspeed;
225 if (traveltime < 0.1) // useless anim
227 this.velocity = '0 0 0';
228 this.nextthink = this.ltime + 0.1;
232 // delete the previous controller, otherwise changing movement midway is glitchy
233 if (this.move_controller != NULL)
235 delete(this.move_controller);
237 controller = new_pure(SUB_CalcMove_controller);
238 controller.owner = this;
239 this.move_controller = controller;
240 controller.platmovetype = this.platmovetype;
241 controller.platmovetype_start = this.platmovetype_start;
242 controller.platmovetype_end = this.platmovetype_end;
243 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
244 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
245 controller.animstate_starttime = time;
246 controller.animstate_endtime = time + traveltime;
247 setthink(controller, SUB_CalcMove_controller_think);
248 controller.think1 = getthink(this);
250 // the thinking is now done by the controller
251 setthink(this, SUB_NullThink); // for PushMove
252 this.nextthink = this.ltime + traveltime;
255 getthink(controller)(controller);
258 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
264 objerror (this, "No speed is defined!");
267 this.finaldest = tdest;
268 setthink(this, SUB_CalcMoveDone);
270 if (tdest == this.origin)
272 this.velocity = '0 0 0';
273 this.nextthink = this.ltime + 0.1;
277 delta = tdest - this.origin;
285 traveltime = vlen (delta) / tspeed;
292 // Very short animations don't really show off the effect
293 // of controlled animation, so let's just use linear movement.
294 // Alternatively entities can choose to specify non-controlled movement.
295 // The only currently implemented alternative movement is linear (value 1)
296 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
298 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
299 this.nextthink = this.ltime + traveltime;
303 // now just run like a bezier curve...
304 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
307 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
309 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
316 calculate this.avelocity and this.nextthink to reach destangle from
319 The calling function should make sure this.setthink is valid
322 void SUB_CalcAngleMoveDone(entity this)
324 // After rotating, set angle to exact final angle
325 this.angles = this.finalangle;
326 this.avelocity = '0 0 0';
328 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
332 // FIXME: I fixed this function only for rotation around the main axes
333 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
336 objerror (this, "No speed is defined!");
338 // take the shortest distance for the angles
339 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
340 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
341 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
342 vector delta = destangle - this.angles;
351 traveltime = vlen (delta) / tspeed;
359 this.finalangle = destangle;
360 setthink(this, SUB_CalcAngleMoveDone);
362 if (traveltime < 0.1)
364 this.avelocity = '0 0 0';
365 this.nextthink = this.ltime + 0.1;
369 this.avelocity = delta * (1 / traveltime);
370 this.nextthink = this.ltime + traveltime;
373 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
375 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
379 void ApplyMinMaxScaleAngles(entity e)
381 if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
383 e.maxs = '1 1 1' * vlen(
384 '1 0 0' * max(-e.mins.x, e.maxs.x) +
385 '0 1 0' * max(-e.mins.y, e.maxs.y) +
386 '0 0 1' * max(-e.mins.z, e.maxs.z)
390 else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
393 '1 0 0' * max(-e.mins.x, e.maxs.x) +
394 '0 1 0' * max(-e.mins.y, e.maxs.y)
397 e.mins_x = -e.maxs.x;
398 e.mins_y = -e.maxs.x;
401 setsize(e, e.mins * e.scale, e.maxs * e.scale);
403 setsize(e, e.mins, e.maxs);
406 void SetBrushEntityModel(entity this, bool with_lod)
410 precache_model(this.model);
411 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
413 vector mi = this.mins;
414 vector ma = this.maxs;
415 _setmodel(this, this.model); // no precision needed
416 setsize(this, mi, ma);
419 _setmodel(this, this.model); // no precision needed
421 InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
423 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
424 this.angles_y = anglemods(this.angles_y - 180);
426 setorigin(this, this.origin);
427 ApplyMinMaxScaleAngles(this);
430 bool LOD_customize(entity this, entity client)
432 if(autocvar_loddebug)
434 int d = autocvar_loddebug;
436 this.modelindex = this.lodmodelindex0;
437 else if(d == 2 || !this.lodmodelindex2)
438 this.modelindex = this.lodmodelindex1;
440 this.modelindex = this.lodmodelindex2;
444 // TODO csqc network this so it only gets sent once
445 vector near_point = NearestPointOnBox(this, client.origin);
446 if(vdist(near_point - client.origin, <, this.loddistance1))
447 this.modelindex = this.lodmodelindex0;
448 else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
449 this.modelindex = this.lodmodelindex1;
451 this.modelindex = this.lodmodelindex2;
456 void LOD_uncustomize(entity this)
458 this.modelindex = this.lodmodelindex0;
461 void LODmodel_attach(entity this)
465 if(!this.loddistance1)
466 this.loddistance1 = 1000;
467 if(!this.loddistance2)
468 this.loddistance2 = 2000;
469 this.lodmodelindex0 = this.modelindex;
471 if(this.lodtarget1 != "")
473 e = find(NULL, targetname, this.lodtarget1);
476 this.lodmodel1 = e.model;
480 if(this.lodtarget2 != "")
482 e = find(NULL, targetname, this.lodtarget2);
485 this.lodmodel2 = e.model;
490 if(autocvar_loddebug < 0)
492 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
495 if(this.lodmodel1 != "")
501 precache_model(this.lodmodel1);
502 _setmodel(this, this.lodmodel1);
503 this.lodmodelindex1 = this.modelindex;
505 if(this.lodmodel2 != "")
507 precache_model(this.lodmodel2);
508 _setmodel(this, this.lodmodel2);
509 this.lodmodelindex2 = this.modelindex;
512 this.modelindex = this.lodmodelindex0;
513 setsize(this, mi, ma);
516 if(this.lodmodelindex1)
517 if (!getSendEntity(this))
518 SetCustomizer(this, LOD_customize, LOD_uncustomize);
527 void SetMovedir(entity this)
529 if(this.movedir != '0 0 0')
530 this.movedir = normalize(this.movedir);
533 makevectors(this.angles);
534 this.movedir = v_forward;
537 this.angles = '0 0 0';
540 void InitTrigger(entity this)
542 // trigger angles are used for one-way touches. An angle of 0 is assumed
543 // to mean no restrictions, so use a yaw of 360 instead.
545 this.solid = SOLID_TRIGGER;
546 SetBrushEntityModel(this, false);
547 set_movetype(this, MOVETYPE_NONE);
552 void InitSolidBSPTrigger(entity this)
554 // trigger angles are used for one-way touches. An angle of 0 is assumed
555 // to mean no restrictions, so use a yaw of 360 instead.
557 this.solid = SOLID_BSP;
558 SetBrushEntityModel(this, false);
559 set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
560 // this.modelindex = 0;
564 bool InitMovingBrushTrigger(entity this)
566 // trigger angles are used for one-way touches. An angle of 0 is assumed
567 // to mean no restrictions, so use a yaw of 360 instead.
568 this.solid = SOLID_BSP;
569 SetBrushEntityModel(this, true);
570 set_movetype(this, MOVETYPE_PUSH);
571 if(this.modelindex == 0)
573 objerror(this, "InitMovingBrushTrigger: no brushes found!");