Fix extra life powerup giving infinite lives in lms & minstagib
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_minelayer.qc
1 #ifdef REGISTER_WEAPON
2 REGISTER_WEAPON(MINE_LAYER, w_minelayer, IT_ROCKETS, 4, WEP_FLAG_MUTATORBLOCKED | WEP_FLAG_RELOADABLE | WEP_TYPE_SPLASH, BOT_PICKUP_RATING_HIGH, "minelayer", "minelayer", _("Mine Layer"))
3 #else
4 #ifdef SVQC
5 void W_Mine_Think (void);
6 .float minelayer_detonate, mine_explodeanyway;
7 .float mine_time;
8 .vector mine_orientation;
9
10 void spawnfunc_weapon_minelayer (void)
11 {
12         weapon_defaultspawnfunc(WEP_MINE_LAYER);
13 }
14
15 void W_Mine_Stick (entity to)
16 {
17         spamsound (self, CH_SHOTS, "weapons/mine_stick.wav", VOL_BASE, ATTN_NORM);
18
19         // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
20
21         entity newmine;
22         newmine = spawn();
23         newmine.classname = self.classname;
24
25         newmine.bot_dodge = self.bot_dodge;
26         newmine.bot_dodgerating = self.bot_dodgerating;
27
28         newmine.owner = self.owner;
29         newmine.realowner = self.realowner;
30         setsize(newmine, '-4 -4 -4', '4 4 4');
31         setorigin(newmine, self.origin);
32         setmodel(newmine, "models/mine.md3");
33         newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
34
35         newmine.mine_orientation = -trace_plane_normal;
36
37         newmine.takedamage = self.takedamage;
38         newmine.damageforcescale = self.damageforcescale;
39         newmine.health = self.health;
40         newmine.event_damage = self.event_damage;
41         newmine.spawnshieldtime = self.spawnshieldtime;
42         newmine.damagedbycontents = TRUE;
43
44         newmine.movetype = MOVETYPE_NONE; // lock the mine in place
45         newmine.projectiledeathtype = self.projectiledeathtype;
46
47         newmine.mine_time = self.mine_time;
48
49         newmine.touch = func_null;
50         newmine.think = W_Mine_Think;
51         newmine.nextthink = time;
52         newmine.cnt = self.cnt;
53         newmine.flags = self.flags;
54
55         remove(self);
56         self = newmine;
57
58         if(to)
59                 SetMovetypeFollow(self, to);
60 }
61
62 void W_Mine_Explode ()
63 {
64         if(other.takedamage == DAMAGE_AIM)
65                 if(other.classname == "player")
66                         if(IsDifferentTeam(self.realowner, other))
67                                 if(other.deadflag == DEAD_NO)
68                                         if(IsFlying(other))
69                                                 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
70
71         self.event_damage = func_null;
72         self.takedamage = DAMAGE_NO;
73
74         RadiusDamage (self, self.realowner, autocvar_g_balance_minelayer_damage, autocvar_g_balance_minelayer_edgedamage, autocvar_g_balance_minelayer_radius, world, autocvar_g_balance_minelayer_force, self.projectiledeathtype, other);
75
76         if (self.realowner.weapon == WEP_MINE_LAYER)
77         {
78                 entity oldself;
79                 oldself = self;
80                 self = self.realowner;
81                 if (!weapon_action(WEP_MINE_LAYER, WR_CHECKAMMO1))
82                 {
83                         self.cnt = WEP_MINE_LAYER;
84                         ATTACK_FINISHED(self) = time;
85                         self.switchweapon = w_getbestweapon(self);
86                 }
87                 self = oldself;
88         }
89         self.realowner.minelayer_mines -= 1;
90         remove (self);
91 }
92
93 void W_Mine_DoRemoteExplode ()
94 {
95         self.event_damage = func_null;
96         self.takedamage = DAMAGE_NO;
97
98         if(self.movetype == MOVETYPE_NONE || self.movetype == MOVETYPE_FOLLOW)
99                 self.velocity = self.mine_orientation; // particle fx and decals need .velocity
100
101         RadiusDamage (self, self.realowner, autocvar_g_balance_minelayer_remote_damage, autocvar_g_balance_minelayer_remote_edgedamage, autocvar_g_balance_minelayer_remote_radius, world, autocvar_g_balance_minelayer_remote_force, self.projectiledeathtype | HITTYPE_BOUNCE, world);
102
103         if (self.realowner.weapon == WEP_MINE_LAYER)
104         {
105                 entity oldself;
106                 oldself = self;
107                 self = self.realowner;
108                 if (!weapon_action(WEP_MINE_LAYER, WR_CHECKAMMO1))
109                 {
110                         self.cnt = WEP_MINE_LAYER;
111                         ATTACK_FINISHED(self) = time;
112                         self.switchweapon = w_getbestweapon(self);
113                 }
114                 self = oldself;
115         }
116         self.realowner.minelayer_mines -= 1;
117         remove (self);
118 }
119
120 void W_Mine_RemoteExplode ()
121 {
122         if(self.realowner.deadflag == DEAD_NO)
123                 if((self.spawnshieldtime >= 0)
124                         ? (time >= self.spawnshieldtime) // timer
125                         : (vlen(NearestPointOnBox(self.realowner, self.origin) - self.origin) > autocvar_g_balance_minelayer_remote_radius) // safety device
126                 )
127                 {
128                         W_Mine_DoRemoteExplode();
129                 }
130 }
131
132 void W_Mine_ProximityExplode ()
133 {
134         // make sure no friend is in the mine's radius. If there is any, explosion is delayed until he's at a safe distance
135         if(autocvar_g_balance_minelayer_protection && self.mine_explodeanyway == 0)
136         {
137                 entity head;
138                 head = findradius(self.origin, autocvar_g_balance_minelayer_radius);
139                 while(head)
140                 {
141                         if(head == self.realowner || !IsDifferentTeam(head, self.realowner))
142                                 return;
143                         head = head.chain;
144                 }
145         }
146
147         self.mine_time = 0;
148         W_Mine_Explode();
149 }
150
151 float W_Mine_Count(entity e)
152 {
153         float minecount = 0;
154         entity mine;
155         for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.realowner == e)
156                 minecount += 1;
157
158         return minecount;
159 }
160
161 void W_Mine_Think (void)
162 {
163         entity head;
164
165         self.nextthink = time;
166
167         if(self.movetype == MOVETYPE_FOLLOW)
168         {
169                 if(LostMovetypeFollow(self))
170                 {
171                         UnsetMovetypeFollow(self);
172                         self.movetype = MOVETYPE_NONE;
173                 }
174         }
175         
176         // our lifetime has expired, it's time to die - mine_time just allows us to play a sound for this
177         // TODO: replace this mine_trigger.wav sound with a real countdown
178         if ((time > self.cnt) && (!self.mine_time))
179         {
180                 if(autocvar_g_balance_minelayer_lifetime_countdown > 0)
181                         spamsound (self, CH_SHOTS, "weapons/mine_trigger.wav", VOL_BASE, ATTN_NORM);
182                 self.mine_time = time + autocvar_g_balance_minelayer_lifetime_countdown;
183                 self.mine_explodeanyway = 1; // make the mine super aggressive -- Samual: Rather, make it not care if a team mate is near.
184         }
185
186         // a player's mines shall explode if he disconnects or dies
187         // TODO: Do this on team change too -- Samual: But isn't a player killed when they switch teams?
188         if(self.realowner.classname != "player" || self.realowner.deadflag != DEAD_NO)
189         {
190                 other = world;
191                 self.projectiledeathtype |= HITTYPE_BOUNCE;
192                 W_Mine_Explode();
193                 return;
194         }
195
196         // set the mine for detonation when a foe gets close enough
197         head = findradius(self.origin, autocvar_g_balance_minelayer_proximityradius);
198         while(head)
199         {
200                 if(head.classname == "player" && head.deadflag == DEAD_NO)
201                 if(head != self.realowner && IsDifferentTeam(head, self.realowner)) // don't trigger for team mates
202                 if(!self.mine_time)
203                 {
204                         spamsound (self, CH_SHOTS, "weapons/mine_trigger.wav", VOL_BASE, ATTN_NORM);
205                         self.mine_time = time + autocvar_g_balance_minelayer_time;
206                 }
207                 head = head.chain;
208         }
209
210         // explode if it's time to
211         if(self.mine_time && time >= self.mine_time)
212         {
213                 W_Mine_ProximityExplode();
214                 return;
215         }
216
217         // remote detonation
218         if (self.realowner.weapon == WEP_MINE_LAYER)
219         if (self.realowner.deadflag == DEAD_NO)
220         if (self.minelayer_detonate)
221                 W_Mine_RemoteExplode();
222 }
223
224 void W_Mine_Touch (void)
225 {
226         if(self.movetype == MOVETYPE_NONE || self.movetype == MOVETYPE_FOLLOW)
227                 return; // we're already a stuck mine, why do we get called? TODO does this even happen?
228
229         if(WarpZone_Projectile_Touch())
230         {
231                 if(wasfreed(self))
232                         self.realowner.minelayer_mines -= 1;
233                 return;
234         }
235
236         if(other && other.classname == "player" && other.deadflag == DEAD_NO)
237         {
238                 // hit a player
239                 // don't stick
240         }
241         else
242         {
243                 W_Mine_Stick(other);
244         }
245 }
246
247 void W_Mine_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
248 {
249         if (self.health <= 0)
250                 return;
251                 
252         float is_from_enemy = (inflictor.realowner != self.realowner);
253                 
254         if (!W_CheckProjectileDamage(inflictor.realowner, self.realowner, deathtype, (is_from_enemy ? 1 : -1)))
255                 return; // g_projectiles_damage says to halt
256                 
257         self.health = self.health - damage;
258         self.angles = vectoangles(self.velocity);
259         
260         if (self.health <= 0)
261                 W_PrepareExplosionByDamage(attacker, W_Mine_Explode);
262 }
263
264 void W_Mine_Attack (void)
265 {
266         entity mine;
267         entity flash;
268
269         // scan how many mines we placed, and return if we reached our limit
270         if(autocvar_g_balance_minelayer_limit)
271         {
272                 if(self.minelayer_mines >= autocvar_g_balance_minelayer_limit)
273                 {
274                         // the refire delay keeps this message from being spammed
275                         sprint(self, strcat("minelayer: You cannot place more than ^2", ftos(autocvar_g_balance_minelayer_limit), " ^7mines at a time\n") );
276                         play2(self, "weapons/unavailable.wav");
277                         return;
278                 }
279         }
280
281         W_DecreaseAmmo(ammo_rockets, autocvar_g_balance_minelayer_ammo, autocvar_g_balance_minelayer_reload_ammo);
282
283         W_SetupShot_ProjectileSize (self, '-4 -4 -4', '4 4 4', FALSE, 5, "weapons/mine_fire.wav", CH_WEAPON_A, autocvar_g_balance_minelayer_damage);
284         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
285
286         mine = WarpZone_RefSys_SpawnSameRefSys(self);
287         mine.owner = mine.realowner = self;
288         if(autocvar_g_balance_minelayer_detonatedelay >= 0)
289                 mine.spawnshieldtime = time + autocvar_g_balance_minelayer_detonatedelay;
290         else
291                 mine.spawnshieldtime = -1;
292         mine.classname = "mine";
293         mine.bot_dodge = TRUE;
294         mine.bot_dodgerating = autocvar_g_balance_minelayer_damage * 2; // * 2 because it can detonate inflight which makes it even more dangerous
295
296         mine.takedamage = DAMAGE_YES;
297         mine.damageforcescale = autocvar_g_balance_minelayer_damageforcescale;
298         mine.health = autocvar_g_balance_minelayer_health;
299         mine.event_damage = W_Mine_Damage;
300         mine.damagedbycontents = TRUE;
301
302         mine.movetype = MOVETYPE_TOSS;
303         PROJECTILE_MAKETRIGGER(mine);
304         mine.projectiledeathtype = WEP_MINE_LAYER;
305         setsize (mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
306
307         setorigin (mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
308         W_SetupProjectileVelocity(mine, autocvar_g_balance_minelayer_speed, 0);
309         mine.angles = vectoangles (mine.velocity);
310
311         mine.touch = W_Mine_Touch;
312         mine.think = W_Mine_Think;
313         mine.nextthink = time;
314         mine.cnt = time + (autocvar_g_balance_minelayer_lifetime - autocvar_g_balance_minelayer_lifetime_countdown);
315         mine.flags = FL_PROJECTILE;
316         mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
317
318         CSQCProjectile(mine, TRUE, PROJECTILE_MINE, TRUE);
319
320         // muzzle flash for 1st person view
321         flash = spawn ();
322         setmodel (flash, "models/flash.md3"); // precision set below
323         SUB_SetFade (flash, time, 0.1);
324         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
325         W_AttachToShotorg(flash, '5 0 0');
326
327         // common properties
328
329         other = mine; MUTATOR_CALLHOOK(EditProjectile);
330         
331         self.minelayer_mines = W_Mine_Count(self);
332 }
333
334 float W_PlacedMines(float detonate)
335 {
336         entity mine;
337         float minfound = 0;
338
339         for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.realowner == self)
340         {
341                 if(detonate)
342                 {
343                         if(!mine.minelayer_detonate)
344                         {
345                                 mine.minelayer_detonate = TRUE;
346                                 minfound = 1;
347                         }
348                 }
349                 else
350                         minfound = 1;
351         }
352         return minfound;
353 }
354
355 float w_minelayer(float req)
356 {
357         entity mine;
358         float ammo_amount;
359
360         if (req == WR_AIM)
361         {
362                 // aim and decide to fire if appropriate
363                 if(self.minelayer_mines >= autocvar_g_balance_minelayer_limit)
364                         self.BUTTON_ATCK = FALSE;
365                 else
366                         self.BUTTON_ATCK = bot_aim(autocvar_g_balance_minelayer_speed, 0, autocvar_g_balance_minelayer_lifetime, FALSE);
367                 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
368                 {
369                         // decide whether to detonate mines
370                         entity targetlist, targ;
371                         float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
372                         float selfdamage, teamdamage, enemydamage;
373                         edgedamage = autocvar_g_balance_minelayer_edgedamage;
374                         coredamage = autocvar_g_balance_minelayer_damage;
375                         edgeradius = autocvar_g_balance_minelayer_radius;
376                         recipricoledgeradius = 1 / edgeradius;
377                         selfdamage = 0;
378                         teamdamage = 0;
379                         enemydamage = 0;
380                         targetlist = findchainfloat(bot_attack, TRUE);
381                         mine = find(world, classname, "mine");
382                         while (mine)
383                         {
384                                 if (mine.realowner != self)
385                                 {
386                                         mine = find(mine, classname, "mine");
387                                         continue;
388                                 }
389                                 targ = targetlist;
390                                 while (targ)
391                                 {
392                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - mine.origin);
393                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
394                                         // count potential damage according to type of target
395                                         if (targ == self)
396                                                 selfdamage = selfdamage + d;
397                                         else if (targ.team == self.team && teamplay)
398                                                 teamdamage = teamdamage + d;
399                                         else if (bot_shouldattack(targ))
400                                                 enemydamage = enemydamage + d;
401                                         targ = targ.chain;
402                                 }
403                                 mine = find(mine, classname, "mine");
404                         }
405                         float desirabledamage;
406                         desirabledamage = enemydamage;
407                         if (time > self.invincible_finished && time > self.spawnshieldtime)
408                                 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
409                         if (teamplay && self.team)
410                                 desirabledamage = desirabledamage - teamdamage;
411
412                         mine = find(world, classname, "mine");
413                         while (mine)
414                         {
415                                 if (mine.realowner != self)
416                                 {
417                                         mine = find(mine, classname, "mine");
418                                         continue;
419                                 }
420                                 makevectors(mine.v_angle);
421                                 targ = targetlist;
422                                 if (skill > 9) // normal players only do this for the target they are tracking
423                                 {
424                                         targ = targetlist;
425                                         while (targ)
426                                         {
427                                                 if (
428                                                         (v_forward * normalize(mine.origin - targ.origin)< 0.1)
429                                                         && desirabledamage > 0.1*coredamage
430                                                 )self.BUTTON_ATCK2 = TRUE;
431                                                 targ = targ.chain;
432                                         }
433                                 }else{
434                                         float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
435                                         //As the distance gets larger, a correct detonation gets near imposible
436                                         //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
437                                         if(v_forward * normalize(mine.origin - self.enemy.origin)< 0.1)
438                                                 if(self.enemy.classname == "player")
439                                                         if(desirabledamage >= 0.1*coredamage)
440                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
441                                                                         self.BUTTON_ATCK2 = TRUE;
442                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
443                                 }
444
445                                 mine = find(mine, classname, "mine");
446                         }
447                         // if we would be doing at X percent of the core damage, detonate it
448                         // but don't fire a new shot at the same time!
449                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
450                                 self.BUTTON_ATCK2 = TRUE;
451                         if ((skill > 6.5) && (selfdamage > self.health))
452                                 self.BUTTON_ATCK2 = FALSE;
453                         //if(self.BUTTON_ATCK2 == TRUE)
454                         //      dprint(ftos(desirabledamage),"\n");
455                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
456                 }
457         }
458         else if (req == WR_THINK)
459         {
460                 if(autocvar_g_balance_minelayer_reload_ammo && self.clip_load < autocvar_g_balance_minelayer_ammo) // forced reload
461                 {
462                         // not if we're holding the minelayer without enough ammo, but can detonate existing mines
463                         if not (W_PlacedMines(FALSE) && self.ammo_rockets < autocvar_g_balance_minelayer_ammo)
464                                 weapon_action(self.weapon, WR_RELOAD);
465                 }
466                 else if (self.BUTTON_ATCK)
467                 {
468                         if(weapon_prepareattack(0, autocvar_g_balance_minelayer_refire))
469                         {
470                                 W_Mine_Attack();
471                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_minelayer_animtime, w_ready);
472                         }
473                 }
474
475                 if (self.BUTTON_ATCK2)
476                 {
477                         if(W_PlacedMines(TRUE))
478                                 sound (self, CH_WEAPON_B, "weapons/mine_det.wav", VOL_BASE, ATTN_NORM);
479                 }
480         }
481         else if (req == WR_PRECACHE)
482         {
483                 precache_model ("models/flash.md3");
484                 precache_model ("models/mine.md3");
485                 precache_model ("models/weapons/g_minelayer.md3");
486                 precache_model ("models/weapons/v_minelayer.md3");
487                 precache_model ("models/weapons/h_minelayer.iqm");
488                 precache_sound ("weapons/mine_det.wav");
489                 precache_sound ("weapons/mine_fire.wav");
490                 precache_sound ("weapons/mine_stick.wav");
491                 precache_sound ("weapons/mine_trigger.wav");
492                 //precache_sound ("weapons/reload.wav"); // until weapons have individual reload sounds, precache the reload sound somewhere else
493         }
494         else if (req == WR_SETUP)
495         {
496                 weapon_setup(WEP_MINE_LAYER);
497                 self.current_ammo = ammo_rockets;
498         }
499         else if (req == WR_CHECKAMMO1)
500         {
501                 // don't switch while placing a mine
502                 if (ATTACK_FINISHED(self) <= time || self.weapon != WEP_MINE_LAYER)
503                 {
504                         ammo_amount = self.ammo_rockets >= autocvar_g_balance_minelayer_ammo;
505                         ammo_amount += self.(weapon_load[WEP_MINE_LAYER]) >= autocvar_g_balance_minelayer_ammo;
506                         return ammo_amount;
507                 }
508         }
509         else if (req == WR_CHECKAMMO2)
510         {
511                 if (W_PlacedMines(FALSE))
512                         return TRUE;
513                 else
514                         return FALSE;
515         }
516         else if (req == WR_RESETPLAYER)
517         {
518                 self.minelayer_mines = 0;
519         }
520         else if (req == WR_RELOAD)
521         {
522                 W_Reload(autocvar_g_balance_minelayer_ammo, autocvar_g_balance_minelayer_reload_ammo, autocvar_g_balance_minelayer_reload_time, "weapons/reload.wav");
523         }
524         else if (req == WR_SUICIDEMESSAGE)
525         {
526                 return WEAPON_MINELAYER_SUICIDE;
527         }
528         else if (req == WR_KILLMESSAGE)
529         {
530                 return WEAPON_MINELAYER_MURDER;
531         }
532         return TRUE;
533 }
534 #endif
535 #ifdef CSQC
536 float w_minelayer(float req)
537 {
538         if(req == WR_IMPACTEFFECT)
539         {
540                 vector org2;
541                 org2 = w_org + w_backoff * 12;
542                 pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
543                 if(!w_issilent)
544                         sound(self, CH_SHOTS, "weapons/mine_exp.wav", VOL_BASE, ATTN_NORM);
545         }
546         else if(req == WR_PRECACHE)
547         {
548                 precache_sound("weapons/mine_exp.wav");
549         }
550         return TRUE;
551 }
552 #endif
553 #endif