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