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