4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../warpzonelib/common.qh"
7 #include "../warpzonelib/server.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
10 #include "../common/weapons/weapons.qh"
11 #include "autocvars.qh"
12 #include "constants.qh"
14 #include "vehicles/vehicles_def.qh"
15 #include "command/common.qh"
17 #include "round_handler.qh"
18 #include "weapons/common.qh"
21 /*============================================
23 Wazat's Xonotic Grappling Hook
25 Contact: Wazat1@gmail.com
28 Installation instructions:
29 --------------------------
31 1. Place hook.c in your gamec source directory with the other source files.
33 2. Add this line to the bottom of progs.src:
37 3. Open defs.h and add these lines to the very bottom:
39 // Wazat's grappling hook
41 void GrapplingHookFrame();
42 void RemoveGrapplingHook(entity pl);
43 void SetGrappleHookBindings();
45 const float GRAPHOOK_FIRE = 20;
46 const float GRAPHOOK_RELEASE = 21;
47 // (note: you can change the hook impulse #'s to whatever you please)
49 4. Open client.c and add this to the top of PutClientInServer():
51 RemoveGrapplingHook(self); // Wazat's Grappling Hook
53 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
55 // Wazat's grappling hook
56 SetGrappleHookBindings();
58 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
62 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
65 And you should be done!
68 ============================================*/
71 .float hook_switchweapon;
73 void RemoveGrapplingHook(entity pl)
79 if(pl.movetype == MOVETYPE_FLY)
80 pl.movetype = MOVETYPE_WALK;
82 //pl.disableclientprediction = false;
85 void GrapplingHookReset(void)
87 if(self.realowner.hook == self)
88 RemoveGrapplingHook(self.owner);
93 void GrapplingHookThink();
94 void GrapplingHook_Stop()
96 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
97 sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
100 self.think = GrapplingHookThink;
101 self.nextthink = time;
102 self.touch = func_null;
103 self.velocity = '0 0 0';
104 self.movetype = MOVETYPE_NONE;
105 self.hook_length = -1;
108 .vector hook_start, hook_end;
109 float GrapplingHookSend(entity to, float sf)
111 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
113 if(sound_allowed(MSG_BROADCAST, self.realowner))
115 WriteByte(MSG_ENTITY, sf);
118 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
122 WriteCoord(MSG_ENTITY, self.hook_start.x);
123 WriteCoord(MSG_ENTITY, self.hook_start.y);
124 WriteCoord(MSG_ENTITY, self.hook_start.z);
128 WriteCoord(MSG_ENTITY, self.hook_end.x);
129 WriteCoord(MSG_ENTITY, self.hook_end.y);
130 WriteCoord(MSG_ENTITY, self.hook_end.z);
135 void GrapplingHookThink()
137 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
138 vector dir, org, end, v0, dv, v, myorg, vs;
139 if(self.realowner.hook != self) // how did that happen?
141 error("Owner lost the hook!\n");
144 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
146 RemoveGrapplingHook(self.realowner);
150 WarpZone_RefSys_AddIncrementally(self, self.aiment);
152 self.nextthink = time;
154 s = self.realowner.cvar_cl_gunalign;
155 if(s != 1 && s != 2 && s != 4)
156 s = 3; // default value
158 vs = hook_shotorigin[s];
160 makevectors(self.realowner.v_angle);
161 org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
162 myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
164 if(self.hook_length < 0)
165 self.hook_length = vlen(myorg - self.origin);
169 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
170 // speed the rope is pulled with
172 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
173 // force the rope will use if it is stretched
175 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
176 // force the rope will use if it is stretched
178 minlength = autocvar_g_balance_grapplehook_length_min;//100;
179 // minimal rope length
180 // if the rope goes below this length, it isn't pulled any more
182 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
183 // if the rope is stretched by more than this amount, more rope is
184 // given to you again
186 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
187 // while hanging on the rope, this friction component will help you a
188 // bit to control the rope
190 dir = self.origin - myorg;
192 dir = normalize(dir);
194 if(autocvar_g_grappling_hook_tarzan)
196 v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
198 // first pull the rope...
199 if(self.realowner.hook_state & HOOK_PULLING)
201 newlength = self.hook_length;
202 newlength = max(newlength - pullspeed * frametime, minlength);
204 if(newlength < dist - ropestretch) // overstretched?
206 newlength = dist - ropestretch;
207 if(v * dir < 0) // only if not already moving in hook direction
208 v = v + frametime * dir * rubberforce_overstretch;
211 self.hook_length = newlength;
214 if(self.realowner.hook_state & HOOK_RELEASING)
217 self.hook_length = newlength;
221 // then pull the player
222 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
223 v = v * (1 - frametime * ropeairfriction);
224 v = v + frametime * dir * spd * rubberforce;
226 dv = ((v - v0) * dir) * dir;
227 if(autocvar_g_grappling_hook_tarzan >= 2)
229 if(self.aiment.movetype == MOVETYPE_WALK)
232 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
233 self.aiment.flags &= ~FL_ONGROUND;
234 self.aiment.pusher = self.realowner;
235 self.aiment.pushltime = time + autocvar_g_maxpushtime;
236 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
240 self.realowner.flags &= ~FL_ONGROUND;
243 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
247 end = self.origin - dir*50;
248 dist = vlen(end - myorg);
250 spd = dist * (pullspeed / 200);
255 self.realowner.velocity = dir*spd;
256 self.realowner.movetype = MOVETYPE_FLY;
258 self.realowner.flags &= ~FL_ONGROUND;
262 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
263 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
265 if(myorg != self.hook_start)
268 self.hook_start = myorg;
270 if(org != self.hook_end)
277 void GrapplingHookTouch (void)
281 GrapplingHook_Stop();
284 if(other.movetype != MOVETYPE_NONE)
286 SetMovetypeFollow(self, other);
287 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
290 //self.realowner.disableclientprediction = true;
293 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
298 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
299 return; // g_balance_projectiledamage says to halt
301 self.health = self.health - damage;
303 if (self.health <= 0)
305 if(attacker != self.realowner)
307 self.realowner.pusher = attacker;
308 self.realowner.pushltime = time + autocvar_g_maxpushtime;
309 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
311 RemoveGrapplingHook(self.realowner);
315 void FireGrapplingHook (void)
322 if(forbidWeaponUse()) return;
323 if(self.vehicle) return;
325 makevectors(self.v_angle);
327 s = self.cvar_cl_gunalign;
328 if(s != 1 && s != 2 && s != 4)
329 s = 3; // default value
331 vs = hook_shotorigin[s];
333 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
334 sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
335 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
337 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
340 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
342 missile = WarpZone_RefSys_SpawnSameRefSys(self);
343 missile.owner = missile.realowner = self;
345 missile.reset = GrapplingHookReset;
346 missile.classname = "grapplinghook";
347 missile.flags = FL_PROJECTILE;
349 missile.movetype = MOVETYPE_FLY;
350 PROJECTILE_MAKETRIGGER(missile);
352 //setmodel (missile, "models/hook.md3"); // precision set below
353 setsize (missile, '-3 -3 -3', '3 3 3');
354 setorigin (missile, org);
356 missile.state = 0; // not latched onto anything
358 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
360 missile.angles = vectoangles (missile.velocity);
361 //missile.glow_color = 250; // 244, 250
362 //missile.glow_size = 120;
363 missile.touch = GrapplingHookTouch;
364 missile.think = GrapplingHookThink;
365 missile.nextthink = time;
367 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
369 missile.health = autocvar_g_balance_grapplehook_health;//120
370 missile.event_damage = GrapplingHook_Damage;
371 missile.takedamage = DAMAGE_AIM;
372 missile.damageforcescale = 0;
373 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
375 missile.hook_start = missile.hook_end = missile.origin;
377 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
380 // void GrapplingHookFrame()
382 // // this function has been modified for Xonotic
383 // - if (self.BUTTON_HOOK && g_grappling_hook)
385 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
386 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
387 // - FireGrapplingHook();
392 // RemoveGrapplingHook(self);
394 // - self.button6_pressed_before = self.BUTTON_HOOK;
396 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
397 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
399 void GrapplingHookFrame()
401 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
403 // offhand hook controls
406 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
408 self.hook_state |= HOOK_FIRING;
409 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
414 self.hook_state |= HOOK_REMOVING;
415 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
418 self.hook_state &= ~HOOK_RELEASING;
419 if(self.BUTTON_CROUCH)
421 self.hook_state &= ~HOOK_PULLING;
422 //self.hook_state |= HOOK_RELEASING;
426 self.hook_state |= HOOK_PULLING;
427 //self.hook_state &= ~HOOK_RELEASING;
430 else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
432 if(self.BUTTON_HOOK && !self.hook_switchweapon)
433 W_SwitchWeapon(WEP_HOOK);
435 self.hook_switchweapon = self.BUTTON_HOOK;
437 if(!g_grappling_hook && self.weapon != WEP_HOOK)
439 self.hook_state &= ~HOOK_FIRING;
440 self.hook_state |= HOOK_REMOVING;
443 if (self.hook_state & HOOK_FIRING)
446 RemoveGrapplingHook(self);
448 self.hook_state &= ~HOOK_FIRING;
449 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
451 else if(self.hook_state & HOOK_REMOVING)
454 RemoveGrapplingHook(self);
455 self.hook_state &= ~HOOK_REMOVING;
459 // if I have no hook or it's not pulling yet, make sure I'm not flying!
460 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
462 self.movetype = MOVETYPE_WALK;
464 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
470 else if(self.hookimpulse == GRAPHOOK_RELEASE)
472 // remove hook, reset movement type
473 RemoveGrapplingHook(self);
477 /*else // make sure the player's movetype is correct
479 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
480 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
482 self.movetype = MOVETYPE_WALK;
485 // note: The hook entity does the actual pulling
488 void GrappleHookInit()
492 hook_shotorigin[0] = '8 8 -12';
493 hook_shotorigin[1] = '8 8 -12';
494 hook_shotorigin[2] = '8 8 -12';
495 hook_shotorigin[3] = '8 8 -12';
499 WEP_ACTION(WEP_HOOK, WR_INIT);
500 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
501 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
502 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
503 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
507 void SetGrappleHookBindings()
509 // this function has been modified for Xonotic
510 // don't remove these lines! old server or demos coud overwrite the new aliases
511 stuffcmd(self, "alias +hook +button6\n");
512 stuffcmd(self, "alias -hook -button6\n");