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