]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_jumppads.qc
New parameter for jump pads: height.
[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 float trigger_push_calculatevelocity_flighttime;
8
9 /*
10         trigger_push_calculatevelocity
11
12         Arguments:
13           org - origin of the object which is to be pushed
14           tgt - target entity (can be either a point or a model entity; if it is
15                 the latter, its midpoint is used)
16           ht  - jump height, measured from the higher one of org and tgt's midpoint
17     
18         Returns: velocity for the jump
19         the global trigger_push_calculatevelocity_flighttime is set to the total
20         jump time
21  */
22
23 vector(vector org, entity tgt, float ht) trigger_push_calculatevelocity =
24 {
25         local float grav, sdist, zdist, vs, vz, jumpheight, trajsign;
26         local vector sdir, torg;
27
28         torg = tgt.origin;
29         if(tgt.model)
30                 torg = torg + (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         dprint("trigger_push_calculatevelocity_flighttime: "); dprint(ftos(trigger_push_calculatevelocity_flighttime)); dprint("\n");
101
102         // finally calculate the velocity
103         return sdir * vs + '0 0 1' * vz;
104 }
105
106 void() trigger_push_touch =
107 {
108         // FIXME: add a .float for whether an entity should be tossed by jumppads
109         if (other.classname != "player")
110         if (other.classname != "corpse")
111         if (other.classname != "body")
112         if (other.classname != "gib")
113         if (other.classname != "missile")
114         if (other.classname != "casing")
115         if (other.classname != "grenade")
116         if (other.classname != "plasma")
117         if (other.classname != "plasma_prim")
118         if (other.classname != "plasma_chain")
119         if (other.classname != "droppedweapon")
120                 return;
121
122         if (other.deadflag && other.classname == "player")
123                 return;
124
125         if (!self.target)
126         {
127                 other.velocity = self.movedir;
128                 other.flags = other.flags - (other.flags & FL_ONGROUND);
129                 return;
130         }
131
132         if (other.classname == "player")
133         {
134                 local float i;
135                 local float found;
136                 if(self.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
137                 {
138                         sound (other, CHAN_ITEM, "misc/jumppad.ogg", 1, ATTN_NORM);
139                         self.pushltime = time + 0.5;
140                 }
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[math_mod(other.jumppadcount, NUM_JUMPPADSUSED)]) = self;
148                         other.jumppadcount = other.jumppadcount + 1;
149                 }
150         }
151
152         self.movedir = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
153
154         other.flags = other.flags - (other.flags & FL_ONGROUND);
155         // reset tracking of oldvelocity for impact damage (sudden velocity changes)
156         other.oldvelocity = other.velocity = self.movedir;
157         // reset tracking of who pushed you into a hazard (for kill credit)
158         other.pushltime = 0;
159
160         if (other.classname == "missile")
161                 other.angles = vectoangles (other.velocity);
162
163         if (self.spawnflags & PUSH_ONCE)
164         {
165                 self.touch = SUB_Null;
166                 self.think = SUB_Remove;
167                 self.nextthink = time;
168         }
169 };
170
171 .vector dest;
172
173 void() trigger_push_findtarget =
174 {
175         local entity e;
176         local vector org;
177         local float flighttime;
178
179         // first calculate a typical start point for the jump
180         org = (self.absmin + self.absmax) * 0.5;
181         org_z = self.absmax_z - PL_MIN_z;
182
183         if (self.target)
184         {
185                 // find the target
186                 self.enemy = find(world, targetname, self.target);
187                 if (!self.enemy)
188                 {
189                         objerror("trigger_push: target not found\n");
190                         remove(self);
191                         return;
192                 }
193
194                 self.movedir = trigger_push_calculatevelocity(org, self.enemy, self.height);
195                 flighttime = trigger_push_calculatevelocity_flighttime;
196         }
197         else
198                 flighttime = 0;
199
200         // calculate the destination and spawn a teleporter waypoint
201         e = spawn();
202         setorigin(e, org);
203         setsize(e, PL_MIN, PL_MAX);
204         e.velocity = self.movedir;
205         tracetoss(e, e);
206         self.dest = trace_endpos;
207         remove(e);
208
209         waypoint_spawnforteleporter(self, self.dest, flighttime);
210 };
211
212 /*
213  * ENTITY PARAMETERS:
214  *
215  *   target:  target of jump
216  *   height:  the absolute value is the height of the highest point of the jump
217  *            trajectory above the higher one of the player and the target.
218  *            the sign indicates whether the highest point is INSIDE (positive)
219  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
220  *            positive values for targets mounted on the floor, and use negative
221  *            values to target a point on the ceiling.
222  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
223  */
224 void() trigger_push =
225 {
226         if (self.angles != '0 0 0')
227                 SetMovedir ();
228
229         self.solid = SOLID_TRIGGER;
230         setmodel (self, self.model);
231         self.movetype = MOVETYPE_NONE;
232         self.modelindex = 0;
233         self.model = "";
234
235         self.touch = trigger_push_touch;
236
237         // normal push setup
238         if (!self.speed)
239                 self.speed = 1000;
240         self.movedir = self.movedir * self.speed * 10;
241
242         // this must be called to spawn the teleport waypoints for bots
243         self.think = trigger_push_findtarget;
244         self.nextthink = time + 0.2;
245 };
246
247 void() target_push = {};
248 void() info_notnull = {};
249 void() target_position = {};