#include "g_hook.qh" #include "weapons/common.qh" #include "weapons/csqcprojectile.qh" #include "weapons/weaponsystem.qh" #include "weapons/selection.qh" #include "weapons/tracing.qh" #include "cl_player.qh" #include "command/common.qh" #include "round_handler.qh" #include "../common/vehicles/all.qh" #include "../common/constants.qh" #include "../common/util.qh" #include "../common/weapons/all.qh" #include "../lib/warpzone/common.qh" #include "../lib/warpzone/server.qh" .int state; /*============================================ Wazat's Xonotic Grappling Hook Contact: Wazat1@gmail.com Installation instructions: -------------------------- 1. Place hook.c in your gamec source directory with the other source files. 2. Add this line to the bottom of progs.src: gamec/hook.c 3. Open defs.h and add these lines to the very bottom: // Wazat's grappling hook .entity hook; void GrapplingHookFrame(); void RemoveGrapplingHook(entity pl); void SetGrappleHookBindings(); // hook impulses const float GRAPHOOK_FIRE = 20; const float GRAPHOOK_RELEASE = 21; // (note: you can change the hook impulse #'s to whatever you please) 4. Open client.c and add this to the top of PutClientInServer(): RemoveGrapplingHook(self); // Wazat's Grappling Hook 5. Find ClientConnect() (in client.c) and add these lines to the bottom: // Wazat's grappling hook SetGrappleHookBindings(); 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame: GrapplingHookFrame(); 7. Build and test the mod. You'll want to bind a key to "+hook" like this: bind ctrl "+hook" And you should be done! ============================================*/ .float hook_length; void RemoveGrapplingHook(entity pl) { if(pl.hook == world) return; remove(pl.hook); pl.hook = world; if(pl.movetype == MOVETYPE_FLY) pl.movetype = MOVETYPE_WALK; //pl.disableclientprediction = false; } void GrapplingHookReset(entity this) { if(this.realowner.hook == this) RemoveGrapplingHook(this.owner); else // in any case: remove(this); } void GrapplingHookThink(); void GrapplingHook_Stop() {SELFPARAM(); Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1); sound (self, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM); self.state = 1; self.think = GrapplingHookThink; self.nextthink = time; self.touch = func_null; self.velocity = '0 0 0'; self.movetype = MOVETYPE_NONE; self.hook_length = -1; } .vector hook_start, hook_end; bool GrapplingHookSend(entity this, entity to, int sf) { WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK); sf = sf & 0x7F; if(sound_allowed(MSG_BROADCAST, self.realowner)) sf |= 0x80; WriteByte(MSG_ENTITY, sf); if(sf & 1) { WriteByte(MSG_ENTITY, num_for_edict(self.realowner)); } if(sf & 2) { WriteCoord(MSG_ENTITY, self.hook_start.x); WriteCoord(MSG_ENTITY, self.hook_start.y); WriteCoord(MSG_ENTITY, self.hook_start.z); } if(sf & 4) { WriteCoord(MSG_ENTITY, self.hook_end.x); WriteCoord(MSG_ENTITY, self.hook_end.y); WriteCoord(MSG_ENTITY, self.hook_end.z); } return true; } int autocvar_g_grappling_hook_tarzan; void GrapplingHookThink() {SELFPARAM(); float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch; vector dir, org, end, v0, dv, v, myorg, vs; if(self.realowner.hook != self) // how did that happen? { error("Owner lost the hook!\n"); return; } if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade")) { RemoveGrapplingHook(self.realowner); return; } if(self.aiment) WarpZone_RefSys_AddIncrementally(self, self.aiment); self.nextthink = time; int s = W_GetGunAlignment(self.realowner); vs = hook_shotorigin[s]; makevectors(self.realowner.v_angle); org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z; myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org); if(self.hook_length < 0) self.hook_length = vlen(myorg - self.origin); int tarzan = autocvar_g_grappling_hook_tarzan; entity pull_entity = self.realowner; float velocity_multiplier = 1; MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier); tarzan = hook_tarzan; pull_entity = hook_pullentity; velocity_multiplier = hook_velmultiplier; if(self.state == 1) { pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000; // speed the rope is pulled with rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000; // force the rope will use if it is stretched rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000; // force the rope will use if it is stretched minlength = autocvar_g_balance_grapplehook_length_min;//100; // minimal rope length // if the rope goes below this length, it isn't pulled any more ropestretch = autocvar_g_balance_grapplehook_stretch;//400; // if the rope is stretched by more than this amount, more rope is // given to you again ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2 // while hanging on the rope, this friction component will help you a // bit to control the rope bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen); dir = self.origin - myorg; dist = vlen(dir); dir = normalize(dir); if(tarzan) { v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity); // first pull the rope... if(self.realowner.hook_state & HOOK_PULLING) { newlength = self.hook_length; newlength = max(newlength - pullspeed * frametime, minlength); if(newlength < dist - ropestretch) // overstretched? { newlength = dist - ropestretch; if(v * dir < 0) // only if not already moving in hook direction v = v + frametime * dir * rubberforce_overstretch; } self.hook_length = newlength; } if(pull_entity.movetype == MOVETYPE_FLY) pull_entity.movetype = MOVETYPE_WALK; if(self.realowner.hook_state & HOOK_RELEASING) { newlength = dist; self.hook_length = newlength; } else { // then pull the player spd = bound(0, (dist - self.hook_length) / ropestretch, 1); v = v * (1 - frametime * ropeairfriction); v = v + frametime * dir * spd * rubberforce; dv = ((v - v0) * dir) * dir; if(tarzan >= 2) { if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade") { entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment); v = v - dv * 0.5; if((frozen_pulling && self.aiment.frozen) || !frozen_pulling) { self.aiment.velocity = self.aiment.velocity - dv * 0.5; self.aiment.flags &= ~FL_ONGROUND; if(self.aiment.flags & FL_PROJECTILE) UpdateCSQCProjectile(self.aiment); } if(self.aiment.classname == "nade") self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go? aim_ent.pusher = self.realowner; aim_ent.pushltime = time + autocvar_g_maxpushtime; aim_ent.istypefrag = aim_ent.BUTTON_CHAT; } } pull_entity.flags &= ~FL_ONGROUND; } if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE)) pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier); if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen) { RemoveGrapplingHook(self.realowner); return; } } else { end = self.origin - dir*50; dist = vlen(end - myorg); if(dist < 200) spd = dist * (pullspeed / 200); else spd = pullspeed; if(spd < 50) spd = 0; self.realowner.velocity = dir*spd; self.realowner.movetype = MOVETYPE_FLY; self.realowner.flags &= ~FL_ONGROUND; } } makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0'); myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9); if(myorg != self.hook_start) { self.SendFlags |= 2; self.hook_start = myorg; } if(org != self.hook_end) { self.SendFlags |= 4; self.hook_end = org; } } void GrapplingHookTouch () {SELFPARAM(); if(other.movetype == MOVETYPE_FOLLOW) return; PROJECTILE_TOUCH; GrapplingHook_Stop(); if(other) if(other.movetype != MOVETYPE_NONE) { SetMovetypeFollow(self, other); WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment); } //self.realowner.disableclientprediction = true; } void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force) {SELFPARAM(); if(self.health <= 0) return; if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions return; // g_balance_projectiledamage says to halt self.health = self.health - damage; if (self.health <= 0) { if(attacker != self.realowner) { self.realowner.pusher = attacker; self.realowner.pushltime = time + autocvar_g_maxpushtime; self.realowner.istypefrag = self.realowner.BUTTON_CHAT; } RemoveGrapplingHook(self.realowner); } } void FireGrapplingHook () {SELFPARAM(); entity missile; vector org; vector vs; if(forbidWeaponUse(self)) return; if(self.vehicle) return; makevectors(self.v_angle); int s = W_GetGunAlignment(self); vs = hook_shotorigin[s]; // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM); org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z; tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self); org = trace_endpos; Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1); missile = WarpZone_RefSys_SpawnSameRefSys(self); missile.owner = missile.realowner = self; self.hook = missile; missile.reset = GrapplingHookReset; missile.classname = "grapplinghook"; missile.flags = FL_PROJECTILE; missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY); PROJECTILE_MAKETRIGGER(missile); //setmodel (missile, MDL_HOOK); // precision set below setsize (missile, '-3 -3 -3', '3 3 3'); setorigin (missile, org); missile.state = 0; // not latched onto anything W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false); missile.angles = vectoangles (missile.velocity); //missile.glow_color = 250; // 244, 250 //missile.glow_size = 120; missile.touch = GrapplingHookTouch; missile.think = GrapplingHookThink; missile.nextthink = time; missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION; missile.health = autocvar_g_balance_grapplehook_health;//120 missile.event_damage = GrapplingHook_Damage; missile.takedamage = DAMAGE_AIM; missile.damageforcescale = 0; missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents); missile.hook_start = missile.hook_end = missile.origin; Net_LinkEntity(missile, false, 0, GrapplingHookSend); } void GrappleHookInit() { if(g_grappling_hook) { hook_shotorigin[0] = '8 8 -12'; hook_shotorigin[1] = '8 8 -12'; hook_shotorigin[2] = '8 8 -12'; hook_shotorigin[3] = '8 8 -12'; } else { Weapon w = WEP_HOOK; w.wr_init(w); hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1); hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2); hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3); hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4); } } void SetGrappleHookBindings() {SELFPARAM(); // this function has been modified for Xonotic // don't remove these lines! old server or demos coud overwrite the new aliases stuffcmd(self, "alias +hook +button6\n"); stuffcmd(self, "alias -hook -button6\n"); }