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(entity this)
85 if(this.realowner.hook == this)
86 RemoveGrapplingHook(this.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 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
111 if(sound_allowed(MSG_BROADCAST, self.realowner))
113 WriteByte(MSG_ENTITY, sf);
116 WriteByte(MSG_ENTITY, etof(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 = W_GetGunAlignment(self.realowner);
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 && STAT(FROZEN, self.aiment)) || !frozen_pulling)
245 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
246 UNSET_ONGROUND(self.aiment);
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 UNSET_ONGROUND(pull_entity);
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 && !STAT(FROZEN, self.aiment))
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 UNSET_ONGROUND(self.realowner);
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 ()
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 this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
325 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
326 return; // g_balance_projectiledamage says to halt
328 this.health = this.health - damage;
330 if (this.health <= 0)
332 if(attacker != this.realowner)
334 this.realowner.pusher = attacker;
335 this.realowner.pushltime = time + autocvar_g_maxpushtime;
336 this.realowner.istypefrag = this.realowner.BUTTON_CHAT;
338 RemoveGrapplingHook(this.realowner);
342 void FireGrapplingHook ()
348 if(forbidWeaponUse(self)) return;
349 if(self.vehicle) return;
351 makevectors(self.v_angle);
353 int s = W_GetGunAlignment(self);
354 vs = hook_shotorigin[s];
356 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
357 sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
358 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
360 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
363 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
365 missile = WarpZone_RefSys_SpawnSameRefSys(self);
366 missile.owner = missile.realowner = self;
368 missile.reset = GrapplingHookReset;
369 missile.classname = "grapplinghook";
370 missile.flags = FL_PROJECTILE;
372 missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
373 PROJECTILE_MAKETRIGGER(missile);
375 //setmodel (missile, MDL_HOOK); // precision set below
376 setsize (missile, '-3 -3 -3', '3 3 3');
377 setorigin (missile, org);
379 missile.state = 0; // not latched onto anything
381 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
383 missile.angles = vectoangles (missile.velocity);
384 //missile.glow_color = 250; // 244, 250
385 //missile.glow_size = 120;
386 missile.touch = GrapplingHookTouch;
387 missile.think = GrapplingHookThink;
388 missile.nextthink = time;
390 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
392 missile.health = autocvar_g_balance_grapplehook_health;//120
393 missile.event_damage = GrapplingHook_Damage;
394 missile.takedamage = DAMAGE_AIM;
395 missile.damageforcescale = 0;
396 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
398 missile.hook_start = missile.hook_end = missile.origin;
400 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
403 void GrappleHookInit()
407 hook_shotorigin[0] = '8 8 -12';
408 hook_shotorigin[1] = '8 8 -12';
409 hook_shotorigin[2] = '8 8 -12';
410 hook_shotorigin[3] = '8 8 -12';
416 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
417 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
418 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
419 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);