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