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 "../common/vehicles/all.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 Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1);
94 sound (self, CH_SHOTS, W_Sound("hook_impact"), 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()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
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);
164 int tarzan = autocvar_g_grappling_hook_tarzan;
165 entity pull_entity = self.realowner;
166 float velocity_multiplier = 1;
167 MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
168 tarzan = hook_tarzan;
169 pull_entity = hook_pullentity;
170 velocity_multiplier = hook_velmultiplier;
174 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
175 // speed the rope is pulled with
177 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
178 // force the rope will use if it is stretched
180 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
181 // force the rope will use if it is stretched
183 minlength = autocvar_g_balance_grapplehook_length_min;//100;
184 // minimal rope length
185 // if the rope goes below this length, it isn't pulled any more
187 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
188 // if the rope is stretched by more than this amount, more rope is
189 // given to you again
191 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
192 // while hanging on the rope, this friction component will help you a
193 // bit to control the rope
195 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
197 dir = self.origin - myorg;
199 dir = normalize(dir);
203 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
205 // first pull the rope...
206 if(self.realowner.hook_state & HOOK_PULLING)
208 newlength = self.hook_length;
209 newlength = max(newlength - pullspeed * frametime, minlength);
211 if(newlength < dist - ropestretch) // overstretched?
213 newlength = dist - ropestretch;
214 if(v * dir < 0) // only if not already moving in hook direction
215 v = v + frametime * dir * rubberforce_overstretch;
218 self.hook_length = newlength;
221 if(pull_entity.movetype == MOVETYPE_FLY)
222 pull_entity.movetype = MOVETYPE_WALK;
224 if(self.realowner.hook_state & HOOK_RELEASING)
227 self.hook_length = newlength;
231 // then pull the player
232 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
233 v = v * (1 - frametime * ropeairfriction);
234 v = v + frametime * dir * spd * rubberforce;
236 dv = ((v - v0) * dir) * dir;
239 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
241 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
243 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
245 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
246 self.aiment.flags &= ~FL_ONGROUND;
247 if(self.aiment.flags & FL_PROJECTILE)
248 UpdateCSQCProjectile(self.aiment);
250 if(self.aiment.classname == "nade")
251 self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
252 aim_ent.pusher = self.realowner;
253 aim_ent.pushltime = time + autocvar_g_maxpushtime;
254 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
258 pull_entity.flags &= ~FL_ONGROUND;
261 if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
262 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
264 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
266 RemoveGrapplingHook(self.realowner);
272 end = self.origin - dir*50;
273 dist = vlen(end - myorg);
275 spd = dist * (pullspeed / 200);
280 self.realowner.velocity = dir*spd;
281 self.realowner.movetype = MOVETYPE_FLY;
283 self.realowner.flags &= ~FL_ONGROUND;
287 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
288 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
290 if(myorg != self.hook_start)
293 self.hook_start = myorg;
295 if(org != self.hook_end)
302 void GrapplingHookTouch (void)
304 if(other.movetype == MOVETYPE_FOLLOW)
308 GrapplingHook_Stop();
311 if(other.movetype != MOVETYPE_NONE)
313 SetMovetypeFollow(self, other);
314 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
317 //self.realowner.disableclientprediction = true;
320 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
325 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
326 return; // g_balance_projectiledamage says to halt
328 self.health = self.health - damage;
330 if (self.health <= 0)
332 if(attacker != self.realowner)
334 self.realowner.pusher = attacker;
335 self.realowner.pushltime = time + autocvar_g_maxpushtime;
336 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
338 RemoveGrapplingHook(self.realowner);
342 void FireGrapplingHook (void)
348 if(forbidWeaponUse(self)) return;
349 if(self.vehicle) return;
351 makevectors(self.v_angle);
353 int s = self.cvar_cl_gunalign;
354 if(s != 1 && s != 2 && s != 4)
355 s = 3; // default value
357 vs = hook_shotorigin[s];
359 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
360 sound (self, CH_WEAPON_B, W_Sound("hook_fire"), VOL_BASE, ATTEN_NORM);
361 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
363 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
366 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
368 missile = WarpZone_RefSys_SpawnSameRefSys(self);
369 missile.owner = missile.realowner = self;
371 missile.reset = GrapplingHookReset;
372 missile.classname = "grapplinghook";
373 missile.flags = FL_PROJECTILE;
375 missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
376 PROJECTILE_MAKETRIGGER(missile);
378 //setmodel (missile, MDL_HOOK); // precision set below
379 setsize (missile, '-3 -3 -3', '3 3 3');
380 setorigin (missile, org);
382 missile.state = 0; // not latched onto anything
384 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
386 missile.angles = vectoangles (missile.velocity);
387 //missile.glow_color = 250; // 244, 250
388 //missile.glow_size = 120;
389 missile.touch = GrapplingHookTouch;
390 missile.think = GrapplingHookThink;
391 missile.nextthink = time;
393 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
395 missile.health = autocvar_g_balance_grapplehook_health;//120
396 missile.event_damage = GrapplingHook_Damage;
397 missile.takedamage = DAMAGE_AIM;
398 missile.damageforcescale = 0;
399 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
401 missile.hook_start = missile.hook_end = missile.origin;
403 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
406 // void GrapplingHookFrame()
408 // // this function has been modified for Xonotic
409 // - if (self.BUTTON_HOOK && g_grappling_hook)
411 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
412 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
413 // - FireGrapplingHook();
418 // RemoveGrapplingHook(self);
420 // - self.button6_pressed_before = self.BUTTON_HOOK;
422 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
423 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
425 void GrapplingHookFrame()
427 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK.m_id && !self.vehicle)
429 // offhand hook controls
432 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
434 self.hook_state |= HOOK_FIRING;
435 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
440 self.hook_state |= HOOK_REMOVING;
441 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
444 self.hook_state &= ~HOOK_RELEASING;
445 if(self.BUTTON_CROUCH && autocvar_g_balance_grapplehook_crouchslide)
447 self.hook_state &= ~HOOK_PULLING;
448 //self.hook_state |= HOOK_RELEASING;
452 self.hook_state |= HOOK_PULLING;
453 //self.hook_state &= ~HOOK_RELEASING;
456 else if(!g_grappling_hook && self.switchweapon != WEP_HOOK.m_id && !self.vehicle)
458 if(self.BUTTON_HOOK && !self.hook_switchweapon)
459 W_SwitchWeapon(WEP_HOOK.m_id);
461 self.hook_switchweapon = self.BUTTON_HOOK;
463 if(!g_grappling_hook && self.weapon != WEP_HOOK.m_id)
465 self.hook_state &= ~HOOK_FIRING;
466 self.hook_state |= HOOK_REMOVING;
469 if (self.hook_state & HOOK_FIRING)
472 RemoveGrapplingHook(self);
474 self.hook_state &= ~HOOK_FIRING;
475 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
477 else if(self.hook_state & HOOK_REMOVING)
480 RemoveGrapplingHook(self);
481 self.hook_state &= ~HOOK_REMOVING;
485 // if I have no hook or it's not pulling yet, make sure I'm not flying!
486 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
488 self.movetype = MOVETYPE_WALK;
490 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
496 else if(self.hookimpulse == GRAPHOOK_RELEASE)
498 // remove hook, reset movement type
499 RemoveGrapplingHook(self);
503 /*else // make sure the player's movetype is correct
505 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
506 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
508 self.movetype = MOVETYPE_WALK;
511 // note: The hook entity does the actual pulling
514 void GrappleHookInit()
518 hook_shotorigin[0] = '8 8 -12';
519 hook_shotorigin[1] = '8 8 -12';
520 hook_shotorigin[2] = '8 8 -12';
521 hook_shotorigin[3] = '8 8 -12';
525 WEP_ACTION(WEP_HOOK.m_id, WR_INIT);
526 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
527 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
528 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
529 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
533 void SetGrappleHookBindings()
535 // this function has been modified for Xonotic
536 // don't remove these lines! old server or demos coud overwrite the new aliases
537 stuffcmd(self, "alias +hook +button6\n");
538 stuffcmd(self, "alias -hook -button6\n");