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