]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
particles: volume weighting = negative count, absolute weighting = positive count
[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, v0, dv;
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                         v0 = self.owner.velocity;
146
147                         // first pull the rope...
148                         newlength = max(newlength - pullspeed * frametime, minlength);
149
150                         if(newlength < dist - ropestretch) // overstretched?
151                         {
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;
155                         }
156
157                         if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
158                                 self.rope_length = newlength;
159
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;
164
165                         dv = ((self.owner.velocity - v0) * dir) * dir;
166                         if(cvar("g_grappling_hook_tarzan") >= 2)
167                         {
168                                 if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.movetype == MOVETYPE_TOSS)
169                                 {
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);
173                                         self.aiment.pusher = self.owner;
174                                         self.aiment.pushltime = time + cvar("g_maxpushtime");
175                                 }
176                         }
177                 }
178                 else
179                 {
180                         end = self.origin - dir*50;
181                         dist = vlen(end - org);
182                         if(dist < 200)
183                                 spd = dist * (pullspeed / 200);
184                         else
185                                 spd = pullspeed;
186                         if(spd < 50)
187                                 spd = 0;
188                         self.owner.velocity = dir*spd;
189                         self.owner.movetype = MOVETYPE_FLY;
190                 }
191
192                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
193
194                 org = org + dir*50; // get the beam out of the player's eyes
195         }
196
197         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
198         te_beam(self.owner, self.origin + v_forward * (-9), org);
199 }
200
201 void GrapplingHookTouch (void)
202 {
203         if (other == self.owner)
204                 return;
205         // altered for Nexuiz
206         //else if (pointcontents (self.origin) == CONTENT_SKY)
207         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
208         {
209                 RemoveGrapplingHook(self.owner);
210                 return;
211         }
212
213         if(other == world)
214         {
215                 vector tic;
216                 tic = self.velocity * sys_ticrate;
217                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
218                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
219                 if(trace_fraction >= 1)
220                 {
221                         dprint("Odd... did not hit...?\n");
222                 }
223                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
224                 {
225                         dprint("Detected and prevented the sky-grapple bug.\n");
226                         RemoveGrapplingHook(self.owner);
227                         return;
228                 }
229         }
230
231         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
232         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
233
234         self.state = 1;
235         self.think = GrapplingHookThink;
236         self.nextthink = time;
237         self.touch = SUB_Null;
238         self.velocity = '0 0 0';
239         self.movetype = MOVETYPE_NONE;
240         self.rope_length = -1;
241
242         if(other)
243                 if(other.movetype != MOVETYPE_NONE)
244                         SetMovetypeFollow(self, other);
245
246         //self.owner.disableclientprediction = TRUE;
247 }
248
249 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
250 {
251         if(self.health > 0)
252         {
253                 self.health = self.health - damage;
254                 if (self.health <= 0)
255                 {
256                         if(attacker != self.owner)
257                         {
258                                 self.owner.pusher = attacker;
259                                 self.owner.pushltime = time + cvar("g_maxpushtime");
260                         }
261                         RemoveGrapplingHook(self.owner);
262                 }
263         }
264 }
265
266 void FireGrapplingHook (void)
267 {
268         local entity missile;
269         local vector org;
270
271         if((arena_roundbased && time < warmup) || (time < restart_countdown))
272                 return;
273
274         makevectors(self.v_angle);
275
276         sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
277         org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
278         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
279
280         missile = spawn ();
281         missile.owner = self;
282         self.hook = missile;
283         missile.classname = "grapplinghook";
284
285         missile.movetype = MOVETYPE_FLY;
286         missile.solid = SOLID_BBOX;
287
288         setmodel (missile, "models/hook.md3"); // precision set below
289         setsize (missile, '-3 -3 -3', '3 3 3');
290         setorigin (missile, org);
291
292         missile.state = 0; // not latched onto anything
293
294         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
295         W_SetupProjectileVelocity(missile);
296
297         missile.angles = vectoangles (missile.velocity);
298         //missile.glow_color = 250; // 244, 250
299         //missile.glow_size = 120;
300         missile.touch = GrapplingHookTouch;
301         missile.think = GrapplingHookThink;
302         missile.nextthink = time + 0.1;
303
304         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
305
306         missile.health = cvar("g_balance_grapplehook_health");//120
307         missile.event_damage = GrapplingHook_Damage;
308         missile.takedamage = DAMAGE_AIM;
309         missile.damageforcescale = 0;
310 }
311
312 void GrapplingHookFrame()
313 {
314         // this function has been modified for Nexuiz
315         if (self.BUTTON_HOOK && g_grappling_hook)
316         {
317                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
318                         if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
319                                 FireGrapplingHook();
320         }
321         else
322         {
323                 if (self.hook)
324                         RemoveGrapplingHook(self);
325         }
326         self.button6_pressed_before = self.BUTTON_HOOK;
327         /*
328         // if I have no hook or it's not pulling yet, make sure I'm not flying!
329         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
330         {
331                 self.movetype = MOVETYPE_WALK;
332         }
333         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
334         {
335                 // fire hook
336                 FireGrapplingHook();
337                 return;
338         }
339         else if(self.hookimpulse == GRAPHOOK_RELEASE)
340         {
341                 // remove hook, reset movement type
342                 RemoveGrapplingHook(self);
343                 return;
344         }
345         */
346         /*else // make sure the player's movetype is correct
347         {
348                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
349                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
350                 {
351                         self.movetype = MOVETYPE_WALK;
352                 }
353         }*/
354         // note: The hook entity does the actual pulling
355 }
356
357 void SetGrappleHookBindings()
358 {
359         // this function has been modified for Nexuiz
360         // don't remove these lines! old server or demos coud overwrite the new aliases
361         stuffcmd(self, "alias +hook +button6\n");
362         stuffcmd(self, "alias -hook -button6\n");
363 }