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
28 pushed_entity - object that is to be pushed
30 Returns: velocity for the jump
32 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht, entity pushed_entity)
34 float grav, sdist, zdist, vs, vz, jumpheight;
37 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
39 grav = PHYS_GRAVITY(NULL);
40 if(pushed_entity && PHYS_ENTGRAVITY(pushed_entity))
41 grav *= PHYS_ENTGRAVITY(pushed_entity);
43 zdist = torg.z - org.z;
44 sdist = vlen(torg - org - zdist * '0 0 1');
45 sdir = normalize(torg - org - zdist * '0 0 1');
47 // how high do we need to push the player?
48 jumpheight = fabs(ht);
50 jumpheight = jumpheight + zdist;
55 You will not understand the following equations anyway...
56 But here is what I did to get them.
61 z(t) = t * vz - 1/2 grav t^2
67 max(z, ti) = jumpheight
69 From these three equations, you will find the three parameters vs, vz
73 // push him so high...
74 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
76 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
82 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
83 // ALWAYS solvable because jumpheight >= zdist
85 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)
87 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)
95 // almost straight line type
96 // jump apex is before the jump
97 // we must take the larger one
98 flighttime = solution.y;
103 // jump apex is during the jump
104 // we must take the larger one too
105 flighttime = solution.y;
113 // almost straight line type
114 // jump apex is after the jump
115 // we must take the smaller one
116 flighttime = solution.x;
121 // jump apex is during the jump
122 // we must take the larger one
123 flighttime = solution.y;
126 vs = sdist / flighttime;
128 // finally calculate the velocity
129 return sdir * vs + '0 0 1' * vz;
132 bool jumppad_push(entity this, entity targ)
134 if (!isPushable(targ))
139 targ.velocity = trigger_push_calculatevelocity(targ.origin, this.enemy, this.height, targ);
141 else if(this.target && this.target != "")
144 RandomSelection_Init();
145 for(e = NULL; (e = find(e, targetname, this.target)); )
148 RandomSelection_AddEnt(e, e.cnt, 1);
150 RandomSelection_AddEnt(e, 1, 1);
152 targ.velocity = trigger_push_calculatevelocity(targ.origin, RandomSelection_chosen_ent, this.height, targ);
156 targ.velocity = this.movedir;
159 UNSET_ONGROUND(targ);
162 if (targ.flags & FL_PROJECTILE)
164 targ.angles = vectoangles (targ.velocity);
165 switch(targ.move_movetype)
168 set_movetype(targ, MOVETYPE_TOSS);
171 case MOVETYPE_BOUNCEMISSILE:
172 set_movetype(targ, MOVETYPE_BOUNCE);
182 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
183 targ.oldvelocity = targ.velocity;
185 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
187 // flash when activated
188 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
189 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
190 this.pushltime = time + 0.2;
192 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
195 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
196 if(targ.(jumppadsused[i]) == this)
200 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
201 targ.jumppadcount = targ.jumppadcount + 1;
204 if(IS_REAL_CLIENT(targ))
207 centerprint(targ, this.message);
210 targ.lastteleporttime = time;
213 animdecide_setaction(targ, ANIMACTION_JUMP, true);
216 targ.jumppadcount = 1;
218 // reset tracking of who pushed you into a hazard (for kill credit)
223 if(this.enemy.target)
224 SUB_UseTargets(this.enemy, targ, this);
226 if (targ.flags & FL_PROJECTILE)
228 targ.angles = vectoangles (targ.velocity);
229 targ.com_phys_gravity_factor = 1;
230 switch(targ.move_movetype)
233 set_movetype(targ, MOVETYPE_TOSS);
236 case MOVETYPE_BOUNCEMISSILE:
237 set_movetype(targ, MOVETYPE_BOUNCE);
241 UpdateCSQCProjectile(targ);
248 void trigger_push_touch(entity this, entity toucher)
250 if (this.active == ACTIVE_NOT)
254 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
257 EXACTTRIGGER_TOUCH(this, toucher);
259 noref bool success = jumppad_push(this, toucher);
262 if (success && (this.spawnflags & PUSH_ONCE))
264 settouch(this, func_null);
265 setthink(this, SUB_Remove);
266 this.nextthink = time;
272 void trigger_push_link(entity this);
273 void trigger_push_updatelink(entity this);
274 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
276 setorigin(tracetest_ent, org);
277 tracetoss(tracetest_ent, tracetest_ent);
283 // since tracetoss starting from jumppad's origin often fails when target
284 // is very close to real destination, start it directly from target's
286 tracetest_ent.velocity.z = 0;
287 setorigin(tracetest_ent, targ.origin + stepheightvec);
288 tracetoss(tracetest_ent, tracetest_ent);
291 setorigin(tracetest_ent, targ.origin + stepheightvec / 2);
292 tracetoss(tracetest_ent, tracetest_ent);
295 setorigin(tracetest_ent, targ.origin);
296 tracetoss(tracetest_ent, tracetest_ent);
302 tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
307 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
308 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
309 bool trigger_push_test(entity this, entity item)
311 // first calculate a typical start point for the jump
312 vector org = (this.absmin + this.absmax) * 0.5;
313 org.z = this.absmax.z - PL_MIN_CONST.z;
319 vector vel = '0 0 0';
321 for(entity t = NULL; (t = find(t, targetname, this.target)); )
325 if(t.move_movetype != MOVETYPE_NONE)
329 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
330 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
331 e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
337 bool r = (trace_ent == item);
343 vector best_target = '0 0 0';
344 vector best_org = '0 0 0';
345 vector best_vel = '0 0 0';
346 bool valid_best_target = false;
347 if (trigger_push_testorigin(e, t, this, org))
349 best_target = trace_endpos;
351 best_vel = e.velocity;
352 valid_best_target = true;
356 vector dist = t.origin - org;
357 if (dist.x || dist.y) // if not perfectly vertical
359 // test trajectory with different starting points, sometimes the trajectory
360 // starting from the jumppad origin can't reach the real destination
361 // and destination waypoint ends up near the jumppad itself
362 vector flatdir = normalize(dist - eZ * dist.z);
363 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
365 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
367 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
368 e.velocity = autocvar_sv_maxspeed * flatdir;
369 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
371 best_target = trace_endpos;
374 valid_best_target = true;
377 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
379 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
380 e.velocity = autocvar_sv_maxspeed * flatdir;
381 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
383 best_target = trace_endpos;
386 valid_best_target = true;
390 if (valid_best_target)
392 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
394 float velxy = vlen(vec2(best_vel));
395 float cost = vlen(vec2(t.origin - best_org)) / velxy;
396 if(velxy < autocvar_sv_maxspeed)
397 velxy = autocvar_sv_maxspeed;
398 cost += vlen(vec2(best_target - t.origin)) / velxy;
399 waypoint_spawnforteleporter(this, best_target, cost, e);
413 objerror (this, "Jumppad with nonexistant target");
419 // exactly one dest - bots love that
420 this.enemy = find(NULL, targetname, this.target);
424 // have to use random selection every single time
432 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
433 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
435 e.velocity = this.movedir;
439 bool r = (trace_ent == item);
443 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
444 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
448 defer(this, 0.1, trigger_push_updatelink);
453 void trigger_push_findtarget(entity this)
455 trigger_push_test(this, NULL);
459 float trigger_push_send(entity this, entity to, float sf)
461 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
463 WriteByte(MSG_ENTITY, this.team);
464 WriteInt24_t(MSG_ENTITY, this.spawnflags);
465 WriteByte(MSG_ENTITY, this.active);
466 WriteCoord(MSG_ENTITY, this.height);
468 WriteCoord(MSG_ENTITY, this.movedir_x);
469 WriteCoord(MSG_ENTITY, this.movedir_y);
470 WriteCoord(MSG_ENTITY, this.movedir_z);
472 trigger_common_write(this, true);
477 void trigger_push_updatelink(entity this)
482 void trigger_push_link(entity this)
484 trigger_link(this, trigger_push_send);
490 * target: target of jump
491 * height: the absolute value is the height of the highest point of the jump
492 * trajectory above the higher one of the player and the target.
493 * the sign indicates whether the highest point is INSIDE (positive)
494 * or OUTSIDE (negative) of the jump trajectory. General rule: use
495 * positive values for targets mounted on the floor, and use negative
496 * values to target a point on the ceiling.
497 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
499 spawnfunc(trigger_push)
505 this.active = ACTIVE_ACTIVE;
506 this.use = trigger_push_use;
507 settouch(this, trigger_push_touch);
512 this.movedir = this.movedir * this.speed * 10;
515 this.noise = "misc/jumppad.wav";
516 precache_sound (this.noise);
518 trigger_push_link(this); // link it now
520 IL_PUSH(g_jumppads, this);
522 // this must be called to spawn the teleport waypoints for bots
523 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
527 bool target_push_send(entity this, entity to, float sf)
529 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
531 WriteByte(MSG_ENTITY, this.cnt);
532 WriteString(MSG_ENTITY, this.targetname);
533 WriteCoord(MSG_ENTITY, this.origin_x);
534 WriteCoord(MSG_ENTITY, this.origin_y);
535 WriteCoord(MSG_ENTITY, this.origin_z);
537 WriteAngle(MSG_ENTITY, this.angles_x);
538 WriteAngle(MSG_ENTITY, this.angles_y);
539 WriteAngle(MSG_ENTITY, this.angles_z);
544 void target_push_use(entity this, entity actor, entity trigger)
546 if(trigger.classname == "trigger_push" || trigger == this)
547 return; // WTF, why is this a thing
549 jumppad_push(this, actor);
552 void target_push_link(entity this)
554 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
555 Net_LinkEntity(this, false, 0, target_push_send);
556 //this.SendFlags |= 1; // update
559 void target_push_init(entity this)
561 this.mangle = this.angles;
562 setorigin(this, this.origin);
563 target_push_link(this);
566 void target_push_init2(entity this)
568 if(this.target && this.target != "") // we have an old style pusher!
570 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
571 this.use = target_push_use;
574 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
577 spawnfunc(target_push) { target_push_init2(this); }
578 spawnfunc(info_notnull) { target_push_init(this); }
579 spawnfunc(target_position) { target_push_init(this); }
583 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
585 this.classname = "jumppad";
586 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
587 this.spawnflags = ReadInt24_t();
588 this.active = ReadByte();
589 this.height = ReadCoord();
591 this.movedir_x = ReadCoord();
592 this.movedir_y = ReadCoord();
593 this.movedir_z = ReadCoord();
595 trigger_common_read(this, true);
597 this.entremove = trigger_remove_generic;
598 this.solid = SOLID_TRIGGER;
599 settouch(this, trigger_push_touch);
600 this.move_time = time;
601 defer(this, 0.25, trigger_push_findtarget);
606 void target_push_remove(entity this)
609 //strunzone(this.classname);
610 //this.classname = string_null;
613 strunzone(this.targetname);
614 this.targetname = string_null;
617 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
619 this.classname = "push_target";
620 this.cnt = ReadByte();
621 this.targetname = strzone(ReadString());
622 this.origin_x = ReadCoord();
623 this.origin_y = ReadCoord();
624 this.origin_z = ReadCoord();
626 this.angles_x = ReadAngle();
627 this.angles_y = ReadAngle();
628 this.angles_z = ReadAngle();
632 setorigin(this, this.origin);
634 this.drawmask = MASK_NORMAL;
635 this.entremove = target_push_remove;