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