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