1 // TODO: split target_push and put it in the target folder
3 #include "../../../server/_all.qh"
5 #include "../../movetypes/movetypes.qh"
7 void trigger_push_use()
11 self.team = activator.team;
18 trigger_push_calculatevelocity
21 org - origin of the object which is to be pushed
22 tgt - target entity (can be either a point or a model entity; if it is
23 the latter, its midpoint is used)
24 ht - jump height, measured from the higher one of org and tgt's midpoint
26 Returns: velocity for the jump
27 the global trigger_push_calculatevelocity_flighttime is set to the total
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
33 float grav, sdist, zdist, vs, vz, jumpheight;
36 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
39 if(PHYS_ENTGRAVITY(other))
40 grav *= PHYS_ENTGRAVITY(other);
42 zdist = torg.z - org.z;
43 sdist = vlen(torg - org - zdist * '0 0 1');
44 sdir = normalize(torg - org - zdist * '0 0 1');
46 // how high do we need to push the player?
47 jumpheight = fabs(ht);
49 jumpheight = jumpheight + zdist;
54 You will not understand the following equations anyway...
55 But here is what I did to get them.
60 z(t) = t * vz - 1/2 grav t^2
66 max(z, ti) = jumpheight
68 From these three equations, you will find the three parameters vs, vz
72 // push him so high...
73 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
75 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
81 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
82 // ALWAYS solvable because jumpheight >= zdist
84 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)
86 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)
93 // almost straight line type
94 // jump apex is before the jump
95 // we must take the larger one
96 trigger_push_calculatevelocity_flighttime = solution.y;
101 // jump apex is during the jump
102 // we must take the larger one too
103 trigger_push_calculatevelocity_flighttime = solution.y;
111 // almost straight line type
112 // jump apex is after the jump
113 // we must take the smaller one
114 trigger_push_calculatevelocity_flighttime = solution.x;
119 // jump apex is during the jump
120 // we must take the larger one
121 trigger_push_calculatevelocity_flighttime = solution.y;
124 vs = sdist / trigger_push_calculatevelocity_flighttime;
126 // finally calculate the velocity
127 return sdir * vs + '0 0 1' * vz;
130 void trigger_push_touch()
132 if (self.active == ACTIVE_NOT)
136 if (!isPushable(other))
141 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
148 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
149 other.move_velocity = other.velocity;
154 RandomSelection_Init();
155 for(e = world; (e = find(e, targetname, self.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, self.height);
163 other.move_velocity = other.velocity;
167 other.velocity = self.movedir;
168 other.move_velocity = other.velocity;
171 UNSET_ONGROUND(other);
173 other.move_flags &= ~FL_ONGROUND;
176 if (IS_PLAYER(other))
178 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
179 other.oldvelocity = other.velocity;
181 if(self.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
183 // flash when activated
184 Send_Effect(EFFECT_JUMPPAD, other.origin, other.velocity, 1);
185 sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
186 self.pushltime = time + 0.2;
188 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
191 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
192 if(other.(jumppadsused[i]) == self)
196 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
197 other.jumppadcount = other.jumppadcount + 1;
200 if(IS_REAL_CLIENT(other))
203 centerprint(other, self.message);
206 other.lastteleporttime = time;
208 if (other.deadflag == DEAD_NO)
209 animdecide_setaction(other, ANIMACTION_JUMP, true);
212 other.jumppadcount = true;
214 // reset tracking of who pushed you into a hazard (for kill credit)
216 other.istypefrag = 0;
219 if(self.enemy.target)
222 WITH(entity, self, self.enemy, SUB_UseTargets());
225 if (other.flags & FL_PROJECTILE)
227 other.angles = vectoangles (other.velocity);
228 switch(other.movetype)
231 other.movetype = MOVETYPE_TOSS;
234 case MOVETYPE_BOUNCEMISSILE:
235 other.movetype = MOVETYPE_BOUNCE;
239 UpdateCSQCProjectile(other);
242 if (self.spawnflags & PUSH_ONCE)
244 self.touch = func_null;
245 self.think = SUB_Remove;
246 self.nextthink = time;
252 void trigger_push_link();
253 void trigger_push_updatelink();
255 void trigger_push_findtarget()
260 // first calculate a typical start point for the jump
261 org = (self.absmin + self.absmax) * 0.5;
262 org_z = self.absmax.z - PL_MIN.z;
267 for(t = world; (t = find(t, targetname, self.target)); )
273 setsize(e, PL_MIN, PL_MAX);
274 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
276 if(e.movetype == MOVETYPE_NONE)
277 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
286 objerror ("Jumppad with nonexistant target");
292 // exactly one dest - bots love that
293 self.enemy = find(world, targetname, self.target);
297 // have to use random selection every single time
306 setsize(e, PL_MIN, PL_MAX);
307 e.velocity = self.movedir;
309 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
314 defer(0.1, trigger_push_updatelink);
319 float trigger_push_send(entity to, float sf)
321 WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
322 WriteByte(MSG_ENTITY, sf);
326 WriteByte(MSG_ENTITY, self.team);
327 WriteInt24_t(MSG_ENTITY, self.spawnflags);
328 WriteByte(MSG_ENTITY, self.active);
329 WriteByte(MSG_ENTITY, self.height);
331 trigger_common_write(true);
336 WriteByte(MSG_ENTITY, self.team);
337 WriteByte(MSG_ENTITY, self.active);
343 void trigger_push_updatelink()
348 void trigger_push_link()
350 //Net_LinkEntity(self, false, 0, trigger_push_send);
357 * target: target of jump
358 * height: the absolute value is the height of the highest point of the jump
359 * trajectory above the higher one of the player and the target.
360 * the sign indicates whether the highest point is INSIDE (positive)
361 * or OUTSIDE (negative) of the jump trajectory. General rule: use
362 * positive values for targets mounted on the floor, and use negative
363 * values to target a point on the ceiling.
364 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
366 void spawnfunc_trigger_push()
372 self.active = ACTIVE_ACTIVE;
373 self.use = trigger_push_use;
374 self.touch = trigger_push_touch;
379 self.movedir = self.movedir * self.speed * 10;
382 self.noise = "misc/jumppad.wav";
383 precache_sound (self.noise);
385 // this must be called to spawn the teleport waypoints for bots
386 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
390 float target_push_send(entity to, float sf)
392 WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
394 WriteByte(MSG_ENTITY, self.cnt);
395 WriteString(MSG_ENTITY, self.targetname);
396 WriteCoord(MSG_ENTITY, self.origin_x);
397 WriteCoord(MSG_ENTITY, self.origin_y);
398 WriteCoord(MSG_ENTITY, self.origin_z);
403 void target_push_link()
405 Net_LinkEntity(self, false, 0, target_push_send);
406 self.SendFlags |= 1; // update
409 void spawnfunc_target_push() { target_push_link(); }
410 void spawnfunc_info_notnull() { target_push_link(); }
411 void spawnfunc_target_position() { target_push_link(); }
417 void ent_trigger_push()
419 float sf = ReadByte();
423 self.classname = "jumppad";
424 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
425 self.spawnflags = ReadInt24_t();
426 self.active = ReadByte();
427 self.height = ReadByte();
429 trigger_common_read(true);
431 self.entremove = trigger_remove_generic;
432 self.solid = SOLID_TRIGGER;
433 self.draw = trigger_draw_generic;
434 self.trigger_touch = trigger_push_touch;
435 self.drawmask = MASK_NORMAL;
436 self.move_time = time;
437 trigger_push_findtarget();
442 self.team = ReadByte();
443 self.active = ReadByte();
447 void target_push_remove()
450 strunzone(self.classname);
451 self.classname = string_null;
454 strunzone(self.targetname);
455 self.targetname = string_null;
458 void ent_target_push()
460 self.classname = "push_target";
461 self.cnt = ReadByte();
462 self.targetname = strzone(ReadString());
463 self.origin_x = ReadCoord();
464 self.origin_y = ReadCoord();
465 self.origin_z = ReadCoord();
466 setorigin(self, self.origin);
468 self.drawmask = MASK_NORMAL;
469 self.entremove = target_push_remove;