Merge branch 'master' into samual/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_hook.qc
1 /*============================================
2
3       Wazat's Xonotic 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 const float GRAPHOOK_FIRE               = 20;
26 const 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 .float hook_length;
51 .float hook_switchweapon;
52
53 void RemoveGrapplingHook(entity pl)
54 {
55         if(pl.hook == world)
56                 return;
57         remove(pl.hook);
58         pl.hook = world;
59         if(pl.movetype == MOVETYPE_FLY)
60                 pl.movetype = MOVETYPE_WALK;
61
62         //pl.disableclientprediction = FALSE;
63 }
64
65 void GrapplingHookReset(void)
66 {
67         if(self.realowner.hook == self)
68                 RemoveGrapplingHook(self.owner);
69         else // in any case:
70                 remove(self);
71 }
72
73 void GrapplingHookThink();
74 void GrapplingHook_Stop()
75 {
76         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
77         sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
78
79         self.state = 1;
80         self.think = GrapplingHookThink;
81         self.nextthink = time;
82         self.touch = func_null;
83         self.velocity = '0 0 0';
84         self.movetype = MOVETYPE_NONE;
85         self.hook_length = -1;
86 }
87
88 .vector hook_start, hook_end;
89 float GrapplingHookSend(entity to, float sf)
90 {
91         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
92         sf = sf & 0x7F;
93         if(sound_allowed(MSG_BROADCAST, self.realowner))
94                 sf |= 0x80;
95         WriteByte(MSG_ENTITY, sf);
96         if(sf & 1)
97         {
98                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
99         }
100         if(sf & 2)
101         {
102                 WriteCoord(MSG_ENTITY, self.hook_start_x);
103                 WriteCoord(MSG_ENTITY, self.hook_start_y);
104                 WriteCoord(MSG_ENTITY, self.hook_start_z);
105         }
106         if(sf & 4)
107         {
108                 WriteCoord(MSG_ENTITY, self.hook_end_x);
109                 WriteCoord(MSG_ENTITY, self.hook_end_y);
110                 WriteCoord(MSG_ENTITY, self.hook_end_z);
111         }
112         return TRUE;
113 }
114
115 void GrapplingHookThink()
116 {
117         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
118         vector dir, org, end, v0, dv, v, myorg, vs;
119         if(self.realowner.hook != self) // how did that happen?
120         {
121                 error("Owner lost the hook!\n");
122                 return;
123         }
124         if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
125         {
126                 RemoveGrapplingHook(self.realowner);
127                 return;
128         }
129         if(self.aiment)
130                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
131
132         self.nextthink = time;
133
134         s = self.realowner.cvar_cl_gunalign;
135         if(s != 1 && s != 2 && s != 4)
136                 s = 3; // default value
137         --s;
138         vs = hook_shotorigin[s];
139
140         makevectors(self.realowner.v_angle);
141         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
142         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
143
144         if(self.hook_length < 0)
145                 self.hook_length = vlen(myorg - self.origin);
146
147         if(self.state == 1)
148         {
149                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
150                 // speed the rope is pulled with
151
152                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
153                 // force the rope will use if it is stretched
154
155                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
156                 // force the rope will use if it is stretched
157
158                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
159                 // minimal rope length
160                 // if the rope goes below this length, it isn't pulled any more
161
162                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
163                 // if the rope is stretched by more than this amount, more rope is
164                 // given to you again
165
166                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
167                 // while hanging on the rope, this friction component will help you a
168                 // bit to control the rope
169
170                 dir = self.origin - myorg;
171                 dist = vlen(dir);
172                 dir = normalize(dir);
173
174                 if(autocvar_g_grappling_hook_tarzan)
175                 {
176                         v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
177
178                         // first pull the rope...
179                         if(self.realowner.hook_state & HOOK_PULLING)
180                         {
181                                 newlength = self.hook_length;
182                                 newlength = max(newlength - pullspeed * frametime, minlength);
183
184                                 if(newlength < dist - ropestretch) // overstretched?
185                                 {
186                                         newlength = dist - ropestretch;
187                                         if(v * dir < 0) // only if not already moving in hook direction
188                                                 v = v + frametime * dir * rubberforce_overstretch;
189                                 }
190
191                                 self.hook_length = newlength;
192                         }
193
194                         if(self.realowner.hook_state & HOOK_RELEASING)
195                         {
196                                 newlength = dist;
197                                 self.hook_length = newlength;
198                         }
199                         else
200                         {
201                                 // then pull the player
202                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
203                                 v = v * (1 - frametime * ropeairfriction);
204                                 v = v + frametime * dir * spd * rubberforce;
205
206                                 dv = ((v - v0) * dir) * dir;
207                                 if(autocvar_g_grappling_hook_tarzan >= 2)
208                                 {
209                                         if(self.aiment.movetype == MOVETYPE_WALK)
210                                         {
211                                                 v = v - dv * 0.5;
212                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
213                                                 self.aiment.flags &= ~FL_ONGROUND;
214                                                 self.aiment.pusher = self.realowner;
215                                                 self.aiment.pushltime = time + autocvar_g_maxpushtime;
216                                                 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
217                                         }
218                                 }
219
220                                 self.realowner.flags &= ~FL_ONGROUND;
221                         }
222
223                         self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
224                 }
225                 else
226                 {
227                         end = self.origin - dir*50;
228                         dist = vlen(end - myorg);
229                         if(dist < 200)
230                                 spd = dist * (pullspeed / 200);
231                         else
232                                 spd = pullspeed;
233                         if(spd < 50)
234                                 spd = 0;
235                         self.realowner.velocity = dir*spd;
236                         self.realowner.movetype = MOVETYPE_FLY;
237
238                         self.realowner.flags &= ~FL_ONGROUND;
239                 }
240         }
241
242         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
243         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
244
245         if(myorg != self.hook_start)
246         {
247                 self.SendFlags |= 2;
248                 self.hook_start = myorg;
249         }
250         if(org != self.hook_end)
251         {
252                 self.SendFlags |= 4;
253                 self.hook_end = org;
254         }
255 }
256
257 void GrapplingHookTouch (void)
258 {
259         PROJECTILE_TOUCH;
260
261         GrapplingHook_Stop();
262
263         if(other)
264                 if(other.movetype != MOVETYPE_NONE)
265                 {
266                         SetMovetypeFollow(self, other);
267                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
268                 }
269
270         //self.realowner.disableclientprediction = TRUE;
271 }
272
273 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
274 {
275         if(self.health <= 0)
276                 return;
277
278         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
279                 return; // g_balance_projectiledamage says to halt
280
281         self.health = self.health - damage;
282
283         if (self.health <= 0)
284         {
285                 if(attacker != self.realowner)
286                 {
287                         self.realowner.pusher = attacker;
288                         self.realowner.pushltime = time + autocvar_g_maxpushtime;
289                         self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
290                 }
291                 RemoveGrapplingHook(self.realowner);
292         }
293 }
294
295 void FireGrapplingHook (void)
296 {
297         entity missile;
298         vector org;
299         float s;
300         vector vs;
301
302         if(forbidWeaponUse()) return;
303         if(self.vehicle) return;
304
305         makevectors(self.v_angle);
306
307         s = self.cvar_cl_gunalign;
308         if(s != 1 && s != 2 && s != 4)
309                 s = 3; // default value
310         --s;
311         vs = hook_shotorigin[s];
312
313         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
314         sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
315         org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
316
317         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
318         org = trace_endpos;
319
320         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
321
322         missile = WarpZone_RefSys_SpawnSameRefSys(self);
323         missile.owner = missile.realowner = self;
324         self.hook = missile;
325         missile.reset = GrapplingHookReset;
326         missile.classname = "grapplinghook";
327         missile.flags = FL_PROJECTILE;
328
329         missile.movetype = MOVETYPE_FLY;
330         PROJECTILE_MAKETRIGGER(missile);
331
332         //setmodel (missile, "models/hook.md3"); // precision set below
333         setsize (missile, '-3 -3 -3', '3 3 3');
334         setorigin (missile, org);
335
336         missile.state = 0; // not latched onto anything
337
338         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
339
340         missile.angles = vectoangles (missile.velocity);
341         //missile.glow_color = 250; // 244, 250
342         //missile.glow_size = 120;
343         missile.touch = GrapplingHookTouch;
344         missile.think = GrapplingHookThink;
345         missile.nextthink = time;
346
347         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
348
349         missile.health = autocvar_g_balance_grapplehook_health;//120
350         missile.event_damage = GrapplingHook_Damage;
351         missile.takedamage = DAMAGE_AIM;
352         missile.damageforcescale = 0;
353         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
354
355         missile.hook_start = missile.hook_end = missile.origin;
356
357         Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
358 }
359
360 //  void GrapplingHookFrame()
361 //  {
362 //         // this function has been modified for Xonotic
363 // -       if (self.BUTTON_HOOK && g_grappling_hook)
364 //         {
365 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
366 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
367 // -                               FireGrapplingHook();
368 //         }
369 // -       else
370 //         {
371 //                 if (self.hook)
372 //                         RemoveGrapplingHook(self);
373 //         }
374 // -       self.button6_pressed_before = self.BUTTON_HOOK;
375 //         /*
376 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
377 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
378
379 void GrapplingHookFrame()
380 {
381         if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
382         {
383                 // offhand hook controls
384                 if(self.BUTTON_HOOK)
385                 {
386                         if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
387                         {
388                                 self.hook_state |= HOOK_FIRING;
389                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
390                         }
391                 }
392                 else
393                 {
394                         self.hook_state |= HOOK_REMOVING;
395                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
396                 }
397
398                 self.hook_state &= ~HOOK_RELEASING;
399                 if(self.BUTTON_CROUCH)
400                 {
401                         self.hook_state &= ~HOOK_PULLING;
402                         //self.hook_state |= HOOK_RELEASING;
403                 }
404                 else
405                 {
406                         self.hook_state |= HOOK_PULLING;
407                         //self.hook_state &= ~HOOK_RELEASING;
408                 }
409         }
410         else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
411         {
412                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
413                         W_SwitchWeapon(WEP_HOOK);
414         }
415         self.hook_switchweapon = self.BUTTON_HOOK;
416
417         if(!g_grappling_hook && self.weapon != WEP_HOOK)
418         {
419                 self.hook_state &= ~HOOK_FIRING;
420                 self.hook_state |= HOOK_REMOVING;
421         }
422
423         if (self.hook_state & HOOK_FIRING)
424         {
425                 if (self.hook)
426                         RemoveGrapplingHook(self);
427                 FireGrapplingHook();
428                 self.hook_state &= ~HOOK_FIRING;
429                 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
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         {
472                 hook_shotorigin[0] = '8 8 -12';
473                 hook_shotorigin[1] = '8 8 -12';
474                 hook_shotorigin[2] = '8 8 -12';
475                 hook_shotorigin[3] = '8 8 -12';
476         }
477         else
478         {
479                 WEP_ACTION(WEP_HOOK, WR_INIT);
480                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
481                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
482                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
483                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
484         }
485 }
486
487 void SetGrappleHookBindings()
488 {
489         // this function has been modified for Xonotic
490         // don't remove these lines! old server or demos coud overwrite the new aliases
491         stuffcmd(self, "alias +hook +button6\n");
492         stuffcmd(self, "alias -hook -button6\n");
493 }