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