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