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 void UnsetMovetypeFollow(entity ent)
65 ent.movetype = MOVETYPE_FLY;
66 ent.solid = SOLID_BBOX;
69 float LostMovetypeFollow(entity ent)
73 if(ent.aiment.classname != ent.aiment_classname)
75 if(ent.aiment.deadflag != ent.aiment_deadflag)
82 .float button6_pressed_before;
84 void RemoveGrapplingHook(entity pl)
90 if(pl.movetype == MOVETYPE_FLY)
91 pl.movetype = MOVETYPE_WALK;
93 pl.hook_time = time + 0.0;
95 //pl.disableclientprediction = FALSE;
98 void GrapplingHookThink();
99 void GrapplingHook_Stop()
101 pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
102 sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
105 self.think = GrapplingHookThink;
106 self.nextthink = time;
107 self.touch = SUB_Null;
108 self.velocity = '0 0 0';
109 self.movetype = MOVETYPE_NONE;
110 self.rope_length = -1;
113 void GrapplingHookThink()
115 float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
116 vector dir, org, end, v0, dv;
117 if(self.owner.health <= 0 || self.owner.hook != self) // how did that happen?
118 { // well, better fix it anyway
122 if(LostMovetypeFollow(self))
124 RemoveGrapplingHook(self.owner);
128 self.nextthink = time;
130 makevectors(self.owner.v_angle);
131 org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
133 tracebox(org, self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self.owner);
134 // do not hit players with this, as they tend to get in the way just too often
135 // NOTE: this assumes sky brushes cannot get in the way
136 // if they can, assume the map is broken! :P
137 if(trace_fraction < 1 && (!self.aiment || trace_ent != self.aiment))
141 GrapplingHook_Stop();
143 // 1. detach the hook
145 UnsetMovetypeFollow(self);
148 self.origin = trace_endpos;
150 // 3. reattach the hook
152 if(trace_ent.movetype != MOVETYPE_NONE)
153 SetMovetypeFollow(self, trace_ent);
156 if(self.rope_length < 0)
157 self.rope_length = vlen(org - self.origin);
161 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
162 // speed the rope is pulled with
164 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
165 // force the rope will use if it is stretched
167 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
168 // force the rope will use if it is stretched
170 minlength = cvar("g_balance_grapplehook_length_min");//100;
171 // minimal rope length
172 // if the rope goes below this length, it isn't pulled any more
174 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
175 // if the rope is stretched by more than this amount, more rope is
176 // given to you again
178 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
179 // while hanging on the rope, this friction component will help you a
180 // bit to control the rope
182 dir = self.origin - org;
184 dir = normalize(dir);
186 if(cvar("g_grappling_hook_tarzan"))
188 newlength = self.rope_length;
189 v0 = self.owner.velocity;
191 // first pull the rope...
192 newlength = max(newlength - pullspeed * frametime, minlength);
194 if(newlength < dist - ropestretch) // overstretched?
196 newlength = dist - ropestretch;
197 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
198 self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
201 if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
202 self.rope_length = newlength;
204 // then pull the player
205 spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
206 self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
207 self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
209 dv = ((self.owner.velocity - v0) * dir) * dir;
210 if(cvar("g_grappling_hook_tarzan") >= 2)
212 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.movetype == MOVETYPE_TOSS)
214 self.owner.velocity = self.owner.velocity - dv * 0.5;
215 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
216 self.aiment.flags = self.aiment.flags - (self.aiment.flags & FL_ONGROUND);
217 self.aiment.pusher = self.owner;
218 self.aiment.pushltime = time + cvar("g_maxpushtime");
224 end = self.origin - dir*50;
225 dist = vlen(end - org);
227 spd = dist * (pullspeed / 200);
232 self.owner.velocity = dir*spd;
233 self.owner.movetype = MOVETYPE_FLY;
236 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
238 org = org + dir*50; // get the beam out of the player's eyes
241 makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
242 te_beam(self.owner, self.origin + v_forward * (-9), org);
245 void GrapplingHookTouch (void)
247 if (other == self.owner)
249 // altered for Nexuiz
250 //else if (pointcontents (self.origin) == CONTENT_SKY)
251 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
253 RemoveGrapplingHook(self.owner);
260 tic = self.velocity * sys_ticrate;
261 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
262 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
263 if(trace_fraction >= 1)
265 dprint("Odd... did not hit...?\n");
267 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
269 dprint("Detected and prevented the sky-grapple bug.\n");
270 RemoveGrapplingHook(self.owner);
276 if(other.movetype != MOVETYPE_NONE)
277 SetMovetypeFollow(self, other);
279 GrapplingHook_Stop();
280 //self.owner.disableclientprediction = TRUE;
283 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
287 self.health = self.health - damage;
288 if (self.health <= 0)
290 if(attacker != self.owner)
292 self.owner.pusher = attacker;
293 self.owner.pushltime = time + cvar("g_maxpushtime");
295 RemoveGrapplingHook(self.owner);
300 void FireGrapplingHook (void)
302 local entity missile;
305 if((arena_roundbased && time < warmup) || (time < restart_countdown))
308 makevectors(self.v_angle);
310 sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
311 org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
312 pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
315 missile.owner = self;
317 missile.classname = "grapplinghook";
319 missile.movetype = MOVETYPE_FLY;
320 missile.solid = SOLID_BBOX;
322 setmodel (missile, "models/hook.md3"); // precision set below
323 setsize (missile, '-3 -3 -3', '3 3 3');
324 setorigin (missile, org);
326 missile.state = 0; // not latched onto anything
328 missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
329 W_SetupProjectileVelocity(missile);
331 missile.angles = vectoangles (missile.velocity);
332 //missile.glow_color = 250; // 244, 250
333 //missile.glow_size = 120;
334 missile.touch = GrapplingHookTouch;
335 missile.think = GrapplingHookThink;
336 missile.nextthink = time + 0.1;
338 missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
340 missile.health = cvar("g_balance_grapplehook_health");//120
341 missile.event_damage = GrapplingHook_Damage;
342 missile.takedamage = DAMAGE_AIM;
343 missile.damageforcescale = 0;
346 void GrapplingHookFrame()
348 // this function has been modified for Nexuiz
349 if (self.BUTTON_HOOK && g_grappling_hook)
351 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
352 if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
358 RemoveGrapplingHook(self);
360 self.button6_pressed_before = self.BUTTON_HOOK;
362 // if I have no hook or it's not pulling yet, make sure I'm not flying!
363 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
365 self.movetype = MOVETYPE_WALK;
367 if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
373 else if(self.hookimpulse == GRAPHOOK_RELEASE)
375 // remove hook, reset movement type
376 RemoveGrapplingHook(self);
380 /*else // make sure the player's movetype is correct
382 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
383 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
385 self.movetype = MOVETYPE_WALK;
388 // note: The hook entity does the actual pulling
391 void SetGrappleHookBindings()
393 // this function has been modified for Nexuiz
394 // don't remove these lines! old server or demos coud overwrite the new aliases
395 stuffcmd(self, "alias +hook +button6\n");
396 stuffcmd(self, "alias -hook -button6\n");