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