]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/jumppads.qc
Increase tracewalk destination height to include player's height; it fixes bots not...
[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  */
31 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
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(other);
39         if(PHYS_ENTGRAVITY(other))
40                 grav *= PHYS_ENTGRAVITY(other);
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);
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);
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                                 targ.lastteleporttime = time;
210
211                         if (!IS_DEAD(targ))
212                                 animdecide_setaction(targ, ANIMACTION_JUMP, true);
213                 }
214                 else
215                         targ.jumppadcount = true;
216
217                 // reset tracking of who pushed you into a hazard (for kill credit)
218                 targ.pushltime = 0;
219                 targ.istypefrag = 0;
220         }
221
222         if(this.enemy.target)
223                 SUB_UseTargets(this.enemy, targ, this);
224
225         if (targ.flags & FL_PROJECTILE)
226         {
227                 targ.angles = vectoangles (targ.velocity);
228                 targ.com_phys_gravity_factor = 1;
229                 switch(targ.move_movetype)
230                 {
231                         case MOVETYPE_FLY:
232                                 set_movetype(targ, MOVETYPE_TOSS);
233                                 targ.gravity = 1;
234                                 break;
235                         case MOVETYPE_BOUNCEMISSILE:
236                                 set_movetype(targ, MOVETYPE_BOUNCE);
237                                 targ.gravity = 1;
238                                 break;
239                 }
240                 UpdateCSQCProjectile(targ);
241         }
242 #endif
243
244         return true;
245 }
246
247 void trigger_push_touch(entity this, entity toucher)
248 {
249         if (this.active == ACTIVE_NOT)
250                 return;
251
252         if(this.team)
253                 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
254                         return;
255
256         EXACTTRIGGER_TOUCH(this, toucher);
257
258         noref bool success = jumppad_push(this, toucher);
259
260 #ifdef SVQC
261         if (success && (this.spawnflags & PUSH_ONCE))
262         {
263                 settouch(this, func_null);
264                 setthink(this, SUB_Remove);
265                 this.nextthink = time;
266         }
267 #endif
268 }
269
270 #ifdef SVQC
271 void trigger_push_link(entity this);
272 void trigger_push_updatelink(entity this);
273 bool trigger_push_testorigin(entity tracetest_ent, entity targ, entity jp, vector org)
274 {
275         setorigin(tracetest_ent, org);
276         tracetoss(tracetest_ent, tracetest_ent);
277         if(trace_startsolid)
278                 return false;
279
280         if(!jp.height)
281         {
282                 // since tracetoss starting from jumppad's origin often fails when target
283                 // is very close to real destination, start it directly from target's
284                 // origin instead
285                 tracetest_ent.velocity.z = 0;
286                 setorigin(tracetest_ent, targ.origin + stepheightvec);
287                 tracetoss(tracetest_ent, tracetest_ent);
288                 if(trace_startsolid)
289                 {
290                         setorigin(tracetest_ent, targ.origin + stepheightvec / 2);
291                         tracetoss(tracetest_ent, tracetest_ent);
292                         if(trace_startsolid)
293                         {
294                                 setorigin(tracetest_ent, targ.origin);
295                                 tracetoss(tracetest_ent, tracetest_ent);
296                                 if(trace_startsolid)
297                                         return false;
298                         }
299                 }
300         }
301         tracebox(trace_endpos, tracetest_ent.mins, tracetest_ent.maxs, trace_endpos - eZ * 1500, true, tracetest_ent);
302         return true;
303 }
304 #endif
305 void trigger_push_findtarget(entity this)
306 {
307         // first calculate a typical start point for the jump
308         vector org = (this.absmin + this.absmax) * 0.5;
309         org.z = this.absmax.z - PL_MIN_CONST.z;
310
311         if (this.target)
312         {
313                 int n = 0;
314 #ifdef SVQC
315                 vector vel = '0 0 0';
316 #endif
317                 for(entity t = NULL; (t = find(t, targetname, this.target)); )
318                 {
319                         ++n;
320 #ifdef SVQC
321                         if(t.move_movetype != MOVETYPE_NONE)
322                                 continue;
323
324                         entity e = spawn();
325                         setsize(e, PL_MIN_CONST, PL_MAX_CONST);
326                         e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
327                         e.velocity = trigger_push_calculatevelocity(org, t, this.height);
328                         vel = e.velocity;
329                         vector best_target = '0 0 0';
330                         vector best_org = '0 0 0';
331                         vector best_vel = '0 0 0';
332                         bool valid_best_target = false;
333                         if (trigger_push_testorigin(e, t, this, org))
334                         {
335                                 best_target = trace_endpos;
336                                 best_org = org;
337                                 best_vel = e.velocity;
338                                 valid_best_target = true;
339                         }
340
341                         vector new_org;
342                         vector dist = t.origin - org;
343                         if (dist.x || dist.y) // if not perfectly vertical
344                         {
345                                 // test trajectory with different starting points, sometimes the trajectory
346                                 // starting from the jumppad origin can't reach the real destination
347                                 // and destination waypoint ends up near the jumppad itself
348                                 vector flatdir = normalize(dist - eZ * dist.z);
349                                 vector ofs = flatdir * 0.5 * min(fabs(this.absmax.x - this.absmin.x), fabs(this.absmax.y - this.absmin.y));
350                                 new_org = org + ofs;
351                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
352                                 vel = e.velocity;
353                                 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
354                                         e.velocity = autocvar_sv_maxspeed * flatdir;
355                                 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
356                                 {
357                                         best_target = trace_endpos;
358                                         best_org = new_org;
359                                         best_vel = vel;
360                                         valid_best_target = true;
361                                 }
362                                 new_org = org - ofs;
363                                 e.velocity = trigger_push_calculatevelocity(new_org, t, this.height);
364                                 vel = e.velocity;
365                                 if (vdist(vec2(e.velocity), <, autocvar_sv_maxspeed))
366                                         e.velocity = autocvar_sv_maxspeed * flatdir;
367                                 if (trigger_push_testorigin(e, t, this, new_org) && (!valid_best_target || trace_endpos.z > best_target.z + 50))
368                                 {
369                                         best_target = trace_endpos;
370                                         best_org = new_org;
371                                         best_vel = vel;
372                                         valid_best_target = true;
373                                 }
374                         }
375
376                         if (valid_best_target)
377                         {
378                                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, best_target + PL_MIN_CONST, best_target + PL_MAX_CONST)))
379                                 {
380                                         float velxy = vlen(vec2(best_vel));
381                                         float cost = vlen(vec2(t.origin - best_org)) / velxy;
382                                         if(velxy < autocvar_sv_maxspeed)
383                                                 velxy = autocvar_sv_maxspeed;
384                                         cost += vlen(vec2(best_target - t.origin)) / velxy;
385                                         waypoint_spawnforteleporter(this, best_target, cost, e);
386                                 }
387                         }
388                         delete(e);
389 #endif
390                 }
391
392                 if(!n)
393                 {
394                         // no dest!
395 #ifdef SVQC
396                         objerror (this, "Jumppad with nonexistant target");
397 #endif
398                         return;
399                 }
400                 else if(n == 1)
401                 {
402                         // exactly one dest - bots love that
403                         this.enemy = find(NULL, targetname, this.target);
404                 }
405                 else
406                 {
407                         // have to use random selection every single time
408                         this.enemy = NULL;
409                 }
410         }
411 #ifdef SVQC
412         else
413         {
414                 entity e = spawn();
415                 setsize(e, PL_MIN_CONST, PL_MAX_CONST);
416                 e.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
417                 setorigin(e, org);
418                 e.velocity = this.movedir;
419                 tracetoss(e, e);
420                 if (!(boxesoverlap(this.absmin, this.absmax + eZ * 50, trace_endpos + PL_MIN_CONST, trace_endpos + PL_MAX_CONST)))
421                         waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity), e);
422                 delete(e);
423         }
424
425         defer(this, 0.1, trigger_push_updatelink);
426 #endif
427 }
428
429 #ifdef SVQC
430 float trigger_push_send(entity this, entity to, float sf)
431 {
432         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
433
434         WriteByte(MSG_ENTITY, this.team);
435         WriteInt24_t(MSG_ENTITY, this.spawnflags);
436         WriteByte(MSG_ENTITY, this.active);
437         WriteCoord(MSG_ENTITY, this.height);
438
439         WriteCoord(MSG_ENTITY, this.movedir_x);
440         WriteCoord(MSG_ENTITY, this.movedir_y);
441         WriteCoord(MSG_ENTITY, this.movedir_z);
442
443         trigger_common_write(this, true);
444
445         return true;
446 }
447
448 void trigger_push_updatelink(entity this)
449 {
450         this.SendFlags |= 1;
451 }
452
453 void trigger_push_link(entity this)
454 {
455         trigger_link(this, trigger_push_send);
456 }
457
458 /*
459  * ENTITY PARAMETERS:
460  *
461  *   target:  target of jump
462  *   height:  the absolute value is the height of the highest point of the jump
463  *            trajectory above the higher one of the player and the target.
464  *            the sign indicates whether the highest point is INSIDE (positive)
465  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
466  *            positive values for targets mounted on the floor, and use negative
467  *            values to target a point on the ceiling.
468  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
469  */
470 spawnfunc(trigger_push)
471 {
472         SetMovedir(this);
473
474         trigger_init(this);
475
476         this.active = ACTIVE_ACTIVE;
477         this.use = trigger_push_use;
478         settouch(this, trigger_push_touch);
479
480         // normal push setup
481         if (!this.speed)
482                 this.speed = 1000;
483         this.movedir = this.movedir * this.speed * 10;
484
485         if (!this.noise)
486                 this.noise = "misc/jumppad.wav";
487         precache_sound (this.noise);
488
489         trigger_push_link(this); // link it now
490
491         // this must be called to spawn the teleport waypoints for bots
492         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
493 }
494
495
496 bool target_push_send(entity this, entity to, float sf)
497 {
498         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
499
500         WriteByte(MSG_ENTITY, this.cnt);
501         WriteString(MSG_ENTITY, this.targetname);
502         WriteCoord(MSG_ENTITY, this.origin_x);
503         WriteCoord(MSG_ENTITY, this.origin_y);
504         WriteCoord(MSG_ENTITY, this.origin_z);
505
506         WriteAngle(MSG_ENTITY, this.angles_x);
507         WriteAngle(MSG_ENTITY, this.angles_y);
508         WriteAngle(MSG_ENTITY, this.angles_z);
509
510         return true;
511 }
512
513 void target_push_use(entity this, entity actor, entity trigger)
514 {
515         if(trigger.classname == "trigger_push" || trigger == this)
516                 return; // WTF, why is this a thing
517
518         jumppad_push(this, actor);
519 }
520
521 void target_push_link(entity this)
522 {
523         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
524         Net_LinkEntity(this, false, 0, target_push_send);
525         //this.SendFlags |= 1; // update
526 }
527
528 void target_push_init(entity this)
529 {
530         this.mangle = this.angles;
531         setorigin(this, this.origin);
532         target_push_link(this);
533 }
534
535 void target_push_init2(entity this)
536 {
537         if(this.target && this.target != "") // we have an old style pusher!
538         {
539                 InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
540                 this.use = target_push_use;
541         }
542
543         target_push_init(this); // normal push target behaviour can be combined with a legacy pusher?
544 }
545
546 spawnfunc(target_push) { target_push_init2(this); }
547 spawnfunc(info_notnull) { target_push_init(this); }
548 spawnfunc(target_position) { target_push_init(this); }
549
550 #elif defined(CSQC)
551
552 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
553 {
554         this.classname = "jumppad";
555         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
556         this.spawnflags = ReadInt24_t();
557         this.active = ReadByte();
558         this.height = ReadCoord();
559
560         this.movedir_x = ReadCoord();
561         this.movedir_y = ReadCoord();
562         this.movedir_z = ReadCoord();
563
564         trigger_common_read(this, true);
565
566         this.entremove = trigger_remove_generic;
567         this.solid = SOLID_TRIGGER;
568         settouch(this, trigger_push_touch);
569         this.move_time = time;
570         defer(this, 0.25, trigger_push_findtarget);
571
572         return true;
573 }
574
575 void target_push_remove(entity this)
576 {
577         //if(this.classname)
578                 //strunzone(this.classname);
579         //this.classname = string_null;
580
581         if(this.targetname)
582                 strunzone(this.targetname);
583         this.targetname = string_null;
584 }
585
586 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
587 {
588         this.classname = "push_target";
589         this.cnt = ReadByte();
590         this.targetname = strzone(ReadString());
591         this.origin_x = ReadCoord();
592         this.origin_y = ReadCoord();
593         this.origin_z = ReadCoord();
594
595         this.angles_x = ReadAngle();
596         this.angles_y = ReadAngle();
597         this.angles_z = ReadAngle();
598
599         return = true;
600
601         setorigin(this, this.origin);
602
603         this.drawmask = MASK_NORMAL;
604         this.entremove = target_push_remove;
605 }
606 #endif