change all function declarations (except builtins and data types) to ANSI C-like...
[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 trigger_push_calculatevelocity(vector org, entity tgt, float ht)
24 {
25         local float grav, sdist, zdist, vs, vz, jumpheight, trajsign;
26         local vector sdir, torg;
27
28         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
29
30         grav = cvar("sv_gravity");
31
32         zdist = torg_z - org_z;
33         sdist = vlen(torg - org - zdist * '0 0 1');
34         sdir = normalize(torg - org - zdist * '0 0 1');
35
36         // how high do we need to push the player?
37         jumpheight = fabs(ht);
38         if(zdist > 0)
39                 jumpheight = jumpheight + zdist;
40
41         /*
42                 STOP.
43
44                 You will not understand the following equations anyway...
45                 But here is what I did to get them.
46
47                 I used the functions
48
49                   s(t) = t * vs
50                   z(t) = t * vz - 1/2 grav t^2
51
52                 and solved for:
53
54                   s(ti) = sdist
55                   z(ti) = zdist
56                   max(z, ti) = jumpheight
57
58                 From these three equations, you will find the three parameters vs, vz
59                 and ti.
60          */
61
62         // push him so high...
63         vz = sqrt(2 * grav * jumpheight); // NOTE: sqrt(positive)!
64         if(ht < 0)
65                 if(zdist < 0)
66                         vz = -vz;
67
68         // how far to push him?
69         if(zdist == 0)
70         {
71                 trigger_push_calculatevelocity_flighttime = sqrt(jumpheight * 8 / grav);
72                 vs = sdist / trigger_push_calculatevelocity_flighttime;
73                         // trajsign is ignored (the high point MUST be inside the jump!)
74         }
75         else
76         {
77                 if(ht > 0)
78                         trajsign = +1;
79                 else
80                         trajsign = -1;
81
82                 // >0: the lower speed that achieves "it"
83                 //     (parabola's maximum inside the jump)
84                 // <0: the higher speed that achieves "it"
85                 //     (parabola's maximum outside the jump)
86
87                 vs = sqrt(jumpheight - zdist);
88                 vs = sqrt(jumpheight) - trajsign * vs; // fteqcc sucks
89                 vs = fabs(vs * sqrt(grav/2) / zdist);
90                 //vs = fabs((sdist / zdist) * sqrt(grav/2) * (sqrt(jumpheight) - trajsign * sqrt(jumpheight - zdist)));
91                 trigger_push_calculatevelocity_flighttime = 1 / vs;
92                         // note: vs cannot be zero here. The difference between the sqrts is zero IFF zdist == 0, which we have excluded.
93                 vs = vs * sdist;
94
95                 // cases to test: "jump up", "jump down" with positive and negative height
96         }
97
98         // finally calculate the velocity
99         return sdir * vs + '0 0 1' * vz;
100 }
101
102 void trigger_push_touch()
103 {
104         // FIXME: add a .float for whether an entity should be tossed by jumppads
105         if (other.classname != "player")
106         if (other.classname != "corpse")
107         if (other.classname != "body")
108         if (other.classname != "gib")
109         if (other.classname != "missile")
110         if (other.classname != "casing")
111         if (other.classname != "grenade")
112         if (other.classname != "plasma")
113         if (other.classname != "plasma_prim")
114         if (other.classname != "plasma_chain")
115         if (other.classname != "droppedweapon")
116                 return;
117
118         if (other.deadflag && other.classname == "player")
119                 return;
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_ITEM, self.noise, 1, 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                 else
152                         other.jumppadcount = TRUE;
153
154                 if(self.message)
155                         centerprint(other, self.message);
156         }
157
158         // reset tracking of who pushed you into a hazard (for kill credit)
159         other.pushltime = 0;
160
161         if (other.flags & FL_PROJECTILE)
162                 other.angles = vectoangles (other.velocity);
163
164         if (self.spawnflags & PUSH_ONCE)
165         {
166                 self.touch = SUB_Null;
167                 self.think = SUB_Remove;
168                 self.nextthink = time;
169         }
170 };
171
172 .vector dest;
173
174 void trigger_push_findtarget()
175 {
176         local entity e;
177         local vector org;
178         local float flighttime;
179
180         // first calculate a typical start point for the jump
181         org = (self.absmin + self.absmax) * 0.5;
182         org_z = self.absmax_z - PL_MIN_z;
183
184         if (self.target)
185         {
186                 // find the target
187                 self.enemy = find(world, targetname, self.target);
188                 if (!self.enemy)
189                 {
190                         objerror("trigger_push: target not found\n");
191                         remove(self);
192                         return;
193                 }
194
195                 self.movedir = trigger_push_calculatevelocity(org, self.enemy, self.height);
196                 flighttime = trigger_push_calculatevelocity_flighttime;
197         }
198         else
199                 flighttime = 0;
200
201         // calculate the destination and spawn a teleporter spawnfunc_waypoint
202         e = spawn();
203         setorigin(e, org);
204         setsize(e, PL_MIN, PL_MAX);
205         e.velocity = self.movedir;
206         tracetoss(e, e);
207         self.dest = trace_endpos;
208         remove(e);
209
210         waypoint_spawnforteleporter(self, self.dest, flighttime);
211 };
212
213 /*
214  * ENTITY PARAMETERS:
215  *
216  *   target:  target of jump
217  *   height:  the absolute value is the height of the highest point of the jump
218  *            trajectory above the higher one of the player and the target.
219  *            the sign indicates whether the highest point is INSIDE (positive)
220  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
221  *            positive values for targets mounted on the floor, and use negative
222  *            values to target a point on the ceiling.
223  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
224  */
225 void spawnfunc_trigger_push()
226 {
227         if (self.angles != '0 0 0')
228                 SetMovedir ();
229
230         self.solid = SOLID_TRIGGER;
231         setmodel (self, self.model); // no precision needed
232         self.movetype = MOVETYPE_NONE;
233         self.modelindex = 0;
234         self.model = "";
235
236         self.touch = trigger_push_touch;
237
238         // normal push setup
239         if (!self.speed)
240                 self.speed = 1000;
241         self.movedir = self.movedir * self.speed * 10;
242
243         if not(self.noise)
244                 self.noise = "misc/jumppad.wav";
245         precache_sound (self.noise);
246
247         // this must be called to spawn the teleport waypoints for bots
248         self.think = trigger_push_findtarget;
249         self.nextthink = time + 0.2;
250 };
251
252 void spawnfunc_target_push() {};
253 void spawnfunc_info_notnull() {};
254 void spawnfunc_target_position() {};