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