1 #include "minelayer.qh"
5 void W_MineLayer_Stick(entity this, entity to)
7 spamsound(this, CH_SHOTS, SND_MINE_STICK, VOL_BASE, ATTN_NORM);
9 // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
11 entity newmine = spawn();
12 IL_PUSH(g_mines, newmine);
13 newmine.weaponentity_fld = this.weaponentity_fld;
14 newmine.classname = this.classname;
16 newmine.bot_dodge = this.bot_dodge;
17 newmine.bot_dodgerating = this.bot_dodgerating;
19 newmine.owner = this.owner;
20 newmine.realowner = this.realowner;
21 setsize(newmine, '-4 -4 -4', '4 4 4');
22 setorigin(newmine, this.origin);
23 setmodel(newmine, MDL_MINELAYER_MINE);
24 newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
26 newmine.mine_orientation = -trace_plane_normal;
28 newmine.takedamage = this.takedamage;
29 newmine.damageforcescale = this.damageforcescale;
30 newmine.health = this.health;
31 newmine.event_damage = this.event_damage;
32 newmine.spawnshieldtime = this.spawnshieldtime;
33 newmine.damagedbycontents = true;
34 IL_PUSH(g_damagedbycontents, newmine);
36 set_movetype(newmine, MOVETYPE_NONE); // lock the mine in place
37 newmine.projectiledeathtype = this.projectiledeathtype;
39 newmine.mine_time = this.mine_time;
41 settouch(newmine, func_null);
42 setthink(newmine, W_MineLayer_Think);
43 newmine.nextthink = time;
44 newmine.cnt = this.cnt;
45 newmine.flags = this.flags;
46 IL_PUSH(g_projectiles, newmine);
47 IL_PUSH(g_bot_dodge, newmine);
52 SetMovetypeFollow(newmine, to);
55 void W_MineLayer_Explode(entity this, entity directhitentity)
57 if(directhitentity.takedamage == DAMAGE_AIM)
58 if(IS_PLAYER(directhitentity))
59 if(DIFF_TEAM(this.realowner, directhitentity))
60 if(!IS_DEAD(directhitentity))
61 if(IsFlying(directhitentity))
62 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
64 this.event_damage = func_null;
65 this.takedamage = DAMAGE_NO;
67 RadiusDamage(this, this.realowner, WEP_CVAR(minelayer, damage), WEP_CVAR(minelayer, edgedamage), WEP_CVAR(minelayer, radius), NULL, NULL, WEP_CVAR(minelayer, force), this.projectiledeathtype, directhitentity);
69 .entity weaponentity = this.weaponentity_fld;
70 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
72 entity own = this.realowner;
73 Weapon w = WEP_MINE_LAYER;
74 if(!w.wr_checkammo1(w, own, weaponentity))
76 own.cnt = WEP_MINE_LAYER.m_id;
77 int slot = weaponslot(weaponentity);
78 ATTACK_FINISHED(own, slot) = time;
79 own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
82 this.realowner.(weaponentity).minelayer_mines -= 1;
86 void W_MineLayer_Explode_think(entity this)
88 W_MineLayer_Explode(this, NULL);
91 void W_MineLayer_DoRemoteExplode(entity this)
93 this.event_damage = func_null;
94 this.takedamage = DAMAGE_NO;
96 if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
97 this.velocity = this.mine_orientation; // particle fx and decals need .velocity
99 RadiusDamage(this, this.realowner, WEP_CVAR(minelayer, remote_damage), WEP_CVAR(minelayer, remote_edgedamage), WEP_CVAR(minelayer, remote_radius), NULL, NULL, WEP_CVAR(minelayer, remote_force), this.projectiledeathtype | HITTYPE_BOUNCE, NULL);
101 .entity weaponentity = this.weaponentity_fld;
102 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
104 entity own = this.realowner;
105 Weapon w = WEP_MINE_LAYER;
106 if(!w.wr_checkammo1(w, own, weaponentity))
108 own.cnt = WEP_MINE_LAYER.m_id;
109 int slot = weaponslot(weaponentity);
110 ATTACK_FINISHED(own, slot) = time;
111 own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
114 this.realowner.(weaponentity).minelayer_mines -= 1;
118 void W_MineLayer_RemoteExplode(entity this)
120 if(!IS_DEAD(this.realowner))
121 if((this.spawnshieldtime >= 0)
122 ? (time >= this.spawnshieldtime) // timer
123 : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(minelayer, remote_radius))) // safety device
126 W_MineLayer_DoRemoteExplode(this);
130 void W_MineLayer_ProximityExplode(entity this)
132 // make sure no friend is in the mine's radius. If there is any, explosion is delayed until he's at a safe distance
133 if(WEP_CVAR(minelayer, protection) && this.mine_explodeanyway == 0)
136 head = findradius(this.origin, WEP_CVAR(minelayer, radius));
139 if(head == this.realowner || SAME_TEAM(head, this.realowner))
146 W_MineLayer_Explode(this, NULL);
149 int W_MineLayer_Count(entity e, .entity weaponentity)
152 IL_EACH(g_mines, it.realowner == e && it.weaponentity_fld == weaponentity,
160 void W_MineLayer_Think(entity this)
164 this.nextthink = time;
166 if(this.move_movetype == MOVETYPE_FOLLOW)
168 if(LostMovetypeFollow(this))
170 UnsetMovetypeFollow(this);
171 set_movetype(this, MOVETYPE_NONE);
175 // our lifetime has expired, it's time to die - mine_time just allows us to play a sound for this
176 // TODO: replace this mine_trigger.wav sound with a real countdown
177 if((time > this.cnt) && (!this.mine_time) && (this.cnt > 0))
179 if(WEP_CVAR(minelayer, lifetime_countdown) > 0)
180 spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
181 this.mine_time = time + WEP_CVAR(minelayer, lifetime_countdown);
182 this.mine_explodeanyway = 1; // make the mine super aggressive -- Samual: Rather, make it not care if a team mate is near.
185 // a player's mines shall explode if he disconnects or dies
186 // TODO: Do this on team change too -- Samual: But isn't a player killed when they switch teams?
187 if(!IS_PLAYER(this.realowner) || IS_DEAD(this.realowner) || STAT(FROZEN, this.realowner))
189 this.projectiledeathtype |= HITTYPE_BOUNCE;
190 W_MineLayer_Explode(this, NULL);
194 // set the mine for detonation when a foe gets close enough
195 head = findradius(this.origin, WEP_CVAR(minelayer, proximityradius));
198 if(IS_PLAYER(head) && !IS_DEAD(head) && !STAT(FROZEN, head))
199 if(head != this.realowner && DIFF_TEAM(head, this.realowner)) // don't trigger for team mates
202 spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
203 this.mine_time = time + WEP_CVAR(minelayer, time);
208 // explode if it's time to
209 if(this.mine_time && time >= this.mine_time)
211 W_MineLayer_ProximityExplode(this);
216 .entity weaponentity = this.weaponentity_fld;
217 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
218 if(!IS_DEAD(this.realowner))
219 if(this.minelayer_detonate)
220 W_MineLayer_RemoteExplode(this);
223 void W_MineLayer_Touch(entity this, entity toucher)
225 if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
226 return; // we're already a stuck mine, why do we get called? TODO does this even happen?
228 if(WarpZone_Projectile_Touch(this, toucher))
232 .entity weaponentity = this.weaponentity_fld;
233 this.realowner.(weaponentity).minelayer_mines -= 1;
238 if((toucher && IS_PLAYER(toucher) && !IS_DEAD(toucher)) || toucher.owner == this.owner)
240 // hit a player or other mine
245 W_MineLayer_Stick(this, toucher);
249 void W_MineLayer_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
254 float is_from_enemy = (inflictor.realowner != this.realowner);
256 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_from_enemy ? 1 : -1)))
257 return; // g_projectiles_damage says to halt
259 this.health = this.health - damage;
260 this.angles = vectoangles(this.velocity);
263 W_PrepareExplosionByDamage(this, attacker, W_MineLayer_Explode_think);
266 void W_MineLayer_Attack(Weapon thiswep, entity actor, .entity weaponentity)
271 // scan how many mines we placed, and return if we reached our limit
272 if(WEP_CVAR(minelayer, limit))
274 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
276 // the refire delay keeps this message from being spammed
277 Send_Notification(NOTIF_ONE, actor, MSG_MULTI, WEAPON_MINELAYER_LIMIT, WEP_CVAR(minelayer, limit));
278 play2(actor, SND(UNAVAILABLE));
283 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(minelayer, ammo), weaponentity);
285 W_SetupShot_ProjectileSize(actor, weaponentity, '-4 -4 -4', '4 4 4', false, 5, SND_MINE_FIRE, CH_WEAPON_A, WEP_CVAR(minelayer, damage));
286 Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
288 mine = WarpZone_RefSys_SpawnSameRefSys(actor);
289 mine.weaponentity_fld = weaponentity;
290 IL_PUSH(g_mines, mine);
291 mine.owner = mine.realowner = actor;
292 if(WEP_CVAR(minelayer, detonatedelay) >= 0)
293 mine.spawnshieldtime = time + WEP_CVAR(minelayer, detonatedelay);
295 mine.spawnshieldtime = -1;
296 mine.classname = "mine";
297 mine.bot_dodge = true;
298 mine.bot_dodgerating = WEP_CVAR(minelayer, damage) * 2; // * 2 because it can detonate inflight which makes it even more dangerous
300 mine.takedamage = DAMAGE_YES;
301 mine.damageforcescale = WEP_CVAR(minelayer, damageforcescale);
302 mine.health = WEP_CVAR(minelayer, health);
303 mine.event_damage = W_MineLayer_Damage;
304 mine.damagedbycontents = true;
305 IL_PUSH(g_damagedbycontents, mine);
307 set_movetype(mine, MOVETYPE_TOSS);
308 PROJECTILE_MAKETRIGGER(mine);
309 mine.projectiledeathtype = WEP_MINE_LAYER.m_id;
310 setsize(mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
312 setorigin(mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
313 W_SetupProjVelocity_Basic(mine, WEP_CVAR(minelayer, speed), 0);
314 mine.angles = vectoangles(mine.velocity);
316 settouch(mine, W_MineLayer_Touch);
317 setthink(mine, W_MineLayer_Think);
318 mine.nextthink = time;
319 mine.cnt = (WEP_CVAR(minelayer, lifetime) - WEP_CVAR(minelayer, lifetime_countdown));
320 mine.flags = FL_PROJECTILE;
321 IL_PUSH(g_projectiles, mine);
322 IL_PUSH(g_bot_dodge, mine);
323 mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
325 if(mine.cnt > 0) { mine.cnt += time; }
327 CSQCProjectile(mine, true, PROJECTILE_MINE, true);
329 // muzzle flash for 1st person view
331 setmodel(flash, MDL_MINELAYER_MUZZLEFLASH); // precision set below
332 SUB_SetFade(flash, time, 0.1);
333 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
334 W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
338 MUTATOR_CALLHOOK(EditProjectile, actor, mine);
340 actor.(weaponentity).minelayer_mines = W_MineLayer_Count(actor, weaponentity);
343 bool W_MineLayer_PlacedMines(entity this, .entity weaponentity, bool detonate)
345 bool minfound = false;
347 IL_EACH(g_mines, it.realowner == this && it.weaponentity_fld == weaponentity,
351 if(!it.minelayer_detonate)
353 it.minelayer_detonate = true;
363 METHOD(MineLayer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
365 // aim and decide to fire if appropriate
366 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
367 PHYS_INPUT_BUTTON_ATCK(actor) = false;
369 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(minelayer, speed), 0, WEP_CVAR(minelayer, lifetime), false);
370 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
372 // decide whether to detonate mines
373 float edgedamage, coredamage, edgeradius, recipricoledgeradius;
374 float selfdamage, teamdamage, enemydamage;
375 edgedamage = WEP_CVAR(minelayer, edgedamage);
376 coredamage = WEP_CVAR(minelayer, damage);
377 edgeradius = WEP_CVAR(minelayer, radius);
378 recipricoledgeradius = 1 / edgeradius;
383 IL_EACH(g_mines, it.realowner == actor,
386 IL_EACH(g_bot_targets, it.bot_attack,
388 float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - mine.origin);
389 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
390 // count potential damage according to type of target
392 selfdamage = selfdamage + d;
393 else if(SAME_TEAM(it, actor))
394 teamdamage = teamdamage + d;
395 else if(bot_shouldattack(actor, it))
396 enemydamage = enemydamage + d;
400 float desirabledamage;
401 desirabledamage = enemydamage;
402 if(time > actor.invincible_finished && time > actor.spawnshieldtime)
403 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
404 if(teamplay && actor.team)
405 desirabledamage = desirabledamage - teamdamage;
407 makevectors(actor.v_angle);
408 IL_EACH(g_mines, it.realowner == actor,
410 if(skill > 9) // normal players only do this for the target they are tracking
413 IL_EACH(g_bot_targets, it.bot_attack,
415 if((v_forward * normalize(mine.origin - it.origin) < 0.1)
416 && desirabledamage > 0.1 * coredamage
417 ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
422 //As the distance gets larger, a correct detonation gets near imposible
423 //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
424 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
425 && IS_PLAYER(actor.enemy)
426 && (desirabledamage >= 0.1 * coredamage)
429 float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
430 if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
431 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
436 // if we would be doing at X percent of the core damage, detonate it
437 // but don't fire a new shot at the same time!
438 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
439 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
440 if((skill > 6.5) && (selfdamage > actor.health))
441 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
442 //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
443 // dprint(ftos(desirabledamage),"\n");
444 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
447 METHOD(MineLayer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
449 if(weaponslot(weaponentity) == 0)
450 actor.minelayer_mines = actor.(weaponentity).minelayer_mines;
452 if(autocvar_g_balance_minelayer_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR(minelayer, ammo)) // forced reload
454 // not if we're holding the minelayer without enough ammo, but can detonate existing mines
455 if(!(W_MineLayer_PlacedMines(actor, weaponentity, false) && GetResourceAmount(actor, thiswep.ammo_type) < WEP_CVAR(minelayer, ammo))) {
456 thiswep.wr_reload(thiswep, actor, weaponentity);
461 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(minelayer, refire)))
463 W_MineLayer_Attack(thiswep, actor, weaponentity);
464 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(minelayer, animtime), w_ready);
470 if(W_MineLayer_PlacedMines(actor, weaponentity, true))
471 sound(actor, CH_WEAPON_B, SND_MINE_DET, VOL_BASE, ATTN_NORM);
474 METHOD(MineLayer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
476 //int slot = 0; // TODO: unhardcode
477 // actually do // don't switch while placing a mine
478 //if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_MINE_LAYER)
480 float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(minelayer, ammo);
481 ammo_amount += actor.(weaponentity).(weapon_load[WEP_MINE_LAYER.m_id]) >= WEP_CVAR(minelayer, ammo);
486 METHOD(MineLayer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
488 if(W_MineLayer_PlacedMines(actor, weaponentity, false))
493 METHOD(MineLayer, wr_resetplayer, void(entity thiswep, entity actor))
495 actor.minelayer_mines = 0;
496 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
498 .entity weaponentity = weaponentities[slot];
499 actor.(weaponentity).minelayer_mines = 0;
502 METHOD(MineLayer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
504 W_Reload(actor, weaponentity, WEP_CVAR(minelayer, ammo), SND_RELOAD);
506 METHOD(MineLayer, wr_suicidemessage, Notification(entity thiswep))
508 return WEAPON_MINELAYER_SUICIDE;
510 METHOD(MineLayer, wr_killmessage, Notification(entity thiswep))
512 return WEAPON_MINELAYER_MURDER;
518 METHOD(MineLayer, wr_impacteffect, void(entity thiswep, entity actor))
521 org2 = w_org + w_backoff * 12;
522 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
524 sound(actor, CH_SHOTS, SND_MINE_EXP, VOL_BASE, ATTN_NORM);