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