2 // TODO: split target_push and put it in the target folder
5 #include <common/physics/movetypes/movetypes.qh>
7 void trigger_push_use(entity this, entity actor, entity trigger)
11 this.team = actor.team;
17 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
18 REGISTER_NET_LINKED(ENT_CLIENT_TARGET_PUSH)
21 trigger_push_calculatevelocity
24 org - origin of the object which is to be pushed
25 tgt - target entity (can be either a point or a model entity; if it is
26 the latter, its midpoint is used)
27 ht - jump height, measured from the higher one of org and tgt's midpoint
29 Returns: velocity for the jump
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
33 float grav, sdist, zdist, vs, vz, jumpheight;
36 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
38 grav = PHYS_GRAVITY(other);
39 if(PHYS_ENTGRAVITY(other))
40 grav *= PHYS_ENTGRAVITY(other);
42 zdist = torg.z - org.z;
43 sdist = vlen(torg - org - zdist * '0 0 1');
44 sdir = normalize(torg - org - zdist * '0 0 1');
46 // how high do we need to push the player?
47 jumpheight = fabs(ht);
49 jumpheight = jumpheight + zdist;
54 You will not understand the following equations anyway...
55 But here is what I did to get them.
60 z(t) = t * vz - 1/2 grav t^2
66 max(z, ti) = jumpheight
68 From these three equations, you will find the three parameters vs, vz
72 // push him so high...
73 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
75 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
81 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
82 // ALWAYS solvable because jumpheight >= zdist
84 solution_y = solution.x; // just in case it is not solvable due to roundoff errors, assume two equal solutions at their center (this is mainly for the usual case with ht == 0)
86 solution_x = solution.y; // solution_x is 0 in this case, so don't use it, but rather use solution_y (which will be sqrt(0.5 * jumpheight / grav), actually)
94 // almost straight line type
95 // jump apex is before the jump
96 // we must take the larger one
97 flighttime = solution.y;
102 // jump apex is during the jump
103 // we must take the larger one too
104 flighttime = solution.y;
112 // almost straight line type
113 // jump apex is after the jump
114 // we must take the smaller one
115 flighttime = solution.x;
120 // jump apex is during the jump
121 // we must take the larger one
122 flighttime = solution.y;
125 vs = sdist / flighttime;
127 // finally calculate the velocity
128 return sdir * vs + '0 0 1' * vz;
131 bool jumppad_push(entity this, entity targ)
133 if (!isPushable(targ))
138 targ.velocity = trigger_push_calculatevelocity(targ.origin, this.enemy, this.height);
140 else if(this.target && this.target != "")
143 RandomSelection_Init();
144 for(e = NULL; (e = find(e, targetname, this.target)); )
147 RandomSelection_AddEnt(e, e.cnt, 1);
149 RandomSelection_AddEnt(e, 1, 1);
151 targ.velocity = trigger_push_calculatevelocity(targ.origin, RandomSelection_chosen_ent, this.height);
155 targ.velocity = this.movedir;
158 UNSET_ONGROUND(targ);
161 if (targ.flags & FL_PROJECTILE)
163 targ.angles = vectoangles (targ.velocity);
164 switch(targ.move_movetype)
167 set_movetype(targ, MOVETYPE_TOSS);
170 case MOVETYPE_BOUNCEMISSILE:
171 set_movetype(targ, MOVETYPE_BOUNCE);
181 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
182 targ.oldvelocity = targ.velocity;
184 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
186 // flash when activated
187 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
188 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
189 this.pushltime = time + 0.2;
191 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
194 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
195 if(targ.(jumppadsused[i]) == this)
199 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
200 targ.jumppadcount = targ.jumppadcount + 1;
203 if(IS_REAL_CLIENT(targ))
206 centerprint(targ, this.message);
209 targ.lastteleporttime = time;
212 animdecide_setaction(targ, ANIMACTION_JUMP, true);
215 targ.jumppadcount = true;
217 // reset tracking of who pushed you into a hazard (for kill credit)
222 if(this.enemy.target)
223 SUB_UseTargets(this.enemy, targ, this);
225 if (targ.flags & FL_PROJECTILE)
227 targ.angles = vectoangles (targ.velocity);
228 targ.com_phys_gravity_factor = 1;
229 switch(targ.move_movetype)
232 set_movetype(targ, MOVETYPE_TOSS);
235 case MOVETYPE_BOUNCEMISSILE:
236 set_movetype(targ, MOVETYPE_BOUNCE);
240 UpdateCSQCProjectile(targ);
247 void trigger_push_touch(entity this, entity toucher)
249 if (this.active == ACTIVE_NOT)
253 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
256 EXACTTRIGGER_TOUCH(this, toucher);
258 noref bool success = jumppad_push(this, toucher);
261 if (success && (this.spawnflags & PUSH_ONCE))
263 settouch(this, func_null);
264 setthink(this, SUB_Remove);
265 this.nextthink = time;
271 void trigger_push_link(entity this);
272 void trigger_push_updatelink(entity this);
273 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
275 setorigin(tracetest_ent, org);
276 tracetoss(tracetest_ent, tracetest_ent);
282 // since tracetoss starting from jumppad's origin often fails when target
283 // is very close to real destination, start it directly from target's
285 tracetest_ent.velocity.z = 0;
286 setorigin(tracetest_ent, targ.origin + stepheightvec);
287 tracetoss(tracetest_ent, tracetest_ent);
290 setorigin(tracetest_ent, targ.origin + stepheightvec / 2);
291 tracetoss(tracetest_ent, tracetest_ent);
294 setorigin(tracetest_ent, targ.origin);
295 tracetoss(tracetest_ent, tracetest_ent);
301 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
305 void trigger_push_findtarget(entity this)
307 // first calculate a typical start point for the jump
308 vector org = (this.absmin + this.absmax) * 0.5;
309 org.z = this.absmax.z - PL_MIN_CONST.z;
315 vector vel = '0 0 0';
317 for(entity t = NULL; (t = find(t, targetname, this.target)); )
321 if(t.move_movetype != MOVETYPE_NONE)
325 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
326 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
327 e.velocity = trigger_push_calculatevelocity(org, t, this.height);
329 vector best_target = '0 0 0';
330 vector best_org = '0 0 0';
331 vector best_vel = '0 0 0';
332 bool valid_best_target = false;
333 if (trigger_push_testorigin(e, t, this, org))
335 best_target = trace_endpos;
337 best_vel = e.velocity;
338 valid_best_target = true;
342 vector dist = t.origin - org;
343 if (dist.x || dist.y) // if not perfectly vertical
345 // test trajectory with different starting points, sometimes the trajectory
346 // starting from the jumppad origin can't reach the real destination
347 // and destination waypoint ends up near the jumppad itself
348 vector flatdir = normalize(dist - eZ * dist.z);
349 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
351 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
353 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
354 e.velocity = autocvar_sv_maxspeed * flatdir;
355 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
357 best_target = trace_endpos;
360 valid_best_target = true;
363 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
365 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
366 e.velocity = autocvar_sv_maxspeed * flatdir;
367 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
369 best_target = trace_endpos;
372 valid_best_target = true;
376 if (valid_best_target)
378 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
380 float velxy = vlen(vec2(best_vel));
381 float cost = vlen(vec2(t.origin - best_org)) / velxy;
382 if(velxy < autocvar_sv_maxspeed)
383 velxy = autocvar_sv_maxspeed;
384 cost += vlen(vec2(best_target - t.origin)) / velxy;
385 waypoint_spawnforteleporter(this, best_target, cost, e);
396 objerror (this, "Jumppad with nonexistant target");
402 // exactly one dest - bots love that
403 this.enemy = find(NULL, targetname, this.target);
407 // have to use random selection every single time
415 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
416 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
418 e.velocity = this.movedir;
420 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
421 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
425 defer(this, 0.1, trigger_push_updatelink);
430 float trigger_push_send(entity this, entity to, float sf)
432 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
434 WriteByte(MSG_ENTITY, this.team);
435 WriteInt24_t(MSG_ENTITY, this.spawnflags);
436 WriteByte(MSG_ENTITY, this.active);
437 WriteCoord(MSG_ENTITY, this.height);
439 WriteCoord(MSG_ENTITY, this.movedir_x);
440 WriteCoord(MSG_ENTITY, this.movedir_y);
441 WriteCoord(MSG_ENTITY, this.movedir_z);
443 trigger_common_write(this, true);
448 void trigger_push_updatelink(entity this)
453 void trigger_push_link(entity this)
455 trigger_link(this, trigger_push_send);
461 * target: target of jump
462 * height: the absolute value is the height of the highest point of the jump
463 * trajectory above the higher one of the player and the target.
464 * the sign indicates whether the highest point is INSIDE (positive)
465 * or OUTSIDE (negative) of the jump trajectory. General rule: use
466 * positive values for targets mounted on the floor, and use negative
467 * values to target a point on the ceiling.
468 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
470 spawnfunc(trigger_push)
476 this.active = ACTIVE_ACTIVE;
477 this.use = trigger_push_use;
478 settouch(this, trigger_push_touch);
483 this.movedir = this.movedir * this.speed * 10;
486 this.noise = "misc/jumppad.wav";
487 precache_sound (this.noise);
489 trigger_push_link(this); // link it now
491 // this must be called to spawn the teleport waypoints for bots
492 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
496 bool target_push_send(entity this, entity to, float sf)
498 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
500 WriteByte(MSG_ENTITY, this.cnt);
501 WriteString(MSG_ENTITY, this.targetname);
502 WriteCoord(MSG_ENTITY, this.origin_x);
503 WriteCoord(MSG_ENTITY, this.origin_y);
504 WriteCoord(MSG_ENTITY, this.origin_z);
506 WriteAngle(MSG_ENTITY, this.angles_x);
507 WriteAngle(MSG_ENTITY, this.angles_y);
508 WriteAngle(MSG_ENTITY, this.angles_z);
513 void target_push_use(entity this, entity actor, entity trigger)
515 if(trigger.classname == "trigger_push" || trigger == this)
516 return; // WTF, why is this a thing
518 jumppad_push(this, actor);
521 void target_push_link(entity this)
523 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
524 Net_LinkEntity(this, false, 0, target_push_send);
525 //this.SendFlags |= 1; // update
528 void target_push_init(entity this)
530 this.mangle = this.angles;
531 setorigin(this, this.origin);
532 target_push_link(this);
535 void target_push_init2(entity this)
537 if(this.target && this.target != "") // we have an old style pusher!
539 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
540 this.use = target_push_use;
543 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
546 spawnfunc(target_push) { target_push_init2(this); }
547 spawnfunc(info_notnull) { target_push_init(this); }
548 spawnfunc(target_position) { target_push_init(this); }
552 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
554 this.classname = "jumppad";
555 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
556 this.spawnflags = ReadInt24_t();
557 this.active = ReadByte();
558 this.height = ReadCoord();
560 this.movedir_x = ReadCoord();
561 this.movedir_y = ReadCoord();
562 this.movedir_z = ReadCoord();
564 trigger_common_read(this, true);
566 this.entremove = trigger_remove_generic;
567 this.solid = SOLID_TRIGGER;
568 settouch(this, trigger_push_touch);
569 this.move_time = time;
570 defer(this, 0.25, trigger_push_findtarget);
575 void target_push_remove(entity this)
578 //strunzone(this.classname);
579 //this.classname = string_null;
582 strunzone(this.targetname);
583 this.targetname = string_null;
586 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
588 this.classname = "push_target";
589 this.cnt = ReadByte();
590 this.targetname = strzone(ReadString());
591 this.origin_x = ReadCoord();
592 this.origin_y = ReadCoord();
593 this.origin_z = ReadCoord();
595 this.angles_x = ReadAngle();
596 this.angles_y = ReadAngle();
597 this.angles_z = ReadAngle();
601 setorigin(this, this.origin);
603 this.drawmask = MASK_NORMAL;
604 this.entremove = target_push_remove;