3 #include <server/bot/api.qh>
4 #include <common/weapons/_all.qh>
5 #include <common/stats.qh>
6 #include <server/damage.qh>
7 #include <server/miscfunctions.qh>
8 #include <common/effects/all.qh>
9 #include "weapons/common.qh"
10 #include "weapons/csqcprojectile.qh"
11 #include "weapons/weaponsystem.qh"
12 #include "weapons/selection.qh"
13 #include "weapons/tracing.qh"
15 #include "command/common.qh"
16 #include "command/vote.qh"
17 #include "round_handler.qh"
18 #include "../common/state.qh"
19 #include "../common/physics/player.qh"
20 #include "../common/vehicles/all.qh"
21 #include "../common/constants.qh"
22 #include "../common/util.qh"
23 #include <common/net_linked.qh>
24 #include <common/weapons/_all.qh>
25 #include "../lib/warpzone/common.qh"
26 #include "../lib/warpzone/server.qh"
28 /*============================================
30 Wazat's Xonotic Grappling Hook
32 Contact: Wazat1@gmail.com
35 Installation instructions:
36 --------------------------
38 1. Place hook.c in your gamec source directory with the other source files.
40 2. Add this line to the bottom of progs.src:
44 3. Open defs.h and add these lines to the very bottom:
46 // Wazat's grappling hook
48 void GrapplingHookFrame();
49 void RemoveGrapplingHook(entity pl);
50 void SetGrappleHookBindings();
52 const float GRAPHOOK_FIRE = 20;
53 const float GRAPHOOK_RELEASE = 21;
54 // (note: you can change the hook impulse #'s to whatever you please)
56 4. Open client.c and add this to the top of PutClientInServer():
58 RemoveGrapplingHook(this); // Wazat's Grappling Hook
60 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
62 // Wazat's grappling hook
63 SetGrappleHookBindings();
65 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
69 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
72 And you should be done!
75 ============================================*/
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 GrapplingHook_Stop(entity this)
115 Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
116 sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
119 setthink(this, GrapplingHookThink);
120 this.nextthink = time;
121 settouch(this, func_null);
122 this.velocity = '0 0 0';
123 set_movetype(this, MOVETYPE_NONE);
124 this.hook_length = -1;
127 .vector hook_start, hook_end;
128 bool GrapplingHookSend(entity this, entity to, int sf)
130 WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
132 if(sound_allowed(MSG_BROADCAST, this.realowner))
134 WriteByte(MSG_ENTITY, sf);
137 WriteByte(MSG_ENTITY, etof(this.realowner));
138 WriteByte(MSG_ENTITY, weaponslot(this.weaponentity_fld));
142 WriteVector(MSG_ENTITY, this.hook_start);
146 WriteVector(MSG_ENTITY, this.hook_end);
151 int autocvar_g_grappling_hook_tarzan;
153 void GrapplingHookThink(entity this)
155 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
156 vector dir, org, end, v0, dv, v, myorg, vs;
157 .entity weaponentity = this.weaponentity_fld;
158 if(this.realowner.(weaponentity).hook != this) // how did that happen?
160 error("Owner lost the hook!\n");
163 if(LostMovetypeFollow(this) || game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
169 WarpZone_RefSys_AddIncrementally(this, this.aiment);
171 this.nextthink = time;
173 int s = W_GunAlign(this.realowner.(weaponentity), STAT(GUNALIGN, this.realowner)) - 1;
174 vs = hook_shotorigin[s];
176 makevectors(this.realowner.v_angle);
177 org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
178 myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
180 if(this.hook_length < 0)
181 this.hook_length = vlen(myorg - this.origin);
183 int tarzan = autocvar_g_grappling_hook_tarzan;
184 entity pull_entity = this.realowner;
185 float velocity_multiplier = 1;
186 MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
187 tarzan = M_ARGV(1, int);
188 pull_entity = M_ARGV(2, entity);
189 velocity_multiplier = M_ARGV(3, float);
193 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
194 // speed the rope is pulled with
196 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
197 // force the rope will use if it is stretched
199 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
200 // force the rope will use if it is stretched
202 minlength = autocvar_g_balance_grapplehook_length_min;//100;
203 // minimal rope length
204 // if the rope goes below this length, it isn't pulled any more
206 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
207 // if the rope is stretched by more than this amount, more rope is
208 // given to you again
210 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
211 // while hanging on the rope, this friction component will help you a
212 // bit to control the rope
214 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
216 dir = this.origin - myorg;
218 dir = normalize(dir);
222 v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
224 // first pull the rope...
225 if(this.realowner.(weaponentity).hook_state & HOOK_PULLING)
227 newlength = this.hook_length;
228 newlength = max(newlength - pullspeed * frametime, minlength);
230 if(newlength < dist - ropestretch) // overstretched?
232 newlength = dist - ropestretch;
233 if(v * dir < 0) // only if not already moving in hook direction
234 v = v + frametime * dir * rubberforce_overstretch;
237 this.hook_length = newlength;
240 if(pull_entity.move_movetype == MOVETYPE_FLY)
241 set_movetype(pull_entity, MOVETYPE_WALK);
243 if(this.realowner.(weaponentity).hook_state & HOOK_RELEASING)
246 this.hook_length = newlength;
250 // then pull the player
251 spd = bound(0, (dist - this.hook_length) / ropestretch, 1);
252 v = v * (1 - frametime * ropeairfriction);
253 v = v + frametime * dir * spd * rubberforce;
255 dv = ((v - v0) * dir) * dir;
258 if(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
260 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
262 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
264 this.aiment.velocity = this.aiment.velocity - dv * 0.5;
265 UNSET_ONGROUND(this.aiment);
266 if(this.aiment.flags & FL_PROJECTILE)
267 UpdateCSQCProjectile(this.aiment);
269 if(this.aiment.classname == "nade")
270 this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
271 aim_ent.pusher = this.realowner;
272 aim_ent.pushltime = time + autocvar_g_maxpushtime;
273 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
277 UNSET_ONGROUND(pull_entity);
280 if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
281 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
283 if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
291 end = this.origin - dir*50;
292 dist = vlen(end - myorg);
294 spd = dist * (pullspeed / 200);
299 this.realowner.velocity = dir*spd;
300 set_movetype(this.realowner, MOVETYPE_FLY);
302 UNSET_ONGROUND(this.realowner);
306 makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
307 myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
309 if(myorg != this.hook_start)
312 this.hook_start = myorg;
314 if(org != this.hook_end)
321 void GrapplingHookTouch(entity this, entity toucher)
323 if(toucher.move_movetype == MOVETYPE_FOLLOW)
325 PROJECTILE_TOUCH(this, toucher);
327 GrapplingHook_Stop(this);
330 //if(toucher.move_movetype != MOVETYPE_NONE)
332 SetMovetypeFollow(this, toucher);
333 WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
336 //this.realowner.disableclientprediction = true;
339 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
341 if(GetResource(this, RES_HEALTH) <= 0)
344 if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
345 return; // g_balance_projectiledamage says to halt
347 TakeResource(this, RES_HEALTH, damage);
349 if (GetResource(this, RES_HEALTH) <= 0)
351 if(attacker != this.realowner)
353 this.realowner.pusher = attacker;
354 this.realowner.pushltime = time + autocvar_g_maxpushtime;
355 this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
361 void FireGrapplingHook(entity actor, .entity weaponentity)
363 if(weaponLocked(actor)) return;
364 if(actor.vehicle) return;
366 int s = W_GunAlign(actor.(weaponentity), STAT(GUNALIGN, actor)) - 1;
367 vector vs = hook_shotorigin[s];
368 vector oldmovedir = actor.(weaponentity).movedir;
369 actor.(weaponentity).movedir = vs;
370 W_SetupShot_ProjectileSize(actor, weaponentity, '-3 -3 -3', '3 3 3', true, 0, SND_HOOK_FIRE, CH_WEAPON_B, 0, WEP_HOOK.m_id);
371 W_MuzzleFlash(WEP_HOOK, actor, weaponentity, w_shotorg, '0 0 0');
372 actor.(weaponentity).movedir = oldmovedir;
374 entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
375 missile.owner = missile.realowner = actor;
376 actor.(weaponentity).hook = missile;
377 missile.weaponentity_fld = weaponentity;
378 missile.reset = GrapplingHookReset;
379 missile.classname = "grapplinghook";
380 missile.flags = FL_PROJECTILE;
381 IL_PUSH(g_projectiles, missile);
382 IL_PUSH(g_bot_dodge, missile);
384 set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
385 PROJECTILE_MAKETRIGGER(missile);
387 //setmodel (missile, MDL_HOOK); // precision set below
388 setsize (missile, '-3 -3 -3', '3 3 3');
389 setorigin(missile, w_shotorg);
391 missile.state = 0; // not latched onto anything
393 W_SetupProjVelocity_Explicit(missile, w_shotdir, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
395 missile.angles = vectoangles (missile.velocity);
396 //missile.glow_color = 250; // 244, 250
397 //missile.glow_size = 120;
398 settouch(missile, GrapplingHookTouch);
399 setthink(missile, GrapplingHookThink);
400 missile.nextthink = time;
402 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
404 SetResourceExplicit(missile, RES_HEALTH, autocvar_g_balance_grapplehook_health);
405 missile.event_damage = GrapplingHook_Damage;
406 missile.takedamage = DAMAGE_AIM;
407 missile.damageforcescale = 0;
408 missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
409 if(missile.damagedbycontents)
410 IL_PUSH(g_damagedbycontents, missile);
412 missile.hook_start = missile.hook_end = missile.origin;
414 Net_LinkEntity(missile, false, 0, GrapplingHookSend);
417 // NOTE: using PRECACHE here to make sure it's called after everything else
418 PRECACHE(GrappleHookInit)
422 hook_shotorigin[0] = '8 8 -12';
423 hook_shotorigin[1] = '8 8 -12';
424 hook_shotorigin[2] = '8 8 -12';
425 hook_shotorigin[3] = '8 8 -12';
431 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
432 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
433 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
434 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);