498f0ff2dbbffb885c36a546ff7f4faba2bac378
[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 void trigger_push_touch(entity this, entity toucher)
134 {
135         if (this.active == ACTIVE_NOT)
136                 return;
137
138         if (!isPushable(toucher))
139                 return;
140
141         if(this.team)
142                 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
143                         return;
144
145         EXACTTRIGGER_TOUCH(this, toucher);
146
147         if(this.enemy)
148         {
149                 toucher.velocity = trigger_push_calculatevelocity(toucher.origin, this.enemy, this.height);
150         }
151         else if(this.target && this.target != "")
152         {
153                 entity e;
154                 RandomSelection_Init();
155                 for(e = NULL; (e = find(e, targetname, this.target)); )
156                 {
157                         if(e.cnt)
158                                 RandomSelection_AddEnt(e, e.cnt, 1);
159                         else
160                                 RandomSelection_AddEnt(e, 1, 1);
161                 }
162                 toucher.velocity = trigger_push_calculatevelocity(toucher.origin, RandomSelection_chosen_ent, this.height);
163         }
164         else
165         {
166                 toucher.velocity = this.movedir;
167         }
168
169         UNSET_ONGROUND(toucher);
170
171 #ifdef CSQC
172         if (toucher.flags & FL_PROJECTILE)
173         {
174                 toucher.angles = vectoangles (toucher.velocity);
175                 switch(toucher.move_movetype)
176                 {
177                         case MOVETYPE_FLY:
178                                 set_movetype(toucher, MOVETYPE_TOSS);
179                                 toucher.gravity = 1;
180                                 break;
181                         case MOVETYPE_BOUNCEMISSILE:
182                                 set_movetype(toucher, MOVETYPE_BOUNCE);
183                                 toucher.gravity = 1;
184                                 break;
185                 }
186         }
187 #endif
188
189 #ifdef SVQC
190         if (IS_PLAYER(toucher))
191         {
192                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
193                 toucher.oldvelocity = toucher.velocity;
194
195                 if(this.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
196                 {
197                         // flash when activated
198                         Send_Effect(EFFECT_JUMPPAD, toucher.origin, toucher.velocity, 1);
199                         _sound (toucher, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
200                         this.pushltime = time + 0.2;
201                 }
202                 if(IS_REAL_CLIENT(toucher) || IS_BOT_CLIENT(toucher))
203                 {
204                         bool found = false;
205                         for(int i = 0; i < toucher.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
206                                 if(toucher.(jumppadsused[i]) == this)
207                                         found = true;
208                         if(!found)
209                         {
210                                 toucher.(jumppadsused[toucher.jumppadcount % NUM_JUMPPADSUSED]) = this;
211                                 toucher.jumppadcount = toucher.jumppadcount + 1;
212                         }
213
214                         if(IS_REAL_CLIENT(toucher))
215                         {
216                                 if(this.message)
217                                         centerprint(toucher, this.message);
218                         }
219                         else
220                                 toucher.lastteleporttime = time;
221
222                         if (!IS_DEAD(toucher))
223                                 animdecide_setaction(toucher, ANIMACTION_JUMP, true);
224                 }
225                 else
226                         toucher.jumppadcount = true;
227
228                 // reset tracking of who pushed you into a hazard (for kill credit)
229                 toucher.pushltime = 0;
230                 toucher.istypefrag = 0;
231         }
232
233         if(this.enemy.target)
234                 SUB_UseTargets(this.enemy, toucher, toucher); // TODO: do we need toucher as trigger too?
235
236         if (toucher.flags & FL_PROJECTILE)
237         {
238                 toucher.angles = vectoangles (toucher.velocity);
239                 toucher.com_phys_gravity_factor = 1;
240                 switch(toucher.move_movetype)
241                 {
242                         case MOVETYPE_FLY:
243                                 set_movetype(toucher, MOVETYPE_TOSS);
244                                 toucher.gravity = 1;
245                                 break;
246                         case MOVETYPE_BOUNCEMISSILE:
247                                 set_movetype(toucher, MOVETYPE_BOUNCE);
248                                 toucher.gravity = 1;
249                                 break;
250                 }
251                 UpdateCSQCProjectile(toucher);
252         }
253
254         /*if (toucher.flags & FL_ITEM)
255         {
256                 ItemUpdate(toucher);
257                 toucher.SendFlags |= ISF_DROP;
258         }*/
259
260         if (this.spawnflags & PUSH_ONCE)
261         {
262                 settouch(this, func_null);
263                 setthink(this, SUB_Remove);
264                 this.nextthink = time;
265         }
266 #endif
267 }
268
269 #ifdef SVQC
270 void trigger_push_link(entity this);
271 void trigger_push_updatelink(entity this);
272 #endif
273 void trigger_push_findtarget(entity this)
274 {
275         entity t;
276         vector org;
277
278         // first calculate a typical start point for the jump
279         org = (this.absmin + this.absmax) * 0.5;
280         org_z = this.absmax.z - STAT(PL_MIN, NULL).z;
281
282         if (this.target)
283         {
284                 float n = 0;
285                 for(t = NULL; (t = find(t, targetname, this.target)); )
286                 {
287                         ++n;
288 #ifdef SVQC
289                         entity e = spawn();
290                         setorigin(e, org);
291                         setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
292                         e.velocity = trigger_push_calculatevelocity(org, t, this.height);
293                         tracetoss(e, e);
294                         if(e.move_movetype == MOVETYPE_NONE)
295                                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
296                         delete(e);
297 #endif
298                 }
299
300                 if(!n)
301                 {
302                         // no dest!
303 #ifdef SVQC
304                         objerror (this, "Jumppad with nonexistant target");
305 #endif
306                         return;
307                 }
308                 else if(n == 1)
309                 {
310                         // exactly one dest - bots love that
311                         this.enemy = find(NULL, targetname, this.target);
312                 }
313                 else
314                 {
315                         // have to use random selection every single time
316                         this.enemy = NULL;
317                 }
318         }
319 #ifdef SVQC
320         else
321         {
322                 entity e = spawn();
323                 setorigin(e, org);
324                 setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
325                 e.velocity = this.movedir;
326                 tracetoss(e, e);
327                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
328                 delete(e);
329         }
330
331         trigger_push_link(this);
332         defer(this, 0.1, trigger_push_updatelink);
333 #endif
334 }
335
336 #ifdef SVQC
337 float trigger_push_send(entity this, entity to, float sf)
338 {
339         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
340
341         WriteByte(MSG_ENTITY, this.team);
342         WriteInt24_t(MSG_ENTITY, this.spawnflags);
343         WriteByte(MSG_ENTITY, this.active);
344         WriteCoord(MSG_ENTITY, this.height);
345
346         WriteCoord(MSG_ENTITY, this.movedir_x);
347         WriteCoord(MSG_ENTITY, this.movedir_y);
348         WriteCoord(MSG_ENTITY, this.movedir_z);
349
350         trigger_common_write(this, true);
351
352         return true;
353 }
354
355 void trigger_push_updatelink(entity this)
356 {
357         this.SendFlags |= 1;
358 }
359
360 void trigger_push_link(entity this)
361 {
362         trigger_link(this, trigger_push_send);
363 }
364
365 /*
366  * ENTITY PARAMETERS:
367  *
368  *   target:  target of jump
369  *   height:  the absolute value is the height of the highest point of the jump
370  *            trajectory above the higher one of the player and the target.
371  *            the sign indicates whether the highest point is INSIDE (positive)
372  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
373  *            positive values for targets mounted on the floor, and use negative
374  *            values to target a point on the ceiling.
375  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
376  */
377 spawnfunc(trigger_push)
378 {
379         SetMovedir(this);
380
381         trigger_init(this);
382
383         this.active = ACTIVE_ACTIVE;
384         this.use = trigger_push_use;
385         settouch(this, trigger_push_touch);
386
387         // normal push setup
388         if (!this.speed)
389                 this.speed = 1000;
390         this.movedir = this.movedir * this.speed * 10;
391
392         if (!this.noise)
393                 this.noise = "misc/jumppad.wav";
394         precache_sound (this.noise);
395
396         // this must be called to spawn the teleport waypoints for bots
397         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
398 }
399
400
401 bool target_push_send(entity this, entity to, float sf)
402 {
403         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
404
405         WriteByte(MSG_ENTITY, this.cnt);
406         WriteString(MSG_ENTITY, this.targetname);
407         WriteCoord(MSG_ENTITY, this.origin_x);
408         WriteCoord(MSG_ENTITY, this.origin_y);
409         WriteCoord(MSG_ENTITY, this.origin_z);
410
411         WriteAngle(MSG_ENTITY, this.angles_x);
412         WriteAngle(MSG_ENTITY, this.angles_y);
413         WriteAngle(MSG_ENTITY, this.angles_z);
414
415         return true;
416 }
417
418 void target_push_link(entity this)
419 {
420         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
421         Net_LinkEntity(this, false, 0, target_push_send);
422         //this.SendFlags |= 1; // update
423 }
424
425 void target_push_init(entity this)
426 {
427         this.mangle = this.angles;
428         setorigin(this, this.origin);
429         target_push_link(this);
430 }
431
432 spawnfunc(target_push) { target_push_init(this); }
433 spawnfunc(info_notnull) { target_push_init(this); }
434 spawnfunc(target_position) { target_push_init(this); }
435
436 #elif defined(CSQC)
437
438 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
439 {
440         this.classname = "jumppad";
441         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
442         this.spawnflags = ReadInt24_t();
443         this.active = ReadByte();
444         this.height = ReadCoord();
445
446         this.movedir_x = ReadCoord();
447         this.movedir_y = ReadCoord();
448         this.movedir_z = ReadCoord();
449
450         trigger_common_read(this, true);
451
452         this.entremove = trigger_remove_generic;
453         this.solid = SOLID_TRIGGER;
454         settouch(this, trigger_push_touch);
455         this.move_time = time;
456         defer(this, 0.25, trigger_push_findtarget);
457
458         return true;
459 }
460
461 void target_push_remove(entity this)
462 {
463         if(this.classname)
464                 strunzone(this.classname);
465         this.classname = string_null;
466
467         if(this.targetname)
468                 strunzone(this.targetname);
469         this.targetname = string_null;
470 }
471
472 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
473 {
474         this.classname = "push_target";
475         this.cnt = ReadByte();
476         this.targetname = strzone(ReadString());
477         this.origin_x = ReadCoord();
478         this.origin_y = ReadCoord();
479         this.origin_z = ReadCoord();
480
481         this.angles_x = ReadAngle();
482         this.angles_y = ReadAngle();
483         this.angles_z = ReadAngle();
484
485         return = true;
486
487         setorigin(this, this.origin);
488
489         this.drawmask = MASK_NORMAL;
490         this.entremove = target_push_remove;
491 }
492 #endif