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 = SUB_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.health <= 0 || self.realowner.hook != self) // how did that happen?
120 { // well, better fix it anyway
124 if(LostMovetypeFollow(self))
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;
219 self.realowner.flags &~= FL_ONGROUND;
222 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
226 end = self.origin - dir*50;
227 dist = vlen(end - myorg);
229 spd = dist * (pullspeed / 200);
234 self.realowner.velocity = dir*spd;
235 self.realowner.movetype = MOVETYPE_FLY;
237 self.realowner.flags &~= FL_ONGROUND;
241 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
242 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
244 if(myorg != self.hook_start)
247 self.hook_start = myorg;
249 if(org != self.hook_end)
256 void GrapplingHookTouch (void)
260 GrapplingHook_Stop();
263 if(other.movetype != MOVETYPE_NONE)
265 SetMovetypeFollow(self, other);
266 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
269 //self.realowner.disableclientprediction = TRUE;
272 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
276 self.health = self.health - damage;
277 if (self.health <= 0)
279 if(attacker != self.realowner)
281 self.realowner.pusher = attacker;
282 self.realowner.pushltime = time + autocvar_g_maxpushtime;
284 RemoveGrapplingHook(self.realowner);
289 void FireGrapplingHook (void)
291 local entity missile;
296 if((arena_roundbased && time < warmup) || (time < game_starttime))
299 if(self.freezetag_frozen)
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";
328 missile.movetype = MOVETYPE_FLY;
329 PROJECTILE_MAKETRIGGER(missile);
331 //setmodel (missile, "models/hook.md3"); // precision set below
332 setsize (missile, '-3 -3 -3', '3 3 3');
333 setorigin (missile, org);
335 missile.state = 0; // not latched onto anything
337 W_SetupProjectileVelocityEx(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
339 missile.angles = vectoangles (missile.velocity);
340 //missile.glow_color = 250; // 244, 250
341 //missile.glow_size = 120;
342 missile.touch = GrapplingHookTouch;
343 missile.think = GrapplingHookThink;
344 missile.nextthink = time;
346 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
348 missile.health = autocvar_g_balance_grapplehook_health;//120
349 missile.event_damage = GrapplingHook_Damage;
350 missile.takedamage = DAMAGE_AIM;
351 missile.damageforcescale = 0;
353 missile.hook_start = missile.hook_end = missile.origin;
355 Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
358 // void GrapplingHookFrame()
360 // // this function has been modified for Xonotic
361 // - if (self.BUTTON_HOOK && g_grappling_hook)
363 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
364 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
365 // - FireGrapplingHook();
370 // RemoveGrapplingHook(self);
372 // - self.button6_pressed_before = self.BUTTON_HOOK;
374 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
375 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
377 void GrapplingHookFrame()
379 if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
381 // offhand hook controls
384 if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
386 self.hook_state |= HOOK_FIRING;
387 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
392 self.hook_state |= HOOK_REMOVING;
393 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
396 self.hook_state &~= HOOK_RELEASING;
397 if(self.BUTTON_CROUCH)
399 self.hook_state &~= HOOK_PULLING;
400 //self.hook_state |= HOOK_RELEASING;
404 self.hook_state |= HOOK_PULLING;
405 //self.hook_state &~= HOOK_RELEASING;
408 else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
410 if(self.BUTTON_HOOK && !self.hook_switchweapon)
411 W_SwitchWeapon(WEP_HOOK);
413 self.hook_switchweapon = self.BUTTON_HOOK;
415 if(!g_grappling_hook && self.weapon != WEP_HOOK)
417 self.hook_state &~= HOOK_FIRING;
418 self.hook_state |= HOOK_REMOVING;
421 if (self.hook_state & HOOK_FIRING)
424 RemoveGrapplingHook(self);
426 self.hook_state &~= HOOK_FIRING;
428 else if(self.hook_state & HOOK_REMOVING)
431 RemoveGrapplingHook(self);
432 self.hook_state &~= HOOK_REMOVING;
436 // if I have no hook or it's not pulling yet, make sure I'm not flying!
437 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
439 self.movetype = MOVETYPE_WALK;
441 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
447 else if(self.hookimpulse == GRAPHOOK_RELEASE)
449 // remove hook, reset movement type
450 RemoveGrapplingHook(self);
454 /*else // make sure the player's movetype is correct
456 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
457 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
459 self.movetype = MOVETYPE_WALK;
462 // note: The hook entity does the actual pulling
465 void GrappleHookInit()
469 hook_shotorigin[0] = '8 8 -12';
470 hook_shotorigin[1] = '8 8 -12';
471 hook_shotorigin[2] = '8 8 -12';
472 hook_shotorigin[3] = '8 8 -12';
476 weapon_action(WEP_HOOK, WR_PRECACHE);
477 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
478 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
479 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
480 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
484 void SetGrappleHookBindings()
486 // this function has been modified for Xonotic
487 // don't remove these lines! old server or demos coud overwrite the new aliases
488 stuffcmd(self, "alias +hook +button6\n");
489 stuffcmd(self, "alias -hook -button6\n");