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