]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_jumppads.qc
new jumppad code - working so it's used now
[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;
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         
67         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
68         if(ht < 0)
69                 if(zdist < 0)
70                         vz = -vz;
71         
72         vector solution;
73         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
74         // ALWAYS solvable because jumpheight >= zdist
75         if(!solution_z)
76                 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)
77         if(zdist == 0)
78                 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)
79         
80         if(zdist < 0)
81         {
82                 // down-jump
83                 if(ht < 0)
84                 {
85                         // almost straight line type
86                         // jump apex is before the jump
87                         // we must take the larger one
88                         trigger_push_calculatevelocity_flighttime = solution_y;
89                 }
90                 else
91                 {
92                         // regular jump
93                         // jump apex is during the jump
94                         // we must take the larger one too
95                         trigger_push_calculatevelocity_flighttime = solution_y;
96                 }
97         }
98         else
99         {
100                 // up-jump
101                 if(ht < 0)
102                 {
103                         // almost straight line type
104                         // jump apex is after the jump
105                         // we must take the smaller one
106                         trigger_push_calculatevelocity_flighttime = solution_x;
107                 }
108                 else
109                 {
110                         // regular jump
111                         // jump apex is during the jump
112                         // we must take the larger one
113                         trigger_push_calculatevelocity_flighttime = solution_y;
114                 }
115         }
116         vs = sdist / trigger_push_calculatevelocity_flighttime;
117
118         // finally calculate the velocity
119         return sdir * vs + '0 0 1' * vz;
120 }
121
122 void trigger_push_touch()
123 {
124         // FIXME: add a .float for whether an entity should be tossed by jumppads
125         if (!other.iscreature)
126         if (other.classname != "corpse")
127         if (other.classname != "body")
128         if (other.classname != "gib")
129         if (other.classname != "casing")
130         if (other.classname != "droppedweapon")
131         if (!other.projectiledeathtype || other.classname == "bullet")
132                 return;
133
134         if (other.deadflag && other.iscreature)
135                 return;
136
137         EXACTTRIGGER_TOUCH;
138
139         if(self.target)
140                 self.movedir = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
141
142         other.flags &~= FL_ONGROUND;
143         // reset tracking of oldvelocity for impact damage (sudden velocity changes)
144         other.oldvelocity = other.velocity = self.movedir;
145
146         UpdateCSQCProjectile(other);
147
148         if (other.classname == "player")
149         {
150                 if(self.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
151                 {
152                         // flash when activated
153                         pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
154                         sound (other, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NORM);
155                         self.pushltime = time + 0.2;
156                 }
157                 if(clienttype(other) == CLIENTTYPE_REAL)
158                 {
159                         local float i;
160                         local float found;
161                         found = FALSE;
162                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
163                                 if(other.(jumppadsused[i]) == self)
164                                         found = TRUE;
165                         if(!found)
166                         {
167                                 other.(jumppadsused[mod(other.jumppadcount, NUM_JUMPPADSUSED)]) = self;
168                                 other.jumppadcount = other.jumppadcount + 1;
169                         }
170
171                         if(self.message)
172                                 centerprint(other, self.message);
173                 }
174                 else
175                         other.jumppadcount = TRUE;
176         }
177
178         if(self.enemy.target)
179         {
180                 entity oldself;
181                 oldself = self;
182                 activator = other;
183                 self = self.enemy;
184                 SUB_UseTargets();
185                 self = oldself;
186         }
187
188         // reset tracking of who pushed you into a hazard (for kill credit)
189         other.pushltime = 0;
190
191         if (other.flags & FL_PROJECTILE)
192                 other.angles = vectoangles (other.velocity);
193
194         if (self.spawnflags & PUSH_ONCE)
195         {
196                 self.touch = SUB_Null;
197                 self.think = SUB_Remove;
198                 self.nextthink = time;
199         }
200 };
201
202 .vector dest;
203
204 void trigger_push_findtarget()
205 {
206         local entity e;
207         local vector org;
208         local float flighttime;
209
210         // first calculate a typical start point for the jump
211         org = (self.absmin + self.absmax) * 0.5;
212         org_z = self.absmax_z - PL_MIN_z;
213
214         if (self.target)
215         {
216                 // find the target
217                 self.enemy = find(world, targetname, self.target);
218                 if (!self.enemy)
219                 {
220                         objerror("trigger_push: target not found\n");
221                         remove(self);
222                         return;
223                 }
224
225                 self.movedir = trigger_push_calculatevelocity(org, self.enemy, self.height);
226                 flighttime = trigger_push_calculatevelocity_flighttime;
227         }
228         else
229                 flighttime = 0;
230
231         // calculate the destination and spawn a teleporter spawnfunc_waypoint
232         e = spawn();
233         setorigin(e, org);
234         setsize(e, PL_MIN, PL_MAX);
235         e.velocity = self.movedir;
236         tracetoss(e, e);
237         self.dest = trace_endpos;
238         remove(e);
239
240         waypoint_spawnforteleporter(self, self.dest, flighttime);
241 };
242
243 /*
244  * ENTITY PARAMETERS:
245  *
246  *   target:  target of jump
247  *   height:  the absolute value is the height of the highest point of the jump
248  *            trajectory above the higher one of the player and the target.
249  *            the sign indicates whether the highest point is INSIDE (positive)
250  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
251  *            positive values for targets mounted on the floor, and use negative
252  *            values to target a point on the ceiling.
253  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
254  */
255 void spawnfunc_trigger_push()
256 {
257         if (self.angles != '0 0 0')
258                 SetMovedir ();
259
260         EXACTTRIGGER_INIT;
261
262         self.touch = trigger_push_touch;
263
264         // normal push setup
265         if (!self.speed)
266                 self.speed = 1000;
267         self.movedir = self.movedir * self.speed * 10;
268
269         if not(self.noise)
270                 self.noise = "misc/jumppad.wav";
271         precache_sound (self.noise);
272
273         // this must be called to spawn the teleport waypoints for bots
274         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
275 };
276
277 void spawnfunc_target_push() {};
278 void spawnfunc_info_notnull() {};
279 void spawnfunc_target_position() {};