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