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