]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
4a32779289d12c54f10da05bb6e357b4c8266a89
[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(entity this)
84 {
85         if(this.realowner.hook == this)
86                 RemoveGrapplingHook(this.owner);
87         else // in any case:
88                 remove(this);
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         WriteHeader(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, etof(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 = W_GetGunAlignment(self.realowner);
155         vs = hook_shotorigin[s];
156
157         makevectors(self.realowner.v_angle);
158         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
159         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
160
161         if(self.hook_length < 0)
162                 self.hook_length = vlen(myorg - self.origin);
163
164         int tarzan = autocvar_g_grappling_hook_tarzan;
165         entity pull_entity = self.realowner;
166         float velocity_multiplier = 1;
167         MUTATOR_CALLHOOK(GrappleHookThink, self, tarzan, pull_entity, velocity_multiplier);
168         tarzan = hook_tarzan;
169         pull_entity = hook_pullentity;
170         velocity_multiplier = hook_velmultiplier;
171
172         if(self.state == 1)
173         {
174                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
175                 // speed the rope is pulled with
176
177                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
178                 // force the rope will use if it is stretched
179
180                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
181                 // force the rope will use if it is stretched
182
183                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
184                 // minimal rope length
185                 // if the rope goes below this length, it isn't pulled any more
186
187                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
188                 // if the rope is stretched by more than this amount, more rope is
189                 // given to you again
190
191                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
192                 // while hanging on the rope, this friction component will help you a
193                 // bit to control the rope
194
195                 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
196
197                 dir = self.origin - myorg;
198                 dist = vlen(dir);
199                 dir = normalize(dir);
200
201                 if(tarzan)
202                 {
203                         v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, self, pull_entity.velocity);
204
205                         // first pull the rope...
206                         if(self.realowner.hook_state & HOOK_PULLING)
207                         {
208                                 newlength = self.hook_length;
209                                 newlength = max(newlength - pullspeed * frametime, minlength);
210
211                                 if(newlength < dist - ropestretch) // overstretched?
212                                 {
213                                         newlength = dist - ropestretch;
214                                         if(v * dir < 0) // only if not already moving in hook direction
215                                                 v = v + frametime * dir * rubberforce_overstretch;
216                                 }
217
218                                 self.hook_length = newlength;
219                         }
220
221                         if(pull_entity.movetype == MOVETYPE_FLY)
222                                 pull_entity.movetype = MOVETYPE_WALK;
223
224                         if(self.realowner.hook_state & HOOK_RELEASING)
225                         {
226                                 newlength = dist;
227                                 self.hook_length = newlength;
228                         }
229                         else
230                         {
231                                 // then pull the player
232                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
233                                 v = v * (1 - frametime * ropeairfriction);
234                                 v = v + frametime * dir * spd * rubberforce;
235
236                                 dv = ((v - v0) * dir) * dir;
237                                 if(tarzan >= 2)
238                                 {
239                                         if(self.aiment.movetype == MOVETYPE_WALK || self.aiment.classname == "nade")
240                                         {
241                                                 entity aim_ent = ((IS_VEHICLE(self.aiment) && self.aiment.owner) ? self.aiment.owner : self.aiment);
242                                                 v = v - dv * 0.5;
243                                                 if((frozen_pulling && STAT(FROZEN, self.aiment)) || !frozen_pulling)
244                                                 {
245                                                         self.aiment.velocity = self.aiment.velocity - dv * 0.5;
246                                                         UNSET_ONGROUND(self.aiment);
247                                                         if(self.aiment.flags & FL_PROJECTILE)
248                                                                 UpdateCSQCProjectile(self.aiment);
249                                                 }
250                                                 if(self.aiment.classname == "nade")
251                                                         self.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
252                                                 aim_ent.pusher = self.realowner;
253                                                 aim_ent.pushltime = time + autocvar_g_maxpushtime;
254                                                 aim_ent.istypefrag = aim_ent.BUTTON_CHAT;
255                                         }
256                                 }
257
258                                 UNSET_ONGROUND(pull_entity);
259                         }
260
261                         if(!frozen_pulling && !(self.aiment.flags & FL_PROJECTILE))
262                                 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(self, pull_entity, v * velocity_multiplier);
263
264                         if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, self.aiment))
265                         {
266                                 RemoveGrapplingHook(self.realowner);
267                                 return;
268                         }
269                 }
270                 else
271                 {
272                         end = self.origin - dir*50;
273                         dist = vlen(end - myorg);
274                         if(dist < 200)
275                                 spd = dist * (pullspeed / 200);
276                         else
277                                 spd = pullspeed;
278                         if(spd < 50)
279                                 spd = 0;
280                         self.realowner.velocity = dir*spd;
281                         self.realowner.movetype = MOVETYPE_FLY;
282
283                         UNSET_ONGROUND(self.realowner);
284                 }
285         }
286
287         makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
288         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
289
290         if(myorg != self.hook_start)
291         {
292                 self.SendFlags |= 2;
293                 self.hook_start = myorg;
294         }
295         if(org != self.hook_end)
296         {
297                 self.SendFlags |= 4;
298                 self.hook_end = org;
299         }
300 }
301
302 void GrapplingHookTouch ()
303 {SELFPARAM();
304         if(other.movetype == MOVETYPE_FOLLOW)
305                 return;
306         PROJECTILE_TOUCH;
307
308         GrapplingHook_Stop();
309
310         if(other)
311                 if(other.movetype != MOVETYPE_NONE)
312                 {
313                         SetMovetypeFollow(self, other);
314                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
315                 }
316
317         //self.realowner.disableclientprediction = true;
318 }
319
320 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
321 {
322         if(this.health <= 0)
323                 return;
324
325         if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
326                 return; // g_balance_projectiledamage says to halt
327
328         this.health = this.health - damage;
329
330         if (this.health <= 0)
331         {
332                 if(attacker != this.realowner)
333                 {
334                         this.realowner.pusher = attacker;
335                         this.realowner.pushltime = time + autocvar_g_maxpushtime;
336                         this.realowner.istypefrag = this.realowner.BUTTON_CHAT;
337                 }
338                 RemoveGrapplingHook(this.realowner);
339         }
340 }
341
342 void FireGrapplingHook ()
343 {SELFPARAM();
344         entity missile;
345         vector org;
346         vector vs;
347
348         if(forbidWeaponUse(self)) return;
349         if(self.vehicle) return;
350
351         makevectors(self.v_angle);
352
353         int s = W_GetGunAlignment(self);
354         vs = hook_shotorigin[s];
355
356         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
357         sound (self, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
358         org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
359
360         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
361         org = trace_endpos;
362
363         Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
364
365         missile = WarpZone_RefSys_SpawnSameRefSys(self);
366         missile.owner = missile.realowner = self;
367         self.hook = missile;
368         missile.reset = GrapplingHookReset;
369         missile.classname = "grapplinghook";
370         missile.flags = FL_PROJECTILE;
371
372         missile.movetype = ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY);
373         PROJECTILE_MAKETRIGGER(missile);
374
375         //setmodel (missile, MDL_HOOK); // precision set below
376         setsize (missile, '-3 -3 -3', '3 3 3');
377         setorigin (missile, org);
378
379         missile.state = 0; // not latched onto anything
380
381         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
382
383         missile.angles = vectoangles (missile.velocity);
384         //missile.glow_color = 250; // 244, 250
385         //missile.glow_size = 120;
386         missile.touch = GrapplingHookTouch;
387         missile.think = GrapplingHookThink;
388         missile.nextthink = time;
389
390         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
391
392         missile.health = autocvar_g_balance_grapplehook_health;//120
393         missile.event_damage = GrapplingHook_Damage;
394         missile.takedamage = DAMAGE_AIM;
395         missile.damageforcescale = 0;
396         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
397
398         missile.hook_start = missile.hook_end = missile.origin;
399
400         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
401 }
402
403 void GrappleHookInit()
404 {
405         if(g_grappling_hook)
406         {
407                 hook_shotorigin[0] = '8 8 -12';
408                 hook_shotorigin[1] = '8 8 -12';
409                 hook_shotorigin[2] = '8 8 -12';
410                 hook_shotorigin[3] = '8 8 -12';
411         }
412         else
413         {
414                 Weapon w = WEP_HOOK;
415                 w.wr_init(w);
416                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
417                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
418                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
419                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
420         }
421 }