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