diff --git a/Assets/Scripts/Model/Content/Core/Ship/CustomizedAi/CustomizedAi.cs b/Assets/Scripts/Model/Content/Core/Ship/CustomizedAi/CustomizedAi.cs index fc22cffb0c..ba8bada69d 100644 --- a/Assets/Scripts/Model/Content/Core/Ship/CustomizedAi/CustomizedAi.cs +++ b/Assets/Scripts/Model/Content/Core/Ship/CustomizedAi/CustomizedAi.cs @@ -1,10 +1,5 @@ using ActionsList; using Arcs; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Ship { @@ -15,10 +10,12 @@ public class CustomizedAi public delegate void EventHandlerShipWeaponInt(GenericShip targetShip, IShipWeapon weapon, ref int priority); public delegate void EventHandlerArcFacingInt(ArcFacing facing, ref int priority); public delegate void EventHandlerActionInt(GenericAction action, ref int priority); + public delegate void EventHandlerActionDbl(GenericAction action, ref double value); public event EventHandlerShipWeaponInt OnGetWeaponPriority; public event EventHandlerArcFacingInt OnGetRotateArcFacingPriority; public event EventHandlerActionInt OnGetActionPriority; + public event EventHandlerActionDbl OnRedActionPriorityModifier; public CustomizedAi(GenericShip host) { @@ -39,5 +36,10 @@ public void CallGetActionPriority(GenericAction action, ref int priority) { OnGetActionPriority?.Invoke(action, ref priority); } + + public void CallGetRedActionPriorityModifier(GenericAction action, ref double value) + { + OnRedActionPriorityModifier?.Invoke(action, ref value); + } } } diff --git a/Assets/Scripts/Model/Content/Core/Ship/GenericShipActions.cs b/Assets/Scripts/Model/Content/Core/Ship/GenericShipActions.cs index 9c77a8201d..1e123be4e6 100644 --- a/Assets/Scripts/Model/Content/Core/Ship/GenericShipActions.cs +++ b/Assets/Scripts/Model/Content/Core/Ship/GenericShipActions.cs @@ -725,8 +725,6 @@ public ActionColor CallOnCheckActionComplexity(GenericAction action, ref ActionC return color; } - // TODO: Added by sampson-matt, need to check difference against my CallOnCheckActionComplexity - public ActionColor CallOnCheckActionColor(GenericAction action, ref ActionColor color) { OnCheckActionColor?.Invoke(action, ref color); diff --git a/Assets/Scripts/Model/Players/AggressorAiPlayer.cs b/Assets/Scripts/Model/Players/AggressorAiPlayer.cs index b9cd51f118..9942430fd8 100644 --- a/Assets/Scripts/Model/Players/AggressorAiPlayer.cs +++ b/Assets/Scripts/Model/Players/AggressorAiPlayer.cs @@ -1,18 +1,14 @@ -using GameModes; +using ActionsList; +using GameModes; using Ship; using SubPhases; -using System; -using System.Collections; using System.Collections.Generic; using System.Linq; -using UnityEngine; namespace Players { - public partial class AggressorAiPlayer : GenericAiPlayer { - public AggressorAiPlayer() : base() { Name = "Aggressor AI"; @@ -43,7 +39,9 @@ private void CalculateNavigation() private void AssignManeuversRecursive() { - GenericShip shipWithoutManeuver = (!DebugManager.DebugStraightToCombat) ? AI.Aggressor.NavigationSubSystem.GetNextShipWithoutAssignedManeuver() : GetNextShipWithoutAssignedManeuver(); + GenericShip shipWithoutManeuver = (!DebugManager.DebugStraightToCombat) ? + AI.Aggressor.NavigationSubSystem.GetNextShipWithoutAssignedManeuver() : + GetNextShipWithoutAssignedManeuver(); if (shipWithoutManeuver != null) { @@ -59,8 +57,7 @@ private void AssignManeuversRecursive() private GenericShip GetNextShipWithoutAssignedManeuver() { return Roster.GetPlayer(Phases.CurrentSubPhase.RequiredPlayer).Ships.Values - .Where(n => n.AssignedManeuver == null && !n.State.IsIonized) - .FirstOrDefault(); + .FirstOrDefault(n => n.AssignedManeuver == null && !n.State.IsIonized); } private void OpenDirectionsUiSilent() @@ -90,37 +87,49 @@ protected override GenericShip SelectTargetForAttack() return AI.Aggressor.TargetingSubSystem.SelectTargetAndWeapon(Selection.ThisShip); } - protected override void PerformActionFromList(List actionsList) + protected override void PerformActionFromList(List actionsList) { bool isActionTaken = false; - List availableActionsList = actionsList; + List availableActionsList = actionsList; - Dictionary actionsPriority = new Dictionary(); + Dictionary actionsPriority = new(); - foreach (var action in availableActionsList) + GenericShip ship = Selection.ThisShip; + + foreach (GenericAction action in availableActionsList) { + ship.CallOnCheckActionComplexity(action, ref action.Color); + ship.CallOnCheckActionColor(action, ref action.Color); + int priority = action.GetActionPriority(); - Selection.ThisShip.Ai.CallGetActionPriority(action, ref priority); + ship.Ai.CallGetActionPriority(action, ref priority); - //Do not perform red action if this is not rotate arc action - if (action.IsRed && !(action is ActionsList.RotateArcAction)) priority = int.MinValue; + // De-prioritize red actions unless overriden + if (action.IsRed) + { + if (ship.IsStressed && !ship.CallCanPerformActionWhileStressed(action)) continue; + + double redActionPriorityModifier = 0.2; + ship.Ai.CallGetRedActionPriorityModifier(action, ref redActionPriorityModifier); + priority = (int)(priority * redActionPriorityModifier); + } actionsPriority.Add(action, priority); } - actionsPriority = actionsPriority.OrderByDescending(n => n.Value).ToDictionary(n => n.Key, n => n.Value); + actionsPriority = actionsPriority.OrderByDescending(n => n.Value) + .ToDictionary(n => n.Key, n => n.Value); if (actionsPriority.Count > 0) { - KeyValuePair prioritizedActions = actionsPriority.First(); + KeyValuePair prioritizedActions = actionsPriority.First(); if (prioritizedActions.Value > 0) { isActionTaken = true; - //Actions.TakeActionStart(prioritizedActions.Key); - JSONObject parameters = new JSONObject(); + JSONObject parameters = new(); parameters.AddField("name", prioritizedActions.Key.Name); GameController.SendCommand( GameCommandTypes.Decision, @@ -148,7 +157,10 @@ public override void PerformManeuver() { Roster.HighlightPlayer(PlayerNo); - GenericShip nextShip = (!DebugManager.DebugStraightToCombat) ? AI.Aggressor.NavigationSubSystem.GetNextShipWithoutFinishedManeuver() : GetNextShipWithoutFinishedManeuver(); + GenericShip nextShip = (!DebugManager.DebugStraightToCombat) ? + AI.Aggressor.NavigationSubSystem.GetNextShipWithoutFinishedManeuver() : + GetNextShipWithoutFinishedManeuver(); + if (nextShip != null) { Selection.ChangeActiveShip("ShipId:" + nextShip.ShipId); @@ -163,8 +175,7 @@ public override void PerformManeuver() private static GenericShip GetNextShipWithoutFinishedManeuver() { return Roster.GetPlayer(Phases.CurrentSubPhase.RequiredPlayer).Ships.Values - .Where(n => !n.IsManeuverPerformed) - .FirstOrDefault(); + .FirstOrDefault(n => !n.IsManeuverPerformed); } } } diff --git a/Assets/Scripts/Model/Rules/RulesList/ActionsRule.cs b/Assets/Scripts/Model/Rules/RulesList/ActionsRule.cs index 14ad65b8a3..cdb8007441 100644 --- a/Assets/Scripts/Model/Rules/RulesList/ActionsRule.cs +++ b/Assets/Scripts/Model/Rules/RulesList/ActionsRule.cs @@ -29,8 +29,7 @@ public void ActionColorCheck(GenericAction action) color = Selection.ThisShip.CallOnCheckActionComplexity(action, ref color); Selection.ThisShip.CallOnCheckActionColor(action, ref color); - //AI perfroms red actions as white, because it is hard to calculate correct priority of red action - if (color == ActionColor.Red && !(Selection.ThisShip.Owner is Players.GenericAiPlayer)) + if (color == ActionColor.Red) { Triggers.RegisterTrigger(new Trigger() {