]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
Merge branch 'master' into Melanosuchus/minigames
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_hook.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qh"
5     #include "../dpdefs/dpextensions.qh"
6     #include "../warpzonelib/common.qh"
7     #include "../warpzonelib/server.qh"
8     #include "../common/constants.qh"
9     #include "../common/util.qh"
10     #include "../common/weapons/weapons.qh"
11     #include "autocvars.qh"
12     #include "constants.qh"
13     #include "defs.qh"
14     #include "vehicles/vehicles_def.qh"
15     #include "command/common.qh"
16     #include "g_hook.qh"
17     #include "round_handler.qh"
18 #endif
19
20 /*============================================
21
22       Wazat's Xonotic Grappling Hook
23
24         Contact: Wazat1@gmail.com
25
26
27 Installation instructions:
28 --------------------------
29
30 1. Place hook.c in your gamec source directory with the other source files.
31
32 2. Add this line to the bottom of progs.src:
33
34 gamec/hook.c
35
36 3. Open defs.h and add these lines to the very bottom:
37
38 // Wazat's grappling hook
39 .entity         hook;
40 void GrapplingHookFrame();
41 void RemoveGrapplingHook(entity pl);
42 void SetGrappleHookBindings();
43 // hook impulses
44 const float GRAPHOOK_FIRE               = 20;
45 const float GRAPHOOK_RELEASE            = 21;
46 // (note: you can change the hook impulse #'s to whatever you please)
47
48 4. Open client.c and add this to the top of PutClientInServer():
49
50         RemoveGrapplingHook(self); // Wazat's Grappling Hook
51
52 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
53
54         // Wazat's grappling hook
55         SetGrappleHookBindings();
56
57 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
58
59         GrapplingHookFrame();
60
61 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
62 bind ctrl "+hook"
63
64 And you should be done!
65
66
67 ============================================*/
68
69 .float hook_length;
70 .float hook_switchweapon;
71
72 void RemoveGrapplingHook(entity pl)
73 {
74         if(pl.hook == world)
75                 return;
76         remove(pl.hook);
77         pl.hook = world;
78         if(pl.movetype == MOVETYPE_FLY)
79                 pl.movetype = MOVETYPE_WALK;
80
81         //pl.disableclientprediction = false;
82 }
83
84 void GrapplingHookReset(void)
85 {
86         if(self.realowner.hook == self)
87                 RemoveGrapplingHook(self.owner);
88         else // in any case:
89                 remove(self);
90 }
91
92 void GrapplingHookThink();
93 void GrapplingHook_Stop()
94 {
95         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
96         sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
97
98         self.state = 1;
99         self.think = GrapplingHookThink;
100         self.nextthink = time;
101         self.touch = func_null;
102         self.velocity = '0 0 0';
103         self.movetype = MOVETYPE_NONE;
104         self.hook_length = -1;
105 }
106
107 .vector hook_start, hook_end;
108 float GrapplingHookSend(entity to, int sf)
109 {
110         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
111         sf = sf & 0x7F;
112         if(sound_allowed(MSG_BROADCAST, self.realowner))
113                 sf |= 0x80;
114         WriteByte(MSG_ENTITY, sf);
115         if(sf & 1)
116         {
117                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
118         }
119         if(sf & 2)
120         {
121                 WriteCoord(MSG_ENTITY, self.hook_start.x);
122                 WriteCoord(MSG_ENTITY, self.hook_start.y);
123                 WriteCoord(MSG_ENTITY, self.hook_start.z);
124         }
125         if(sf & 4)
126         {
127                 WriteCoord(MSG_ENTITY, self.hook_end.x);
128                 WriteCoord(MSG_ENTITY, self.hook_end.y);
129                 WriteCoord(MSG_ENTITY, self.hook_end.z);
130         }
131         return true;
132 }
133
134 void GrapplingHookThink()
135 {
136         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch, s;
137         vector dir, org, end, v0, dv, v, myorg, vs;
138         if(self.realowner.hook != self) // how did that happen?
139         {
140                 error("Owner lost the hook!\n");
141                 return;
142         }
143         if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
144         {
145                 RemoveGrapplingHook(self.realowner);
146                 return;
147         }
148         if(self.aiment)
149                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
150
151         self.nextthink = time;
152
153         s = self.realowner.cvar_cl_gunalign;
154         if(s != 1 && s != 2 && s != 4)
155                 s = 3; // default value
156         --s;
157         vs = hook_shotorigin[s];
158
159         makevectors(self.realowner.v_angle);
160         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
161         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
162
163         if(self.hook_length < 0)
164                 self.hook_length = vlen(myorg - self.origin);
165
166         if(self.state == 1)
167         {
168                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
169                 // speed the rope is pulled with
170
171                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
172                 // force the rope will use if it is stretched
173
174                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
175                 // force the rope will use if it is stretched
176
177                 minlength = autocvar_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 = autocvar_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 = autocvar_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 - myorg;
190                 dist = vlen(dir);
191                 dir = normalize(dir);
192
193                 if(autocvar_g_grappling_hook_tarzan)
194                 {
195                         v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
196
197                         // first pull the rope...
198                         if(self.realowner.hook_state & HOOK_PULLING)
199                         {
200                                 newlength = self.hook_length;
201                                 newlength = max(newlength - pullspeed * frametime, minlength);
202
203                                 if(newlength < dist - ropestretch) // overstretched?
204                                 {
205                                         newlength = dist - ropestretch;
206                                         if(v * dir < 0) // only if not already moving in hook direction
207                                                 v = v + frametime * dir * rubberforce_overstretch;
208                                 }
209
210                                 self.hook_length = newlength;
211                         }
212
213                         if(self.realowner.hook_state & HOOK_RELEASING)
214                         {
215                                 newlength = dist;
216                                 self.hook_length = newlength;
217                         }
218                         else
219                         {
220                                 // then pull the player
221                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
222                                 v = v * (1 - frametime * ropeairfriction);
223                                 v = v + frametime * dir * spd * rubberforce;
224
225                                 dv = ((v - v0) * dir) * dir;
226                                 if(autocvar_g_grappling_hook_tarzan >= 2)
227                                 {
228                                         if(self.aiment.movetype == MOVETYPE_WALK)
229                                         {
230                                                 v = v - dv * 0.5;
231                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
232                                                 self.aiment.flags &= ~FL_ONGROUND;
233                                                 self.aiment.pusher = self.realowner;
234                                                 self.aiment.pushltime = time + autocvar_g_maxpushtime;
235                                                 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
236                                         }
237                                 }
238
239                                 self.realowner.flags &= ~FL_ONGROUND;
240                         }
241
242                         self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
243                 }
244                 else
245                 {
246                         end = self.origin - dir*50;
247                         dist = vlen(end - myorg);
248                         if(dist < 200)
249                                 spd = dist * (pullspeed / 200);
250                         else
251                                 spd = pullspeed;
252                         if(spd < 50)
253                                 spd = 0;
254                         self.realowner.velocity = dir*spd;
255                         self.realowner.movetype = MOVETYPE_FLY;
256
257                         self.realowner.flags &= ~FL_ONGROUND;
258                 }
259         }
260
261         makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
262         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
263
264         if(myorg != self.hook_start)
265         {
266                 self.SendFlags |= 2;
267                 self.hook_start = myorg;
268         }
269         if(org != self.hook_end)
270         {
271                 self.SendFlags |= 4;
272                 self.hook_end = org;
273         }
274 }
275
276 void GrapplingHookTouch (void)
277 {
278         PROJECTILE_TOUCH;
279
280         GrapplingHook_Stop();
281
282         if(other)
283                 if(other.movetype != MOVETYPE_NONE)
284                 {
285                         SetMovetypeFollow(self, other);
286                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
287                 }
288
289         //self.realowner.disableclientprediction = true;
290 }
291
292 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
293 {
294         if(self.health <= 0)
295                 return;
296
297         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
298                 return; // g_balance_projectiledamage says to halt
299
300         self.health = self.health - damage;
301
302         if (self.health <= 0)
303         {
304                 if(attacker != self.realowner)
305                 {
306                         self.realowner.pusher = attacker;
307                         self.realowner.pushltime = time + autocvar_g_maxpushtime;
308                         self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
309                 }
310                 RemoveGrapplingHook(self.realowner);
311         }
312 }
313
314 void FireGrapplingHook (void)
315 {
316         entity missile;
317         vector org;
318         float s;
319         vector vs;
320
321         if(forbidWeaponUse()) return;
322         if(self.vehicle) return;
323
324         makevectors(self.v_angle);
325
326         s = self.cvar_cl_gunalign;
327         if(s != 1 && s != 2 && s != 4)
328                 s = 3; // default value
329         --s;
330         vs = hook_shotorigin[s];
331
332         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
333         sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
334         org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
335
336         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
337         org = trace_endpos;
338
339         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
340
341         missile = WarpZone_RefSys_SpawnSameRefSys(self);
342         missile.owner = missile.realowner = self;
343         self.hook = missile;
344         missile.reset = GrapplingHookReset;
345         missile.classname = "grapplinghook";
346         missile.flags = FL_PROJECTILE;
347
348         missile.movetype = MOVETYPE_FLY;
349         PROJECTILE_MAKETRIGGER(missile);
350
351         //setmodel (missile, "models/hook.md3"); // precision set below
352         setsize (missile, '-3 -3 -3', '3 3 3');
353         setorigin (missile, org);
354
355         missile.state = 0; // not latched onto anything
356
357         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
358
359         missile.angles = vectoangles (missile.velocity);
360         //missile.glow_color = 250; // 244, 250
361         //missile.glow_size = 120;
362         missile.touch = GrapplingHookTouch;
363         missile.think = GrapplingHookThink;
364         missile.nextthink = time;
365
366         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
367
368         missile.health = autocvar_g_balance_grapplehook_health;//120
369         missile.event_damage = GrapplingHook_Damage;
370         missile.takedamage = DAMAGE_AIM;
371         missile.damageforcescale = 0;
372         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
373
374         missile.hook_start = missile.hook_end = missile.origin;
375
376         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
377 }
378
379 //  void GrapplingHookFrame()
380 //  {
381 //         // this function has been modified for Xonotic
382 // -       if (self.BUTTON_HOOK && g_grappling_hook)
383 //         {
384 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
385 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
386 // -                               FireGrapplingHook();
387 //         }
388 // -       else
389 //         {
390 //                 if (self.hook)
391 //                         RemoveGrapplingHook(self);
392 //         }
393 // -       self.button6_pressed_before = self.BUTTON_HOOK;
394 //         /*
395 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
396 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
397
398 void GrapplingHookFrame()
399 {
400         if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
401         {
402                 // offhand hook controls
403                 if(self.BUTTON_HOOK)
404                 {
405                         if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
406                         {
407                                 self.hook_state |= HOOK_FIRING;
408                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
409                         }
410                 }
411                 else
412                 {
413                         self.hook_state |= HOOK_REMOVING;
414                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
415                 }
416
417                 self.hook_state &= ~HOOK_RELEASING;
418                 if(self.BUTTON_CROUCH)
419                 {
420                         self.hook_state &= ~HOOK_PULLING;
421                         //self.hook_state |= HOOK_RELEASING;
422                 }
423                 else
424                 {
425                         self.hook_state |= HOOK_PULLING;
426                         //self.hook_state &= ~HOOK_RELEASING;
427                 }
428         }
429         else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
430         {
431                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
432                         W_SwitchWeapon(WEP_HOOK);
433         }
434         self.hook_switchweapon = self.BUTTON_HOOK;
435
436         if(!g_grappling_hook && self.weapon != WEP_HOOK)
437         {
438                 self.hook_state &= ~HOOK_FIRING;
439                 self.hook_state |= HOOK_REMOVING;
440         }
441
442         if (self.hook_state & HOOK_FIRING)
443         {
444                 if (self.hook)
445                         RemoveGrapplingHook(self);
446                 FireGrapplingHook();
447                 self.hook_state &= ~HOOK_FIRING;
448                 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
449         }
450         else if(self.hook_state & HOOK_REMOVING)
451         {
452                 if (self.hook)
453                         RemoveGrapplingHook(self);
454                 self.hook_state &= ~HOOK_REMOVING;
455         }
456
457         /*
458         // if I have no hook or it's not pulling yet, make sure I'm not flying!
459         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
460         {
461                 self.movetype = MOVETYPE_WALK;
462         }
463         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
464         {
465                 // fire hook
466                 FireGrapplingHook();
467                 return;
468         }
469         else if(self.hookimpulse == GRAPHOOK_RELEASE)
470         {
471                 // remove hook, reset movement type
472                 RemoveGrapplingHook(self);
473                 return;
474         }
475         */
476         /*else // make sure the player's movetype is correct
477         {
478                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
479                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
480                 {
481                         self.movetype = MOVETYPE_WALK;
482                 }
483         }*/
484         // note: The hook entity does the actual pulling
485 }
486
487 void GrappleHookInit()
488 {
489         if(g_grappling_hook)
490         {
491                 hook_shotorigin[0] = '8 8 -12';
492                 hook_shotorigin[1] = '8 8 -12';
493                 hook_shotorigin[2] = '8 8 -12';
494                 hook_shotorigin[3] = '8 8 -12';
495         }
496         else
497         {
498                 WEP_ACTION(WEP_HOOK, WR_INIT);
499                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
500                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
501                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
502                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
503         }
504 }
505
506 void SetGrappleHookBindings()
507 {
508         // this function has been modified for Xonotic
509         // don't remove these lines! old server or demos coud overwrite the new aliases
510         stuffcmd(self, "alias +hook +button6\n");
511         stuffcmd(self, "alias -hook -button6\n");
512 }