1 // TODO: split target_push and put it in the target folder
5 void trigger_push_use()
9 self.team = activator.team;
16 trigger_push_calculatevelocity
19 org - origin of the object which is to be pushed
20 tgt - target entity (can be either a point or a model entity; if it is
21 the latter, its midpoint is used)
22 ht - jump height, measured from the higher one of org and tgt's midpoint
24 Returns: velocity for the jump
25 the global trigger_push_calculatevelocity_flighttime is set to the total
29 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
31 float grav, sdist, zdist, vs, vz, jumpheight;
34 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
37 if(PHYS_ENTGRAVITY(other))
38 grav *= PHYS_ENTGRAVITY(other);
40 zdist = torg.z - org.z;
41 sdist = vlen(torg - org - zdist * '0 0 1');
42 sdir = normalize(torg - org - zdist * '0 0 1');
44 // how high do we need to push the player?
45 jumpheight = fabs(ht);
47 jumpheight = jumpheight + zdist;
52 You will not understand the following equations anyway...
53 But here is what I did to get them.
58 z(t) = t * vz - 1/2 grav t^2
64 max(z, ti) = jumpheight
66 From these three equations, you will find the three parameters vs, vz
70 // push him so high...
71 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
73 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
79 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
80 // ALWAYS solvable because jumpheight >= zdist
82 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)
84 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)
91 // almost straight line type
92 // jump apex is before the jump
93 // we must take the larger one
94 trigger_push_calculatevelocity_flighttime = solution.y;
99 // jump apex is during the jump
100 // we must take the larger one too
101 trigger_push_calculatevelocity_flighttime = solution.y;
109 // almost straight line type
110 // jump apex is after the jump
111 // we must take the smaller one
112 trigger_push_calculatevelocity_flighttime = solution.x;
117 // jump apex is during the jump
118 // we must take the larger one
119 trigger_push_calculatevelocity_flighttime = solution.y;
122 vs = sdist / trigger_push_calculatevelocity_flighttime;
124 // finally calculate the velocity
125 return sdir * vs + '0 0 1' * vz;
128 void trigger_push_touch()
130 if (self.active == ACTIVE_NOT)
134 if (!isPushable(other))
139 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
146 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
151 RandomSelection_Init();
152 for(e = world; (e = find(e, targetname, self.target)); )
155 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
157 RandomSelection_Add(e, 0, string_null, 1, 1);
159 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
163 other.velocity = self.movedir;
166 UNSET_ONGROUND(other);
169 if (IS_PLAYER(other))
171 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
172 other.oldvelocity = other.velocity;
174 if(self.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
176 // flash when activated
177 pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
178 sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
179 self.pushltime = time + 0.2;
181 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
184 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
185 if(other.(jumppadsused[i]) == self)
189 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
190 other.jumppadcount = other.jumppadcount + 1;
193 if(IS_REAL_CLIENT(other))
196 centerprint(other, self.message);
199 other.lastteleporttime = time;
201 if (other.deadflag == DEAD_NO)
202 animdecide_setaction(other, ANIMACTION_JUMP, true);
205 other.jumppadcount = true;
207 // reset tracking of who pushed you into a hazard (for kill credit)
209 other.istypefrag = 0;
212 if(self.enemy.target)
222 if (other.flags & FL_PROJECTILE)
224 other.angles = vectoangles (other.velocity);
225 switch(other.movetype)
228 other.movetype = MOVETYPE_TOSS;
231 case MOVETYPE_BOUNCEMISSILE:
232 other.movetype = MOVETYPE_BOUNCE;
236 UpdateCSQCProjectile(other);
239 if (self.spawnflags & PUSH_ONCE)
241 self.touch = func_null;
242 self.think = SUB_Remove;
243 self.nextthink = time;
249 void trigger_push_link();
250 void trigger_push_updatelink();
252 void trigger_push_findtarget()
257 // first calculate a typical start point for the jump
258 org = (self.absmin + self.absmax) * 0.5;
259 org_z = self.absmax.z - PL_MIN_z;
264 for(t = world; (t = find(t, targetname, self.target)); )
270 setsize(e, PL_MIN, PL_MAX);
271 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
273 if(e.movetype == MOVETYPE_NONE)
274 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
283 objerror ("Jumppad with nonexistant target");
289 // exactly one dest - bots love that
290 self.enemy = find(world, targetname, self.target);
294 // have to use random selection every single time
303 setsize(e, PL_MIN, PL_MAX);
304 e.velocity = self.movedir;
306 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
311 defer(0.1, trigger_push_updatelink);
316 float trigger_push_send(entity to, float sf)
318 WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
319 WriteByte(MSG_ENTITY, sf);
323 WriteString(MSG_ENTITY, self.target);
324 WriteByte(MSG_ENTITY, self.team);
325 WriteInt24_t(MSG_ENTITY, self.spawnflags);
326 WriteByte(MSG_ENTITY, self.active);
327 WriteByte(MSG_ENTITY, self.warpzone_isboxy);
328 WriteByte(MSG_ENTITY, self.height);
329 WriteByte(MSG_ENTITY, self.scale);
330 WriteCoord(MSG_ENTITY, self.origin_x);
331 WriteCoord(MSG_ENTITY, self.origin_y);
332 WriteCoord(MSG_ENTITY, self.origin_z);
334 WriteCoord(MSG_ENTITY, self.mins_x);
335 WriteCoord(MSG_ENTITY, self.mins_y);
336 WriteCoord(MSG_ENTITY, self.mins_z);
337 WriteCoord(MSG_ENTITY, self.maxs_x);
338 WriteCoord(MSG_ENTITY, self.maxs_y);
339 WriteCoord(MSG_ENTITY, self.maxs_z);
341 WriteCoord(MSG_ENTITY, self.movedir_x);
342 WriteCoord(MSG_ENTITY, self.movedir_y);
343 WriteCoord(MSG_ENTITY, self.movedir_z);
348 WriteByte(MSG_ENTITY, self.team);
349 WriteByte(MSG_ENTITY, self.active);
355 void trigger_push_updatelink()
360 void trigger_push_link()
362 Net_LinkEntity(self, false, 0, trigger_push_send);
369 * target: target of jump
370 * height: the absolute value is the height of the highest point of the jump
371 * trajectory above the higher one of the player and the target.
372 * the sign indicates whether the highest point is INSIDE (positive)
373 * or OUTSIDE (negative) of the jump trajectory. General rule: use
374 * positive values for targets mounted on the floor, and use negative
375 * values to target a point on the ceiling.
376 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
378 void spawnfunc_trigger_push()
384 self.active = ACTIVE_ACTIVE;
385 self.use = trigger_push_use;
386 self.touch = trigger_push_touch;
391 self.movedir = self.movedir * self.speed * 10;
394 self.noise = "misc/jumppad.wav";
395 precache_sound (self.noise);
397 // this must be called to spawn the teleport waypoints for bots
398 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
402 float target_push_send(entity to, float sf)
404 WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
406 WriteByte(MSG_ENTITY, self.cnt);
407 WriteString(MSG_ENTITY, self.targetname);
408 WriteCoord(MSG_ENTITY, self.origin_x);
409 WriteCoord(MSG_ENTITY, self.origin_y);
410 WriteCoord(MSG_ENTITY, self.origin_z);
415 void target_push_link()
417 Net_LinkEntity(self, false, 0, target_push_send);
418 self.SendFlags |= 1; // update
421 void spawnfunc_target_push() { target_push_link(); }
422 void spawnfunc_info_notnull() { target_push_link(); }
423 void spawnfunc_target_position() { target_push_link(); }
428 void trigger_push_remove()
431 strunzone(self.target);
432 self.target = string_null;
434 void ent_trigger_push()
436 float sf = ReadByte();
440 self.classname = "jumppad";
441 self.target = strzone(ReadString());
442 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
443 self.spawnflags = ReadInt24_t();
444 self.active = ReadByte();
445 self.warpzone_isboxy = ReadByte();
446 self.height = ReadByte();
447 self.scale = ReadByte();
449 self.origin_x = ReadCoord();
450 self.origin_y = ReadCoord();
451 self.origin_z = ReadCoord();
452 setorigin(self, self.origin);
454 self.mins_x = ReadCoord();
455 self.mins_y = ReadCoord();
456 self.mins_z = ReadCoord();
457 self.maxs_x = ReadCoord();
458 self.maxs_y = ReadCoord();
459 self.maxs_z = ReadCoord();
460 setsize(self, self.mins, self.maxs);
462 self.movedir_x = ReadCoord();
463 self.movedir_y = ReadCoord();
464 self.movedir_z = ReadCoord();
466 self.entremove = trigger_push_remove;
467 self.solid = SOLID_TRIGGER;
468 self.draw = trigger_draw_generic;
469 self.trigger_touch = trigger_push_touch;
470 self.drawmask = MASK_NORMAL;
471 self.move_time = time;
472 trigger_push_findtarget();
477 self.team = ReadByte();
478 self.active = ReadByte();
482 void target_push_remove()
485 strunzone(self.classname);
486 self.classname = string_null;
489 strunzone(self.targetname);
490 self.targetname = string_null;
493 void ent_target_push()
495 self.classname = "push_target";
496 self.cnt = ReadByte();
497 self.targetname = strzone(ReadString());
498 self.origin_x = ReadCoord();
499 self.origin_y = ReadCoord();
500 self.origin_z = ReadCoord();
501 setorigin(self, self.origin);
503 self.drawmask = MASK_NORMAL;
504 self.entremove = target_push_remove;