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