class MosqueBombingRunSquadAI extends BombingRunSquadAI;

#define INNER_DANGER_RADIUS		100
#define OUTER_DANGER_RADIUS		200
#define MAX_CLOSE_ENEMIES		2
#define MIN_PASS_DISTANCE		200
#define MAX_PASS_DISTANCE		5000
#define HUMAN_MIN_HEALTH_REQUIREMENT	60
#define BOT_MIN_HEALTH_REQUIREMENT	60
#define LOG_TAG				'MBR_Mosque'

function bool TryPassTo(vector V,Bot CarrierBot,Pawn Recipient)
{
local Pawn Carrier;
local Float passDistance;
local Bool willPass;
local Float angle;
local int recipientLifeExpectency;

Carrier = CarrierBot.Pawn;
passDistance = VSize(Recipient.Location - Carrier.Location);
angle = (V Dot Normal(Recipient.Location - Carrier.Location));
recipientLifeExpectency = Recipient.Health + Recipient.ShieldStrength;
willPass = false;

if (Recipient.IsHumanControlled())
	{
	if ((passDistance < MAX_PASS_DISTANCE)
		&& (passDistance > MIN_PASS_DISTANCE)
		&& (recipientLifeExpectency > HUMAN_MIN_HEALTH_REQUIREMENT)
		&& (angle > 0.5)
		&& (Carrier.LineOfSightTo(Recipient))
		&& isNotSurrounded(Recipient))
		{
		willPass = true;
		}
	}
else // bot controlled, same as before except "IsHumanControlled()" requirement removed
	{
	if (((recipientLifeExpectency >= FMin(BOT_MIN_HEALTH_REQUIREMENT,Carrier.Health))
			|| Recipient.InCurrentCombo() || (Recipient.Controller.Adrenaline == Recipient.Controller.AdrenalineMax))
		&& (passDistance < MAX_PASS_DISTANCE)
		&& (angle > 0.5)
		&& ((CarrierBot.Focus == CarrierBot.MoveTarget)
			|| (Recipient.Controller.MoveTarget == CarrierBot.RouteCache[0])
			|| (Recipient.Controller.MoveTarget == CarrierBot.RouteCache[1])
			|| (Recipient.Controller.MoveTarget == CarrierBot.RouteCache[2]))
		&& Carrier.LineOfSightTo(Recipient))
		{
		willPass = true;
		}
	}

if (willPass)
	{
	PassTarget = Bot(Recipient.Controller);
	Carrier.Weapon.SetAITarget(Recipient);
	CarrierBot.bPlannedShot = true;
	CarrierBot.Target = Recipient;
	Carrier.Weapon.BotFire(false);
	}

return(willPass);
}

function bool isNotSurrounded(Pawn recipient)
{
local Controller C;
local Float distanceToEnemy;
local int closeEnemyCount;
local int recipientTeamNumber;

//initialization
closeEnemyCount = 0;
recipientTeamNumber = recipient.GetTeamNum();

//Examine every controller in the game
for (C=Level.ControllerList; C!=None; C=C.NextController)
	{
	//pawn must exist and must be on the opposite team of recipient [if !(C.SameTeamAs(recipient.Controller))]
	if ((C.Pawn != none) && (C.GetTeamNum() != recipientTeamNumber))
		{
		distanceToEnemy = VSize(recipient.Location - C.pawn.Location); //get distance from intended receiver to this enemy
		if (distanceToEnemy < INNER_DANGER_RADIUS)
			{
			//log("Can't pass, one enemy too close!",LOG_TAG);
			return(false);
			}
		if (distanceToEnemy < OUTER_DANGER_RADIUS) closeEnemyCount += 1; //if this enemy is within the danger radius, increment count
		}
	}

if (closeEnemyCount <= MAX_CLOSE_ENEMIES)
	{
	//log("Can pass, not surrounded",LOG_TAG);
	return(true);
	}
else
	{
	//log("Can't pass, too many close enemies!",LOG_TAG);
	return(false);
	}
}