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;
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;
41 grav = autocvar_sv_gravity;
43 grav = PHYS_GRAVITY(other);
45 if(PHYS_ENTGRAVITY(other))
46 grav *= PHYS_ENTGRAVITY(other);
48 zdist = torg.z - org.z;
49 sdist = vlen(torg - org - zdist * '0 0 1');
50 sdir = normalize(torg - org - zdist * '0 0 1');
52 // how high do we need to push the player?
53 jumpheight = fabs(ht);
55 jumpheight = jumpheight + zdist;
60 You will not understand the following equations anyway...
61 But here is what I did to get them.
66 z(t) = t * vz - 1/2 grav t^2
72 max(z, ti) = jumpheight
74 From these three equations, you will find the three parameters vs, vz
78 // push him so high...
79 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
81 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
87 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
88 // ALWAYS solvable because jumpheight >= zdist
90 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)
92 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)
99 // almost straight line type
100 // jump apex is before the jump
101 // we must take the larger one
102 trigger_push_calculatevelocity_flighttime = solution.y;
107 // jump apex is during the jump
108 // we must take the larger one too
109 trigger_push_calculatevelocity_flighttime = solution.y;
117 // almost straight line type
118 // jump apex is after the jump
119 // we must take the smaller one
120 trigger_push_calculatevelocity_flighttime = solution.x;
125 // jump apex is during the jump
126 // we must take the larger one
127 trigger_push_calculatevelocity_flighttime = solution.y;
130 vs = sdist / trigger_push_calculatevelocity_flighttime;
132 // finally calculate the velocity
133 return sdir * vs + '0 0 1' * vz;
136 void trigger_push_touch()
138 if (this.active == ACTIVE_NOT)
142 if (!isPushable(other))
147 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, other)))
154 other.velocity = trigger_push_calculatevelocity(other.origin, this.enemy, this.height);
155 other.move_velocity = other.velocity;
160 RandomSelection_Init();
161 for(e = world; (e = find(e, targetname, this.target)); )
164 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
166 RandomSelection_Add(e, 0, string_null, 1, 1);
168 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, this.height);
169 other.move_velocity = other.velocity;
173 other.velocity = this.movedir;
174 other.move_velocity = other.velocity;
178 UNSET_ONGROUND(other);
180 other.move_flags &= ~FL_ONGROUND;
184 if (IS_PLAYER(other))
186 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
187 other.oldvelocity = other.velocity;
189 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
191 // flash when activated
192 Send_Effect(EFFECT_JUMPPAD, other.origin, other.velocity, 1);
193 _sound (other, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
194 this.pushltime = time + 0.2;
196 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
199 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
200 if(other.(jumppadsused[i]) == this)
204 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = this;
205 other.jumppadcount = other.jumppadcount + 1;
208 if(IS_REAL_CLIENT(other))
211 centerprint(other, this.message);
214 other.lastteleporttime = time;
216 if (other.deadflag == DEAD_NO)
217 animdecide_setaction(other, ANIMACTION_JUMP, true);
220 other.jumppadcount = true;
222 // reset tracking of who pushed you into a hazard (for kill credit)
224 other.istypefrag = 0;
227 if(this.enemy.target)
230 WITH(entity, self, this.enemy, SUB_UseTargets());
233 if (other.flags & FL_PROJECTILE)
235 other.angles = vectoangles (other.velocity);
236 switch(other.movetype)
239 other.movetype = MOVETYPE_TOSS;
242 case MOVETYPE_BOUNCEMISSILE:
243 other.movetype = MOVETYPE_BOUNCE;
247 UpdateCSQCProjectile(other);
250 if (this.spawnflags & PUSH_ONCE)
252 this.touch = func_null;
253 this.think = SUB_Remove_self;
254 this.nextthink = time;
260 void trigger_push_link();
261 void trigger_push_updatelink();
263 void trigger_push_findtarget()
268 // first calculate a typical start point for the jump
269 org = (self.absmin + self.absmax) * 0.5;
270 org_z = self.absmax.z - PL_MIN.z;
275 for(t = world; (t = find(t, targetname, self.target)); )
281 setsize(e, PL_MIN, PL_MAX);
282 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
284 if(e.movetype == MOVETYPE_NONE)
285 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
294 objerror ("Jumppad with nonexistant target");
300 // exactly one dest - bots love that
301 self.enemy = find(world, targetname, self.target);
305 // have to use random selection every single time
314 setsize(e, PL_MIN, PL_MAX);
315 e.velocity = self.movedir;
317 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
322 defer(self, 0.1, trigger_push_updatelink);
327 float trigger_push_send(entity this, entity to, float sf)
329 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
330 WriteByte(MSG_ENTITY, sf);
334 WriteByte(MSG_ENTITY, self.team);
335 WriteInt24_t(MSG_ENTITY, self.spawnflags);
336 WriteByte(MSG_ENTITY, self.active);
337 WriteCoord(MSG_ENTITY, self.height);
339 trigger_common_write(true);
344 WriteByte(MSG_ENTITY, self.team);
345 WriteByte(MSG_ENTITY, self.active);
351 void trigger_push_updatelink()
356 void trigger_push_link()
358 BITSET_ASSIGN(self.effects, EF_NODEPTHTEST);
359 Net_LinkEntity(self, false, 0, trigger_push_send);
365 * target: target of jump
366 * height: the absolute value is the height of the highest point of the jump
367 * trajectory above the higher one of the player and the target.
368 * the sign indicates whether the highest point is INSIDE (positive)
369 * or OUTSIDE (negative) of the jump trajectory. General rule: use
370 * positive values for targets mounted on the floor, and use negative
371 * values to target a point on the ceiling.
372 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
374 spawnfunc(trigger_push)
380 self.active = ACTIVE_ACTIVE;
381 self.use = trigger_push_use;
382 self.touch = trigger_push_touch;
387 self.movedir = self.movedir * self.speed * 10;
390 self.noise = "misc/jumppad.wav";
391 precache_sound (self.noise);
393 // this must be called to spawn the teleport waypoints for bots
394 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
398 bool target_push_send(entity this, entity to, float sf)
400 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
402 WriteByte(MSG_ENTITY, self.cnt);
403 WriteString(MSG_ENTITY, self.targetname);
404 WriteCoord(MSG_ENTITY, self.origin_x);
405 WriteCoord(MSG_ENTITY, self.origin_y);
406 WriteCoord(MSG_ENTITY, self.origin_z);
408 WriteAngle(MSG_ENTITY, self.angles_x);
409 WriteAngle(MSG_ENTITY, self.angles_y);
410 WriteAngle(MSG_ENTITY, self.angles_z);
415 void target_push_link()
417 BITSET_ASSIGN(self.effects, EF_NODEPTHTEST);
418 Net_LinkEntity(self, false, 0, target_push_send);
419 //self.SendFlags |= 1; // update
422 spawnfunc(target_push) { target_push_link(); }
423 spawnfunc(info_notnull) { target_push_link(); }
424 spawnfunc(target_position) { make_pure(this); target_push_link(); }
428 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
431 float sf = ReadByte();
435 self.classname = "jumppad";
436 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
437 self.spawnflags = ReadInt24_t();
438 self.active = ReadByte();
439 self.height = ReadCoord();
441 trigger_common_read(true);
443 self.entremove = trigger_remove_generic;
444 self.solid = SOLID_TRIGGER;
445 //self.draw = trigger_draw_generic;
446 self.move_touch = trigger_push_touch;
447 self.drawmask = MASK_NORMAL;
448 self.move_time = time;
449 defer(self, 0.25, trigger_push_findtarget);
454 self.team = ReadByte();
455 self.active = ReadByte();
460 void target_push_remove()
463 strunzone(self.classname);
464 self.classname = string_null;
467 strunzone(self.targetname);
468 self.targetname = string_null;
471 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
474 self.classname = "push_target";
475 self.cnt = ReadByte();
476 self.targetname = strzone(ReadString());
477 self.origin_x = ReadCoord();
478 self.origin_y = ReadCoord();
479 self.origin_z = ReadCoord();
481 self.angles_x = ReadAngle();
482 self.angles_y = ReadAngle();
483 self.angles_z = ReadAngle();
487 setorigin(self, self.origin);
489 self.drawmask = MASK_NORMAL;
490 self.entremove = target_push_remove;