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