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