]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/t_jumppads.qc
Merge branch 'master' of ssh://git.xonotic.org/xonotic-data.pk3dir into savagex/plat...
[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(teams_matter)
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         if (self.active == ACTIVE_NOT)
131                 return;
132
133         // FIXME: add a .float for whether an entity should be tossed by jumppads
134         if (!other.iscreature)
135         if (other.classname != "corpse")
136         if (other.classname != "body")
137         if (other.classname != "gib")
138         if (other.classname != "casing")
139         if (other.classname != "droppedweapon")
140         if (other.classname != "keepawayball")
141         if (!other.projectiledeathtype || other.classname == "bullet")
142                 return;
143
144         if (other.deadflag && other.iscreature)
145                 return;
146
147         if(self.team)
148                 if((self.spawnflags & 4 == 0) == (self.team != other.team))
149                         return;
150
151         EXACTTRIGGER_TOUCH;
152
153         if(self.target)
154                 self.movedir = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
155
156         other.flags &~= FL_ONGROUND;
157
158         other.velocity = self.movedir;
159
160         if (other.classname == "player")
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, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NORM);
170                         self.pushltime = time + 0.2;
171                 }
172                 local float ct;
173                 ct = clienttype(other);
174                 if( ct == CLIENTTYPE_REAL || ct == CLIENTTYPE_BOT)
175                 {
176                         local float i;
177                         local float found;
178                         found = FALSE;
179                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
180                                 if(other.(jumppadsused[i]) == self)
181                                         found = TRUE;
182                         if(!found)
183                         {
184                                 other.(jumppadsused[mod(other.jumppadcount, NUM_JUMPPADSUSED)]) = self;
185                                 other.jumppadcount = other.jumppadcount + 1;
186                         }
187
188                         if(ct == CLIENTTYPE_REAL)
189                         {
190                                 if(self.message)
191                                         centerprint(other, self.message);
192                         }
193                         else
194                                 other.lastteleporttime = time;
195                 }
196                 else
197                         other.jumppadcount = TRUE;
198
199                 // reset tracking of who pushed you into a hazard (for kill credit)
200                 other.pushltime = 0;
201         }
202
203         if(self.enemy.target)
204         {
205                 entity oldself;
206                 oldself = self;
207                 activator = other;
208                 self = self.enemy;
209                 SUB_UseTargets();
210                 self = oldself;
211         }
212
213         if (other.flags & FL_PROJECTILE)
214         {
215                 other.angles = vectoangles (other.velocity);
216                 switch(other.movetype)
217                 {
218                         case MOVETYPE_FLY:
219                                 other.movetype = MOVETYPE_TOSS;
220                                 other.gravity = 1;
221                                 break;
222                         case MOVETYPE_BOUNCEMISSILE:
223                                 other.movetype = MOVETYPE_BOUNCE;
224                                 other.gravity = 1;
225                                 break;
226                 }
227                 UpdateCSQCProjectile(other);
228         }
229
230         if (self.spawnflags & PUSH_ONCE)
231         {
232                 self.touch = SUB_Null;
233                 self.think = SUB_Remove;
234                 self.nextthink = time;
235         }
236 };
237
238 .vector dest;
239 void trigger_push_findtarget()
240 {
241         local entity e;
242         local vector org;
243         local float flighttime;
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                 // find the target
252                 self.enemy = find(world, targetname, self.target);
253                 if (!self.enemy)
254                 {
255                         objerror("trigger_push: target not found\n");
256                         remove(self);
257                         return;
258                 }
259
260                 self.movedir = trigger_push_calculatevelocity(org, self.enemy, self.height);
261                 flighttime = trigger_push_calculatevelocity_flighttime;
262         }
263         else
264                 flighttime = 0;
265
266         // calculate the destination and spawn a teleporter spawnfunc_waypoint
267         e = spawn();
268         setorigin(e, org);
269         setsize(e, PL_MIN, PL_MAX);
270         e.velocity = self.movedir;
271         tracetoss(e, e);
272         self.dest = trace_endpos;
273         remove(e);
274
275         waypoint_spawnforteleporter(self, self.dest, flighttime);
276 };
277
278 /*
279  * ENTITY PARAMETERS:
280  *
281  *   target:  target of jump
282  *   height:  the absolute value is the height of the highest point of the jump
283  *            trajectory above the higher one of the player and the target.
284  *            the sign indicates whether the highest point is INSIDE (positive)
285  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
286  *            positive values for targets mounted on the floor, and use negative
287  *            values to target a point on the ceiling.
288  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
289  */
290 void spawnfunc_trigger_push()
291 {
292         SetMovedir ();
293
294         EXACTTRIGGER_INIT;
295
296         self.active = ACTIVE_ACTIVE;
297         self.use = trigger_push_use;
298         self.touch = trigger_push_touch;
299
300         // normal push setup
301         if (!self.speed)
302                 self.speed = 1000;
303         self.movedir = self.movedir * self.speed * 10;
304
305         if not(self.noise)
306                 self.noise = "misc/jumppad.wav";
307         precache_sound (self.noise);
308
309         // this must be called to spawn the teleport waypoints for bots
310         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
311 };
312
313 void spawnfunc_target_push() {};
314 void spawnfunc_info_notnull() {};
315 void spawnfunc_target_position() {};