3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include <common/effects/all.qh>
6 #include "weapons/common.qh"
7 #include "weapons/csqcprojectile.qh"
8 #include "weapons/weaponsystem.qh"
9 #include "weapons/selection.qh"
10 #include "weapons/tracing.qh"
12 #include "command/common.qh"
13 #include "round_handler.qh"
14 #include "../common/state.qh"
15 #include "../common/physics/player.qh"
16 #include "../common/vehicles/all.qh"
17 #include "../common/constants.qh"
18 #include "../common/util.qh"
19 #include <common/net_linked.qh>
20 #include <common/weapons/_all.qh>
21 #include "../lib/warpzone/common.qh"
22 #include "../lib/warpzone/server.qh"
24 /*============================================
26 Wazat's Xonotic Grappling Hook
28 Contact: Wazat1@gmail.com
31 Installation instructions:
32 --------------------------
34 1. Place hook.c in your gamec source directory with the other source files.
36 2. Add this line to the bottom of progs.src:
40 3. Open defs.h and add these lines to the very bottom:
42 // Wazat's grappling hook
44 void GrapplingHookFrame();
45 void RemoveGrapplingHook(entity pl);
46 void SetGrappleHookBindings();
48 const float GRAPHOOK_FIRE = 20;
49 const float GRAPHOOK_RELEASE = 21;
50 // (note: you can change the hook impulse #'s to whatever you please)
52 4. Open client.c and add this to the top of PutClientInServer():
54 RemoveGrapplingHook(this); // Wazat's Grappling Hook
56 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
58 // Wazat's grappling hook
59 SetGrappleHookBindings();
61 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
65 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
68 And you should be done!
71 ============================================*/
73 void RemoveGrapplingHooks(entity pl)
75 if(pl.move_movetype == MOVETYPE_FLY)
76 set_movetype(pl, MOVETYPE_WALK);
78 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
80 .entity weaponentity = weaponentities[slot];
81 if(!pl.(weaponentity))
82 continue; // continue incase other slots exist?
83 if(pl.(weaponentity).hook)
84 delete(pl.(weaponentity).hook);
85 pl.(weaponentity).hook = NULL;
88 //pl.disableclientprediction = false;
91 void RemoveHook(entity this)
93 entity player = this.realowner;
94 .entity weaponentity = this.weaponentity_fld;
96 if(player.(weaponentity).hook == this)
97 player.(weaponentity).hook = NULL;
99 if(player.move_movetype == MOVETYPE_FLY)
100 set_movetype(player, MOVETYPE_WALK);
104 void GrapplingHookReset(entity this)
109 void GrapplingHook_Stop(entity this)
111 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
112 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
115 setthink(this, GrapplingHookThink);
116 this.nextthink = time;
117 settouch(this, func_null);
118 this.velocity = '0 0 0';
119 set_movetype(this, MOVETYPE_NONE);
120 this.hook_length = -1;
123 .vector hook_start, hook_end;
124 bool GrapplingHookSend(entity this, entity to, int sf)
126 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
128 if(sound_allowed(MSG_BROADCAST, this.realowner))
130 WriteByte(MSG_ENTITY, sf);
133 WriteByte(MSG_ENTITY, etof(this.realowner));
134 WriteByte(MSG_ENTITY, weaponslot(this.weaponentity_fld));
138 WriteVector(MSG_ENTITY, this.hook_start);
142 WriteVector(MSG_ENTITY, this.hook_end);
147 int autocvar_g_grappling_hook_tarzan;
149 void GrapplingHookThink(entity this)
151 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
152 vector dir, org, end, v0, dv, v, myorg, vs;
153 .entity weaponentity = this.weaponentity_fld;
154 if(this.realowner.(weaponentity).hook != this) // how did that happen?
156 error("Owner lost the hook!\n");
159 if(LostMovetypeFollow(this) || game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
165 WarpZone_RefSys_AddIncrementally(this, this.aiment);
167 this.nextthink = time;
169 int s = W_GunAlign(this.realowner.(weaponentity), STAT(GUNALIGN, this.realowner)) - 1;
170 vs = hook_shotorigin[s];
172 makevectors(this.realowner.v_angle);
173 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
174 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
176 if(this.hook_length < 0)
177 this.hook_length = vlen(myorg - this.origin);
179 int tarzan = autocvar_g_grappling_hook_tarzan;
180 entity pull_entity = this.realowner;
181 float velocity_multiplier = 1;
182 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
183 tarzan = M_ARGV(1, int);
184 pull_entity = M_ARGV(2, entity);
185 velocity_multiplier = M_ARGV(3, float);
189 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
190 // speed the rope is pulled with
192 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
193 // force the rope will use if it is stretched
195 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
196 // force the rope will use if it is stretched
198 minlength = autocvar_g_balance_grapplehook_length_min;//100;
199 // minimal rope length
200 // if the rope goes below this length, it isn't pulled any more
202 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
203 // if the rope is stretched by more than this amount, more rope is
204 // given to you again
206 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
207 // while hanging on the rope, this friction component will help you a
208 // bit to control the rope
210 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
212 dir = this.origin - myorg;
214 dir = normalize(dir);
218 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
220 // first pull the rope...
221 if(this.realowner.(weaponentity).hook_state & HOOK_PULLING)
223 newlength = this.hook_length;
224 newlength = max(newlength - pullspeed * frametime, minlength);
226 if(newlength < dist - ropestretch) // overstretched?
228 newlength = dist - ropestretch;
229 if(v * dir < 0) // only if not already moving in hook direction
230 v = v + frametime * dir * rubberforce_overstretch;
233 this.hook_length = newlength;
236 if(pull_entity.move_movetype == MOVETYPE_FLY)
237 set_movetype(pull_entity, MOVETYPE_WALK);
239 if(this.realowner.(weaponentity).hook_state & HOOK_RELEASING)
242 this.hook_length = newlength;
246 // then pull the player
247 spd = bound(0, (dist - this.hook_length) / ropestretch, 1);
248 v = v * (1 - frametime * ropeairfriction);
249 v = v + frametime * dir * spd * rubberforce;
251 dv = ((v - v0) * dir) * dir;
254 if(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
256 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
258 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
260 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
261 UNSET_ONGROUND(this.aiment);
262 if(this.aiment.flags & FL_PROJECTILE)
263 UpdateCSQCProjectile(this.aiment);
265 if(this.aiment.classname == "nade")
266 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
267 aim_ent.pusher = this.realowner;
268 aim_ent.pushltime = time + autocvar_g_maxpushtime;
269 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
273 UNSET_ONGROUND(pull_entity);
276 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
277 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
279 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
287 end = this.origin - dir*50;
288 dist = vlen(end - myorg);
290 spd = dist * (pullspeed / 200);
295 this.realowner.velocity = dir*spd;
296 set_movetype(this.realowner, MOVETYPE_FLY);
298 UNSET_ONGROUND(this.realowner);
302 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
303 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
305 if(myorg != this.hook_start)
308 this.hook_start = myorg;
310 if(org != this.hook_end)
317 void GrapplingHookTouch(entity this, entity toucher)
319 if(toucher.move_movetype == MOVETYPE_FOLLOW)
321 PROJECTILE_TOUCH(this, toucher);
323 GrapplingHook_Stop(this);
326 //if(toucher.move_movetype != MOVETYPE_NONE)
328 SetMovetypeFollow(this, toucher);
329 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
332 //this.realowner.disableclientprediction = true;
335 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
337 if(GetResource(this, RES_HEALTH) <= 0)
340 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
341 return; // g_balance_projectiledamage says to halt
343 TakeResource(this, RES_HEALTH, damage);
345 if (GetResource(this, RES_HEALTH) <= 0)
347 if(attacker != this.realowner)
349 this.realowner.pusher = attacker;
350 this.realowner.pushltime = time + autocvar_g_maxpushtime;
351 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
357 void FireGrapplingHook(entity actor, .entity weaponentity)
359 if(weaponLocked(actor)) return;
360 if(actor.vehicle) return;
362 // TODO: offhand hook shoots from eye
363 W_SetupShot_ProjectileSize(actor, weaponentity, '-3 -3 -3', '3 3 3', true, 0, SND_HOOK_FIRE, CH_WEAPON_B, 0, WEP_HOOK.m_id);
364 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, w_shotorg, '0 0 0', 1);
366 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
367 missile.owner = missile.realowner = actor;
368 actor.(weaponentity).hook = missile;
369 missile.weaponentity_fld = weaponentity;
370 missile.reset = GrapplingHookReset;
371 missile.classname = "grapplinghook";
372 missile.flags = FL_PROJECTILE;
373 IL_PUSH(g_projectiles, missile);
374 IL_PUSH(g_bot_dodge, missile);
376 set_movetype(missile, ((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, w_shotorg);
383 missile.state = 0; // not latched onto anything
385 W_SetupProjVelocity_Explicit(missile, w_shotdir, 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 settouch(missile, GrapplingHookTouch);
391 setthink(missile, GrapplingHookThink);
392 missile.nextthink = time;
394 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
396 SetResourceExplicit(missile, RES_HEALTH, autocvar_g_balance_grapplehook_health);
397 missile.event_damage = GrapplingHook_Damage;
398 missile.takedamage = DAMAGE_AIM;
399 missile.damageforcescale = 0;
400 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
401 if(missile.damagedbycontents)
402 IL_PUSH(g_damagedbycontents, missile);
404 missile.hook_start = missile.hook_end = missile.origin;
406 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
409 void GrappleHookInit()
413 hook_shotorigin[0] = '8 8 -12';
414 hook_shotorigin[1] = '8 8 -12';
415 hook_shotorigin[2] = '8 8 -12';
416 hook_shotorigin[3] = '8 8 -12';
422 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
423 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
424 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
425 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);