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