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