f8ce6c0545c468fc2fd152f1e01217620701a494
[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         sound (self, CHAN_PROJECTILE, "weapons/grabber_impact.wav", VOL_BASE, ATTN_NORM);\r
106 \r
107         self.state = 1;\r
108         self.think =GrabberThink;\r
109         self.nextthink = time;\r
110         self.touch = SUB_Null;\r
111         self.velocity = '0 0 0';\r
112         self.movetype = MOVETYPE_NONE;\r
113         self.grabber_length = -1;\r
114 }\r
115 \r
116 void GrabberThink()\r
117 {\r
118         float spd, dist, minlength, pullspeed, ropestretch, ropeairfriction, rubberforce, newlength, rubberforce_overstretch;\r
119         vector dir, org, end, v0, dv, v, myorg;\r
120         if(self.owner.health <= 0 || self.owner.grabber != self)        // how did that happen?\r
121         {                                                                                                               // well, better fix it anyway\r
122                 remove(self);\r
123                 return;\r
124         }\r
125         if(LostMovetypeFollow(self))\r
126         {\r
127                 RemoveGrabber(self.owner);\r
128                 return;\r
129         }\r
130         if(self.aiment)\r
131                 WarpZone_RefSys_AddIncrementally(self, self.aiment);\r
132 \r
133         // prevent the grabber from sticking to a player that has been swallowed\r
134         if(self.aiment.stat_eaten)\r
135         {\r
136                 // 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
137                 // otherwise, link the grabber to the player who ate our linked player\r
138                 if(self.aiment.predator != self.owner)\r
139                         SetMovetypeFollow(self, self.aiment.predator);\r
140                 else\r
141                         RemoveGrabber(self.owner);\r
142         }\r
143 \r
144         self.nextthink = time;\r
145 \r
146         makevectors(self.owner.v_angle);\r
147         org = self.owner.origin + self.owner.view_ofs + v_forward * grabber_shotorigin_x + v_right * grabber_shotorigin_y + v_up * grabber_shotorigin_z;\r
148         myorg = WarpZone_RefSys_TransformOrigin(self.owner, self, org);\r
149 \r
150         if(self.grabber_length < 0)\r
151                 self.grabber_length = vlen(myorg - self.origin);\r
152 \r
153         if(self.state == 1)\r
154         {\r
155                 pullspeed = cvar("g_balance_grabber_speed_pull");//2000;\r
156                 // speed the rope is pulled with\r
157 \r
158                 rubberforce = cvar("g_balance_grabber_force_rubber");//2000;\r
159                 // force the rope will use if it is stretched\r
160 \r
161                 rubberforce_overstretch = cvar("g_balance_grabber_force_rubber_overstretch");//1000;\r
162                 // force the rope will use if it is stretched\r
163 \r
164                 minlength = cvar("g_balance_grabber_length_min");//100;\r
165                 // minimal rope length\r
166                 // if the rope goes below this length, it isn't pulled any more\r
167 \r
168                 ropestretch = cvar("g_balance_grabber_stretch");//400;\r
169                 // if the rope is stretched by more than this amount, more rope is\r
170                 // given to you again\r
171 \r
172                 ropeairfriction = cvar("g_balance_grabber_airfriction");//0.2\r
173                 // while hanging on the rope, this friction component will help you a\r
174                 // bit to control the rope\r
175 \r
176                 dir = self.origin - myorg;\r
177                 dist = vlen(dir);\r
178                 dir = normalize(dir);\r
179 \r
180                 if(cvar("g_grabber_tarzan"))\r
181                 {\r
182                         v = v0 = WarpZone_RefSys_TransformVelocity(self.owner, self, self.owner.velocity);\r
183 \r
184                         // first pull the rope...\r
185                         if(self.owner.grabber_state & GRABBER_PULLING)\r
186                         {\r
187                                 newlength = self.grabber_length;\r
188                                 newlength = max(newlength - pullspeed * frametime, minlength);\r
189 \r
190                                 if(newlength < dist - ropestretch) // overstretched?\r
191                                 {\r
192                                         newlength = dist - ropestretch;\r
193                                         if(v * dir < 0) // only if not already moving in grabber direction\r
194                                                 v = v + frametime * dir * rubberforce_overstretch;\r
195                                 }\r
196 \r
197                                 self.grabber_length = newlength;\r
198                         }\r
199 \r
200                         if(self.owner.grabber_state & GRABBER_RELEASING)\r
201                         {\r
202                                 newlength = dist;\r
203                                 self.grabber_length = newlength;\r
204                         }\r
205                         else\r
206                         {\r
207                                 // then pull the player\r
208                                 spd = bound(0, (dist - self.grabber_length) / ropestretch, 1);\r
209                                 v = v * (1 - frametime * ropeairfriction);\r
210                                 v = v + frametime * dir * spd * rubberforce;\r
211 \r
212                                 dv = ((v - v0) * dir) * dir;\r
213                                 if(cvar("g_grabber_tarzan") >= 2)\r
214                                 {\r
215                                         if(self.aiment.movetype == MOVETYPE_WALK)\r
216                                         {\r
217                                                 v = v - dv * 0.5;\r
218                                                 self.aiment.velocity = self.aiment.velocity - dv * 0.5;\r
219                                                 self.aiment.flags &~= FL_ONGROUND;\r
220                                                 self.aiment.pusher = self.owner;\r
221                                                 self.aiment.pushltime = time + cvar("g_maxpushtime");\r
222                                         }\r
223                                 }\r
224 \r
225                                 self.owner.flags &~= FL_ONGROUND;\r
226                         }\r
227 \r
228                         self.owner.velocity = WarpZone_RefSys_TransformVelocity(self, self.owner, v);\r
229                 }\r
230                 else\r
231                 {\r
232                         end = self.origin - dir*50;\r
233                         dist = vlen(end - myorg);\r
234                         if(dist < 200)\r
235                                 spd = dist * (pullspeed / 200);\r
236                         else\r
237                                 spd = pullspeed;\r
238                         if(spd < 50)\r
239                                 spd = 0;\r
240                         self.owner.velocity = dir*spd;\r
241                         self.owner.movetype = MOVETYPE_FLY;\r
242 \r
243                         self.owner.flags &~= FL_ONGROUND;\r
244                 }\r
245         }\r
246 \r
247         makevectors(self.angles_x * '-1 0 0' + self.angles_y * '0 1 0');\r
248         te_beam(self.owner, WarpZone_RefSys_TransformOrigin(self, self.owner, self.origin) + v_forward * (-9), org);\r
249 }\r
250 \r
251 void GrabberTouch (void)\r
252 {\r
253         if(SUB_OwnerCheck())\r
254                 return;\r
255         if(SUB_NoImpactCheck())\r
256         {\r
257                 RemoveGrabber(self.owner);\r
258                 return;\r
259         }\r
260         PROJECTILE_TOUCH;\r
261 \r
262         Grabber_Stop();\r
263 \r
264         if(other)\r
265                 if(other.movetype != MOVETYPE_NONE)\r
266                 {\r
267                         if(other.classname == "player")\r
268                         {\r
269                                 if(other.BUTTON_CHAT)\r
270                                         self.owner.typehitsound += 1;\r
271                                 else\r
272                                         self.owner.hitsound += 1; // play this for team mates too, as we could be grabbing them to heal them\r
273                                 W_Grabber_UpdateStats(self.owner, FALSE, TRUE); // count this as a hit\r
274                         }\r
275 \r
276                         SetMovetypeFollow(self, other);\r
277                         WarpZone_RefSys_BeginAddingIncrementally(self, self.aiment);\r
278                 }\r
279 \r
280         //self.owner.disableclientprediction = TRUE;\r
281 }\r
282 \r
283 void Grabber_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)\r
284 {\r
285         if(self.health > 0)\r
286         {\r
287                 self.health = self.health - damage;\r
288                 if (self.health <= 0)\r
289                 {\r
290                         if(attacker != self.owner)\r
291                         {\r
292                                 self.owner.pusher = attacker;\r
293                                 self.owner.pushltime = time + cvar("g_maxpushtime");\r
294                         }\r
295                         RemoveGrabber(self.owner);\r
296                 }\r
297         }\r
298 }\r
299 \r
300 void FireGrabber (void)\r
301 {\r
302         local entity missile;\r
303         local vector org;\r
304 \r
305         W_Grabber_UpdateStats(self, TRUE, FALSE);\r
306 \r
307         if((arena_roundbased && time < warmup) || (time < game_starttime))\r
308                 return;\r
309 \r
310         makevectors(self.v_angle);\r
311 \r
312         // UGLY WORKAROUND: play this on CHAN_WEAPON2 so it can't cut off fire sounds\r
313         sound (self, CHAN_WEAPON2, "weapons/grabber_fire.wav", VOL_BASE, ATTN_NORM);\r
314         org = self.origin + self.view_ofs + v_forward * grabber_shotorigin_x + v_right * grabber_shotorigin_y + v_up * grabber_shotorigin_z;\r
315         pointparticles(particleeffectnum("grapple_muzzleflash"), org, '0 0 0', 1);\r
316 \r
317         missile = WarpZone_RefSys_SpawnSameRefSys(self);\r
318         missile.owner = self;\r
319         self.grabber = missile;\r
320         missile.classname = "grabber";\r
321 \r
322         missile.movetype = MOVETYPE_FLY;\r
323         PROJECTILE_MAKETRIGGER(missile);\r
324 \r
325         setmodel (missile, "models/grabber.md3"); // precision set below\r
326         setsize (missile, '-3 -3 -3', '3 3 3');\r
327         setorigin (missile, org);\r
328 \r
329         missile.state = 0; // not latched onto anything\r
330 \r
331         W_SetupProjectileVelocityEx(missile, v_forward, v_up, cvar("g_balance_grabber_speed_fly"), 0, 0, 0);\r
332 \r
333         missile.angles = vectoangles (missile.velocity);\r
334         //missile.glow_color = 250; // 244, 250\r
335         //missile.glow_size = 120;\r
336         missile.touch =GrabberTouch;\r
337         missile.think =GrabberThink;\r
338         missile.nextthink = time + 0.1;\r
339 \r
340         missile.effects = /*EF_FULLBRIGHT | EF_ADDITIVE |*/ EF_LOWPRECISION;\r
341 \r
342         missile.health = cvar("g_balance_grabber_health");//120\r
343         missile.event_damage =Grabber_Damage;\r
344         missile.takedamage = DAMAGE_AIM;\r
345         missile.damageforcescale = 0;\r
346 }\r
347 \r
348 void GrabberFrame()\r
349 {\r
350         if(timeoutStatus != 2 && self.weapon != WEP_GRABBER)\r
351         {\r
352                 self.grabber_state |= GRABBER_REMOVING;\r
353                 self.grabber_state &~= GRABBER_WAITING_FOR_RELEASE;\r
354 \r
355                 self.grabber_state &~= GRABBER_RELEASING;\r
356                 if(self.BUTTON_CROUCH)\r
357                 {\r
358                         self.grabber_state &~= GRABBER_PULLING;\r
359                         //self.grabber_state |= GRABBER_RELEASING;\r
360                 }\r
361                 else\r
362                 {\r
363                         self.grabber_state |= GRABBER_PULLING;\r
364                         //self.grabber_state &~= GRABBER_RELEASING;\r
365                 }\r
366         }\r
367 \r
368         if(self.weapon != WEP_GRABBER)\r
369         {\r
370                 self.grabber_state &~= GRABBER_FIRING;\r
371                 self.grabber_state |= GRABBER_REMOVING;\r
372         }\r
373 \r
374         if (self.grabber_state & GRABBER_FIRING)\r
375         {\r
376                 if (self.grabber)\r
377                         RemoveGrabber(self);\r
378                 FireGrabber();\r
379                 self.grabber_state &~= GRABBER_FIRING;\r
380         }\r
381         else if(self.grabber_state & GRABBER_REMOVING)\r
382         {\r
383                 if (self.grabber)\r
384                         RemoveGrabber(self);\r
385                 self.grabber_state &~= GRABBER_REMOVING;\r
386         }\r
387 \r
388         /*\r
389         // if I have no grabber or it's not pulling yet, make sure I'm not flying!\r
390         if((self.grabber == world || !self.grabber.state) && self.movetype == MOVETYPE_FLY)\r
391         {\r
392                 self.movetype = MOVETYPE_WALK;\r
393         }\r
394         if(self.impulse == GRABBER_FIRE && self.grabber_time <= time)\r
395         {\r
396                 // fire grabber\r
397                 FireGrabber();\r
398                 return;\r
399         }\r
400         else if(self.grabberimpulse == GRABBER_RELEASE)\r
401         {\r
402                 // remove grabber, reset movement type\r
403                 RemoveGrabber(self);\r
404                 return;\r
405         }\r
406         */\r
407         /*else // make sure the player's movetype is correct\r
408         {\r
409                 //if(self.grabber == world && self.movetype == MOVETYPE_FLY)\r
410                 if((self.grabber == world || !self.grabber.state) && self.movetype == MOVETYPE_FLY)\r
411                 {\r
412                         self.movetype = MOVETYPE_WALK;\r
413                 }\r
414         }*/\r
415         // note: The grabber entity does the actual pulling\r
416 }\r
417 \r
418 void GrabberInit()\r
419 {\r
420         grabber_shotorigin = shotorg_adjust('26.2148 9.2059 -15.9772', TRUE, FALSE);\r
421 }\r
422 \r
423 void SetGrabberBindings()\r
424 {\r
425         // this function has been modified for Voretournament\r
426         // don't remove these lines! old server or demos coud overwrite the new aliases\r
427         stuffcmd(self, "alias +grabber +button6\n");\r
428         stuffcmd(self, "alias -grabber -button6\n");\r
429 }\r