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