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