]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/jumppads.qc
Add an argument to SetMovedir
[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 /*
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 {SELFPARAM();
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                         Send_Effect(EFFECT_JUMPPAD, 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                 activator = other;
221                 WITH(entity, self, self.enemy, SUB_UseTargets());
222         }
223
224         if (other.flags & FL_PROJECTILE)
225         {
226                 other.angles = vectoangles (other.velocity);
227                 switch(other.movetype)
228                 {
229                         case MOVETYPE_FLY:
230                                 other.movetype = MOVETYPE_TOSS;
231                                 other.gravity = 1;
232                                 break;
233                         case MOVETYPE_BOUNCEMISSILE:
234                                 other.movetype = MOVETYPE_BOUNCE;
235                                 other.gravity = 1;
236                                 break;
237                 }
238                 UpdateCSQCProjectile(other);
239         }
240
241         if (self.spawnflags & PUSH_ONCE)
242         {
243                 self.touch = func_null;
244                 self.think = SUB_Remove;
245                 self.nextthink = time;
246         }
247 #endif
248 }
249
250 #ifdef SVQC
251 void trigger_push_link();
252 void trigger_push_updatelink();
253 #endif
254 void trigger_push_findtarget()
255 {SELFPARAM();
256         entity t;
257         vector org;
258
259         // first calculate a typical start point for the jump
260         org = (self.absmin + self.absmax) * 0.5;
261         org_z = self.absmax.z - PL_MIN.z;
262
263         if (self.target)
264         {
265                 float n = 0;
266                 for(t = world; (t = find(t, targetname, self.target)); )
267                 {
268                         ++n;
269 #ifdef SVQC
270                         entity e = spawn();
271                         setorigin(e, org);
272                         setsize(e, PL_MIN, PL_MAX);
273                         e.velocity = trigger_push_calculatevelocity(org, t, self.height);
274                         tracetoss(e, e);
275                         if(e.movetype == MOVETYPE_NONE)
276                                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
277                         remove(e);
278 #endif
279                 }
280
281                 if(!n)
282                 {
283                         // no dest!
284 #ifdef SVQC
285                         objerror ("Jumppad with nonexistant target");
286 #endif
287                         return;
288                 }
289                 else if(n == 1)
290                 {
291                         // exactly one dest - bots love that
292                         self.enemy = find(world, targetname, self.target);
293                 }
294                 else
295                 {
296                         // have to use random selection every single time
297                         self.enemy = world;
298                 }
299         }
300 #ifdef SVQC
301         else
302         {
303                 entity e = spawn();
304                 setorigin(e, org);
305                 setsize(e, PL_MIN, PL_MAX);
306                 e.velocity = self.movedir;
307                 tracetoss(e, e);
308                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
309                 remove(e);
310         }
311
312         trigger_push_link();
313         defer(0.1, trigger_push_updatelink);
314 #endif
315 }
316
317 #ifdef SVQC
318 float trigger_push_send(entity to, float sf)
319 {SELFPARAM();
320         WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
321         WriteByte(MSG_ENTITY, sf);
322
323         if(sf & 1)
324         {
325                 WriteByte(MSG_ENTITY, self.team);
326                 WriteInt24_t(MSG_ENTITY, self.spawnflags);
327                 WriteByte(MSG_ENTITY, self.active);
328                 WriteByte(MSG_ENTITY, self.height);
329
330                 trigger_common_write(true);
331         }
332
333         if(sf & 2)
334         {
335                 WriteByte(MSG_ENTITY, self.team);
336                 WriteByte(MSG_ENTITY, self.active);
337         }
338
339         return true;
340 }
341
342 void trigger_push_updatelink()
343 {SELFPARAM();
344         self.SendFlags |= 1;
345 }
346
347 void trigger_push_link()
348 {
349         //Net_LinkEntity(self, false, 0, trigger_push_send);
350 }
351 #endif
352 #ifdef SVQC
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         EXACTTRIGGER_INIT;
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         WriteByte(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         return true;
400 }
401
402 void target_push_link()
403 {SELFPARAM();
404         Net_LinkEntity(self, false, 0, target_push_send);
405         self.SendFlags |= 1; // update
406 }
407
408 spawnfunc(target_push) { target_push_link(); }
409 spawnfunc(info_notnull) { target_push_link(); }
410 spawnfunc(target_position) { target_push_link(); }
411
412 #endif
413
414 #ifdef CSQC
415
416 void ent_trigger_push()
417 {SELFPARAM();
418         float sf = ReadByte();
419
420         if(sf & 1)
421         {
422                 self.classname = "jumppad";
423                 int mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
424                 self.spawnflags = ReadInt24_t();
425                 self.active = ReadByte();
426                 self.height = ReadByte();
427
428                 trigger_common_read(true);
429
430                 self.entremove = trigger_remove_generic;
431                 self.solid = SOLID_TRIGGER;
432                 self.draw = trigger_draw_generic;
433                 self.trigger_touch = trigger_push_touch;
434                 self.drawmask = MASK_NORMAL;
435                 self.move_time = time;
436                 trigger_push_findtarget();
437         }
438
439         if(sf & 2)
440         {
441                 self.team = ReadByte();
442                 self.active = ReadByte();
443         }
444 }
445
446 void target_push_remove()
447 {SELFPARAM();
448         if(self.classname)
449                 strunzone(self.classname);
450         self.classname = string_null;
451
452         if(self.targetname)
453                 strunzone(self.targetname);
454         self.targetname = string_null;
455 }
456
457 void ent_target_push()
458 {SELFPARAM();
459         self.classname = "push_target";
460         self.cnt = ReadByte();
461         self.targetname = strzone(ReadString());
462         self.origin_x = ReadCoord();
463         self.origin_y = ReadCoord();
464         self.origin_z = ReadCoord();
465         setorigin(self, self.origin);
466
467         self.drawmask = MASK_NORMAL;
468         self.entremove = target_push_remove;
469 }
470 #endif