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"
20 /*============================================
22 Wazat's Xonotic Grappling Hook
24 Contact: Wazat1@gmail.com
27 Installation instructions:
28 --------------------------
30 1. Place hook.c in your gamec source directory with the other source files.
32 2. Add this line to the bottom of progs.src:
36 3. Open defs.h and add these lines to the very bottom:
38 // Wazat's grappling hook
40 void GrapplingHookFrame();
41 void RemoveGrapplingHook(entity pl);
42 void SetGrappleHookBindings();
44 const float GRAPHOOK_FIRE = 20;
45 const float GRAPHOOK_RELEASE = 21;
46 // (note: you can change the hook impulse #'s to whatever you please)
48 4. Open client.c and add this to the top of PutClientInServer():
50 RemoveGrapplingHook(self); // Wazat's Grappling Hook
52 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
54 // Wazat's grappling hook
55 SetGrappleHookBindings();
57 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
61 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
64 And you should be done!
67 ============================================*/
70 .float hook_switchweapon;
72 void RemoveGrapplingHook(entity pl)
78 if(pl.movetype == MOVETYPE_FLY)
79 pl.movetype = MOVETYPE_WALK;
81 //pl.disableclientprediction = false;
84 void GrapplingHookReset(void)
86 if(self.realowner.hook == self)
87 RemoveGrapplingHook(self.owner);
92 void GrapplingHookThink();
93 void GrapplingHook_Stop()
95 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
96 sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
99 self.think = GrapplingHookThink;
100 self.nextthink = time;
101 self.touch = func_null;
102 self.velocity = '0 0 0';
103 self.movetype = MOVETYPE_NONE;
104 self.hook_length = -1;
107 .vector hook_start, hook_end;
108 float GrapplingHookSend(entity to, float sf)
110 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
112 if(sound_allowed(MSG_BROADCAST, self.realowner))
114 WriteByte(MSG_ENTITY, sf);
117 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
121 WriteCoord(MSG_ENTITY, self.hook_start.x);
122 WriteCoord(MSG_ENTITY, self.hook_start.y);
123 WriteCoord(MSG_ENTITY, self.hook_start.z);
127 WriteCoord(MSG_ENTITY, self.hook_end.x);
128 WriteCoord(MSG_ENTITY, self.hook_end.y);
129 WriteCoord(MSG_ENTITY, self.hook_end.z);
134 void GrapplingHookThink()
136 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
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()))
145 RemoveGrapplingHook(self.realowner);
149 WarpZone_RefSys_AddIncrementally(self, self.aiment);
151 self.nextthink = time;
153 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);
168 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
169 // speed the rope is pulled with
171 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
172 // force the rope will use if it is stretched
174 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
175 // force the rope will use if it is stretched
177 minlength = autocvar_g_balance_grapplehook_length_min;//100;
178 // minimal rope length
179 // if the rope goes below this length, it isn't pulled any more
181 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
182 // if the rope is stretched by more than this amount, more rope is
183 // given to you again
185 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
186 // while hanging on the rope, this friction component will help you a
187 // bit to control the rope
189 dir = self.origin - myorg;
191 dir = normalize(dir);
193 if(autocvar_g_grappling_hook_tarzan)
195 v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
197 // first pull the rope...
198 if(self.realowner.hook_state & HOOK_PULLING)
200 newlength = self.hook_length;
201 newlength = max(newlength - pullspeed * frametime, minlength);
203 if(newlength < dist - ropestretch) // overstretched?
205 newlength = dist - ropestretch;
206 if(v * dir < 0) // only if not already moving in hook direction
207 v = v + frametime * dir * rubberforce_overstretch;
210 self.hook_length = newlength;
213 if(self.realowner.hook_state & HOOK_RELEASING)
216 self.hook_length = newlength;
220 // then pull the player
221 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
222 v = v * (1 - frametime * ropeairfriction);
223 v = v + frametime * dir * spd * rubberforce;
225 dv = ((v - v0) * dir) * dir;
226 if(autocvar_g_grappling_hook_tarzan >= 2)
228 if(self.aiment.movetype == MOVETYPE_WALK)
231 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
232 self.aiment.flags &= ~FL_ONGROUND;
233 self.aiment.pusher = self.realowner;
234 self.aiment.pushltime = time + autocvar_g_maxpushtime;
235 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
239 self.realowner.flags &= ~FL_ONGROUND;
242 self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
246 end = self.origin - dir*50;
247 dist = vlen(end - myorg);
249 spd = dist * (pullspeed / 200);
254 self.realowner.velocity = dir*spd;
255 self.realowner.movetype = MOVETYPE_FLY;
257 self.realowner.flags &= ~FL_ONGROUND;
261 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
262 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
264 if(myorg != self.hook_start)
267 self.hook_start = myorg;
269 if(org != self.hook_end)
276 void GrapplingHookTouch (void)
280 GrapplingHook_Stop();
283 if(other.movetype != MOVETYPE_NONE)
285 SetMovetypeFollow(self, other);
286 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
289 //self.realowner.disableclientprediction = true;
292 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
297 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
298 return; // g_balance_projectiledamage says to halt
300 self.health = self.health - damage;
302 if (self.health <= 0)
304 if(attacker != self.realowner)
306 self.realowner.pusher = attacker;
307 self.realowner.pushltime = time + autocvar_g_maxpushtime;
308 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
310 RemoveGrapplingHook(self.realowner);
314 void FireGrapplingHook (void)
321 if(forbidWeaponUse()) return;
322 if(self.vehicle) return;
324 makevectors(self.v_angle);
326 s = self.cvar_cl_gunalign;
327 if(s != 1 && s != 2 && s != 4)
328 s = 3; // default value
330 vs = hook_shotorigin[s];
332 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
333 sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
334 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
336 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
339 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
341 missile = WarpZone_RefSys_SpawnSameRefSys(self);
342 missile.owner = missile.realowner = self;
344 missile.reset = GrapplingHookReset;
345 missile.classname = "grapplinghook";
346 missile.flags = FL_PROJECTILE;
348 missile.movetype = MOVETYPE_FLY;
349 PROJECTILE_MAKETRIGGER(missile);
351 //setmodel (missile, "models/hook.md3"); // precision set below
352 setsize (missile, '-3 -3 -3', '3 3 3');
353 setorigin (missile, org);
355 missile.state = 0; // not latched onto anything
357 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
359 missile.angles = vectoangles (missile.velocity);
360 //missile.glow_color = 250; // 244, 250
361 //missile.glow_size = 120;
362 missile.touch = GrapplingHookTouch;
363 missile.think = GrapplingHookThink;
364 missile.nextthink = time;
366 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
368 missile.health = autocvar_g_balance_grapplehook_health;//120
369 missile.event_damage = GrapplingHook_Damage;
370 missile.takedamage = DAMAGE_AIM;
371 missile.damageforcescale = 0;
372 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
374 missile.hook_start = missile.hook_end = missile.origin;
376 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
379 // void GrapplingHookFrame()
381 // // this function has been modified for Xonotic
382 // - if (self.BUTTON_HOOK && g_grappling_hook)
384 // - if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
385 // - if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
386 // - FireGrapplingHook();
391 // RemoveGrapplingHook(self);
393 // - self.button6_pressed_before = self.BUTTON_HOOK;
395 // // if I have no hook or it's not pulling yet, make sure I'm not flying!
396 // if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
398 void GrapplingHookFrame()
400 if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
402 // offhand hook controls
405 if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
407 self.hook_state |= HOOK_FIRING;
408 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
413 self.hook_state |= HOOK_REMOVING;
414 self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
417 self.hook_state &= ~HOOK_RELEASING;
418 if(self.BUTTON_CROUCH)
420 self.hook_state &= ~HOOK_PULLING;
421 //self.hook_state |= HOOK_RELEASING;
425 self.hook_state |= HOOK_PULLING;
426 //self.hook_state &= ~HOOK_RELEASING;
429 else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
431 if(self.BUTTON_HOOK && !self.hook_switchweapon)
432 W_SwitchWeapon(WEP_HOOK);
434 self.hook_switchweapon = self.BUTTON_HOOK;
436 if(!g_grappling_hook && self.weapon != WEP_HOOK)
438 self.hook_state &= ~HOOK_FIRING;
439 self.hook_state |= HOOK_REMOVING;
442 if (self.hook_state & HOOK_FIRING)
445 RemoveGrapplingHook(self);
447 self.hook_state &= ~HOOK_FIRING;
448 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
450 else if(self.hook_state & HOOK_REMOVING)
453 RemoveGrapplingHook(self);
454 self.hook_state &= ~HOOK_REMOVING;
458 // if I have no hook or it's not pulling yet, make sure I'm not flying!
459 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
461 self.movetype = MOVETYPE_WALK;
463 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
469 else if(self.hookimpulse == GRAPHOOK_RELEASE)
471 // remove hook, reset movement type
472 RemoveGrapplingHook(self);
476 /*else // make sure the player's movetype is correct
478 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
479 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
481 self.movetype = MOVETYPE_WALK;
484 // note: The hook entity does the actual pulling
487 void GrappleHookInit()
491 hook_shotorigin[0] = '8 8 -12';
492 hook_shotorigin[1] = '8 8 -12';
493 hook_shotorigin[2] = '8 8 -12';
494 hook_shotorigin[3] = '8 8 -12';
498 WEP_ACTION(WEP_HOOK, WR_INIT);
499 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
500 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
501 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
502 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
506 void SetGrappleHookBindings()
508 // this function has been modified for Xonotic
509 // don't remove these lines! old server or demos coud overwrite the new aliases
510 stuffcmd(self, "alias +hook +button6\n");
511 stuffcmd(self, "alias -hook -button6\n");