]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
feature not for 2.5: jetpack
[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 /*
72         if(ent.movetype != MOVETYPE_FOLLOW)
73                 if(ent.aiment)
74                         error("???");
75 */
76         if(ent.aiment)
77         {
78                 if(ent.aiment.classname != ent.aiment_classname)
79                         return 1;
80                 if(ent.aiment.deadflag != ent.aiment_deadflag)
81                         return 1;
82         }
83         return 0;
84 }
85
86 .float hook_length;
87 .float hook_switchweapon;
88
89 void RemoveGrapplingHook(entity pl)
90 {
91         if(pl.hook == world)
92                 return;
93         remove(pl.hook);
94         pl.hook = world;
95         if(pl.movetype == MOVETYPE_FLY)
96                 pl.movetype = MOVETYPE_WALK;
97
98         //pl.disableclientprediction = FALSE;
99 }
100
101 void GrapplingHookThink();
102 void GrapplingHook_Stop()
103 {
104         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
105         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
106
107         self.state = 1;
108         self.think = GrapplingHookThink;
109         self.nextthink = time;
110         self.touch = SUB_Null;
111         self.velocity = '0 0 0';
112         self.movetype = MOVETYPE_NONE;
113         self.hook_length = -1;
114 }
115
116 void GrapplingHookThink()
117 {
118         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
119         vector dir, org, end, v0, dv;
120         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
121         {                                                                                                               // well, better fix it anyway
122                 remove(self);
123                 return;
124         }
125         if(LostMovetypeFollow(self))
126         {
127                 RemoveGrapplingHook(self.owner);
128                 return;
129         }
130
131         self.nextthink = time;
132
133         makevectors(self.owner.v_angle);
134         org = self.owner.origin + self.owner.view_ofs + v_forward * hook_shotorigin_x + v_right * hook_shotorigin_y + v_up * hook_shotorigin_z;
135
136 #if 0
137         tracebox(org, self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self.owner);
138         // do not hit players with this, as they tend to get in the way just too often
139         // NOTE: this assumes sky brushes cannot get in the way
140         // if they can, assume the map is broken! :P
141         if(trace_fraction < 1 && (!self.aiment || trace_ent != self.aiment))
142         {
143                 // 0. stop it
144                 if(self.state != 1)
145                         GrapplingHook_Stop();
146
147                 // 1. detach the hook
148                 if(self.aiment)
149                         UnsetMovetypeFollow(self);
150
151                 // 2. cut it off
152                 self.origin = trace_endpos;
153
154                 // 3. reattach the hook
155                 if(trace_ent)
156                         if(trace_ent.movetype != MOVETYPE_NONE)
157                                 SetMovetypeFollow(self, trace_ent);
158         }
159 #endif
160
161         if(self.hook_length < 0)
162                 self.hook_length = vlen(org - self.origin);
163
164         if(self.state == 1)
165         {
166                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
167                 // speed the rope is pulled with
168
169                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
170                 // force the rope will use if it is stretched
171
172                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
173                 // force the rope will use if it is stretched
174
175                 minlength = cvar("g_balance_grapplehook_length_min");//100;
176                 // minimal rope length
177                 // if the rope goes below this length, it isn't pulled any more
178
179                 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
180                 // if the rope is stretched by more than this amount, more rope is
181                 // given to you again
182
183                 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
184                 // while hanging on the rope, this friction component will help you a
185                 // bit to control the rope
186
187                 dir = self.origin - org;
188                 dist = vlen(dir);
189                 dir = normalize(dir);
190
191                 if(cvar("g_grappling_hook_tarzan"))
192                 {
193                         v0 = self.owner.velocity;
194
195                         // first pull the rope...
196                         if(self.owner.hook_state & HOOK_PULLING)
197                         {
198                                 newlength = self.hook_length;
199                                 newlength = max(newlength - pullspeed * frametime, minlength);
200
201                                 if(newlength < dist - ropestretch) // overstretched?
202                                 {
203                                         newlength = dist - ropestretch;
204                                         if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
205                                                 self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
206                                 }
207
208                                 self.hook_length = newlength;
209                         }
210
211                         if(self.owner.hook_state & HOOK_RELEASING)
212                         {
213                                 newlength = dist;
214                                 self.hook_length = newlength;
215                         }
216                         else
217                         {
218                                 // then pull the player
219                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
220                                 self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
221                                 self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
222
223                                 dv = ((self.owner.velocity - v0) * dir) * dir;
224                                 if(cvar("g_grappling_hook_tarzan") >= 2)
225                                 {
226                                         if(self.aiment.movetype == MOVETYPE_WALK)
227                                         {
228                                                 self.owner.velocity = self.owner.velocity - dv * 0.5;
229                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
230                                                 self.aiment.flags &~= FL_ONGROUND;
231                                                 self.aiment.pusher = self.owner;
232                                                 self.aiment.pushltime = time + cvar("g_maxpushtime");
233                                         }
234                                 }
235
236                                 self.owner.flags &~= FL_ONGROUND;
237                         }
238                 }
239                 else
240                 {
241                         end = self.origin - dir*50;
242                         dist = vlen(end - org);
243                         if(dist < 200)
244                                 spd = dist * (pullspeed / 200);
245                         else
246                                 spd = pullspeed;
247                         if(spd < 50)
248                                 spd = 0;
249                         self.owner.velocity = dir*spd;
250                         self.owner.movetype = MOVETYPE_FLY;
251
252                         self.owner.flags &~= FL_ONGROUND;
253                 }
254         }
255
256         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
257         te_beam(self.owner, self.origin + v_forward * (-9), org);
258 }
259
260 void GrapplingHookTouch (void)
261 {
262         if(SUB_OwnerCheck())
263                 return;
264         if(SUB_NoImpactCheck())
265         {
266                 RemoveGrapplingHook(self.owner);
267                 return;
268         }
269
270         if(other == world)
271         {
272                 vector tic;
273                 tic = self.velocity * sys_ticrate;
274                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
275                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
276                 if(trace_fraction >= 1)
277                 {
278                         dprint("Odd... did not hit...?\n");
279                 }
280                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
281                 {
282                         dprint("Detected and prevented the sky-grapple bug.\n");
283                         RemoveGrapplingHook(self.owner);
284                         return;
285                 }
286         }
287
288         GrapplingHook_Stop();
289
290         if(other)
291                 if(other.movetype != MOVETYPE_NONE)
292                         SetMovetypeFollow(self, other);
293
294         //self.owner.disableclientprediction = TRUE;
295 }
296
297 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
298 {
299         if(self.health > 0)
300         {
301                 self.health = self.health - damage;
302                 if (self.health <= 0)
303                 {
304                         if(attacker != self.owner)
305                         {
306                                 self.owner.pusher = attacker;
307                                 self.owner.pushltime = time + cvar("g_maxpushtime");
308                         }
309                         RemoveGrapplingHook(self.owner);
310                 }
311         }
312 }
313
314 void FireGrapplingHook (void)
315 {
316         local entity missile;
317         local vector org;
318
319         if((arena_roundbased && time < warmup) || (time < game_starttime))
320                 return;
321
322         makevectors(self.v_angle);
323
324         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
325         sound (self, CHAN_WEAPON2, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
326         org = self.origin + self.view_ofs + v_forward * hook_shotorigin_x + v_right * hook_shotorigin_y + v_up * hook_shotorigin_z;
327         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
328
329         missile = spawn ();
330         missile.owner = self;
331         self.hook = missile;
332         missile.classname = "grapplinghook";
333
334         missile.movetype = MOVETYPE_FLY;
335         missile.solid = SOLID_BBOX;
336
337         setmodel (missile, "models/hook.md3"); // precision set below
338         setsize (missile, '-3 -3 -3', '3 3 3');
339         setorigin (missile, org);
340
341         missile.state = 0; // not latched onto anything
342
343         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
344         W_SetupProjectileVelocity(missile);
345
346         missile.angles = vectoangles (missile.velocity);
347         //missile.glow_color = 250; // 244, 250
348         //missile.glow_size = 120;
349         missile.touch = GrapplingHookTouch;
350         missile.think = GrapplingHookThink;
351         missile.nextthink = time + 0.1;
352
353         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
354
355         missile.health = cvar("g_balance_grapplehook_health");//120
356         missile.event_damage = GrapplingHook_Damage;
357         missile.takedamage = DAMAGE_AIM;
358         missile.damageforcescale = 0;
359 }
360
361 //  void GrapplingHookFrame()
362 //  {
363 //         // this function has been modified for Nexuiz
364 // -       if (self.BUTTON_HOOK && g_grappling_hook)
365 //         {
366 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
367 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
368 // -                               FireGrapplingHook();
369 //         }
370 // -       else
371 //         {
372 //                 if (self.hook)
373 //                         RemoveGrapplingHook(self);
374 //         }
375 // -       self.button6_pressed_before = self.BUTTON_HOOK;
376 //         /*
377 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
378 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
379
380 void GrapplingHookFrame()
381 {
382         if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
383         {
384                 // offhand hook controls
385                 if(self.BUTTON_HOOK)
386                 {
387                         if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
388                         {
389                                 self.hook_state |= HOOK_FIRING;
390                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
391                         }
392                 }
393                 else
394                 {
395                         self.hook_state |= HOOK_REMOVING;
396                         self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
397                 }
398
399                 self.hook_state &~= HOOK_RELEASING;
400                 if(self.BUTTON_CROUCH)
401                 {
402                         self.hook_state &~= HOOK_PULLING;
403                         //self.hook_state |= HOOK_RELEASING;
404                 }
405                 else
406                 {
407                         self.hook_state |= HOOK_PULLING;
408                         //self.hook_state &~= HOOK_RELEASING;
409                 }
410         }
411         else if(!g_jetpack && !g_grappling_hook && self.switchweapon != WEP_HOOK)
412         {
413                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
414                         W_SwitchWeapon(WEP_HOOK);
415         }
416         self.hook_switchweapon = self.BUTTON_HOOK;
417
418         if(!g_grappling_hook && self.weapon != WEP_HOOK)
419         {
420                 self.hook_state &~= HOOK_FIRING;
421                 self.hook_state |= HOOK_REMOVING;
422         }
423
424         if (self.hook_state & HOOK_FIRING)
425         {
426                 if (self.hook)
427                         RemoveGrapplingHook(self);
428                 FireGrapplingHook();
429                 self.hook_state &~= HOOK_FIRING;
430         }
431         else if(self.hook_state & HOOK_REMOVING)
432         {
433                 if (self.hook)
434                         RemoveGrapplingHook(self);
435                 self.hook_state &~= HOOK_REMOVING;
436         }
437
438         /*
439         // if I have no hook or it's not pulling yet, make sure I'm not flying!
440         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
441         {
442                 self.movetype = MOVETYPE_WALK;
443         }
444         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
445         {
446                 // fire hook
447                 FireGrapplingHook();
448                 return;
449         }
450         else if(self.hookimpulse == GRAPHOOK_RELEASE)
451         {
452                 // remove hook, reset movement type
453                 RemoveGrapplingHook(self);
454                 return;
455         }
456         */
457         /*else // make sure the player's movetype is correct
458         {
459                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
460                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
461                 {
462                         self.movetype = MOVETYPE_WALK;
463                 }
464         }*/
465         // note: The hook entity does the actual pulling
466 }
467
468 void GrappleHookInit()
469 {
470         if(g_grappling_hook)
471                 hook_shotorigin = '8 -8 -12';
472         else
473                 hook_shotorigin = shotorg_adjust('26.2148 9.2059 -15.9772', FALSE, FALSE);
474 }
475
476 void SetGrappleHookBindings()
477 {
478         // this function has been modified for Nexuiz
479         // don't remove these lines! old server or demos coud overwrite the new aliases
480         stuffcmd(self, "alias +hook +button6\n");
481         stuffcmd(self, "alias -hook -button6\n");
482 }