1 #include "minelayer.qh"
4 spawnfunc(weapon_minelayer) { weapon_defaultspawnfunc(this, WEP_MINE_LAYER); }
6 void W_MineLayer_Stick(entity this, entity to)
8 spamsound(this, CH_SHOTS, SND_MINE_STICK, VOL_BASE, ATTN_NORM);
10 // in order for mines to face properly when sticking to the ground, they must be a server side entity rather than a csqc projectile
12 entity newmine = spawn();
13 IL_PUSH(g_mines, newmine);
14 newmine.weaponentity_fld = this.weaponentity_fld;
15 newmine.classname = this.classname;
17 newmine.bot_dodge = this.bot_dodge;
18 newmine.bot_dodgerating = this.bot_dodgerating;
20 newmine.owner = this.owner;
21 newmine.realowner = this.realowner;
22 setsize(newmine, '-4 -4 -4', '4 4 4');
23 setorigin(newmine, this.origin);
24 setmodel(newmine, MDL_MINELAYER_MINE);
25 newmine.angles = vectoangles(-trace_plane_normal); // face against the surface
27 newmine.mine_orientation = -trace_plane_normal;
29 newmine.takedamage = this.takedamage;
30 newmine.damageforcescale = this.damageforcescale;
31 newmine.health = this.health;
32 newmine.event_damage = this.event_damage;
33 newmine.spawnshieldtime = this.spawnshieldtime;
34 newmine.damagedbycontents = true;
35 IL_PUSH(g_damagedbycontents, newmine);
37 set_movetype(newmine, MOVETYPE_NONE); // lock the mine in place
38 newmine.projectiledeathtype = this.projectiledeathtype;
40 newmine.mine_time = this.mine_time;
42 settouch(newmine, func_null);
43 setthink(newmine, W_MineLayer_Think);
44 newmine.nextthink = time;
45 newmine.cnt = this.cnt;
46 newmine.flags = this.flags;
47 IL_PUSH(g_projectiles, newmine);
48 IL_PUSH(g_bot_dodge, newmine);
53 SetMovetypeFollow(newmine, to);
56 void W_MineLayer_Explode(entity this, entity directhitentity)
58 if(directhitentity.takedamage == DAMAGE_AIM)
59 if(IS_PLAYER(directhitentity))
60 if(DIFF_TEAM(this.realowner, directhitentity))
61 if(!IS_DEAD(directhitentity))
62 if(IsFlying(directhitentity))
63 Send_Notification(NOTIF_ONE, this.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_AIRSHOT);
65 this.event_damage = func_null;
66 this.takedamage = DAMAGE_NO;
68 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);
70 .entity weaponentity = this.weaponentity_fld;
71 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
73 entity own = this.realowner;
74 Weapon w = WEP_MINE_LAYER;
75 if(!w.wr_checkammo1(w, own, weaponentity))
77 own.cnt = WEP_MINE_LAYER.m_id;
78 int slot = weaponslot(weaponentity);
79 ATTACK_FINISHED(own, slot) = time;
80 own.(weaponentity).m_switchweapon = w_getbestweapon(own, weaponentity);
83 this.realowner.(weaponentity).minelayer_mines -= 1;
87 void W_MineLayer_Explode_think(entity this)
89 W_MineLayer_Explode(this, NULL);
92 void W_MineLayer_DoRemoteExplode(entity this)
94 this.event_damage = func_null;
95 this.takedamage = DAMAGE_NO;
97 if(this.move_movetype == MOVETYPE_NONE || this.move_movetype == MOVETYPE_FOLLOW)
98 this.velocity = this.mine_orientation; // particle fx and decals need .velocity
100 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);
102 .entity weaponentity = this.weaponentity_fld;
103 if(this.realowner.(weaponentity).m_weapon == WEP_MINE_LAYER)
105 entity own = this.realowner;
106 Weapon w = WEP_MINE_LAYER;
107 if(!w.wr_checkammo1(w, own, weaponentity))
109 own.cnt = WEP_MINE_LAYER.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, vector hitloc, vector force)
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 this.health = this.health - damage;
261 this.angles = vectoangles(this.velocity);
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));
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 mine.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 = WEP_MINE_LAYER.m_id;
311 setsize(mine, '-4 -4 -4', '4 4 4'); // give it some size so it can be shot
313 setorigin(mine, w_shotorg - v_forward * 4); // move it back so it hits the wall at the right point
314 W_SetupProjVelocity_Basic(mine, WEP_CVAR(minelayer, speed), 0);
315 mine.angles = vectoangles(mine.velocity);
317 settouch(mine, W_MineLayer_Touch);
318 setthink(mine, W_MineLayer_Think);
319 mine.nextthink = time;
320 mine.cnt = (WEP_CVAR(minelayer, lifetime) - WEP_CVAR(minelayer, lifetime_countdown));
321 mine.flags = FL_PROJECTILE;
322 IL_PUSH(g_projectiles, mine);
323 IL_PUSH(g_bot_dodge, mine);
324 mine.missile_flags = MIF_SPLASH | MIF_ARC | MIF_PROXY;
326 if(mine.cnt > 0) { mine.cnt += time; }
328 CSQCProjectile(mine, true, PROJECTILE_MINE, true);
330 // muzzle flash for 1st person view
332 setmodel(flash, MDL_MINELAYER_MUZZLEFLASH); // precision set below
333 SUB_SetFade(flash, time, 0.1);
334 flash.effects = EF_ADDITIVE | EF_FULLBRIGHT | EF_LOWPRECISION;
335 W_AttachToShotorg(actor, weaponentity, flash, '5 0 0');
339 MUTATOR_CALLHOOK(EditProjectile, actor, mine);
341 actor.(weaponentity).minelayer_mines = W_MineLayer_Count(actor, weaponentity);
344 bool W_MineLayer_PlacedMines(entity this, .entity weaponentity, bool detonate)
346 bool minfound = false;
348 IL_EACH(g_mines, it.realowner == this && it.weaponentity_fld == weaponentity,
352 if(!it.minelayer_detonate)
354 it.minelayer_detonate = true;
364 METHOD(MineLayer, wr_aim, void(entity thiswep, entity actor, .entity weaponentity))
366 // aim and decide to fire if appropriate
367 if(actor.(weaponentity).minelayer_mines >= WEP_CVAR(minelayer, limit))
368 PHYS_INPUT_BUTTON_ATCK(actor) = false;
370 PHYS_INPUT_BUTTON_ATCK(actor) = bot_aim(actor, weaponentity, WEP_CVAR(minelayer, speed), 0, WEP_CVAR(minelayer, lifetime), false);
371 if(skill >= 2) // skill 0 and 1 bots won't detonate mines!
373 // decide whether to detonate mines
374 float edgedamage, coredamage, edgeradius, recipricoledgeradius;
375 float selfdamage, teamdamage, enemydamage;
376 edgedamage = WEP_CVAR(minelayer, edgedamage);
377 coredamage = WEP_CVAR(minelayer, damage);
378 edgeradius = WEP_CVAR(minelayer, radius);
379 recipricoledgeradius = 1 / edgeradius;
384 IL_EACH(g_mines, it.realowner == actor,
387 IL_EACH(g_bot_targets, it.bot_attack,
389 float d = vlen(it.origin + (it.mins + it.maxs) * 0.5 - mine.origin);
390 d = bound(0, edgedamage + (coredamage - edgedamage) * sqrt(1 - d * recipricoledgeradius), 10000);
391 // count potential damage according to type of target
393 selfdamage = selfdamage + d;
394 else if(SAME_TEAM(it, actor))
395 teamdamage = teamdamage + d;
396 else if(bot_shouldattack(actor, it))
397 enemydamage = enemydamage + d;
401 float desirabledamage;
402 desirabledamage = enemydamage;
403 if(time > actor.invincible_finished && time > actor.spawnshieldtime)
404 desirabledamage = desirabledamage - selfdamage * autocvar_g_balance_selfdamagepercent;
405 if(teamplay && actor.team)
406 desirabledamage = desirabledamage - teamdamage;
408 makevectors(actor.v_angle);
409 IL_EACH(g_mines, it.realowner == actor,
411 if(skill > 9) // normal players only do this for the target they are tracking
414 IL_EACH(g_bot_targets, it.bot_attack,
416 if((v_forward * normalize(mine.origin - it.origin) < 0.1)
417 && desirabledamage > 0.1 * coredamage
418 ) PHYS_INPUT_BUTTON_ATCK2(actor) = true;
423 //As the distance gets larger, a correct detonation gets near imposible
424 //Bots are assumed to use the mine spawnfunc_light to see if the mine gets near a player
425 if((v_forward * normalize(it.origin - actor.enemy.origin) < 0.1)
426 && IS_PLAYER(actor.enemy)
427 && (desirabledamage >= 0.1 * coredamage)
430 float distance = bound(300, vlen(actor.origin - actor.enemy.origin), 30000);
431 if(random() / distance * 300 > frametime * bound(0, (10 - skill) * 0.2, 1))
432 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
437 // if we would be doing at X percent of the core damage, detonate it
438 // but don't fire a new shot at the same time!
439 if(desirabledamage >= 0.75 * coredamage) //this should do group damage in rare fortunate events
440 PHYS_INPUT_BUTTON_ATCK2(actor) = true;
441 if((skill > 6.5) && (selfdamage > actor.health))
442 PHYS_INPUT_BUTTON_ATCK2(actor) = false;
443 //if(PHYS_INPUT_BUTTON_ATCK2(actor) == true)
444 // dprint(ftos(desirabledamage),"\n");
445 if(PHYS_INPUT_BUTTON_ATCK2(actor)) PHYS_INPUT_BUTTON_ATCK(actor) = false;
448 METHOD(MineLayer, wr_think, void(entity thiswep, entity actor, .entity weaponentity, int fire))
450 if(weaponslot(weaponentity) == 0)
451 actor.minelayer_mines = actor.(weaponentity).minelayer_mines;
453 if(autocvar_g_balance_minelayer_reload_ammo && actor.(weaponentity).clip_load < WEP_CVAR(minelayer, ammo)) // forced reload
455 // not if we're holding the minelayer without enough ammo, but can detonate existing mines
456 if(!(W_MineLayer_PlacedMines(actor, weaponentity, false) && actor.(thiswep.ammo_field) < WEP_CVAR(minelayer, ammo))) {
457 thiswep.wr_reload(thiswep, actor, weaponentity);
462 if(weapon_prepareattack(thiswep, actor, weaponentity, false, WEP_CVAR(minelayer, refire)))
464 W_MineLayer_Attack(thiswep, actor, weaponentity);
465 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, WEP_CVAR(minelayer, animtime), w_ready);
471 if(W_MineLayer_PlacedMines(actor, weaponentity, true))
472 sound(actor, CH_WEAPON_B, SND_MINE_DET, VOL_BASE, ATTN_NORM);
475 METHOD(MineLayer, wr_checkammo1, bool(entity thiswep, entity actor, .entity weaponentity))
477 //int slot = 0; // TODO: unhardcode
478 // actually do // don't switch while placing a mine
479 //if(ATTACK_FINISHED(actor, slot) <= time || PS(actor).m_weapon != WEP_MINE_LAYER)
481 float ammo_amount = actor.(thiswep.ammo_field) >= WEP_CVAR(minelayer, ammo);
482 ammo_amount += actor.(weaponentity).(weapon_load[WEP_MINE_LAYER.m_id]) >= WEP_CVAR(minelayer, ammo);
487 METHOD(MineLayer, wr_checkammo2, bool(entity thiswep, entity actor, .entity weaponentity))
489 if(W_MineLayer_PlacedMines(actor, weaponentity, false))
494 METHOD(MineLayer, wr_resetplayer, void(entity thiswep, entity actor))
496 actor.minelayer_mines = 0;
497 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
499 .entity weaponentity = weaponentities[slot];
500 actor.(weaponentity).minelayer_mines = 0;
503 METHOD(MineLayer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
505 W_Reload(actor, weaponentity, WEP_CVAR(minelayer, ammo), SND_RELOAD);
507 METHOD(MineLayer, wr_suicidemessage, Notification(entity thiswep))
509 return WEAPON_MINELAYER_SUICIDE;
511 METHOD(MineLayer, wr_killmessage, Notification(entity thiswep))
513 return WEAPON_MINELAYER_MURDER;
519 METHOD(MineLayer, wr_impacteffect, void(entity thiswep, entity actor))
522 org2 = w_org + w_backoff * 12;
523 pointparticles(EFFECT_ROCKET_EXPLODE, org2, '0 0 0', 1);
525 sound(actor, CH_SHOTS, SND_MINE_EXP, VOL_BASE, ATTN_NORM);