]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
Merge master into qc_physics_prehax (blame TimePath if it's completely broken)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_hook.qc
1 #include "g_hook.qh"
2 #include "_all.qh"
3
4 #include "weapons/common.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 "vehicles/vehicle.qh"
12 #include "../common/constants.qh"
13 #include "../common/util.qh"
14 #include "../common/weapons/all.qh"
15 #include "../warpzonelib/common.qh"
16 #include "../warpzonelib/server.qh"
17
18 /*============================================
19
20       Wazat's Xonotic Grappling Hook
21
22         Contact: Wazat1@gmail.com
23
24
25 Installation instructions:
26 --------------------------
27
28 1. Place hook.c in your gamec source directory with the other source files.
29
30 2. Add this line to the bottom of progs.src:
31
32 gamec/hook.c
33
34 3. Open defs.h and add these lines to the very bottom:
35
36 // Wazat's grappling hook
37 .entity         hook;
38 void GrapplingHookFrame();
39 void RemoveGrapplingHook(entity pl);
40 void SetGrappleHookBindings();
41 // hook impulses
42 const float GRAPHOOK_FIRE               = 20;
43 const float GRAPHOOK_RELEASE            = 21;
44 // (note: you can change the hook impulse #'s to whatever you please)
45
46 4. Open client.c and add this to the top of PutClientInServer():
47
48         RemoveGrapplingHook(self); // Wazat's Grappling Hook
49
50 5. Find ClientConnect() (in client.c) and add these lines to the bottom:
51
52         // Wazat's grappling hook
53         SetGrappleHookBindings();
54
55 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:
56
57         GrapplingHookFrame();
58
59 7. Build and test the mod.  You'll want to bind a key to "+hook" like this:
60 bind ctrl "+hook"
61
62 And you should be done!
63
64
65 ============================================*/
66
67 .float hook_length;
68 .float hook_switchweapon;
69
70 void RemoveGrapplingHook(entity pl)
71 {
72         if(pl.hook == world)
73                 return;
74         remove(pl.hook);
75         pl.hook = world;
76         if(pl.movetype == MOVETYPE_FLY)
77                 pl.movetype = MOVETYPE_WALK;
78
79         //pl.disableclientprediction = false;
80 }
81
82 void GrapplingHookReset(void)
83 {
84         if(self.realowner.hook == self)
85                 RemoveGrapplingHook(self.owner);
86         else // in any case:
87                 remove(self);
88 }
89
90 void GrapplingHookThink();
91 void GrapplingHook_Stop()
92 {
93         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
94         sound (self, CH_SHOTS, "weapons/hook_impact.wav", VOL_BASE, ATTEN_NORM);
95
96         self.state = 1;
97         self.think = GrapplingHookThink;
98         self.nextthink = time;
99         self.touch = func_null;
100         self.velocity = '0 0 0';
101         self.movetype = MOVETYPE_NONE;
102         self.hook_length = -1;
103 }
104
105 .vector hook_start, hook_end;
106 float GrapplingHookSend(entity to, int sf)
107 {
108         WriteByte(MSG_ENTITY, ENT_CLIENT_HOOK);
109         sf = sf & 0x7F;
110         if(sound_allowed(MSG_BROADCAST, self.realowner))
111                 sf |= 0x80;
112         WriteByte(MSG_ENTITY, sf);
113         if(sf & 1)
114         {
115                 WriteByte(MSG_ENTITY, num_for_edict(self.realowner));
116         }
117         if(sf & 2)
118         {
119                 WriteCoord(MSG_ENTITY, self.hook_start.x);
120                 WriteCoord(MSG_ENTITY, self.hook_start.y);
121                 WriteCoord(MSG_ENTITY, self.hook_start.z);
122         }
123         if(sf & 4)
124         {
125                 WriteCoord(MSG_ENTITY, self.hook_end.x);
126                 WriteCoord(MSG_ENTITY, self.hook_end.y);
127                 WriteCoord(MSG_ENTITY, self.hook_end.z);
128         }
129         return true;
130 }
131
132 void GrapplingHookThink()
133 {
134         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
135         vector dir, org, end, v0, dv, v, myorg, vs;
136         if(self.realowner.hook != self) // how did that happen?
137         {
138                 error("Owner lost the hook!\n");
139                 return;
140         }
141         if(LostMovetypeFollow(self) || intermission_running || (round_handler_IsActive() && !round_handler_IsRoundStarted()))
142         {
143                 RemoveGrapplingHook(self.realowner);
144                 return;
145         }
146         if(self.aiment)
147                 WarpZone_RefSys_AddIncrementally(self, self.aiment);
148
149         self.nextthink = time;
150
151         int s = self.realowner.cvar_cl_gunalign;
152         if(s != 1 && s != 2 && s != 4)
153                 s = 3; // default value
154         --s;
155         vs = hook_shotorigin[s];
156
157         makevectors(self.realowner.v_angle);
158         org = self.realowner.origin + self.realowner.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
159         myorg = WarpZone_RefSys_TransformOrigin(self.realowner, self, org);
160
161         if(self.hook_length < 0)
162                 self.hook_length = vlen(myorg - self.origin);
163
164         if(self.state == 1)
165         {
166                 pullspeed = autocvar_g_balance_grapplehook_speed_pull;//2000;
167                 // speed the rope is pulled with
168
169                 rubberforce = autocvar_g_balance_grapplehook_force_rubber;//2000;
170                 // force the rope will use if it is stretched
171
172                 rubberforce_overstretch = autocvar_g_balance_grapplehook_force_rubber_overstretch;//1000;
173                 // force the rope will use if it is stretched
174
175                 minlength = autocvar_g_balance_grapplehook_length_min;//100;
176                 // minimal rope length
177                 // if the rope goes below this length, it isn't pulled any more
178
179                 ropestretch = autocvar_g_balance_grapplehook_stretch;//400;
180                 // if the rope is stretched by more than this amount, more rope is
181                 // given to you again
182
183                 ropeairfriction = autocvar_g_balance_grapplehook_airfriction;//0.2
184                 // while hanging on the rope, this friction component will help you a
185                 // bit to control the rope
186
187                 dir = self.origin - myorg;
188                 dist = vlen(dir);
189                 dir = normalize(dir);
190
191                 if(autocvar_g_grappling_hook_tarzan)
192                 {
193                         v = v0 = WarpZone_RefSys_TransformVelocity(self.realowner, self, self.realowner.velocity);
194
195                         // first pull the rope...
196                         if(self.realowner.hook_state & HOOK_PULLING)
197                         {
198                                 newlength = self.hook_length;
199                                 newlength = max(newlength - pullspeed * frametime, minlength);
200
201                                 if(newlength < dist - ropestretch) // overstretched?
202                                 {
203                                         newlength = dist - ropestretch;
204                                         if(v * dir < 0) // only if not already moving in hook direction
205                                                 v = v + frametime * dir * rubberforce_overstretch;
206                                 }
207
208                                 self.hook_length = newlength;
209                         }
210
211                         if(self.realowner.hook_state & HOOK_RELEASING)
212                         {
213                                 newlength = dist;
214                                 self.hook_length = newlength;
215                         }
216                         else
217                         {
218                                 // then pull the player
219                                 spd = bound(0, (dist - self.hook_length) / ropestretch, 1);
220                                 v = v * (1 - frametime * ropeairfriction);
221                                 v = v + frametime * dir * spd * rubberforce;
222
223                                 dv = ((v - v0) * dir) * dir;
224                                 if(autocvar_g_grappling_hook_tarzan >= 2)
225                                 {
226                                         if(self.aiment.movetype == MOVETYPE_WALK)
227                                         {
228                                                 v = v - dv * 0.5;
229                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;
230                                                 self.aiment.flags &= ~FL_ONGROUND;
231                                                 self.aiment.pusher = self.realowner;
232                                                 self.aiment.pushltime = time + autocvar_g_maxpushtime;
233                                                 self.aiment.istypefrag = self.aiment.BUTTON_CHAT;
234                                         }
235                                 }
236
237                                 self.realowner.flags &= ~FL_ONGROUND;
238                         }
239
240                         self.realowner.velocity = WarpZone_RefSys_TransformVelocity(self, self.realowner, v);
241                 }
242                 else
243                 {
244                         end = self.origin - dir*50;
245                         dist = vlen(end - myorg);
246                         if(dist < 200)
247                                 spd = dist * (pullspeed / 200);
248                         else
249                                 spd = pullspeed;
250                         if(spd < 50)
251                                 spd = 0;
252                         self.realowner.velocity = dir*spd;
253                         self.realowner.movetype = MOVETYPE_FLY;
254
255                         self.realowner.flags &= ~FL_ONGROUND;
256                 }
257         }
258
259         makevectors(self.angles.x * '-1 0 0' + self.angles.y * '0 1 0');
260         myorg = WarpZone_RefSys_TransformOrigin(self, self.realowner, self.origin); // + v_forward * (-9);
261
262         if(myorg != self.hook_start)
263         {
264                 self.SendFlags |= 2;
265                 self.hook_start = myorg;
266         }
267         if(org != self.hook_end)
268         {
269                 self.SendFlags |= 4;
270                 self.hook_end = org;
271         }
272 }
273
274 void GrapplingHookTouch (void)
275 {
276         PROJECTILE_TOUCH;
277
278         GrapplingHook_Stop();
279
280         if(other)
281                 if(other.movetype != MOVETYPE_NONE)
282                 {
283                         SetMovetypeFollow(self, other);
284                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);
285                 }
286
287         //self.realowner.disableclientprediction = true;
288 }
289
290 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
291 {
292         if(self.health <= 0)
293                 return;
294
295         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, -1)) // no exceptions
296                 return; // g_balance_projectiledamage says to halt
297
298         self.health = self.health - damage;
299
300         if (self.health <= 0)
301         {
302                 if(attacker != self.realowner)
303                 {
304                         self.realowner.pusher = attacker;
305                         self.realowner.pushltime = time + autocvar_g_maxpushtime;
306                         self.realowner.istypefrag = self.realowner.BUTTON_CHAT;
307                 }
308                 RemoveGrapplingHook(self.realowner);
309         }
310 }
311
312 void FireGrapplingHook (void)
313 {
314         entity missile;
315         vector org;
316         vector vs;
317
318         if(forbidWeaponUse()) return;
319         if(self.vehicle) return;
320
321         makevectors(self.v_angle);
322
323         int s = self.cvar_cl_gunalign;
324         if(s != 1 && s != 2 && s != 4)
325                 s = 3; // default value
326         --s;
327         vs = hook_shotorigin[s];
328
329         // UGLY WORKAROUND: play this on CH_WEAPON_B so it can't cut off fire sounds
330         sound (self, CH_WEAPON_B, "weapons/hook_fire.wav", VOL_BASE, ATTEN_NORM);
331         org = self.origin + self.view_ofs + v_forward * vs.x + v_right * -vs.y + v_up * vs.z;
332
333         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
334         org = trace_endpos;
335
336         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
337
338         missile = WarpZone_RefSys_SpawnSameRefSys(self);
339         missile.owner = missile.realowner = self;
340         self.hook = missile;
341         missile.reset = GrapplingHookReset;
342         missile.classname = "grapplinghook";
343         missile.flags = FL_PROJECTILE;
344
345         missile.movetype = MOVETYPE_FLY;
346         PROJECTILE_MAKETRIGGER(missile);
347
348         //setmodel (missile, "models/hook.md3"); // precision set below
349         setsize (missile, '-3 -3 -3', '3 3 3');
350         setorigin (missile, org);
351
352         missile.state = 0; // not latched onto anything
353
354         W_SetupProjVelocity_Explicit(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, false);
355
356         missile.angles = vectoangles (missile.velocity);
357         //missile.glow_color = 250; // 244, 250
358         //missile.glow_size = 120;
359         missile.touch = GrapplingHookTouch;
360         missile.think = GrapplingHookThink;
361         missile.nextthink = time;
362
363         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
364
365         missile.health = autocvar_g_balance_grapplehook_health;//120
366         missile.event_damage = GrapplingHook_Damage;
367         missile.takedamage = DAMAGE_AIM;
368         missile.damageforcescale = 0;
369         missile.damagedbycontents = (autocvar_g_balance_grapplehook_damagedbycontents);
370
371         missile.hook_start = missile.hook_end = missile.origin;
372
373         Net_LinkEntity(missile, false, 0, GrapplingHookSend);
374 }
375
376 //  void GrapplingHookFrame()
377 //  {
378 //         // this function has been modified for Xonotic
379 // -       if (self.BUTTON_HOOK && g_grappling_hook)
380 //         {
381 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
382 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
383 // -                               FireGrapplingHook();
384 //         }
385 // -       else
386 //         {
387 //                 if (self.hook)
388 //                         RemoveGrapplingHook(self);
389 //         }
390 // -       self.button6_pressed_before = self.BUTTON_HOOK;
391 //         /*
392 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
393 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
394
395 void GrapplingHookFrame()
396 {
397         if(g_grappling_hook && timeout_status != TIMEOUT_ACTIVE && self.weapon != WEP_HOOK)
398         {
399                 // offhand hook controls
400                 if(self.BUTTON_HOOK)
401                 {
402                         if (!(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE)) && (time > self.hook_refire))
403                         {
404                                 self.hook_state |= HOOK_FIRING;
405                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
406                         }
407                 }
408                 else
409                 {
410                         self.hook_state |= HOOK_REMOVING;
411                         self.hook_state &= ~HOOK_WAITING_FOR_RELEASE;
412                 }
413
414                 self.hook_state &= ~HOOK_RELEASING;
415                 if(self.BUTTON_CROUCH)
416                 {
417                         self.hook_state &= ~HOOK_PULLING;
418                         //self.hook_state |= HOOK_RELEASING;
419                 }
420                 else
421                 {
422                         self.hook_state |= HOOK_PULLING;
423                         //self.hook_state &= ~HOOK_RELEASING;
424                 }
425         }
426         else if(!g_grappling_hook && self.switchweapon != WEP_HOOK)
427         {
428                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
429                         W_SwitchWeapon(WEP_HOOK);
430         }
431         self.hook_switchweapon = self.BUTTON_HOOK;
432
433         if(!g_grappling_hook && self.weapon != WEP_HOOK)
434         {
435                 self.hook_state &= ~HOOK_FIRING;
436                 self.hook_state |= HOOK_REMOVING;
437         }
438
439         if (self.hook_state & HOOK_FIRING)
440         {
441                 if (self.hook)
442                         RemoveGrapplingHook(self);
443                 FireGrapplingHook();
444                 self.hook_state &= ~HOOK_FIRING;
445                 self.hook_refire = max(self.hook_refire, time + autocvar_g_balance_grapplehook_refire * W_WeaponRateFactor());
446         }
447         else if(self.hook_state & HOOK_REMOVING)
448         {
449                 if (self.hook)
450                         RemoveGrapplingHook(self);
451                 self.hook_state &= ~HOOK_REMOVING;
452         }
453
454         /*
455         // if I have no hook or it's not pulling yet, make sure I'm not flying!
456         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
457         {
458                 self.movetype = MOVETYPE_WALK;
459         }
460         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
461         {
462                 // fire hook
463                 FireGrapplingHook();
464                 return;
465         }
466         else if(self.hookimpulse == GRAPHOOK_RELEASE)
467         {
468                 // remove hook, reset movement type
469                 RemoveGrapplingHook(self);
470                 return;
471         }
472         */
473         /*else // make sure the player's movetype is correct
474         {
475                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
476                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
477                 {
478                         self.movetype = MOVETYPE_WALK;
479                 }
480         }*/
481         // note: The hook entity does the actual pulling
482 }
483
484 void GrappleHookInit()
485 {
486         if(g_grappling_hook)
487         {
488                 hook_shotorigin[0] = '8 8 -12';
489                 hook_shotorigin[1] = '8 8 -12';
490                 hook_shotorigin[2] = '8 8 -12';
491                 hook_shotorigin[3] = '8 8 -12';
492         }
493         else
494         {
495                 WEP_ACTION(WEP_HOOK, WR_INIT);
496                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 1);
497                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 2);
498                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 3);
499                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), false, false, 4);
500         }
501 }
502
503 void SetGrappleHookBindings()
504 {
505         // this function has been modified for Xonotic
506         // don't remove these lines! old server or demos coud overwrite the new aliases
507         stuffcmd(self, "alias +hook +button6\n");
508         stuffcmd(self, "alias -hook -button6\n");
509 }