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