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/state.qh"
12 #include "../common/physics/player.qh"
13 #include "../common/vehicles/all.qh"
14 #include "../common/constants.qh"
15 #include "../common/util.qh"
16 #include "../common/weapons/all.qh"
17 #include "../lib/warpzone/common.qh"
18 #include "../lib/warpzone/server.qh"
22 /*============================================
24 Wazat's Xonotic Grappling Hook
26 Contact: Wazat1@gmail.com
29 Installation instructions:
30 --------------------------
32 1. Place hook.c in your gamec source directory with the other source files.
34 2. Add this line to the bottom of progs.src:
38 3. Open defs.h and add these lines to the very bottom:
40 // Wazat's grappling hook
42 void GrapplingHookFrame();
43 void RemoveGrapplingHook(entity pl);
44 void SetGrappleHookBindings();
46 const float GRAPHOOK_FIRE = 20;
47 const float GRAPHOOK_RELEASE = 21;
48 // (note: you can change the hook impulse #'s to whatever you please)
50 4. Open client.c and add this to the top of PutClientInServer():
52 RemoveGrapplingHook(this); // Wazat's Grappling Hook
54 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
56 // Wazat's grappling hook
57 SetGrappleHookBindings();
59 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
63 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
66 And you should be done!
69 ============================================*/
73 void RemoveGrapplingHook(entity pl)
79 if(pl.move_movetype == MOVETYPE_FLY)
80 set_movetype(pl, MOVETYPE_WALK);
82 //pl.disableclientprediction = false;
85 void GrapplingHookReset(entity this)
87 if(this.realowner.hook == this)
88 RemoveGrapplingHook(this.owner);
93 void GrapplingHookThink(entity this);
94 void GrapplingHook_Stop(entity this)
96 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
97 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
100 setthink(this, GrapplingHookThink);
101 this.nextthink = time;
102 settouch(this, func_null);
103 this.velocity = '0 0 0';
104 set_movetype(this, MOVETYPE_NONE);
105 this.hook_length = -1;
108 .vector hook_start, hook_end;
109 bool GrapplingHookSend(entity this, entity to, int sf)
111 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
113 if(sound_allowed(MSG_BROADCAST, this.realowner))
115 WriteByte(MSG_ENTITY, sf);
118 WriteByte(MSG_ENTITY, etof(this.realowner));
122 WriteCoord(MSG_ENTITY, this.hook_start.x);
123 WriteCoord(MSG_ENTITY, this.hook_start.y);
124 WriteCoord(MSG_ENTITY, this.hook_start.z);
128 WriteCoord(MSG_ENTITY, this.hook_end.x);
129 WriteCoord(MSG_ENTITY, this.hook_end.y);
130 WriteCoord(MSG_ENTITY, this.hook_end.z);
135 int autocvar_g_grappling_hook_tarzan;
137 void GrapplingHookThink(entity this)
139 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
140 vector dir, org, end, v0, dv, v, myorg, vs;
141 if(this.realowner.hook != this) // how did that happen?
143 error("Owner lost the hook!\n");
146 if(LostMovetypeFollow(this) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
148 RemoveGrapplingHook(this.realowner);
152 WarpZone_RefSys_AddIncrementally(this, this.aiment);
154 this.nextthink = time;
156 int s = W_GetGunAlignment(this.realowner);
157 vs = hook_shotorigin[s];
159 makevectors(this.realowner.v_angle);
160 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
161 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
163 if(this.hook_length < 0)
164 this.hook_length = vlen(myorg - this.origin);
166 int tarzan = autocvar_g_grappling_hook_tarzan;
167 entity pull_entity = this.realowner;
168 float velocity_multiplier = 1;
169 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
170 tarzan = M_ARGV(1, int);
171 pull_entity = M_ARGV(2, entity);
172 velocity_multiplier = M_ARGV(3, float);
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 = this.origin - myorg;
201 dir = normalize(dir);
205 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
207 // first pull the rope...
208 if(this.realowner.hook_state & HOOK_PULLING)
210 newlength = this.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 this.hook_length = newlength;
223 if(pull_entity.move_movetype == MOVETYPE_FLY)
224 set_movetype(pull_entity, MOVETYPE_WALK);
226 if(this.realowner.hook_state & HOOK_RELEASING)
229 this.hook_length = newlength;
233 // then pull the player
234 spd = bound(0, (dist - this.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(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
243 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
245 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
247 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
248 UNSET_ONGROUND(this.aiment);
249 if(this.aiment.flags & FL_PROJECTILE)
250 UpdateCSQCProjectile(this.aiment);
252 if(this.aiment.classname == "nade")
253 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
254 aim_ent.pusher = this.realowner;
255 aim_ent.pushltime = time + autocvar_g_maxpushtime;
256 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
260 UNSET_ONGROUND(pull_entity);
263 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
264 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
266 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
268 RemoveGrapplingHook(this.realowner);
274 end = this.origin - dir*50;
275 dist = vlen(end - myorg);
277 spd = dist * (pullspeed / 200);
282 this.realowner.velocity = dir*spd;
283 set_movetype(this.realowner, MOVETYPE_FLY);
285 UNSET_ONGROUND(this.realowner);
289 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
290 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
292 if(myorg != this.hook_start)
295 this.hook_start = myorg;
297 if(org != this.hook_end)
304 void GrapplingHookTouch(entity this, entity toucher)
306 if(toucher.move_movetype == MOVETYPE_FOLLOW)
308 PROJECTILE_TOUCH(this, toucher);
310 GrapplingHook_Stop(this);
313 if(toucher.move_movetype != MOVETYPE_NONE)
315 SetMovetypeFollow(this, toucher);
316 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
319 //this.realowner.disableclientprediction = true;
322 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
327 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
328 return; // g_balance_projectiledamage says to halt
330 this.health = this.health - damage;
332 if (this.health <= 0)
334 if(attacker != this.realowner)
336 this.realowner.pusher = attacker;
337 this.realowner.pushltime = time + autocvar_g_maxpushtime;
338 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
340 RemoveGrapplingHook(this.realowner);
344 void FireGrapplingHook(entity actor)
350 if(forbidWeaponUse(actor)) return;
351 if(actor.vehicle) return;
353 makevectors(actor.v_angle);
355 int s = W_GetGunAlignment(actor);
356 vs = hook_shotorigin[s];
358 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
359 sound (actor, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
360 org = actor.origin + actor.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
362 tracebox(actor.origin + actor.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, actor);
365 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
367 missile = WarpZone_RefSys_SpawnSameRefSys(actor);
368 missile.owner = missile.realowner = actor;
369 actor.hook = missile;
370 missile.reset = GrapplingHookReset;
371 missile.classname = "grapplinghook";
372 missile.flags = FL_PROJECTILE;
373 IL_PUSH(g_projectiles, missile);
375 set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
376 PROJECTILE_MAKETRIGGER(missile);
378 //setmodel (missile, MDL_HOOK); // precision set below
379 setsize (missile, '-3 -3 -3', '3 3 3');
380 setorigin(missile, org);
382 missile.state = 0; // not latched onto anything
384 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
386 missile.angles = vectoangles (missile.velocity);
387 //missile.glow_color = 250; // 244, 250
388 //missile.glow_size = 120;
389 settouch(missile, GrapplingHookTouch);
390 setthink(missile, GrapplingHookThink);
391 missile.nextthink = time;
393 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
395 missile.health = autocvar_g_balance_grapplehook_health;//120
396 missile.event_damage = GrapplingHook_Damage;
397 missile.takedamage = DAMAGE_AIM;
398 missile.damageforcescale = 0;
399 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
401 missile.hook_start = missile.hook_end = missile.origin;
403 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
406 void GrappleHookInit()
410 hook_shotorigin[0] = '8 8 -12';
411 hook_shotorigin[1] = '8 8 -12';
412 hook_shotorigin[2] = '8 8 -12';
413 hook_shotorigin[3] = '8 8 -12';
419 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
420 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
421 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
422 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);