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