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