]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_jumppads.qc
Unify boolean constants
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / t_jumppads.qc
1 const float PUSH_ONCE                   = 1;
2 const 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(fabs(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 (IS_PLAYER(other))
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, ATTEN_NORM);
179                         self.pushltime = time + 0.2;
180                 }
181                 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
182                 {
183                         float i;
184                         float found;
185                         found = false;
186                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
187                                 if(other.(jumppadsused[i]) == self)
188                                         found = true;
189                         if(!found)
190                         {
191                                 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
192                                 other.jumppadcount = other.jumppadcount + 1;
193                         }
194
195                         if(IS_REAL_CLIENT(other))
196                         {
197                                 if(self.message)
198                                         centerprint(other, self.message);
199                         }
200                         else
201                                 other.lastteleporttime = time;
202
203                         if (other.deadflag == DEAD_NO)
204                                 animdecide_setaction(other, ANIMACTION_JUMP, true);
205                 }
206                 else
207                         other.jumppadcount = true;
208
209                 // reset tracking of who pushed you into a hazard (for kill credit)
210                 other.pushltime = 0;
211                 other.istypefrag = 0;
212         }
213
214         if(self.enemy.target)
215         {
216                 entity oldself;
217                 oldself = self;
218                 activator = other;
219                 self = self.enemy;
220                 SUB_UseTargets();
221                 self = oldself;
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 }
248
249 .vector dest;
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() {}