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 "t_teleporters.qh"
10 #include "command/common.qh"
11 #include "round_handler.qh"
12 #include "../common/vehicles/sv_vehicles.qh"
13 #include "../common/constants.qh"
14 #include "../common/util.qh"
15 #include "../common/weapons/all.qh"
16 #include "../warpzonelib/common.qh"
17 #include "../warpzonelib/server.qh"
19 /*============================================
21 Wazat's Xonotic Grappling Hook
23 Contact: Wazat1@gmail.com
26 Installation instructions:
27 --------------------------
29 1. Place hook.c in your gamec source directory with the other source files.
31 2. Add this line to the bottom of progs.src:
35 3. Open defs.h and add these lines to the very bottom:
37 // Wazat's grappling hook
39 void GrapplingHookFrame();
40 void RemoveGrapplingHook(entity pl);
41 void SetGrappleHookBindings();
43 const float GRAPHOOK_FIRE = 20;
44 const float GRAPHOOK_RELEASE = 21;
45 // (note: you can change the hook impulse #'s to whatever you please)
47 4. Open client.c and add this to the top of PutClientInServer():
49 RemoveGrapplingHook(self); // Wazat's Grappling Hook
51 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
53 // Wazat's grappling hook
54 SetGrappleHookBindings();
56 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
60 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
63 And you should be done!
66 ============================================*/
69 .float hook_switchweapon;
71 void RemoveGrapplingHook(entity pl)
77 if(pl.movetype == MOVETYPE_FLY)
78 pl.movetype = MOVETYPE_WALK;
80 //pl.disableclientprediction = false;
83 void GrapplingHookReset(void)
85 if(self.realowner.hook == self)
86 RemoveGrapplingHook(self.owner);
91 void GrapplingHookThink();
92 void GrapplingHook_Stop()
94 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
95 sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
98 self.think = GrapplingHookThink;
99 self.nextthink = time;
100 self.touch = func_null;
101 self.velocity = '0 0 0';
102 self.movetype = MOVETYPE_NONE;
103 self.hook_length = -1;
106 .vector hook_start, hook_end;
107 float GrapplingHookSend(entity to, int sf)
109 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
111 if(sound_allowed(MSG_BROADCAST, self.realowner))
113 WriteByte(MSG_ENTITY, sf);
116 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
120 WriteCoord(MSG_ENTITY, self.hook_start.x);
121 WriteCoord(MSG_ENTITY, self.hook_start.y);
122 WriteCoord(MSG_ENTITY, self.hook_start.z);
126 WriteCoord(MSG_ENTITY, self.hook_end.x);
127 WriteCoord(MSG_ENTITY, self.hook_end.y);
128 WriteCoord(MSG_ENTITY, self.hook_end.z);
133 void GrapplingHookThink()
135 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
136 vector dir, org, end, v0, dv, v, myorg, vs;
137 if(self.realowner.hook != self) // how did that happen?
139 error("Owner lost the hook!\n");
142 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
144 RemoveGrapplingHook(self.realowner);
148 WarpZone_RefSys_AddIncrementally(self, self.aiment);
150 self.nextthink = time;
152 int s = self.realowner.cvar_cl_gunalign;
153 if(s != 1 && s != 2 && s != 4)
154 s = 3; // default value
156 vs = hook_shotorigin[s];
158 makevectors(self.realowner.v_angle);
159 org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
160 myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
162 if(self.hook_length < 0)
163 self.hook_length = vlen(myorg - self.origin);
167 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
168 // speed the rope is pulled with
170 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
171 // force the rope will use if it is stretched
173 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
174 // force the rope will use if it is stretched
176 minlength = autocvar_g_balance_grapplehook_length_min;//100;
177 // minimal rope length
178 // if the rope goes below this length, it isn't pulled any more
180 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
181 // if the rope is stretched by more than this amount, more rope is
182 // given to you again
184 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
185 // while hanging on the rope, this friction component will help you a
186 // bit to control the rope
188 dir = self.origin - myorg;
190 dir = normalize(dir);
192 if(autocvar_g_grappling_hook_tarzan)
194 v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
196 // first pull the rope...
197 if(self.realowner.hook_state & HOOK_PULLING)
199 newlength = self.hook_length;
200 newlength = max(newlength - pullspeed * frametime, minlength);
202 if(newlength < dist - ropestretch) // overstretched?
204 newlength = dist - ropestretch;
205 if(v * dir < 0) // only if not already moving in hook direction
206 v = v + frametime * dir * rubberforce_overstretch;
209 self.hook_length = newlength;
212 if(self.realowner.hook_state & HOOK_RELEASING)
215 self.hook_length = newlength;
219 // then pull the player
220 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
221 v = v * (1 - frametime * ropeairfriction);
222 v = v + frametime * dir * spd * rubberforce;
224 dv = ((v - v0) * dir) * dir;
225 if(autocvar_g_grappling_hook_tarzan >= 2)
227 if(self.aiment.movetype == MOVETYPE_WALK)
230 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
231 self.aiment.flags &= ~FL_ONGROUND;
232 self.aiment.pusher = self.realowner;
233 self.aiment.pushltime = time + autocvar_g_maxpushtime;
234 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
238 self.realowner.flags &= ~FL_ONGROUND;
241 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
245 end = self.origin - dir*50;
246 dist = vlen(end - myorg);
248 spd = dist * (pullspeed / 200);
253 self.realowner.velocity = dir*spd;
254 self.realowner.movetype = MOVETYPE_FLY;
256 self.realowner.flags &= ~FL_ONGROUND;
260 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
261 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
263 if(myorg != self.hook_start)
266 self.hook_start = myorg;
268 if(org != self.hook_end)
275 void GrapplingHookTouch (void)
279 GrapplingHook_Stop();
282 if(other.movetype != MOVETYPE_NONE)
284 SetMovetypeFollow(self, other);
285 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
288 //self.realowner.disableclientprediction = true;
291 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
296 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
297 return; // g_balance_projectiledamage says to halt
299 self.health = self.health - damage;
301 if (self.health <= 0)
303 if(attacker != self.realowner)
305 self.realowner.pusher = attacker;
306 self.realowner.pushltime = time + autocvar_g_maxpushtime;
307 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
309 RemoveGrapplingHook(self.realowner);
313 void FireGrapplingHook (void)
319 if(forbidWeaponUse(self)) return;
320 if(self.vehicle) return;
322 makevectors(self.v_angle);
324 int s = self.cvar_cl_gunalign;
325 if(s != 1 && s != 2 && s != 4)
326 s = 3; // default value
328 vs = hook_shotorigin[s];
330 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
331 sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
332 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
334 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
337 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
339 missile = WarpZone_RefSys_SpawnSameRefSys(self);
340 missile.owner = missile.realowner = self;
342 missile.reset = GrapplingHookReset;
343 missile.classname = "grapplinghook";
344 missile.flags = FL_PROJECTILE;
346 missile.movetype = MOVETYPE_FLY;
347 PROJECTILE_MAKETRIGGER(missile);
349 //setmodel (missile, "models/hook.md3"); // precision set below
350 setsize (missile, '-3 -3 -3', '3 3 3');
351 setorigin (missile, org);
353 missile.state = 0; // not latched onto anything
355 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
357 missile.angles = vectoangles (missile.velocity);
358 //missile.glow_color = 250; // 244, 250
359 //missile.glow_size = 120;
360 missile.touch = GrapplingHookTouch;
361 missile.think = GrapplingHookThink;
362 missile.nextthink = time;
364 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
366 missile.health = autocvar_g_balance_grapplehook_health;//120
367 missile.event_damage = GrapplingHook_Damage;
368 missile.takedamage = DAMAGE_AIM;
369 missile.damageforcescale = 0;
370 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
372 missile.hook_start = missile.hook_end = missile.origin;
374 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
377 // void GrapplingHookFrame()
379 // // this function has been modified for Xonotic
380 // - if (self.BUTTON_HOOK && g_grappling_hook)
382 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
383 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
384 // - FireGrapplingHook();
389 // RemoveGrapplingHook(self);
391 // - self.button6_pressed_before = self.BUTTON_HOOK;
393 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
394 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
396 void GrapplingHookFrame()
398 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
400 // offhand hook controls
403 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
405 self.hook_state |= HOOK_FIRING;
406 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
411 self.hook_state |= HOOK_REMOVING;
412 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
415 self.hook_state &= ~HOOK_RELEASING;
416 if(self.BUTTON_CROUCH)
418 self.hook_state &= ~HOOK_PULLING;
419 //self.hook_state |= HOOK_RELEASING;
423 self.hook_state |= HOOK_PULLING;
424 //self.hook_state &= ~HOOK_RELEASING;
427 else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
429 if(self.BUTTON_HOOK && !self.hook_switchweapon)
430 W_SwitchWeapon(WEP_HOOK);
432 self.hook_switchweapon = self.BUTTON_HOOK;
434 if(!g_grappling_hook && self.weapon != WEP_HOOK)
436 self.hook_state &= ~HOOK_FIRING;
437 self.hook_state |= HOOK_REMOVING;
440 if (self.hook_state & HOOK_FIRING)
443 RemoveGrapplingHook(self);
445 self.hook_state &= ~HOOK_FIRING;
446 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
448 else if(self.hook_state & HOOK_REMOVING)
451 RemoveGrapplingHook(self);
452 self.hook_state &= ~HOOK_REMOVING;
456 // if I have no hook or it's not pulling yet, make sure I'm not flying!
457 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
459 self.movetype = MOVETYPE_WALK;
461 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
467 else if(self.hookimpulse == GRAPHOOK_RELEASE)
469 // remove hook, reset movement type
470 RemoveGrapplingHook(self);
474 /*else // make sure the player's movetype is correct
476 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
477 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
479 self.movetype = MOVETYPE_WALK;
482 // note: The hook entity does the actual pulling
485 void GrappleHookInit()
489 hook_shotorigin[0] = '8 8 -12';
490 hook_shotorigin[1] = '8 8 -12';
491 hook_shotorigin[2] = '8 8 -12';
492 hook_shotorigin[3] = '8 8 -12';
496 WEP_ACTION(WEP_HOOK, WR_INIT);
497 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
498 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
499 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
500 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
504 void SetGrappleHookBindings()
506 // this function has been modified for Xonotic
507 // don't remove these lines! old server or demos coud overwrite the new aliases
508 stuffcmd(self, "alias +hook +button6\n");
509 stuffcmd(self, "alias -hook -button6\n");