]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_jumppads.qc
#include this
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / t_jumppads.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qc"
5     #include "../dpdefs/dpextensions.qc"
6     #include "sys-post.qh"
7     #include "../warpzonelib/util_server.qh"
8     #include "../common/constants.qh"
9     #include "../common/util.qh"
10     #include "../common/animdecide.qh"
11     #include "../common/weapons/weapons.qh"
12     #include "weapons/csqcprojectile.qh"
13     #include "constants.qh"
14     #include "defs.qh"
15 #endif
16
17 const float PUSH_ONCE                   = 1;
18 const float PUSH_SILENT         = 2;
19
20 .float pushltime;
21 .float istypefrag;
22 .float height;
23
24 void() SUB_UseTargets;
25
26 float trigger_push_calculatevelocity_flighttime;
27
28 void trigger_push_use()
29 {
30         if(teamplay)
31                 self.team = activator.team;
32 }
33
34 /*
35         trigger_push_calculatevelocity
36
37         Arguments:
38           org - origin of the object which is to be pushed
39           tgt - target entity (can be either a point or a model entity; if it is
40                 the latter, its midpoint is used)
41           ht  - jump height, measured from the higher one of org and tgt's midpoint
42
43         Returns: velocity for the jump
44         the global trigger_push_calculatevelocity_flighttime is set to the total
45         jump time
46  */
47
48 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
49 {
50         float grav, sdist, zdist, vs, vz, jumpheight;
51         vector sdir, torg;
52
53         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
54
55         grav = autocvar_sv_gravity;
56         if(other.gravity)
57                 grav *= other.gravity;
58
59         zdist = torg.z - org.z;
60         sdist = vlen(torg - org - zdist * '0 0 1');
61         sdir = normalize(torg - org - zdist * '0 0 1');
62
63         // how high do we need to push the player?
64         jumpheight = fabs(ht);
65         if(zdist > 0)
66                 jumpheight = jumpheight + zdist;
67
68         /*
69                 STOP.
70
71                 You will not understand the following equations anyway...
72                 But here is what I did to get them.
73
74                 I used the functions
75
76                   s(t) = t * vs
77                   z(t) = t * vz - 1/2 grav t^2
78
79                 and solved for:
80
81                   s(ti) = sdist
82                   z(ti) = zdist
83                   max(z, ti) = jumpheight
84
85                 From these three equations, you will find the three parameters vs, vz
86                 and ti.
87          */
88
89         // push him so high...
90         vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
91
92         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
93         if(ht < 0)
94                 if(zdist < 0)
95                         vz = -vz;
96
97         vector solution;
98         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
99         // ALWAYS solvable because jumpheight >= zdist
100         if(!solution.z)
101                 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)
102         if(zdist == 0)
103                 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)
104
105         if(zdist < 0)
106         {
107                 // down-jump
108                 if(ht < 0)
109                 {
110                         // almost straight line type
111                         // jump apex is before the jump
112                         // we must take the larger one
113                         trigger_push_calculatevelocity_flighttime = solution.y;
114                 }
115                 else
116                 {
117                         // regular jump
118                         // jump apex is during the jump
119                         // we must take the larger one too
120                         trigger_push_calculatevelocity_flighttime = solution.y;
121                 }
122         }
123         else
124         {
125                 // up-jump
126                 if(ht < 0)
127                 {
128                         // almost straight line type
129                         // jump apex is after the jump
130                         // we must take the smaller one
131                         trigger_push_calculatevelocity_flighttime = solution.x;
132                 }
133                 else
134                 {
135                         // regular jump
136                         // jump apex is during the jump
137                         // we must take the larger one
138                         trigger_push_calculatevelocity_flighttime = solution.y;
139                 }
140         }
141         vs = sdist / trigger_push_calculatevelocity_flighttime;
142
143         // finally calculate the velocity
144         return sdir * vs + '0 0 1' * vz;
145 }
146
147 void trigger_push_touch()
148 {
149         if (self.active == ACTIVE_NOT)
150                 return;
151
152         if (!isPushable(other))
153                 return;
154
155         if(self.team)
156                 if(((self.spawnflags & 4) == 0) == (self.team != other.team))
157                         return;
158
159         EXACTTRIGGER_TOUCH;
160
161         if(self.enemy)
162         {
163                 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
164         }
165         else if(self.target)
166         {
167                 entity e;
168                 RandomSelection_Init();
169                 for(e = world; (e = find(e, targetname, self.target)); )
170                 {
171                         if(e.cnt)
172                                 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
173                         else
174                                 RandomSelection_Add(e, 0, string_null, 1, 1);
175                 }
176                 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
177         }
178         else
179         {
180                 other.velocity = self.movedir;
181         }
182
183         other.flags &= ~FL_ONGROUND;
184
185         if (IS_PLAYER(other))
186         {
187                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
188                 other.oldvelocity = other.velocity;
189
190                 if(self.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
191                 {
192                         // flash when activated
193                         pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
194                         sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
195                         self.pushltime = time + 0.2;
196                 }
197                 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
198                 {
199                         float i;
200                         float found;
201                         found = false;
202                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
203                                 if(other.(jumppadsused[i]) == self)
204                                         found = true;
205                         if(!found)
206                         {
207                                 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
208                                 other.jumppadcount = other.jumppadcount + 1;
209                         }
210
211                         if(IS_REAL_CLIENT(other))
212                         {
213                                 if(self.message)
214                                         centerprint(other, self.message);
215                         }
216                         else
217                                 other.lastteleporttime = time;
218
219                         if (other.deadflag == DEAD_NO)
220                                 animdecide_setaction(other, ANIMACTION_JUMP, true);
221                 }
222                 else
223                         other.jumppadcount = true;
224
225                 // reset tracking of who pushed you into a hazard (for kill credit)
226                 other.pushltime = 0;
227                 other.istypefrag = 0;
228         }
229
230         if(self.enemy.target)
231         {
232                 entity oldself;
233                 oldself = self;
234                 activator = other;
235                 self = self.enemy;
236                 SUB_UseTargets();
237                 self = oldself;
238         }
239
240         if (other.flags & FL_PROJECTILE)
241         {
242                 other.angles = vectoangles (other.velocity);
243                 switch(other.movetype)
244                 {
245                         case MOVETYPE_FLY:
246                                 other.movetype = MOVETYPE_TOSS;
247                                 other.gravity = 1;
248                                 break;
249                         case MOVETYPE_BOUNCEMISSILE:
250                                 other.movetype = MOVETYPE_BOUNCE;
251                                 other.gravity = 1;
252                                 break;
253                 }
254                 UpdateCSQCProjectile(other);
255         }
256
257         if (self.spawnflags & PUSH_ONCE)
258         {
259                 self.touch = func_null;
260                 self.think = SUB_Remove;
261                 self.nextthink = time;
262         }
263 }
264
265 .vector dest;
266 void trigger_push_findtarget()
267 {
268         entity e, t;
269         vector org;
270
271         // first calculate a typical start point for the jump
272         org = (self.absmin + self.absmax) * 0.5;
273         org_z = self.absmax.z - PL_MIN_z;
274
275         if (self.target)
276         {
277                 float n;
278                 n = 0;
279                 for(t = world; (t = find(t, targetname, self.target)); )
280                 {
281                         ++n;
282                         e = spawn();
283                         setorigin(e, org);
284                         setsize(e, PL_MIN, PL_MAX);
285                         e.velocity = trigger_push_calculatevelocity(org, t, self.height);
286                         tracetoss(e, e);
287                         if(e.movetype == MOVETYPE_NONE)
288                                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
289                         remove(e);
290                 }
291
292                 if(n == 0)
293                 {
294                         // no dest!
295                         objerror ("Jumppad with nonexistant target");
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         else
310         {
311                 e = spawn();
312                 setorigin(e, org);
313                 setsize(e, PL_MIN, PL_MAX);
314                 e.velocity = self.movedir;
315                 tracetoss(e, e);
316                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
317                 remove(e);
318         }
319 }
320
321 /*
322  * ENTITY PARAMETERS:
323  *
324  *   target:  target of jump
325  *   height:  the absolute value is the height of the highest point of the jump
326  *            trajectory above the higher one of the player and the target.
327  *            the sign indicates whether the highest point is INSIDE (positive)
328  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
329  *            positive values for targets mounted on the floor, and use negative
330  *            values to target a point on the ceiling.
331  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
332  */
333 void spawnfunc_trigger_push()
334 {
335         SetMovedir ();
336
337         EXACTTRIGGER_INIT;
338
339         self.active = ACTIVE_ACTIVE;
340         self.use = trigger_push_use;
341         self.touch = trigger_push_touch;
342
343         // normal push setup
344         if (!self.speed)
345                 self.speed = 1000;
346         self.movedir = self.movedir * self.speed * 10;
347
348         if (!self.noise)
349                 self.noise = "misc/jumppad.wav";
350         precache_sound (self.noise);
351
352         // this must be called to spawn the teleport waypoints for bots
353         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
354 }
355
356 void spawnfunc_target_push() {}
357 void spawnfunc_info_notnull() {}
358 void spawnfunc_target_position() {}