///two towers attack stuff public bool CanTwoAttack(TowerBehavior destination, TowerBehavior attackingTower, TowerBehavior queueTower, float attackPercentage, out float queuePercent) { queuePercent = 0; if (queueTower.StationedUnits <= 0) { return(false); } int unitsLeft; int numOriginalAttacking = Mathf.CeilToInt(attackingTower.StationedUnits * attackPercentage); int maxAdditionAttackers = queueTower.StationedUnits; if (SimulateAttackUtil.SimulateAttack((numOriginalAttacking + maxAdditionAttackers), destination, attackingTower.Faction, out unitsLeft) == false) { //incase for overload attack maxAdditionAttackers -= unitsLeft - 1; queuePercent = AIConstants.RoundPercentToClosestOption((float)maxAdditionAttackers / (float)queueTower.StationedUnits); return(false); } maxAdditionAttackers -= unitsLeft - 1; queuePercent = AIConstants.RoundPercentToClosestOption((float)maxAdditionAttackers / (float)queueTower.StationedUnits); if (SimulateAttackUtil.DistanceCheck(queueTower, destination) == false) { return(false); } return(true); }
//function is called when the UnitsMoved event is fired private void TowerHasAttack(MovedUnitsInfo attackInfo) { //check to see if the tower being attacked is the AI and the attacking tower is the player if (attackInfo.FromFaction != FactionController.OtherFaction1 && attackInfo.ToFaction == FactionController.OtherFaction1) { //Check if the enemy units can even reach this tower, if (SimulateAttackUtil.DistanceCheck(attackInfo.From, attackInfo.To) == false) { return; } //Check if the enemy units can even take over the tower int EnemyUnitsLeft; if (SimulateAttackUtil.SimulateAttack(attackInfo.NumberOfUnits, attackInfo.To, attackInfo.FromFaction, out EnemyUnitsLeft) == false) { return; } //random check to see if the Ai will defend it float rand = Random.Range(0f, 100f); if (rand <= AIGameStateManager.PercentChanceAIWontDefend) { return; } //if we make it through the check, then Defend Defend(attackInfo, EnemyUnitsLeft); } }
public bool CanOverloadAttack(List <MultiAttackInfo> info) { //at this point it only equals 2 //can the first tower reach the second tower if (SimulateAttackUtil.DistanceCheck(info[0].AI.myTower, info[1].AI.myTower) == false) { return(false); } return(true); }
//This is the function the AI calls to see if it should attack the chosen tower public bool ShouldAIAttack(TowerBehavior AI, TowerBehavior destination, out float percentage, out AIConstants.ReasonFailed reason) { float chanceToAttackAnyway = AIGameStateManager.PercentToAttackAnyway; //setting our out variables(must be garunteed set before the function returns) reason = AIConstants.ReasonFailed.None; percentage = 0; if (AI.StationedUnits <= 0) { //percentage is already set to zero reason = AIConstants.ReasonFailed.NotEnoughtoSend; return(false); } //random chance to attack anyway int randomNum = Random.Range(0, 101); if (randomNum <= chanceToAttackAnyway) { percentage = AIConstants.RoundPercentToClosestOption(((float)AI.StationedUnits / (float)AI.StationedUnits)); return(true); } else { bool cantravel = SimulateAttackUtil.DistanceCheck(AI, destination); bool enoughtUnits = HaveEnoughSoldiers(AI, destination, ref percentage); //if distance is ever a factor, than the we don't need to adjust the percentage if (enoughtUnits == false && cantravel == false) { reason = AIConstants.ReasonFailed.Both; return(false); } else if (enoughtUnits == false) { reason = AIConstants.ReasonFailed.Units; return(false); } else if (cantravel == false) { reason = AIConstants.ReasonFailed.Distance; return(false); } } return(true); }
private List <AIBehavior> closestFriendlyTowers(TowerBehavior defendingAI) { //Get the list of current AI Towers List <AIBehavior> AI = AIController.GetAITowers(); //Hashtable for all of the towers SortedDictionary <float, List <AIBehavior> > closestTowers = new SortedDictionary <float, List <AIBehavior> >(); foreach (AIBehavior ai in AI) { if (ai.myTower == defendingAI) { continue; } float distance = Vector3.Distance(defendingAI.transform.position, ai.myTower.transform.position); if (SimulateAttackUtil.DistanceCheck(defendingAI, ai.myTower) == true) { if (!closestTowers.ContainsKey(distance)) { List <AIBehavior> newList = new List <AIBehavior>(); newList.Add(ai); closestTowers.Add(distance, newList); } else { closestTowers[distance].Add(ai); } } } List <AIBehavior> towersToSend = new List <AIBehavior>(); foreach (KeyValuePair <float, List <AIBehavior> > pair in closestTowers) { foreach (AIBehavior ai in pair.Value) { towersToSend.Add(ai); } } return(towersToSend); }