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 = cvar("g_balance_grapplehook_speed_pull");//2000;
179 // speed the rope is pulled with
181 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
182 // force the rope will use if it is stretched
184 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
185 // force the rope will use if it is stretched
187 minlength = cvar("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 = cvar("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 = cvar("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(cvar("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(cvar("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 + cvar("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 + cvar("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;
346 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
348 missile = WarpZone_RefSys_SpawnSameRefSys(self);
349 missile.owner = self;
351 missile.classname = "grapplinghook";
353 missile.movetype = MOVETYPE_FLY;
354 PROJECTILE_MAKETRIGGER(missile);
356 //setmodel (missile, "models/hook.md3"); // precision set below
357 setsize (missile, '-3 -3 -3', '3 3 3');
358 setorigin (missile, org);
360 missile.state = 0; // not latched onto anything
362 W_SetupProjectileVelocityEx(missile, v_forward, v_up, cvar("g_balance_grapplehook_speed_fly"), 0, 0, 0);
364 missile.angles = vectoangles (missile.velocity);
365 //missile.glow_color = 250; // 244, 250
366 //missile.glow_size = 120;
367 missile.touch = GrapplingHookTouch;
368 missile.think = GrapplingHookThink;
369 missile.nextthink = time + 0.1;
371 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
373 missile.health = cvar("g_balance_grapplehook_health");//120
374 missile.event_damage = GrapplingHook_Damage;
375 missile.takedamage = DAMAGE_AIM;
376 missile.damageforcescale = 0;
378 missile.hook_start = missile.hook_end = missile.origin;
380 Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
383 // void GrapplingHookFrame()
385 // // this function has been modified for Xonotic
386 // - if (self.BUTTON_HOOK && g_grappling_hook)
388 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
389 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
390 // - FireGrapplingHook();
395 // RemoveGrapplingHook(self);
397 // - self.button6_pressed_before = self.BUTTON_HOOK;
399 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
400 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
402 void GrapplingHookFrame()
404 if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
406 // offhand hook controls
409 if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
411 self.hook_state |= HOOK_FIRING;
412 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
417 self.hook_state |= HOOK_REMOVING;
418 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
421 self.hook_state &~= HOOK_RELEASING;
422 if(self.BUTTON_CROUCH)
424 self.hook_state &~= HOOK_PULLING;
425 //self.hook_state |= HOOK_RELEASING;
429 self.hook_state |= HOOK_PULLING;
430 //self.hook_state &~= HOOK_RELEASING;
433 else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
435 if(self.BUTTON_HOOK && !self.hook_switchweapon)
436 W_SwitchWeapon(WEP_HOOK);
438 self.hook_switchweapon = self.BUTTON_HOOK;
440 if(!g_grappling_hook && self.weapon != WEP_HOOK)
442 self.hook_state &~= HOOK_FIRING;
443 self.hook_state |= HOOK_REMOVING;
446 if (self.hook_state & HOOK_FIRING)
449 RemoveGrapplingHook(self);
451 self.hook_state &~= HOOK_FIRING;
453 else if(self.hook_state & HOOK_REMOVING)
456 RemoveGrapplingHook(self);
457 self.hook_state &~= HOOK_REMOVING;
461 // if I have no hook or it's not pulling yet, make sure I'm not flying!
462 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
464 self.movetype = MOVETYPE_WALK;
466 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
472 else if(self.hookimpulse == GRAPHOOK_RELEASE)
474 // remove hook, reset movement type
475 RemoveGrapplingHook(self);
479 /*else // make sure the player's movetype is correct
481 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
482 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
484 self.movetype = MOVETYPE_WALK;
487 // note: The hook entity does the actual pulling
490 void GrappleHookInit()
494 hook_shotorigin[0] = '8 8 -12';
495 hook_shotorigin[1] = '8 8 -12';
496 hook_shotorigin[2] = '8 8 -12';
497 hook_shotorigin[3] = '8 8 -12';
501 weapon_action(WEP_HOOK, WR_PRECACHE);
502 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
503 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
504 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
505 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
509 void SetGrappleHookBindings()
511 // this function has been modified for Xonotic
512 // don't remove these lines! old server or demos coud overwrite the new aliases
513 stuffcmd(self, "alias +hook +button6\n");
514 stuffcmd(self, "alias -hook -button6\n");