Proper projectile, and temporary mine model.
[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         spamsound (self, CHAN_PROJECTILE, "weapons/grenade_stick.wav", VOL_BASE, ATTN_NORM);
122         self.movetype = MOVETYPE_NONE; // lock the mine in place
123         // 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
124 }
125
126 void W_Mine_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
127 {
128         if (self.health <= 0)
129                 return;
130         self.health = self.health - damage;
131         self.angles = vectoangles(self.velocity);
132         if (self.health <= 0)
133                 W_PrepareExplosionByDamage(attacker, W_Mine_Explode);
134 }
135
136 void W_Mine_Attack (void)
137 {
138         local entity mine;
139         local entity flash;
140
141         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
142                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_minelayer_ammo");
143
144         W_SetupShot_ProjectileSize (self, '-3 -3 -3', '3 3 3', FALSE, 5, "weapons/rocket_fire.wav", cvar("g_balance_minelayer_damage"));
145         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
146
147         mine = WarpZone_RefSys_SpawnSameRefSys(self);
148         mine.owner = self;
149         self.lastmine = mine;
150         if(cvar("g_balance_minelayer_detonatedelay") >= 0)
151                 mine.spawnshieldtime = time + cvar("g_balance_minelayer_detonatedelay");
152         else
153                 mine.spawnshieldtime = -1;
154         mine.classname = "mine";
155         mine.bot_dodge = TRUE;
156         mine.bot_dodgerating = cvar("g_balance_minelayer_damage") * 2; // * 2 because it can be detonated inflight which makes it even more dangerous
157
158         mine.takedamage = DAMAGE_YES;
159         mine.damageforcescale = cvar("g_balance_minelayer_damageforcescale");
160         mine.health = cvar("g_balance_minelayer_health");
161         mine.event_damage = W_Mine_Damage;
162
163         mine.movetype = MOVETYPE_TOSS;
164         PROJECTILE_MAKETRIGGER(mine);
165         mine.projectiledeathtype = WEP_MINE_LAYER;
166         setsize (mine, '-6 -6 -6', '6 6 6'); // give it some size so it can be shot
167
168         setorigin (mine, w_shotorg - v_forward * 6); // move it back so it hits the wall at the right point
169         W_SetupProjectileVelocity(mine, cvar("g_balance_minelayer_speedstart"), 0);
170         mine.angles = vectoangles (mine.velocity);
171
172         mine.touch = W_Mine_Touch;
173         mine.think = W_Mine_Think;
174         mine.nextthink = time;
175         mine.cnt = time + cvar("g_balance_minelayer_lifetime");
176         mine.flags = FL_PROJECTILE;
177
178         CSQCProjectile(mine, FALSE, PROJECTILE_MINE, FALSE);
179
180         // muzzle flash for 1st person view
181         flash = spawn ();
182         setmodel (flash, "models/flash.md3"); // precision set below
183         SUB_SetFade (flash, time, 0.1);
184         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
185         W_AttachToShotorg(flash, '5 0 0');
186
187         // common properties
188 }
189
190 void spawnfunc_weapon_minelayer (void); // defined in t_items.qc
191
192 float w_minelayer(float req)
193 {
194         entity min;
195         float minfound;
196         if (req == WR_AIM)
197         {
198                 // aim and decide to fire if appropriate
199                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_minelayer_speed"), 0, cvar("g_balance_minelayer_lifetime"), FALSE);
200                 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
201                 {
202                         // decide whether to detonate mines
203                         local entity mine, targetlist, targ;
204                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
205                         local float selfdamage, teamdamage, enemydamage;
206                         edgedamage = cvar("g_balance_minelayer_edgedamage");
207                         coredamage = cvar("g_balance_minelayer_damage");
208                         edgeradius = cvar("g_balance_minelayer_radius");
209                         recipricoledgeradius = 1 / edgeradius;
210                         selfdamage = 0;
211                         teamdamage = 0;
212                         enemydamage = 0;
213                         targetlist = findchainfloat(bot_attack, TRUE);
214                         mine = find(world, classname, "mine");
215                         while (mine)
216                         {
217                                 if (mine.owner != self)
218                                 {
219                                         mine = find(mine, classname, "mine");
220                                         continue;
221                                 }
222                                 targ = targetlist;
223                                 while (targ)
224                                 {
225                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - mine.origin);
226                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
227                                         // count potential damage according to type of target
228                                         if (targ == self)
229                                                 selfdamage = selfdamage + d;
230                                         else if (targ.team == self.team && teams_matter)
231                                                 teamdamage = teamdamage + d;
232                                         else if (bot_shouldattack(targ))
233                                                 enemydamage = enemydamage + d;
234                                         targ = targ.chain;
235                                 }
236                                 mine = find(mine, classname, "mine");
237                         }
238                         local float desirabledamage;
239                         desirabledamage = enemydamage;
240                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
241                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
242                         if (self.team && teamplay != 1)
243                                 desirabledamage = desirabledamage - teamdamage;
244
245                         mine = find(world, classname, "mine");
246                         while (mine)
247                         {
248                                 if (mine.owner != self)
249                                 {
250                                         mine = find(mine, classname, "mine");
251                                         continue;
252                                 }
253                                 makevectors(mine.v_angle);
254                                 targ = targetlist;
255                                 if (skill > 9) // normal players only do this for the target they are tracking
256                                 {
257                                         targ = targetlist;
258                                         while (targ)
259                                         {
260                                                 if (
261                                                         (v_forward * normalize(mine.origin - targ.origin)< 0.1)
262                                                         && desirabledamage > 0.1*coredamage
263                                                 )self.BUTTON_ATCK2 = TRUE;
264                                                 targ = targ.chain;
265                                         }
266                                 }else{
267                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
268                                         //As the distance gets larger, a correct detonation gets near imposible
269                                         //Bots are assumed to use the rocket spawnfunc_light to see if the rocket gets near a player
270                                         if(v_forward * normalize(mine.origin - self.enemy.origin)< 0.1)
271                                                 if(self.enemy.classname == "player")
272                                                         if(desirabledamage >= 0.1*coredamage)
273                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
274                                                                         self.BUTTON_ATCK2 = TRUE;
275                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
276                                 }
277
278                                 mine = find(mine, classname, "mine");
279                         }
280                         // if we would be doing at X percent of the core damage, detonate it
281                         // but don't fire a new shot at the same time!
282                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
283                                 self.BUTTON_ATCK2 = TRUE;
284                         if ((skill > 6.5) && (selfdamage > self.health))
285                                 self.BUTTON_ATCK2 = FALSE;
286                         //if(self.BUTTON_ATCK2 == TRUE)
287                         //      dprint(ftos(desirabledamage),"\n");
288                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
289                 }
290         }
291         else if (req == WR_THINK)
292         {
293                 if (self.BUTTON_ATCK)
294                 {
295                         if(weapon_prepareattack(0, cvar("g_balance_minelayer_refire")))
296                         {
297                                 W_Mine_Attack();
298                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_minelayer_animtime"), w_ready);
299                         }
300                 }
301
302                 if (self.BUTTON_ATCK2)
303                 {
304                         minfound = 0;
305                         for(min = world; (min = find(min, classname, "mine")); ) if(min.owner == self)
306                         {
307                                 if(!min.minelayer_detonate)
308                                 {
309                                         min.minelayer_detonate = TRUE;
310                                         minfound = 1;
311                                 }
312                         }
313                         if(minfound)
314                                 sound (self, CHAN_WEAPON2, "weapons/rocket_det.wav", VOL_BASE, ATTN_NORM);
315                 }
316         }
317         else if (req == WR_PRECACHE)
318         {
319                 precache_model ("models/flash.md3");
320                 precache_model ("models/weapons/g_rl.md3");
321                 precache_model ("models/weapons/v_rl.md3");
322                 precache_model ("models/weapons/h_rl.iqm");
323                 precache_sound ("weapons/rocket_det.wav");
324                 precache_sound ("weapons/rocket_fire.wav");
325                 precache_sound ("weapons/rocket_mode.wav");
326         }
327         else if (req == WR_SETUP)
328         {
329                 weapon_setup(WEP_MINE_LAYER);
330         }
331         else if (req == WR_CHECKAMMO1)
332         {
333                 // don't switch while placing a mine
334                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_MINE_LAYER)
335                         && self.ammo_rockets < cvar("g_balance_minelayer_ammo"))
336                         return FALSE;
337         }
338         else if (req == WR_CHECKAMMO2)
339                 return FALSE;
340         return TRUE;
341 };
342 #endif
343 #ifdef CSQC
344 float w_minelayer(float req)
345 {
346         if(req == WR_IMPACTEFFECT)
347         {
348                 vector org2;
349                 org2 = w_org + w_backoff * 12;
350                 pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
351                 if(!w_issilent)
352                         sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
353         }
354         else if(req == WR_PRECACHE)
355         {
356                 precache_sound("weapons/rocket_impact.wav");
357         }
358         else if (req == WR_SUICIDEMESSAGE)
359                 w_deathtypestring = "%s exploded";
360         else if (req == WR_KILLMESSAGE)
361         {
362                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
363                         w_deathtypestring = "%s got too close to %s's mine";
364                 else if(w_deathtype & HITTYPE_SPLASH)
365                         w_deathtypestring = "%s almost dodged %s's mine";
366                 else
367                         w_deathtypestring = "%s ate %s's mine";
368         }
369         return TRUE;
370 }
371 #endif
372 #endif