]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_hook.qc
allow "uid2name" on bots ;)
[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     if(self.freezetag_frozen)
336         return;
337
338         makevectors(self.v_angle);
339
340         s = self.cvar_cl_gunalign;
341         if(s != 1 && s != 2 && s != 4)
342                 s = 3; // default value
343         --s;
344         vs = hook_shotorigin[s];
345
346         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds
347         sound (self, CHAN_WEAPON2, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
348         org = self.origin + self.view_ofs + v_forward * vs_x + v_right * -vs_y + v_up * vs_z;
349
350         tracebox(self.origin + self.view_ofs, '-3 -3 -3', '3 3 3', org, MOVE_NORMAL, self);
351         org = trace_endpos;
352
353         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
354
355         missile = WarpZone_RefSys_SpawnSameRefSys(self);
356         missile.owner = self;
357         self.hook = missile;
358         missile.classname = "grapplinghook";
359
360         missile.movetype = MOVETYPE_FLY;
361         PROJECTILE_MAKETRIGGER(missile);
362
363         //setmodel (missile, "models/hook.md3"); // precision set below
364         setsize (missile, '-3 -3 -3', '3 3 3');
365         setorigin (missile, org);
366
367         missile.state = 0; // not latched onto anything
368
369         W_SetupProjectileVelocityEx(missile, v_forward, v_up, autocvar_g_balance_grapplehook_speed_fly, 0, 0, 0, FALSE);
370
371         missile.angles = vectoangles (missile.velocity);
372         //missile.glow_color = 250; // 244, 250
373         //missile.glow_size = 120;
374         missile.touch = GrapplingHookTouch;
375         missile.think = GrapplingHookThink;
376         missile.nextthink = time;
377
378         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
379
380         missile.health = autocvar_g_balance_grapplehook_health;//120
381         missile.event_damage = GrapplingHook_Damage;
382         missile.takedamage = DAMAGE_AIM;
383         missile.damageforcescale = 0;
384
385         missile.hook_start = missile.hook_end = missile.origin;
386
387         Net_LinkEntity(missile, FALSE, 0, GrapplingHookSend);
388 }
389
390 //  void GrapplingHookFrame()
391 //  {
392 //         // this function has been modified for Xonotic
393 // -       if (self.BUTTON_HOOK && g_grappling_hook)
394 //         {
395 // -               if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
396 // -                       if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
397 // -                               FireGrapplingHook();
398 //         }
399 // -       else
400 //         {
401 //                 if (self.hook)
402 //                         RemoveGrapplingHook(self);
403 //         }
404 // -       self.button6_pressed_before = self.BUTTON_HOOK;
405 //         /*
406 //         // if I have no hook or it's not pulling yet, make sure I'm not flying!
407 //         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
408
409 void GrapplingHookFrame()
410 {
411         if(g_grappling_hook && timeoutStatus != 2 && self.weapon != WEP_HOOK)
412         {
413                 // offhand hook controls
414                 if(self.BUTTON_HOOK)
415                 {
416                         if not(self.hook || (self.hook_state & HOOK_WAITING_FOR_RELEASE))
417                         {
418                                 self.hook_state |= HOOK_FIRING;
419                                 self.hook_state |= HOOK_WAITING_FOR_RELEASE;
420                         }
421                 }
422                 else
423                 {
424                         self.hook_state |= HOOK_REMOVING;
425                         self.hook_state &~= HOOK_WAITING_FOR_RELEASE;
426                 }
427
428                 self.hook_state &~= HOOK_RELEASING;
429                 if(self.BUTTON_CROUCH)
430                 {
431                         self.hook_state &~= HOOK_PULLING;
432                         //self.hook_state |= HOOK_RELEASING;
433                 }
434                 else
435                 {
436                         self.hook_state |= HOOK_PULLING;
437                         //self.hook_state &~= HOOK_RELEASING;
438                 }
439         }
440         else if(!(self.items & IT_JETPACK) && !g_grappling_hook && self.switchweapon != WEP_HOOK)
441         {
442                 if(self.BUTTON_HOOK && !self.hook_switchweapon)
443                         W_SwitchWeapon(WEP_HOOK);
444         }
445         self.hook_switchweapon = self.BUTTON_HOOK;
446
447         if(!g_grappling_hook && self.weapon != WEP_HOOK)
448         {
449                 self.hook_state &~= HOOK_FIRING;
450                 self.hook_state |= HOOK_REMOVING;
451         }
452
453         if (self.hook_state & HOOK_FIRING)
454         {
455                 if (self.hook)
456                         RemoveGrapplingHook(self);
457                 FireGrapplingHook();
458                 self.hook_state &~= HOOK_FIRING;
459         }
460         else if(self.hook_state & HOOK_REMOVING)
461         {
462                 if (self.hook)
463                         RemoveGrapplingHook(self);
464                 self.hook_state &~= HOOK_REMOVING;
465         }
466
467         /*
468         // if I have no hook or it's not pulling yet, make sure I'm not flying!
469         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
470         {
471                 self.movetype = MOVETYPE_WALK;
472         }
473         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
474         {
475                 // fire hook
476                 FireGrapplingHook();
477                 return;
478         }
479         else if(self.hookimpulse == GRAPHOOK_RELEASE)
480         {
481                 // remove hook, reset movement type
482                 RemoveGrapplingHook(self);
483                 return;
484         }
485         */
486         /*else // make sure the player's movetype is correct
487         {
488                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
489                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
490                 {
491                         self.movetype = MOVETYPE_WALK;
492                 }
493         }*/
494         // note: The hook entity does the actual pulling
495 }
496
497 void GrappleHookInit()
498 {
499         if(g_grappling_hook)
500         {
501                 hook_shotorigin[0] = '8 8 -12';
502                 hook_shotorigin[1] = '8 8 -12';
503                 hook_shotorigin[2] = '8 8 -12';
504                 hook_shotorigin[3] = '8 8 -12';
505         }
506         else
507         {
508                 weapon_action(WEP_HOOK, WR_PRECACHE);
509                 hook_shotorigin[0] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 1);
510                 hook_shotorigin[1] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 2);
511                 hook_shotorigin[2] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 3);
512                 hook_shotorigin[3] = shotorg_adjust_values(CL_Weapon_GetShotOrg(WEP_HOOK), FALSE, FALSE, 4);
513         }
514 }
515
516 void SetGrappleHookBindings()
517 {
518         // this function has been modified for Xonotic
519         // don't remove these lines! old server or demos coud overwrite the new aliases
520         stuffcmd(self, "alias +hook +button6\n");
521         stuffcmd(self, "alias -hook -button6\n");
522 }