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