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