Grabber: Don't behave differently while the player is crouching. Fixes a bug
[voretournament/voretournament.git] / data / qcsrc / server / g_grabber.qc
1 /*============================================\r
2 \r
3       Wazat's Voretournament Grabber\r
4 \r
5         Contact: Wazat1@gmail.com\r
6 \r
7 \r
8 Installation instructions:\r
9 --------------------------\r
10 \r
11 1. Place grabber.c in your gamec source directory with the other source files.\r
12 \r
13 2. Add this line to the bottom of progs.src:\r
14 \r
15 gamec/grabber.c\r
16 \r
17 3. Open defs.h and add these lines to the very bottom:\r
18 \r
19 // Wazat's grabber\r
20 .entity         grabber;\r
21 voidGrabberFrame();\r
22 void RemoveGrabber(entity pl);\r
23 void SetGrabberBindings();\r
24 // grabber impulses\r
25 float GRABBER_FIRE              = 20;\r
26 float GRABBER_RELEASE           = 21;\r
27 // (note: you can change the grabber impulse #'s to whatever you please)\r
28 \r
29 4. Open client.c and add this to the top of PutClientInServer():\r
30 \r
31         RemoveGrabber(self); // Wazat's Grabber\r
32 \r
33 5. Find ClientConnect() (in client.c) and add these lines to the bottom:\r
34 \r
35         // Wazat's grabber\r
36         SetGrabberBindings();\r
37 \r
38 6. Still in client.c, find PlayerPreThink and add this line just above the call to W_WeaponFrame:\r
39 \r
40         GrabberFrame();\r
41 \r
42 7. Build and test the mod.  You'll want to bind a key to "+grabber" like this:\r
43 bind ctrl "+grabber"\r
44 \r
45 And you should be done!\r
46 \r
47 \r
48 ============================================*/\r
49 \r
50 .string aiment_classname;\r
51 .float aiment_deadflag;\r
52 void SetMovetypeFollow(entity ent, entity e)\r
53 {\r
54         // FIXME this may not be warpzone aware\r
55         ent.movetype = MOVETYPE_FOLLOW; // make the hole follow\r
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.\r
57         ent.aiment = e; // make the hole follow bmodel\r
58         ent.punchangle = e.angles; // the original angles of bmodel\r
59         ent.view_ofs = ent.origin - e.origin; // relative origin\r
60         ent.v_angle = ent.angles - e.angles; // relative angles\r
61         ent.aiment_classname = strzone(e.classname);\r
62         ent.aiment_deadflag = e.deadflag;\r
63 }\r
64 void UnsetMovetypeFollow(entity ent)\r
65 {\r
66         ent.movetype = MOVETYPE_FLY;\r
67         PROJECTILE_MAKETRIGGER(ent);\r
68         ent.aiment = world;\r
69 }\r
70 float LostMovetypeFollow(entity ent)\r
71 {\r
72 /*\r
73         if(ent.movetype != MOVETYPE_FOLLOW)\r
74                 if(ent.aiment)\r
75                         error("???");\r
76 */\r
77         if(ent.aiment)\r
78         {\r
79                 if(ent.aiment.classname != ent.aiment_classname)\r
80                         return 1;\r
81                 if(ent.aiment.deadflag != ent.aiment_deadflag)\r
82                         return 1;\r
83         }\r
84         return 0;\r
85 }\r
86 \r
87 .float grabber_length;\r
88 \r
89 void RemoveGrabber(entity pl)\r
90 {\r
91         if(pl.grabber == world)\r
92                 return;\r
93         remove(pl.grabber);\r
94         pl.grabber = world;\r
95         if(pl.movetype == MOVETYPE_FLY)\r
96                 pl.movetype = MOVETYPE_WALK;\r
97 \r
98         //pl.disableclientprediction = FALSE;\r
99 }\r
100 \r
101 void GrabberThink();\r
102 void Grabber_Stop()\r
103 {\r
104         pointparticles(particleeffectnum("grabber_impact"), self.origin, '0 0 0', 1);\r
105         if(other.classname == "player")\r
106                 sound (self, CHAN_PROJECTILE, "weapons/grabber_impact_player.wav", VOL_BASE, ATTN_NORM);\r
107         else\r
108                 sound (self, CHAN_PROJECTILE, "weapons/grabber_impact_world.wav", VOL_BASE, ATTN_NORM);\r
109 \r
110         self.state = 1;\r
111         self.think = GrabberThink;\r
112         self.nextthink = time;\r
113         self.touch = SUB_Null;\r
114         self.velocity = '0 0 0';\r
115         self.movetype = MOVETYPE_NONE;\r
116         self.grabber_length = -1;\r
117 }\r
118 \r
119 void GrabberThink()\r
120 {\r
121         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;\r
122         vector dir, org, end, v0, dv, v, myorg;\r
123         if(self.owner.health <= 0 || self.owner.grabber != self)        // how did that happen?\r
124         {                                                                                                               // well, better fix it anyway\r
125                 remove(self);\r
126                 return;\r
127         }\r
128         if(LostMovetypeFollow(self))\r
129         {\r
130                 RemoveGrabber(self.owner);\r
131                 return;\r
132         }\r
133         if(self.aiment)\r
134                 WarpZone_RefSys_AddIncrementally(self, self.aiment);\r
135 \r
136         // prevent the grabber from sticking to a player that has been swallowed\r
137         if(self.aiment.stat_eaten)\r
138         {\r
139                 // if the grabber is linked to a player that we swallowed, disconnect it (or the grabber will stick to us / the player inside us)\r
140                 // otherwise, link the grabber to the player who ate our linked player\r
141                 if(self.aiment.predator != self.owner)\r
142                         SetMovetypeFollow(self, self.aiment.predator);\r
143                 else\r
144                         RemoveGrabber(self.owner);\r
145         }\r
146 \r
147         self.nextthink = time;\r
148 \r
149         makevectors(self.owner.v_angle);\r
150         org = self.owner.origin + self.owner.view_ofs + v_forward * grabber_shotorigin_x + v_right * grabber_shotorigin_y + v_up * grabber_shotorigin_z;\r
151         myorg = WarpZone_RefSys_TransformOrigin(self.owner, self, org);\r
152 \r
153         if(self.grabber_length < 0)\r
154                 self.grabber_length = vlen(myorg - self.origin);\r
155 \r
156         if(self.state == 1)\r
157         {\r
158                 pullspeed = cvar("g_balance_grabber_speed_pull");//2000;\r
159                 // speed the rope is pulled with\r
160 \r
161                 rubberforce = cvar("g_balance_grabber_force_rubber");//2000;\r
162                 // force the rope will use if it is stretched\r
163 \r
164                 rubberforce_overstretch = cvar("g_balance_grabber_force_rubber_overstretch");//1000;\r
165                 // force the rope will use if it is stretched\r
166 \r
167                 minlength = cvar("g_balance_grabber_length_min");//100;\r
168                 // minimal rope length\r
169                 // if the rope goes below this length, it isn't pulled any more\r
170 \r
171                 ropestretch = cvar("g_balance_grabber_stretch");//400;\r
172                 // if the rope is stretched by more than this amount, more rope is\r
173                 // given to you again\r
174 \r
175                 ropeairfriction = cvar("g_balance_grabber_airfriction");//0.2\r
176                 // while hanging on the rope, this friction component will help you a\r
177                 // bit to control the rope\r
178 \r
179                 dir = self.origin - myorg;\r
180                 dist = vlen(dir);\r
181                 dir = normalize(dir);\r
182 \r
183                 if(cvar("g_grabber_tarzan"))\r
184                 {\r
185                         v = v0 = WarpZone_RefSys_TransformVelocity(self.owner, self, self.owner.velocity);\r
186 \r
187                         // first pull the rope...\r
188                         if(self.owner.grabber_state & GRABBER_PULLING)\r
189                         {\r
190                                 newlength = self.grabber_length;\r
191                                 newlength = max(newlength - pullspeed * frametime, minlength);\r
192 \r
193                                 if(newlength < dist - ropestretch) // overstretched?\r
194                                 {\r
195                                         newlength = dist - ropestretch;\r
196                                         if(v * dir < 0) // only if not already moving in grabber direction\r
197                                                 v = v + frametime * dir * rubberforce_overstretch;\r
198                                 }\r
199 \r
200                                 self.grabber_length = newlength;\r
201                         }\r
202 \r
203                         if(self.owner.grabber_state & GRABBER_RELEASING)\r
204                         {\r
205                                 newlength = dist;\r
206                                 self.grabber_length = newlength;\r
207                         }\r
208                         else\r
209                         {\r
210                                 // then pull the player\r
211                                 spd = bound(0, (dist - self.grabber_length) / ropestretch, 1);\r
212                                 v = v * (1 - frametime * ropeairfriction);\r
213                                 v = v + frametime * dir * spd * rubberforce;\r
214 \r
215                                 dv = ((v - v0) * dir) * dir;\r
216                                 if(cvar("g_grabber_tarzan") >= 2)\r
217                                 {\r
218                                         if(self.aiment.movetype == MOVETYPE_WALK)\r
219                                         {\r
220                                                 v = v - dv * 0.5;\r
221                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;\r
222                                                 self.aiment.flags &~= FL_ONGROUND;\r
223                                                 self.aiment.pusher = self.owner;\r
224                                                 self.aiment.pushltime = time + cvar("g_maxpushtime");\r
225                                         }\r
226                                 }\r
227 \r
228                                 self.owner.flags &~= FL_ONGROUND;\r
229                         }\r
230 \r
231                         self.owner.velocity = WarpZone_RefSys_TransformVelocity(self, self.owner, v);\r
232                 }\r
233                 else\r
234                 {\r
235                         end = self.origin - dir*50;\r
236                         dist = vlen(end - myorg);\r
237                         if(dist < 200)\r
238                                 spd = dist * (pullspeed / 200);\r
239                         else\r
240                                 spd = pullspeed;\r
241                         if(spd < 50)\r
242                                 spd = 0;\r
243                         self.owner.velocity = dir*spd;\r
244                         self.owner.movetype = MOVETYPE_FLY;\r
245 \r
246                         self.owner.flags &~= FL_ONGROUND;\r
247                 }\r
248         }\r
249 \r
250         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');\r
251         te_beam(self.owner, WarpZone_RefSys_TransformOrigin(self, self.owner, self.origin) + v_forward * (-9), org);\r
252 }\r
253 \r
254 void GrabberTouch (void)\r
255 {\r
256         if(SUB_OwnerCheck())\r
257                 return;\r
258         if(SUB_NoImpactCheck())\r
259         {\r
260                 RemoveGrabber(self.owner);\r
261                 return;\r
262         }\r
263         PROJECTILE_TOUCH;\r
264 \r
265         Grabber_Stop();\r
266 \r
267         if(other)\r
268                 if(other.movetype != MOVETYPE_NONE)\r
269                 {\r
270                         if(other.classname == "player")\r
271                         {\r
272                                 if(other.BUTTON_CHAT)\r
273                                         self.owner.typehitsound += 1;\r
274                                 else\r
275                                         self.owner.hitsound += 1; // play this for team mates too, as we could be grabbing them to heal them\r
276                                 W_Grabber_UpdateStats(self.owner, FALSE, TRUE); // count this as a hit\r
277                         }\r
278 \r
279                         SetMovetypeFollow(self, other);\r
280                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);\r
281                 }\r
282 \r
283         //self.owner.disableclientprediction = TRUE;\r
284 }\r
285 \r
286 void Grabber_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)\r
287 {\r
288         if(self.health > 0)\r
289         {\r
290                 self.health = self.health - damage;\r
291                 if (self.health <= 0)\r
292                 {\r
293                         if(attacker != self.owner)\r
294                         {\r
295                                 self.owner.pusher = attacker;\r
296                                 self.owner.pushltime = time + cvar("g_maxpushtime");\r
297                         }\r
298                         RemoveGrabber(self.owner);\r
299                 }\r
300         }\r
301 }\r
302 \r
303 void FireGrabber (void)\r
304 {\r
305         local entity missile;\r
306         local vector org;\r
307 \r
308         W_Grabber_UpdateStats(self, TRUE, FALSE);\r
309 \r
310         if((arena_roundbased && time < warmup) || (time < game_starttime))\r
311                 return;\r
312 \r
313         makevectors(self.v_angle);\r
314 \r
315         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds\r
316         sound (self, CHAN_WEAPON2, "weapons/grabber_fire.wav", VOL_BASE, ATTN_NORM);\r
317         org = self.origin + self.view_ofs + v_forward * grabber_shotorigin_x + v_right * grabber_shotorigin_y + v_up * grabber_shotorigin_z;\r
318         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);\r
319 \r
320         missile = WarpZone_RefSys_SpawnSameRefSys(self);\r
321         missile.owner = self;\r
322         self.grabber = missile;\r
323         missile.classname = "grabber";\r
324 \r
325         missile.movetype = MOVETYPE_FLY;\r
326         PROJECTILE_MAKETRIGGER(missile);\r
327 \r
328         setmodel (missile, "models/grabber.md3"); // precision set below\r
329         setsize (missile, '-3 -3 -3', '3 3 3');\r
330         setorigin (missile, org);\r
331 \r
332         missile.state = 0; // not latched onto anything\r
333 \r
334         W_SetupProjectileVelocityEx(missile, v_forward, v_up, cvar("g_balance_grabber_speed_fly"), 0, 0, 0);\r
335 \r
336         missile.angles = vectoangles (missile.velocity);\r
337         //missile.glow_color = 250; // 244, 250\r
338         //missile.glow_size = 120;\r
339         missile.touch =GrabberTouch;\r
340         missile.think =GrabberThink;\r
341         missile.nextthink = time + 0.1;\r
342 \r
343         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;\r
344 \r
345         missile.health = cvar("g_balance_grabber_health");//120\r
346         missile.event_damage =Grabber_Damage;\r
347         missile.takedamage = DAMAGE_AIM;\r
348         missile.damageforcescale = 0;\r
349 }\r
350 \r
351 void GrabberFrame()\r
352 {\r
353         if(timeoutStatus != 2 && self.weapon != WEP_GRABBER)\r
354         {\r
355                 self.grabber_state |= GRABBER_REMOVING;\r
356                 self.grabber_state &~= GRABBER_WAITING_FOR_RELEASE;\r
357 \r
358                 self.grabber_state &~= GRABBER_RELEASING;\r
359                 self.grabber_state |= GRABBER_PULLING;\r
360         }\r
361 \r
362         if(self.weapon != WEP_GRABBER)\r
363         {\r
364                 self.grabber_state &~= GRABBER_FIRING;\r
365                 self.grabber_state |= GRABBER_REMOVING;\r
366         }\r
367 \r
368         if (self.grabber_state & GRABBER_FIRING)\r
369         {\r
370                 if (self.grabber)\r
371                         RemoveGrabber(self);\r
372                 FireGrabber();\r
373                 self.grabber_state &~= GRABBER_FIRING;\r
374         }\r
375         else if(self.grabber_state & GRABBER_REMOVING)\r
376         {\r
377                 if (self.grabber)\r
378                         RemoveGrabber(self);\r
379                 self.grabber_state &~= GRABBER_REMOVING;\r
380         }\r
381 \r
382         /*\r
383         // if I have no grabber or it's not pulling yet, make sure I'm not flying!\r
384         if((self.grabber == world || !self.grabber.state) && self.movetype == MOVETYPE_FLY)\r
385         {\r
386                 self.movetype = MOVETYPE_WALK;\r
387         }\r
388         if(self.impulse == GRABBER_FIRE && self.grabber_time <= time)\r
389         {\r
390                 // fire grabber\r
391                 FireGrabber();\r
392                 return;\r
393         }\r
394         else if(self.grabberimpulse == GRABBER_RELEASE)\r
395         {\r
396                 // remove grabber, reset movement type\r
397                 RemoveGrabber(self);\r
398                 return;\r
399         }\r
400         */\r
401         /*else // make sure the player's movetype is correct\r
402         {\r
403                 //if(self.grabber == world && self.movetype == MOVETYPE_FLY)\r
404                 if((self.grabber == world || !self.grabber.state) && self.movetype == MOVETYPE_FLY)\r
405                 {\r
406                         self.movetype = MOVETYPE_WALK;\r
407                 }\r
408         }*/\r
409         // note: The grabber entity does the actual pulling\r
410 }\r
411 \r
412 void GrabberInit()\r
413 {\r
414         grabber_shotorigin = shotorg_adjust('26.2148 9.2059 -15.9772', TRUE, FALSE);\r
415 }\r
416 \r
417 void SetGrabberBindings()\r
418 {\r
419         // this function has been modified for Voretournament\r
420         // don't remove these lines! old server or demos coud overwrite the new aliases\r
421         stuffcmd(self, "alias +grabber +button6\n");\r
422         stuffcmd(self, "alias -grabber -button6\n");\r
423 }\r