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