]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
hook: also detach when the aiment's deadflag changes (to prevent hook from staying...
[divverent/nexuiz.git] / data / qcsrc / server / g_hook.qc
1 /*============================================
2
3       Wazat's Nexuiz Grappling Hook
4
5         Contact: Wazat1@gmail.com
6
7
8 Installation instructions:
9 --------------------------
10
11 1. Place hook.c in your gamec source directory with the other source files.
12
13 2. Add this line to the bottom of progs.src:
14
15 gamec/hook.c
16
17 3. Open defs.h and add these lines to the very bottom:
18
19 // Wazat's grappling hook
20 .entity         hook;
21 void GrapplingHookFrame();
22 void RemoveGrapplingHook(entity pl);
23 void SetGrappleHookBindings();
24 // hook impulses
25 float GRAPHOOK_FIRE             = 20;
26 float GRAPHOOK_RELEASE          = 21;
27 // (note: you can change the hook impulse #'s to whatever you please)
28
29 4. Open client.c and add this to the top of PutClientInServer():
30
31         RemoveGrapplingHook(self); // Wazat's Grappling Hook
32
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
34
35         // Wazat's grappling hook
36         SetGrappleHookBindings();
37
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
39
40         GrapplingHookFrame();
41
42 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
43 bind ctrl "+hook"
44
45 And you should be done!
46
47
48 ============================================*/
49
50 .string aiment_classname;
51 .float aiment_deadflag;
52 void SetMovetypeFollow(entity ent, entity e)
53 {
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;
62 }
63 float LostMovetypeFollow(entity ent)
64 {
65         if(ent.aiment)
66         {
67                 if(ent.aiment.classname != ent.aiment_classname)
68                         return 1;
69                 if(ent.aiment.deadflag != ent.aiment_deadflag)
70                         return 1;
71         }
72         return 0;
73 }
74
75 .float rope_length;
76 .float button6_pressed_before;
77
78 void RemoveGrapplingHook(entity pl)
79 {
80         if(pl.hook == world)
81                 return;
82         remove(pl.hook);
83         pl.hook = world;
84         if(pl.movetype == MOVETYPE_FLY)
85                 pl.movetype = MOVETYPE_WALK;
86
87         pl.hook_time = time + 0.0;
88
89         //pl.disableclientprediction = FALSE;
90 }
91
92 void GrapplingHookThink()
93 {
94         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
95         vector dir, org, end;
96         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
97         {                                                                                                               // well, better fix it anyway
98                 remove(self);
99                 return;
100         }
101         if(LostMovetypeFollow(self))
102         {
103                 RemoveGrapplingHook(self.owner);
104                 return;
105         }
106
107         self.nextthink = time;
108
109         makevectors(self.owner.v_angle);
110         org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
111
112         if(self.rope_length < 0)
113                 self.rope_length = vlen(org - self.origin);
114
115         if(self.state == 1)
116         {
117                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
118                 // speed the rope is pulled with
119
120                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
121                 // force the rope will use if it is stretched
122
123                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
124                 // force the rope will use if it is stretched
125
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
129
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
133
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
137
138                 dir = self.origin - org;
139                 dist = vlen(dir);
140                 dir = normalize(dir);
141
142                 if(cvar("g_grappling_hook_tarzan"))
143                 {
144                         newlength = self.rope_length;
145
146                         // first pull the rope...
147                         newlength = max(newlength - pullspeed * frametime, minlength);
148
149                         if(newlength < dist - ropestretch) // overstretched?
150                         {
151                                 newlength = dist - ropestretch;
152                                 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
153                                         self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
154                         }
155
156                         if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
157                                 self.rope_length = newlength;
158
159                         // then pull the player
160                         spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
161                         self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
162                         self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
163                 }
164                 else
165                 {
166                         end = self.origin - dir*50;
167                         dist = vlen(end - org);
168                         if(dist < 200)
169                                 spd = dist * (pullspeed / 200);
170                         else
171                                 spd = pullspeed;
172                         if(spd < 50)
173                                 spd = 0;
174                         self.owner.velocity = dir*spd;
175                         self.owner.movetype = MOVETYPE_FLY;
176                 }
177
178                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
179
180                 org = org + dir*50; // get the beam out of the player's eyes
181         }
182
183         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
184         te_beam(self, self.origin + v_forward * (-9), org);
185 }
186
187 void GrapplingHookTouch (void)
188 {
189         if (other == self.owner)
190                 return;
191         // altered for Nexuiz
192         //else if (pointcontents (self.origin) == CONTENT_SKY)
193         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
194         {
195                 RemoveGrapplingHook(self.owner);
196                 return;
197         }
198
199         if(other == world)
200         {
201                 vector tic;
202                 tic = self.velocity * sys_ticrate;
203                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
204                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
205                 if(trace_fraction >= 1)
206                 {
207                         dprint("Odd... did not hit...?\n");
208                 }
209                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
210                 {
211                         dprint("Detected and prevented the sky-grapple bug.\n");
212                         RemoveGrapplingHook(self.owner);
213                         return;
214                 }
215         }
216
217         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
218         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
219
220         self.state = 1;
221         self.think = GrapplingHookThink;
222         self.nextthink = time;
223         self.touch = SUB_Null;
224         self.velocity = '0 0 0';
225         self.movetype = MOVETYPE_NONE;
226         self.rope_length = -1;
227
228         if(other)
229                 if(other.movetype != MOVETYPE_NONE)
230                         SetMovetypeFollow(self, other);
231
232         //self.owner.disableclientprediction = TRUE;
233 }
234
235 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
236 {
237         if(self.health > 0)
238         {
239                 self.health = self.health - damage;
240                 if (self.health <= 0)
241                 {
242                         if(attacker != self.owner)
243                         {
244                                 self.owner.pusher = attacker;
245                                 self.owner.pushltime = time + cvar("g_maxpushtime");
246                         }
247                         RemoveGrapplingHook(self.owner);
248                 }
249         }
250 }
251
252 void FireGrapplingHook (void)
253 {
254         local entity missile;
255         local vector org;
256
257         if((arena_roundbased && time < warmup) || (time < restart_countdown))
258                 return;
259
260         makevectors(self.v_angle);
261
262         sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
263         org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
264         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
265
266         missile = spawn ();
267         missile.owner = self;
268         self.hook = missile;
269         missile.classname = "grapplinghook";
270
271         missile.movetype = MOVETYPE_FLY;
272         missile.solid = SOLID_BBOX;
273
274         setmodel (missile, "models/hook.md3"); // precision set below
275         setsize (missile, '-3 -3 -3', '3 3 3');
276         setorigin (missile, org);
277
278         missile.state = 0; // not latched onto anything
279
280         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
281         W_SetupProjectileVelocity(missile);
282
283         missile.angles = vectoangles (missile.velocity);
284         //missile.glow_color = 250; // 244, 250
285         //missile.glow_size = 120;
286         missile.touch = GrapplingHookTouch;
287         missile.think = GrapplingHookThink;
288         missile.nextthink = time + 0.1;
289
290         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
291
292         missile.health = cvar("g_balance_grapplehook_health");//120
293         missile.event_damage = GrapplingHook_Damage;
294         missile.takedamage = DAMAGE_AIM;
295         missile.damageforcescale = 0;
296 }
297
298 void GrapplingHookFrame()
299 {
300         // this function has been modified for Nexuiz
301         if (self.BUTTON_HOOK && g_grappling_hook)
302         {
303                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
304                         if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
305                                 FireGrapplingHook();
306         }
307         else
308         {
309                 if (self.hook)
310                         RemoveGrapplingHook(self);
311         }
312         self.button6_pressed_before = self.BUTTON_HOOK;
313         /*
314         // if I have no hook or it's not pulling yet, make sure I'm not flying!
315         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
316         {
317                 self.movetype = MOVETYPE_WALK;
318         }
319         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
320         {
321                 // fire hook
322                 FireGrapplingHook();
323                 return;
324         }
325         else if(self.hookimpulse == GRAPHOOK_RELEASE)
326         {
327                 // remove hook, reset movement type
328                 RemoveGrapplingHook(self);
329                 return;
330         }
331         */
332         /*else // make sure the player's movetype is correct
333         {
334                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
335                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
336                 {
337                         self.movetype = MOVETYPE_WALK;
338                 }
339         }*/
340         // note: The hook entity does the actual pulling
341 }
342
343 void SetGrappleHookBindings()
344 {
345         // this function has been modified for Nexuiz
346         // don't remove these lines! old server or demos coud overwrite the new aliases
347         stuffcmd(self, "alias +hook +button6\n");
348         stuffcmd(self, "alias -hook -button6\n");
349 }