]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/jumppads.qc
f80d36097401e6314d916f9dc5ee3e08c92892b8
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / 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 #endif
312
313 /// if (item != NULL) returns true if the item can be reached by using this jumppad, false otherwise
314 /// if (item == NULL) tests jumppad's trajectory and eventually spawns waypoints for it (return value doesn't matter)
315 bool trigger_push_test(entity this, entity item)
316 {
317         // first calculate a typical start point for the jump
318         vector org = (this.absmin + this.absmax) * 0.5;
319         org.z = this.absmax.z - PL_MIN_CONST.z - 10;
320
321         if (this.target)
322         {
323                 int n = 0;
324 #ifdef SVQC
325                 vector vel = '0 0 0';
326 #endif
327                 for(entity t = NULL; (t = find(t, targetname, this.target)); )
328                 {
329                         ++n;
330 #ifdef SVQC
331                         if(t.move_movetype != MOVETYPE_NONE)
332                                 continue;
333
334                         entity e = spawn();
335                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
336                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
337                         e.velocity = trigger_push_calculatevelocity(org, t, this.height, e);
338
339                         if(item)
340                         {
341                                 setorigin(e, org);
342                                 tracetoss(e, e);
343                                 bool r = (trace_ent == item);
344                                 delete(e);
345                                 return r;
346                         }
347
348                         vel = e.velocity;
349                         vector best_target = '0 0 0';
350                         vector best_org = '0 0 0';
351                         vector best_vel = '0 0 0';
352                         bool valid_best_target = false;
353                         if (trigger_push_testorigin(e, t, this, org))
354                         {
355                                 best_target = trace_endpos;
356                                 best_org = org;
357                                 best_vel = e.velocity;
358                                 valid_best_target = true;
359                         }
360
361                         vector new_org;
362                         vector dist = t.origin - org;
363                         if (dist.x || dist.y) // if not perfectly vertical
364                         {
365                                 // test trajectory with different starting points, sometimes the trajectory
366                                 // starting from the jumppad origin can't reach the real destination
367                                 // and destination waypoint ends up near the jumppad itself
368                                 vector flatdir = normalize(dist - eZ * dist.z);
369                                 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
370                                 new_org = org + ofs;
371                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
372                                 vel = e.velocity;
373                                 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
374                                         e.velocity = autocvar_sv_maxspeed * flatdir;
375                                 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
376                                 {
377                                         best_target = trace_endpos;
378                                         best_org = new_org;
379                                         best_vel = vel;
380                                         valid_best_target = true;
381                                 }
382                                 new_org = org - ofs;
383                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height, e);
384                                 vel = e.velocity;
385                                 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
386                                         e.velocity = autocvar_sv_maxspeed * flatdir;
387                                 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
388                                 {
389                                         best_target = trace_endpos;
390                                         best_org = new_org;
391                                         best_vel = vel;
392                                         valid_best_target = true;
393                                 }
394                         }
395
396                         if (valid_best_target)
397                         {
398                                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
399                                 {
400                                         float velxy = vlen(vec2(best_vel));
401                                         float cost = vlen(vec2(t.origin - best_org)) / velxy;
402                                         if(velxy < autocvar_sv_maxspeed)
403                                                 velxy = autocvar_sv_maxspeed;
404                                         cost += vlen(vec2(best_target - t.origin)) / velxy;
405                                         waypoint_spawnforteleporter(this, best_target, cost, e);
406                                 }
407                         }
408                         delete(e);
409 #endif
410                 }
411
412                 if(item)
413                         return false;
414
415                 if(!n)
416                 {
417                         // no dest!
418 #ifdef SVQC
419                         objerror (this, "Jumppad with nonexistant target");
420 #endif
421                         return false;
422                 }
423                 else if(n == 1)
424                 {
425                         // exactly one dest - bots love that
426                         this.enemy = find(NULL, targetname, this.target);
427                 }
428                 else
429                 {
430                         // have to use random selection every single time
431                         this.enemy = NULL;
432                 }
433         }
434 #ifdef SVQC
435         else
436         {
437                 entity e = spawn();
438                 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
439                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
440                 setorigin(e, org);
441                 e.velocity = this.movedir;
442                 tracetoss(e, e);
443                 if(item)
444                 {
445                         bool r = (trace_ent == item);
446                         delete(e);
447                         return r;
448                 }
449                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
450                         waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
451                 delete(e);
452         }
453
454         defer(this, 0.1, trigger_push_updatelink);
455 #endif
456         return true;
457 }
458
459 void trigger_push_findtarget(entity this)
460 {
461         trigger_push_test(this, NULL);
462 }
463
464 #ifdef SVQC
465 float trigger_push_send(entity this, entity to, float sf)
466 {
467         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
468
469         WriteByte(MSG_ENTITY, this.team);
470         WriteInt24_t(MSG_ENTITY, this.spawnflags);
471         WriteByte(MSG_ENTITY, this.active);
472         WriteCoord(MSG_ENTITY, this.height);
473
474         WriteVector(MSG_ENTITY, this.movedir);
475
476         trigger_common_write(this, true);
477
478         return true;
479 }
480
481 void trigger_push_updatelink(entity this)
482 {
483         this.SendFlags |= SF_TRIGGER_INIT;
484 }
485
486 void trigger_push_link(entity this)
487 {
488         trigger_link(this, trigger_push_send);
489 }
490
491 /*
492  * ENTITY PARAMETERS:
493  *
494  *   target:  target of jump
495  *   height:  the absolute value is the height of the highest point of the jump
496  *            trajectory above the higher one of the player and the target.
497  *            the sign indicates whether the highest point is INSIDE (positive)
498  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
499  *            positive values for targets mounted on the floor, and use negative
500  *            values to target a point on the ceiling.
501  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
502  */
503 spawnfunc(trigger_push)
504 {
505         SetMovedir(this);
506
507         trigger_init(this);
508
509         this.active = ACTIVE_ACTIVE;
510         this.use = trigger_push_use;
511         settouch(this, trigger_push_touch);
512
513         // normal push setup
514         if (!this.speed)
515                 this.speed = 1000;
516         this.movedir = this.movedir * this.speed * 10;
517
518         if (!this.noise)
519                 this.noise = "misc/jumppad.wav";
520         precache_sound (this.noise);
521
522         trigger_push_link(this); // link it now
523
524         IL_PUSH(g_jumppads, this);
525
526         // this must be called to spawn the teleport waypoints for bots
527         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
528 }
529
530
531 bool target_push_send(entity this, entity to, float sf)
532 {
533         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
534
535         WriteByte(MSG_ENTITY, this.cnt);
536         WriteString(MSG_ENTITY, this.targetname);
537         WriteVector(MSG_ENTITY, this.origin);
538
539         WriteAngle(MSG_ENTITY, this.angles_x);
540         WriteAngle(MSG_ENTITY, this.angles_y);
541         WriteAngle(MSG_ENTITY, this.angles_z);
542
543         return true;
544 }
545
546 void target_push_use(entity this, entity actor, entity trigger)
547 {
548         if(trigger.classname == "trigger_push" || trigger == this)
549                 return; // WTF, why is this a thing
550
551         jumppad_push(this, actor);
552 }
553
554 void target_push_link(entity this)
555 {
556         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
557         Net_LinkEntity(this, false, 0, target_push_send);
558         //this.SendFlags |= 1; // update
559 }
560
561 void target_push_init(entity this)
562 {
563         this.mangle = this.angles;
564         setorigin(this, this.origin);
565         target_push_link(this);
566 }
567
568 void target_push_init2(entity this)
569 {
570         if(this.target && this.target != "") // we have an old style pusher!
571         {
572                 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
573                 this.use = target_push_use;
574         }
575
576         target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
577 }
578
579 spawnfunc(target_push)
580 {
581         target_push_init2(this);
582 }
583
584 spawnfunc(info_notnull)
585 {
586         target_push_init(this);
587 }
588 spawnfunc(target_position)
589 {
590         target_push_init(this);
591 }
592
593 #elif defined(CSQC)
594
595 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
596 {
597         this.classname = "jumppad";
598         int mytm = ReadByte();
599         if(mytm)
600         {
601                 this.team = mytm - 1;
602         }
603         this.spawnflags = ReadInt24_t();
604         this.active = ReadByte();
605         this.height = ReadCoord();
606
607         this.movedir = ReadVector();
608
609         trigger_common_read(this, true);
610
611         this.entremove = trigger_remove_generic;
612         this.solid = SOLID_TRIGGER;
613         settouch(this, trigger_push_touch);
614         this.move_time = time;
615         defer(this, 0.25, trigger_push_findtarget);
616
617         return true;
618 }
619
620 void target_push_remove(entity this)
621 {
622         // strfree(this.classname);
623         strfree(this.targetname);
624 }
625
626 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
627 {
628         this.classname = "push_target";
629         this.cnt = ReadByte();
630         this.targetname = strzone(ReadString());
631         this.origin = ReadVector();
632
633         this.angles_x = ReadAngle();
634         this.angles_y = ReadAngle();
635         this.angles_z = ReadAngle();
636
637         return = true;
638
639         setorigin(this, this.origin);
640
641         this.drawmask = MASK_NORMAL;
642         this.entremove = target_push_remove;
643 }
644 #endif