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