]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
Increase the maximum number of mutators
[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
69 void RemoveGrapplingHook(entity pl)
70 {
71         if(pl.hook == world)
72                 return;
73         remove(pl.hook);
74         pl.hook = world;
75         if(pl.movetype == MOVETYPE_FLY)
76                 pl.movetype = MOVETYPE_WALK;
77
78         //pl.disableclientprediction = false;
79 }
80
81 void GrapplingHookReset(void)
82 {SELFPARAM();
83         if(self.realowner.hook == self)
84                 RemoveGrapplingHook(self.owner);
85         else // in any case:
86                 remove(self);
87 }
88
89 void GrapplingHookThink();
90 void GrapplingHook_Stop()
91 {SELFPARAM();
92         Send_Effect(EFFECT_HOOK_IMPACT, self.origin, '0 0 0', 1);
93         sound (self, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
94
95         self.state = 1;
96         self.think = GrapplingHookThink;
97         self.nextthink = time;
98         self.touch = func_null;
99         self.velocity = '0 0 0';
100         self.movetype = MOVETYPE_NONE;
101         self.hook_length = -1;
102 }
103
104 .vector hook_start, hook_end;
105 bool GrapplingHookSend(entity this, entity to, int sf)
106 {
107         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
108         sf = sf & 0x7F;
109         if(sound_allowed(MSG_BROADCAST, self.realowner))
110                 sf |= 0x80;
111         WriteByte(MSG_ENTITY, sf);
112         if(sf & 1)
113         {
114                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
115         }
116         if(sf & 2)
117         {
118                 WriteCoord(MSG_ENTITY, self.hook_start.x);
119                 WriteCoord(MSG_ENTITY, self.hook_start.y);
120                 WriteCoord(MSG_ENTITY, self.hook_start.z);
121         }
122         if(sf & 4)
123         {
124                 WriteCoord(MSG_ENTITY, self.hook_end.x);
125                 WriteCoord(MSG_ENTITY, self.hook_end.y);
126                 WriteCoord(MSG_ENTITY, self.hook_end.z);
127         }
128         return true;
129 }
130
131 int autocvar_g_grappling_hook_tarzan;
132
133 void GrapplingHookThink()
134 {SELFPARAM();
135         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
136         vector dir, org, end, v0, dv, v, myorg, vs;
137         if(self.realowner.hook != self) // how did that happen?
138         {
139                 error("Owner lost the hook!\n");
140                 return;
141         }
142         if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((self.aiment.flags & FL_PROJECTILE) && self.aiment.classname != "nade"))
143         {
144                 RemoveGrapplingHook(self.realowner);
145                 return;
146         }
147         if(self.aiment)
148                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
149
150         self.nextthink = time;
151
152         int s = self.realowner.cvar_cl_gunalign;
153         if(s != 1 && s != 2 && s != 4)
154                 s = 3; // default value
155         --s;
156         vs = hook_shotorigin[s];
157
158         makevectors(self.realowner.v_angle);
159         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
160         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
161
162         if(self.hook_length < 0)
163                 self.hook_length = vlen(myorg - self.origin);
164
165         int tarzan = autocvar_g_grappling_hook_tarzan;
166         entity pull_entity = self.realowner;
167         float velocity_multiplier = 1;
168         MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
169         tarzan = hook_tarzan;
170         pull_entity = hook_pullentity;
171         velocity_multiplier = hook_velmultiplier;
172
173         if(self.state == 1)
174         {
175                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
176                 // speed the rope is pulled with
177
178                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
179                 // force the rope will use if it is stretched
180
181                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
182                 // force the rope will use if it is stretched
183
184                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
185                 // minimal rope length
186                 // if the rope goes below this length, it isn't pulled any more
187
188                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
189                 // if the rope is stretched by more than this amount, more rope is
190                 // given to you again
191
192                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
193                 // while hanging on the rope, this friction component will help you a
194                 // bit to control the rope
195
196                 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
197
198                 dir = self.origin - myorg;
199                 dist = vlen(dir);
200                 dir = normalize(dir);
201
202                 if(tarzan)
203                 {
204                         v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
205
206                         // first pull the rope...
207                         if(self.realowner.hook_state & HOOK_PULLING)
208                         {
209                                 newlength = self.hook_length;
210                                 newlength = max(newlength - pullspeed * frametime, minlength);
211
212                                 if(newlength < dist - ropestretch) // overstretched?
213                                 {
214                                         newlength = dist - ropestretch;
215                                         if(v * dir < 0) // only if not already moving in hook direction
216                                                 v = v + frametime * dir * rubberforce_overstretch;
217                                 }
218
219                                 self.hook_length = newlength;
220                         }
221
222                         if(pull_entity.movetype == MOVETYPE_FLY)
223                                 pull_entity.movetype = MOVETYPE_WALK;
224
225                         if(self.realowner.hook_state & HOOK_RELEASING)
226                         {
227                                 newlength = dist;
228                                 self.hook_length = newlength;
229                         }
230                         else
231                         {
232                                 // then pull the player
233                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
234                                 v = v * (1 - frametime * ropeairfriction);
235                                 v = v + frametime * dir * spd * rubberforce;
236
237                                 dv = ((v - v0) * dir) * dir;
238                                 if(tarzan >= 2)
239                                 {
240                                         if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
241                                         {
242                                                 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
243                                                 v = v - dv * 0.5;
244                                                 if((frozen_pulling && self.aiment.frozen) || !frozen_pulling)
245                                                 {
246                                                         self.aiment.velocity = self.aiment.velocity - dv * 0.5;
247                                                         self.aiment.flags &= ~FL_ONGROUND;
248                                                         if(self.aiment.flags & FL_PROJECTILE)
249                                                                 UpdateCSQCProjectile(self.aiment);
250                                                 }
251                                                 if(self.aiment.classname == "nade")
252                                                         self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
253                                                 aim_ent.pusher = self.realowner;
254                                                 aim_ent.pushltime = time + autocvar_g_maxpushtime;
255                                                 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
256                                         }
257                                 }
258
259                                 pull_entity.flags &= ~FL_ONGROUND;
260                         }
261
262                         if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
263                                 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
264
265                         if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !self.aiment.frozen)
266                         {
267                                 RemoveGrapplingHook(self.realowner);
268                                 return;
269                         }
270                 }
271                 else
272                 {
273                         end = self.origin - dir*50;
274                         dist = vlen(end - myorg);
275                         if(dist < 200)
276                                 spd = dist * (pullspeed / 200);
277                         else
278                                 spd = pullspeed;
279                         if(spd < 50)
280                                 spd = 0;
281                         self.realowner.velocity = dir*spd;
282                         self.realowner.movetype = MOVETYPE_FLY;
283
284                         self.realowner.flags &= ~FL_ONGROUND;
285                 }
286         }
287
288         makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
289         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
290
291         if(myorg != self.hook_start)
292         {
293                 self.SendFlags |= 2;
294                 self.hook_start = myorg;
295         }
296         if(org != self.hook_end)
297         {
298                 self.SendFlags |= 4;
299                 self.hook_end = org;
300         }
301 }
302
303 void GrapplingHookTouch (void)
304 {SELFPARAM();
305         if(other.movetype == MOVETYPE_FOLLOW)
306                 return;
307         PROJECTILE_TOUCH;
308
309         GrapplingHook_Stop();
310
311         if(other)
312                 if(other.movetype != MOVETYPE_NONE)
313                 {
314                         SetMovetypeFollow(self, other);
315                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
316                 }
317
318         //self.realowner.disableclientprediction = true;
319 }
320
321 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
322 {SELFPARAM();
323         if(self.health <= 0)
324                 return;
325
326         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
327                 return; // g_balance_projectiledamage says to halt
328
329         self.health = self.health - damage;
330
331         if (self.health <= 0)
332         {
333                 if(attacker != self.realowner)
334                 {
335                         self.realowner.pusher = attacker;
336                         self.realowner.pushltime = time + autocvar_g_maxpushtime;
337                         self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
338                 }
339                 RemoveGrapplingHook(self.realowner);
340         }
341 }
342
343 void FireGrapplingHook (void)
344 {SELFPARAM();
345         entity missile;
346         vector org;
347         vector vs;
348
349         if(forbidWeaponUse(self)) return;
350         if(self.vehicle) return;
351
352         makevectors(self.v_angle);
353
354         int s = self.cvar_cl_gunalign;
355         if(s != 1 && s != 2 && s != 4)
356                 s = 3; // default value
357         --s;
358         vs = hook_shotorigin[s];
359
360         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
361         sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
362         org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
363
364         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
365         org = trace_endpos;
366
367         Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
368
369         missile = WarpZone_RefSys_SpawnSameRefSys(self);
370         missile.owner = missile.realowner = self;
371         self.hook = missile;
372         missile.reset = GrapplingHookReset;
373         missile.classname = "grapplinghook";
374         missile.flags = FL_PROJECTILE;
375
376         missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
377         PROJECTILE_MAKETRIGGER(missile);
378
379         //setmodel (missile, MDL_HOOK); // precision set below
380         setsize (missile, '-3 -3 -3', '3 3 3');
381         setorigin (missile, org);
382
383         missile.state = 0; // not latched onto anything
384
385         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
386
387         missile.angles = vectoangles (missile.velocity);
388         //missile.glow_color = 250; // 244, 250
389         //missile.glow_size = 120;
390         missile.touch = GrapplingHookTouch;
391         missile.think = GrapplingHookThink;
392         missile.nextthink = time;
393
394         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
395
396         missile.health = autocvar_g_balance_grapplehook_health;//120
397         missile.event_damage = GrapplingHook_Damage;
398         missile.takedamage = DAMAGE_AIM;
399         missile.damageforcescale = 0;
400         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
401
402         missile.hook_start = missile.hook_end = missile.origin;
403
404         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
405 }
406
407 void GrappleHookInit()
408 {
409         if(g_grappling_hook)
410         {
411                 hook_shotorigin[0] = '8 8 -12';
412                 hook_shotorigin[1] = '8 8 -12';
413                 hook_shotorigin[2] = '8 8 -12';
414                 hook_shotorigin[3] = '8 8 -12';
415         }
416         else
417         {
418                 Weapon w = WEP_HOOK;
419                 w.wr_init(w);
420                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
421                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
422                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
423                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
424         }
425 }
426
427 void SetGrappleHookBindings()
428 {SELFPARAM();
429         // this function has been modified for Xonotic
430         // don't remove these lines! old server or demos coud overwrite the new aliases
431         stuffcmd(self, "alias +hook +button6\n");
432         stuffcmd(self, "alias -hook -button6\n");
433 }