1 // TODO: split target_push and put it in the target folder
5 const float PUSH_ONCE = 1;
6 const float PUSH_SILENT = 2;
11 void() SUB_UseTargets;
13 void trigger_push_use()
17 self.team = activator.team;
23 float trigger_push_calculatevelocity_flighttime;
26 trigger_push_calculatevelocity
29 org - origin of the object which is to be pushed
30 tgt - target entity (can be either a point or a model entity; if it is
31 the latter, its midpoint is used)
32 ht - jump height, measured from the higher one of org and tgt's midpoint
34 Returns: velocity for the jump
35 the global trigger_push_calculatevelocity_flighttime is set to the total
39 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
41 float grav, sdist, zdist, vs, vz, jumpheight;
44 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
47 if(PHYS_ENTGRAVITY(other))
48 grav *= PHYS_ENTGRAVITY(other);
50 zdist = torg_z - org_z;
51 sdist = vlen(torg - org - zdist * '0 0 1');
52 sdir = normalize(torg - org - zdist * '0 0 1');
54 // how high do we need to push the player?
55 jumpheight = fabs(ht);
57 jumpheight = jumpheight + zdist;
62 You will not understand the following equations anyway...
63 But here is what I did to get them.
68 z(t) = t * vz - 1/2 grav t^2
74 max(z, ti) = jumpheight
76 From these three equations, you will find the three parameters vs, vz
80 // push him so high...
81 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
83 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
89 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
90 // ALWAYS solvable because jumpheight >= zdist
92 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)
94 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)
101 // almost straight line type
102 // jump apex is before the jump
103 // we must take the larger one
104 trigger_push_calculatevelocity_flighttime = solution_y;
109 // jump apex is during the jump
110 // we must take the larger one too
111 trigger_push_calculatevelocity_flighttime = solution_y;
119 // almost straight line type
120 // jump apex is after the jump
121 // we must take the smaller one
122 trigger_push_calculatevelocity_flighttime = solution_x;
127 // jump apex is during the jump
128 // we must take the larger one
129 trigger_push_calculatevelocity_flighttime = solution_y;
132 vs = sdist / trigger_push_calculatevelocity_flighttime;
134 // finally calculate the velocity
135 return sdir * vs + '0 0 1' * vz;
138 void trigger_push_touch()
140 if (self.active == ACTIVE_NOT)
144 if (!isPushable(other))
149 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
156 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
161 RandomSelection_Init();
162 for(e = world; (e = find(e, targetname, self.target)); )
165 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
167 RandomSelection_Add(e, 0, string_null, 1, 1);
169 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
173 other.velocity = self.movedir;
176 UNSET_ONGROUND(other);
179 if (IS_PLAYER(other))
181 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
182 other.oldvelocity = other.velocity;
184 if(self.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
186 // flash when activated
187 pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
188 sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
189 self.pushltime = time + 0.2;
192 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
197 for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
198 if(other.(jumppadsused[i]) == self)
202 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
203 other.jumppadcount = other.jumppadcount + 1;
206 if(IS_REAL_CLIENT(other))
209 centerprint(other, self.message);
212 other.lastteleporttime = time;
214 if (other.deadflag == DEAD_NO)
215 animdecide_setaction(other, ANIMACTION_JUMP, TRUE);
218 other.jumppadcount = TRUE;
220 // reset tracking of who pushed you into a hazard (for kill credit)
222 other.istypefrag = 0;
225 if(self.enemy.target)
235 if (other.flags & FL_PROJECTILE)
237 other.angles = vectoangles (other.velocity);
238 switch(other.movetype)
241 other.movetype = MOVETYPE_TOSS;
244 case MOVETYPE_BOUNCEMISSILE:
245 other.movetype = MOVETYPE_BOUNCE;
249 UpdateCSQCProjectile(other);
252 if (self.spawnflags & PUSH_ONCE)
254 self.touch = func_null;
255 self.think = SUB_Remove;
256 self.nextthink = time;
262 void trigger_push_link();
263 void trigger_push_updatelink();
265 void trigger_push_findtarget()
270 // first calculate a typical start point for the jump
271 org = (self.absmin + self.absmax) * 0.5;
272 org_z = self.absmax_z - PL_MIN_z;
277 for(t = world; (t = find(t, targetname, self.target)); )
283 setsize(e, PL_MIN, PL_MAX);
284 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
286 if(e.movetype == MOVETYPE_NONE)
287 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
296 objerror ("Jumppad with nonexistant target");
302 // exactly one dest - bots love that
303 self.enemy = find(world, targetname, self.target);
307 // have to use random selection every single time
316 setsize(e, PL_MIN, PL_MAX);
317 e.velocity = self.movedir;
319 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
324 defer(0.1, trigger_push_updatelink);
329 float trigger_push_send(entity to, float sf)
331 WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
332 WriteByte(MSG_ENTITY, sf);
336 WriteString(MSG_ENTITY, self.target);
337 WriteByte(MSG_ENTITY, self.team);
338 WriteInt24_t(MSG_ENTITY, self.spawnflags);
339 WriteByte(MSG_ENTITY, self.active);
340 WriteByte(MSG_ENTITY, self.warpzone_isboxy);
341 WriteByte(MSG_ENTITY, self.height);
342 WriteByte(MSG_ENTITY, self.scale);
343 WriteCoord(MSG_ENTITY, self.origin_x);
344 WriteCoord(MSG_ENTITY, self.origin_y);
345 WriteCoord(MSG_ENTITY, self.origin_z);
347 WriteCoord(MSG_ENTITY, self.mins_x);
348 WriteCoord(MSG_ENTITY, self.mins_y);
349 WriteCoord(MSG_ENTITY, self.mins_z);
350 WriteCoord(MSG_ENTITY, self.maxs_x);
351 WriteCoord(MSG_ENTITY, self.maxs_y);
352 WriteCoord(MSG_ENTITY, self.maxs_z);
354 WriteCoord(MSG_ENTITY, self.movedir_x);
355 WriteCoord(MSG_ENTITY, self.movedir_y);
356 WriteCoord(MSG_ENTITY, self.movedir_z);
358 WriteCoord(MSG_ENTITY, self.angles_x);
359 WriteCoord(MSG_ENTITY, self.angles_y);
360 WriteCoord(MSG_ENTITY, self.angles_z);
365 WriteByte(MSG_ENTITY, self.team);
366 WriteByte(MSG_ENTITY, self.active);
372 void trigger_push_updatelink()
377 void trigger_push_link()
379 Net_LinkEntity(self, FALSE, 0, trigger_push_send);
386 * target: target of jump
387 * height: the absolute value is the height of the highest point of the jump
388 * trajectory above the higher one of the player and the target.
389 * the sign indicates whether the highest point is INSIDE (positive)
390 * or OUTSIDE (negative) of the jump trajectory. General rule: use
391 * positive values for targets mounted on the floor, and use negative
392 * values to target a point on the ceiling.
393 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
395 void spawnfunc_trigger_push()
401 self.active = ACTIVE_ACTIVE;
402 self.use = trigger_push_use;
403 self.touch = trigger_push_touch;
408 self.movedir = self.movedir * self.speed * 10;
411 self.noise = "misc/jumppad.wav";
412 precache_sound (self.noise);
414 // this must be called to spawn the teleport waypoints for bots
415 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
419 float target_push_send(entity to, float sf)
421 WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
423 WriteByte(MSG_ENTITY, self.cnt);
424 WriteString(MSG_ENTITY, self.targetname);
425 WriteCoord(MSG_ENTITY, self.origin_x);
426 WriteCoord(MSG_ENTITY, self.origin_y);
427 WriteCoord(MSG_ENTITY, self.origin_z);
432 void target_push_link()
434 Net_LinkEntity(self, FALSE, 0, target_push_send);
435 self.SendFlags |= 1; // update
438 void spawnfunc_target_push() { target_push_link(); }
439 void spawnfunc_info_notnull() { target_push_link(); }
440 void spawnfunc_target_position() { target_push_link(); }
445 void ent_trigger_push()
447 float sf = ReadByte();
451 self.classname = "jumppad";
452 self.target = strzone(ReadString());
453 float mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
454 self.spawnflags = ReadInt24_t();
455 self.active = ReadByte();
456 self.warpzone_isboxy = ReadByte();
457 self.height = ReadByte();
458 self.scale = ReadByte();
459 self.origin_x = ReadCoord();
460 self.origin_y = ReadCoord();
461 self.origin_z = ReadCoord();
462 setorigin(self, self.origin);
463 self.mins_x = ReadCoord();
464 self.mins_y = ReadCoord();
465 self.mins_z = ReadCoord();
466 self.maxs_x = ReadCoord();
467 self.maxs_y = ReadCoord();
468 self.maxs_z = ReadCoord();
469 setsize(self, self.mins, self.maxs);
470 self.movedir_x = ReadCoord();
471 self.movedir_y = ReadCoord();
472 self.movedir_z = ReadCoord();
473 self.angles_x = ReadCoord();
474 self.angles_y = ReadCoord();
475 self.angles_z = ReadCoord();
477 self.solid = SOLID_TRIGGER;
478 self.draw = trigger_draw_generic;
479 self.trigger_touch = trigger_push_touch;
480 self.drawmask = MASK_ENGINE;
481 self.move_time = time;
482 trigger_push_findtarget();
487 self.team = ReadByte();
488 self.active = ReadByte();
492 void ent_target_push()
494 self.classname = "push_target";
495 self.cnt = ReadByte();
496 self.targetname = strzone(ReadString());
497 self.origin_x = ReadCoord();
498 self.origin_y = ReadCoord();
499 self.origin_z = ReadCoord();
500 setorigin(self, self.origin);
502 self.drawmask = MASK_ENGINE;