Rename sv_vq3compat to sv_q3defragcompat to clarify its purpose
[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                 if(this.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
196                 {
197                         // flash when activated
198                         Send_Effect(EFFECT_JUMPPAD, targ.origin, targ.velocity, 1);
199                         _sound (targ, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
200                         this.pushltime = time + 0.2;
201                 }
202                 if(IS_REAL_CLIENT(targ) || IS_BOT_CLIENT(targ))
203                 {
204                         bool found = false;
205                         for(int i = 0; i < targ.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
206                                 if(targ.(jumppadsused[i]) == this)
207                                         found = true;
208                         if(!found)
209                         {
210                                 targ.(jumppadsused[targ.jumppadcount % NUM_JUMPPADSUSED]) = this;
211                                 targ.jumppadcount = targ.jumppadcount + 1;
212                         }
213
214                         if(IS_REAL_CLIENT(targ))
215                         {
216                                 if(this.message)
217                                         centerprint(targ, this.message);
218                         }
219                         else
220                         {
221                                 targ.lastteleporttime = time;
222                                 targ.lastteleport_origin = targ.origin;
223                         }
224
225                         if (!IS_DEAD(targ))
226                                 animdecide_setaction(targ, ANIMACTION_JUMP, true);
227                 }
228                 else
229                         targ.jumppadcount = 1;
230
231                 // reset tracking of who pushed you into a hazard (for kill credit)
232                 targ.pushltime = 0;
233                 targ.istypefrag = 0;
234         }
235
236         if(this.enemy.target)
237                 SUB_UseTargets(this.enemy, targ, this);
238
239         if (targ.flags & FL_PROJECTILE)
240         {
241                 targ.angles = vectoangles (targ.velocity);
242                 targ.com_phys_gravity_factor = 1;
243                 switch(targ.move_movetype)
244                 {
245                         case MOVETYPE_FLY:
246                                 set_movetype(targ, MOVETYPE_TOSS);
247                                 targ.gravity = 1;
248                                 break;
249                         case MOVETYPE_BOUNCEMISSILE:
250                                 set_movetype(targ, MOVETYPE_BOUNCE);
251                                 targ.gravity = 1;
252                                 break;
253                 }
254                 UpdateCSQCProjectile(targ);
255         }
256 #endif
257
258         return true;
259 }
260
261 void trigger_push_touch(entity this, entity toucher)
262 {
263         if (this.active == ACTIVE_NOT)
264                 return;
265
266         if(this.team)
267                 if(((this.spawnflags & INVERT_TEAMS) == 0) == (DIFF_TEAM(this, toucher)))
268                         return;
269
270         EXACTTRIGGER_TOUCH(this, toucher);
271
272         noref bool success = jumppad_push(this, toucher);
273
274 #ifdef SVQC
275         if (success && (this.spawnflags & PUSH_ONCE))
276         {
277                 settouch(this, func_null);
278                 setthink(this, SUB_Remove);
279                 this.nextthink = time;
280         }
281 #endif
282 }
283
284 #ifdef SVQC
285 void trigger_push_link(entity this);
286 void trigger_push_updatelink(entity this);
287 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
288 {
289         setorigin(tracetest_ent, org);
290         tracetoss(tracetest_ent, tracetest_ent);
291         if(trace_startsolid)
292                 return false;
293
294         if (!jp.height)
295         {
296                 // since tracetoss starting from jumppad's origin often fails when target
297                 // is very close to real destination, start it directly from target's
298                 // origin instead
299                 vector ofs = '0 0 0';
300                 if (vdist(vec2(tracetest_ent.velocity), <, autocvar_sv_maxspeed))
301                         ofs = stepheightvec;
302
303                 tracetest_ent.velocity.z = 0;
304                 setorigin(tracetest_ent, targ.origin + ofs);
305                 tracetoss(tracetest_ent, tracetest_ent);
306                 if (trace_startsolid && ofs.z)
307                 {
308                         setorigin(tracetest_ent, targ.origin + ofs / 2);
309                         tracetoss(tracetest_ent, tracetest_ent);
310                         if (trace_startsolid && ofs.z)
311                         {
312                                 setorigin(tracetest_ent, targ.origin);
313                                 tracetoss(tracetest_ent, tracetest_ent);
314                                 if (trace_startsolid)
315                                         return false;
316                         }
317                 }
318         }
319         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
320         return true;
321 }
322
323 bool trigger_push_testorigin_for_item(entity tracetest_ent, entity item, vector org)
324 {
325         setorigin(tracetest_ent, org);
326         tracetoss(tracetest_ent, tracetest_ent);
327
328         if(trace_startsolid)
329                 return false;
330         if (trace_ent == item)
331                 return true;
332
333         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
334
335         if (trace_ent == item)
336                 return true;
337
338         return false;
339 }
340 #endif
341
342 #ifdef SVQC
343 vector trigger_push_get_start_point(entity this)
344 {
345         // calculate a typical start point for the jump
346         vector org = (this.absmin + this.absmax) * 0.5;
347         org.z = this.absmax.z - PL_MIN_CONST.z - 7;
348         return org;
349 }
350
351 float trigger_push_get_push_time(entity this, vector endpos)
352 {
353         vector org = trigger_push_get_start_point(this);
354
355         float grav = PHYS_GRAVITY(NULL);
356
357         entity t = this.enemy;
358         if (t)
359         {
360                 entity e = spawn();
361                 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
362                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
363                 vector v = trigger_push_calculatevelocity(org, t, this.height, e);
364                 vector v2 = trigger_push_calculatevelocity(endpos, t, this.height, e);
365                 delete(e);
366                 return (v.z + v2.z) / grav;
367         }
368         else if (!(this.target && this.target != ""))
369         {
370                 if (!this.team)
371                 {
372                         vector v = this.movedir;
373
374                         float t = v.z / grav;
375                         float jump_height = 1/2 * grav * (t ** 2);
376                         float remaining_height = org.z + jump_height - endpos.z;
377                         float v2_z = sqrt(2 * grav * remaining_height);
378
379                         return (v.z + v2_z) / grav;
380                 }
381         }
382         return 0;
383 }
384 #endif
385
386 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
387 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
388 bool trigger_push_test(entity this, entity item)
389 {
390 #ifdef SVQC
391         vector org = trigger_push_get_start_point(this);
392 #endif
393
394         if (this.target)
395         {
396                 int n = 0;
397 #ifdef SVQC
398                 vector vel = '0 0 0';
399 #endif
400                 for(entity t = NULL; (t = find(t, targetname, this.target)); )
401                 {
402                         ++n;
403 #ifdef SVQC
404                         if(t.move_movetype != MOVETYPE_NONE)
405                                 continue;
406
407                         // bots can't tell teamed jumppads from normal ones
408                         if (this.team)
409                                 continue;
410
411                         entity e = spawn();
412                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
413                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
414                         e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
415
416                         vel = e.velocity;
417                         vector best_target = '0 0 0';
418                         vector best_org = '0 0 0';
419                         vector best_vel = '0 0 0';
420                         bool valid_best_target = false;
421                         if (item)
422                         {
423                                 if (!trigger_push_testorigin_for_item(e, item, org))
424                                 {
425                                         delete(e);
426                                         return false;
427                                 }
428                         }
429                         else
430                         {
431                                 if (trigger_push_testorigin(e, t, this, org))
432                                 {
433                                         best_target = trace_endpos;
434                                         best_org = org;
435                                         best_vel = e.velocity;
436                                         valid_best_target = true;
437                                 }
438                         }
439
440                         vector new_org;
441                         vector dist = t.origin - org;
442                         if (dist.x || dist.y) // if not perfectly vertical
443                         {
444                                 // test trajectory with different starting points, sometimes the trajectory
445                                 // starting from the jumppad origin can't reach the real destination
446                                 // and destination waypoint ends up near the jumppad itself
447                                 vector flatdir = normalize(dist - eZ * dist.z);
448                                 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
449                                 new_org = org + ofs;
450
451                                 LABEL(new_test)
452                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
453                                 if (item)
454                                 {
455                                         if (!trigger_push_testorigin_for_item(e, item, new_org))
456                                         {
457                                                 delete(e);
458                                                 return false;
459                                         }
460                                 }
461                                 else
462                                 {
463                                         vel = e.velocity;
464                                         if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
465                                                 e.velocity = autocvar_sv_maxspeed * flatdir;
466                                         if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
467                                         {
468                                                 best_target = trace_endpos;
469                                                 best_org = new_org;
470                                                 best_vel = vel;
471                                                 valid_best_target = true;
472                                         }
473                                 }
474                                 if (ofs && new_org != org - ofs)
475                                 {
476                                         new_org = org - ofs;
477                                         goto new_test;
478                                 }
479                         }
480
481                         if (item)
482                         {
483                                 delete(e);
484                                 return true;
485                         }
486
487                         if (valid_best_target)
488                         {
489                                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
490                                 {
491                                         float velxy = vlen(vec2(best_vel));
492                                         float cost = vlen(vec2(t.origin - best_org)) / velxy;
493                                         if(velxy < autocvar_sv_maxspeed)
494                                                 velxy = autocvar_sv_maxspeed;
495                                         cost += vlen(vec2(best_target - t.origin)) / velxy;
496                                         waypoint_spawnforteleporter(this, best_target, cost, e);
497                                 }
498                         }
499                         delete(e);
500 #endif
501                 }
502
503                 if(item)
504                         return false;
505
506                 if(!n)
507                 {
508                         // no dest!
509 #ifdef SVQC
510                         objerror (this, "Jumppad with nonexistant target");
511 #endif
512                         return false;
513                 }
514                 else if(n == 1)
515                 {
516                         // exactly one dest - bots love that
517                         if (!this.team)
518                                 this.enemy = find(NULL, targetname, this.target);
519                         else // bots can't tell teamed jumppads from normal ones
520                                 this.enemy = NULL;
521                 }
522                 else
523                 {
524                         // have to use random selection every single time
525                         this.enemy = NULL;
526                 }
527
528         }
529 #ifdef SVQC
530         else
531         {
532                 if (!this.team)
533                 {
534                         entity e = spawn();
535                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
536                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
537                         setorigin(e, org);
538                         e.velocity = this.movedir;
539                         tracetoss(e, e);
540                         if (item)
541                         {
542                                 bool r = (trace_ent == item);
543                                 delete(e);
544                                 return r;
545                         }
546                         if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
547                                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
548                         delete(e);
549                 }
550                 else if (item)
551                         return false;
552         }
553
554         defer(this, 0.1, trigger_push_updatelink);
555 #endif
556         return true;
557 }
558
559 void trigger_push_findtarget(entity this)
560 {
561         trigger_push_test(this, NULL);
562 }
563
564 #ifdef SVQC
565 float trigger_push_send(entity this, entity to, float sf)
566 {
567         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
568
569         WriteByte(MSG_ENTITY, this.team);
570         WriteInt24_t(MSG_ENTITY, this.spawnflags);
571         WriteByte(MSG_ENTITY, this.active);
572         WriteCoord(MSG_ENTITY, this.height);
573
574         WriteVector(MSG_ENTITY, this.movedir);
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         WriteAngle(MSG_ENTITY, this.angles_x);
640         WriteAngle(MSG_ENTITY, this.angles_y);
641         WriteAngle(MSG_ENTITY, this.angles_z);
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         this.classname = "jumppad";
698         int mytm = ReadByte();
699         if(mytm)
700         {
701                 this.team = mytm - 1;
702         }
703         this.spawnflags = ReadInt24_t();
704         this.active = ReadByte();
705         this.height = ReadCoord();
706
707         this.movedir = ReadVector();
708
709         trigger_common_read(this, true);
710
711         this.entremove = trigger_remove_generic;
712         this.solid = SOLID_TRIGGER;
713         settouch(this, trigger_push_touch);
714         this.move_time = time;
715         defer(this, 0.25, trigger_push_findtarget);
716
717         return true;
718 }
719
720 void target_push_remove(entity this)
721 {
722         // strfree(this.classname);
723         strfree(this.targetname);
724 }
725
726 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
727 {
728         this.classname = "push_target";
729         this.cnt = ReadByte();
730         this.targetname = strzone(ReadString());
731         this.origin = ReadVector();
732
733         this.angles_x = ReadAngle();
734         this.angles_y = ReadAngle();
735         this.angles_z = ReadAngle();
736
737         return = true;
738
739         setorigin(this, this.origin);
740
741         this.drawmask = MASK_NORMAL;
742         this.entremove = target_push_remove;
743 }
744 #endif