]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
hook: new "tarzan" mode that can pull players "symmetrically"
[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                                 }
174                         }
175                 }
176                 else
177                 {
178                         end = self.origin - dir*50;
179                         dist = vlen(end - org);
180                         if(dist < 200)
181                                 spd = dist * (pullspeed / 200);
182                         else
183                                 spd = pullspeed;
184                         if(spd < 50)
185                                 spd = 0;
186                         self.owner.velocity = dir*spd;
187                         self.owner.movetype = MOVETYPE_FLY;
188                 }
189
190                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
191
192                 org = org + dir*50; // get the beam out of the player's eyes
193         }
194
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);
197 }
198
199 void GrapplingHookTouch (void)
200 {
201         if (other == self.owner)
202                 return;
203         // altered for Nexuiz
204         //else if (pointcontents (self.origin) == CONTENT_SKY)
205         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
206         {
207                 RemoveGrapplingHook(self.owner);
208                 return;
209         }
210
211         if(other == world)
212         {
213                 vector tic;
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)
218                 {
219                         dprint("Odd... did not hit...?\n");
220                 }
221                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
222                 {
223                         dprint("Detected and prevented the sky-grapple bug.\n");
224                         RemoveGrapplingHook(self.owner);
225                         return;
226                 }
227         }
228
229         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
230         sound (self, CHAN_PROJECTILE, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
231
232         self.state = 1;
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;
239
240         if(other)
241                 if(other.movetype != MOVETYPE_NONE)
242                         SetMovetypeFollow(self, other);
243
244         //self.owner.disableclientprediction = TRUE;
245 }
246
247 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
248 {
249         if(self.health > 0)
250         {
251                 self.health = self.health - damage;
252                 if (self.health <= 0)
253                 {
254                         if(attacker != self.owner)
255                         {
256                                 self.owner.pusher = attacker;
257                                 self.owner.pushltime = time + cvar("g_maxpushtime");
258                         }
259                         RemoveGrapplingHook(self.owner);
260                 }
261         }
262 }
263
264 void FireGrapplingHook (void)
265 {
266         local entity missile;
267         local vector org;
268
269         if((arena_roundbased && time < warmup) || (time < restart_countdown))
270                 return;
271
272         makevectors(self.v_angle);
273
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);
277
278         missile = spawn ();
279         missile.owner = self;
280         self.hook = missile;
281         missile.classname = "grapplinghook";
282
283         missile.movetype = MOVETYPE_FLY;
284         missile.solid = SOLID_BBOX;
285
286         setmodel (missile, "models/hook.md3"); // precision set below
287         setsize (missile, '-3 -3 -3', '3 3 3');
288         setorigin (missile, org);
289
290         missile.state = 0; // not latched onto anything
291
292         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
293         W_SetupProjectileVelocity(missile);
294
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;
301
302         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
303
304         missile.health = cvar("g_balance_grapplehook_health");//120
305         missile.event_damage = GrapplingHook_Damage;
306         missile.takedamage = DAMAGE_AIM;
307         missile.damageforcescale = 0;
308 }
309
310 void GrapplingHookFrame()
311 {
312         // this function has been modified for Nexuiz
313         if (self.BUTTON_HOOK && g_grappling_hook)
314         {
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)
317                                 FireGrapplingHook();
318         }
319         else
320         {
321                 if (self.hook)
322                         RemoveGrapplingHook(self);
323         }
324         self.button6_pressed_before = self.BUTTON_HOOK;
325         /*
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)
328         {
329                 self.movetype = MOVETYPE_WALK;
330         }
331         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
332         {
333                 // fire hook
334                 FireGrapplingHook();
335                 return;
336         }
337         else if(self.hookimpulse == GRAPHOOK_RELEASE)
338         {
339                 // remove hook, reset movement type
340                 RemoveGrapplingHook(self);
341                 return;
342         }
343         */
344         /*else // make sure the player's movetype is correct
345         {
346                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
347                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
348                 {
349                         self.movetype = MOVETYPE_WALK;
350                 }
351         }*/
352         // note: The hook entity does the actual pulling
353 }
354
355 void SetGrappleHookBindings()
356 {
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");
361 }