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