1 // TODO: split target_push and put it in the target folder
4 #include "../../movetypes/movetypes.qh"
6 void trigger_push_use()
10 self.team = activator.team;
17 trigger_push_calculatevelocity
20 org - origin of the object which is to be pushed
21 tgt - target entity (can be either a point or a model entity; if it is
22 the latter, its midpoint is used)
23 ht - jump height, measured from the higher one of org and tgt's midpoint
25 Returns: velocity for the jump
26 the global trigger_push_calculatevelocity_flighttime is set to the total
30 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
32 float grav, sdist, zdist, vs, vz, jumpheight;
35 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
38 if(PHYS_ENTGRAVITY(other))
39 grav *= PHYS_ENTGRAVITY(other);
41 zdist = torg.z - org.z;
42 sdist = vlen(torg - org - zdist * '0 0 1');
43 sdir = normalize(torg - org - zdist * '0 0 1');
45 // how high do we need to push the player?
46 jumpheight = fabs(ht);
48 jumpheight = jumpheight + zdist;
53 You will not understand the following equations anyway...
54 But here is what I did to get them.
59 z(t) = t * vz - 1/2 grav t^2
65 max(z, ti) = jumpheight
67 From these three equations, you will find the three parameters vs, vz
71 // push him so high...
72 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
74 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
80 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
81 // ALWAYS solvable because jumpheight >= zdist
83 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)
85 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)
92 // almost straight line type
93 // jump apex is before the jump
94 // we must take the larger one
95 trigger_push_calculatevelocity_flighttime = solution.y;
100 // jump apex is during the jump
101 // we must take the larger one too
102 trigger_push_calculatevelocity_flighttime = solution.y;
110 // almost straight line type
111 // jump apex is after the jump
112 // we must take the smaller one
113 trigger_push_calculatevelocity_flighttime = solution.x;
118 // jump apex is during the jump
119 // we must take the larger one
120 trigger_push_calculatevelocity_flighttime = solution.y;
123 vs = sdist / trigger_push_calculatevelocity_flighttime;
125 // finally calculate the velocity
126 return sdir * vs + '0 0 1' * vz;
129 void trigger_push_touch()
131 if (self.active == ACTIVE_NOT)
135 if (!isPushable(other))
140 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
147 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
148 other.move_velocity = other.velocity;
153 RandomSelection_Init();
154 for(e = world; (e = find(e, targetname, self.target)); )
157 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
159 RandomSelection_Add(e, 0, string_null, 1, 1);
161 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
162 other.move_velocity = other.velocity;
166 other.velocity = self.movedir;
167 other.move_velocity = other.velocity;
170 UNSET_ONGROUND(other);
172 other.move_flags &= ~FL_ONGROUND;
175 if (IS_PLAYER(other))
177 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
178 other.oldvelocity = other.velocity;
180 if(self.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
182 // flash when activated
183 pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
184 sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
185 self.pushltime = time + 0.2;
187 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
190 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
191 if(other.(jumppadsused[i]) == self)
195 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
196 other.jumppadcount = other.jumppadcount + 1;
199 if(IS_REAL_CLIENT(other))
202 centerprint(other, self.message);
205 other.lastteleporttime = time;
207 if (other.deadflag == DEAD_NO)
208 animdecide_setaction(other, ANIMACTION_JUMP, true);
211 other.jumppadcount = true;
213 // reset tracking of who pushed you into a hazard (for kill credit)
215 other.istypefrag = 0;
218 if(self.enemy.target)
228 if (other.flags & FL_PROJECTILE)
230 other.angles = vectoangles (other.velocity);
231 switch(other.movetype)
234 other.movetype = MOVETYPE_TOSS;
237 case MOVETYPE_BOUNCEMISSILE:
238 other.movetype = MOVETYPE_BOUNCE;
242 UpdateCSQCProjectile(other);
245 if (self.spawnflags & PUSH_ONCE)
247 self.touch = func_null;
248 self.think = SUB_Remove;
249 self.nextthink = time;
255 void trigger_push_link();
256 void trigger_push_updatelink();
258 void trigger_push_findtarget()
263 // first calculate a typical start point for the jump
264 org = (self.absmin + self.absmax) * 0.5;
265 org_z = self.absmax.z - PL_MIN_z;
270 for(t = world; (t = find(t, targetname, self.target)); )
276 setsize(e, PL_MIN, PL_MAX);
277 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
279 if(e.movetype == MOVETYPE_NONE)
280 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
289 objerror ("Jumppad with nonexistant target");
295 // exactly one dest - bots love that
296 self.enemy = find(world, targetname, self.target);
300 // have to use random selection every single time
309 setsize(e, PL_MIN, PL_MAX);
310 e.velocity = self.movedir;
312 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
317 defer(0.1, trigger_push_updatelink);
322 float trigger_push_send(entity to, float sf)
324 WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
325 WriteByte(MSG_ENTITY, sf);
329 WriteByte(MSG_ENTITY, self.team);
330 WriteInt24_t(MSG_ENTITY, self.spawnflags);
331 WriteByte(MSG_ENTITY, self.active);
332 WriteByte(MSG_ENTITY, self.height);
334 trigger_common_write(true);
339 WriteByte(MSG_ENTITY, self.team);
340 WriteByte(MSG_ENTITY, self.active);
346 void trigger_push_updatelink()
351 void trigger_push_link()
353 Net_LinkEntity(self, false, 0, trigger_push_send);
360 * target: target of jump
361 * height: the absolute value is the height of the highest point of the jump
362 * trajectory above the higher one of the player and the target.
363 * the sign indicates whether the highest point is INSIDE (positive)
364 * or OUTSIDE (negative) of the jump trajectory. General rule: use
365 * positive values for targets mounted on the floor, and use negative
366 * values to target a point on the ceiling.
367 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
369 void spawnfunc_trigger_push()
375 self.active = ACTIVE_ACTIVE;
376 self.use = trigger_push_use;
377 self.trigger_touch = trigger_push_touch;
378 self.think = trigger_think_generic;
379 self.nextthink = time;
384 self.movedir = self.movedir * self.speed * 10;
387 self.noise = "misc/jumppad.wav";
388 precache_sound (self.noise);
390 // this must be called to spawn the teleport waypoints for bots
391 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
395 float target_push_send(entity to, float sf)
397 WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
399 WriteByte(MSG_ENTITY, self.cnt);
400 WriteString(MSG_ENTITY, self.targetname);
401 WriteCoord(MSG_ENTITY, self.origin_x);
402 WriteCoord(MSG_ENTITY, self.origin_y);
403 WriteCoord(MSG_ENTITY, self.origin_z);
408 void target_push_link()
410 Net_LinkEntity(self, false, 0, target_push_send);
411 self.SendFlags |= 1; // update
414 void spawnfunc_target_push() { target_push_link(); }
415 void spawnfunc_info_notnull() { target_push_link(); }
416 void spawnfunc_target_position() { target_push_link(); }
422 void ent_trigger_push()
424 float sf = ReadByte();
428 self.classname = "jumppad";
429 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
430 self.spawnflags = ReadInt24_t();
431 self.active = ReadByte();
432 self.height = ReadByte();
434 trigger_common_read(true);
436 self.entremove = trigger_remove_generic;
437 self.solid = SOLID_TRIGGER;
438 self.draw = trigger_draw_generic;
439 self.trigger_touch = trigger_push_touch;
440 self.drawmask = MASK_NORMAL;
441 self.move_time = time;
442 trigger_push_findtarget();
447 self.team = ReadByte();
448 self.active = ReadByte();
452 void target_push_remove()
455 strunzone(self.classname);
456 self.classname = string_null;
459 strunzone(self.targetname);
460 self.targetname = string_null;
463 void ent_target_push()
465 self.classname = "push_target";
466 self.cnt = ReadByte();
467 self.targetname = strzone(ReadString());
468 self.origin_x = ReadCoord();
469 self.origin_y = ReadCoord();
470 self.origin_z = ReadCoord();
471 setorigin(self, self.origin);
473 self.drawmask = MASK_NORMAL;
474 self.entremove = target_push_remove;