]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
ef4cbc5a65409f2858f5a90561fa4c0a4910d240
[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 "player.qh"
9 #include "command/common.qh"
10 #include "round_handler.qh"
11 #include "../common/state.qh"
12 #include "../common/physics/player.qh"
13 #include "../common/vehicles/all.qh"
14 #include "../common/constants.qh"
15 #include "../common/util.qh"
16 #include <common/net_linked.qh>
17 #include <common/weapons/_all.qh>
18 #include "../lib/warpzone/common.qh"
19 #include "../lib/warpzone/server.qh"
20
21 .int state;
22
23 /*============================================
24
25       Wazat's Xonotic Grappling Hook
26
27         Contact: Wazat1@gmail.com
28
29
30 Installation instructions:
31 --------------------------
32
33 1. Place hook.c in your gamec source directory with the other source files.
34
35 2. Add this line to the bottom of progs.src:
36
37 gamec/hook.c
38
39 3. Open defs.h and add these lines to the very bottom:
40
41 // Wazat's grappling hook
42 .entity         hook;
43 void GrapplingHookFrame();
44 void RemoveGrapplingHook(entity pl);
45 void SetGrappleHookBindings();
46 // hook impulses
47 const float GRAPHOOK_FIRE               = 20;
48 const float GRAPHOOK_RELEASE            = 21;
49 // (note: you can change the hook impulse #'s to whatever you please)
50
51 4. Open client.c and add this to the top of PutClientInServer():
52
53         RemoveGrapplingHook(this); // Wazat's Grappling Hook
54
55 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
56
57         // Wazat's grappling hook
58         SetGrappleHookBindings();
59
60 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
61
62         GrapplingHookFrame();
63
64 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
65 bind ctrl "+hook"
66
67 And you should be done!
68
69
70 ============================================*/
71
72 .float hook_length;
73
74 void RemoveGrapplingHooks(entity pl)
75 {
76         if(pl.move_movetype == MOVETYPE_FLY)
77                 set_movetype(pl, MOVETYPE_WALK);
78
79         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
80     {
81         .entity weaponentity = weaponentities[slot];
82         if(pl.(weaponentity).hook)
83         {
84                 delete(pl.(weaponentity).hook);
85                 pl.(weaponentity).hook = NULL;
86         }
87     }
88
89         //pl.disableclientprediction = false;
90 }
91
92 void RemoveHook(entity this)
93 {
94         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
95     {
96         .entity weaponentity = weaponentities[slot];
97         if(this.realowner.(weaponentity).hook == this)
98                 this.realowner.(weaponentity).hook = NULL;
99     }
100
101     if(this.realowner.move_movetype == MOVETYPE_FLY)
102         set_movetype(this.realowner, MOVETYPE_WALK);
103     delete(this);
104 }
105
106 void GrapplingHookReset(entity this)
107 {
108         RemoveHook(this);
109 }
110
111 void GrapplingHookThink(entity this);
112 void GrapplingHook_Stop(entity this)
113 {
114         Send_Effect(EFFECT_HOOK_IMPACT, this.origin, '0 0 0', 1);
115         sound (this, CH_SHOTS, SND_HOOK_IMPACT, VOL_BASE, ATTEN_NORM);
116
117         this.state = 1;
118         setthink(this, GrapplingHookThink);
119         this.nextthink = time;
120         settouch(this, func_null);
121         this.velocity = '0 0 0';
122         set_movetype(this, MOVETYPE_NONE);
123         this.hook_length = -1;
124 }
125
126 .vector hook_start, hook_end;
127 bool GrapplingHookSend(entity this, entity to, int sf)
128 {
129         WriteHeader(MSG_ENTITY, ENT_CLIENT_HOOK);
130         sf = sf & 0x7F;
131         if(sound_allowed(MSG_BROADCAST, this.realowner))
132                 sf |= 0x80;
133         WriteByte(MSG_ENTITY, sf);
134         if(sf & 1)
135         {
136                 WriteByte(MSG_ENTITY, etof(this.realowner));
137                 WriteByte(MSG_ENTITY, weaponslot(this.weaponentity_fld));
138         }
139         if(sf & 2)
140         {
141                 WriteCoord(MSG_ENTITY, this.hook_start.x);
142                 WriteCoord(MSG_ENTITY, this.hook_start.y);
143                 WriteCoord(MSG_ENTITY, this.hook_start.z);
144         }
145         if(sf & 4)
146         {
147                 WriteCoord(MSG_ENTITY, this.hook_end.x);
148                 WriteCoord(MSG_ENTITY, this.hook_end.y);
149                 WriteCoord(MSG_ENTITY, this.hook_end.z);
150         }
151         return true;
152 }
153
154 int autocvar_g_grappling_hook_tarzan;
155
156 void GrapplingHookThink(entity this)
157 {
158         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
159         vector dir, org, end, v0, dv, v, myorg, vs;
160         .entity weaponentity = this.weaponentity_fld;
161         if(this.realowner.(weaponentity).hook != this)  // how did that happen?
162         {
163                 error("Owner lost the hook!\n");
164                 return;
165         }
166         if(LostMovetypeFollow(this) || game_stopped || (round_handler_IsActive() && !round_handler_IsRoundStarted()) || ((this.aiment.flags & FL_PROJECTILE) && this.aiment.classname != "nade"))
167         {
168                 RemoveHook(this);
169                 return;
170         }
171         if(this.aiment)
172                 WarpZone_RefSys_AddIncrementally(this, this.aiment);
173
174         this.nextthink = time;
175
176         int s = W_GunAlign(this.realowner.(weaponentity), STAT(GUNALIGN, this.realowner)) - 1;
177         vs = hook_shotorigin[s];
178
179         makevectors(this.realowner.v_angle);
180         org = this.realowner.origin + this.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
181         myorg = WarpZone_RefSys_TransformOrigin(this.realowner, this, org);
182
183         if(this.hook_length < 0)
184                 this.hook_length = vlen(myorg - this.origin);
185
186         int tarzan = autocvar_g_grappling_hook_tarzan;
187         entity pull_entity = this.realowner;
188         float velocity_multiplier = 1;
189         MUTATOR_CALLHOOK(GrappleHookThink, this, tarzan, pull_entity, velocity_multiplier);
190         tarzan = M_ARGV(1, int);
191         pull_entity = M_ARGV(2, entity);
192         velocity_multiplier = M_ARGV(3, float);
193
194         if(this.state == 1)
195         {
196                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
197                 // speed the rope is pulled with
198
199                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
200                 // force the rope will use if it is stretched
201
202                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
203                 // force the rope will use if it is stretched
204
205                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
206                 // minimal rope length
207                 // if the rope goes below this length, it isn't pulled any more
208
209                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
210                 // if the rope is stretched by more than this amount, more rope is
211                 // given to you again
212
213                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
214                 // while hanging on the rope, this friction component will help you a
215                 // bit to control the rope
216
217                 bool frozen_pulling = (autocvar_g_grappling_hook_tarzan >= 2 && autocvar_g_balance_grapplehook_pull_frozen);
218
219                 dir = this.origin - myorg;
220                 dist = vlen(dir);
221                 dir = normalize(dir);
222
223                 if(tarzan)
224                 {
225                         v = v0 = WarpZone_RefSys_TransformVelocity(pull_entity, this, pull_entity.velocity);
226
227                         // first pull the rope...
228                         if(this.realowner.(weaponentity).hook_state & HOOK_PULLING)
229                         {
230                                 newlength = this.hook_length;
231                                 newlength = max(newlength - pullspeed * frametime, minlength);
232
233                                 if(newlength < dist - ropestretch) // overstretched?
234                                 {
235                                         newlength = dist - ropestretch;
236                                         if(v * dir < 0) // only if not already moving in hook direction
237                                                 v = v + frametime * dir * rubberforce_overstretch;
238                                 }
239
240                                 this.hook_length = newlength;
241                         }
242
243                         if(pull_entity.move_movetype == MOVETYPE_FLY)
244                                 set_movetype(pull_entity, MOVETYPE_WALK);
245
246                         if(this.realowner.(weaponentity).hook_state & HOOK_RELEASING)
247                         {
248                                 newlength = dist;
249                                 this.hook_length = newlength;
250                         }
251                         else
252                         {
253                                 // then pull the player
254                                 spd = bound(0, (dist - this.hook_length) / ropestretch, 1);
255                                 v = v * (1 - frametime * ropeairfriction);
256                                 v = v + frametime * dir * spd * rubberforce;
257
258                                 dv = ((v - v0) * dir) * dir;
259                                 if(tarzan >= 2)
260                                 {
261                                         if(this.aiment.move_movetype == MOVETYPE_WALK || this.aiment.classname == "nade")
262                                         {
263                                                 entity aim_ent = ((IS_VEHICLE(this.aiment) && this.aiment.owner) ? this.aiment.owner : this.aiment);
264                                                 v = v - dv * 0.5;
265                                                 if((frozen_pulling && STAT(FROZEN, this.aiment)) || !frozen_pulling)
266                                                 {
267                                                         this.aiment.velocity = this.aiment.velocity - dv * 0.5;
268                                                         UNSET_ONGROUND(this.aiment);
269                                                         if(this.aiment.flags & FL_PROJECTILE)
270                                                                 UpdateCSQCProjectile(this.aiment);
271                                                 }
272                                                 if(this.aiment.classname == "nade")
273                                                         this.aiment.nextthink = time + autocvar_g_balance_grapplehook_nade_time; // set time after letting go?
274                                                 aim_ent.pusher = this.realowner;
275                                                 aim_ent.pushltime = time + autocvar_g_maxpushtime;
276                                                 aim_ent.istypefrag = PHYS_INPUT_BUTTON_CHAT(aim_ent);
277                                         }
278                                 }
279
280                                 UNSET_ONGROUND(pull_entity);
281                         }
282
283                         if(!frozen_pulling && !(this.aiment.flags & FL_PROJECTILE))
284                                 pull_entity.velocity = WarpZone_RefSys_TransformVelocity(this, pull_entity, v * velocity_multiplier);
285
286                         if(frozen_pulling && autocvar_g_balance_grapplehook_pull_frozen == 2 && !STAT(FROZEN, this.aiment))
287                         {
288                                 RemoveHook(this);
289                                 return;
290                         }
291                 }
292                 else
293                 {
294                         end = this.origin - dir*50;
295                         dist = vlen(end - myorg);
296                         if(dist < 200)
297                                 spd = dist * (pullspeed / 200);
298                         else
299                                 spd = pullspeed;
300                         if(spd < 50)
301                                 spd = 0;
302                         this.realowner.velocity = dir*spd;
303                         set_movetype(this.realowner, MOVETYPE_FLY);
304
305                         UNSET_ONGROUND(this.realowner);
306                 }
307         }
308
309         makevectors(this.angles.x * '-1 0 0' + this.angles.y * '0 1 0');
310         myorg = WarpZone_RefSys_TransformOrigin(this, this.realowner, this.origin); // + v_forward * (-9);
311
312         if(myorg != this.hook_start)
313         {
314                 this.SendFlags |= 2;
315                 this.hook_start = myorg;
316         }
317         if(org != this.hook_end)
318         {
319                 this.SendFlags |= 4;
320                 this.hook_end = org;
321         }
322 }
323
324 void GrapplingHookTouch(entity this, entity toucher)
325 {
326         if(toucher.move_movetype == MOVETYPE_FOLLOW)
327                 return;
328         PROJECTILE_TOUCH(this, toucher);
329
330         GrapplingHook_Stop(this);
331
332         if(toucher)
333                 if(toucher.move_movetype != MOVETYPE_NONE)
334                 {
335                         SetMovetypeFollow(this, toucher);
336                         WarpZone_RefSys_BeginAddingIncrementally(this, this.aiment);
337                 }
338
339         //this.realowner.disableclientprediction = true;
340 }
341
342 void GrapplingHook_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
343 {
344         if(this.health <= 0)
345                 return;
346
347         if (!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, -1)) // no exceptions
348                 return; // g_balance_projectiledamage says to halt
349
350         this.health = this.health - damage;
351
352         if (this.health <= 0)
353         {
354                 if(attacker != this.realowner)
355                 {
356                         this.realowner.pusher = attacker;
357                         this.realowner.pushltime = time + autocvar_g_maxpushtime;
358                         this.realowner.istypefrag = PHYS_INPUT_BUTTON_CHAT(this.realowner);
359                 }
360                 RemoveHook(this);
361         }
362 }
363
364 void FireGrapplingHook(entity actor, .entity weaponentity)
365 {
366         if(forbidWeaponUse(actor)) return;
367         if(actor.vehicle) return;
368
369         makevectors(actor.v_angle);
370
371         int s = W_GunAlign(actor.(weaponentity), STAT(GUNALIGN, actor)) - 1;
372         vector vs = hook_shotorigin[s];
373
374         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
375         sound (actor, CH_WEAPON_B, SND_HOOK_FIRE, VOL_BASE, ATTEN_NORM);
376         vector org = actor.origin + actor.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
377
378         tracebox(actor.origin + actor.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, actor);
379         org = trace_endpos;
380
381         Send_Effect(EFFECT_HOOK_MUZZLEFLASH, org, '0 0 0', 1);
382
383         entity missile = WarpZone_RefSys_SpawnSameRefSys(actor);
384         missile.owner = missile.realowner = actor;
385         actor.(weaponentity).hook = missile;
386         missile.weaponentity_fld = weaponentity;
387         missile.reset = GrapplingHookReset;
388         missile.classname = "grapplinghook";
389         missile.flags = FL_PROJECTILE;
390         IL_PUSH(g_projectiles, missile);
391         IL_PUSH(g_bot_dodge, missile);
392
393         set_movetype(missile, ((autocvar_g_balance_grapplehook_gravity) ? MOVETYPE_TOSS : MOVETYPE_FLY));
394         PROJECTILE_MAKETRIGGER(missile);
395
396         //setmodel (missile, MDL_HOOK); // precision set below
397         setsize (missile, '-3 -3 -3', '3 3 3');
398         setorigin(missile, org);
399
400         missile.state = 0; // not latched onto anything
401
402         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
403
404         missile.angles = vectoangles (missile.velocity);
405         //missile.glow_color = 250; // 244, 250
406         //missile.glow_size = 120;
407         settouch(missile, GrapplingHookTouch);
408         setthink(missile, GrapplingHookThink);
409         missile.nextthink = time;
410
411         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
412
413         missile.health = autocvar_g_balance_grapplehook_health;//120
414         missile.event_damage = GrapplingHook_Damage;
415         missile.takedamage = DAMAGE_AIM;
416         missile.damageforcescale = 0;
417         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
418         if(missile.damagedbycontents)
419                 IL_PUSH(g_damagedbycontents, missile);
420
421         missile.hook_start = missile.hook_end = missile.origin;
422
423         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
424 }
425
426 void GrappleHookInit()
427 {
428         if(g_grappling_hook)
429         {
430                 hook_shotorigin[0] = '8 8 -12';
431                 hook_shotorigin[1] = '8 8 -12';
432                 hook_shotorigin[2] = '8 8 -12';
433                 hook_shotorigin[3] = '8 8 -12';
434         }
435         else
436         {
437                 Weapon w = WEP_HOOK;
438                 w.wr_init(w);
439                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 1);
440                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 2);
441                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 3);
442                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK.m_id), false, false, 4);
443         }
444 }