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 if(PHYS_ENTGRAVITY(other))
42 grav *= PHYS_ENTGRAVITY(other);
44 zdist = torg.z - org.z;
45 sdist = vlen(torg - org - zdist * '0 0 1');
46 sdir = normalize(torg - org - zdist * '0 0 1');
48 // how high do we need to push the player?
49 jumpheight = fabs(ht);
51 jumpheight = jumpheight + zdist;
56 You will not understand the following equations anyway...
57 But here is what I did to get them.
62 z(t) = t * vz - 1/2 grav t^2
68 max(z, ti) = jumpheight
70 From these three equations, you will find the three parameters vs, vz
74 // push him so high...
75 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
77 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
83 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
84 // ALWAYS solvable because jumpheight >= zdist
86 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)
88 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)
95 // almost straight line type
96 // jump apex is before the jump
97 // we must take the larger one
98 trigger_push_calculatevelocity_flighttime = solution.y;
103 // jump apex is during the jump
104 // we must take the larger one too
105 trigger_push_calculatevelocity_flighttime = solution.y;
113 // almost straight line type
114 // jump apex is after the jump
115 // we must take the smaller one
116 trigger_push_calculatevelocity_flighttime = solution.x;
121 // jump apex is during the jump
122 // we must take the larger one
123 trigger_push_calculatevelocity_flighttime = solution.y;
126 vs = sdist / trigger_push_calculatevelocity_flighttime;
128 // finally calculate the velocity
129 return sdir * vs + '0 0 1' * vz;
132 void trigger_push_touch()
134 if (this.active == ACTIVE_NOT)
138 if (!isPushable(other))
143 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, other)))
150 other.velocity = trigger_push_calculatevelocity(other.origin, this.enemy, this.height);
151 other.move_velocity = other.velocity;
156 RandomSelection_Init();
157 for(e = world; (e = find(e, targetname, this.target)); )
160 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
162 RandomSelection_Add(e, 0, string_null, 1, 1);
164 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, this.height);
165 other.move_velocity = other.velocity;
169 other.velocity = this.movedir;
170 other.move_velocity = other.velocity;
173 UNSET_ONGROUND(other);
175 other.move_flags &= ~FL_ONGROUND;
178 if (IS_PLAYER(other))
180 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
181 other.oldvelocity = other.velocity;
183 if(this.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
185 // flash when activated
186 Send_Effect(EFFECT_JUMPPAD, other.origin, other.velocity, 1);
187 _sound (other, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
188 this.pushltime = time + 0.2;
190 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
193 for(int i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
194 if(other.(jumppadsused[i]) == this)
198 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = this;
199 other.jumppadcount = other.jumppadcount + 1;
202 if(IS_REAL_CLIENT(other))
205 centerprint(other, this.message);
208 other.lastteleporttime = time;
210 if (other.deadflag == DEAD_NO)
211 animdecide_setaction(other, ANIMACTION_JUMP, true);
214 other.jumppadcount = true;
216 // reset tracking of who pushed you into a hazard (for kill credit)
218 other.istypefrag = 0;
221 if(this.enemy.target)
224 WITH(entity, self, this.enemy, SUB_UseTargets());
227 if (other.flags & FL_PROJECTILE)
229 other.angles = vectoangles (other.velocity);
230 switch(other.movetype)
233 other.movetype = MOVETYPE_TOSS;
236 case MOVETYPE_BOUNCEMISSILE:
237 other.movetype = MOVETYPE_BOUNCE;
241 UpdateCSQCProjectile(other);
244 if (this.spawnflags & PUSH_ONCE)
246 this.touch = func_null;
247 this.think = SUB_Remove_self;
248 this.nextthink = time;
254 void trigger_push_link();
255 void trigger_push_updatelink();
257 void trigger_push_findtarget()
262 // first calculate a typical start point for the jump
263 org = (self.absmin + self.absmax) * 0.5;
264 org_z = self.absmax.z - PL_MIN.z;
269 for(t = world; (t = find(t, targetname, self.target)); )
275 setsize(e, PL_MIN, PL_MAX);
276 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
278 if(e.movetype == MOVETYPE_NONE)
279 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
288 objerror ("Jumppad with nonexistant target");
294 // exactly one dest - bots love that
295 self.enemy = find(world, targetname, self.target);
299 // have to use random selection every single time
308 setsize(e, PL_MIN, PL_MAX);
309 e.velocity = self.movedir;
311 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
316 defer(0.1, trigger_push_updatelink);
321 float trigger_push_send(entity to, float sf)
323 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
324 WriteByte(MSG_ENTITY, sf);
328 WriteByte(MSG_ENTITY, self.team);
329 WriteInt24_t(MSG_ENTITY, self.spawnflags);
330 WriteByte(MSG_ENTITY, self.active);
331 WriteByte(MSG_ENTITY, self.height);
333 trigger_common_write(true);
338 WriteByte(MSG_ENTITY, self.team);
339 WriteByte(MSG_ENTITY, self.active);
345 void trigger_push_updatelink()
350 void trigger_push_link()
352 //Net_LinkEntity(self, false, 0, trigger_push_send);
359 * target: target of jump
360 * height: the absolute value is the height of the highest point of the jump
361 * trajectory above the higher one of the player and the target.
362 * the sign indicates whether the highest point is INSIDE (positive)
363 * or OUTSIDE (negative) of the jump trajectory. General rule: use
364 * positive values for targets mounted on the floor, and use negative
365 * values to target a point on the ceiling.
366 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
368 spawnfunc(trigger_push)
374 self.active = ACTIVE_ACTIVE;
375 self.use = trigger_push_use;
376 self.touch = trigger_push_touch;
381 self.movedir = self.movedir * self.speed * 10;
384 self.noise = "misc/jumppad.wav";
385 precache_sound (self.noise);
387 // this must be called to spawn the teleport waypoints for bots
388 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
392 bool target_push_send(entity this, entity to, float sf)
394 WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
396 WriteByte(MSG_ENTITY, self.cnt);
397 WriteString(MSG_ENTITY, self.targetname);
398 WriteCoord(MSG_ENTITY, self.origin_x);
399 WriteCoord(MSG_ENTITY, self.origin_y);
400 WriteCoord(MSG_ENTITY, self.origin_z);
405 void target_push_link()
407 //Net_LinkEntity(self, false, 0, target_push_send);
408 //self.SendFlags |= 1; // update
411 spawnfunc(target_push) { target_push_link(); }
412 spawnfunc(info_notnull) { target_push_link(); }
413 spawnfunc(target_position) { make_pure(this); target_push_link(); }
419 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
421 float sf = ReadByte();
425 self.classname = "jumppad";
426 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
427 self.spawnflags = ReadInt24_t();
428 self.active = ReadByte();
429 self.height = ReadByte();
431 trigger_common_read(true);
433 self.entremove = trigger_remove_generic;
434 self.solid = SOLID_TRIGGER;
435 self.draw = trigger_draw_generic;
436 self.trigger_touch = trigger_push_touch;
437 self.drawmask = MASK_NORMAL;
438 self.move_time = time;
439 trigger_push_findtarget();
444 self.team = ReadByte();
445 self.active = ReadByte();
450 void target_push_remove()
453 strunzone(self.classname);
454 self.classname = string_null;
457 strunzone(self.targetname);
458 self.targetname = string_null;
461 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
463 self.classname = "push_target";
464 self.cnt = ReadByte();
465 self.targetname = strzone(ReadString());
466 self.origin_x = ReadCoord();
467 self.origin_y = ReadCoord();
468 self.origin_z = ReadCoord();
472 setorigin(self, self.origin);
474 self.drawmask = MASK_NORMAL;
475 self.entremove = target_push_remove;