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
30 the global trigger_push_calculatevelocity_flighttime is set to the total
34 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
36 float grav, sdist, zdist, vs, vz, jumpheight;
39 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
41 grav = PHYS_GRAVITY(other);
42 if(PHYS_ENTGRAVITY(other))
43 grav *= PHYS_ENTGRAVITY(other);
45 zdist = torg.z - org.z;
46 sdist = vlen(torg - org - zdist * '0 0 1');
47 sdir = normalize(torg - org - zdist * '0 0 1');
49 // how high do we need to push the player?
50 jumpheight = fabs(ht);
52 jumpheight = jumpheight + zdist;
57 You will not understand the following equations anyway...
58 But here is what I did to get them.
63 z(t) = t * vz - 1/2 grav t^2
69 max(z, ti) = jumpheight
71 From these three equations, you will find the three parameters vs, vz
75 // push him so high...
76 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
78 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
84 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
85 // ALWAYS solvable because jumpheight >= zdist
87 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)
89 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)
96 // almost straight line type
97 // jump apex is before the jump
98 // we must take the larger one
99 trigger_push_calculatevelocity_flighttime = solution.y;
104 // jump apex is during the jump
105 // we must take the larger one too
106 trigger_push_calculatevelocity_flighttime = solution.y;
114 // almost straight line type
115 // jump apex is after the jump
116 // we must take the smaller one
117 trigger_push_calculatevelocity_flighttime = solution.x;
122 // jump apex is during the jump
123 // we must take the larger one
124 trigger_push_calculatevelocity_flighttime = solution.y;
127 vs = sdist / trigger_push_calculatevelocity_flighttime;
129 // finally calculate the velocity
130 return sdir * vs + '0 0 1' * vz;
133 bool jumppad_push(entity this, entity targ)
135 if (!isPushable(targ))
140 targ.velocity = trigger_push_calculatevelocity(targ.origin, this.enemy, this.height);
142 else if(this.target && this.target != "")
145 RandomSelection_Init();
146 for(e = NULL; (e = find(e, targetname, this.target)); )
149 RandomSelection_AddEnt(e, e.cnt, 1);
151 RandomSelection_AddEnt(e, 1, 1);
153 targ.velocity = trigger_push_calculatevelocity(targ.origin, RandomSelection_chosen_ent, this.height);
157 targ.velocity = this.movedir;
160 UNSET_ONGROUND(targ);
163 if (targ.flags & FL_PROJECTILE)
165 targ.angles = vectoangles (targ.velocity);
166 switch(targ.move_movetype)
169 set_movetype(targ, MOVETYPE_TOSS);
172 case MOVETYPE_BOUNCEMISSILE:
173 set_movetype(targ, MOVETYPE_BOUNCE);
183 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
184 targ.oldvelocity = targ.velocity;
186 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
188 // flash when activated
189 Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
190 _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
191 this.pushltime = time + 0.2;
193 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
196 for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
197 if(targ.(jumppadsused[i]) == this)
201 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
202 targ.jumppadcount = targ.jumppadcount + 1;
205 if(IS_REAL_CLIENT(targ))
208 centerprint(targ, this.message);
211 targ.lastteleporttime = time;
214 animdecide_setaction(targ, ANIMACTION_JUMP, true);
217 targ.jumppadcount = true;
219 // reset tracking of who pushed you into a hazard (for kill credit)
224 if(this.enemy.target)
225 SUB_UseTargets(this.enemy, targ, this);
227 if (targ.flags & FL_PROJECTILE)
229 targ.angles = vectoangles (targ.velocity);
230 targ.com_phys_gravity_factor = 1;
231 switch(targ.move_movetype)
234 set_movetype(targ, MOVETYPE_TOSS);
237 case MOVETYPE_BOUNCEMISSILE:
238 set_movetype(targ, MOVETYPE_BOUNCE);
242 UpdateCSQCProjectile(targ);
249 void trigger_push_touch(entity this, entity toucher)
251 if (this.active == ACTIVE_NOT)
255 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
258 EXACTTRIGGER_TOUCH(this, toucher);
260 noref bool success = jumppad_push(this, toucher);
263 if (success && (this.spawnflags & PUSH_ONCE))
265 settouch(this, func_null);
266 setthink(this, SUB_Remove);
267 this.nextthink = time;
273 void trigger_push_link(entity this);
274 void trigger_push_updatelink(entity this);
275 bool trigger_push_testorigin(entity e, entity targ, entity jp, vector org)
281 if(e.move_movetype == MOVETYPE_NONE)
283 tracebox(trace_endpos, e.mins, e.maxs, trace_endpos - eZ * 1500, true, jp);
289 void trigger_push_findtarget(entity this)
291 // first calculate a typical start point for the jump
292 vector org = (this.absmin + this.absmax) * 0.5;
293 org.z = this.absmax.z - PL_MIN_CONST.z;
298 for(entity t = NULL; (t = find(t, targetname, this.target)); )
303 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
304 e.velocity = trigger_push_calculatevelocity(org, t, this.height);
305 vector best_target = '0 0 0';
306 vector best_org = '0 0 0';
307 vector best_vel = '0 0 0';
308 bool valid_best_target = false;
309 if (trigger_push_testorigin(e, t, this, org))
311 best_target = trace_endpos;
313 best_vel = e.velocity;
314 valid_best_target = true;
318 vector dist = t.origin - org;
319 if (dist.x || dist.y) // if not perfectly vertical
321 // test trajectory with different starting points, sometimes the trajectory
322 // starting from the jumppad origin can't reach the real destination
323 // and destination waypoint ends up near the jumppad itself
324 vector flatdir = normalize(dist - eZ * dist.z);
325 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
327 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
328 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
330 best_target = trace_endpos;
332 best_vel = e.velocity;
333 valid_best_target = true;
336 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
337 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
339 best_target = trace_endpos;
341 best_vel = e.velocity;
342 valid_best_target = true;
345 if (valid_best_target)
347 float distxy = (vlen((best_target - eZ * best_target.z) - (best_org - eZ * best_org.z)));
348 float velxy = vlen(best_vel - eZ * best_vel.z);
349 waypoint_spawnforteleporter(this, best_target, distxy / velxy);
359 objerror (this, "Jumppad with nonexistant target");
365 // exactly one dest - bots love that
366 this.enemy = find(NULL, targetname, this.target);
370 // have to use random selection every single time
379 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
380 e.velocity = this.movedir;
382 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
386 defer(this, 0.1, trigger_push_updatelink);
391 float trigger_push_send(entity this, entity to, float sf)
393 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
395 WriteByte(MSG_ENTITY, this.team);
396 WriteInt24_t(MSG_ENTITY, this.spawnflags);
397 WriteByte(MSG_ENTITY, this.active);
398 WriteCoord(MSG_ENTITY, this.height);
400 WriteCoord(MSG_ENTITY, this.movedir_x);
401 WriteCoord(MSG_ENTITY, this.movedir_y);
402 WriteCoord(MSG_ENTITY, this.movedir_z);
404 trigger_common_write(this, true);
409 void trigger_push_updatelink(entity this)
414 void trigger_push_link(entity this)
416 trigger_link(this, trigger_push_send);
422 * target: target of jump
423 * height: the absolute value is the height of the highest point of the jump
424 * trajectory above the higher one of the player and the target.
425 * the sign indicates whether the highest point is INSIDE (positive)
426 * or OUTSIDE (negative) of the jump trajectory. General rule: use
427 * positive values for targets mounted on the floor, and use negative
428 * values to target a point on the ceiling.
429 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
431 spawnfunc(trigger_push)
437 this.active = ACTIVE_ACTIVE;
438 this.use = trigger_push_use;
439 settouch(this, trigger_push_touch);
444 this.movedir = this.movedir * this.speed * 10;
447 this.noise = "misc/jumppad.wav";
448 precache_sound (this.noise);
450 trigger_push_link(this); // link it now
452 // this must be called to spawn the teleport waypoints for bots
453 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
457 bool target_push_send(entity this, entity to, float sf)
459 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
461 WriteByte(MSG_ENTITY, this.cnt);
462 WriteString(MSG_ENTITY, this.targetname);
463 WriteCoord(MSG_ENTITY, this.origin_x);
464 WriteCoord(MSG_ENTITY, this.origin_y);
465 WriteCoord(MSG_ENTITY, this.origin_z);
467 WriteAngle(MSG_ENTITY, this.angles_x);
468 WriteAngle(MSG_ENTITY, this.angles_y);
469 WriteAngle(MSG_ENTITY, this.angles_z);
474 void target_push_use(entity this, entity actor, entity trigger)
476 if(trigger.classname == "trigger_push" || trigger == this)
477 return; // WTF, why is this a thing
479 jumppad_push(this, actor);
482 void target_push_link(entity this)
484 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
485 Net_LinkEntity(this, false, 0, target_push_send);
486 //this.SendFlags |= 1; // update
489 void target_push_init(entity this)
491 this.mangle = this.angles;
492 setorigin(this, this.origin);
493 target_push_link(this);
496 void target_push_init2(entity this)
498 if(this.target && this.target != "") // we have an old style pusher!
500 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
501 this.use = target_push_use;
504 target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
507 spawnfunc(target_push) { target_push_init2(this); }
508 spawnfunc(info_notnull) { target_push_init(this); }
509 spawnfunc(target_position) { target_push_init(this); }
513 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
515 this.classname = "jumppad";
516 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
517 this.spawnflags = ReadInt24_t();
518 this.active = ReadByte();
519 this.height = ReadCoord();
521 this.movedir_x = ReadCoord();
522 this.movedir_y = ReadCoord();
523 this.movedir_z = ReadCoord();
525 trigger_common_read(this, true);
527 this.entremove = trigger_remove_generic;
528 this.solid = SOLID_TRIGGER;
529 settouch(this, trigger_push_touch);
530 this.move_time = time;
531 defer(this, 0.25, trigger_push_findtarget);
536 void target_push_remove(entity this)
539 //strunzone(this.classname);
540 //this.classname = string_null;
543 strunzone(this.targetname);
544 this.targetname = string_null;
547 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
549 this.classname = "push_target";
550 this.cnt = ReadByte();
551 this.targetname = strzone(ReadString());
552 this.origin_x = ReadCoord();
553 this.origin_y = ReadCoord();
554 this.origin_z = ReadCoord();
556 this.angles_x = ReadAngle();
557 this.angles_y = ReadAngle();
558 this.angles_z = ReadAngle();
562 setorigin(this, this.origin);
564 this.drawmask = MASK_NORMAL;
565 this.entremove = target_push_remove;