]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
Merge branch 'terencehill/obsolete_cvars_removal' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_hook.qc
1 #include "g_hook.qh"
2 #include "_all.qh"
3
4 #include "weapons/common.qh"
5 #include "weapons/weaponsystem.qh"
6 #include "weapons/selection.qh"
7 #include "weapons/tracing.qh"
8 #include "cl_player.qh"
9 #include "command/common.qh"
10 #include "round_handler.qh"
11 #include "../common/vehicles/all.qh"
12 #include "../common/constants.qh"
13 #include "../common/util.qh"
14 #include "../common/weapons/all.qh"
15 #include "../warpzonelib/common.qh"
16 #include "../warpzonelib/server.qh"
17
18 /*============================================
19
20       Wazat's Xonotic Grappling Hook
21
22         Contact: Wazat1@gmail.com
23
24
25 Installation instructions:
26 --------------------------
27
28 1. Place hook.c in your gamec source directory with the other source files.
29
30 2. Add this line to the bottom of progs.src:
31
32 gamec/hook.c
33
34 3. Open defs.h and add these lines to the very bottom:
35
36 // Wazat's grappling hook
37 .entity         hook;
38 void GrapplingHookFrame();
39 void RemoveGrapplingHook(entity pl);
40 void SetGrappleHookBindings();
41 // hook impulses
42 const float GRAPHOOK_FIRE               = 20;
43 const float GRAPHOOK_RELEASE            = 21;
44 // (note: you can change the hook impulse #'s to whatever you please)
45
46 4. Open client.c and add this to the top of PutClientInServer():
47
48         RemoveGrapplingHook(self); // Wazat's Grappling Hook
49
50 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
51
52         // Wazat's grappling hook
53         SetGrappleHookBindings();
54
55 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
56
57         GrapplingHookFrame();
58
59 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
60 bind ctrl "+hook"
61
62 And you should be done!
63
64
65 ============================================*/
66
67 .float hook_length;
68 .float hook_switchweapon;
69
70 void RemoveGrapplingHook(entity pl)
71 {
72         if(pl.hook == world)
73                 return;
74         remove(pl.hook);
75         pl.hook = world;
76         if(pl.movetype == MOVETYPE_FLY)
77                 pl.movetype = MOVETYPE_WALK;
78
79         //pl.disableclientprediction = false;
80 }
81
82 void GrapplingHookReset(void)
83 {SELFPARAM();
84         if(self.realowner.hook == self)
85                 RemoveGrapplingHook(self.owner);
86         else // in any case:
87                 remove(self);
88 }
89
90 void GrapplingHookThink();
91 void GrapplingHook_Stop()
92 {SELFPARAM();
93         Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1);
94         sound (self, CH_SHOTS, W_Sound("hook_impact"), VOL_BASE, ATTEN_NORM);
95
96         self.state = 1;
97         self.think = GrapplingHookThink;
98         self.nextthink = time;
99         self.touch = func_null;
100         self.velocity = '0 0 0';
101         self.movetype = MOVETYPE_NONE;
102         self.hook_length = -1;
103 }
104
105 .vector hook_start, hook_end;
106 float GrapplingHookSend(entity to, int sf)
107 {SELFPARAM();
108         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
109         sf = sf & 0x7F;
110         if(sound_allowed(MSG_BROADCAST, self.realowner))
111                 sf |= 0x80;
112         WriteByte(MSG_ENTITY, sf);
113         if(sf & 1)
114         {
115                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
116         }
117         if(sf & 2)
118         {
119                 WriteCoord(MSG_ENTITY, self.hook_start.x);
120                 WriteCoord(MSG_ENTITY, self.hook_start.y);
121                 WriteCoord(MSG_ENTITY, self.hook_start.z);
122         }
123         if(sf & 4)
124         {
125                 WriteCoord(MSG_ENTITY, self.hook_end.x);
126                 WriteCoord(MSG_ENTITY, self.hook_end.y);
127                 WriteCoord(MSG_ENTITY, self.hook_end.z);
128         }
129         return true;
130 }
131
132 void GrapplingHookThink()
133 {SELFPARAM();
134         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
135         vector dir, org, end, v0, dv, v, myorg, vs;
136         if(self.realowner.hook != self) // how did that happen?
137         {
138                 error("Owner lost the hook!\n");
139                 return;
140         }
141         if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
142         {
143                 RemoveGrapplingHook(self.realowner);
144                 return;
145         }
146         if(self.aiment)
147                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
148
149         self.nextthink = time;
150
151         int s = self.realowner.cvar_cl_gunalign;
152         if(s != 1 && s != 2 && s != 4)
153                 s = 3; // default value
154         --s;
155         vs = hook_shotorigin[s];
156
157         makevectors(self.realowner.v_angle);
158         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
159         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
160
161         if(self.hook_length < 0)
162                 self.hook_length = vlen(myorg - self.origin);
163
164         int tarzan = autocvar_g_grappling_hook_tarzan;
165         entity pull_entity = self.realowner;
166         float velocity_multiplier = 1;
167         MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
168         tarzan = hook_tarzan;
169         pull_entity = hook_pullentity;
170         velocity_multiplier = hook_velmultiplier;
171
172         if(self.state == 1)
173         {
174                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
175                 // speed the rope is pulled with
176
177                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
178                 // force the rope will use if it is stretched
179
180                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
181                 // force the rope will use if it is stretched
182
183                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
184                 // minimal rope length
185                 // if the rope goes below this length, it isn't pulled any more
186
187                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
188                 // if the rope is stretched by more than this amount, more rope is
189                 // given to you again
190
191                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
192                 // while hanging on the rope, this friction component will help you a
193                 // bit to control the rope
194
195                 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
196
197                 dir = self.origin - myorg;
198                 dist = vlen(dir);
199                 dir = normalize(dir);
200
201                 if(tarzan)
202                 {
203                         v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
204
205                         // first pull the rope...
206                         if(self.realowner.hook_state & HOOK_PULLING)
207                         {
208                                 newlength = self.hook_length;
209                                 newlength = max(newlength - pullspeed * frametime, minlength);
210
211                                 if(newlength < dist - ropestretch) // overstretched?
212                                 {
213                                         newlength = dist - ropestretch;
214                                         if(v * dir < 0) // only if not already moving in hook direction
215                                                 v = v + frametime * dir * rubberforce_overstretch;
216                                 }
217
218                                 self.hook_length = newlength;
219                         }
220
221                         if(pull_entity.movetype == MOVETYPE_FLY)
222                                 pull_entity.movetype = MOVETYPE_WALK;
223
224                         if(self.realowner.hook_state & HOOK_RELEASING)
225                         {
226                                 newlength = dist;
227                                 self.hook_length = newlength;
228                         }
229                         else
230                         {
231                                 // then pull the player
232                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
233                                 v = v * (1 - frametime * ropeairfriction);
234                                 v = v + frametime * dir * spd * rubberforce;
235
236                                 dv = ((v - v0) * dir) * dir;
237                                 if(tarzan >= 2)
238                                 {
239                                         if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
240                                         {
241                                                 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
242                                                 v = v - dv * 0.5;
243                                                 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
244                                                 {
245                                                         self.aiment.velocity = self.aiment.velocity - dv * 0.5;
246                                                         self.aiment.flags &= ~FL_ONGROUND;
247                                                         if(self.aiment.flags & FL_PROJECTILE)
248                                                                 UpdateCSQCProjectile(self.aiment);
249                                                 }
250                                                 if(self.aiment.classname == "nade")
251                                                         self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
252                                                 aim_ent.pusher = self.realowner;
253                                                 aim_ent.pushltime = time + autocvar_g_maxpushtime;
254                                                 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
255                                         }
256                                 }
257
258                                 pull_entity.flags &= ~FL_ONGROUND;
259                         }
260
261                         if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
262                                 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
263
264                         if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
265                         {
266                                 RemoveGrapplingHook(self.realowner);
267                                 return;
268                         }
269                 }
270                 else
271                 {
272                         end = self.origin - dir*50;
273                         dist = vlen(end - myorg);
274                         if(dist < 200)
275                                 spd = dist * (pullspeed / 200);
276                         else
277                                 spd = pullspeed;
278                         if(spd < 50)
279                                 spd = 0;
280                         self.realowner.velocity = dir*spd;
281                         self.realowner.movetype = MOVETYPE_FLY;
282
283                         self.realowner.flags &= ~FL_ONGROUND;
284                 }
285         }
286
287         makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
288         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
289
290         if(myorg != self.hook_start)
291         {
292                 self.SendFlags |= 2;
293                 self.hook_start = myorg;
294         }
295         if(org != self.hook_end)
296         {
297                 self.SendFlags |= 4;
298                 self.hook_end = org;
299         }
300 }
301
302 void GrapplingHookTouch (void)
303 {SELFPARAM();
304         if(other.movetype == MOVETYPE_FOLLOW)
305                 return;
306         PROJECTILE_TOUCH;
307
308         GrapplingHook_Stop();
309
310         if(other)
311                 if(other.movetype != MOVETYPE_NONE)
312                 {
313                         SetMovetypeFollow(self, other);
314                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
315                 }
316
317         //self.realowner.disableclientprediction = true;
318 }
319
320 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
321 {SELFPARAM();
322         if(self.health <= 0)
323                 return;
324
325         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
326                 return; // g_balance_projectiledamage says to halt
327
328         self.health = self.health - damage;
329
330         if (self.health <= 0)
331         {
332                 if(attacker != self.realowner)
333                 {
334                         self.realowner.pusher = attacker;
335                         self.realowner.pushltime = time + autocvar_g_maxpushtime;
336                         self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
337                 }
338                 RemoveGrapplingHook(self.realowner);
339         }
340 }
341
342 void FireGrapplingHook (void)
343 {SELFPARAM();
344         entity missile;
345         vector org;
346         vector vs;
347
348         if(forbidWeaponUse(self)) return;
349         if(self.vehicle) return;
350
351         makevectors(self.v_angle);
352
353         int s = self.cvar_cl_gunalign;
354         if(s != 1 && s != 2 && s != 4)
355                 s = 3; // default value
356         --s;
357         vs = hook_shotorigin[s];
358
359         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
360         sound (self, CH_WEAPON_B, W_Sound("hook_fire"), VOL_BASE, ATTEN_NORM);
361         org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
362
363         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
364         org = trace_endpos;
365
366         Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
367
368         missile = WarpZone_RefSys_SpawnSameRefSys(self);
369         missile.owner = missile.realowner = self;
370         self.hook = missile;
371         missile.reset = GrapplingHookReset;
372         missile.classname = "grapplinghook";
373         missile.flags = FL_PROJECTILE;
374
375         missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
376         PROJECTILE_MAKETRIGGER(missile);
377
378         //setmodel (missile, MDL_HOOK); // precision set below
379         setsize (missile, '-3 -3 -3', '3 3 3');
380         setorigin (missile, org);
381
382         missile.state = 0; // not latched onto anything
383
384         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
385
386         missile.angles = vectoangles (missile.velocity);
387         //missile.glow_color = 250; // 244, 250
388         //missile.glow_size = 120;
389         missile.touch = GrapplingHookTouch;
390         missile.think = GrapplingHookThink;
391         missile.nextthink = time;
392
393         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
394
395         missile.health = autocvar_g_balance_grapplehook_health;//120
396         missile.event_damage = GrapplingHook_Damage;
397         missile.takedamage = DAMAGE_AIM;
398         missile.damageforcescale = 0;
399         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
400
401         missile.hook_start = missile.hook_end = missile.origin;
402
403         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
404 }
405
406 //  void GrapplingHookFrame()
407 //  {
408 //         // this function has been modified for Xonotic
409 // -       if (self.BUTTON_HOOK && g_grappling_hook)
410 //         {
411 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
412 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
413 // -                               FireGrapplingHook();
414 //         }
415 // -       else
416 //         {
417 //                 if (self.hook)
418 //                         RemoveGrapplingHook(self);
419 //         }
420 // -       self.button6_pressed_before = self.BUTTON_HOOK;
421 //         /*
422 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
423 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
424
425 void GrapplingHookFrame()
426 {SELFPARAM();
427         if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK.m_id && !self.vehicle)
428         {
429                 // offhand hook controls
430                 if(self.BUTTON_HOOK)
431                 {
432                         if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
433                         {
434                                 self.hook_state |= HOOK_FIRING;
435                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
436                         }
437                 }
438                 else
439                 {
440                         self.hook_state |= HOOK_REMOVING;
441                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
442                 }
443
444                 self.hook_state &= ~HOOK_RELEASING;
445                 if(self.BUTTON_CROUCH && autocvar_g_balance_grapplehook_crouchslide)
446                 {
447                         self.hook_state &= ~HOOK_PULLING;
448                         //self.hook_state |= HOOK_RELEASING;
449                 }
450                 else
451                 {
452                         self.hook_state |= HOOK_PULLING;
453                         //self.hook_state &= ~HOOK_RELEASING;
454                 }
455         }
456         else if(!g_grappling_hook && self.switchweapon != WEP_HOOK.m_id && !self.vehicle)
457         {
458                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
459                         W_SwitchWeapon(WEP_HOOK.m_id);
460         }
461         self.hook_switchweapon = self.BUTTON_HOOK;
462
463         if(!g_grappling_hook && self.weapon != WEP_HOOK.m_id)
464         {
465                 self.hook_state &= ~HOOK_FIRING;
466                 self.hook_state |= HOOK_REMOVING;
467         }
468
469         if (self.hook_state & HOOK_FIRING)
470         {
471                 if (self.hook)
472                         RemoveGrapplingHook(self);
473                 FireGrapplingHook();
474                 self.hook_state &= ~HOOK_FIRING;
475                 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
476         }
477         else if(self.hook_state & HOOK_REMOVING)
478         {
479                 if (self.hook)
480                         RemoveGrapplingHook(self);
481                 self.hook_state &= ~HOOK_REMOVING;
482         }
483
484         /*
485         // if I have no hook or it's not pulling yet, make sure I'm not flying!
486         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
487         {
488                 self.movetype = MOVETYPE_WALK;
489         }
490         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
491         {
492                 // fire hook
493                 FireGrapplingHook();
494                 return;
495         }
496         else if(self.hookimpulse == GRAPHOOK_RELEASE)
497         {
498                 // remove hook, reset movement type
499                 RemoveGrapplingHook(self);
500                 return;
501         }
502         */
503         /*else // make sure the player's movetype is correct
504         {
505                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
506                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
507                 {
508                         self.movetype = MOVETYPE_WALK;
509                 }
510         }*/
511         // note: The hook entity does the actual pulling
512 }
513
514 void GrappleHookInit()
515 {
516         if(g_grappling_hook)
517         {
518                 hook_shotorigin[0] = '8 8 -12';
519                 hook_shotorigin[1] = '8 8 -12';
520                 hook_shotorigin[2] = '8 8 -12';
521                 hook_shotorigin[3] = '8 8 -12';
522         }
523         else
524         {
525                 WEP_ACTION(WEP_HOOK.m_id, WR_INIT);
526                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
527                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
528                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
529                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
530         }
531 }
532
533 void SetGrappleHookBindings()
534 {SELFPARAM();
535         // this function has been modified for Xonotic
536         // don't remove these lines! old server or demos coud overwrite the new aliases
537         stuffcmd(self, "alias +hook +button6\n");
538         stuffcmd(self, "alias -hook -button6\n");
539 }