3 #include "weapons/common.qh"
4 #include "weapons/csqcprojectile.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 "../lib/warpzone/common.qh"
16 #include "../lib/warpzone/server.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 ============================================*/
71 void RemoveGrapplingHook(entity pl)
77 if(pl.movetype == MOVETYPE_FLY)
78 pl.movetype = MOVETYPE_WALK;
80 //pl.disableclientprediction = false;
83 void GrapplingHookReset(void)
85 if(self.realowner.hook == self)
86 RemoveGrapplingHook(self.owner);
91 void GrapplingHookThink();
92 void GrapplingHook_Stop()
94 Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1);
95 sound (self, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
98 self.think = GrapplingHookThink;
99 self.nextthink = time;
100 self.touch = func_null;
101 self.velocity = '0 0 0';
102 self.movetype = MOVETYPE_NONE;
103 self.hook_length = -1;
106 .vector hook_start, hook_end;
107 bool GrapplingHookSend(entity this, entity to, int sf)
109 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
111 if(sound_allowed(MSG_BROADCAST, self.realowner))
113 WriteByte(MSG_ENTITY, sf);
116 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
120 WriteCoord(MSG_ENTITY, self.hook_start.x);
121 WriteCoord(MSG_ENTITY, self.hook_start.y);
122 WriteCoord(MSG_ENTITY, self.hook_start.z);
126 WriteCoord(MSG_ENTITY, self.hook_end.x);
127 WriteCoord(MSG_ENTITY, self.hook_end.y);
128 WriteCoord(MSG_ENTITY, self.hook_end.z);
133 int autocvar_g_grappling_hook_tarzan;
135 void GrapplingHookThink()
137 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
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()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
146 RemoveGrapplingHook(self.realowner);
150 WarpZone_RefSys_AddIncrementally(self, self.aiment);
152 self.nextthink = time;
154 int 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);
167 int tarzan = autocvar_g_grappling_hook_tarzan;
168 entity pull_entity = self.realowner;
169 float velocity_multiplier = 1;
170 MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
171 tarzan = hook_tarzan;
172 pull_entity = hook_pullentity;
173 velocity_multiplier = hook_velmultiplier;
177 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
178 // speed the rope is pulled with
180 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
181 // force the rope will use if it is stretched
183 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
184 // force the rope will use if it is stretched
186 minlength = autocvar_g_balance_grapplehook_length_min;//100;
187 // minimal rope length
188 // if the rope goes below this length, it isn't pulled any more
190 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
191 // if the rope is stretched by more than this amount, more rope is
192 // given to you again
194 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
195 // while hanging on the rope, this friction component will help you a
196 // bit to control the rope
198 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
200 dir = self.origin - myorg;
202 dir = normalize(dir);
206 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
208 // first pull the rope...
209 if(self.realowner.hook_state & HOOK_PULLING)
211 newlength = self.hook_length;
212 newlength = max(newlength - pullspeed * frametime, minlength);
214 if(newlength < dist - ropestretch) // overstretched?
216 newlength = dist - ropestretch;
217 if(v * dir < 0) // only if not already moving in hook direction
218 v = v + frametime * dir * rubberforce_overstretch;
221 self.hook_length = newlength;
224 if(pull_entity.movetype == MOVETYPE_FLY)
225 pull_entity.movetype = MOVETYPE_WALK;
227 if(self.realowner.hook_state & HOOK_RELEASING)
230 self.hook_length = newlength;
234 // then pull the player
235 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
236 v = v * (1 - frametime * ropeairfriction);
237 v = v + frametime * dir * spd * rubberforce;
239 dv = ((v - v0) * dir) * dir;
242 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
244 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
246 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
248 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
249 self.aiment.flags &= ~FL_ONGROUND;
250 if(self.aiment.flags & FL_PROJECTILE)
251 UpdateCSQCProjectile(self.aiment);
253 if(self.aiment.classname == "nade")
254 self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
255 aim_ent.pusher = self.realowner;
256 aim_ent.pushltime = time + autocvar_g_maxpushtime;
257 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
261 pull_entity.flags &= ~FL_ONGROUND;
264 if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
265 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
267 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
269 RemoveGrapplingHook(self.realowner);
275 end = self.origin - dir*50;
276 dist = vlen(end - myorg);
278 spd = dist * (pullspeed / 200);
283 self.realowner.velocity = dir*spd;
284 self.realowner.movetype = MOVETYPE_FLY;
286 self.realowner.flags &= ~FL_ONGROUND;
290 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
291 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
293 if(myorg != self.hook_start)
296 self.hook_start = myorg;
298 if(org != self.hook_end)
305 void GrapplingHookTouch (void)
307 if(other.movetype == MOVETYPE_FOLLOW)
311 GrapplingHook_Stop();
314 if(other.movetype != MOVETYPE_NONE)
316 SetMovetypeFollow(self, other);
317 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
320 //self.realowner.disableclientprediction = true;
323 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
328 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
329 return; // g_balance_projectiledamage says to halt
331 self.health = self.health - damage;
333 if (self.health <= 0)
335 if(attacker != self.realowner)
337 self.realowner.pusher = attacker;
338 self.realowner.pushltime = time + autocvar_g_maxpushtime;
339 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
341 RemoveGrapplingHook(self.realowner);
345 void FireGrapplingHook (void)
351 if(forbidWeaponUse(self)) return;
352 if(self.vehicle) return;
354 makevectors(self.v_angle);
356 int s = self.cvar_cl_gunalign;
357 if(s != 1 && s != 2 && s != 4)
358 s = 3; // default value
360 vs = hook_shotorigin[s];
362 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
363 sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
364 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
366 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
369 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
371 missile = WarpZone_RefSys_SpawnSameRefSys(self);
372 missile.owner = missile.realowner = self;
374 missile.reset = GrapplingHookReset;
375 missile.classname = "grapplinghook";
376 missile.flags = FL_PROJECTILE;
378 missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
379 PROJECTILE_MAKETRIGGER(missile);
381 //setmodel (missile, MDL_HOOK); // precision set below
382 setsize (missile, '-3 -3 -3', '3 3 3');
383 setorigin (missile, org);
385 missile.state = 0; // not latched onto anything
387 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
389 missile.angles = vectoangles (missile.velocity);
390 //missile.glow_color = 250; // 244, 250
391 //missile.glow_size = 120;
392 missile.touch = GrapplingHookTouch;
393 missile.think = GrapplingHookThink;
394 missile.nextthink = time;
396 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
398 missile.health = autocvar_g_balance_grapplehook_health;//120
399 missile.event_damage = GrapplingHook_Damage;
400 missile.takedamage = DAMAGE_AIM;
401 missile.damageforcescale = 0;
402 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
404 missile.hook_start = missile.hook_end = missile.origin;
406 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
409 void GrappleHookInit()
413 hook_shotorigin[0] = '8 8 -12';
414 hook_shotorigin[1] = '8 8 -12';
415 hook_shotorigin[2] = '8 8 -12';
416 hook_shotorigin[3] = '8 8 -12';
422 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
423 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
424 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
425 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
429 void SetGrappleHookBindings()
431 // this function has been modified for Xonotic
432 // don't remove these lines! old server or demos coud overwrite the new aliases
433 stuffcmd(self, "alias +hook +button6\n");
434 stuffcmd(self, "alias -hook -button6\n");