]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
fteqcc should implement DWIM:
[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 void UnsetMovetypeFollow(entity ent)
64 {
65         ent.movetype = MOVETYPE_FLY;
66         ent.solid = SOLID_BBOX;
67         ent.aiment = world;
68 }
69 float LostMovetypeFollow(entity ent)
70 {
71         if(ent.aiment)
72         {
73                 if(ent.aiment.classname != ent.aiment_classname)
74                         return 1;
75                 if(ent.aiment.deadflag != ent.aiment_deadflag)
76                         return 1;
77         }
78         return 0;
79 }
80
81 .float rope_length;
82 .float button6_pressed_before;
83
84 void RemoveGrapplingHook(entity pl)
85 {
86         if(pl.hook == world)
87                 return;
88         remove(pl.hook);
89         pl.hook = world;
90         if(pl.movetype == MOVETYPE_FLY)
91                 pl.movetype = MOVETYPE_WALK;
92
93         pl.hook_time = time + 0.0;
94
95         //pl.disableclientprediction = FALSE;
96 }
97
98 void GrapplingHookThink()
99 {
100         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
101         vector dir, org, end, v0, dv;
102         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
103         {                                                                                                               // well, better fix it anyway
104                 remove(self);
105                 return;
106         }
107         if(LostMovetypeFollow(self))
108         {
109                 RemoveGrapplingHook(self.owner);
110                 return;
111         }
112
113         self.nextthink = time;
114
115         makevectors(self.owner.v_angle);
116         org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
117
118         tracebox(org, self.mins, self.maxs, self.origin, MOVE_NOMONSTERS, self.owner);
119         // do not hit players with this, as they tend to get in the way just too often
120         if(trace_fraction < 1 && (!self.aiment || trace_ent != self.aiment))
121         {
122                 // 1. detach the hook
123                 if(self.aiment)
124                         UnsetMovetypeFollow(self);
125
126                 // 2. cut it off
127                 self.origin = trace_endpos;
128
129                 // 3. reattach the hook
130                 if(trace_ent)
131                         if(trace_ent.movetype != MOVETYPE_NONE)
132                                 SetMovetypeFollow(self, trace_ent);
133         }
134
135         if(self.rope_length < 0)
136                 self.rope_length = vlen(org - self.origin);
137
138         if(self.state == 1)
139         {
140                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
141                 // speed the rope is pulled with
142
143                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
144                 // force the rope will use if it is stretched
145
146                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
147                 // force the rope will use if it is stretched
148
149                 minlength = cvar("g_balance_grapplehook_length_min");//100;
150                 // minimal rope length
151                 // if the rope goes below this length, it isn't pulled any more
152
153                 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
154                 // if the rope is stretched by more than this amount, more rope is
155                 // given to you again
156
157                 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
158                 // while hanging on the rope, this friction component will help you a
159                 // bit to control the rope
160
161                 dir = self.origin - org;
162                 dist = vlen(dir);
163                 dir = normalize(dir);
164
165                 if(cvar("g_grappling_hook_tarzan"))
166                 {
167                         newlength = self.rope_length;
168                         v0 = self.owner.velocity;
169
170                         // first pull the rope...
171                         newlength = max(newlength - pullspeed * frametime, minlength);
172
173                         if(newlength < dist - ropestretch) // overstretched?
174                         {
175                                 newlength = dist - ropestretch;
176                                 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
177                                         self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
178                         }
179
180                         if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
181                                 self.rope_length = newlength;
182
183                         // then pull the player
184                         spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
185                         self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
186                         self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
187
188                         dv = ((self.owner.velocity - v0) * dir) * dir;
189                         if(cvar("g_grappling_hook_tarzan") >= 2)
190                         {
191                                 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.movetype == MOVETYPE_TOSS)
192                                 {
193                                         self.owner.velocity = self.owner.velocity - dv * 0.5;
194                                         self.aiment.velocity = self.aiment.velocity - dv * 0.5;
195                                         self.aiment.flags = self.aiment.flags - (self.aiment.flags & FL_ONGROUND);
196                                         self.aiment.pusher = self.owner;
197                                         self.aiment.pushltime = time + cvar("g_maxpushtime");
198                                 }
199                         }
200                 }
201                 else
202                 {
203                         end = self.origin - dir*50;
204                         dist = vlen(end - org);
205                         if(dist < 200)
206                                 spd = dist * (pullspeed / 200);
207                         else
208                                 spd = pullspeed;
209                         if(spd < 50)
210                                 spd = 0;
211                         self.owner.velocity = dir*spd;
212                         self.owner.movetype = MOVETYPE_FLY;
213                 }
214
215                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
216
217                 org = org + dir*50; // get the beam out of the player's eyes
218         }
219
220         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
221         te_beam(self.owner, self.origin + v_forward * (-9), org);
222 }
223
224 void GrapplingHookTouch (void)
225 {
226         if (other == self.owner)
227                 return;
228         // altered for Nexuiz
229         //else if (pointcontents (self.origin) == CONTENT_SKY)
230         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
231         {
232                 RemoveGrapplingHook(self.owner);
233                 return;
234         }
235
236         if(other == world)
237         {
238                 vector tic;
239                 tic = self.velocity * sys_ticrate;
240                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
241                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
242                 if(trace_fraction >= 1)
243                 {
244                         dprint("Odd... did not hit...?\n");
245                 }
246                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
247                 {
248                         dprint("Detected and prevented the sky-grapple bug.\n");
249                         RemoveGrapplingHook(self.owner);
250                         return;
251                 }
252         }
253
254         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
255         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
256
257         self.state = 1;
258         self.think = GrapplingHookThink;
259         self.nextthink = time;
260         self.touch = SUB_Null;
261         self.velocity = '0 0 0';
262         self.movetype = MOVETYPE_NONE;
263         self.rope_length = -1;
264
265         if(other)
266                 if(other.movetype != MOVETYPE_NONE)
267                         SetMovetypeFollow(self, other);
268
269         //self.owner.disableclientprediction = TRUE;
270 }
271
272 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
273 {
274         if(self.health > 0)
275         {
276                 self.health = self.health - damage;
277                 if (self.health <= 0)
278                 {
279                         if(attacker != self.owner)
280                         {
281                                 self.owner.pusher = attacker;
282                                 self.owner.pushltime = time + cvar("g_maxpushtime");
283                         }
284                         RemoveGrapplingHook(self.owner);
285                 }
286         }
287 }
288
289 void FireGrapplingHook (void)
290 {
291         local entity missile;
292         local vector org;
293
294         if((arena_roundbased && time < warmup) || (time < restart_countdown))
295                 return;
296
297         makevectors(self.v_angle);
298
299         sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
300         org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
301         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
302
303         missile = spawn ();
304         missile.owner = self;
305         self.hook = missile;
306         missile.classname = "grapplinghook";
307
308         missile.movetype = MOVETYPE_FLY;
309         missile.solid = SOLID_BBOX;
310
311         setmodel (missile, "models/hook.md3"); // precision set below
312         setsize (missile, '-3 -3 -3', '3 3 3');
313         setorigin (missile, org);
314
315         missile.state = 0; // not latched onto anything
316
317         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
318         W_SetupProjectileVelocity(missile);
319
320         missile.angles = vectoangles (missile.velocity);
321         //missile.glow_color = 250; // 244, 250
322         //missile.glow_size = 120;
323         missile.touch = GrapplingHookTouch;
324         missile.think = GrapplingHookThink;
325         missile.nextthink = time + 0.1;
326
327         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
328
329         missile.health = cvar("g_balance_grapplehook_health");//120
330         missile.event_damage = GrapplingHook_Damage;
331         missile.takedamage = DAMAGE_AIM;
332         missile.damageforcescale = 0;
333 }
334
335 void GrapplingHookFrame()
336 {
337         // this function has been modified for Nexuiz
338         if (self.BUTTON_HOOK && g_grappling_hook)
339         {
340                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
341                         if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
342                                 FireGrapplingHook();
343         }
344         else
345         {
346                 if (self.hook)
347                         RemoveGrapplingHook(self);
348         }
349         self.button6_pressed_before = self.BUTTON_HOOK;
350         /*
351         // if I have no hook or it's not pulling yet, make sure I'm not flying!
352         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
353         {
354                 self.movetype = MOVETYPE_WALK;
355         }
356         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
357         {
358                 // fire hook
359                 FireGrapplingHook();
360                 return;
361         }
362         else if(self.hookimpulse == GRAPHOOK_RELEASE)
363         {
364                 // remove hook, reset movement type
365                 RemoveGrapplingHook(self);
366                 return;
367         }
368         */
369         /*else // make sure the player's movetype is correct
370         {
371                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
372                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
373                 {
374                         self.movetype = MOVETYPE_WALK;
375                 }
376         }*/
377         // note: The hook entity does the actual pulling
378 }
379
380 void SetGrappleHookBindings()
381 {
382         // this function has been modified for Nexuiz
383         // don't remove these lines! old server or demos coud overwrite the new aliases
384         stuffcmd(self, "alias +hook +button6\n");
385         stuffcmd(self, "alias -hook -button6\n");
386 }