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 ============================================*/
69 void RemoveGrapplingHook(entity pl)
75 if(pl.movetype == MOVETYPE_FLY)
76 pl.movetype = MOVETYPE_WALK;
78 //pl.disableclientprediction = false;
81 void GrapplingHookReset(void)
83 if(self.realowner.hook == self)
84 RemoveGrapplingHook(self.owner);
89 void GrapplingHookThink();
90 void GrapplingHook_Stop()
92 Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1);
93 sound (self, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
96 self.think = GrapplingHookThink;
97 self.nextthink = time;
98 self.touch = func_null;
99 self.velocity = '0 0 0';
100 self.movetype = MOVETYPE_NONE;
101 self.hook_length = -1;
104 .vector hook_start, hook_end;
105 float GrapplingHookSend(entity to, int sf)
107 WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
109 if(sound_allowed(MSG_BROADCAST, self.realowner))
111 WriteByte(MSG_ENTITY, sf);
114 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
118 WriteCoord(MSG_ENTITY, self.hook_start.x);
119 WriteCoord(MSG_ENTITY, self.hook_start.y);
120 WriteCoord(MSG_ENTITY, self.hook_start.z);
124 WriteCoord(MSG_ENTITY, self.hook_end.x);
125 WriteCoord(MSG_ENTITY, self.hook_end.y);
126 WriteCoord(MSG_ENTITY, self.hook_end.z);
131 int autocvar_g_grappling_hook_tarzan;
133 void GrapplingHookThink()
135 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
136 vector dir, org, end, v0, dv, v, myorg, vs;
137 if(self.realowner.hook != self) // how did that happen?
139 error("Owner lost the hook!\n");
142 if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
144 RemoveGrapplingHook(self.realowner);
148 WarpZone_RefSys_AddIncrementally(self, self.aiment);
150 self.nextthink = time;
152 int s = self.realowner.cvar_cl_gunalign;
153 if(s != 1 && s != 2 && s != 4)
154 s = 3; // default value
156 vs = hook_shotorigin[s];
158 makevectors(self.realowner.v_angle);
159 org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
160 myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
162 if(self.hook_length < 0)
163 self.hook_length = vlen(myorg - self.origin);
165 int tarzan = autocvar_g_grappling_hook_tarzan;
166 entity pull_entity = self.realowner;
167 float velocity_multiplier = 1;
168 MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
169 tarzan = hook_tarzan;
170 pull_entity = hook_pullentity;
171 velocity_multiplier = hook_velmultiplier;
175 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
176 // speed the rope is pulled with
178 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
179 // force the rope will use if it is stretched
181 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
182 // force the rope will use if it is stretched
184 minlength = autocvar_g_balance_grapplehook_length_min;//100;
185 // minimal rope length
186 // if the rope goes below this length, it isn't pulled any more
188 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
189 // if the rope is stretched by more than this amount, more rope is
190 // given to you again
192 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
193 // while hanging on the rope, this friction component will help you a
194 // bit to control the rope
196 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
198 dir = self.origin - myorg;
200 dir = normalize(dir);
204 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
206 // first pull the rope...
207 if(self.realowner.hook_state & HOOK_PULLING)
209 newlength = self.hook_length;
210 newlength = max(newlength - pullspeed * frametime, minlength);
212 if(newlength < dist - ropestretch) // overstretched?
214 newlength = dist - ropestretch;
215 if(v * dir < 0) // only if not already moving in hook direction
216 v = v + frametime * dir * rubberforce_overstretch;
219 self.hook_length = newlength;
222 if(pull_entity.movetype == MOVETYPE_FLY)
223 pull_entity.movetype = MOVETYPE_WALK;
225 if(self.realowner.hook_state & HOOK_RELEASING)
228 self.hook_length = newlength;
232 // then pull the player
233 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
234 v = v * (1 - frametime * ropeairfriction);
235 v = v + frametime * dir * spd * rubberforce;
237 dv = ((v - v0) * dir) * dir;
240 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
242 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
244 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
246 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
247 self.aiment.flags &= ~FL_ONGROUND;
248 if(self.aiment.flags & FL_PROJECTILE)
249 UpdateCSQCProjectile(self.aiment);
251 if(self.aiment.classname == "nade")
252 self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
253 aim_ent.pusher = self.realowner;
254 aim_ent.pushltime = time + autocvar_g_maxpushtime;
255 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
259 pull_entity.flags &= ~FL_ONGROUND;
262 if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
263 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
265 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
267 RemoveGrapplingHook(self.realowner);
273 end = self.origin - dir*50;
274 dist = vlen(end - myorg);
276 spd = dist * (pullspeed / 200);
281 self.realowner.velocity = dir*spd;
282 self.realowner.movetype = MOVETYPE_FLY;
284 self.realowner.flags &= ~FL_ONGROUND;
288 makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
289 myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
291 if(myorg != self.hook_start)
294 self.hook_start = myorg;
296 if(org != self.hook_end)
303 void GrapplingHookTouch (void)
305 if(other.movetype == MOVETYPE_FOLLOW)
309 GrapplingHook_Stop();
312 if(other.movetype != MOVETYPE_NONE)
314 SetMovetypeFollow(self, other);
315 WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
318 //self.realowner.disableclientprediction = true;
321 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
326 if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
327 return; // g_balance_projectiledamage says to halt
329 self.health = self.health - damage;
331 if (self.health <= 0)
333 if(attacker != self.realowner)
335 self.realowner.pusher = attacker;
336 self.realowner.pushltime = time + autocvar_g_maxpushtime;
337 self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
339 RemoveGrapplingHook(self.realowner);
343 void FireGrapplingHook (void)
349 if(forbidWeaponUse(self)) return;
350 if(self.vehicle) return;
352 makevectors(self.v_angle);
354 int s = self.cvar_cl_gunalign;
355 if(s != 1 && s != 2 && s != 4)
356 s = 3; // default value
358 vs = hook_shotorigin[s];
360 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
361 sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
362 org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
364 tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
367 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
369 missile = WarpZone_RefSys_SpawnSameRefSys(self);
370 missile.owner = missile.realowner = self;
372 missile.reset = GrapplingHookReset;
373 missile.classname = "grapplinghook";
374 missile.flags = FL_PROJECTILE;
376 missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
377 PROJECTILE_MAKETRIGGER(missile);
379 //setmodel (missile, MDL_HOOK); // precision set below
380 setsize (missile, '-3 -3 -3', '3 3 3');
381 setorigin (missile, org);
383 missile.state = 0; // not latched onto anything
385 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
387 missile.angles = vectoangles (missile.velocity);
388 //missile.glow_color = 250; // 244, 250
389 //missile.glow_size = 120;
390 missile.touch = GrapplingHookTouch;
391 missile.think = GrapplingHookThink;
392 missile.nextthink = time;
394 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
396 missile.health = autocvar_g_balance_grapplehook_health;//120
397 missile.event_damage = GrapplingHook_Damage;
398 missile.takedamage = DAMAGE_AIM;
399 missile.damageforcescale = 0;
400 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
402 missile.hook_start = missile.hook_end = missile.origin;
404 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
407 void GrappleHookInit()
411 hook_shotorigin[0] = '8 8 -12';
412 hook_shotorigin[1] = '8 8 -12';
413 hook_shotorigin[2] = '8 8 -12';
414 hook_shotorigin[3] = '8 8 -12';
420 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
421 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
422 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
423 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
427 void SetGrappleHookBindings()
429 // this function has been modified for Xonotic
430 // don't remove these lines! old server or demos coud overwrite the new aliases
431 stuffcmd(self, "alias +hook +button6\n");
432 stuffcmd(self, "alias -hook -button6\n");