1 /*============================================
3 Wazat's Xonotic Grappling Hook
5 Contact: Wazat1@gmail.com
8 Installation instructions:
9 --------------------------
11 1. Place hook.c in your gamec source directory with the other source files.
13 2. Add this line to the bottom of progs.src:
17 3. Open defs.h and add these lines to the very bottom:
19 // Wazat's grappling hook
21 void GrapplingHookFrame();
22 void RemoveGrapplingHook(entity pl);
23 void SetGrappleHookBindings();
25 float GRAPHOOK_FIRE = 20;
26 float GRAPHOOK_RELEASE = 21;
27 // (note: you can change the hook impulse #'s to whatever you please)
29 4. Open client.c and add this to the top of PutClientInServer():
31 RemoveGrapplingHook(self); // Wazat's Grappling Hook
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
35 // Wazat's grappling hook
36 SetGrappleHookBindings();
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
42 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
45 And you should be done!
48 ============================================*/
51 .float hook_switchweapon;
53 void RemoveGrapplingHook(entity pl)
59 if(pl.movetype == MOVETYPE_FLY)
60 pl.movetype = MOVETYPE_WALK;
62 //pl.disableclientprediction = FALSE;
65 void GrapplingHookReset(void)
67 if(self.realowner.hook == self)
68 RemoveGrapplingHook(self.owner);
73 void GrapplingHookThink();
74 void GrapplingHook_Stop()
76 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
77 sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
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;
88 .vector hook_start, hook_end;
89 float GrapplingHookSend(entity to, float sf)
91 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
93 if(sound_allowed(MSG_BROADCAST, self.realowner))
95 WriteByte(MSG_ENTITY, sf);
98 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
102 WriteCoord(MSG_ENTITY, self.hook_start_x);
103 WriteCoord(MSG_ENTITY, self.hook_start_y);
104 WriteCoord(MSG_ENTITY, self.hook_start_z);
108 WriteCoord(MSG_ENTITY, self.hook_end_x);
109 WriteCoord(MSG_ENTITY, self.hook_end_y);
110 WriteCoord(MSG_ENTITY, self.hook_end_z);
115 void GrapplingHookThink()
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?
121 error("Owner lost the hook!\n");
124 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
126 RemoveGrapplingHook(self.realowner);
130 WarpZone_RefSys_AddIncrementally(self, self.aiment);
132 self.nextthink = time;
134 s = self.realowner.cvar_cl_gunalign;
135 if(s != 1 && s != 2 && s != 4)
136 s = 3; // default value
138 vs = hook_shotorigin[s];
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);
144 if(self.hook_length < 0)
145 self.hook_length = vlen(myorg - self.origin);
149 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
150 // speed the rope is pulled with
152 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
153 // force the rope will use if it is stretched
155 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
156 // force the rope will use if it is stretched
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
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
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
170 dir = self.origin - myorg;
172 dir = normalize(dir);
174 if(autocvar_g_grappling_hook_tarzan)
176 v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
178 // first pull the rope...
179 if(self.realowner.hook_state & HOOK_PULLING)
181 newlength = self.hook_length;
182 newlength = max(newlength - pullspeed * frametime, minlength);
184 if(newlength < dist - ropestretch) // overstretched?
186 newlength = dist - ropestretch;
187 if(v * dir < 0) // only if not already moving in hook direction
188 v = v + frametime * dir * rubberforce_overstretch;
191 self.hook_length = newlength;
194 if(self.realowner.hook_state & HOOK_RELEASING)
197 self.hook_length = newlength;
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;
206 dv = ((v - v0) * dir) * dir;
207 if(autocvar_g_grappling_hook_tarzan >= 2)
209 if(self.aiment.movetype == MOVETYPE_WALK)
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;
220 self.realowner.flags &~= FL_ONGROUND;
223 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
227 end = self.origin - dir*50;
228 dist = vlen(end - myorg);
230 spd = dist * (pullspeed / 200);
235 self.realowner.velocity = dir*spd;
236 self.realowner.movetype = MOVETYPE_FLY;
238 self.realowner.flags &~= FL_ONGROUND;
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);
245 if(myorg != self.hook_start)
248 self.hook_start = myorg;
250 if(org != self.hook_end)
257 void GrapplingHookTouch (void)
261 GrapplingHook_Stop();
264 if(other.movetype != MOVETYPE_NONE)
266 SetMovetypeFollow(self, other);
267 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
270 //self.realowner.disableclientprediction = TRUE;
273 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
278 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
279 return; // g_balance_projectiledamage says to halt
281 self.health = self.health - damage;
283 if (self.health <= 0)
285 if(attacker != self.realowner)
287 self.realowner.pusher = attacker;
288 self.realowner.pushltime = time + autocvar_g_maxpushtime;
289 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
291 RemoveGrapplingHook(self.realowner);
295 void FireGrapplingHook (void)
302 if(forbidWeaponUse()) return;
303 if(self.vehicle) return;
305 makevectors(self.v_angle);
307 s = self.cvar_cl_gunalign;
308 if(s != 1 && s != 2 && s != 4)
309 s = 3; // default value
311 vs = hook_shotorigin[s];
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, ATTN_NORM);
315 org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
317 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
320 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
322 missile = WarpZone_RefSys_SpawnSameRefSys(self);
323 missile.owner = missile.realowner = self;
325 missile.reset = GrapplingHookReset;
326 missile.classname = "grapplinghook";
327 missile.flags = FL_PROJECTILE;
329 missile.movetype = MOVETYPE_FLY;
330 PROJECTILE_MAKETRIGGER(missile);
332 //setmodel (missile, "models/hook.md3"); // precision set below
333 setsize (missile, '-3 -3 -3', '3 3 3');
334 setorigin (missile, org);
336 missile.state = 0; // not latched onto anything
338 W_SetupProjectileVelocityEx(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
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;
347 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
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);
355 missile.hook_start = missile.hook_end = missile.origin;
357 Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
360 // void GrapplingHookFrame()
362 // // this function has been modified for Xonotic
363 // - if (self.BUTTON_HOOK && g_grappling_hook)
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();
372 // RemoveGrapplingHook(self);
374 // - self.button6_pressed_before = self.BUTTON_HOOK;
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)
379 void GrapplingHookFrame()
381 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
383 // offhand hook controls
386 if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
388 self.hook_state |= HOOK_FIRING;
389 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
394 self.hook_state |= HOOK_REMOVING;
395 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
398 self.hook_state &~= HOOK_RELEASING;
399 if(self.BUTTON_CROUCH)
401 self.hook_state &~= HOOK_PULLING;
402 //self.hook_state |= HOOK_RELEASING;
406 self.hook_state |= HOOK_PULLING;
407 //self.hook_state &~= HOOK_RELEASING;
410 else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
412 if(self.BUTTON_HOOK && !self.hook_switchweapon)
413 W_SwitchWeapon(WEP_HOOK);
415 self.hook_switchweapon = self.BUTTON_HOOK;
417 if(!g_grappling_hook && self.weapon != WEP_HOOK)
419 self.hook_state &~= HOOK_FIRING;
420 self.hook_state |= HOOK_REMOVING;
423 if (self.hook_state & HOOK_FIRING)
426 RemoveGrapplingHook(self);
428 self.hook_state &~= HOOK_FIRING;
430 else if(self.hook_state & HOOK_REMOVING)
433 RemoveGrapplingHook(self);
434 self.hook_state &~= HOOK_REMOVING;
438 // if I have no hook or it's not pulling yet, make sure I'm not flying!
439 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
441 self.movetype = MOVETYPE_WALK;
443 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
449 else if(self.hookimpulse == GRAPHOOK_RELEASE)
451 // remove hook, reset movement type
452 RemoveGrapplingHook(self);
456 /*else // make sure the player's movetype is correct
458 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
459 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
461 self.movetype = MOVETYPE_WALK;
464 // note: The hook entity does the actual pulling
467 void GrappleHookInit()
471 hook_shotorigin[0] = '8 8 -12';
472 hook_shotorigin[1] = '8 8 -12';
473 hook_shotorigin[2] = '8 8 -12';
474 hook_shotorigin[3] = '8 8 -12';
478 weapon_action(WEP_HOOK, WR_PRECACHE);
479 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
480 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
481 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
482 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
486 void SetGrappleHookBindings()
488 // this function has been modified for Xonotic
489 // don't remove these lines! old server or demos coud overwrite the new aliases
490 stuffcmd(self, "alias +hook +button6\n");
491 stuffcmd(self, "alias -hook -button6\n");