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