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