1 // TODO: split target_push and put it in the target folder
7 const float PUSH_ONCE = 1;
8 const float PUSH_SILENT = 2;
13 void() SUB_UseTargets;
15 void trigger_push_use()
19 self.team = activator.team;
25 float trigger_push_calculatevelocity_flighttime;
28 trigger_push_calculatevelocity
31 org - origin of the object which is to be pushed
32 tgt - target entity (can be either a point or a model entity; if it is
33 the latter, its midpoint is used)
34 ht - jump height, measured from the higher one of org and tgt's midpoint
36 Returns: velocity for the jump
37 the global trigger_push_calculatevelocity_flighttime is set to the total
41 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
43 float grav, sdist, zdist, vs, vz, jumpheight;
46 torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
49 if(PHYS_ENTGRAVITY(other))
50 grav *= PHYS_ENTGRAVITY(other);
52 zdist = torg_z - org_z;
53 sdist = vlen(torg - org - zdist * '0 0 1');
54 sdir = normalize(torg - org - zdist * '0 0 1');
56 // how high do we need to push the player?
57 jumpheight = fabs(ht);
59 jumpheight = jumpheight + zdist;
64 You will not understand the following equations anyway...
65 But here is what I did to get them.
70 z(t) = t * vz - 1/2 grav t^2
76 max(z, ti) = jumpheight
78 From these three equations, you will find the three parameters vs, vz
82 // push him so high...
83 vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
85 // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
91 solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
92 // ALWAYS solvable because jumpheight >= zdist
94 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)
96 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)
103 // almost straight line type
104 // jump apex is before the jump
105 // we must take the larger one
106 trigger_push_calculatevelocity_flighttime = solution_y;
111 // jump apex is during the jump
112 // we must take the larger one too
113 trigger_push_calculatevelocity_flighttime = solution_y;
121 // almost straight line type
122 // jump apex is after the jump
123 // we must take the smaller one
124 trigger_push_calculatevelocity_flighttime = solution_x;
129 // jump apex is during the jump
130 // we must take the larger one
131 trigger_push_calculatevelocity_flighttime = solution_y;
134 vs = sdist / trigger_push_calculatevelocity_flighttime;
136 // finally calculate the velocity
137 return sdir * vs + '0 0 1' * vz;
140 void trigger_push_touch()
142 if (self.active == ACTIVE_NOT)
146 if (!isPushable(other))
151 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
158 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
163 RandomSelection_Init();
164 for(e = world; (e = find(e, targetname, self.target)); )
167 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
169 RandomSelection_Add(e, 0, string_null, 1, 1);
171 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
175 other.velocity = self.movedir;
178 UNSET_ONGROUND(other);
181 if (IS_PLAYER(other))
183 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
184 other.oldvelocity = other.velocity;
186 if(self.pushltime < time) // prevent "snorring" sound when a player hits the jumppad more than once
188 // flash when activated
189 pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
190 sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
191 self.pushltime = time + 0.2;
194 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
199 for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
200 if(other.(jumppadsused[i]) == self)
204 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
205 other.jumppadcount = other.jumppadcount + 1;
208 if(IS_REAL_CLIENT(other))
211 centerprint(other, self.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(self.enemy.target)
237 if (other.flags & FL_PROJECTILE)
239 other.angles = vectoangles (other.velocity);
240 switch(other.movetype)
243 other.movetype = MOVETYPE_TOSS;
246 case MOVETYPE_BOUNCEMISSILE:
247 other.movetype = MOVETYPE_BOUNCE;
251 UpdateCSQCProjectile(other);
254 if (self.spawnflags & PUSH_ONCE)
256 self.touch = func_null;
257 self.think = SUB_Remove;
258 self.nextthink = time;
264 void trigger_push_link();
265 void trigger_push_updatelink();
267 void trigger_push_findtarget()
272 // first calculate a typical start point for the jump
273 org = (self.absmin + self.absmax) * 0.5;
274 org_z = self.absmax_z - PL_MIN_z;
279 for(t = world; (t = find(t, targetname, self.target)); )
285 setsize(e, PL_MIN, PL_MAX);
286 e.velocity = trigger_push_calculatevelocity(org, t, self.height);
288 if(e.movetype == MOVETYPE_NONE)
289 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
298 objerror ("Jumppad with nonexistant target");
304 // exactly one dest - bots love that
305 self.enemy = find(world, targetname, self.target);
309 // have to use random selection every single time
318 setsize(e, PL_MIN, PL_MAX);
319 e.velocity = self.movedir;
321 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
326 defer(0.1, trigger_push_updatelink);
331 float trigger_push_send(entity to, float sf)
333 WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
334 WriteByte(MSG_ENTITY, sf);
338 WriteString(MSG_ENTITY, self.target);
339 WriteByte(MSG_ENTITY, self.team);
340 WriteInt24_t(MSG_ENTITY, self.spawnflags);
341 WriteByte(MSG_ENTITY, self.active);
342 WriteByte(MSG_ENTITY, self.warpzone_isboxy);
343 WriteByte(MSG_ENTITY, self.height);
344 WriteByte(MSG_ENTITY, self.scale);
345 WriteCoord(MSG_ENTITY, self.origin_x);
346 WriteCoord(MSG_ENTITY, self.origin_y);
347 WriteCoord(MSG_ENTITY, self.origin_z);
349 WriteCoord(MSG_ENTITY, self.mins_x);
350 WriteCoord(MSG_ENTITY, self.mins_y);
351 WriteCoord(MSG_ENTITY, self.mins_z);
352 WriteCoord(MSG_ENTITY, self.maxs_x);
353 WriteCoord(MSG_ENTITY, self.maxs_y);
354 WriteCoord(MSG_ENTITY, self.maxs_z);
356 WriteCoord(MSG_ENTITY, self.movedir_x);
357 WriteCoord(MSG_ENTITY, self.movedir_y);
358 WriteCoord(MSG_ENTITY, self.movedir_z);
360 WriteCoord(MSG_ENTITY, self.angles_x);
361 WriteCoord(MSG_ENTITY, self.angles_y);
362 WriteCoord(MSG_ENTITY, self.angles_z);
367 WriteByte(MSG_ENTITY, self.team);
368 WriteByte(MSG_ENTITY, self.active);
374 void trigger_push_updatelink()
379 void trigger_push_link()
381 Net_LinkEntity(self, FALSE, 0, trigger_push_send);
388 * target: target of jump
389 * height: the absolute value is the height of the highest point of the jump
390 * trajectory above the higher one of the player and the target.
391 * the sign indicates whether the highest point is INSIDE (positive)
392 * or OUTSIDE (negative) of the jump trajectory. General rule: use
393 * positive values for targets mounted on the floor, and use negative
394 * values to target a point on the ceiling.
395 * movedir: if target is not set, this * speed * 10 is the velocity to be reached.
397 void spawnfunc_trigger_push()
403 self.active = ACTIVE_ACTIVE;
404 self.use = trigger_push_use;
405 self.touch = trigger_push_touch;
410 self.movedir = self.movedir * self.speed * 10;
413 self.noise = "misc/jumppad.wav";
414 precache_sound (self.noise);
416 // this must be called to spawn the teleport waypoints for bots
417 InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
421 float target_push_send(entity to, float sf)
423 WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
425 WriteByte(MSG_ENTITY, self.cnt);
426 WriteString(MSG_ENTITY, self.targetname);
427 WriteCoord(MSG_ENTITY, self.origin_x);
428 WriteCoord(MSG_ENTITY, self.origin_y);
429 WriteCoord(MSG_ENTITY, self.origin_z);
434 void target_push_link()
436 Net_LinkEntity(self, FALSE, 0, target_push_send);
437 self.SendFlags |= 1; // update
440 void spawnfunc_target_push() { target_push_link(); }
441 void spawnfunc_info_notnull() { target_push_link(); }
442 void spawnfunc_target_position() { target_push_link(); }
447 void ent_trigger_push()
449 float sf = ReadByte();
453 self.classname = "jumppad";
454 self.target = strzone(ReadString());
455 float mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
456 self.spawnflags = ReadInt24_t();
457 self.active = ReadByte();
458 self.warpzone_isboxy = ReadByte();
459 self.height = ReadByte();
460 self.scale = ReadByte();
461 self.origin_x = ReadCoord();
462 self.origin_y = ReadCoord();
463 self.origin_z = ReadCoord();
464 setorigin(self, self.origin);
465 self.mins_x = ReadCoord();
466 self.mins_y = ReadCoord();
467 self.mins_z = ReadCoord();
468 self.maxs_x = ReadCoord();
469 self.maxs_y = ReadCoord();
470 self.maxs_z = ReadCoord();
471 setsize(self, self.mins, self.maxs);
472 self.movedir_x = ReadCoord();
473 self.movedir_y = ReadCoord();
474 self.movedir_z = ReadCoord();
475 self.angles_x = ReadCoord();
476 self.angles_y = ReadCoord();
477 self.angles_z = ReadCoord();
479 self.solid = SOLID_TRIGGER;
480 self.draw = trigger_draw_generic;
481 self.trigger_touch = trigger_push_touch;
482 self.drawmask = MASK_ENGINE;
483 self.move_time = time;
484 trigger_push_findtarget();
489 self.team = ReadByte();
490 self.active = ReadByte();
494 void ent_target_push()
496 self.classname = "push_target";
497 self.cnt = ReadByte();
498 self.targetname = strzone(ReadString());
499 self.origin_x = ReadCoord();
500 self.origin_y = ReadCoord();
501 self.origin_z = ReadCoord();
502 setorigin(self, self.origin);
504 self.drawmask = MASK_ENGINE;