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