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 SetResourceAmountExplicit(newmine, RESOURCE_HEALTH, GetResourceAmount(this, RESOURCE_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, this.weaponentity_fld, directhitentity);
69 .entity weaponentity = this.weaponentity_fld;
70 Weapon thiswep = WEP_MINE_LAYER;
71 if(this.realowner.(weaponentity).m_weapon == thiswep)
73 entity own = this.realowner;
74 if(!thiswep.wr_checkammo1(thiswep, own, weaponentity))
76 own.cnt = thiswep.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),
100 NULL, NULL, WEP_CVAR(minelayer, remote_force), this.projectiledeathtype | HITTYPE_BOUNCE, this.weaponentity_fld, NULL);
102 .entity weaponentity = this.weaponentity_fld;
103 Weapon thiswep = WEP_MINE_LAYER;
104 if(this.realowner.(weaponentity).m_weapon == thiswep)
106 entity own = this.realowner;
107 if(!thiswep.wr_checkammo1(thiswep, own, weaponentity))
109 own.cnt = thiswep.m_id;
110 int slot = weaponslot(weaponentity);
111 ATTACK_FINISHED(own, slot) = time;
112 own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
115 this.realowner.(weaponentity).minelayer_mines -= 1;
119 void W_MineLayer_RemoteExplode(entity this)
121 if(!IS_DEAD(this.realowner))
122 if((this.spawnshieldtime >= 0)
123 ? (time >= this.spawnshieldtime) // timer
124 : (vdist(NearestPointOnBox(this.realowner, this.origin) - this.origin, >, WEP_CVAR(minelayer, remote_radius))) // safety device
127 W_MineLayer_DoRemoteExplode(this);
131 void W_MineLayer_ProximityExplode(entity this)
133 // make sure no friend is in the mine's radius. If there is any, explosion is delayed until he's at a safe distance
134 if(WEP_CVAR(minelayer, protection) && this.mine_explodeanyway == 0)
137 head = findradius(this.origin, WEP_CVAR(minelayer, radius));
140 if(head == this.realowner || SAME_TEAM(head, this.realowner))
147 W_MineLayer_Explode(this, NULL);
150 int W_MineLayer_Count(entity e, .entity weaponentity)
153 IL_EACH(g_mines, it.realowner == e && it.weaponentity_fld == weaponentity,
161 void W_MineLayer_Think(entity this)
165 this.nextthink = time;
167 if(this.move_movetype == MOVETYPE_FOLLOW)
169 if(LostMovetypeFollow(this))
171 UnsetMovetypeFollow(this);
172 set_movetype(this, MOVETYPE_NONE);
176 // our lifetime has expired, it's time to die - mine_time just allows us to play a sound for this
177 // TODO: replace this mine_trigger.wav sound with a real countdown
178 if((time > this.cnt) && (!this.mine_time) && (this.cnt > 0))
180 if(WEP_CVAR(minelayer, lifetime_countdown) > 0)
181 spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
182 this.mine_time = time + WEP_CVAR(minelayer, lifetime_countdown);
183 this.mine_explodeanyway = 1; // make the mine super aggressive -- Samual: Rather, make it not care if a team mate is near.
186 // a player's mines shall explode if he disconnects or dies
187 // TODO: Do this on team change too -- Samual: But isn't a player killed when they switch teams?
188 if(!IS_PLAYER(this.realowner) || IS_DEAD(this.realowner) || STAT(FROZEN, this.realowner))
190 this.projectiledeathtype |= HITTYPE_BOUNCE;
191 W_MineLayer_Explode(this, NULL);
195 // set the mine for detonation when a foe gets close enough
196 head = findradius(this.origin, WEP_CVAR(minelayer, proximityradius));
199 if(IS_PLAYER(head) && !IS_DEAD(head) && !STAT(FROZEN, head))
200 if(head != this.realowner && DIFF_TEAM(head, this.realowner)) // don't trigger for team mates
203 spamsound(this, CH_SHOTS, SND_MINE_TRIGGER, VOL_BASE, ATTN_NORM);
204 this.mine_time = time + WEP_CVAR(minelayer, time);
209 // explode if it's time to
210 if(this.mine_time && time >= this.mine_time)
212 W_MineLayer_ProximityExplode(this);
217 .entity weaponentity = this.weaponentity_fld;
218 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
219 if(!IS_DEAD(this.realowner))
220 if(this.minelayer_detonate)
221 W_MineLayer_RemoteExplode(this);
224 void W_MineLayer_Touch(entity this, entity toucher)
226 if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
227 return; // we're already a stuck mine, why do we get called? TODO does this even happen?
229 if(WarpZone_Projectile_Touch(this, toucher))
233 .entity weaponentity = this.weaponentity_fld;
234 this.realowner.(weaponentity).minelayer_mines -= 1;
239 if((toucher && IS_PLAYER(toucher) && !IS_DEAD(toucher)) || toucher.owner == this.owner)
241 // hit a player or other mine
246 W_MineLayer_Stick(this, toucher);
250 void W_MineLayer_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
252 if(GetResourceAmount(this, RESOURCE_HEALTH) <= 0)
255 float is_from_enemy = (inflictor.realowner != this.realowner);
257 if(!W_CheckProjectileDamage(inflictor.realowner, this.realowner, deathtype, (is_from_enemy ? 1 : -1)))
258 return; // g_projectiles_damage says to halt
260 TakeResource(this, RESOURCE_HEALTH, damage);
261 this.angles = vectoangles(this.velocity);
263 if(GetResourceAmount(this, RESOURCE_HEALTH) <= 0)
264 W_PrepareExplosionByDamage(this, attacker, W_MineLayer_Explode_think);
267 void W_MineLayer_Attack(Weapon thiswep, entity actor, .entity weaponentity)
272 // scan how many mines we placed, and return if we reached our limit
273 if(WEP_CVAR(minelayer, limit))
275 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
277 // the refire delay keeps this message from being spammed
278 Send_Notification(NOTIF_ONE, actor, MSG_MULTI, WEAPON_MINELAYER_LIMIT, WEP_CVAR(minelayer, limit));
279 play2(actor, SND(UNAVAILABLE));
284 W_DecreaseAmmo(thiswep, actor, WEP_CVAR(minelayer, ammo), weaponentity);
286 W_SetupShot_ProjectileSize(actor, weaponentity, '-4 -4 -4', '4 4 4', false, 5, SND_MINE_FIRE, CH_WEAPON_A, WEP_CVAR(minelayer, damage), thiswep.m_id);
287 Send_Effect(EFFECT_ROCKET_MUZZLEFLASH, w_shotorg, w_shotdir * 1000, 1);
289 mine = WarpZone_RefSys_SpawnSameRefSys(actor);
290 mine.weaponentity_fld = weaponentity;
291 IL_PUSH(g_mines, mine);
292 mine.owner = mine.realowner = actor;
293 if(WEP_CVAR(minelayer, detonatedelay) >= 0)
294 mine.spawnshieldtime = time + WEP_CVAR(minelayer, detonatedelay);
296 mine.spawnshieldtime = -1;
297 mine.classname = "mine";
298 mine.bot_dodge = true;
299 mine.bot_dodgerating = WEP_CVAR(minelayer, damage) * 2; // * 2 because it can detonate inflight which makes it even more dangerous
301 mine.takedamage = DAMAGE_YES;
302 mine.damageforcescale = WEP_CVAR(minelayer, damageforcescale);
303 SetResourceAmountExplicit(mine, RESOURCE_HEALTH, WEP_CVAR(minelayer, health));
304 mine.event_damage = W_MineLayer_Damage;
305 mine.damagedbycontents = true;
306 IL_PUSH(g_damagedbycontents, mine);
308 set_movetype(mine, MOVETYPE_TOSS);
309 PROJECTILE_MAKETRIGGER(mine);
310 mine.projectiledeathtype = thiswep.m_id;
311 mine.weaponentity_fld = weaponentity;
312 setsize(mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
314 setorigin(mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
315 W_SetupProjVelocity_Basic(mine, WEP_CVAR(minelayer, speed), 0);
316 mine.angles = vectoangles(mine.velocity);
318 settouch(mine, W_MineLayer_Touch);
319 setthink(mine, W_MineLayer_Think);
320 mine.nextthink = time;
321 mine.cnt = (WEP_CVAR(minelayer, lifetime) - WEP_CVAR(minelayer, lifetime_countdown));
322 mine.flags = FL_PROJECTILE;
323 IL_PUSH(g_projectiles, mine);
324 IL_PUSH(g_bot_dodge, mine);
325 mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
327 if(mine.cnt > 0) { mine.cnt += time; }
329 CSQCProjectile(mine, true, PROJECTILE_MINE, true);
331 // muzzle flash for 1st person view
333 setmodel(flash, MDL_MINELAYER_MUZZLEFLASH); // precision set below
334 SUB_SetFade(flash, time, 0.1);
335 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
336 W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
340 MUTATOR_CALLHOOK(EditProjectile, actor, mine);
342 actor.(weaponentity).minelayer_mines = W_MineLayer_Count(actor, weaponentity);
345 bool W_MineLayer_PlacedMines(entity this, .entity weaponentity, bool detonate)
347 bool minfound = false;
349 IL_EACH(g_mines, it.realowner == this && it.weaponentity_fld == weaponentity,
353 if(!it.minelayer_detonate)
355 it.minelayer_detonate = true;
365 METHOD(MineLayer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
367 // aim and decide to fire if appropriate
368 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
369 PHYS_INPUT_BUTTON_ATCK(actor) = false;
371 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(minelayer, speed), 0, WEP_CVAR(minelayer, lifetime), false);
372 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
374 // decide whether to detonate mines
375 float edgedamage, coredamage, edgeradius, recipricoledgeradius;
376 float selfdamage, teamdamage, enemydamage;
377 edgedamage = WEP_CVAR(minelayer, edgedamage);
378 coredamage = WEP_CVAR(minelayer, damage);
379 edgeradius = WEP_CVAR(minelayer, radius);
380 recipricoledgeradius = 1 / edgeradius;
385 IL_EACH(g_mines, it.realowner == actor,
388 IL_EACH(g_bot_targets, it.bot_attack,
390 float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - mine.origin);
391 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
392 // count potential damage according to type of target
394 selfdamage = selfdamage + d;
395 else if(SAME_TEAM(it, actor))
396 teamdamage = teamdamage + d;
397 else if(bot_shouldattack(actor, it))
398 enemydamage = enemydamage + d;
402 float desirabledamage;
403 desirabledamage = enemydamage;
404 if(time > actor.invincible_finished && time > actor.spawnshieldtime)
405 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
406 if(teamplay && actor.team)
407 desirabledamage = desirabledamage - teamdamage;
409 makevectors(actor.v_angle);
410 IL_EACH(g_mines, it.realowner == actor,
412 if(skill > 9) // normal players only do this for the target they are tracking
415 IL_EACH(g_bot_targets, it.bot_attack,
417 if((v_forward * normalize(mine.origin - it.origin) < 0.1)
418 && desirabledamage > 0.1 * coredamage
419 ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
424 //As the distance gets larger, a correct detonation gets near imposible
425 //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
426 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
427 && IS_PLAYER(actor.enemy)
428 && (desirabledamage >= 0.1 * coredamage)
431 float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
432 if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
433 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
438 // if we would be doing at X percent of the core damage, detonate it
439 // but don't fire a new shot at the same time!
440 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
441 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
442 if((skill > 6.5) && (selfdamage > GetResourceAmount(actor, RESOURCE_HEALTH)))
443 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
444 //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
445 // dprint(ftos(desirabledamage),"\n");
446 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
449 METHOD(MineLayer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
451 if(autocvar_g_balance_minelayer_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR(minelayer, ammo)) // forced reload
453 // not if we're holding the minelayer without enough ammo, but can detonate existing mines
454 if(!(W_MineLayer_PlacedMines(actor, weaponentity, false) && GetResourceAmount(actor, thiswep.ammo_type) < WEP_CVAR(minelayer, ammo))) {
455 thiswep.wr_reload(thiswep, actor, weaponentity);
460 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(minelayer, refire)))
462 W_MineLayer_Attack(thiswep, actor, weaponentity);
463 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(minelayer, animtime), w_ready);
469 if(W_MineLayer_PlacedMines(actor, weaponentity, true))
470 sound(actor, CH_WEAPON_B, SND_MINE_DET, VOL_BASE, ATTN_NORM);
473 METHOD(MineLayer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
475 //int slot = 0; // TODO: unhardcode
476 // actually do // don't switch while placing a mine
477 //if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_MINE_LAYER)
479 float ammo_amount = GetResourceAmount(actor, thiswep.ammo_type) >= WEP_CVAR(minelayer, ammo);
480 ammo_amount += actor.(weaponentity).(weapon_load[thiswep.m_id]) >= WEP_CVAR(minelayer, ammo);
485 METHOD(MineLayer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
487 if(W_MineLayer_PlacedMines(actor, weaponentity, false))
492 METHOD(MineLayer, wr_resetplayer, void(entity thiswep, entity actor))
494 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
496 .entity weaponentity = weaponentities[slot];
497 actor.(weaponentity).minelayer_mines = 0;
500 METHOD(MineLayer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
502 W_Reload(actor, weaponentity, WEP_CVAR(minelayer, ammo), SND_RELOAD);
504 METHOD(MineLayer, wr_suicidemessage, Notification(entity thiswep))
506 return WEAPON_MINELAYER_SUICIDE;
508 METHOD(MineLayer, wr_killmessage, Notification(entity thiswep))
510 return WEAPON_MINELAYER_MURDER;
516 METHOD(MineLayer, wr_impacteffect, void(entity thiswep, entity actor))
519 org2 = w_org + w_backoff * 12;
520 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
522 sound(actor, CH_SHOTS, SND_MINE_EXP, VOL_BASE, ATTN_NORM);