]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_hook.qc
two big changes:
[divverent/nexuiz.git] / data / qcsrc / server / g_hook.qc
1 /*============================================
2
3       Wazat's Nexuiz 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 .float rope_length;
51 .float button6_pressed_before;
52
53 void RemoveGrapplingHook(entity pl)
54 {
55         if(pl.hook == world)
56                 return;
57         remove(pl.hook);
58         pl.hook = world;
59         if(pl.movetype == MOVETYPE_FLY)
60                 pl.movetype = MOVETYPE_WALK;
61
62         pl.hook_time = time + 0.0;
63
64         //pl.disableclientprediction = FALSE;
65 }
66
67 void GrapplingHookThink()
68 {
69         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;
70         vector dir, org, end;
71         if(self.owner.health <= 0 || self.owner.hook != self)   // how did that happen?
72         {                                                                                                               // well, better fix it anyway
73                 remove(self);
74                 return;
75         }
76
77         self.nextthink = time;
78
79         makevectors(self.owner.v_angle);
80         org = self.owner.origin + self.owner.view_ofs + v_forward * 8 - v_right * 8 + v_up * -12;
81
82         if(self.rope_length < 0)
83                 self.rope_length = vlen(org - self.origin);
84
85         if(self.state == 1)
86         {
87                 pullspeed = cvar("g_balance_grapplehook_speed_pull");//2000;
88                 // speed the rope is pulled with
89
90                 rubberforce = cvar("g_balance_grapplehook_force_rubber");//2000;
91                 // force the rope will use if it is stretched
92
93                 rubberforce_overstretch = cvar("g_balance_grapplehook_force_rubber_overstretch");//1000;
94                 // force the rope will use if it is stretched
95
96                 minlength = cvar("g_balance_grapplehook_length_min");//100;
97                 // minimal rope length
98                 // if the rope goes below this length, it isn't pulled any more
99
100                 ropestretch = cvar("g_balance_grapplehook_stretch");//400;
101                 // if the rope is stretched by more than this amount, more rope is
102                 // given to you again
103
104                 ropeairfriction = cvar("g_balance_grapplehook_airfriction");//0.2
105                 // while hanging on the rope, this friction component will help you a
106                 // bit to control the rope
107
108                 dir = self.origin - org;
109                 dist = vlen(dir);
110                 dir = normalize(dir);
111
112                 if(cvar("g_grappling_hook_tarzan"))
113                 {
114                         newlength = self.rope_length;
115
116                         // first pull the rope...
117                         newlength = max(newlength - pullspeed * frametime, minlength);
118
119                         if(newlength < dist - ropestretch) // overstretched?
120                         {
121                                 newlength = dist - ropestretch;
122                                 if(self.owner.velocity * dir < 0) // only if not already moving in hook direction
123                                         self.owner.velocity = self.owner.velocity + frametime * dir * rubberforce_overstretch;
124                         }
125
126                         if(!self.owner.BUTTON_CROUCH) // crouch key = don't pull
127                                 self.rope_length = newlength;
128
129                         // then pull the player
130                         spd = bound(0, (dist - self.rope_length) / ropestretch, 1);
131                         self.owner.velocity = self.owner.velocity * (1 - frametime * ropeairfriction);
132                         self.owner.velocity = self.owner.velocity + frametime * dir * spd * rubberforce;
133                 }
134                 else
135                 {
136                         end = self.origin - dir*50;
137                         dist = vlen(end - org);
138                         if(dist < 200)
139                                 spd = dist * (pullspeed / 200);
140                         else
141                                 spd = pullspeed;
142                         if(spd < 50)
143                                 spd = 0;
144                         self.owner.velocity = dir*spd;
145                         self.owner.movetype = MOVETYPE_FLY;
146                 }
147
148                 self.owner.flags = self.owner.flags - (self.owner.flags & FL_ONGROUND);
149
150                 org = org + dir*50; // get the beam out of the player's eyes
151         }
152
153         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');
154         te_beam(self, self.origin + v_forward * (-9), org);
155 }
156
157 void GrapplingHookTouch (void)
158 {
159         if (other == self.owner)
160                 return;
161         // altered for Nexuiz
162         //else if (pointcontents (self.origin) == CONTENT_SKY)
163         else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
164         {
165                 RemoveGrapplingHook(self.owner);
166                 return;
167         }
168
169         if(other == world)
170         {
171                 vector tic;
172                 tic = self.velocity * sys_ticrate;
173                 tic = tic + normalize(tic) * vlen(self.maxs - self.mins);
174                 traceline(self.origin - tic, self.origin + tic, MOVE_NORMAL, self);
175                 if(trace_fraction >= 1)
176                 {
177                         dprint("Odd... did not hit...?\n");
178                 }
179                 else if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
180                 {
181                         dprint("Detected and prevented the sky-grapple bug.\n");
182                         RemoveGrapplingHook(self.owner);
183                         return;
184                 }
185         }
186
187         pointparticles(particleeffectnum("grapple_impact"), self.origin, '0 0 0', 1);
188         sound (self, CHAN_BODY, "weapons/hook_impact.wav", VOL_BASE, ATTN_NORM);
189
190         self.state = 1;
191         self.think = GrapplingHookThink;
192         self.nextthink = time;
193         self.touch = SUB_Null;
194         self.velocity = '0 0 0';
195         self.movetype = MOVETYPE_NONE;
196         self.rope_length = -1;
197
198         //self.owner.disableclientprediction = TRUE;
199 }
200
201 void GrapplingHook_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
202 {
203         if(self.health > 0)
204         {
205                 self.health = self.health - damage;
206                 if (self.health <= 0)
207                 {
208                         if(attacker != self.owner)
209                         {
210                                 self.owner.pusher = attacker;
211                                 self.owner.pushltime = time + cvar("g_maxpushtime");
212                         }
213                         RemoveGrapplingHook(self.owner);
214                 }
215         }
216 }
217
218 void FireGrapplingHook (void)
219 {
220         local entity missile;
221         local vector org;
222
223         if((arena_roundbased && time < warmup) || (time < restart_countdown))
224                 return;
225
226         makevectors(self.v_angle);
227
228         sound (self, CHAN_WEAPON, "weapons/hook_fire.wav", VOL_BASE, ATTN_NORM);
229         org = self.origin + self.view_ofs + v_forward * 8 - v_right * 8 + '0 0 -12';
230         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);
231
232         missile = spawn ();
233         missile.owner = self;
234         self.hook = missile;
235         missile.classname = "grapplinghook";
236
237         missile.movetype = MOVETYPE_FLY;
238         missile.solid = SOLID_BBOX;
239
240         setmodel (missile, "models/hook.md3"); // precision set below
241         setsize (missile, '-3 -3 -3', '3 3 3');
242         setorigin (missile, org);
243
244         missile.state = 0; // not latched onto anything
245
246         missile.velocity = v_forward * cvar("g_balance_grapplehook_speed_fly");
247         W_SetupProjectileVelocity(missile);
248
249         missile.angles = vectoangles (missile.velocity);
250         //missile.glow_color = 250; // 244, 250
251         //missile.glow_size = 120;
252         missile.touch = GrapplingHookTouch;
253         missile.think = GrapplingHookThink;
254         missile.nextthink = time + 0.1;
255
256         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;
257
258         missile.health = cvar("g_balance_grapplehook_health");//120
259         missile.event_damage = GrapplingHook_Damage;
260         missile.takedamage = DAMAGE_AIM;
261         missile.damageforcescale = 0;
262 }
263
264 void GrapplingHookFrame()
265 {
266         // this function has been modified for Nexuiz
267         if (self.BUTTON_HOOK && g_grappling_hook)
268         {
269                 if (!self.hook && self.hook_time <= time && !self.button6_pressed_before)
270                         if (timeoutStatus != 2) //only allow the player to fire the grappling hook if the game is not paused (timeout)
271                                 FireGrapplingHook();
272         }
273         else
274         {
275                 if (self.hook)
276                         RemoveGrapplingHook(self);
277         }
278         self.button6_pressed_before = self.BUTTON_HOOK;
279         /*
280         // if I have no hook or it's not pulling yet, make sure I'm not flying!
281         if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
282         {
283                 self.movetype = MOVETYPE_WALK;
284         }
285         if(self.impulse == GRAPHOOK_FIRE && self.hook_time <= time && g_grappling_hook)
286         {
287                 // fire hook
288                 FireGrapplingHook();
289                 return;
290         }
291         else if(self.hookimpulse == GRAPHOOK_RELEASE)
292         {
293                 // remove hook, reset movement type
294                 RemoveGrapplingHook(self);
295                 return;
296         }
297         */
298         /*else // make sure the player's movetype is correct
299         {
300                 //if(self.hook == world && self.movetype == MOVETYPE_FLY)
301                 if((self.hook == world || !self.hook.state) && self.movetype == MOVETYPE_FLY)
302                 {
303                         self.movetype = MOVETYPE_WALK;
304                 }
305         }*/
306         // note: The hook entity does the actual pulling
307 }
308
309 void SetGrappleHookBindings()
310 {
311         // this function has been modified for Nexuiz
312         // don't remove these lines! old server or demos coud overwrite the new aliases
313         stuffcmd(self, "alias +hook +button6\n");
314         stuffcmd(self, "alias -hook -button6\n");
315 }