/// <summary> /// Performs an ability based on the ai's decision data. /// </summary> /// <param name="decision">The data object containing the ai's decision of action.</param> /// <param name="aiFormation">The Formation of the Ai.</param> /// <returns>Returns the result of the ability being performed.</returns> private AbilityResult PerformAiDecision(CombatAiDecision decision, Formation aiFormation) { lock (_key) { int targetPosition = decision.TargetCoordinate.PositionX + decision.TargetCoordinate.PositionY * GameplayConstants.MaxFormationRows; var result = _abilityManager.PerformAbility(decision.Actor, decision.Ability, targetPosition, decision.TargetFormation); if (result.FailureReason == null) { _battle.ActionsLeftPerFormation[aiFormation].Remove(decision.Actor); } return(result); } }
/// <summary> /// Performs an action for one Ai CombatEntity that is available to act this turn. /// </summary> private void PerformAiAction() { CombatAiDecision decision = null; IEnumerable <Formation> myAllies = null; IEnumerable <Formation> myEnemies = null; IEnumerable <CombatEntity> affectedEntities = null; CombatEntity nextActiveEntity = null; lock (_key) { var available = _battle.ActionsLeftPerFormation.Where(kvp => kvp.Key.OwnerId == GameplayConstants.AiId) .Where(kvp => kvp.Value != null && kvp.Value.Count > 0) .FirstOrDefault(); // If no available ai actions left in turn if (available.Equals(default(KeyValuePair <Formation, List <CombatEntity> >))) { return; } var myFormation = available.Key; var myEntity = available.Value.FirstOrDefault(); // If defenders turn, ai is a defender, all defenders are allies and attackers are enemies if (_battle.IsDefenderTurn) { myAllies = _battle.Defenders; myEnemies = _battle.Attackers; } else { myAllies = _battle.Attackers; myEnemies = _battle.Defenders; } decision = _combatAi.MakeDecision(myFormation, myEntity, myAllies, myEnemies); if (decision == null) { return; } if (decision.IsDefending) { affectedEntities = PerformDefend(myEntity, myFormation, out nextActiveEntity); } else { var result = PerformAiDecision(decision, myFormation); affectedEntities = result.AffectedEntities; CheckForDeadEntities(affectedEntities, decision.TargetFormation); } } Task.Run(() => SuccessfulActionEvent.Invoke(this, new SuccessfulActionEventArgs { Ability = decision.Ability, Actor = decision.Actor, AffectedEntities = affectedEntities, ParticipantIds = _participantIds, NextActiveEntityId = nextActiveEntity != null ? nextActiveEntity.Id : -1 })).Wait(); }