1 // TODO: split target_push and put it in the target folder
4 #include <common/physics/movetypes/movetypes.qh>
6 void trigger_push_use(entity this, entity actor, entity trigger)
10 this.team = actor.team;
16 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
17 REGISTER_NET_LINKED(ENT_CLIENT_TARGET_PUSH)
20 trigger_push_calculatevelocity
23 org - origin of the object which is to be pushed
24 tgt - target entity (can be either a point or a model entity; if it is
25 the latter, its midpoint is used)
26 ht - jump height, measured from the higher one of org and tgt's midpoint
28 Returns: velocity for the jump
29 the global trigger_push_calculatevelocity_flighttime is set to the total
33 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
35 float grav, sdist, zdist, vs, vz, jumpheight;
38 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
40 grav = PHYS_GRAVITY(other);
41 if(PHYS_ENTGRAVITY(other))
42 grav *= PHYS_ENTGRAVITY(other);
44 zdist = torg.z - org.z;
45 sdist = vlen(torg - org - zdist * '0 0 1');
46 sdir = normalize(torg - org - zdist * '0 0 1');
48 // how high do we need to push the player?
49 jumpheight = fabs(ht);
51 jumpheight = jumpheight + zdist;
56 You will not understand the following equations anyway...
57 But here is what I did to get them.
62 z(t) = t * vz - 1/2 grav t^2
68 max(z, ti) = jumpheight
70 From these three equations, you will find the three parameters vs, vz
74 // push him so high...
75 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
77 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
83 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
84 // ALWAYS solvable because jumpheight >= zdist
86 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)
88 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 trigger_push_calculatevelocity_flighttime = solution.y;
103 // jump apex is during the jump
104 // we must take the larger one too
105 trigger_push_calculatevelocity_flighttime = solution.y;
113 // almost straight line type
114 // jump apex is after the jump
115 // we must take the smaller one
116 trigger_push_calculatevelocity_flighttime = solution.x;
121 // jump apex is during the jump
122 // we must take the larger one
123 trigger_push_calculatevelocity_flighttime = solution.y;
126 vs = sdist / trigger_push_calculatevelocity_flighttime;
128 // finally calculate the velocity
129 return sdir * vs + '0 0 1' * vz;
132 void trigger_push_touch(entity this)
134 if (this.active == ACTIVE_NOT)
137 if (!isPushable(other))
141 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, other)))
148 other.velocity = trigger_push_calculatevelocity(other.origin, this.enemy, this.height);
149 other.move_velocity = other.velocity;
151 else if(this.target && this.target != "")
154 RandomSelection_Init();
155 for(e = NULL; (e = find(e, targetname, this.target)); )
158 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
160 RandomSelection_Add(e, 0, string_null, 1, 1);
162 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, this.height);
163 other.move_velocity = other.velocity;
167 other.velocity = this.movedir;
168 other.move_velocity = other.velocity;
172 UNSET_ONGROUND(other);
174 other.move_flags &= ~FL_ONGROUND;
176 if (other.flags & FL_PROJECTILE)
178 other.move_angles = vectoangles (other.move_velocity);
179 switch(other.move_movetype)
182 other.move_movetype = MOVETYPE_TOSS;
185 case MOVETYPE_BOUNCEMISSILE:
186 other.move_movetype = MOVETYPE_BOUNCE;
194 if (IS_PLAYER(other))
196 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
197 other.oldvelocity = other.velocity;
199 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
201 // flash when activated
202 Send_Effect(EFFECT_JUMPPAD, other.origin, other.velocity, 1);
203 _sound (other, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
204 this.pushltime = time + 0.2;
206 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
209 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
210 if(other.(jumppadsused[i]) == this)
214 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = this;
215 other.jumppadcount = other.jumppadcount + 1;
218 if(IS_REAL_CLIENT(other))
221 centerprint(other, this.message);
224 other.lastteleporttime = time;
227 animdecide_setaction(other, ANIMACTION_JUMP, true);
230 other.jumppadcount = true;
232 // reset tracking of who pushed you into a hazard (for kill credit)
234 other.istypefrag = 0;
237 if(this.enemy.target)
238 SUB_UseTargets(this.enemy, other, other); // TODO: do we need other as trigger too?
240 if (other.flags & FL_PROJECTILE)
242 other.angles = vectoangles (other.velocity);
243 switch(other.movetype)
246 other.movetype = MOVETYPE_TOSS;
249 case MOVETYPE_BOUNCEMISSILE:
250 other.movetype = MOVETYPE_BOUNCE;
254 UpdateCSQCProjectile(other);
257 /*if (other.flags & FL_ITEM)
260 other.SendFlags |= ISF_DROP;
263 if (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);
276 void trigger_push_findtarget(entity this)
281 // first calculate a typical start point for the jump
282 org = (this.absmin + this.absmax) * 0.5;
283 org_z = this.absmax.z - STAT(PL_MIN, NULL).z;
288 for(t = NULL; (t = find(t, targetname, this.target)); )
294 setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
295 e.velocity = trigger_push_calculatevelocity(org, t, this.height);
297 if(e.movetype == MOVETYPE_NONE)
298 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
307 objerror (this, "Jumppad with nonexistant target");
313 // exactly one dest - bots love that
314 this.enemy = find(NULL, targetname, this.target);
318 // have to use random selection every single time
327 setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
328 e.velocity = this.movedir;
330 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
334 trigger_push_link(this);
335 defer(this, 0.1, trigger_push_updatelink);
340 float trigger_push_send(entity this, entity to, float sf)
342 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
344 WriteByte(MSG_ENTITY, this.team);
345 WriteInt24_t(MSG_ENTITY, this.spawnflags);
346 WriteByte(MSG_ENTITY, this.active);
347 WriteCoord(MSG_ENTITY, this.height);
349 WriteCoord(MSG_ENTITY, this.movedir_x);
350 WriteCoord(MSG_ENTITY, this.movedir_y);
351 WriteCoord(MSG_ENTITY, this.movedir_z);
353 trigger_common_write(this, true);
358 void trigger_push_updatelink(entity this)
363 void trigger_push_link(entity this)
365 trigger_link(this, trigger_push_send);
371 * target: target of jump
372 * height: the absolute value is the height of the highest point of the jump
373 * trajectory above the higher one of the player and the target.
374 * the sign indicates whether the highest point is INSIDE (positive)
375 * or OUTSIDE (negative) of the jump trajectory. General rule: use
376 * positive values for targets mounted on the floor, and use negative
377 * values to target a point on the ceiling.
378 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
380 spawnfunc(trigger_push)
386 this.active = ACTIVE_ACTIVE;
387 this.use = trigger_push_use;
388 settouch(this, trigger_push_touch);
393 this.movedir = this.movedir * this.speed * 10;
396 this.noise = "misc/jumppad.wav";
397 precache_sound (this.noise);
399 // this must be called to spawn the teleport waypoints for bots
400 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
404 bool target_push_send(entity this, entity to, float sf)
406 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
408 WriteByte(MSG_ENTITY, this.cnt);
409 WriteString(MSG_ENTITY, this.targetname);
410 WriteCoord(MSG_ENTITY, this.origin_x);
411 WriteCoord(MSG_ENTITY, this.origin_y);
412 WriteCoord(MSG_ENTITY, this.origin_z);
414 WriteAngle(MSG_ENTITY, this.angles_x);
415 WriteAngle(MSG_ENTITY, this.angles_y);
416 WriteAngle(MSG_ENTITY, this.angles_z);
421 void target_push_link(entity this)
423 BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
424 Net_LinkEntity(this, false, 0, target_push_send);
425 //this.SendFlags |= 1; // update
428 void target_push_init(entity this)
430 this.mangle = this.angles;
431 setorigin(this, this.origin);
432 target_push_link(this);
435 spawnfunc(target_push) { target_push_init(this); }
436 spawnfunc(info_notnull) { target_push_init(this); }
437 spawnfunc(target_position) { target_push_init(this); }
441 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
443 this.classname = "jumppad";
444 int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
445 this.spawnflags = ReadInt24_t();
446 this.active = ReadByte();
447 this.height = ReadCoord();
449 this.movedir_x = ReadCoord();
450 this.movedir_y = ReadCoord();
451 this.movedir_z = ReadCoord();
453 trigger_common_read(this, true);
455 this.entremove = trigger_remove_generic;
456 this.solid = SOLID_TRIGGER;
457 settouch(this, trigger_push_touch);
458 this.move_time = time;
459 defer(this, 0.25, trigger_push_findtarget);
464 void target_push_remove(entity this)
467 strunzone(this.classname);
468 this.classname = string_null;
471 strunzone(this.targetname);
472 this.targetname = string_null;
475 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
477 this.classname = "push_target";
478 this.cnt = ReadByte();
479 this.targetname = strzone(ReadString());
480 this.origin_x = ReadCoord();
481 this.origin_y = ReadCoord();
482 this.origin_z = ReadCoord();
484 this.angles_x = ReadAngle();
485 this.angles_y = ReadAngle();
486 this.angles_z = ReadAngle();
490 setorigin(this, this.origin);
492 this.drawmask = MASK_NORMAL;
493 this.entremove = target_push_remove;