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