3 #include <server/defs.qh>
4 #include <server/g_damage.qh>
5 #include <server/miscfunctions.qh>
6 #include <common/effects/all.qh>
7 #include "weapons/common.qh"
8 #include "weapons/csqcprojectile.qh"
9 #include "weapons/weaponsystem.qh"
10 #include "weapons/selection.qh"
11 #include "weapons/tracing.qh"
13 #include "command/common.qh"
14 #include "command/vote.qh"
15 #include "round_handler.qh"
16 #include "../common/state.qh"
17 #include "../common/physics/player.qh"
18 #include "../common/vehicles/all.qh"
19 #include "../common/constants.qh"
20 #include "../common/util.qh"
21 #include <common/net_linked.qh>
22 #include <common/weapons/_all.qh>
23 #include "../lib/warpzone/common.qh"
24 #include "../lib/warpzone/server.qh"
26 /*============================================
28 Wazat's Xonotic Grappling Hook
30 Contact: Wazat1@gmail.com
33 Installation instructions:
34 --------------------------
36 1. Place hook.c in your gamec source directory with the other source files.
38 2. Add this line to the bottom of progs.src:
42 3. Open defs.h and add these lines to the very bottom:
44 // Wazat's grappling hook
46 void GrapplingHookFrame();
47 void RemoveGrapplingHook(entity pl);
48 void SetGrappleHookBindings();
50 const float GRAPHOOK_FIRE = 20;
51 const float GRAPHOOK_RELEASE = 21;
52 // (note: you can change the hook impulse #'s to whatever you please)
54 4. Open client.c and add this to the top of PutClientInServer():
56 RemoveGrapplingHook(this); // Wazat's Grappling Hook
58 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
60 // Wazat's grappling hook
61 SetGrappleHookBindings();
63 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
67 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
70 And you should be done!
73 ============================================*/
75 void RemoveGrapplingHooks(entity pl)
77 if(pl.move_movetype == MOVETYPE_FLY)
78 set_movetype(pl, MOVETYPE_WALK);
80 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
82 .entity weaponentity = weaponentities[slot];
83 if(!pl.(weaponentity))
84 continue; // continue incase other slots exist?
85 if(pl.(weaponentity).hook)
86 delete(pl.(weaponentity).hook);
87 pl.(weaponentity).hook = NULL;
90 //pl.disableclientprediction = false;
93 void RemoveHook(entity this)
95 entity player = this.realowner;
96 .entity weaponentity = this.weaponentity_fld;
98 if(player.(weaponentity).hook == this)
99 player.(weaponentity).hook = NULL;
101 if(player.move_movetype == MOVETYPE_FLY)
102 set_movetype(player, MOVETYPE_WALK);
106 void GrapplingHookReset(entity this)
111 void GrapplingHook_Stop(entity this)
113 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
114 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
117 setthink(this, GrapplingHookThink);
118 this.nextthink = time;
119 settouch(this, func_null);
120 this.velocity = '0 0 0';
121 set_movetype(this, MOVETYPE_NONE);
122 this.hook_length = -1;
125 .vector hook_start, hook_end;
126 bool GrapplingHookSend(entity this, entity to, int sf)
128 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
130 if(sound_allowed(MSG_BROADCAST, this.realowner))
132 WriteByte(MSG_ENTITY, sf);
135 WriteByte(MSG_ENTITY, etof(this.realowner));
136 WriteByte(MSG_ENTITY, weaponslot(this.weaponentity_fld));
140 WriteVector(MSG_ENTITY, this.hook_start);
144 WriteVector(MSG_ENTITY, this.hook_end);
149 int autocvar_g_grappling_hook_tarzan;
151 void GrapplingHookThink(entity this)
153 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
154 vector dir, org, end, v0, dv, v, myorg, vs;
155 .entity weaponentity = this.weaponentity_fld;
156 if(this.realowner.(weaponentity).hook != this) // how did that happen?
158 error("Owner lost the hook!\n");
161 if(LostMovetypeFollow(this) || game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
167 WarpZone_RefSys_AddIncrementally(this, this.aiment);
169 this.nextthink = time;
171 int s = W_GunAlign(this.realowner.(weaponentity), STAT(GUNALIGN, this.realowner)) - 1;
172 vs = hook_shotorigin[s];
174 makevectors(this.realowner.v_angle);
175 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
176 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
178 if(this.hook_length < 0)
179 this.hook_length = vlen(myorg - this.origin);
181 int tarzan = autocvar_g_grappling_hook_tarzan;
182 entity pull_entity = this.realowner;
183 float velocity_multiplier = 1;
184 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
185 tarzan = M_ARGV(1, int);
186 pull_entity = M_ARGV(2, entity);
187 velocity_multiplier = M_ARGV(3, float);
191 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
192 // speed the rope is pulled with
194 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
195 // force the rope will use if it is stretched
197 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
198 // force the rope will use if it is stretched
200 minlength = autocvar_g_balance_grapplehook_length_min;//100;
201 // minimal rope length
202 // if the rope goes below this length, it isn't pulled any more
204 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
205 // if the rope is stretched by more than this amount, more rope is
206 // given to you again
208 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
209 // while hanging on the rope, this friction component will help you a
210 // bit to control the rope
212 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
214 dir = this.origin - myorg;
216 dir = normalize(dir);
220 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
222 // first pull the rope...
223 if(this.realowner.(weaponentity).hook_state & HOOK_PULLING)
225 newlength = this.hook_length;
226 newlength = max(newlength - pullspeed * frametime, minlength);
228 if(newlength < dist - ropestretch) // overstretched?
230 newlength = dist - ropestretch;
231 if(v * dir < 0) // only if not already moving in hook direction
232 v = v + frametime * dir * rubberforce_overstretch;
235 this.hook_length = newlength;
238 if(pull_entity.move_movetype == MOVETYPE_FLY)
239 set_movetype(pull_entity, MOVETYPE_WALK);
241 if(this.realowner.(weaponentity).hook_state & HOOK_RELEASING)
244 this.hook_length = newlength;
248 // then pull the player
249 spd = bound(0, (dist - this.hook_length) / ropestretch, 1);
250 v = v * (1 - frametime * ropeairfriction);
251 v = v + frametime * dir * spd * rubberforce;
253 dv = ((v - v0) * dir) * dir;
256 if(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
258 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
260 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
262 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
263 UNSET_ONGROUND(this.aiment);
264 if(this.aiment.flags & FL_PROJECTILE)
265 UpdateCSQCProjectile(this.aiment);
267 if(this.aiment.classname == "nade")
268 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
269 aim_ent.pusher = this.realowner;
270 aim_ent.pushltime = time + autocvar_g_maxpushtime;
271 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
275 UNSET_ONGROUND(pull_entity);
278 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
279 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
281 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
289 end = this.origin - dir*50;
290 dist = vlen(end - myorg);
292 spd = dist * (pullspeed / 200);
297 this.realowner.velocity = dir*spd;
298 set_movetype(this.realowner, MOVETYPE_FLY);
300 UNSET_ONGROUND(this.realowner);
304 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
305 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
307 if(myorg != this.hook_start)
310 this.hook_start = myorg;
312 if(org != this.hook_end)
319 void GrapplingHookTouch(entity this, entity toucher)
321 if(toucher.move_movetype == MOVETYPE_FOLLOW)
323 PROJECTILE_TOUCH(this, toucher);
325 GrapplingHook_Stop(this);
328 //if(toucher.move_movetype != MOVETYPE_NONE)
330 SetMovetypeFollow(this, toucher);
331 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
334 //this.realowner.disableclientprediction = true;
337 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
339 if(GetResource(this, RES_HEALTH) <= 0)
342 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
343 return; // g_balance_projectiledamage says to halt
345 TakeResource(this, RES_HEALTH, damage);
347 if (GetResource(this, RES_HEALTH) <= 0)
349 if(attacker != this.realowner)
351 this.realowner.pusher = attacker;
352 this.realowner.pushltime = time + autocvar_g_maxpushtime;
353 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
359 void FireGrapplingHook(entity actor, .entity weaponentity)
361 if(weaponLocked(actor)) return;
362 if(actor.vehicle) return;
364 int s = W_GunAlign(actor.(weaponentity), STAT(GUNALIGN, actor)) - 1;
365 vector vs = hook_shotorigin[s];
366 vector oldmovedir = actor.(weaponentity).movedir;
367 actor.(weaponentity).movedir = vs;
368 W_SetupShot_ProjectileSize(actor, weaponentity, '-3 -3 -3', '3 3 3', true, 0, SND_HOOK_FIRE, CH_WEAPON_B, 0, WEP_HOOK.m_id);
369 W_MuzzleFlash(WEP_HOOK, actor, weaponentity, w_shotorg, '0 0 0');
370 actor.(weaponentity).movedir = oldmovedir;
372 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
373 missile.owner = missile.realowner = actor;
374 actor.(weaponentity).hook = missile;
375 missile.weaponentity_fld = weaponentity;
376 missile.reset = GrapplingHookReset;
377 missile.classname = "grapplinghook";
378 missile.flags = FL_PROJECTILE;
379 IL_PUSH(g_projectiles, missile);
380 IL_PUSH(g_bot_dodge, missile);
382 set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
383 PROJECTILE_MAKETRIGGER(missile);
385 //setmodel (missile, MDL_HOOK); // precision set below
386 setsize (missile, '-3 -3 -3', '3 3 3');
387 setorigin(missile, w_shotorg);
389 missile.state = 0; // not latched onto anything
391 W_SetupProjVelocity_Explicit(missile, w_shotdir, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
393 missile.angles = vectoangles (missile.velocity);
394 //missile.glow_color = 250; // 244, 250
395 //missile.glow_size = 120;
396 settouch(missile, GrapplingHookTouch);
397 setthink(missile, GrapplingHookThink);
398 missile.nextthink = time;
400 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
402 SetResourceExplicit(missile, RES_HEALTH, autocvar_g_balance_grapplehook_health);
403 missile.event_damage = GrapplingHook_Damage;
404 missile.takedamage = DAMAGE_AIM;
405 missile.damageforcescale = 0;
406 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
407 if(missile.damagedbycontents)
408 IL_PUSH(g_damagedbycontents, missile);
410 missile.hook_start = missile.hook_end = missile.origin;
412 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
415 void GrappleHookInit()
419 hook_shotorigin[0] = '8 8 -12';
420 hook_shotorigin[1] = '8 8 -12';
421 hook_shotorigin[2] = '8 8 -12';
422 hook_shotorigin[3] = '8 8 -12';
428 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
429 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
430 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
431 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);