4 #include "weapons/common.qh"
5 #include "weapons/weaponsystem.qh"
6 #include "weapons/selection.qh"
7 #include "weapons/tracing.qh"
8 #include "cl_player.qh"
9 #include "command/common.qh"
10 #include "round_handler.qh"
11 #include "vehicles/vehicle.qh"
12 #include "../common/constants.qh"
13 #include "../common/util.qh"
14 #include "../common/weapons/all.qh"
15 #include "../warpzonelib/common.qh"
16 #include "../warpzonelib/server.qh"
18 /*============================================
20 Wazat's Xonotic Grappling Hook
22 Contact: Wazat1@gmail.com
25 Installation instructions:
26 --------------------------
28 1. Place hook.c in your gamec source directory with the other source files.
30 2. Add this line to the bottom of progs.src:
34 3. Open defs.h and add these lines to the very bottom:
36 // Wazat's grappling hook
38 void GrapplingHookFrame();
39 void RemoveGrapplingHook(entity pl);
40 void SetGrappleHookBindings();
42 const float GRAPHOOK_FIRE = 20;
43 const float GRAPHOOK_RELEASE = 21;
44 // (note: you can change the hook impulse #'s to whatever you please)
46 4. Open client.c and add this to the top of PutClientInServer():
48 RemoveGrapplingHook(self); // Wazat's Grappling Hook
50 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
52 // Wazat's grappling hook
53 SetGrappleHookBindings();
55 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
59 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
62 And you should be done!
65 ============================================*/
68 .float hook_switchweapon;
70 void RemoveGrapplingHook(entity pl)
76 if(pl.movetype == MOVETYPE_FLY)
77 pl.movetype = MOVETYPE_WALK;
79 //pl.disableclientprediction = false;
82 void GrapplingHookReset(void)
84 if(self.realowner.hook == self)
85 RemoveGrapplingHook(self.owner);
90 void GrapplingHookThink();
91 void GrapplingHook_Stop()
93 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
94 sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
97 self.think = GrapplingHookThink;
98 self.nextthink = time;
99 self.touch = func_null;
100 self.velocity = '0 0 0';
101 self.movetype = MOVETYPE_NONE;
102 self.hook_length = -1;
105 .vector hook_start, hook_end;
106 float GrapplingHookSend(entity to, int sf)
108 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
110 if(sound_allowed(MSG_BROADCAST, self.realowner))
112 WriteByte(MSG_ENTITY, sf);
115 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
119 WriteCoord(MSG_ENTITY, self.hook_start.x);
120 WriteCoord(MSG_ENTITY, self.hook_start.y);
121 WriteCoord(MSG_ENTITY, self.hook_start.z);
125 WriteCoord(MSG_ENTITY, self.hook_end.x);
126 WriteCoord(MSG_ENTITY, self.hook_end.y);
127 WriteCoord(MSG_ENTITY, self.hook_end.z);
132 void GrapplingHookThink()
134 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
135 vector dir, org, end, v0, dv, v, myorg, vs;
136 if(self.realowner.hook != self) // how did that happen?
138 error("Owner lost the hook!\n");
141 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
143 RemoveGrapplingHook(self.realowner);
147 WarpZone_RefSys_AddIncrementally(self, self.aiment);
149 self.nextthink = time;
151 int s = self.realowner.cvar_cl_gunalign;
152 if(s != 1 && s != 2 && s != 4)
153 s = 3; // default value
155 vs = hook_shotorigin[s];
157 makevectors(self.realowner.v_angle);
158 org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
159 myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
161 if(self.hook_length < 0)
162 self.hook_length = vlen(myorg - self.origin);
166 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
167 // speed the rope is pulled with
169 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
170 // force the rope will use if it is stretched
172 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
173 // force the rope will use if it is stretched
175 minlength = autocvar_g_balance_grapplehook_length_min;//100;
176 // minimal rope length
177 // if the rope goes below this length, it isn't pulled any more
179 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
180 // if the rope is stretched by more than this amount, more rope is
181 // given to you again
183 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
184 // while hanging on the rope, this friction component will help you a
185 // bit to control the rope
187 dir = self.origin - myorg;
189 dir = normalize(dir);
191 if(autocvar_g_grappling_hook_tarzan)
193 v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
195 // first pull the rope...
196 if(self.realowner.hook_state & HOOK_PULLING)
198 newlength = self.hook_length;
199 newlength = max(newlength - pullspeed * frametime, minlength);
201 if(newlength < dist - ropestretch) // overstretched?
203 newlength = dist - ropestretch;
204 if(v * dir < 0) // only if not already moving in hook direction
205 v = v + frametime * dir * rubberforce_overstretch;
208 self.hook_length = newlength;
211 if(self.realowner.hook_state & HOOK_RELEASING)
214 self.hook_length = newlength;
218 // then pull the player
219 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
220 v = v * (1 - frametime * ropeairfriction);
221 v = v + frametime * dir * spd * rubberforce;
223 dv = ((v - v0) * dir) * dir;
224 if(autocvar_g_grappling_hook_tarzan >= 2)
226 if(self.aiment.movetype == MOVETYPE_WALK)
229 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
230 self.aiment.flags &= ~FL_ONGROUND;
231 self.aiment.pusher = self.realowner;
232 self.aiment.pushltime = time + autocvar_g_maxpushtime;
233 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
237 self.realowner.flags &= ~FL_ONGROUND;
240 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
244 end = self.origin - dir*50;
245 dist = vlen(end - myorg);
247 spd = dist * (pullspeed / 200);
252 self.realowner.velocity = dir*spd;
253 self.realowner.movetype = MOVETYPE_FLY;
255 self.realowner.flags &= ~FL_ONGROUND;
259 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
260 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
262 if(myorg != self.hook_start)
265 self.hook_start = myorg;
267 if(org != self.hook_end)
274 void GrapplingHookTouch (void)
278 GrapplingHook_Stop();
281 if(other.movetype != MOVETYPE_NONE)
283 SetMovetypeFollow(self, other);
284 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
287 //self.realowner.disableclientprediction = true;
290 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
295 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
296 return; // g_balance_projectiledamage says to halt
298 self.health = self.health - damage;
300 if (self.health <= 0)
302 if(attacker != self.realowner)
304 self.realowner.pusher = attacker;
305 self.realowner.pushltime = time + autocvar_g_maxpushtime;
306 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
308 RemoveGrapplingHook(self.realowner);
312 void FireGrapplingHook (void)
318 if(forbidWeaponUse()) return;
319 if(self.vehicle) return;
321 makevectors(self.v_angle);
323 int s = self.cvar_cl_gunalign;
324 if(s != 1 && s != 2 && s != 4)
325 s = 3; // default value
327 vs = hook_shotorigin[s];
329 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
330 sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
331 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
333 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
336 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
338 missile = WarpZone_RefSys_SpawnSameRefSys(self);
339 missile.owner = missile.realowner = self;
341 missile.reset = GrapplingHookReset;
342 missile.classname = "grapplinghook";
343 missile.flags = FL_PROJECTILE;
345 missile.movetype = MOVETYPE_FLY;
346 PROJECTILE_MAKETRIGGER(missile);
348 //setmodel (missile, "models/hook.md3"); // precision set below
349 setsize (missile, '-3 -3 -3', '3 3 3');
350 setorigin (missile, org);
352 missile.state = 0; // not latched onto anything
354 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
356 missile.angles = vectoangles (missile.velocity);
357 //missile.glow_color = 250; // 244, 250
358 //missile.glow_size = 120;
359 missile.touch = GrapplingHookTouch;
360 missile.think = GrapplingHookThink;
361 missile.nextthink = time;
363 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
365 missile.health = autocvar_g_balance_grapplehook_health;//120
366 missile.event_damage = GrapplingHook_Damage;
367 missile.takedamage = DAMAGE_AIM;
368 missile.damageforcescale = 0;
369 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
371 missile.hook_start = missile.hook_end = missile.origin;
373 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
376 // void GrapplingHookFrame()
378 // // this function has been modified for Xonotic
379 // - if (self.BUTTON_HOOK && g_grappling_hook)
381 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
382 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
383 // - FireGrapplingHook();
388 // RemoveGrapplingHook(self);
390 // - self.button6_pressed_before = self.BUTTON_HOOK;
392 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
393 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
395 void GrapplingHookFrame()
397 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
399 // offhand hook controls
402 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
404 self.hook_state |= HOOK_FIRING;
405 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
410 self.hook_state |= HOOK_REMOVING;
411 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
414 self.hook_state &= ~HOOK_RELEASING;
415 if(self.BUTTON_CROUCH)
417 self.hook_state &= ~HOOK_PULLING;
418 //self.hook_state |= HOOK_RELEASING;
422 self.hook_state |= HOOK_PULLING;
423 //self.hook_state &= ~HOOK_RELEASING;
426 else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
428 if(self.BUTTON_HOOK && !self.hook_switchweapon)
429 W_SwitchWeapon(WEP_HOOK);
431 self.hook_switchweapon = self.BUTTON_HOOK;
433 if(!g_grappling_hook && self.weapon != WEP_HOOK)
435 self.hook_state &= ~HOOK_FIRING;
436 self.hook_state |= HOOK_REMOVING;
439 if (self.hook_state & HOOK_FIRING)
442 RemoveGrapplingHook(self);
444 self.hook_state &= ~HOOK_FIRING;
445 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
447 else if(self.hook_state & HOOK_REMOVING)
450 RemoveGrapplingHook(self);
451 self.hook_state &= ~HOOK_REMOVING;
455 // if I have no hook or it's not pulling yet, make sure I'm not flying!
456 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
458 self.movetype = MOVETYPE_WALK;
460 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
466 else if(self.hookimpulse == GRAPHOOK_RELEASE)
468 // remove hook, reset movement type
469 RemoveGrapplingHook(self);
473 /*else // make sure the player's movetype is correct
475 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
476 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
478 self.movetype = MOVETYPE_WALK;
481 // note: The hook entity does the actual pulling
484 void GrappleHookInit()
488 hook_shotorigin[0] = '8 8 -12';
489 hook_shotorigin[1] = '8 8 -12';
490 hook_shotorigin[2] = '8 8 -12';
491 hook_shotorigin[3] = '8 8 -12';
495 WEP_ACTION(WEP_HOOK, WR_INIT);
496 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
497 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
498 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
499 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
503 void SetGrappleHookBindings()
505 // this function has been modified for Xonotic
506 // don't remove these lines! old server or demos coud overwrite the new aliases
507 stuffcmd(self, "alias +hook +button6\n");
508 stuffcmd(self, "alias -hook -button6\n");