1 /*============================================
3 Wazat's Nexuiz Grappling Hook
5 Contact: Wazat1@gmail.com
8 Installation instructions:
9 --------------------------
11 1. Place hook.c in your gamec source directory with the other source files.
13 2. Add this line to the bottom of progs.src:
17 3. Open defs.h and add these lines to the very bottom:
19 // Wazat's grappling hook
21 void GrapplingHookFrame();
22 void RemoveGrapplingHook(entity pl);
23 void SetGrappleHookBindings();
25 float GRAPHOOK_FIRE = 20;
26 float GRAPHOOK_RELEASE = 21;
27 // (note: you can change the hook impulse #'s to whatever you please)
29 4. Open client.c and add this to the top of PutClientInServer():
31 RemoveGrapplingHook(self); // Wazat's Grappling Hook
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
35 // Wazat's grappling hook
36 SetGrappleHookBindings();
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
42 7. Build and test the mod. You'll want to bind a key to "+hook" like this:
45 And you should be done!
48 ============================================*/
50 .string aiment_classname;
51 .float aiment_deadflag;
52 void SetMovetypeFollow(entity ent, entity e)
54 ent.movetype = MOVETYPE_FOLLOW; // make the hole follow
55 ent.solid = SOLID_NOT; // MOVETYPE_FOLLOW is always non-solid
56 ent.aiment = e; // make the hole follow bmodel
57 ent.punchangle = e.angles; // the original angles of bmodel
58 ent.view_ofs = ent.origin - e.origin; // relative origin
59 ent.v_angle = ent.angles - e.angles; // relative angles
60 ent.aiment_classname = strzone(e.classname);
61 ent.aiment_deadflag = e.deadflag;
63 float LostMovetypeFollow(entity ent)
67 if(ent.aiment.classname != ent.aiment_classname)
69 if(ent.aiment.deadflag != ent.aiment_deadflag)
76 .float button6_pressed_before;
78 void RemoveGrapplingHook(entity pl)
84 if(pl.movetype == MOVETYPE_FLY)
85 pl.movetype = MOVETYPE_WALK;
87 pl.hook_time = time + 0.0;
89 //pl.disableclientprediction = FALSE;
92 void GrapplingHookThink()
94 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
95 vector dir, org, end, v0, dv;
96 if(self.owner.health <= 0 || self.owner.hook != self) // how did that happen?
97 { // well, better fix it anyway
101 if(LostMovetypeFollow(self))
103 RemoveGrapplingHook(self.owner);
107 self.nextthink = time;
109 makevectors(self.owner.v_angle);
110 org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
112 if(self.rope_length < 0)
113 self.rope_length = vlen(org - self.origin);
117 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
118 // speed the rope is pulled with
120 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
121 // force the rope will use if it is stretched
123 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
124 // force the rope will use if it is stretched
126 minlength = cvar("g_balance_grapplehook_length_min");//100;
127 // minimal rope length
128 // if the rope goes below this length, it isn't pulled any more
130 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
131 // if the rope is stretched by more than this amount, more rope is
132 // given to you again
134 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
135 // while hanging on the rope, this friction component will help you a
136 // bit to control the rope
138 dir = self.origin - org;
140 dir = normalize(dir);
142 if(cvar("g_grappling_hook_tarzan"))
144 newlength = self.rope_length;
145 v0 = self.owner.velocity;
147 // first pull the rope...
148 newlength = max(newlength - pullspeed * frametime, minlength);
150 if(newlength < dist - ropestretch) // overstretched?
152 newlength = dist - ropestretch;
153 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
154 self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
157 if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
158 self.rope_length = newlength;
160 // then pull the player
161 spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
162 self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
163 self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
165 dv = ((self.owner.velocity - v0) * dir) * dir;
166 if(cvar("g_grappling_hook_tarzan") >= 2)
168 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.movetype == MOVETYPE_TOSS)
170 self.owner.velocity = self.owner.velocity - dv * 0.5;
171 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
172 self.aiment.flags = self.aiment.flags - (self.aiment.flags & FL_ONGROUND);
178 end = self.origin - dir*50;
179 dist = vlen(end - org);
181 spd = dist * (pullspeed / 200);
186 self.owner.velocity = dir*spd;
187 self.owner.movetype = MOVETYPE_FLY;
190 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
192 org = org + dir*50; // get the beam out of the player's eyes
195 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
196 te_beam(self.owner, self.origin + v_forward * (-9), org);
199 void GrapplingHookTouch (void)
201 if (other == self.owner)
203 // altered for Nexuiz
204 //else if (pointcontents (self.origin) == CONTENT_SKY)
205 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
207 RemoveGrapplingHook(self.owner);
214 tic = self.velocity * sys_ticrate;
215 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
216 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
217 if(trace_fraction >= 1)
219 dprint("Odd... did not hit...?\n");
221 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
223 dprint("Detected and prevented the sky-grapple bug.\n");
224 RemoveGrapplingHook(self.owner);
229 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
230 sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
233 self.think = GrapplingHookThink;
234 self.nextthink = time;
235 self.touch = SUB_Null;
236 self.velocity = '0 0 0';
237 self.movetype = MOVETYPE_NONE;
238 self.rope_length = -1;
241 if(other.movetype != MOVETYPE_NONE)
242 SetMovetypeFollow(self, other);
244 //self.owner.disableclientprediction = TRUE;
247 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
251 self.health = self.health - damage;
252 if (self.health <= 0)
254 if(attacker != self.owner)
256 self.owner.pusher = attacker;
257 self.owner.pushltime = time + cvar("g_maxpushtime");
259 RemoveGrapplingHook(self.owner);
264 void FireGrapplingHook (void)
266 local entity missile;
269 if((arena_roundbased && time < warmup) || (time < restart_countdown))
272 makevectors(self.v_angle);
274 sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
275 org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
276 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
279 missile.owner = self;
281 missile.classname = "grapplinghook";
283 missile.movetype = MOVETYPE_FLY;
284 missile.solid = SOLID_BBOX;
286 setmodel (missile, "models/hook.md3"); // precision set below
287 setsize (missile, '-3 -3 -3', '3 3 3');
288 setorigin (missile, org);
290 missile.state = 0; // not latched onto anything
292 missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
293 W_SetupProjectileVelocity(missile);
295 missile.angles = vectoangles (missile.velocity);
296 //missile.glow_color = 250; // 244, 250
297 //missile.glow_size = 120;
298 missile.touch = GrapplingHookTouch;
299 missile.think = GrapplingHookThink;
300 missile.nextthink = time + 0.1;
302 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
304 missile.health = cvar("g_balance_grapplehook_health");//120
305 missile.event_damage = GrapplingHook_Damage;
306 missile.takedamage = DAMAGE_AIM;
307 missile.damageforcescale = 0;
310 void GrapplingHookFrame()
312 // this function has been modified for Nexuiz
313 if (self.BUTTON_HOOK && g_grappling_hook)
315 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
316 if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
322 RemoveGrapplingHook(self);
324 self.button6_pressed_before = self.BUTTON_HOOK;
326 // if I have no hook or it's not pulling yet, make sure I'm not flying!
327 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
329 self.movetype = MOVETYPE_WALK;
331 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
337 else if(self.hookimpulse == GRAPHOOK_RELEASE)
339 // remove hook, reset movement type
340 RemoveGrapplingHook(self);
344 /*else // make sure the player's movetype is correct
346 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
347 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
349 self.movetype = MOVETYPE_WALK;
352 // note: The hook entity does the actual pulling
355 void SetGrappleHookBindings()
357 // this function has been modified for Nexuiz
358 // don't remove these lines! old server or demos coud overwrite the new aliases
359 stuffcmd(self, "alias +hook +button6\n");
360 stuffcmd(self, "alias -hook -button6\n");