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 ============================================*/
50 .string aiment_classname;
51 .float aiment_deadflag;
52 void SetMovetypeFollow(entity ent, entity e)
54 // FIXME this may not be warpzone aware
55 ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
56 ent.solid = SOLID_NOT; // MOVETYPE_FOLLOW is always non-solid - this means this cannot be teleported by warpzones any more! Instead, we must notice when our owner gets teleported.
57 ent.aiment = e; // make the hole follow bmodel
58 ent.punchangle = e.angles; // the original angles of bmodel
59 ent.view_ofs = ent.origin - e.origin; // relative origin
60 ent.v_angle = ent.angles - e.angles; // relative angles
61 ent.aiment_classname = strzone(e.classname);
62 ent.aiment_deadflag = e.deadflag;
64 void UnsetMovetypeFollow(entity ent)
66 ent.movetype = MOVETYPE_FLY;
67 PROJECTILE_MAKETRIGGER(ent);
70 float LostMovetypeFollow(entity ent)
73 if(ent.movetype != MOVETYPE_FOLLOW)
79 if(ent.aiment.classname != ent.aiment_classname)
81 if(ent.aiment.deadflag != ent.aiment_deadflag)
88 .float hook_switchweapon;
90 void RemoveGrapplingHook(entity pl)
96 if(pl.movetype == MOVETYPE_FLY)
97 pl.movetype = MOVETYPE_WALK;
99 //pl.disableclientprediction = FALSE;
102 void GrapplingHookThink();
103 void GrapplingHook_Stop()
105 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
106 sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
109 self.think = GrapplingHookThink;
110 self.nextthink = time;
111 self.touch = SUB_Null;
112 self.velocity = '0 0 0';
113 self.movetype = MOVETYPE_NONE;
114 self.hook_length = -1;
117 .vector hook_start, hook_end;
118 float GrapplingHookSend(entity to, float sf)
120 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
122 if(sound_allowed(MSG_BROADCAST, self.owner))
124 WriteByte(MSG_ENTITY, sf);
127 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
131 WriteCoord(MSG_ENTITY, self.hook_start_x);
132 WriteCoord(MSG_ENTITY, self.hook_start_y);
133 WriteCoord(MSG_ENTITY, self.hook_start_z);
137 WriteCoord(MSG_ENTITY, self.hook_end_x);
138 WriteCoord(MSG_ENTITY, self.hook_end_y);
139 WriteCoord(MSG_ENTITY, self.hook_end_z);
144 void GrapplingHookThink()
146 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
147 vector dir, org, end, v0, dv, v, myorg, vs;
148 if(self.owner.health <= 0 || self.owner.hook != self) // how did that happen?
149 { // well, better fix it anyway
153 if(LostMovetypeFollow(self))
155 RemoveGrapplingHook(self.owner);
159 WarpZone_RefSys_AddIncrementally(self, self.aiment);
161 self.nextthink = time;
163 s = self.owner.cvar_cl_gunalign;
164 if(s != 1 && s != 2 && s != 4)
165 s = 3; // default value
167 vs = hook_shotorigin[s];
169 makevectors(self.owner.v_angle);
170 org = self.owner.origin + self.owner.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
171 myorg = WarpZone_RefSys_TransformOrigin(self.owner, self, org);
173 if(self.hook_length < 0)
174 self.hook_length = vlen(myorg - self.origin);
178 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
179 // speed the rope is pulled with
181 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
182 // force the rope will use if it is stretched
184 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
185 // force the rope will use if it is stretched
187 minlength = autocvar_g_balance_grapplehook_length_min;//100;
188 // minimal rope length
189 // if the rope goes below this length, it isn't pulled any more
191 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
192 // if the rope is stretched by more than this amount, more rope is
193 // given to you again
195 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
196 // while hanging on the rope, this friction component will help you a
197 // bit to control the rope
199 dir = self.origin - myorg;
201 dir = normalize(dir);
203 if(autocvar_g_grappling_hook_tarzan)
205 v = v0 = WarpZone_RefSys_TransformVelocity(self.owner, self, self.owner.velocity);
207 // first pull the rope...
208 if(self.owner.hook_state & HOOK_PULLING)
210 newlength = self.hook_length;
211 newlength = max(newlength - pullspeed * frametime, minlength);
213 if(newlength < dist - ropestretch) // overstretched?
215 newlength = dist - ropestretch;
216 if(v * dir < 0) // only if not already moving in hook direction
217 v = v + frametime * dir * rubberforce_overstretch;
220 self.hook_length = newlength;
223 if(self.owner.hook_state & HOOK_RELEASING)
226 self.hook_length = newlength;
230 // then pull the player
231 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
232 v = v * (1 - frametime * ropeairfriction);
233 v = v + frametime * dir * spd * rubberforce;
235 dv = ((v - v0) * dir) * dir;
236 if(autocvar_g_grappling_hook_tarzan >= 2)
238 if(self.aiment.movetype == MOVETYPE_WALK)
241 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
242 self.aiment.flags &~= FL_ONGROUND;
243 self.aiment.pusher = self.owner;
244 self.aiment.pushltime = time + autocvar_g_maxpushtime;
248 self.owner.flags &~= FL_ONGROUND;
251 self.owner.velocity = WarpZone_RefSys_TransformVelocity(self, self.owner, v);
255 end = self.origin - dir*50;
256 dist = vlen(end - myorg);
258 spd = dist * (pullspeed / 200);
263 self.owner.velocity = dir*spd;
264 self.owner.movetype = MOVETYPE_FLY;
266 self.owner.flags &~= FL_ONGROUND;
270 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
271 myorg = WarpZone_RefSys_TransformOrigin(self, self.owner, self.origin); // + v_forward * (-9);
273 if(myorg != self.hook_start)
276 self.hook_start = myorg;
278 if(org != self.hook_end)
285 void GrapplingHookTouch (void)
289 if(SUB_NoImpactCheck())
291 RemoveGrapplingHook(self.owner);
296 GrapplingHook_Stop();
299 if(other.movetype != MOVETYPE_NONE)
301 SetMovetypeFollow(self, other);
302 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
305 //self.owner.disableclientprediction = TRUE;
308 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
312 self.health = self.health - damage;
313 if (self.health <= 0)
315 if(attacker != self.owner)
317 self.owner.pusher = attacker;
318 self.owner.pushltime = time + autocvar_g_maxpushtime;
320 RemoveGrapplingHook(self.owner);
325 void FireGrapplingHook (void)
327 local entity missile;
332 if((arena_roundbased && time < warmup) || (time < game_starttime))
335 makevectors(self.v_angle);
337 s = self.cvar_cl_gunalign;
338 if(s != 1 && s != 2 && s != 4)
339 s = 3; // default value
341 vs = hook_shotorigin[s];
343 // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
344 sound (self, CHAN_WEAPON2, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
345 org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
347 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
350 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
352 missile = WarpZone_RefSys_SpawnSameRefSys(self);
353 missile.owner = self;
355 missile.classname = "grapplinghook";
357 missile.movetype = MOVETYPE_FLY;
358 PROJECTILE_MAKETRIGGER(missile);
360 //setmodel (missile, "models/hook.md3"); // precision set below
361 setsize (missile, '-3 -3 -3', '3 3 3');
362 setorigin (missile, org);
364 missile.state = 0; // not latched onto anything
366 W_SetupProjectileVelocityEx(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
368 missile.angles = vectoangles (missile.velocity);
369 //missile.glow_color = 250; // 244, 250
370 //missile.glow_size = 120;
371 missile.touch = GrapplingHookTouch;
372 missile.think = GrapplingHookThink;
373 missile.nextthink = time;
375 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
377 missile.health = autocvar_g_balance_grapplehook_health;//120
378 missile.event_damage = GrapplingHook_Damage;
379 missile.takedamage = DAMAGE_AIM;
380 missile.damageforcescale = 0;
382 missile.hook_start = missile.hook_end = missile.origin;
384 Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
387 // void GrapplingHookFrame()
389 // // this function has been modified for Xonotic
390 // - if (self.BUTTON_HOOK && g_grappling_hook)
392 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
393 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
394 // - FireGrapplingHook();
399 // RemoveGrapplingHook(self);
401 // - self.button6_pressed_before = self.BUTTON_HOOK;
403 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
404 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
406 void GrapplingHookFrame()
408 if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
410 // offhand hook controls
413 if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
415 self.hook_state |= HOOK_FIRING;
416 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
421 self.hook_state |= HOOK_REMOVING;
422 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
425 self.hook_state &~= HOOK_RELEASING;
426 if(self.BUTTON_CROUCH)
428 self.hook_state &~= HOOK_PULLING;
429 //self.hook_state |= HOOK_RELEASING;
433 self.hook_state |= HOOK_PULLING;
434 //self.hook_state &~= HOOK_RELEASING;
437 else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
439 if(self.BUTTON_HOOK && !self.hook_switchweapon)
440 W_SwitchWeapon(WEP_HOOK);
442 self.hook_switchweapon = self.BUTTON_HOOK;
444 if(!g_grappling_hook && self.weapon != WEP_HOOK)
446 self.hook_state &~= HOOK_FIRING;
447 self.hook_state |= HOOK_REMOVING;
450 if (self.hook_state & HOOK_FIRING)
453 RemoveGrapplingHook(self);
455 self.hook_state &~= HOOK_FIRING;
457 else if(self.hook_state & HOOK_REMOVING)
460 RemoveGrapplingHook(self);
461 self.hook_state &~= HOOK_REMOVING;
465 // if I have no hook or it's not pulling yet, make sure I'm not flying!
466 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
468 self.movetype = MOVETYPE_WALK;
470 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
476 else if(self.hookimpulse == GRAPHOOK_RELEASE)
478 // remove hook, reset movement type
479 RemoveGrapplingHook(self);
483 /*else // make sure the player's movetype is correct
485 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
486 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
488 self.movetype = MOVETYPE_WALK;
491 // note: The hook entity does the actual pulling
494 void GrappleHookInit()
498 hook_shotorigin[0] = '8 8 -12';
499 hook_shotorigin[1] = '8 8 -12';
500 hook_shotorigin[2] = '8 8 -12';
501 hook_shotorigin[3] = '8 8 -12';
505 weapon_action(WEP_HOOK, WR_PRECACHE);
506 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
507 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
508 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
509 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
513 void SetGrappleHookBindings()
515 // this function has been modified for Xonotic
516 // don't remove these lines! old server or demos coud overwrite the new aliases
517 stuffcmd(self, "alias +hook +button6\n");
518 stuffcmd(self, "alias -hook -button6\n");