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