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 void GrapplingHookThink()
119 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
120 vector dir, org, end, v0, dv, v, myorg, vs;
121 if(self.owner.health <= 0 || self.owner.hook != self) // how did that happen?
122 { // well, better fix it anyway
126 if(LostMovetypeFollow(self))
128 RemoveGrapplingHook(self.owner);
132 WarpZone_RefSys_AddIncrementally(self, self.aiment);
134 self.nextthink = time;
136 s = self.owner.cvar_cl_gunalign;
137 if(s != 1 && s != 2 && s != 4)
138 s = 3; // default value
140 vs = hook_shotorigin[s];
142 makevectors(self.owner.v_angle);
143 org = self.owner.origin + self.owner.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
144 myorg = WarpZone_RefSys_TransformOrigin(self.owner, self, org);
146 if(self.hook_length < 0)
147 self.hook_length = vlen(myorg - self.origin);
151 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
152 // speed the rope is pulled with
154 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
155 // force the rope will use if it is stretched
157 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
158 // force the rope will use if it is stretched
160 minlength = cvar("g_balance_grapplehook_length_min");//100;
161 // minimal rope length
162 // if the rope goes below this length, it isn't pulled any more
164 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
165 // if the rope is stretched by more than this amount, more rope is
166 // given to you again
168 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
169 // while hanging on the rope, this friction component will help you a
170 // bit to control the rope
172 dir = self.origin - myorg;
174 dir = normalize(dir);
176 if(cvar("g_grappling_hook_tarzan"))
178 v = v0 = WarpZone_RefSys_TransformVelocity(self.owner, self, self.owner.velocity);
180 // first pull the rope...
181 if(self.owner.hook_state & HOOK_PULLING)
183 newlength = self.hook_length;
184 newlength = max(newlength - pullspeed * frametime, minlength);
186 if(newlength < dist - ropestretch) // overstretched?
188 newlength = dist - ropestretch;
189 if(v * dir < 0) // only if not already moving in hook direction
190 v = v + frametime * dir * rubberforce_overstretch;
193 self.hook_length = newlength;
196 if(self.owner.hook_state & HOOK_RELEASING)
199 self.hook_length = newlength;
203 // then pull the player
204 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
205 v = v * (1 - frametime * ropeairfriction);
206 v = v + frametime * dir * spd * rubberforce;
208 dv = ((v - v0) * dir) * dir;
209 if(cvar("g_grappling_hook_tarzan") >= 2)
211 if(self.aiment.movetype == MOVETYPE_WALK)
214 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
215 self.aiment.flags &~= FL_ONGROUND;
216 self.aiment.pusher = self.owner;
217 self.aiment.pushltime = time + cvar("g_maxpushtime");
221 self.owner.flags &~= FL_ONGROUND;
224 self.owner.velocity = WarpZone_RefSys_TransformVelocity(self, self.owner, v);
228 end = self.origin - dir*50;
229 dist = vlen(end - myorg);
231 spd = dist * (pullspeed / 200);
236 self.owner.velocity = dir*spd;
237 self.owner.movetype = MOVETYPE_FLY;
239 self.owner.flags &~= FL_ONGROUND;
243 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
244 myorg = WarpZone_RefSys_TransformOrigin(self, self.owner, self.origin) + v_forward * (-9);
246 // TODO turn into a csqc entity
247 WriteByte(MSG_BROADCAST, SVC_TEMPENTITY);
248 WriteByte(MSG_BROADCAST, TE_CSQC_BEAM);
249 WriteByte(MSG_BROADCAST, num_for_edict(self.owner));
250 WriteByte(MSG_BROADCAST, 0);
251 WriteCoord(MSG_BROADCAST, myorg_x);
252 WriteCoord(MSG_BROADCAST, myorg_y);
253 WriteCoord(MSG_BROADCAST, myorg_z);
254 WriteCoord(MSG_BROADCAST, org_x);
255 WriteCoord(MSG_BROADCAST, org_y);
256 WriteCoord(MSG_BROADCAST, org_z);
259 void GrapplingHookTouch (void)
263 if(SUB_NoImpactCheck())
265 RemoveGrapplingHook(self.owner);
270 GrapplingHook_Stop();
273 if(other.movetype != MOVETYPE_NONE)
275 SetMovetypeFollow(self, other);
276 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
279 //self.owner.disableclientprediction = TRUE;
282 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
286 self.health = self.health - damage;
287 if (self.health <= 0)
289 if(attacker != self.owner)
291 self.owner.pusher = attacker;
292 self.owner.pushltime = time + cvar("g_maxpushtime");
294 RemoveGrapplingHook(self.owner);
299 void FireGrapplingHook (void)
301 local entity missile;
306 if((arena_roundbased && time < warmup) || (time < game_starttime))
309 makevectors(self.v_angle);
311 s = self.cvar_cl_gunalign;
312 if(s != 1 && s != 2 && s != 4)
313 s = 3; // default value
315 vs = hook_shotorigin[s];
317 // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
318 sound (self, CHAN_WEAPON2, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
319 org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
320 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
322 missile = WarpZone_RefSys_SpawnSameRefSys(self);
323 missile.owner = self;
325 missile.classname = "grapplinghook";
327 missile.movetype = MOVETYPE_FLY;
328 PROJECTILE_MAKETRIGGER(missile);
330 setmodel (missile, "models/hook.md3"); // precision set below
331 setsize (missile, '-3 -3 -3', '3 3 3');
332 setorigin (missile, org);
334 missile.state = 0; // not latched onto anything
336 W_SetupProjectileVelocityEx(missile, v_forward, v_up, cvar("g_balance_grapplehook_speed_fly"), 0, 0, 0);
338 missile.angles = vectoangles (missile.velocity);
339 //missile.glow_color = 250; // 244, 250
340 //missile.glow_size = 120;
341 missile.touch = GrapplingHookTouch;
342 missile.think = GrapplingHookThink;
343 missile.nextthink = time + 0.1;
345 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
347 missile.health = cvar("g_balance_grapplehook_health");//120
348 missile.event_damage = GrapplingHook_Damage;
349 missile.takedamage = DAMAGE_AIM;
350 missile.damageforcescale = 0;
353 // void GrapplingHookFrame()
355 // // this function has been modified for Xonotic
356 // - if (self.BUTTON_HOOK && g_grappling_hook)
358 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
359 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
360 // - FireGrapplingHook();
365 // RemoveGrapplingHook(self);
367 // - self.button6_pressed_before = self.BUTTON_HOOK;
369 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
370 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
372 void GrapplingHookFrame()
374 if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
376 // offhand hook controls
379 if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
381 self.hook_state |= HOOK_FIRING;
382 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
387 self.hook_state |= HOOK_REMOVING;
388 self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
391 self.hook_state &~= HOOK_RELEASING;
392 if(self.BUTTON_CROUCH)
394 self.hook_state &~= HOOK_PULLING;
395 //self.hook_state |= HOOK_RELEASING;
399 self.hook_state |= HOOK_PULLING;
400 //self.hook_state &~= HOOK_RELEASING;
403 else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
405 if(self.BUTTON_HOOK && !self.hook_switchweapon)
406 W_SwitchWeapon(WEP_HOOK);
408 self.hook_switchweapon = self.BUTTON_HOOK;
410 if(!g_grappling_hook && self.weapon != WEP_HOOK)
412 self.hook_state &~= HOOK_FIRING;
413 self.hook_state |= HOOK_REMOVING;
416 if (self.hook_state & HOOK_FIRING)
419 RemoveGrapplingHook(self);
421 self.hook_state &~= HOOK_FIRING;
423 else if(self.hook_state & HOOK_REMOVING)
426 RemoveGrapplingHook(self);
427 self.hook_state &~= HOOK_REMOVING;
431 // if I have no hook or it's not pulling yet, make sure I'm not flying!
432 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
434 self.movetype = MOVETYPE_WALK;
436 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
442 else if(self.hookimpulse == GRAPHOOK_RELEASE)
444 // remove hook, reset movement type
445 RemoveGrapplingHook(self);
449 /*else // make sure the player's movetype is correct
451 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
452 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
454 self.movetype = MOVETYPE_WALK;
457 // note: The hook entity does the actual pulling
460 void GrappleHookInit()
464 hook_shotorigin[0] = '8 8 -12';
465 hook_shotorigin[1] = '8 8 -12';
466 hook_shotorigin[2] = '8 8 -12';
467 hook_shotorigin[3] = '8 8 -12';
471 weapon_action(WEP_HOOK, WR_PRECACHE);
472 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
473 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
474 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
475 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
479 void SetGrappleHookBindings()
481 // this function has been modified for Xonotic
482 // don't remove these lines! old server or demos coud overwrite the new aliases
483 stuffcmd(self, "alias +hook +button6\n");
484 stuffcmd(self, "alias -hook -button6\n");