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"
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/net_linked.qh>
17 #include <common/weapons/_all.qh>
18 #include "../lib/warpzone/common.qh"
19 #include "../lib/warpzone/server.qh"
23 /*============================================
25 Wazat's Xonotic Grappling Hook
27 Contact: Wazat1@gmail.com
30 Installation instructions:
31 --------------------------
33 1. Place hook.c in your gamec source directory with the other source files.
35 2. Add this line to the bottom of progs.src:
39 3. Open defs.h and add these lines to the very bottom:
41 // Wazat's grappling hook
43 void GrapplingHookFrame();
44 void RemoveGrapplingHook(entity pl);
45 void SetGrappleHookBindings();
47 const float GRAPHOOK_FIRE = 20;
48 const float GRAPHOOK_RELEASE = 21;
49 // (note: you can change the hook impulse #'s to whatever you please)
51 4. Open client.c and add this to the top of PutClientInServer():
53 RemoveGrapplingHook(this); // Wazat's Grappling Hook
55 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
57 // Wazat's grappling hook
58 SetGrappleHookBindings();
60 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
64 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
67 And you should be done!
70 ============================================*/
74 void RemoveGrapplingHook(entity pl)
80 if(pl.move_movetype == MOVETYPE_FLY)
81 set_movetype(pl, MOVETYPE_WALK);
83 //pl.disableclientprediction = false;
86 void GrapplingHookReset(entity this)
88 if(this.realowner.hook == this)
89 RemoveGrapplingHook(this.owner);
94 void GrapplingHookThink(entity this);
95 void GrapplingHook_Stop(entity this)
97 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
98 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
101 setthink(this, GrapplingHookThink);
102 this.nextthink = time;
103 settouch(this, func_null);
104 this.velocity = '0 0 0';
105 set_movetype(this, MOVETYPE_NONE);
106 this.hook_length = -1;
109 .vector hook_start, hook_end;
110 bool GrapplingHookSend(entity this, entity to, int sf)
112 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
114 if(sound_allowed(MSG_BROADCAST, this.realowner))
116 WriteByte(MSG_ENTITY, sf);
119 WriteByte(MSG_ENTITY, etof(this.realowner));
123 WriteCoord(MSG_ENTITY, this.hook_start.x);
124 WriteCoord(MSG_ENTITY, this.hook_start.y);
125 WriteCoord(MSG_ENTITY, this.hook_start.z);
129 WriteCoord(MSG_ENTITY, this.hook_end.x);
130 WriteCoord(MSG_ENTITY, this.hook_end.y);
131 WriteCoord(MSG_ENTITY, this.hook_end.z);
136 int autocvar_g_grappling_hook_tarzan;
138 void GrapplingHookThink(entity this)
140 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
141 vector dir, org, end, v0, dv, v, myorg, vs;
142 if(this.realowner.hook != this) // how did that happen?
144 error("Owner lost the hook!\n");
147 if(LostMovetypeFollow(this) || gameover || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
149 RemoveGrapplingHook(this.realowner);
153 WarpZone_RefSys_AddIncrementally(this, this.aiment);
155 this.nextthink = time;
157 int s = W_GetGunAlignment(this.realowner);
158 vs = hook_shotorigin[s];
160 makevectors(this.realowner.v_angle);
161 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
162 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
164 if(this.hook_length < 0)
165 this.hook_length = vlen(myorg - this.origin);
167 int tarzan = autocvar_g_grappling_hook_tarzan;
168 entity pull_entity = this.realowner;
169 float velocity_multiplier = 1;
170 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
171 tarzan = M_ARGV(1, int);
172 pull_entity = M_ARGV(2, entity);
173 velocity_multiplier = M_ARGV(3, float);
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 = this.origin - myorg;
202 dir = normalize(dir);
206 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
208 // first pull the rope...
209 if(this.realowner.hook_state & HOOK_PULLING)
211 newlength = this.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 this.hook_length = newlength;
224 if(pull_entity.move_movetype == MOVETYPE_FLY)
225 set_movetype(pull_entity, MOVETYPE_WALK);
227 if(this.realowner.hook_state & HOOK_RELEASING)
230 this.hook_length = newlength;
234 // then pull the player
235 spd = bound(0, (dist - this.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(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
244 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
246 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
248 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
249 UNSET_ONGROUND(this.aiment);
250 if(this.aiment.flags & FL_PROJECTILE)
251 UpdateCSQCProjectile(this.aiment);
253 if(this.aiment.classname == "nade")
254 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
255 aim_ent.pusher = this.realowner;
256 aim_ent.pushltime = time + autocvar_g_maxpushtime;
257 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
261 UNSET_ONGROUND(pull_entity);
264 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
265 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
267 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
269 RemoveGrapplingHook(this.realowner);
275 end = this.origin - dir*50;
276 dist = vlen(end - myorg);
278 spd = dist * (pullspeed / 200);
283 this.realowner.velocity = dir*spd;
284 set_movetype(this.realowner, MOVETYPE_FLY);
286 UNSET_ONGROUND(this.realowner);
290 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
291 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
293 if(myorg != this.hook_start)
296 this.hook_start = myorg;
298 if(org != this.hook_end)
305 void GrapplingHookTouch(entity this, entity toucher)
307 if(toucher.move_movetype == MOVETYPE_FOLLOW)
309 PROJECTILE_TOUCH(this, toucher);
311 GrapplingHook_Stop(this);
314 if(toucher.move_movetype != MOVETYPE_NONE)
316 SetMovetypeFollow(this, toucher);
317 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
320 //this.realowner.disableclientprediction = true;
323 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
328 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
329 return; // g_balance_projectiledamage says to halt
331 this.health = this.health - damage;
333 if (this.health <= 0)
335 if(attacker != this.realowner)
337 this.realowner.pusher = attacker;
338 this.realowner.pushltime = time + autocvar_g_maxpushtime;
339 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
341 RemoveGrapplingHook(this.realowner);
345 void FireGrapplingHook(entity actor)
351 if(forbidWeaponUse(actor)) return;
352 if(actor.vehicle) return;
354 makevectors(actor.v_angle);
356 int s = W_GetGunAlignment(actor);
357 vs = hook_shotorigin[s];
359 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
360 sound (actor, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
361 org = actor.origin + actor.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
363 tracebox(actor.origin + actor.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, actor);
366 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
368 missile = WarpZone_RefSys_SpawnSameRefSys(actor);
369 missile.owner = missile.realowner = actor;
370 actor.hook = missile;
371 missile.reset = GrapplingHookReset;
372 missile.classname = "grapplinghook";
373 missile.flags = FL_PROJECTILE;
374 IL_PUSH(g_projectiles, missile);
375 IL_PUSH(g_bot_dodge, missile);
377 set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
378 PROJECTILE_MAKETRIGGER(missile);
380 //setmodel (missile, MDL_HOOK); // precision set below
381 setsize (missile, '-3 -3 -3', '3 3 3');
382 setorigin(missile, org);
384 missile.state = 0; // not latched onto anything
386 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
388 missile.angles = vectoangles (missile.velocity);
389 //missile.glow_color = 250; // 244, 250
390 //missile.glow_size = 120;
391 settouch(missile, GrapplingHookTouch);
392 setthink(missile, GrapplingHookThink);
393 missile.nextthink = time;
395 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
397 missile.health = autocvar_g_balance_grapplehook_health;//120
398 missile.event_damage = GrapplingHook_Damage;
399 missile.takedamage = DAMAGE_AIM;
400 missile.damageforcescale = 0;
401 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
402 if(missile.damagedbycontents)
403 IL_PUSH(g_damagedbycontents, missile);
405 missile.hook_start = missile.hook_end = missile.origin;
407 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
410 void GrappleHookInit()
414 hook_shotorigin[0] = '8 8 -12';
415 hook_shotorigin[1] = '8 8 -12';
416 hook_shotorigin[2] = '8 8 -12';
417 hook_shotorigin[3] = '8 8 -12';
423 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
424 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
425 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
426 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);