]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_minelayer.qc
More tiny tweaks to cvar and comment
[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 .float minelayer_detonate;
6 .float mine_number, mine_time;
7
8 void spawnfunc_weapon_minelayer (void)
9 {
10         weapon_defaultspawnfunc(WEP_MINE_LAYER);
11 }
12
13 void W_Mine_Stick ()
14 {
15         spamsound (self, CHAN_PROJECTILE, "weapons/mine_stick.wav", VOL_BASE, ATTN_NORM);
16
17         // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
18
19         local entity newmine;
20         newmine = spawn();
21         newmine.classname = self.classname;
22
23         newmine.bot_dodge = self.bot_dodge;
24         newmine.bot_dodgerating = self.bot_dodgerating;
25
26         newmine.owner = self.owner;
27         setsize(newmine, '-4 -4 -4', '4 4 4');
28         setorigin(newmine, self.origin);
29         setmodel(newmine, "models/mine.md3");
30         newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
31
32         newmine.takedamage = self.takedamage;
33         newmine.damageforcescale = self.damageforcescale;
34         newmine.health = self.health;
35         newmine.event_damage = self.event_damage;
36
37         newmine.movetype = MOVETYPE_NONE; // lock the mine in place
38         newmine.projectiledeathtype = self.projectiledeathtype;
39
40         newmine.mine_number = self.mine_number;
41         newmine.mine_time = self.mine_time;
42
43         newmine.touch = SUB_Null;
44         newmine.think = self.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_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         // set the mine for detonation when a foe gets close enough
142         head = findradius(self.origin, cvar("g_balance_minelayer_proximityradius"));
143         while(head)
144         {
145                 if(head.classname == "player" && head.deadflag == DEAD_NO)
146                 if(head != self.owner && IsDifferentTeam(head, self.owner)) // don't trigger for team mates
147                 if(!self.mine_time)
148                 {
149                         spamsound (self, CHAN_PROJECTILE, "weapons/mine_trigger.wav", VOL_BASE, ATTN_NORM);
150                         self.mine_time = time + cvar("g_balance_minelayer_time");
151                 }
152                 head = head.chain;
153         }
154
155         // explode if it's time to
156         if(self.mine_time && time >= self.mine_time)
157                 W_Mine_ProximityExplode();
158
159         // remote detonation
160         if (self.owner.weapon == WEP_MINE_LAYER)
161         if (self.owner.deadflag == DEAD_NO)
162         if (self.minelayer_detonate)
163                 W_Mine_RemoteExplode();
164
165         if(self.csqcprojectile_clientanimate == 0)
166                 UpdateCSQCProjectile(self);
167 }
168
169 void W_Mine_Touch (void)
170 {
171         PROJECTILE_TOUCH;
172         if(!other || (other.takedamage != DAMAGE_AIM && other.movetype == MOVETYPE_NONE))
173                 W_Mine_Stick();
174         else if(self.movetype != MOVETYPE_NONE) // don't unstick a locked mine when someone touches it
175                 self.velocity = '0 0 0';
176 }
177
178 void W_Mine_Damage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
179 {
180         if (self.health <= 0)
181                 return;
182         self.health = self.health - damage;
183         self.angles = vectoangles(self.velocity);
184         if (self.health <= 0)
185                 W_PrepareExplosionByDamage(attacker, W_Mine_Explode);
186 }
187
188 void W_Mine_Attack (void)
189 {
190         local entity mine;
191         local entity flash;
192
193         // scan how many mines we placed, and return if we reached our limit
194         if(cvar("g_balance_minelayer_limit"))
195         {
196                 self.mine_number = 0;
197                 for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
198                         self.mine_number += 1;
199
200                 if(self.mine_number >= cvar("g_balance_minelayer_limit"))
201                 {
202                         // the refire delay keeps this message from being spammed
203                         sprint(self, strcat("You cannot place more than ^2", cvar_string("g_balance_minelayer_limit"), " ^7mines at a time\n") );
204                         play2(self, "weapons/unavailable.wav");
205                         return;
206                 }
207         }
208
209         if not(self.items & IT_UNLIMITED_WEAPON_AMMO)
210                 self.ammo_rockets = self.ammo_rockets - cvar("g_balance_minelayer_ammo");
211
212         W_SetupShot_ProjectileSize (self, '-4 -4 -4', '4 4 4', FALSE, 5, "weapons/mine_fire.wav", cvar("g_balance_minelayer_damage"));
213         pointparticles(particleeffectnum("rocketlauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
214
215         mine = WarpZone_RefSys_SpawnSameRefSys(self);
216         mine.owner = self;
217         if(cvar("g_balance_minelayer_detonatedelay") >= 0)
218                 mine.spawnshieldtime = time + cvar("g_balance_minelayer_detonatedelay");
219         else
220                 mine.spawnshieldtime = -1;
221         mine.classname = "mine";
222         mine.bot_dodge = TRUE;
223         mine.bot_dodgerating = cvar("g_balance_minelayer_damage") * 2; // * 2 because it can detonate inflight which makes it even more dangerous
224
225         mine.takedamage = DAMAGE_YES;
226         mine.damageforcescale = cvar("g_balance_minelayer_damageforcescale");
227         mine.health = cvar("g_balance_minelayer_health");
228         mine.event_damage = W_Mine_Damage;
229
230         mine.movetype = MOVETYPE_TOSS;
231         PROJECTILE_MAKETRIGGER(mine);
232         mine.projectiledeathtype = WEP_MINE_LAYER;
233         setsize (mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
234
235         setorigin (mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
236         W_SetupProjectileVelocity(mine, cvar("g_balance_minelayer_speed"), 0);
237         mine.angles = vectoangles (mine.velocity);
238
239         mine.touch = W_Mine_Touch;
240         mine.think = W_Mine_Think;
241         mine.nextthink = time;
242         mine.cnt = time + cvar("g_balance_minelayer_lifetime");
243         mine.flags = FL_PROJECTILE;
244
245         CSQCProjectile(mine, FALSE, PROJECTILE_MINE, TRUE);
246
247         // muzzle flash for 1st person view
248         flash = spawn ();
249         setmodel (flash, "models/flash.md3"); // precision set below
250         SUB_SetFade (flash, time, 0.1);
251         flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
252         W_AttachToShotorg(flash, '5 0 0');
253
254         // common properties
255 }
256
257 void spawnfunc_weapon_minelayer (void); // defined in t_items.qc
258
259 float w_minelayer(float req)
260 {
261         entity mine;
262         float minfound;
263         if (req == WR_AIM)
264         {
265                 // aim and decide to fire if appropriate
266                 self.BUTTON_ATCK = bot_aim(cvar("g_balance_minelayer_speed"), 0, cvar("g_balance_minelayer_lifetime"), FALSE);
267                 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
268                 {
269                         // decide whether to detonate mines
270                         local entity mine, targetlist, targ;
271                         local float edgedamage, coredamage, edgeradius, recipricoledgeradius, d;
272                         local float selfdamage, teamdamage, enemydamage;
273                         edgedamage = cvar("g_balance_minelayer_edgedamage");
274                         coredamage = cvar("g_balance_minelayer_damage");
275                         edgeradius = cvar("g_balance_minelayer_radius");
276                         recipricoledgeradius = 1 / edgeradius;
277                         selfdamage = 0;
278                         teamdamage = 0;
279                         enemydamage = 0;
280                         targetlist = findchainfloat(bot_attack, TRUE);
281                         mine = find(world, classname, "mine");
282                         while (mine)
283                         {
284                                 if (mine.owner != self)
285                                 {
286                                         mine = find(mine, classname, "mine");
287                                         continue;
288                                 }
289                                 targ = targetlist;
290                                 while (targ)
291                                 {
292                                         d = vlen(targ.origin + (targ.mins + targ.maxs) * 0.5 - mine.origin);
293                                         d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
294                                         // count potential damage according to type of target
295                                         if (targ == self)
296                                                 selfdamage = selfdamage + d;
297                                         else if (targ.team == self.team && teams_matter)
298                                                 teamdamage = teamdamage + d;
299                                         else if (bot_shouldattack(targ))
300                                                 enemydamage = enemydamage + d;
301                                         targ = targ.chain;
302                                 }
303                                 mine = find(mine, classname, "mine");
304                         }
305                         local float desirabledamage;
306                         desirabledamage = enemydamage;
307                         if (teamplay != 1 && time > self.invincible_finished && time > self.spawnshieldtime)
308                                 desirabledamage = desirabledamage - selfdamage * cvar("g_balance_selfdamagepercent");
309                         if (self.team && teamplay != 1)
310                                 desirabledamage = desirabledamage - teamdamage;
311
312                         mine = find(world, classname, "mine");
313                         while (mine)
314                         {
315                                 if (mine.owner != self)
316                                 {
317                                         mine = find(mine, classname, "mine");
318                                         continue;
319                                 }
320                                 makevectors(mine.v_angle);
321                                 targ = targetlist;
322                                 if (skill > 9) // normal players only do this for the target they are tracking
323                                 {
324                                         targ = targetlist;
325                                         while (targ)
326                                         {
327                                                 if (
328                                                         (v_forward * normalize(mine.origin - targ.origin)< 0.1)
329                                                         && desirabledamage > 0.1*coredamage
330                                                 )self.BUTTON_ATCK2 = TRUE;
331                                                 targ = targ.chain;
332                                         }
333                                 }else{
334                                         local float distance; distance= bound(300,vlen(self.origin-self.enemy.origin),30000);
335                                         //As the distance gets larger, a correct detonation gets near imposible
336                                         //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
337                                         if(v_forward * normalize(mine.origin - self.enemy.origin)< 0.1)
338                                                 if(self.enemy.classname == "player")
339                                                         if(desirabledamage >= 0.1*coredamage)
340                                                                 if(random()/distance*300 > frametime*bound(0,(10-skill)*0.2,1))
341                                                                         self.BUTTON_ATCK2 = TRUE;
342                                 //      dprint(ftos(random()/distance*300),">");dprint(ftos(frametime*bound(0,(10-skill)*0.2,1)),"\n");
343                                 }
344
345                                 mine = find(mine, classname, "mine");
346                         }
347                         // if we would be doing at X percent of the core damage, detonate it
348                         // but don't fire a new shot at the same time!
349                         if (desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
350                                 self.BUTTON_ATCK2 = TRUE;
351                         if ((skill > 6.5) && (selfdamage > self.health))
352                                 self.BUTTON_ATCK2 = FALSE;
353                         //if(self.BUTTON_ATCK2 == TRUE)
354                         //      dprint(ftos(desirabledamage),"\n");
355                         if (self.BUTTON_ATCK2 == TRUE) self.BUTTON_ATCK = FALSE;
356                 }
357         }
358         else if (req == WR_THINK)
359         {
360                 if (self.BUTTON_ATCK)
361                 {
362                         if(weapon_prepareattack(0, cvar("g_balance_minelayer_refire")))
363                         {
364                                 W_Mine_Attack();
365                                 weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_minelayer_animtime"), w_ready);
366                         }
367                 }
368
369                 if (self.BUTTON_ATCK2)
370                 {
371                         minfound = 0;
372                         for(mine = world; (mine = find(mine, classname, "mine")); ) if(mine.owner == self)
373                         {
374                                 if(!mine.minelayer_detonate)
375                                 {
376                                         mine.minelayer_detonate = TRUE;
377                                         minfound = 1;
378                                 }
379                         }
380                         if(minfound)
381                                 sound (self, CHAN_WEAPON2, "weapons/mine_det.wav", VOL_BASE, ATTN_NORM);
382                 }
383         }
384         else if (req == WR_PRECACHE)
385         {
386                 precache_model ("models/flash.md3");
387                 precache_model ("models/mine.md3");
388                 precache_model ("models/weapons/g_minelayer.md3");
389                 precache_model ("models/weapons/v_minelayer.md3");
390                 precache_model ("models/weapons/h_minelayer.iqm");
391                 precache_sound ("weapons/mine_det.wav");
392                 precache_sound ("weapons/mine_fire.wav");
393                 precache_sound ("weapons/mine_stick.wav");
394                 precache_sound ("weapons/mine_trigger.wav");
395         }
396         else if (req == WR_SETUP)
397         {
398                 weapon_setup(WEP_MINE_LAYER);
399         }
400         else if (req == WR_CHECKAMMO1)
401         {
402                 // don't switch while placing a mine
403                 if ((ATTACK_FINISHED(self) <= time || self.weapon != WEP_MINE_LAYER)
404                         && self.ammo_rockets < cvar("g_balance_minelayer_ammo"))
405                         return FALSE;
406         }
407         else if (req == WR_CHECKAMMO2)
408                 return FALSE;
409         return TRUE;
410 };
411 #endif
412 #ifdef CSQC
413 float w_minelayer(float req)
414 {
415         if(req == WR_IMPACTEFFECT)
416         {
417                 vector org2;
418                 org2 = w_org + w_backoff * 12;
419                 pointparticles(particleeffectnum("rocket_explode"), org2, '0 0 0', 1);
420                 if(!w_issilent)
421                         sound(self, CHAN_PROJECTILE, "weapons/mine_exp.wav", VOL_BASE, ATTN_NORM);
422         }
423         else if(req == WR_PRECACHE)
424         {
425                 precache_sound("weapons/mine_exp.wav");
426         }
427         else if (req == WR_SUICIDEMESSAGE)
428                 w_deathtypestring = "%s exploded";
429         else if (req == WR_KILLMESSAGE)
430         {
431                 if(w_deathtype & HITTYPE_BOUNCE) // (remote detonation)
432                         w_deathtypestring = "%s got too close to %s's mine";
433                 else if(w_deathtype & HITTYPE_SPLASH)
434                         w_deathtypestring = "%s almost dodged %s's mine";
435                 else
436                         w_deathtypestring = "%s stepped on %s's mine";
437         }
438         return TRUE;
439 }
440 #endif
441 #endif