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"
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 ============================================*/
77 void RemoveGrapplingHooks(entity pl)
79 if(pl.move_movetype == MOVETYPE_FLY)
80 set_movetype(pl, MOVETYPE_WALK);
82 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
84 .entity weaponentity = weaponentities[slot];
85 if(!pl.(weaponentity))
86 continue; // continue incase other slots exist?
87 if(pl.(weaponentity).hook)
88 delete(pl.(weaponentity).hook);
89 pl.(weaponentity).hook = NULL;
92 //pl.disableclientprediction = false;
95 void RemoveHook(entity this)
97 entity player = this.realowner;
98 .entity weaponentity = this.weaponentity_fld;
100 if(player.(weaponentity).hook == this)
101 player.(weaponentity).hook = NULL;
103 if(player.move_movetype == MOVETYPE_FLY)
104 set_movetype(player, MOVETYPE_WALK);
108 void GrapplingHookReset(entity this)
113 void GrapplingHookThink(entity this);
114 void GrapplingHook_Stop(entity this)
116 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
117 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
120 setthink(this, GrapplingHookThink);
121 this.nextthink = time;
122 settouch(this, func_null);
123 this.velocity = '0 0 0';
124 set_movetype(this, MOVETYPE_NONE);
125 this.hook_length = -1;
128 .vector hook_start, hook_end;
129 bool GrapplingHookSend(entity this, entity to, int sf)
131 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
133 if(sound_allowed(MSG_BROADCAST, this.realowner))
135 WriteByte(MSG_ENTITY, sf);
138 WriteByte(MSG_ENTITY, etof(this.realowner));
139 WriteByte(MSG_ENTITY, weaponslot(this.weaponentity_fld));
143 WriteVector(MSG_ENTITY, this.hook_start);
147 WriteVector(MSG_ENTITY, this.hook_end);
152 int autocvar_g_grappling_hook_tarzan;
154 void GrapplingHookThink(entity this)
156 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
157 vector dir, org, end, v0, dv, v, myorg, vs;
158 .entity weaponentity = this.weaponentity_fld;
159 if(this.realowner.(weaponentity).hook != this) // how did that happen?
161 error("Owner lost the hook!\n");
164 if(LostMovetypeFollow(this) || game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
170 WarpZone_RefSys_AddIncrementally(this, this.aiment);
172 this.nextthink = time;
174 int s = W_GunAlign(this.realowner.(weaponentity), STAT(GUNALIGN, this.realowner)) - 1;
175 vs = hook_shotorigin[s];
177 makevectors(this.realowner.v_angle);
178 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
179 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
181 if(this.hook_length < 0)
182 this.hook_length = vlen(myorg - this.origin);
184 int tarzan = autocvar_g_grappling_hook_tarzan;
185 entity pull_entity = this.realowner;
186 float velocity_multiplier = 1;
187 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
188 tarzan = M_ARGV(1, int);
189 pull_entity = M_ARGV(2, entity);
190 velocity_multiplier = M_ARGV(3, float);
194 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
195 // speed the rope is pulled with
197 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
198 // force the rope will use if it is stretched
200 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
201 // force the rope will use if it is stretched
203 minlength = autocvar_g_balance_grapplehook_length_min;//100;
204 // minimal rope length
205 // if the rope goes below this length, it isn't pulled any more
207 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
208 // if the rope is stretched by more than this amount, more rope is
209 // given to you again
211 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
212 // while hanging on the rope, this friction component will help you a
213 // bit to control the rope
215 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
217 dir = this.origin - myorg;
219 dir = normalize(dir);
223 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
225 // first pull the rope...
226 if(this.realowner.(weaponentity).hook_state & HOOK_PULLING)
228 newlength = this.hook_length;
229 newlength = max(newlength - pullspeed * frametime, minlength);
231 if(newlength < dist - ropestretch) // overstretched?
233 newlength = dist - ropestretch;
234 if(v * dir < 0) // only if not already moving in hook direction
235 v = v + frametime * dir * rubberforce_overstretch;
238 this.hook_length = newlength;
241 if(pull_entity.move_movetype == MOVETYPE_FLY)
242 set_movetype(pull_entity, MOVETYPE_WALK);
244 if(this.realowner.(weaponentity).hook_state & HOOK_RELEASING)
247 this.hook_length = newlength;
251 // then pull the player
252 spd = bound(0, (dist - this.hook_length) / ropestretch, 1);
253 v = v * (1 - frametime * ropeairfriction);
254 v = v + frametime * dir * spd * rubberforce;
256 dv = ((v - v0) * dir) * dir;
259 if(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
261 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
263 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
265 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
266 UNSET_ONGROUND(this.aiment);
267 if(this.aiment.flags & FL_PROJECTILE)
268 UpdateCSQCProjectile(this.aiment);
270 if(this.aiment.classname == "nade")
271 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
272 aim_ent.pusher = this.realowner;
273 aim_ent.pushltime = time + autocvar_g_maxpushtime;
274 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
278 UNSET_ONGROUND(pull_entity);
281 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
282 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
284 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
292 end = this.origin - dir*50;
293 dist = vlen(end - myorg);
295 spd = dist * (pullspeed / 200);
300 this.realowner.velocity = dir*spd;
301 set_movetype(this.realowner, MOVETYPE_FLY);
303 UNSET_ONGROUND(this.realowner);
307 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
308 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
310 if(myorg != this.hook_start)
313 this.hook_start = myorg;
315 if(org != this.hook_end)
322 void GrapplingHookTouch(entity this, entity toucher)
324 if(toucher.move_movetype == MOVETYPE_FOLLOW)
326 PROJECTILE_TOUCH(this, toucher);
328 GrapplingHook_Stop(this);
331 if(toucher.move_movetype != MOVETYPE_NONE)
333 SetMovetypeFollow(this, toucher);
334 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
337 //this.realowner.disableclientprediction = true;
340 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
345 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
346 return; // g_balance_projectiledamage says to halt
348 this.health = this.health - damage;
350 if (this.health <= 0)
352 if(attacker != this.realowner)
354 this.realowner.pusher = attacker;
355 this.realowner.pushltime = time + autocvar_g_maxpushtime;
356 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
362 void FireGrapplingHook(entity actor, .entity weaponentity)
364 if(forbidWeaponUse(actor)) return;
365 if(actor.vehicle) return;
367 makevectors(actor.v_angle);
369 int s = W_GunAlign(actor.(weaponentity), STAT(GUNALIGN, actor)) - 1;
370 vector vs = hook_shotorigin[s];
372 // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
373 sound (actor, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
374 vector org = actor.origin + actor.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
376 tracebox(actor.origin + actor.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, actor);
379 Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
381 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
382 missile.owner = missile.realowner = actor;
383 actor.(weaponentity).hook = missile;
384 missile.weaponentity_fld = weaponentity;
385 missile.reset = GrapplingHookReset;
386 missile.classname = "grapplinghook";
387 missile.flags = FL_PROJECTILE;
388 IL_PUSH(g_projectiles, missile);
389 IL_PUSH(g_bot_dodge, missile);
391 set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
392 PROJECTILE_MAKETRIGGER(missile);
394 //setmodel (missile, MDL_HOOK); // precision set below
395 setsize (missile, '-3 -3 -3', '3 3 3');
396 setorigin(missile, org);
398 missile.state = 0; // not latched onto anything
400 W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
402 missile.angles = vectoangles (missile.velocity);
403 //missile.glow_color = 250; // 244, 250
404 //missile.glow_size = 120;
405 settouch(missile, GrapplingHookTouch);
406 setthink(missile, GrapplingHookThink);
407 missile.nextthink = time;
409 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
411 missile.health = autocvar_g_balance_grapplehook_health;//120
412 missile.event_damage = GrapplingHook_Damage;
413 missile.takedamage = DAMAGE_AIM;
414 missile.damageforcescale = 0;
415 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
416 if(missile.damagedbycontents)
417 IL_PUSH(g_damagedbycontents, missile);
419 missile.hook_start = missile.hook_end = missile.origin;
421 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
424 void GrappleHookInit()
428 hook_shotorigin[0] = '8 8 -12';
429 hook_shotorigin[1] = '8 8 -12';
430 hook_shotorigin[2] = '8 8 -12';
431 hook_shotorigin[3] = '8 8 -12';
437 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
438 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
439 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
440 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);