]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/trigger/jumppads.qc
Wrap some statuses with parentesis to avoid possible obscure bugs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / trigger / jumppads.qc
1 #include "jumppads.qh"
2 // TODO: split target_push and put it in the target folder
3 #ifdef SVQC
4 #include <common/physics/movetypes/movetypes.qh>
5
6 void trigger_push_use(entity this, entity actor, entity trigger)
7 {
8         if(teamplay)
9         {
10                 this.team = actor.team;
11                 this.SendFlags |= SF_TRIGGER_UPDATE;
12         }
13 }
14 #endif
15
16 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
17 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH_VELOCITY)
18 REGISTER_NET_LINKED(ENT_CLIENT_TARGET_PUSH)
19
20 /*
21         trigger_push_calculatevelocity
22
23         Arguments:
24           org - origin of the object which is to be pushed
25           tgt - target entity (can be either a point or a model entity; if it is
26                 the latter, its midpoint is used)
27           ht  - jump height, measured from the higher one of org and tgt's midpoint
28           pushed_entity - object that is to be pushed
29
30         Returns: velocity for the jump
31  */
32 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht, entity pushed_entity)
33 {
34         float grav, sdist, zdist, vs, vz, jumpheight;
35         vector sdir, torg;
36
37         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
38
39         grav = PHYS_GRAVITY(NULL);
40         if(pushed_entity && pushed_entity.gravity)
41                 grav *= pushed_entity.gravity;
42
43         // Q3 has frametime-dependent gravity, but its trigger_push velocity calculation doesn't account for that.
44         // This discrepancy can be simulated accurately which ensures that all entities will arrive
45         // where they would in Q3 with gravity 800 at 125fps, even if entity-specific gravity is applied.
46         // This can be hard-coded because we don't support the Q3 world.gravity field at this time.
47         // See physicsCPMA.cfg for maths and test results.
48         if (Q3COMPAT_COMMON)
49                 grav /= 750/800; // exact float, unlike 800/750
50
51         zdist = torg.z - org.z;
52         sdist = vlen(torg - org - zdist * '0 0 1');
53         sdir = normalize(torg - org - zdist * '0 0 1');
54
55         // how high do we need to push the player?
56         jumpheight = fabs(ht);
57         if(zdist > 0)
58                 jumpheight = jumpheight + zdist;
59
60         /*
61                 STOP.
62
63                 You will not understand the following equations anyway...
64                 But here is what I did to get them.
65
66                 I used the functions
67
68                   s(t) = t * vs
69                   z(t) = t * vz - 1/2 grav t^2
70
71                 and solved for:
72
73                   s(ti) = sdist
74                   z(ti) = zdist
75                   max(z, ti) = jumpheight
76
77                 From these three equations, you will find the three parameters vs, vz
78                 and ti.
79          */
80
81         // push them so high...
82         vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
83
84         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
85         if(ht < 0)
86                 if(zdist < 0)
87                         vz = -vz;
88
89         vector solution;
90         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
91         // ALWAYS solvable because jumpheight >= zdist
92         if(!solution.z)
93                 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)
94         if(zdist == 0)
95                 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)
96
97         float flighttime;
98         if(zdist < 0)
99         {
100                 // down-jump
101                 if(ht < 0)
102                 {
103                         // almost straight line type
104                         // jump apex is before the jump
105                         // we must take the larger one
106                         flighttime = solution.y;
107                 }
108                 else
109                 {
110                         // regular jump
111                         // jump apex is during the jump
112                         // we must take the larger one too
113                         flighttime = solution.y;
114                 }
115         }
116         else
117         {
118                 // up-jump
119                 if(ht < 0)
120                 {
121                         // almost straight line type
122                         // jump apex is after the jump
123                         // we must take the smaller one
124                         flighttime = solution.x;
125                 }
126                 else
127                 {
128                         // regular jump
129                         // jump apex is during the jump
130                         // we must take the larger one
131                         flighttime = solution.y;
132                 }
133         }
134         vs = sdist / flighttime;
135
136         // finally calculate the velocity
137         return sdir * vs + '0 0 1' * vz;
138 }
139
140 vector trigger_push_velocity_calculatevelocity(entity this, vector org, entity tgt, float speed, float count, entity pushed_entity, bool already_pushed)
141 {
142         bool is_playerdir_xy = boolean(this.spawnflags & PUSH_VELOCITY_PLAYERDIR_XY);
143         bool is_add_xy = boolean(this.spawnflags & PUSH_VELOCITY_ADD_XY);
144         bool is_playerdir_z = boolean(this.spawnflags & PUSH_VELOCITY_PLAYERDIR_Z);
145         bool is_add_z = boolean(this.spawnflags & PUSH_VELOCITY_ADD_Z);
146         bool is_bidirectional_xy = boolean(this.spawnflags & PUSH_VELOCITY_BIDIRECTIONAL_XY);
147         bool is_bidirectional_z = boolean(this.spawnflags & PUSH_VELOCITY_BIDIRECTIONAL_Z);
148         bool is_clamp_negative_adds = boolean(this.spawnflags & PUSH_VELOCITY_CLAMP_NEGATIVE_ADDS);
149
150         vector sdir = normalize(vec2(pushed_entity.velocity));
151         float zdir = pushed_entity.velocity.z;
152         if(zdir != 0) zdir = copysign(1, zdir);
153
154         vector vs_tgt = '0 0 0';
155         float vz_tgt = 0;
156         if (!is_playerdir_xy || !is_playerdir_z)
157         {
158                 vector vel_tgt = trigger_push_calculatevelocity(org, tgt, 0, pushed_entity);
159                 vs_tgt = vec2(vel_tgt);
160                 vz_tgt = vel_tgt.z;
161
162                 // bidirectional jump pads do not play nicely with xonotic's jump pad targets
163                 if (is_bidirectional_xy)
164                 {
165                         if (normalize(vs_tgt) * sdir < 0)
166                         {
167                                 vs_tgt *= -1;
168                         }
169                 }
170
171                 if (is_bidirectional_z)
172                 {
173                         if (signbit(vz_tgt) != signbit(zdir))
174                         {
175                                 vz_tgt *= -1;
176                         }
177                 }
178         }
179
180         vector vs;
181         if (is_playerdir_xy)
182         {
183                 vs = sdir * speed;
184         }
185         else
186         {
187                 vs = vs_tgt;
188         }
189
190         float vz;
191         if (is_playerdir_z)
192         {
193                 vz = zdir * count;
194         }
195         else
196         {
197                 vz = vz_tgt;
198         }
199
200         if (is_add_xy)
201         {
202                 vector vs_add = vec2(pushed_entity.velocity);
203                 if (already_pushed)
204                 {
205                         vs = vs_add;
206                 }
207                 else
208                 {
209                         vs += vs_add;
210
211                         if (is_clamp_negative_adds)
212                         {
213                                 if ((normalize(vs) * sdir) < 0)
214                                 {
215                                         vs = '0 0 0';
216                                 }
217                         }
218                 }
219         }
220
221         if (is_add_z)
222         {
223                 float vz_add = pushed_entity.velocity.z;
224                 if (already_pushed)
225                 {
226                         vz = vz_add;
227                 }
228                 else
229                 {
230                         vz += vz_add;
231
232                         if (is_clamp_negative_adds)
233                         {
234                                 if (signbit(vz) != signbit(zdir))
235                                 {
236                                         vz = 0;
237                                 }
238                         }
239                 }
240         }
241
242         return vs + '0 0 1' * vz;
243 }
244
245 bool jumppad_push(entity this, entity targ, bool is_velocity_pad)
246 {
247         if (!isPushable(targ))
248                 return false;
249
250         vector org = targ.origin;
251
252         if(Q3COMPAT_COMMON || (this.spawnflags & PUSH_STATIC))
253                 org = (this.absmin + this.absmax) * 0.5;
254
255         bool already_pushed = false;
256         if(is_velocity_pad) // remember velocity jump pads
257         {
258                 if(this == targ.last_pushed || (targ.last_pushed && !STAT(Q3COMPAT, targ))) // if q3compat is active overwrite last stored jump pad, otherwise ignore
259                 {
260                         already_pushed = true;
261                 }
262                 else
263                 {
264                         targ.last_pushed = this; // may be briefly out of sync between client and server if client prediction is toggled
265                 }
266         }
267
268         if(this.enemy)
269         {
270                 if(!is_velocity_pad)
271                 {
272                         targ.velocity = trigger_push_calculatevelocity(org, this.enemy, this.height, targ);
273                 }
274                 else
275                 {
276                         targ.velocity = trigger_push_velocity_calculatevelocity(this, org, this.enemy, this.speed, this.count, targ, already_pushed);
277                 }
278         }
279         else if(this.target && this.target != "")
280         {
281                 entity e;
282                 RandomSelection_Init();
283                 for(e = NULL; (e = find(e, targetname, this.target)); )
284                 {
285                         if(e.cnt)
286                                 RandomSelection_AddEnt(e, e.cnt, 1);
287                         else
288                                 RandomSelection_AddEnt(e, 1, 1);
289                 }
290                 if(!is_velocity_pad)
291                 {
292                         targ.velocity = trigger_push_calculatevelocity(org, RandomSelection_chosen_ent, this.height, targ);
293                 }
294                 else
295                 {
296                         targ.velocity = trigger_push_velocity_calculatevelocity(this, org, RandomSelection_chosen_ent, this.speed, this.count, targ, already_pushed);
297                 }
298         }
299         else
300         {
301                 if(!is_velocity_pad)
302                 {
303                         targ.velocity = this.movedir;
304                 }
305                 else
306                 {
307 #ifdef SVQC
308                         objerror (this, "Jumppad with no target");
309 #endif
310                         return false;
311                 }
312         }
313
314         if(!is_velocity_pad) UNSET_ONGROUND(targ);
315
316 #ifdef CSQC
317         if (targ.flags & FL_PROJECTILE)
318         {
319                 targ.angles = vectoangles (targ.velocity);
320                 switch(targ.move_movetype)
321                 {
322                         case MOVETYPE_FLY:
323                                 set_movetype(targ, MOVETYPE_TOSS);
324                                 targ.gravity = 1;
325                                 break;
326                         case MOVETYPE_BOUNCEMISSILE:
327                                 set_movetype(targ, MOVETYPE_BOUNCE);
328                                 targ.gravity = 1;
329                                 break;
330                 }
331         }
332 #endif
333
334 #ifdef SVQC
335         if (IS_PLAYER(targ))
336         {
337                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
338                 targ.oldvelocity = targ.velocity;
339
340                 // prevent sound spam when a player hits the jumppad more than once
341                 // or when a dead player gets stuck in the jumppad for some reason
342                 if(!already_pushed && this.pushltime < time && !(IS_DEAD(targ) && targ.velocity == '0 0 0'))
343                 {
344                         // flash when activated
345                         Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
346                         _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
347                         this.pushltime = time + 0.2;
348                 }
349                 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
350                 {
351                         bool found = false;
352                         for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
353                                 if(targ.(jumppadsused[i]) == this)
354                                         found = true;
355                         if(!found)
356                         {
357                                 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
358                                 targ.jumppadcount = targ.jumppadcount + 1;
359                         }
360
361                         if(IS_REAL_CLIENT(targ))
362                         {
363                                 if(this.message)
364                                         centerprint(targ, this.message);
365                         }
366                         else
367                         {
368                                 targ.lastteleporttime = time;
369                                 targ.lastteleport_origin = targ.origin;
370                         }
371
372                         if (!IS_DEAD(targ))
373                                 animdecide_setaction(targ, ANIMACTION_JUMP, true);
374                 }
375                 else
376                         targ.jumppadcount = 1;
377
378                 // reset tracking of who pushed you into a hazard (for kill credit)
379                 targ.pushltime = 0;
380                 targ.istypefrag = 0;
381         }
382
383         if(this.enemy.target)
384                 SUB_UseTargets(this.enemy, targ, this);
385
386         if (targ.flags & FL_PROJECTILE)
387         {
388                 targ.angles = vectoangles (targ.velocity);
389                 targ.com_phys_gravity_factor = 1;
390                 switch(targ.move_movetype)
391                 {
392                         case MOVETYPE_FLY:
393                                 set_movetype(targ, MOVETYPE_TOSS);
394                                 targ.gravity = 1;
395                                 break;
396                         case MOVETYPE_BOUNCEMISSILE:
397                                 set_movetype(targ, MOVETYPE_BOUNCE);
398                                 targ.gravity = 1;
399                                 break;
400                 }
401                 UpdateCSQCProjectile(targ);
402         }
403 #endif
404
405         return true;
406 }
407
408 void trigger_push_touch(entity this, entity toucher)
409 {
410         if (this.active == ACTIVE_NOT)
411                 return;
412
413         if(this.team)
414                 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
415                         return;
416
417         EXACTTRIGGER_TOUCH(this, toucher);
418
419         noref bool success = jumppad_push(this, toucher, false);
420
421 #ifdef SVQC
422         if (success && (this.spawnflags & PUSH_ONCE))
423         {
424                 settouch(this, func_null);
425                 setthink(this, SUB_Remove);
426                 this.nextthink = time;
427         }
428 #endif
429 }
430
431 void trigger_push_velocity_touch(entity this, entity toucher)
432 {
433         if (this.active == ACTIVE_NOT)
434                 return;
435
436         if(this.team && DIFF_TEAM(this, toucher))
437                 return;
438
439         EXACTTRIGGER_TOUCH(this, toucher);
440
441         jumppad_push(this, toucher, true);
442 }
443
444 #ifdef SVQC
445 void trigger_push_link(entity this);
446 void trigger_push_updatelink(entity this);
447 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
448 {
449         setorigin(tracetest_ent, org);
450         tracetoss(tracetest_ent, tracetest_ent);
451         if(trace_startsolid)
452                 return false;
453
454         if (!jp.height)
455         {
456                 // since tracetoss starting from jumppad's origin often fails when target
457                 // is very close to real destination, start it directly from target's
458                 // origin instead
459                 vector ofs = '0 0 0';
460                 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
461                         ofs = stepheightvec;
462
463                 tracetest_ent.velocity.z = 0;
464                 setorigin(tracetest_ent, targ.origin + ofs);
465                 tracetoss(tracetest_ent, tracetest_ent);
466                 if (trace_startsolid && ofs.z)
467                 {
468                         setorigin(tracetest_ent, targ.origin + ofs / 2);
469                         tracetoss(tracetest_ent, tracetest_ent);
470                         if (trace_startsolid && ofs.z)
471                         {
472                                 setorigin(tracetest_ent, targ.origin);
473                                 tracetoss(tracetest_ent, tracetest_ent);
474                                 if (trace_startsolid)
475                                         return false;
476                         }
477                 }
478         }
479         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
480         return true;
481 }
482
483 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
484 {
485         setorigin(tracetest_ent, org);
486         tracetoss(tracetest_ent, tracetest_ent);
487
488         if(trace_startsolid)
489                 return false;
490         if (trace_ent == item)
491                 return true;
492
493         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
494
495         if (trace_ent == item)
496                 return true;
497
498         return false;
499 }
500 #endif
501
502 #ifdef SVQC
503 vector trigger_push_get_start_point(entity this)
504 {
505         // calculate a typical start point for the jump
506         vector org = (this.absmin + this.absmax) * 0.5;
507         org.z = this.absmax.z - PL_MIN_CONST.z - 7;
508         return org;
509 }
510
511 float trigger_push_get_push_time(entity this, vector endpos)
512 {
513         vector org = trigger_push_get_start_point(this);
514
515         float grav = PHYS_GRAVITY(NULL);
516
517         entity t = this.enemy;
518         if (t)
519         {
520                 entity e = spawn();
521                 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
522                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
523                 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
524                 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
525                 delete(e);
526                 return (v.z + v2.z) / grav;
527         }
528         else if (!(this.target && this.target != ""))
529         {
530                 if (!this.team)
531                 {
532                         vector v = this.movedir;
533
534                         float t = v.z / grav;
535                         float jump_height = 1/2 * grav * (t ** 2);
536                         float remaining_height = org.z + jump_height - endpos.z;
537                         float v2_z = sqrt(2 * grav * remaining_height);
538
539                         return (v.z + v2_z) / grav;
540                 }
541         }
542         return 0;
543 }
544 #endif
545
546 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
547 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
548 bool trigger_push_test(entity this, entity item)
549 {
550 #ifdef SVQC
551         vector org = trigger_push_get_start_point(this);
552 #endif
553
554         if (this.target)
555         {
556                 int n = 0;
557 #ifdef SVQC
558                 vector vel = '0 0 0';
559 #endif
560                 for(entity t = NULL; (t = find(t, targetname, this.target)); )
561                 {
562                         ++n;
563 #ifdef SVQC
564                         if(t.move_movetype != MOVETYPE_NONE)
565                                 continue;
566
567                         // bots can't tell teamed jumppads from normal ones
568                         if (this.team)
569                                 continue;
570
571                         entity e = spawn();
572                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
573                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
574                         e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
575
576                         vel = e.velocity;
577                         vector best_target = '0 0 0';
578                         vector best_org = '0 0 0';
579                         vector best_vel = '0 0 0';
580                         bool valid_best_target = false;
581                         if (item)
582                         {
583                                 if (!trigger_push_testorigin_for_item(e, item, org))
584                                 {
585                                         delete(e);
586                                         return false;
587                                 }
588                         }
589                         else
590                         {
591                                 // optimization: if horizontal velocity is 0 then target is not good since the trajectory
592                                 // will definitely go back to the jumppad (horizontal velocity of best_vel can't be 0 anyway)
593                                 if ((e.velocity.x != 0 || e.velocity.y != 0)
594                                         && trigger_push_testorigin(e, t, this, org))
595                                 {
596                                         best_target = trace_endpos;
597                                         best_org = org;
598                                         best_vel = e.velocity;
599                                         valid_best_target = true;
600                                 }
601                         }
602
603                         vector new_org;
604                         vector dist = t.origin - org;
605                         if (dist.x || dist.y) // if not perfectly vertical
606                         {
607                                 // test trajectory with different starting points, sometimes the trajectory
608                                 // starting from the jumppad origin can't reach the real destination
609                                 // and destination waypoint ends up near the jumppad itself
610                                 vector flatdir = normalize(dist - eZ * dist.z);
611                                 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
612                                 new_org = org + ofs;
613
614                                 LABEL(new_test)
615                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
616                                 if (item)
617                                 {
618                                         if (!trigger_push_testorigin_for_item(e, item, new_org))
619                                         {
620                                                 delete(e);
621                                                 return false;
622                                         }
623                                 }
624                                 else
625                                 {
626                                         vel = e.velocity;
627                                         if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
628                                                 e.velocity = autocvar_sv_maxspeed * flatdir;
629                                         if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
630                                         {
631                                                 best_target = trace_endpos;
632                                                 best_org = new_org;
633                                                 best_vel = vel;
634                                                 valid_best_target = true;
635                                         }
636                                 }
637                                 if (ofs && new_org != org - ofs)
638                                 {
639                                         new_org = org - ofs;
640                                         goto new_test;
641                                 }
642                         }
643
644                         if (item)
645                         {
646                                 delete(e);
647                                 return true;
648                         }
649
650                         if (valid_best_target)
651                         {
652                                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
653                                 {
654                                         float velxy = vlen(vec2(best_vel));
655                                         float cost = vlen(vec2(t.origin - best_org)) / velxy;
656                                         if(velxy < autocvar_sv_maxspeed)
657                                                 velxy = autocvar_sv_maxspeed;
658                                         cost += vlen(vec2(best_target - t.origin)) / velxy;
659                                         waypoint_spawnforteleporter(this, best_target, cost, e);
660                                 }
661                         }
662                         delete(e);
663 #endif
664                 }
665
666                 if(item)
667                         return false;
668
669                 if(!n)
670                 {
671                         // no dest!
672 #ifdef SVQC
673                         objerror (this, "Jumppad with nonexistant target");
674 #endif
675                         return false;
676                 }
677                 else if(n == 1)
678                 {
679                         // exactly one dest - bots love that
680                         if (!this.team)
681                                 this.enemy = find(NULL, targetname, this.target);
682                         else // bots can't tell teamed jumppads from normal ones
683                                 this.enemy = NULL;
684                 }
685                 else
686                 {
687                         // have to use random selection every single time
688                         this.enemy = NULL;
689                 }
690
691         }
692 #ifdef SVQC
693         else
694         {
695                 if (!this.team)
696                 {
697                         entity e = spawn();
698                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
699                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
700                         setorigin(e, org);
701                         e.velocity = this.movedir;
702                         tracetoss(e, e);
703                         if (item)
704                         {
705                                 bool r = (trace_ent == item);
706                                 delete(e);
707                                 return r;
708                         }
709                         if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
710                                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
711                         delete(e);
712                 }
713                 else if (item)
714                         return false;
715         }
716
717         defer(this, 0.1, trigger_push_updatelink);
718 #endif
719         return true;
720 }
721
722 void trigger_push_findtarget(entity this)
723 {
724         trigger_push_test(this, NULL);
725 }
726
727 #ifdef SVQC
728 float trigger_push_send(entity this, entity to, float sf)
729 {
730         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
731
732         WriteByte(MSG_ENTITY, this.team);
733         WriteInt24_t(MSG_ENTITY, this.spawnflags);
734         WriteByte(MSG_ENTITY, this.active);
735         WriteCoord(MSG_ENTITY, this.height);
736
737         trigger_common_write(this, true);
738
739         return true;
740 }
741
742 float trigger_push_velocity_send(entity this, entity to, float sf)
743 {
744         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH_VELOCITY);
745
746         WriteByte(MSG_ENTITY, this.team);
747         WriteInt24_t(MSG_ENTITY, this.spawnflags);
748         WriteByte(MSG_ENTITY, this.active);
749         WriteCoord(MSG_ENTITY, this.speed);
750         WriteCoord(MSG_ENTITY, this.count);
751
752         trigger_common_write(this, true);
753
754         return true;
755 }
756
757 void trigger_push_updatelink(entity this)
758 {
759         this.SendFlags |= SF_TRIGGER_INIT;
760 }
761
762 void trigger_push_link(entity this)
763 {
764         trigger_link(this, trigger_push_send);
765 }
766
767 void trigger_push_velocity_link(entity this)
768 {
769         trigger_link(this, trigger_push_velocity_send);
770 }
771
772 /*
773  * ENTITY PARAMETERS:
774  *
775  *   target:  target of jump
776  *   height:  the absolute value is the height of the highest point of the jump
777  *            trajectory above the higher one of the player and the target.
778  *            the sign indicates whether the highest point is INSIDE (positive)
779  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
780  *            positive values for targets mounted on the floor, and use negative
781  *            values to target a point on the ceiling.
782  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
783  */
784 spawnfunc(trigger_push)
785 {
786         SetMovedir(this);
787
788         WarpZoneLib_ExactTrigger_Init(this, false);
789         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
790         this.active = ACTIVE_ACTIVE;
791         this.use = trigger_push_use;
792         settouch(this, trigger_push_touch);
793
794         // normal push setup
795         if (!this.speed)
796                 this.speed = 1000;
797         this.movedir = this.movedir * this.speed * 10;
798
799         if (!this.noise)
800                 this.noise = "misc/jumppad.wav";
801         precache_sound (this.noise);
802
803         trigger_push_link(this); // link it now
804
805         IL_PUSH(g_jumppads, this);
806
807         // this must be called to spawn the teleport waypoints for bots
808         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
809 }
810
811 /*
812  * ENTITY PARAMETERS:
813  *
814  *   target:  this points to the target_position to which the player will jump.
815  *   speed:   XY speed for player-directional velocity pads - either sets or adds to the player's horizontal velocity.
816  *   count:   Z speed for player-directional velocity pads - either sets or adds to the player's vertical velocity.
817  */
818 spawnfunc(trigger_push_velocity)
819 {
820         WarpZoneLib_ExactTrigger_Init(this, false);
821         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
822         this.active = ACTIVE_ACTIVE;
823         this.use = trigger_push_use;
824         settouch(this, trigger_push_velocity_touch);
825
826         // normal push setup
827         if (!this.noise)
828                 this.noise = "misc/jumppad.wav";
829         precache_sound (this.noise);
830
831         trigger_push_velocity_link(this); // link it now
832 }
833
834
835 bool target_push_send(entity this, entity to, float sf)
836 {
837         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
838
839         WriteByte(MSG_ENTITY, this.cnt);
840         WriteString(MSG_ENTITY, this.targetname);
841         WriteVector(MSG_ENTITY, this.origin);
842
843         WriteAngleVector(MSG_ENTITY, this.angles);
844
845         return true;
846 }
847
848 void target_push_use(entity this, entity actor, entity trigger)
849 {
850         if(trigger.classname == "trigger_push" || trigger == this)
851                 return; // WTF, why is this a thing
852
853         jumppad_push(this, actor, false);
854 }
855
856 void target_push_link(entity this)
857 {
858         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
859         Net_LinkEntity(this, false, 0, target_push_send);
860         //this.SendFlags |= 1; // update
861 }
862
863 void target_push_init(entity this)
864 {
865         this.mangle = this.angles;
866         setorigin(this, this.origin);
867         target_push_link(this);
868 }
869
870 void target_push_init2(entity this)
871 {
872         if(this.target && this.target != "") // we have an old style pusher!
873         {
874                 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
875                 this.use = target_push_use;
876         }
877
878         target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
879 }
880
881 spawnfunc(target_push)
882 {
883         target_push_init2(this);
884 }
885
886 spawnfunc(info_notnull)
887 {
888         target_push_init(this);
889 }
890 spawnfunc(target_position)
891 {
892         target_push_init(this);
893 }
894
895 #elif defined(CSQC)
896
897 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
898 {
899         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
900         this.spawnflags = ReadInt24_t();
901         this.active = ReadByte();
902         this.height = ReadCoord();
903
904         trigger_common_read(this, true);
905
906         this.entremove = trigger_remove_generic;
907         this.solid = SOLID_TRIGGER;
908         settouch(this, trigger_push_touch);
909         this.move_time = time;
910         defer(this, 0.25, trigger_push_findtarget);
911
912         return true;
913 }
914
915 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH_VELOCITY, bool isnew)
916 {
917         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
918         this.spawnflags = ReadInt24_t();
919         this.active = ReadByte();
920         this.speed = ReadCoord();
921         this.count = ReadCoord();
922
923         trigger_common_read(this, true);
924
925         this.entremove = trigger_remove_generic;
926         this.solid = SOLID_TRIGGER;
927         settouch(this, trigger_push_velocity_touch);
928         this.move_time = time;
929
930         return true;
931 }
932
933 void target_push_remove(entity this)
934 {
935         // strfree(this.classname);
936         strfree(this.targetname);
937 }
938
939 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
940 {
941         this.cnt = ReadByte();
942         this.targetname = strzone(ReadString());
943         this.origin = ReadVector();
944
945         this.angles = ReadAngleVector();
946
947         return = true;
948
949         setorigin(this, this.origin);
950
951         this.drawmask = MASK_NORMAL;
952         this.entremove = target_push_remove;
953 }
954 #endif