Skip to content
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
2 changes: 0 additions & 2 deletions Assets/Scripts/Model/Content/Core/Ship/GenericShipActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
57 changes: 34 additions & 23 deletions Assets/Scripts/Model/Players/AggressorAiPlayer.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
Expand Down Expand Up @@ -90,37 +87,49 @@ protected override GenericShip SelectTargetForAttack()
return AI.Aggressor.TargetingSubSystem.SelectTargetAndWeapon(Selection.ThisShip);
}

protected override void PerformActionFromList(List<ActionsList.GenericAction> actionsList)
protected override void PerformActionFromList(List<GenericAction> actionsList)
{
bool isActionTaken = false;

List<ActionsList.GenericAction> availableActionsList = actionsList;
List<GenericAction> availableActionsList = actionsList;

Dictionary<ActionsList.GenericAction, int> actionsPriority = new Dictionary<ActionsList.GenericAction, int>();
Dictionary<GenericAction, int> 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<ActionsList.GenericAction, int> prioritizedActions = actionsPriority.First();
KeyValuePair<GenericAction, int> 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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
3 changes: 1 addition & 2 deletions Assets/Scripts/Model/Rules/RulesList/ActionsRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down