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