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