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