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