]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_jumppads.qc
allow multiple targets for jumppads too (random selection)
[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         local float grav, sdist, zdist, vs, vz, jumpheight;
34         local 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         vector move;
131
132         if (self.active == ACTIVE_NOT)
133                 return;
134
135         if (!isPushable(other))
136                 return;
137
138         if(self.team)
139                 if((self.spawnflags & 4 == 0) == (self.team != other.team))
140                         return;
141
142         EXACTTRIGGER_TOUCH;
143
144         if(self.enemy)
145         {
146                 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
147         }
148         else if(self.target)
149         {
150                 entity e;
151                 RandomSelection_Init();
152                 for(e = world; (e = find(e, targetname, self.target)); )
153                 {
154                         if(e.cnt)
155                                 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
156                         else
157                                 RandomSelection_Add(e, 0, string_null, 1, 1);
158                 }
159                 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
160         }
161         else
162         {
163                 other.velocity = self.movedir;
164         }
165
166         other.flags &~= FL_ONGROUND;
167
168         if (other.classname == "player")
169         {
170                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
171                 other.oldvelocity = other.velocity;
172
173                 if(self.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
174                 {
175                         // flash when activated
176                         pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
177                         sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NORM);
178                         self.pushltime = time + 0.2;
179                 }
180                 local float ct;
181                 ct = clienttype(other);
182                 if( ct == CLIENTTYPE_REAL || ct == CLIENTTYPE_BOT)
183                 {
184                         local float i;
185                         local float found;
186                         found = FALSE;
187                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
188                                 if(other.(jumppadsused[i]) == self)
189                                         found = TRUE;
190                         if(!found)
191                         {
192                                 other.(jumppadsused[mod(other.jumppadcount, NUM_JUMPPADSUSED)]) = self;
193                                 other.jumppadcount = other.jumppadcount + 1;
194                         }
195
196                         if(ct == CLIENTTYPE_REAL)
197                         {
198                                 if(self.message)
199                                         centerprint(other, self.message);
200                         }
201                         else
202                                 other.lastteleporttime = time;
203                 }
204                 else
205                         other.jumppadcount = TRUE;
206
207                 // reset tracking of who pushed you into a hazard (for kill credit)
208                 other.pushltime = 0;
209         }
210
211         if(self.enemy.target)
212         {
213                 entity oldself;
214                 oldself = self;
215                 activator = other;
216                 self = self.enemy;
217                 SUB_UseTargets();
218                 self = oldself;
219         }
220
221         if (other.flags & FL_PROJECTILE)
222         {
223                 other.angles = vectoangles (other.velocity);
224                 switch(other.movetype)
225                 {
226                         case MOVETYPE_FLY:
227                                 other.movetype = MOVETYPE_TOSS;
228                                 other.gravity = 1;
229                                 break;
230                         case MOVETYPE_BOUNCEMISSILE:
231                                 other.movetype = MOVETYPE_BOUNCE;
232                                 other.gravity = 1;
233                                 break;
234                 }
235                 UpdateCSQCProjectile(other);
236         }
237
238         if (self.spawnflags & PUSH_ONCE)
239         {
240                 self.touch = SUB_Null;
241                 self.think = SUB_Remove;
242                 self.nextthink = time;
243         }
244 };
245
246 .vector dest;
247 void trigger_push_findtarget()
248 {
249         local entity e;
250         local vector org;
251         local float flighttime;
252
253         // first calculate a typical start point for the jump
254         org = (self.absmin + self.absmax) * 0.5;
255         org_z = self.absmax_z - PL_MIN_z;
256
257         if (self.target)
258         {
259                 float n;
260                 n = 0;
261                 for(e = world; (e = find(e, targetname, self.target)); )
262                 {
263                         ++n;
264
265                         trigger_push_calculatevelocity(org, self.enemy, self.height);
266                         flighttime = trigger_push_calculatevelocity_flighttime;
267
268                         if(e.movetype == MOVETYPE_NONE)
269                                 waypoint_spawnforteleporter(self, e.origin, flighttime);
270                 }
271
272                 if(n == 0)
273                 {
274                         // no dest!
275                         objerror ("Jumppad with nonexistant target");
276                         return;
277                 }
278                 else if(n == 1)
279                 {
280                         // exactly one dest - bots love that
281                         self.enemy = find(e, targetname, self.target);
282                 }
283                 else
284                 {
285                         // have to use random selection every single time
286                         self.enemy = world;
287                 }
288         }
289         else
290         {
291                 e = spawn();
292                 setorigin(e, org);
293                 setsize(e, PL_MIN, PL_MAX);
294                 e.velocity = self.movedir;
295                 tracetoss(e, e);
296                 self.dest = trace_endpos;
297                 remove(e);
298                 waypoint_spawnforteleporter(self, self.dest, vlen(self.dest - org) / vlen(self.movedir));
299         }
300 };
301
302 /*
303  * ENTITY PARAMETERS:
304  *
305  *   target:  target of jump
306  *   height:  the absolute value is the height of the highest point of the jump
307  *            trajectory above the higher one of the player and the target.
308  *            the sign indicates whether the highest point is INSIDE (positive)
309  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
310  *            positive values for targets mounted on the floor, and use negative
311  *            values to target a point on the ceiling.
312  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
313  */
314 void spawnfunc_trigger_push()
315 {
316         SetMovedir ();
317
318         EXACTTRIGGER_INIT;
319
320         self.active = ACTIVE_ACTIVE;
321         self.use = trigger_push_use;
322         self.touch = trigger_push_touch;
323
324         // normal push setup
325         if (!self.speed)
326                 self.speed = 1000;
327         self.movedir = self.movedir * self.speed * 10;
328
329         if not(self.noise)
330                 self.noise = "misc/jumppad.wav";
331         precache_sound (self.noise);
332
333         // this must be called to spawn the teleport waypoints for bots
334         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
335 };
336
337 void spawnfunc_target_push() {};
338 void spawnfunc_info_notnull() {};
339 void spawnfunc_target_position() {};