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