]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
fix timelimit display after ready-restart (no longer 20.166666667)
[divverent/nexuiz.git] / data / qcsrc / server / g_hook.qc
1 /*============================================
2
3       Wazat's Nexuiz Grappling Hook
4
5         Contact: Wazat1@gmail.com
6
7
8 Installation instructions:
9 --------------------------
10
11 1. Place hook.c in your gamec source directory with the other source files.
12
13 2. Add this line to the bottom of progs.src:
14
15 gamec/hook.c
16
17 3. Open defs.h and add these lines to the very bottom:
18
19 // Wazat's grappling hook
20 .entity         hook;
21 void GrapplingHookFrame();
22 void RemoveGrapplingHook(entity pl);
23 void SetGrappleHookBindings();
24 // hook impulses
25 float GRAPHOOK_FIRE             = 20;
26 float GRAPHOOK_RELEASE          = 21;
27 // (note: you can change the hook impulse #'s to whatever you please)
28
29 4. Open client.c and add this to the top of PutClientInServer():
30
31         RemoveGrapplingHook(self); // Wazat's Grappling Hook
32
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
34
35         // Wazat's grappling hook
36         SetGrappleHookBindings();
37
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
39
40         GrapplingHookFrame();
41
42 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
43 bind ctrl "+hook"
44
45 And you should be done!
46
47
48 ============================================*/
49
50 .string aiment_classname;
51 .float aiment_deadflag;
52 void SetMovetypeFollow(entity ent, entity e)
53 {
54         ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
55         ent.solid = SOLID_NOT; // MOVETYPE_FOLLOW is always non-solid
56         ent.aiment = e; // make the hole follow bmodel
57         ent.punchangle = e.angles; // the original angles of bmodel
58         ent.view_ofs = ent.origin - e.origin; // relative origin
59         ent.v_angle = ent.angles - e.angles; // relative angles
60         ent.aiment_classname = strzone(e.classname);
61         ent.aiment_deadflag = e.deadflag;
62 }
63 void UnsetMovetypeFollow(entity ent)
64 {
65         ent.movetype = MOVETYPE_FLY;
66         ent.solid = SOLID_BBOX;
67         ent.aiment = world;
68 }
69 float LostMovetypeFollow(entity ent)
70 {
71         if(ent.aiment)
72         {
73                 if(ent.aiment.classname != ent.aiment_classname)
74                         return 1;
75                 if(ent.aiment.deadflag != ent.aiment_deadflag)
76                         return 1;
77         }
78         return 0;
79 }
80
81 .float rope_length;
82 .float button6_pressed_before;
83
84 void RemoveGrapplingHook(entity pl)
85 {
86         if(pl.hook == world)
87                 return;
88         remove(pl.hook);
89         pl.hook = world;
90         if(pl.movetype == MOVETYPE_FLY)
91                 pl.movetype = MOVETYPE_WALK;
92
93         pl.hook_time = time + 0.0;
94
95         //pl.disableclientprediction = FALSE;
96 }
97
98 void GrapplingHookThink();
99 void GrapplingHook_Stop()
100 {
101         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
102         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
103
104         self.state = 1;
105         self.think = GrapplingHookThink;
106         self.nextthink = time;
107         self.touch = SUB_Null;
108         self.velocity = '0 0 0';
109         self.movetype = MOVETYPE_NONE;
110         self.rope_length = -1;
111 }
112
113 void GrapplingHookThink()
114 {
115         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
116         vector dir, org, end, v0, dv;
117         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
118         {                                                                                                               // well, better fix it anyway
119                 remove(self);
120                 return;
121         }
122         if(LostMovetypeFollow(self))
123         {
124                 RemoveGrapplingHook(self.owner);
125                 return;
126         }
127
128         self.nextthink = time;
129
130         makevectors(self.owner.v_angle);
131         org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
132
133 #if 0
134         tracebox(org, self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self.owner);
135         // do not hit players with this, as they tend to get in the way just too often
136         // NOTE: this assumes sky brushes cannot get in the way
137         // if they can, assume the map is broken! :P
138         if(trace_fraction < 1 && (!self.aiment || trace_ent != self.aiment))
139         {
140                 // 0. stop it
141                 if(self.state != 1)
142                         GrapplingHook_Stop();
143
144                 // 1. detach the hook
145                 if(self.aiment)
146                         UnsetMovetypeFollow(self);
147
148                 // 2. cut it off
149                 self.origin = trace_endpos;
150
151                 // 3. reattach the hook
152                 if(trace_ent)
153                         if(trace_ent.movetype != MOVETYPE_NONE)
154                                 SetMovetypeFollow(self, trace_ent);
155         }
156 #endif
157
158         if(self.rope_length < 0)
159                 self.rope_length = vlen(org - self.origin);
160
161         if(self.state == 1)
162         {
163                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
164                 // speed the rope is pulled with
165
166                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
167                 // force the rope will use if it is stretched
168
169                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
170                 // force the rope will use if it is stretched
171
172                 minlength = cvar("g_balance_grapplehook_length_min");//100;
173                 // minimal rope length
174                 // if the rope goes below this length, it isn't pulled any more
175
176                 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
177                 // if the rope is stretched by more than this amount, more rope is
178                 // given to you again
179
180                 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
181                 // while hanging on the rope, this friction component will help you a
182                 // bit to control the rope
183
184                 dir = self.origin - org;
185                 dist = vlen(dir);
186                 dir = normalize(dir);
187
188                 if(cvar("g_grappling_hook_tarzan"))
189                 {
190                         newlength = self.rope_length;
191                         v0 = self.owner.velocity;
192
193                         // first pull the rope...
194                         newlength = max(newlength - pullspeed * frametime, minlength);
195
196                         if(newlength < dist - ropestretch) // overstretched?
197                         {
198                                 newlength = dist - ropestretch;
199                                 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
200                                         self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
201                         }
202
203                         if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
204                                 self.rope_length = newlength;
205
206                         // then pull the player
207                         spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
208                         self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
209                         self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
210
211                         dv = ((self.owner.velocity - v0) * dir) * dir;
212                         if(cvar("g_grappling_hook_tarzan") >= 2)
213                         {
214                                 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.movetype == MOVETYPE_TOSS)
215                                 {
216                                         self.owner.velocity = self.owner.velocity - dv * 0.5;
217                                         self.aiment.velocity = self.aiment.velocity - dv * 0.5;
218                                         self.aiment.flags = self.aiment.flags - (self.aiment.flags & FL_ONGROUND);
219                                         self.aiment.pusher = self.owner;
220                                         self.aiment.pushltime = time + cvar("g_maxpushtime");
221                                 }
222                         }
223                 }
224                 else
225                 {
226                         end = self.origin - dir*50;
227                         dist = vlen(end - org);
228                         if(dist < 200)
229                                 spd = dist * (pullspeed / 200);
230                         else
231                                 spd = pullspeed;
232                         if(spd < 50)
233                                 spd = 0;
234                         self.owner.velocity = dir*spd;
235                         self.owner.movetype = MOVETYPE_FLY;
236                 }
237
238                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
239
240                 org = org + dir*50; // get the beam out of the player's eyes
241         }
242
243         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
244         te_beam(self.owner, self.origin + v_forward * (-9), org);
245 }
246
247 void GrapplingHookTouch (void)
248 {
249         if (other == self.owner)
250                 return;
251         // altered for Nexuiz
252         //else if (pointcontents (self.origin) == CONTENT_SKY)
253         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
254         {
255                 RemoveGrapplingHook(self.owner);
256                 return;
257         }
258
259         if(other == world)
260         {
261                 vector tic;
262                 tic = self.velocity * sys_ticrate;
263                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
264                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
265                 if(trace_fraction >= 1)
266                 {
267                         dprint("Odd... did not hit...?\n");
268                 }
269                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
270                 {
271                         dprint("Detected and prevented the sky-grapple bug.\n");
272                         RemoveGrapplingHook(self.owner);
273                         return;
274                 }
275         }
276
277         if(other)
278                 if(other.movetype != MOVETYPE_NONE)
279                         SetMovetypeFollow(self, other);
280
281         GrapplingHook_Stop();
282         //self.owner.disableclientprediction = TRUE;
283 }
284
285 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
286 {
287         if(self.health > 0)
288         {
289                 self.health = self.health - damage;
290                 if (self.health <= 0)
291                 {
292                         if(attacker != self.owner)
293                         {
294                                 self.owner.pusher = attacker;
295                                 self.owner.pushltime = time + cvar("g_maxpushtime");
296                         }
297                         RemoveGrapplingHook(self.owner);
298                 }
299         }
300 }
301
302 void FireGrapplingHook (void)
303 {
304         local entity missile;
305         local vector org;
306
307         if((arena_roundbased && time < warmup) || (time < game_starttime))
308                 return;
309
310         makevectors(self.v_angle);
311
312         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
313         sound (self, CHAN_WEAPON2, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
314         org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
315         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
316
317         missile = spawn ();
318         missile.owner = self;
319         self.hook = missile;
320         missile.classname = "grapplinghook";
321
322         missile.movetype = MOVETYPE_FLY;
323         missile.solid = SOLID_BBOX;
324
325         setmodel (missile, "models/hook.md3"); // precision set below
326         setsize (missile, '-3 -3 -3', '3 3 3');
327         setorigin (missile, org);
328
329         missile.state = 0; // not latched onto anything
330
331         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
332         W_SetupProjectileVelocity(missile);
333
334         missile.angles = vectoangles (missile.velocity);
335         //missile.glow_color = 250; // 244, 250
336         //missile.glow_size = 120;
337         missile.touch = GrapplingHookTouch;
338         missile.think = GrapplingHookThink;
339         missile.nextthink = time + 0.1;
340
341         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
342
343         missile.health = cvar("g_balance_grapplehook_health");//120
344         missile.event_damage = GrapplingHook_Damage;
345         missile.takedamage = DAMAGE_AIM;
346         missile.damageforcescale = 0;
347 }
348
349 void GrapplingHookFrame()
350 {
351         // this function has been modified for Nexuiz
352         if (self.BUTTON_HOOK && g_grappling_hook)
353         {
354                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
355                         if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
356                                 FireGrapplingHook();
357         }
358         else
359         {
360                 if (self.hook)
361                         RemoveGrapplingHook(self);
362         }
363         self.button6_pressed_before = self.BUTTON_HOOK;
364         /*
365         // if I have no hook or it's not pulling yet, make sure I'm not flying!
366         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
367         {
368                 self.movetype = MOVETYPE_WALK;
369         }
370         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
371         {
372                 // fire hook
373                 FireGrapplingHook();
374                 return;
375         }
376         else if(self.hookimpulse == GRAPHOOK_RELEASE)
377         {
378                 // remove hook, reset movement type
379                 RemoveGrapplingHook(self);
380                 return;
381         }
382         */
383         /*else // make sure the player's movetype is correct
384         {
385                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
386                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
387                 {
388                         self.movetype = MOVETYPE_WALK;
389                 }
390         }*/
391         // note: The hook entity does the actual pulling
392 }
393
394 void SetGrappleHookBindings()
395 {
396         // this function has been modified for Nexuiz
397         // don't remove these lines! old server or demos coud overwrite the new aliases
398         stuffcmd(self, "alias +hook +button6\n");
399         stuffcmd(self, "alias -hook -button6\n");
400 }