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