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