]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/jumppads.qc
Merge branch 'master' into Mario/showspecs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / trigger / jumppads.qc
1 // TODO: split target_push and put it in the target folder
2 #ifdef SVQC
3 #include "jumppads.qh"
4 #include <common/physics/movetypes/movetypes.qh>
5
6 void trigger_push_use(entity this, entity actor, entity trigger)
7 {
8         if(teamplay)
9         {
10                 this.team = actor.team;
11                 this.SendFlags |= 2;
12         }
13 }
14 #endif
15
16 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_PUSH)
17 REGISTER_NET_LINKED(ENT_CLIENT_TARGET_PUSH)
18
19 /*
20         trigger_push_calculatevelocity
21
22         Arguments:
23           org - origin of the object which is to be pushed
24           tgt - target entity (can be either a point or a model entity; if it is
25                 the latter, its midpoint is used)
26           ht  - jump height, measured from the higher one of org and tgt's midpoint
27
28         Returns: velocity for the jump
29         the global trigger_push_calculatevelocity_flighttime is set to the total
30         jump time
31  */
32
33 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
34 {
35         float grav, sdist, zdist, vs, vz, jumpheight;
36         vector sdir, torg;
37
38         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
39
40         grav = PHYS_GRAVITY(other);
41         if(PHYS_ENTGRAVITY(other))
42                 grav *= PHYS_ENTGRAVITY(other);
43
44         zdist = torg.z - org.z;
45         sdist = vlen(torg - org - zdist * '0 0 1');
46         sdir = normalize(torg - org - zdist * '0 0 1');
47
48         // how high do we need to push the player?
49         jumpheight = fabs(ht);
50         if(zdist > 0)
51                 jumpheight = jumpheight + zdist;
52
53         /*
54                 STOP.
55
56                 You will not understand the following equations anyway...
57                 But here is what I did to get them.
58
59                 I used the functions
60
61                   s(t) = t * vs
62                   z(t) = t * vz - 1/2 grav t^2
63
64                 and solved for:
65
66                   s(ti) = sdist
67                   z(ti) = zdist
68                   max(z, ti) = jumpheight
69
70                 From these three equations, you will find the three parameters vs, vz
71                 and ti.
72          */
73
74         // push him so high...
75         vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
76
77         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
78         if(ht < 0)
79                 if(zdist < 0)
80                         vz = -vz;
81
82         vector solution;
83         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
84         // ALWAYS solvable because jumpheight >= zdist
85         if(!solution.z)
86                 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)
87         if(zdist == 0)
88                 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)
89
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                         trigger_push_calculatevelocity_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                         trigger_push_calculatevelocity_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                         trigger_push_calculatevelocity_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                         trigger_push_calculatevelocity_flighttime = solution.y;
124                 }
125         }
126         vs = sdist / trigger_push_calculatevelocity_flighttime;
127
128         // finally calculate the velocity
129         return sdir * vs + '0 0 1' * vz;
130 }
131
132 void trigger_push_touch(entity this, entity toucher)
133 {
134         if (this.active == ACTIVE_NOT)
135                 return;
136
137         if (!isPushable(toucher))
138                 return;
139
140         if(this.team)
141                 if(((this.spawnflags & 4) == 0) == (DIFF_TEAM(this, toucher)))
142                         return;
143
144         EXACTTRIGGER_TOUCH(this, toucher);
145
146         if(this.enemy)
147         {
148                 toucher.velocity = trigger_push_calculatevelocity(toucher.origin, this.enemy, this.height);
149                 toucher.move_velocity = toucher.velocity;
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_Add(e, 0, string_null, e.cnt, 1);
159                         else
160                                 RandomSelection_Add(e, 0, string_null, 1, 1);
161                 }
162                 toucher.velocity = trigger_push_calculatevelocity(toucher.origin, RandomSelection_chosen_ent, this.height);
163                 toucher.move_velocity = toucher.velocity;
164         }
165         else
166         {
167                 toucher.velocity = this.movedir;
168                 toucher.move_velocity = toucher.velocity;
169         }
170
171 #ifdef SVQC
172         UNSET_ONGROUND(toucher);
173 #elif defined(CSQC)
174         toucher.move_flags &= ~FL_ONGROUND;
175
176         if (toucher.flags & FL_PROJECTILE)
177         {
178                 toucher.move_angles = vectoangles (toucher.move_velocity);
179                 switch(toucher.move_movetype)
180                 {
181                         case MOVETYPE_FLY:
182                                 toucher.move_movetype = MOVETYPE_TOSS;
183                                 toucher.gravity = 1;
184                                 break;
185                         case MOVETYPE_BOUNCEMISSILE:
186                                 toucher.move_movetype = MOVETYPE_BOUNCE;
187                                 toucher.gravity = 1;
188                                 break;
189                 }
190         }
191 #endif
192
193 #ifdef SVQC
194         if (IS_PLAYER(toucher))
195         {
196                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
197                 toucher.oldvelocity = toucher.velocity;
198
199                 if(this.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
200                 {
201                         // flash when activated
202                         Send_Effect(EFFECT_JUMPPAD, toucher.origin, toucher.velocity, 1);
203                         _sound (toucher, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
204                         this.pushltime = time + 0.2;
205                 }
206                 if(IS_REAL_CLIENT(toucher) || IS_BOT_CLIENT(toucher))
207                 {
208                         bool found = false;
209                         for(int i = 0; i < toucher.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
210                                 if(toucher.(jumppadsused[i]) == this)
211                                         found = true;
212                         if(!found)
213                         {
214                                 toucher.(jumppadsused[toucher.jumppadcount % NUM_JUMPPADSUSED]) = this;
215                                 toucher.jumppadcount = toucher.jumppadcount + 1;
216                         }
217
218                         if(IS_REAL_CLIENT(toucher))
219                         {
220                                 if(this.message)
221                                         centerprint(toucher, this.message);
222                         }
223                         else
224                                 toucher.lastteleporttime = time;
225
226                         if (!IS_DEAD(toucher))
227                                 animdecide_setaction(toucher, ANIMACTION_JUMP, true);
228                 }
229                 else
230                         toucher.jumppadcount = true;
231
232                 // reset tracking of who pushed you into a hazard (for kill credit)
233                 toucher.pushltime = 0;
234                 toucher.istypefrag = 0;
235         }
236
237         if(this.enemy.target)
238                 SUB_UseTargets(this.enemy, toucher, toucher); // TODO: do we need toucher as trigger too?
239
240         if (toucher.flags & FL_PROJECTILE)
241         {
242                 toucher.angles = vectoangles (toucher.velocity);
243                 switch(toucher.movetype)
244                 {
245                         case MOVETYPE_FLY:
246                                 toucher.movetype = MOVETYPE_TOSS;
247                                 toucher.gravity = 1;
248                                 break;
249                         case MOVETYPE_BOUNCEMISSILE:
250                                 toucher.movetype = MOVETYPE_BOUNCE;
251                                 toucher.gravity = 1;
252                                 break;
253                 }
254                 UpdateCSQCProjectile(toucher);
255         }
256
257         /*if (toucher.flags & FL_ITEM)
258         {
259                 ItemUpdate(toucher);
260                 toucher.SendFlags |= ISF_DROP;
261         }*/
262
263         if (this.spawnflags & PUSH_ONCE)
264         {
265                 settouch(this, func_null);
266                 setthink(this, SUB_Remove);
267                 this.nextthink = time;
268         }
269 #endif
270 }
271
272 #ifdef SVQC
273 void trigger_push_link(entity this);
274 void trigger_push_updatelink(entity this);
275 #endif
276 void trigger_push_findtarget(entity this)
277 {
278         entity t;
279         vector org;
280
281         // first calculate a typical start point for the jump
282         org = (this.absmin + this.absmax) * 0.5;
283         org_z = this.absmax.z - STAT(PL_MIN, NULL).z;
284
285         if (this.target)
286         {
287                 float n = 0;
288                 for(t = NULL; (t = find(t, targetname, this.target)); )
289                 {
290                         ++n;
291 #ifdef SVQC
292                         entity e = spawn();
293                         setorigin(e, org);
294                         setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
295                         e.velocity = trigger_push_calculatevelocity(org, t, this.height);
296                         tracetoss(e, e);
297                         if(e.movetype == MOVETYPE_NONE)
298                                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
299                         remove(e);
300 #endif
301                 }
302
303                 if(!n)
304                 {
305                         // no dest!
306 #ifdef SVQC
307                         objerror (this, "Jumppad with nonexistant target");
308 #endif
309                         return;
310                 }
311                 else if(n == 1)
312                 {
313                         // exactly one dest - bots love that
314                         this.enemy = find(NULL, targetname, this.target);
315                 }
316                 else
317                 {
318                         // have to use random selection every single time
319                         this.enemy = NULL;
320                 }
321         }
322 #ifdef SVQC
323         else
324         {
325                 entity e = spawn();
326                 setorigin(e, org);
327                 setsize(e, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
328                 e.velocity = this.movedir;
329                 tracetoss(e, e);
330                 waypoint_spawnforteleporter(this, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
331                 remove(e);
332         }
333
334         trigger_push_link(this);
335         defer(this, 0.1, trigger_push_updatelink);
336 #endif
337 }
338
339 #ifdef SVQC
340 float trigger_push_send(entity this, entity to, float sf)
341 {
342         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
343
344         WriteByte(MSG_ENTITY, this.team);
345         WriteInt24_t(MSG_ENTITY, this.spawnflags);
346         WriteByte(MSG_ENTITY, this.active);
347         WriteCoord(MSG_ENTITY, this.height);
348
349         WriteCoord(MSG_ENTITY, this.movedir_x);
350         WriteCoord(MSG_ENTITY, this.movedir_y);
351         WriteCoord(MSG_ENTITY, this.movedir_z);
352
353         trigger_common_write(this, true);
354
355         return true;
356 }
357
358 void trigger_push_updatelink(entity this)
359 {
360         this.SendFlags |= 1;
361 }
362
363 void trigger_push_link(entity this)
364 {
365         trigger_link(this, trigger_push_send);
366 }
367
368 /*
369  * ENTITY PARAMETERS:
370  *
371  *   target:  target of jump
372  *   height:  the absolute value is the height of the highest point of the jump
373  *            trajectory above the higher one of the player and the target.
374  *            the sign indicates whether the highest point is INSIDE (positive)
375  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
376  *            positive values for targets mounted on the floor, and use negative
377  *            values to target a point on the ceiling.
378  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
379  */
380 spawnfunc(trigger_push)
381 {
382         SetMovedir(this);
383
384         trigger_init(this);
385
386         this.active = ACTIVE_ACTIVE;
387         this.use = trigger_push_use;
388         settouch(this, trigger_push_touch);
389
390         // normal push setup
391         if (!this.speed)
392                 this.speed = 1000;
393         this.movedir = this.movedir * this.speed * 10;
394
395         if (!this.noise)
396                 this.noise = "misc/jumppad.wav";
397         precache_sound (this.noise);
398
399         // this must be called to spawn the teleport waypoints for bots
400         InitializeEntity(this, trigger_push_findtarget, INITPRIO_FINDTARGET);
401 }
402
403
404 bool target_push_send(entity this, entity to, float sf)
405 {
406         WriteHeader(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
407
408         WriteByte(MSG_ENTITY, this.cnt);
409         WriteString(MSG_ENTITY, this.targetname);
410         WriteCoord(MSG_ENTITY, this.origin_x);
411         WriteCoord(MSG_ENTITY, this.origin_y);
412         WriteCoord(MSG_ENTITY, this.origin_z);
413
414         WriteAngle(MSG_ENTITY, this.angles_x);
415         WriteAngle(MSG_ENTITY, this.angles_y);
416         WriteAngle(MSG_ENTITY, this.angles_z);
417
418         return true;
419 }
420
421 void target_push_link(entity this)
422 {
423         BITSET_ASSIGN(this.effects, EF_NODEPTHTEST);
424         Net_LinkEntity(this, false, 0, target_push_send);
425         //this.SendFlags |= 1; // update
426 }
427
428 void target_push_init(entity this)
429 {
430         this.mangle = this.angles;
431         setorigin(this, this.origin);
432         target_push_link(this);
433 }
434
435 spawnfunc(target_push) { target_push_init(this); }
436 spawnfunc(info_notnull) { target_push_init(this); }
437 spawnfunc(target_position) { target_push_init(this); }
438
439 #elif defined(CSQC)
440
441 NET_HANDLE(ENT_CLIENT_TRIGGER_PUSH, bool isnew)
442 {
443         this.classname = "jumppad";
444         int mytm = ReadByte(); if(mytm) { this.team = mytm - 1; }
445         this.spawnflags = ReadInt24_t();
446         this.active = ReadByte();
447         this.height = ReadCoord();
448
449         this.movedir_x = ReadCoord();
450         this.movedir_y = ReadCoord();
451         this.movedir_z = ReadCoord();
452
453         trigger_common_read(this, true);
454
455         this.entremove = trigger_remove_generic;
456         this.solid = SOLID_TRIGGER;
457         settouch(this, trigger_push_touch);
458         this.move_time = time;
459         defer(this, 0.25, trigger_push_findtarget);
460
461         return true;
462 }
463
464 void target_push_remove(entity this)
465 {
466         if(this.classname)
467                 strunzone(this.classname);
468         this.classname = string_null;
469
470         if(this.targetname)
471                 strunzone(this.targetname);
472         this.targetname = string_null;
473 }
474
475 NET_HANDLE(ENT_CLIENT_TARGET_PUSH, bool isnew)
476 {
477         this.classname = "push_target";
478         this.cnt = ReadByte();
479         this.targetname = strzone(ReadString());
480         this.origin_x = ReadCoord();
481         this.origin_y = ReadCoord();
482         this.origin_z = ReadCoord();
483
484         this.angles_x = ReadAngle();
485         this.angles_y = ReadAngle();
486         this.angles_z = ReadAngle();
487
488         return = true;
489
490         setorigin(this, this.origin);
491
492         this.drawmask = MASK_NORMAL;
493         this.entremove = target_push_remove;
494 }
495 #endif