]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/trigger/jumppads.qc
Merge branch 'master' into Juhu/strafehud-fixes
[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_TARGET_PUSH)
18
19 /*
20         trigger_push_calculatevelocity
21
22         Arguments:
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
27           pushed_entity - object that is to be pushed
28
29         Returns: velocity for the jump
30  */
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht, entity pushed_entity)
32 {
33         float grav, sdist, zdist, vs, vz, jumpheight;
34         vector sdir, torg;
35
36         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
37
38         grav = PHYS_GRAVITY(NULL);
39         if(pushed_entity && pushed_entity.gravity)
40                 grav *= pushed_entity.gravity;
41
42         zdist = torg.z - org.z;
43         sdist = vlen(torg - org - zdist * '0 0 1');
44         sdir = normalize(torg - org - zdist * '0 0 1');
45
46         // how high do we need to push the player?
47         jumpheight = fabs(ht);
48         if(zdist > 0)
49                 jumpheight = jumpheight + zdist;
50
51         /*
52                 STOP.
53
54                 You will not understand the following equations anyway...
55                 But here is what I did to get them.
56
57                 I used the functions
58
59                   s(t) = t * vs
60                   z(t) = t * vz - 1/2 grav t^2
61
62                 and solved for:
63
64                   s(ti) = sdist
65                   z(ti) = zdist
66                   max(z, ti) = jumpheight
67
68                 From these three equations, you will find the three parameters vs, vz
69                 and ti.
70          */
71
72         // push them so high...
73         vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
74
75         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
76         if(ht < 0)
77                 if(zdist < 0)
78                         vz = -vz;
79
80         vector solution;
81         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
82         // ALWAYS solvable because jumpheight >= zdist
83         if(!solution.z)
84                 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         if(zdist == 0)
86                 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)
87
88         float flighttime;
89         if(zdist < 0)
90         {
91                 // down-jump
92                 if(ht < 0)
93                 {
94                         // almost straight line type
95                         // jump apex is before the jump
96                         // we must take the larger one
97                         flighttime = solution.y;
98                 }
99                 else
100                 {
101                         // regular jump
102                         // jump apex is during the jump
103                         // we must take the larger one too
104                         flighttime = solution.y;
105                 }
106         }
107         else
108         {
109                 // up-jump
110                 if(ht < 0)
111                 {
112                         // almost straight line type
113                         // jump apex is after the jump
114                         // we must take the smaller one
115                         flighttime = solution.x;
116                 }
117                 else
118                 {
119                         // regular jump
120                         // jump apex is during the jump
121                         // we must take the larger one
122                         flighttime = solution.y;
123                 }
124         }
125         vs = sdist / flighttime;
126
127         // finally calculate the velocity
128         return sdir * vs + '0 0 1' * vz;
129 }
130
131 bool jumppad_push(entity this, entity targ)
132 {
133         if (!isPushable(targ))
134                 return false;
135
136         vector org = targ.origin;
137
138         if(Q3COMPAT_COMMON || this.spawnflags & PUSH_STATIC)
139                 org = (this.absmin + this.absmax) * 0.5;
140
141         if(this.enemy)
142         {
143                 targ.velocity = trigger_push_calculatevelocity(org, this.enemy, this.height, targ);
144         }
145         else if(this.target && this.target != "")
146         {
147                 entity e;
148                 RandomSelection_Init();
149                 for(e = NULL; (e = find(e, targetname, this.target)); )
150                 {
151                         if(e.cnt)
152                                 RandomSelection_AddEnt(e, e.cnt, 1);
153                         else
154                                 RandomSelection_AddEnt(e, 1, 1);
155                 }
156                 targ.velocity = trigger_push_calculatevelocity(org, RandomSelection_chosen_ent, this.height, targ);
157         }
158         else
159         {
160                 targ.velocity = this.movedir;
161         }
162
163         UNSET_ONGROUND(targ);
164
165 #ifdef CSQC
166         if (targ.flags & FL_PROJECTILE)
167         {
168                 targ.angles = vectoangles (targ.velocity);
169                 switch(targ.move_movetype)
170                 {
171                         case MOVETYPE_FLY:
172                                 set_movetype(targ, MOVETYPE_TOSS);
173                                 targ.gravity = 1;
174                                 break;
175                         case MOVETYPE_BOUNCEMISSILE:
176                                 set_movetype(targ, MOVETYPE_BOUNCE);
177                                 targ.gravity = 1;
178                                 break;
179                 }
180         }
181 #endif
182
183 #ifdef SVQC
184         if (IS_PLAYER(targ))
185         {
186                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
187                 targ.oldvelocity = targ.velocity;
188
189                 // prevent sound spam when a player hits the jumppad more than once
190                 // or when a dead player gets stuck in the jumppad for some reason
191                 if(this.pushltime < time && !(IS_DEAD(targ) && targ.velocity == '0 0 0'))
192                 {
193                         // flash when activated
194                         Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
195                         _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
196                         this.pushltime = time + 0.2;
197                 }
198                 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
199                 {
200                         bool found = false;
201                         for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
202                                 if(targ.(jumppadsused[i]) == this)
203                                         found = true;
204                         if(!found)
205                         {
206                                 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
207                                 targ.jumppadcount = targ.jumppadcount + 1;
208                         }
209
210                         if(IS_REAL_CLIENT(targ))
211                         {
212                                 if(this.message)
213                                         centerprint(targ, this.message);
214                         }
215                         else
216                         {
217                                 targ.lastteleporttime = time;
218                                 targ.lastteleport_origin = targ.origin;
219                         }
220
221                         if (!IS_DEAD(targ))
222                                 animdecide_setaction(targ, ANIMACTION_JUMP, true);
223                 }
224                 else
225                         targ.jumppadcount = 1;
226
227                 // reset tracking of who pushed you into a hazard (for kill credit)
228                 targ.pushltime = 0;
229                 targ.istypefrag = 0;
230         }
231
232         if(this.enemy.target)
233                 SUB_UseTargets(this.enemy, targ, this);
234
235         if (targ.flags & FL_PROJECTILE)
236         {
237                 targ.angles = vectoangles (targ.velocity);
238                 targ.com_phys_gravity_factor = 1;
239                 switch(targ.move_movetype)
240                 {
241                         case MOVETYPE_FLY:
242                                 set_movetype(targ, MOVETYPE_TOSS);
243                                 targ.gravity = 1;
244                                 break;
245                         case MOVETYPE_BOUNCEMISSILE:
246                                 set_movetype(targ, MOVETYPE_BOUNCE);
247                                 targ.gravity = 1;
248                                 break;
249                 }
250                 UpdateCSQCProjectile(targ);
251         }
252 #endif
253
254         return true;
255 }
256
257 void trigger_push_touch(entity this, entity toucher)
258 {
259         if (this.active == ACTIVE_NOT)
260                 return;
261
262         if(this.team)
263                 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
264                         return;
265
266         EXACTTRIGGER_TOUCH(this, toucher);
267
268         noref bool success = jumppad_push(this, toucher);
269
270 #ifdef SVQC
271         if (success && (this.spawnflags & PUSH_ONCE))
272         {
273                 settouch(this, func_null);
274                 setthink(this, SUB_Remove);
275                 this.nextthink = time;
276         }
277 #endif
278 }
279
280 #ifdef SVQC
281 void trigger_push_link(entity this);
282 void trigger_push_updatelink(entity this);
283 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
284 {
285         setorigin(tracetest_ent, org);
286         tracetoss(tracetest_ent, tracetest_ent);
287         if(trace_startsolid)
288                 return false;
289
290         if (!jp.height)
291         {
292                 // since tracetoss starting from jumppad's origin often fails when target
293                 // is very close to real destination, start it directly from target's
294                 // origin instead
295                 vector ofs = '0 0 0';
296                 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
297                         ofs = stepheightvec;
298
299                 tracetest_ent.velocity.z = 0;
300                 setorigin(tracetest_ent, targ.origin + ofs);
301                 tracetoss(tracetest_ent, tracetest_ent);
302                 if (trace_startsolid && ofs.z)
303                 {
304                         setorigin(tracetest_ent, targ.origin + ofs / 2);
305                         tracetoss(tracetest_ent, tracetest_ent);
306                         if (trace_startsolid && ofs.z)
307                         {
308                                 setorigin(tracetest_ent, targ.origin);
309                                 tracetoss(tracetest_ent, tracetest_ent);
310                                 if (trace_startsolid)
311                                         return false;
312                         }
313                 }
314         }
315         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
316         return true;
317 }
318
319 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
320 {
321         setorigin(tracetest_ent, org);
322         tracetoss(tracetest_ent, tracetest_ent);
323
324         if(trace_startsolid)
325                 return false;
326         if (trace_ent == item)
327                 return true;
328
329         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
330
331         if (trace_ent == item)
332                 return true;
333
334         return false;
335 }
336 #endif
337
338 #ifdef SVQC
339 vector trigger_push_get_start_point(entity this)
340 {
341         // calculate a typical start point for the jump
342         vector org = (this.absmin + this.absmax) * 0.5;
343         org.z = this.absmax.z - PL_MIN_CONST.z - 7;
344         return org;
345 }
346
347 float trigger_push_get_push_time(entity this, vector endpos)
348 {
349         vector org = trigger_push_get_start_point(this);
350
351         float grav = PHYS_GRAVITY(NULL);
352
353         entity t = this.enemy;
354         if (t)
355         {
356                 entity e = spawn();
357                 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
358                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
359                 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
360                 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
361                 delete(e);
362                 return (v.z + v2.z) / grav;
363         }
364         else if (!(this.target && this.target != ""))
365         {
366                 if (!this.team)
367                 {
368                         vector v = this.movedir;
369
370                         float t = v.z / grav;
371                         float jump_height = 1/2 * grav * (t ** 2);
372                         float remaining_height = org.z + jump_height - endpos.z;
373                         float v2_z = sqrt(2 * grav * remaining_height);
374
375                         return (v.z + v2_z) / grav;
376                 }
377         }
378         return 0;
379 }
380 #endif
381
382 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
383 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
384 bool trigger_push_test(entity this, entity item)
385 {
386 #ifdef SVQC
387         vector org = trigger_push_get_start_point(this);
388 #endif
389
390         if (this.target)
391         {
392                 int n = 0;
393 #ifdef SVQC
394                 vector vel = '0 0 0';
395 #endif
396                 for(entity t = NULL; (t = find(t, targetname, this.target)); )
397                 {
398                         ++n;
399 #ifdef SVQC
400                         if(t.move_movetype != MOVETYPE_NONE)
401                                 continue;
402
403                         // bots can't tell teamed jumppads from normal ones
404                         if (this.team)
405                                 continue;
406
407                         entity e = spawn();
408                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
409                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
410                         e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
411
412                         vel = e.velocity;
413                         vector best_target = '0 0 0';
414                         vector best_org = '0 0 0';
415                         vector best_vel = '0 0 0';
416                         bool valid_best_target = false;
417                         if (item)
418                         {
419                                 if (!trigger_push_testorigin_for_item(e, item, org))
420                                 {
421                                         delete(e);
422                                         return false;
423                                 }
424                         }
425                         else
426                         {
427                                 if (trigger_push_testorigin(e, t, this, org))
428                                 {
429                                         best_target = trace_endpos;
430                                         best_org = org;
431                                         best_vel = e.velocity;
432                                         valid_best_target = true;
433                                 }
434                         }
435
436                         vector new_org;
437                         vector dist = t.origin - org;
438                         if (dist.x || dist.y) // if not perfectly vertical
439                         {
440                                 // test trajectory with different starting points, sometimes the trajectory
441                                 // starting from the jumppad origin can't reach the real destination
442                                 // and destination waypoint ends up near the jumppad itself
443                                 vector flatdir = normalize(dist - eZ * dist.z);
444                                 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
445                                 new_org = org + ofs;
446
447                                 LABEL(new_test)
448                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
449                                 if (item)
450                                 {
451                                         if (!trigger_push_testorigin_for_item(e, item, new_org))
452                                         {
453                                                 delete(e);
454                                                 return false;
455                                         }
456                                 }
457                                 else
458                                 {
459                                         vel = e.velocity;
460                                         if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
461                                                 e.velocity = autocvar_sv_maxspeed * flatdir;
462                                         if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
463                                         {
464                                                 best_target = trace_endpos;
465                                                 best_org = new_org;
466                                                 best_vel = vel;
467                                                 valid_best_target = true;
468                                         }
469                                 }
470                                 if (ofs && new_org != org - ofs)
471                                 {
472                                         new_org = org - ofs;
473                                         goto new_test;
474                                 }
475                         }
476
477                         if (item)
478                         {
479                                 delete(e);
480                                 return true;
481                         }
482
483                         if (valid_best_target)
484                         {
485                                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
486                                 {
487                                         float velxy = vlen(vec2(best_vel));
488                                         float cost = vlen(vec2(t.origin - best_org)) / velxy;
489                                         if(velxy < autocvar_sv_maxspeed)
490                                                 velxy = autocvar_sv_maxspeed;
491                                         cost += vlen(vec2(best_target - t.origin)) / velxy;
492                                         waypoint_spawnforteleporter(this, best_target, cost, e);
493                                 }
494                         }
495                         delete(e);
496 #endif
497                 }
498
499                 if(item)
500                         return false;
501
502                 if(!n)
503                 {
504                         // no dest!
505 #ifdef SVQC
506                         objerror (this, "Jumppad with nonexistant target");
507 #endif
508                         return false;
509                 }
510                 else if(n == 1)
511                 {
512                         // exactly one dest - bots love that
513                         if (!this.team)
514                                 this.enemy = find(NULL, targetname, this.target);
515                         else // bots can't tell teamed jumppads from normal ones
516                                 this.enemy = NULL;
517                 }
518                 else
519                 {
520                         // have to use random selection every single time
521                         this.enemy = NULL;
522                 }
523
524         }
525 #ifdef SVQC
526         else
527         {
528                 if (!this.team)
529                 {
530                         entity e = spawn();
531                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
532                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
533                         setorigin(e, org);
534                         e.velocity = this.movedir;
535                         tracetoss(e, e);
536                         if (item)
537                         {
538                                 bool r = (trace_ent == item);
539                                 delete(e);
540                                 return r;
541                         }
542                         if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
543                                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
544                         delete(e);
545                 }
546                 else if (item)
547                         return false;
548         }
549
550         defer(this, 0.1, trigger_push_updatelink);
551 #endif
552         return true;
553 }
554
555 void trigger_push_findtarget(entity this)
556 {
557         trigger_push_test(this, NULL);
558 }
559
560 #ifdef SVQC
561 float trigger_push_send(entity this, entity to, float sf)
562 {
563         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
564
565         WriteByte(MSG_ENTITY, this.team);
566         WriteInt24_t(MSG_ENTITY, this.spawnflags);
567         WriteByte(MSG_ENTITY, this.active);
568         WriteCoord(MSG_ENTITY, this.height);
569
570         trigger_common_write(this, true);
571
572         return true;
573 }
574
575 void trigger_push_updatelink(entity this)
576 {
577         this.SendFlags |= SF_TRIGGER_INIT;
578 }
579
580 void trigger_push_link(entity this)
581 {
582         trigger_link(this, trigger_push_send);
583 }
584
585 /*
586  * ENTITY PARAMETERS:
587  *
588  *   target:  target of jump
589  *   height:  the absolute value is the height of the highest point of the jump
590  *            trajectory above the higher one of the player and the target.
591  *            the sign indicates whether the highest point is INSIDE (positive)
592  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
593  *            positive values for targets mounted on the floor, and use negative
594  *            values to target a point on the ceiling.
595  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
596  */
597 spawnfunc(trigger_push)
598 {
599         SetMovedir(this);
600
601         trigger_init(this);
602
603         this.active = ACTIVE_ACTIVE;
604         this.use = trigger_push_use;
605         settouch(this, trigger_push_touch);
606
607         // normal push setup
608         if (!this.speed)
609                 this.speed = 1000;
610         this.movedir = this.movedir * this.speed * 10;
611
612         if (!this.noise)
613                 this.noise = "misc/jumppad.wav";
614         precache_sound (this.noise);
615
616         trigger_push_link(this); // link it now
617
618         IL_PUSH(g_jumppads, this);
619
620         // this must be called to spawn the teleport waypoints for bots
621         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
622 }
623
624
625 bool target_push_send(entity this, entity to, float sf)
626 {
627         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
628
629         WriteByte(MSG_ENTITY, this.cnt);
630         WriteString(MSG_ENTITY, this.targetname);
631         WriteVector(MSG_ENTITY, this.origin);
632
633         WriteAngleVector(MSG_ENTITY, this.angles);
634
635         return true;
636 }
637
638 void target_push_use(entity this, entity actor, entity trigger)
639 {
640         if(trigger.classname == "trigger_push" || trigger == this)
641                 return; // WTF, why is this a thing
642
643         jumppad_push(this, actor);
644 }
645
646 void target_push_link(entity this)
647 {
648         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
649         Net_LinkEntity(this, false, 0, target_push_send);
650         //this.SendFlags |= 1; // update
651 }
652
653 void target_push_init(entity this)
654 {
655         this.mangle = this.angles;
656         setorigin(this, this.origin);
657         target_push_link(this);
658 }
659
660 void target_push_init2(entity this)
661 {
662         if(this.target && this.target != "") // we have an old style pusher!
663         {
664                 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
665                 this.use = target_push_use;
666         }
667
668         target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
669 }
670
671 spawnfunc(target_push)
672 {
673         target_push_init2(this);
674 }
675
676 spawnfunc(info_notnull)
677 {
678         target_push_init(this);
679 }
680 spawnfunc(target_position)
681 {
682         target_push_init(this);
683 }
684
685 #elif defined(CSQC)
686
687 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
688 {
689         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
690         this.spawnflags = ReadInt24_t();
691         this.active = ReadByte();
692         this.height = ReadCoord();
693
694         trigger_common_read(this, true);
695
696         this.entremove = trigger_remove_generic;
697         this.solid = SOLID_TRIGGER;
698         settouch(this, trigger_push_touch);
699         this.move_time = time;
700         defer(this, 0.25, trigger_push_findtarget);
701
702         return true;
703 }
704
705 void target_push_remove(entity this)
706 {
707         // strfree(this.classname);
708         strfree(this.targetname);
709 }
710
711 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
712 {
713         this.cnt = ReadByte();
714         this.targetname = strzone(ReadString());
715         this.origin = ReadVector();
716
717         this.angles = ReadAngleVector();
718
719         return = true;
720
721         setorigin(this, this.origin);
722
723         this.drawmask = MASK_NORMAL;
724         this.entremove = target_push_remove;
725 }
726 #endif