]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
Merge branch 'master' into terencehill/physics_panel_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_hook.qc
1 /*============================================
2
3       Wazat's Xonotic 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 .float hook_length;
51 .float hook_switchweapon;
52
53 void RemoveGrapplingHook(entity pl)
54 {
55         if(pl.hook == world)
56                 return;
57         remove(pl.hook);
58         pl.hook = world;
59         if(pl.movetype == MOVETYPE_FLY)
60                 pl.movetype = MOVETYPE_WALK;
61
62         //pl.disableclientprediction = FALSE;
63 }
64
65 void GrapplingHookReset(void)
66 {
67         if(self.realowner.hook == self)
68                 RemoveGrapplingHook(self.owner);
69         else // in any case:
70                 remove(self);
71 }
72
73 void GrapplingHookThink();
74 void GrapplingHook_Stop()
75 {
76         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
77         sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
78
79         self.state = 1;
80         self.think = GrapplingHookThink;
81         self.nextthink = time;
82         self.touch = SUB_Null;
83         self.velocity = '0 0 0';
84         self.movetype = MOVETYPE_NONE;
85         self.hook_length = -1;
86 }
87
88 .vector hook_start, hook_end;
89 float GrapplingHookSend(entity to, float sf)
90 {
91         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
92         sf = sf & 0x7F;
93         if(sound_allowed(MSG_BROADCAST, self.realowner))
94                 sf |= 0x80;
95         WriteByte(MSG_ENTITY, sf);
96         if(sf & 1)
97         {
98                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
99         }
100         if(sf & 2)
101         {
102                 WriteCoord(MSG_ENTITY, self.hook_start_x);
103                 WriteCoord(MSG_ENTITY, self.hook_start_y);
104                 WriteCoord(MSG_ENTITY, self.hook_start_z);
105         }
106         if(sf & 4)
107         {
108                 WriteCoord(MSG_ENTITY, self.hook_end_x);
109                 WriteCoord(MSG_ENTITY, self.hook_end_y);
110                 WriteCoord(MSG_ENTITY, self.hook_end_z);
111         }
112         return TRUE;
113 }
114
115 void GrapplingHookThink()
116 {
117         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
118         vector dir, org, end, v0, dv, v, myorg, vs;
119         if(self.realowner.health <= 0 || self.realowner.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.realowner);
127                 return;
128         }
129         if(self.aiment)
130                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
131
132         self.nextthink = time;
133
134         s = self.realowner.cvar_cl_gunalign;
135         if(s != 1 && s != 2 && s != 4)
136                 s = 3; // default value
137         --s;
138         vs = hook_shotorigin[s];
139
140         makevectors(self.realowner.v_angle);
141         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
142         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
143
144         if(self.hook_length < 0)
145                 self.hook_length = vlen(myorg - self.origin);
146
147         if(self.state == 1)
148         {
149                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
150                 // speed the rope is pulled with
151
152                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
153                 // force the rope will use if it is stretched
154
155                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
156                 // force the rope will use if it is stretched
157
158                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
159                 // minimal rope length
160                 // if the rope goes below this length, it isn't pulled any more
161
162                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
163                 // if the rope is stretched by more than this amount, more rope is
164                 // given to you again
165
166                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
167                 // while hanging on the rope, this friction component will help you a
168                 // bit to control the rope
169
170                 dir = self.origin - myorg;
171                 dist = vlen(dir);
172                 dir = normalize(dir);
173
174                 if(autocvar_g_grappling_hook_tarzan)
175                 {
176                         v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
177
178                         // first pull the rope...
179                         if(self.realowner.hook_state & HOOK_PULLING)
180                         {
181                                 newlength = self.hook_length;
182                                 newlength = max(newlength - pullspeed * frametime, minlength);
183
184                                 if(newlength < dist - ropestretch) // overstretched?
185                                 {
186                                         newlength = dist - ropestretch;
187                                         if(v * dir < 0) // only if not already moving in hook direction
188                                                 v = v + frametime * dir * rubberforce_overstretch;
189                                 }
190
191                                 self.hook_length = newlength;
192                         }
193
194                         if(self.realowner.hook_state & HOOK_RELEASING)
195                         {
196                                 newlength = dist;
197                                 self.hook_length = newlength;
198                         }
199                         else
200                         {
201                                 // then pull the player
202                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
203                                 v = v * (1 - frametime * ropeairfriction);
204                                 v = v + frametime * dir * spd * rubberforce;
205
206                                 dv = ((v - v0) * dir) * dir;
207                                 if(autocvar_g_grappling_hook_tarzan >= 2)
208                                 {
209                                         if(self.aiment.movetype == MOVETYPE_WALK)
210                                         {
211                                                 v = v - dv * 0.5;
212                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
213                                                 self.aiment.flags &~= FL_ONGROUND;
214                                                 self.aiment.pusher = self.realowner;
215                                                 self.aiment.pushltime = time + autocvar_g_maxpushtime;
216                                         }
217                                 }
218
219                                 self.realowner.flags &~= FL_ONGROUND;
220                         }
221
222                         self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
223                 }
224                 else
225                 {
226                         end = self.origin - dir*50;
227                         dist = vlen(end - myorg);
228                         if(dist < 200)
229                                 spd = dist * (pullspeed / 200);
230                         else
231                                 spd = pullspeed;
232                         if(spd < 50)
233                                 spd = 0;
234                         self.realowner.velocity = dir*spd;
235                         self.realowner.movetype = MOVETYPE_FLY;
236
237                         self.realowner.flags &~= FL_ONGROUND;
238                 }
239         }
240
241         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
242         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
243
244         if(myorg != self.hook_start)
245         {
246                 self.SendFlags |= 2;
247                 self.hook_start = myorg;
248         }
249         if(org != self.hook_end)
250         {
251                 self.SendFlags |= 4;
252                 self.hook_end = org;
253         }
254 }
255
256 void GrapplingHookTouch (void)
257 {
258         PROJECTILE_TOUCH;
259
260         GrapplingHook_Stop();
261
262         if(other)
263                 if(other.movetype != MOVETYPE_NONE)
264                 {
265                         SetMovetypeFollow(self, other);
266                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
267                 }
268
269         //self.realowner.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.realowner)
280                         {
281                                 self.realowner.pusher = attacker;
282                                 self.realowner.pushltime = time + autocvar_g_maxpushtime;
283                         }
284                         RemoveGrapplingHook(self.realowner);
285                 }
286         }
287 }
288
289 void FireGrapplingHook (void)
290 {
291         local entity missile;
292         local vector org;
293         float s;
294         vector vs;
295
296         if((arena_roundbased && time < warmup) || (time < game_starttime))
297                 return;
298
299   if(self.freezetag_frozen)
300                 return;
301         
302         if(self.vehicle)
303                 return;
304
305         makevectors(self.v_angle);
306
307         s = self.cvar_cl_gunalign;
308         if(s != 1 && s != 2 && s != 4)
309                 s = 3; // default value
310         --s;
311         vs = hook_shotorigin[s];
312
313         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
314         sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
315         org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
316
317         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
318         org = trace_endpos;
319
320         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
321
322         missile = WarpZone_RefSys_SpawnSameRefSys(self);
323         missile.owner = missile.realowner = self;
324         self.hook = missile;
325         missile.reset = GrapplingHookReset;
326         missile.classname = "grapplinghook";
327
328         missile.movetype = MOVETYPE_FLY;
329         PROJECTILE_MAKETRIGGER(missile);
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         W_SetupProjectileVelocityEx(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
338
339         missile.angles = vectoangles (missile.velocity);
340         //missile.glow_color = 250; // 244, 250
341         //missile.glow_size = 120;
342         missile.touch = GrapplingHookTouch;
343         missile.think = GrapplingHookThink;
344         missile.nextthink = time;
345
346         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
347
348         missile.health = autocvar_g_balance_grapplehook_health;//120
349         missile.event_damage = GrapplingHook_Damage;
350         missile.takedamage = DAMAGE_AIM;
351         missile.damageforcescale = 0;
352
353         missile.hook_start = missile.hook_end = missile.origin;
354
355         Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
356 }
357
358 //  void GrapplingHookFrame()
359 //  {
360 //         // this function has been modified for Xonotic
361 // -       if (self.BUTTON_HOOK && g_grappling_hook)
362 //         {
363 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
364 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
365 // -                               FireGrapplingHook();
366 //         }
367 // -       else
368 //         {
369 //                 if (self.hook)
370 //                         RemoveGrapplingHook(self);
371 //         }
372 // -       self.button6_pressed_before = self.BUTTON_HOOK;
373 //         /*
374 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
375 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
376
377 void GrapplingHookFrame()
378 {
379         if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
380         {
381                 // offhand hook controls
382                 if(self.BUTTON_HOOK)
383                 {
384                         if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
385                         {
386                                 self.hook_state |= HOOK_FIRING;
387                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
388                         }
389                 }
390                 else
391                 {
392                         self.hook_state |= HOOK_REMOVING;
393                         self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
394                 }
395
396                 self.hook_state &~= HOOK_RELEASING;
397                 if(self.BUTTON_CROUCH)
398                 {
399                         self.hook_state &~= HOOK_PULLING;
400                         //self.hook_state |= HOOK_RELEASING;
401                 }
402                 else
403                 {
404                         self.hook_state |= HOOK_PULLING;
405                         //self.hook_state &~= HOOK_RELEASING;
406                 }
407         }
408         else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
409         {
410                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
411                         W_SwitchWeapon(WEP_HOOK);
412         }
413         self.hook_switchweapon = self.BUTTON_HOOK;
414
415         if(!g_grappling_hook && self.weapon != WEP_HOOK)
416         {
417                 self.hook_state &~= HOOK_FIRING;
418                 self.hook_state |= HOOK_REMOVING;
419         }
420
421         if (self.hook_state & HOOK_FIRING)
422         {
423                 if (self.hook)
424                         RemoveGrapplingHook(self);
425                 FireGrapplingHook();
426                 self.hook_state &~= HOOK_FIRING;
427         }
428         else if(self.hook_state & HOOK_REMOVING)
429         {
430                 if (self.hook)
431                         RemoveGrapplingHook(self);
432                 self.hook_state &~= HOOK_REMOVING;
433         }
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         {
469                 hook_shotorigin[0] = '8 8 -12';
470                 hook_shotorigin[1] = '8 8 -12';
471                 hook_shotorigin[2] = '8 8 -12';
472                 hook_shotorigin[3] = '8 8 -12';
473         }
474         else
475         {
476                 weapon_action(WEP_HOOK, WR_PRECACHE);
477                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
478                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
479                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
480                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
481         }
482 }
483
484 void SetGrappleHookBindings()
485 {
486         // this function has been modified for Xonotic
487         // don't remove these lines! old server or demos coud overwrite the new aliases
488         stuffcmd(self, "alias +hook +button6\n");
489         stuffcmd(self, "alias -hook -button6\n");
490 }