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