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