make weapon death messages translatable
[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_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, minelayer_mines;
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 ()
15 {
16         spamsound (self, CHAN_PROJECTILE, "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         setsize(newmine, '-4 -4 -4', '4 4 4');
29         setorigin(newmine, self.origin);
30         setmodel(newmine, "models/mine.md3");
31         newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
32
33         newmine.takedamage = self.takedamage;
34         newmine.damageforcescale = self.damageforcescale;
35         newmine.health = self.health;
36         newmine.event_damage = self.event_damage;
37         newmine.spawnshieldtime = self.spawnshieldtime;
38
39         newmine.movetype = MOVETYPE_NONE; // lock the mine in place
40         newmine.projectiledeathtype = self.projectiledeathtype;
41
42         newmine.mine_time = self.mine_time;
43
44         newmine.touch = SUB_Null;
45         newmine.think = W_Mine_Think;
46         newmine.nextthink = time;
47         newmine.cnt = self.cnt;
48         newmine.flags = self.flags;
49
50         remove(self);
51         self = newmine;
52 }
53
54 void W_Mine_Explode ()
55 {
56         if(other.takedamage == DAMAGE_AIM)
57                 if(other.classname == "player")
58                         if(IsDifferentTeam(self.owner, other))
59                                 if(IsFlying(other))
60                                         AnnounceTo(self.owner, "airshot");
61
62         self.event_damage = SUB_Null;
63         self.takedamage = DAMAGE_NO;
64
65         RadiusDamage (self, self.owner, autocvar_g_balance_minelayer_damage, autocvar_g_balance_minelayer_edgedamage, autocvar_g_balance_minelayer_radius, world, autocvar_g_balance_minelayer_force, self.projectiledeathtype, other);
66
67         if (self.owner.weapon == WEP_MINE_LAYER)
68         {
69                 if(self.owner.ammo_rockets < autocvar_g_balance_minelayer_ammo)
70                 {
71                         self.owner.cnt = WEP_MINE_LAYER;
72                         ATTACK_FINISHED(self.owner) = time;
73                         self.owner.switchweapon = w_getbestweapon(self.owner);
74                 }
75         }
76         remove (self);
77 }
78
79 void W_Mine_DoRemoteExplode ()
80 {
81         self.event_damage = SUB_Null;
82         self.takedamage = DAMAGE_NO;
83
84         RadiusDamage (self, self.owner, 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);
85
86         if (self.owner.weapon == WEP_MINE_LAYER)
87         {
88                 if(self.owner.ammo_rockets < autocvar_g_balance_minelayer_ammo)
89                 {
90                         self.owner.cnt = WEP_MINE_LAYER;
91                         ATTACK_FINISHED(self.owner) = time;
92                         self.owner.switchweapon = w_getbestweapon(self.owner);
93                 }
94         }
95         remove (self);
96 }
97
98 void W_Mine_RemoteExplode ()
99 {
100         if(self.owner.deadflag == DEAD_NO)
101                 if((self.spawnshieldtime >= 0)
102                         ? (time >= self.spawnshieldtime) // timer
103                         : (vlen(NearestPointOnBox(self.owner, self.origin) - self.origin) > autocvar_g_balance_minelayer_remote_radius) // safety device
104                 )
105                 {
106                         W_Mine_DoRemoteExplode();
107                 }
108 }
109
110 void W_Mine_ProximityExplode ()
111 {
112         // make sure no friend is in the mine's radius. If there is any, explosion is delayed until he's at a safe distance
113         if(autocvar_g_balance_minelayer_protection)
114         {
115                 entity head;
116                 head = findradius(self.origin, autocvar_g_balance_minelayer_radius);
117                 while(head)
118                 {
119                         if(head == self.owner || !IsDifferentTeam(head, self.owner))
120                                 return;
121                         head = head.chain;
122                 }
123         }
124
125         self.mine_time = 0;
126         W_Mine_Explode();
127 }
128
129 void W_Mine_Think (void)
130 {
131         entity head;
132
133         self.nextthink = time;
134         if (time > self.cnt)
135         {
136                 other = world;
137                 self.projectiledeathtype |= HITTYPE_BOUNCE;
138                 W_Mine_Explode();
139                 return;
140         }
141
142         // a player's mines shall explode if he disconnects or dies
143         // TODO: Do this on team change too
144         if(self.owner.classname != "player" || self.owner.deadflag != DEAD_NO)
145         {
146                 other = world;
147                 self.projectiledeathtype |= HITTYPE_BOUNCE;
148                 W_Mine_Explode();
149                 return;
150         }
151
152         // set the mine for detonation when a foe gets close enough
153         head = findradius(self.origin, autocvar_g_balance_minelayer_proximityradius);
154         while(head)
155         {
156                 if(head.classname == "player" && head.deadflag == DEAD_NO)
157                 if(head != self.owner && IsDifferentTeam(head, self.owner)) // don't trigger for team mates
158                 if(!self.mine_time)
159                 {
160                         spamsound (self, CHAN_PROJECTILE, "weapons/mine_trigger.wav", VOL_BASE, ATTN_NORM);
161                         self.mine_time = time + autocvar_g_balance_minelayer_time;
162                 }
163                 head = head.chain;
164         }
165
166         // explode if it's time to
167         if(self.mine_time && time >= self.mine_time)
168                 W_Mine_ProximityExplode();
169
170         // remote detonation
171         if (self.owner.weapon == WEP_MINE_LAYER)
172         if (self.owner.deadflag == DEAD_NO)
173         if (self.minelayer_detonate)
174                 W_Mine_RemoteExplode();
175 }
176
177 void W_Mine_Touch (void)
178 {
179         PROJECTILE_TOUCH;
180         if(!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))
181                 W_Mine_Stick();
182         else if(self.movetype != MOVETYPE_NONE) // don't unstick a locked mine when someone touches it
183                 self.velocity = '0 0 0';
184 }
185
186 void W_Mine_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
187 {
188         if (self.health <= 0)
189                 return;
190         self.health = self.health - damage;
191         self.angles = vectoangles(self.velocity);
192         if (self.health <= 0)
193                 W_PrepareExplosionByDamage(attacker, W_Mine_Explode);
194 }
195
196 void W_Mine_Attack (void)
197 {
198         entity mine;
199         entity flash;
200
201         // scan how many mines we placed, and return if we reached our limit
202         if(autocvar_g_balance_minelayer_limit)
203         {
204                 self.minelayer_mines = 0;
205                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
206                         self.minelayer_mines += 1;
207
208                 if(self.minelayer_mines >= autocvar_g_balance_minelayer_limit)
209                 {
210                         // the refire delay keeps this message from being spammed
211                         sprint(self, strcat("You cannot place more than ^2", ftos(autocvar_g_balance_minelayer_limit), " ^7mines at a time\n") );
212                         play2(self, "weapons/unavailable.wav");
213                         return;
214                 }
215         }
216
217         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
218                 self.ammo_rockets = self.ammo_rockets - autocvar_g_balance_minelayer_ammo;
219
220         W_SetupShot_ProjectileSize (self, '-4 -4 -4', '4 4 4', FALSE, 5, "weapons/mine_fire.wav", CHAN_WEAPON, autocvar_g_balance_minelayer_damage);
221         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
222
223         mine = WarpZone_RefSys_SpawnSameRefSys(self);
224         mine.owner = self;
225         if(autocvar_g_balance_minelayer_detonatedelay >= 0)
226                 mine.spawnshieldtime = time + autocvar_g_balance_minelayer_detonatedelay;
227         else
228                 mine.spawnshieldtime = -1;
229         mine.classname = "mine";
230         mine.bot_dodge = TRUE;
231         mine.bot_dodgerating = autocvar_g_balance_minelayer_damage * 2; // * 2 because it can detonate inflight which makes it even more dangerous
232
233         mine.takedamage = DAMAGE_YES;
234         mine.damageforcescale = autocvar_g_balance_minelayer_damageforcescale;
235         mine.health = autocvar_g_balance_minelayer_health;
236         mine.event_damage = W_Mine_Damage;
237
238         mine.movetype = MOVETYPE_TOSS;
239         PROJECTILE_MAKETRIGGER(mine);
240         mine.projectiledeathtype = WEP_MINE_LAYER;
241         setsize (mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
242
243         setorigin (mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
244         W_SetupProjectileVelocity(mine, autocvar_g_balance_minelayer_speed, 0);
245         mine.angles = vectoangles (mine.velocity);
246
247         mine.touch = W_Mine_Touch;
248         mine.think = W_Mine_Think;
249         mine.nextthink = time;
250         mine.cnt = time + autocvar_g_balance_minelayer_lifetime;
251         mine.flags = FL_PROJECTILE;
252
253         CSQCProjectile(mine, TRUE, PROJECTILE_MINE, TRUE);
254
255         // muzzle flash for 1st person view
256         flash = spawn ();
257         setmodel (flash, "models/flash.md3"); // precision set below
258         SUB_SetFade (flash, time, 0.1);
259         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
260         W_AttachToShotorg(flash, '5 0 0');
261
262         // common properties
263
264         other = mine; MUTATOR_CALLHOOK(EditProjectile);
265 }
266
267 void spawnfunc_weapon_minelayer (void); // defined in t_items.qc
268
269 float w_minelayer(float req)
270 {
271         entity mine;
272         float minfound;
273         if (req == WR_AIM)
274         {
275                 // aim and decide to fire if appropriate
276                 self.BUTTON_ATCK = bot_aim(autocvar_g_balance_minelayer_speed, 0, autocvar_g_balance_minelayer_lifetime, FALSE);
277                 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
278                 {
279                         // decide whether to detonate mines
280                         entity targetlist, targ;
281                         float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
282                         float selfdamage, teamdamage, enemydamage;
283                         edgedamage = autocvar_g_balance_minelayer_edgedamage;
284                         coredamage = autocvar_g_balance_minelayer_damage;
285                         edgeradius = autocvar_g_balance_minelayer_radius;
286                         recipricoledgeradius = 1 / edgeradius;
287                         selfdamage = 0;
288                         teamdamage = 0;
289                         enemydamage = 0;
290                         targetlist = findchainfloat(bot_attack, TRUE);
291                         mine = find(world, classname, "mine");
292                         while (mine)
293                         {
294                                 if (mine.owner != self)
295                                 {
296                                         mine = find(mine, classname, "mine");
297                                         continue;
298                                 }
299                                 targ = targetlist;
300                                 while (targ)
301                                 {
302                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - mine.origin);
303                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
304                                         // count potential damage according to type of target
305                                         if (targ == self)
306                                                 selfdamage = selfdamage + d;
307                                         else if (targ.team == self.team && teams_matter)
308                                                 teamdamage = teamdamage + d;
309                                         else if (bot_shouldattack(targ))
310                                                 enemydamage = enemydamage + d;
311                                         targ = targ.chain;
312                                 }
313                                 mine = find(mine, classname, "mine");
314                         }
315                         float desirabledamage;
316                         desirabledamage = enemydamage;
317                         if (time > self.invincible_finished && time > self.spawnshieldtime)
318                                 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
319                         if (teams_matter && self.team)
320                                 desirabledamage = desirabledamage - teamdamage;
321
322                         mine = find(world, classname, "mine");
323                         while (mine)
324                         {
325                                 if (mine.owner != self)
326                                 {
327                                         mine = find(mine, classname, "mine");
328                                         continue;
329                                 }
330                                 makevectors(mine.v_angle);
331                                 targ = targetlist;
332                                 if (skill > 9) // normal players only do this for the target they are tracking
333                                 {
334                                         targ = targetlist;
335                                         while (targ)
336                                         {
337                                                 if (
338                                                         (v_forward * normalize(mine.origin - targ.origin)< 0.1)
339                                                         && desirabledamage > 0.1*coredamage
340                                                 )self.BUTTON_ATCK2 = TRUE;
341                                                 targ = targ.chain;
342                                         }
343                                 }else{
344                                         float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
345                                         //As the distance gets larger, a correct detonation gets near imposible
346                                         //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
347                                         if(v_forward * normalize(mine.origin - self.enemy.origin)< 0.1)
348                                                 if(self.enemy.classname == "player")
349                                                         if(desirabledamage >= 0.1*coredamage)
350                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
351                                                                         self.BUTTON_ATCK2 = TRUE;
352                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
353                                 }
354
355                                 mine = find(mine, classname, "mine");
356                         }
357                         // if we would be doing at X percent of the core damage, detonate it
358                         // but don't fire a new shot at the same time!
359                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
360                                 self.BUTTON_ATCK2 = TRUE;
361                         if ((skill > 6.5) && (selfdamage > self.health))
362                                 self.BUTTON_ATCK2 = FALSE;
363                         //if(self.BUTTON_ATCK2 == TRUE)
364                         //      dprint(ftos(desirabledamage),"\n");
365                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
366                 }
367         }
368         else if (req == WR_THINK)
369         {
370                 if (self.BUTTON_ATCK)
371                 {
372                         if(weapon_prepareattack(0, autocvar_g_balance_minelayer_refire))
373                         {
374                                 W_Mine_Attack();
375                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_minelayer_animtime, w_ready);
376                         }
377                 }
378
379                 if (self.BUTTON_ATCK2)
380                 {
381                         minfound = 0;
382                         for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
383                         {
384                                 if(!mine.minelayer_detonate)
385                                 {
386                                         mine.minelayer_detonate = TRUE;
387                                         minfound = 1;
388                                 }
389                         }
390                         if(minfound)
391                                 sound (self, CHAN_WEAPON2, "weapons/mine_det.wav", VOL_BASE, ATTN_NORM);
392                 }
393         }
394         else if (req == WR_PRECACHE)
395         {
396                 precache_model ("models/flash.md3");
397                 precache_model ("models/mine.md3");
398                 precache_model ("models/weapons/g_minelayer.md3");
399                 precache_model ("models/weapons/v_minelayer.md3");
400                 precache_model ("models/weapons/h_minelayer.iqm");
401                 precache_sound ("weapons/mine_det.wav");
402                 precache_sound ("weapons/mine_fire.wav");
403                 precache_sound ("weapons/mine_stick.wav");
404                 precache_sound ("weapons/mine_trigger.wav");
405         }
406         else if (req == WR_SETUP)
407         {
408                 weapon_setup(WEP_MINE_LAYER);
409         }
410         else if (req == WR_CHECKAMMO1)
411         {
412                 // don't switch while placing a mine
413                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_MINE_LAYER)
414                         && self.ammo_rockets < autocvar_g_balance_minelayer_ammo)
415                         return FALSE;
416         }
417         else if (req == WR_CHECKAMMO2)
418                 return FALSE;
419         return TRUE;
420 };
421 #endif
422 #ifdef CSQC
423 float w_minelayer(float req)
424 {
425         if(req == WR_IMPACTEFFECT)
426         {
427                 vector org2;
428                 org2 = w_org + w_backoff * 12;
429                 pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
430                 if(!w_issilent)
431                         sound(self, CHAN_PROJECTILE, "weapons/mine_exp.wav", VOL_BASE, ATTN_NORM);
432         }
433         else if(req == WR_PRECACHE)
434         {
435                 precache_sound("weapons/mine_exp.wav");
436         }
437         else if (req == WR_SUICIDEMESSAGE)
438                 w_deathtypestring = _("%s exploded");
439         else if (req == WR_KILLMESSAGE)
440         {
441                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
442                         w_deathtypestring = _("%s got too close to %s's mine");
443                 else if(w_deathtype & HITTYPE_SPLASH)
444                         w_deathtypestring = _("%s almost dodged %s's mine");
445                 else
446                         w_deathtypestring = _("%s stepped on %s's mine");
447         }
448         return TRUE;
449 }
450 #endif
451 #endif