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, SND_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 int autocvar_g_grappling_hook_tarzan;
134 void GrapplingHookThink()
136 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
137 vector dir, org, end, v0, dv, v, myorg, vs;
138 if(self.realowner.hook != self) // how did that happen?
140 error("Owner lost the hook!\n");
143 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
145 RemoveGrapplingHook(self.realowner);
149 WarpZone_RefSys_AddIncrementally(self, self.aiment);
151 self.nextthink = time;
153 int s = self.realowner.cvar_cl_gunalign;
154 if(s != 1 && s != 2 && s != 4)
155 s = 3; // default value
157 vs = hook_shotorigin[s];
159 makevectors(self.realowner.v_angle);
160 org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
161 myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
163 if(self.hook_length < 0)
164 self.hook_length = vlen(myorg - self.origin);
166 int tarzan = autocvar_g_grappling_hook_tarzan;
167 entity pull_entity = self.realowner;
168 float velocity_multiplier = 1;
169 MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
170 tarzan = hook_tarzan;
171 pull_entity = hook_pullentity;
172 velocity_multiplier = hook_velmultiplier;
176 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
177 // speed the rope is pulled with
179 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
180 // force the rope will use if it is stretched
182 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
183 // force the rope will use if it is stretched
185 minlength = autocvar_g_balance_grapplehook_length_min;//100;
186 // minimal rope length
187 // if the rope goes below this length, it isn't pulled any more
189 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
190 // if the rope is stretched by more than this amount, more rope is
191 // given to you again
193 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
194 // while hanging on the rope, this friction component will help you a
195 // bit to control the rope
197 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
199 dir = self.origin - myorg;
201 dir = normalize(dir);
205 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
207 // first pull the rope...
208 if(self.realowner.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(pull_entity.movetype == MOVETYPE_FLY)
224 pull_entity.movetype = MOVETYPE_WALK;
226 if(self.realowner.hook_state & HOOK_RELEASING)
229 self.hook_length = newlength;
233 // then pull the player
234 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
235 v = v * (1 - frametime * ropeairfriction);
236 v = v + frametime * dir * spd * rubberforce;
238 dv = ((v - v0) * dir) * dir;
241 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
243 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
245 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
247 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
248 self.aiment.flags &= ~FL_ONGROUND;
249 if(self.aiment.flags & FL_PROJECTILE)
250 UpdateCSQCProjectile(self.aiment);
252 if(self.aiment.classname == "nade")
253 self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
254 aim_ent.pusher = self.realowner;
255 aim_ent.pushltime = time + autocvar_g_maxpushtime;
256 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
260 pull_entity.flags &= ~FL_ONGROUND;
263 if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
264 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
266 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
268 RemoveGrapplingHook(self.realowner);
274 end = self.origin - dir*50;
275 dist = vlen(end - myorg);
277 spd = dist * (pullspeed / 200);
282 self.realowner.velocity = dir*spd;
283 self.realowner.movetype = MOVETYPE_FLY;
285 self.realowner.flags &= ~FL_ONGROUND;
289 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
290 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
292 if(myorg != self.hook_start)
295 self.hook_start = myorg;
297 if(org != self.hook_end)
304 void GrapplingHookTouch (void)
306 if(other.movetype == MOVETYPE_FOLLOW)
310 GrapplingHook_Stop();
313 if(other.movetype != MOVETYPE_NONE)
315 SetMovetypeFollow(self, other);
316 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
319 //self.realowner.disableclientprediction = true;
322 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
327 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
328 return; // g_balance_projectiledamage says to halt
330 self.health = self.health - damage;
332 if (self.health <= 0)
334 if(attacker != self.realowner)
336 self.realowner.pusher = attacker;
337 self.realowner.pushltime = time + autocvar_g_maxpushtime;
338 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
340 RemoveGrapplingHook(self.realowner);
344 void FireGrapplingHook (void)
350 if(forbidWeaponUse(self)) return;
351 if(self.vehicle) return;
353 makevectors(self.v_angle);
355 int s = self.cvar_cl_gunalign;
356 if(s != 1 && s != 2 && s != 4)
357 s = 3; // default value
359 vs = hook_shotorigin[s];
361 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
362 sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
363 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
365 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
368 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
370 missile = WarpZone_RefSys_SpawnSameRefSys(self);
371 missile.owner = missile.realowner = self;
373 missile.reset = GrapplingHookReset;
374 missile.classname = "grapplinghook";
375 missile.flags = FL_PROJECTILE;
377 missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
378 PROJECTILE_MAKETRIGGER(missile);
380 //setmodel (missile, MDL_HOOK); // precision set below
381 setsize (missile, '-3 -3 -3', '3 3 3');
382 setorigin (missile, org);
384 missile.state = 0; // not latched onto anything
386 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
388 missile.angles = vectoangles (missile.velocity);
389 //missile.glow_color = 250; // 244, 250
390 //missile.glow_size = 120;
391 missile.touch = GrapplingHookTouch;
392 missile.think = GrapplingHookThink;
393 missile.nextthink = time;
395 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
397 missile.health = autocvar_g_balance_grapplehook_health;//120
398 missile.event_damage = GrapplingHook_Damage;
399 missile.takedamage = DAMAGE_AIM;
400 missile.damageforcescale = 0;
401 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
403 missile.hook_start = missile.hook_end = missile.origin;
405 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
408 // void GrapplingHookFrame()
410 // // this function has been modified for Xonotic
411 // - if (self.BUTTON_HOOK && g_grappling_hook)
413 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
414 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
415 // - FireGrapplingHook();
420 // RemoveGrapplingHook(self);
422 // - self.button6_pressed_before = self.BUTTON_HOOK;
424 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
425 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
427 void _GrapplingHookFrame();
428 void GrapplingHookFrame()
430 if (self.offhand == OFFHAND_HOOK) {
431 if (timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK.m_id && !self.vehicle) {
432 // offhand hook controls
433 if (self.BUTTON_HOOK) {
434 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire)) {
435 self.hook_state |= HOOK_FIRING;
436 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
439 self.hook_state |= HOOK_REMOVING;
440 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
443 self.hook_state &= ~HOOK_RELEASING;
444 if (self.BUTTON_CROUCH && autocvar_g_balance_grapplehook_crouchslide) {
445 self.hook_state &= ~HOOK_PULLING;
446 //self.hook_state |= HOOK_RELEASING;
448 self.hook_state |= HOOK_PULLING;
449 //self.hook_state &= ~HOOK_RELEASING;
453 if (self.switchweapon != WEP_HOOK.m_id && !self.vehicle) {
454 if (self.BUTTON_HOOK && !self.hook_switchweapon)
455 W_SwitchWeapon(WEP_HOOK.m_id);
457 if (self.weapon != WEP_HOOK.m_id && self.offhand != OFFHAND_HOOK) {
458 self.hook_state &= ~HOOK_FIRING;
459 self.hook_state |= HOOK_REMOVING;
462 self.hook_switchweapon = self.BUTTON_HOOK;
463 _GrapplingHookFrame();
466 void _GrapplingHookFrame()
468 if (self.hook_state & HOOK_FIRING)
471 RemoveGrapplingHook(self);
473 self.hook_state &= ~HOOK_FIRING;
474 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
476 else if(self.hook_state & HOOK_REMOVING)
479 RemoveGrapplingHook(self);
480 self.hook_state &= ~HOOK_REMOVING;
484 // if I have no hook or it's not pulling yet, make sure I'm not flying!
485 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
487 self.movetype = MOVETYPE_WALK;
489 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
495 else if(self.hookimpulse == GRAPHOOK_RELEASE)
497 // remove hook, reset movement type
498 RemoveGrapplingHook(self);
502 /*else // make sure the player's movetype is correct
504 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
505 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
507 self.movetype = MOVETYPE_WALK;
510 // note: The hook entity does the actual pulling
513 void GrappleHookInit()
517 hook_shotorigin[0] = '8 8 -12';
518 hook_shotorigin[1] = '8 8 -12';
519 hook_shotorigin[2] = '8 8 -12';
520 hook_shotorigin[3] = '8 8 -12';
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");