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