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