]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_jumppads.qc
17b574d1584ef007fef730ca48bccde48cb5ceff
[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                         float i;
175                         float found;
176                         found = false;
177                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
178                                 if(other.(jumppadsused[i]) == self)
179                                         found = true;
180                         if(!found)
181                         {
182                                 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
183                                 other.jumppadcount = other.jumppadcount + 1;
184                         }
185
186                         if(IS_REAL_CLIENT(other))
187                         {
188                                 if(self.message)
189                                         centerprint(other, self.message);
190                         }
191                         else
192                                 other.lastteleporttime = time;
193
194                         if (other.deadflag == DEAD_NO)
195                                 animdecide_setaction(other, ANIMACTION_JUMP, true);
196                 }
197                 else
198                         other.jumppadcount = true;
199
200                 // reset tracking of who pushed you into a hazard (for kill credit)
201                 other.pushltime = 0;
202                 other.istypefrag = 0;
203         }
204
205         if(self.enemy.target)
206         {
207                 entity oldself;
208                 oldself = self;
209                 activator = other;
210                 self = self.enemy;
211                 SUB_UseTargets();
212                 self = oldself;
213         }
214
215         if (other.flags & FL_PROJECTILE)
216         {
217                 other.angles = vectoangles (other.velocity);
218                 switch(other.movetype)
219                 {
220                         case MOVETYPE_FLY:
221                                 other.movetype = MOVETYPE_TOSS;
222                                 other.gravity = 1;
223                                 break;
224                         case MOVETYPE_BOUNCEMISSILE:
225                                 other.movetype = MOVETYPE_BOUNCE;
226                                 other.gravity = 1;
227                                 break;
228                 }
229                 UpdateCSQCProjectile(other);
230         }
231
232         if (self.spawnflags & PUSH_ONCE)
233         {
234                 self.touch = func_null;
235                 self.think = SUB_Remove;
236                 self.nextthink = time;
237         }
238 }
239
240 void trigger_push_findtarget()
241 {
242         entity e, t;
243         vector org;
244
245         // first calculate a typical start point for the jump
246         org = (self.absmin + self.absmax) * 0.5;
247         org_z = self.absmax.z - PL_MIN_z;
248
249         if (self.target)
250         {
251                 float n;
252                 n = 0;
253                 for(t = world; (t = find(t, targetname, self.target)); )
254                 {
255                         ++n;
256                         e = spawn();
257                         setorigin(e, org);
258                         setsize(e, PL_MIN, PL_MAX);
259                         e.velocity = trigger_push_calculatevelocity(org, t, self.height);
260                         tracetoss(e, e);
261                         if(e.movetype == MOVETYPE_NONE)
262                                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
263                         remove(e);
264                 }
265
266                 if(n == 0)
267                 {
268                         // no dest!
269                         objerror ("Jumppad with nonexistant target");
270                         return;
271                 }
272                 else if(n == 1)
273                 {
274                         // exactly one dest - bots love that
275                         self.enemy = find(world, targetname, self.target);
276                 }
277                 else
278                 {
279                         // have to use random selection every single time
280                         self.enemy = world;
281                 }
282         }
283         else
284         {
285                 e = spawn();
286                 setorigin(e, org);
287                 setsize(e, PL_MIN, PL_MAX);
288                 e.velocity = self.movedir;
289                 tracetoss(e, e);
290                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
291                 remove(e);
292         }
293 }
294
295 /*
296  * ENTITY PARAMETERS:
297  *
298  *   target:  target of jump
299  *   height:  the absolute value is the height of the highest point of the jump
300  *            trajectory above the higher one of the player and the target.
301  *            the sign indicates whether the highest point is INSIDE (positive)
302  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
303  *            positive values for targets mounted on the floor, and use negative
304  *            values to target a point on the ceiling.
305  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
306  */
307 void spawnfunc_trigger_push()
308 {
309         SetMovedir ();
310
311         EXACTTRIGGER_INIT;
312
313         self.active = ACTIVE_ACTIVE;
314         self.use = trigger_push_use;
315         self.touch = trigger_push_touch;
316
317         // normal push setup
318         if (!self.speed)
319                 self.speed = 1000;
320         self.movedir = self.movedir * self.speed * 10;
321
322         if (!self.noise)
323                 self.noise = "misc/jumppad.wav";
324         precache_sound (self.noise);
325
326         // this must be called to spawn the teleport waypoints for bots
327         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
328 }
329
330 void spawnfunc_target_push() {}
331 void spawnfunc_info_notnull() {}
332 void spawnfunc_target_position() {}