Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions Order.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using FormationSorter.Utilities;
using HarmonyLib;
using TaleWorlds.Core;
using TaleWorlds.Engine;
using TaleWorlds.InputSystem;
Expand Down Expand Up @@ -52,7 +54,7 @@ private static void RefreshOrderVMs()
Mission.OrderItemVM
= new(OrderSubType.None, OrderSetType.None, new(OrderText), (_, _) => { }) { ShortcutKey = Mission.InputKeyItemVM, IsTitle = true };
Mission.OrderItemVM.IsTitle = true;
Mission.OrderItemVM.TooltipText = OrderText;
//Mission.OrderItemVM.TooltipText = OrderText;
Mission.OrderItemVM.OrderIconID = OrderIcon;
Mission.OrderItemVM.ShortcutKey = Mission.InputKeyItemVM;
Mission.OrderItemVM.IsActive = true;
Expand Down Expand Up @@ -103,14 +105,14 @@ internal static void OnOrder(bool tierSort = false, bool equalSort = false, bool
else
InformationManager.DisplayMessage(new("No troops need sorting between the selected formations", Colors.White, SubModule.Id));
return;
default:
{
if (Mission.IsOrderShoutingAllowed())
Mission.PlayerAgent.MakeVoice(SkinVoiceManager.VoiceType.MpRegroup, SkinVoiceManager.CombatVoiceNetworkPredictionType.NoPrediction);
InformationManager.DisplayMessage(new($"Sorted {numUnitsSorted} {(numUnitsSorted == 1 ? "troop" : "troops")} between the selected formations",
Colors.White, SubModule.Id));
break;
}
default:
{
if (Mission.IsOrderShoutingAllowed())
Mission.PlayerAgent.MakeVoice(SkinVoiceManager.VoiceType.MpRegroup, SkinVoiceManager.CombatVoiceNetworkPredictionType.NoPrediction);
InformationManager.DisplayMessage(new($"Sorted {numUnitsSorted} {(numUnitsSorted == 1 ? "troop" : "troops")} between the selected formations",
Colors.White, SubModule.Id));
break;
}
}
_ = Mission.MissionOrderVM.TryCloseToggleOrder();
}
Expand Down Expand Up @@ -180,24 +182,36 @@ private static int SortAgentsBetweenFormations(HashSet<Formation> formations, bo
ref numAgentsSorted, ref classesWithMissingFormations, ref changedFormations);
}
MethodInfo alternativeClass = typeof(ModuleExtensions).GetCachedMethod("AlternativeClass"); // only v1.1.0+
MethodInfo fallbackClass = typeof(ModuleExtensions).GetCachedMethod("FallbackClass"); // dynamically get FallbackClass
if (filledFormations.Count > 0)
{
foreach (Formation formation in emptyFormations)
{
FormationClass formationClass = formation.GetFormationClass();
Formation forCopy = filledFormations.FirstOrDefault(f => f.CountOfUnits > 0 && f.GetFormationClass() == formationClass)
?? filledFormations.FirstOrDefault(f => f.CountOfUnits > 0 && f.GetFormationClass() == formationClass.FallbackClass())
?? (alternativeClass is null
? null
: filledFormations.FirstOrDefault(f
=> f.CountOfUnits > 0 && f.GetFormationClass()
== (FormationClass)alternativeClass.Invoke(null, new object[] { formationClass })))
?? filledFormations.FirstOrDefault(f => f.CountOfUnits > 0);
?? (fallbackClass is null
? null
: filledFormations.FirstOrDefault(f
=> f.CountOfUnits > 0 && f.GetFormationClass()
== (FormationClass)fallbackClass.Invoke(null, new object[] { formationClass })
)
)
?? (alternativeClass is null
? null
: filledFormations.FirstOrDefault(f
=> f.CountOfUnits > 0 && f.GetFormationClass()
== (FormationClass)alternativeClass.Invoke(null, new object[] { formationClass })
)
)
?? filledFormations.FirstOrDefault(f => f.CountOfUnits > 0);

if (forCopy is null)
continue;
_ = typeof(Formation).GetCachedMethod("CopyOrdersFrom").Invoke(formation, new object[] { forCopy });
formation.SetPositioning(forCopy.CreateNewOrderWorldPosition(WorldPosition.WorldPositionEnforcedCache.None), forCopy.Direction,
forCopy.UnitSpacing);
}
}
foreach (Formation formation in changedFormations)
formation.Team.TriggerOnFormationsChanged(formation);
foreach (Formation formation in formations)
Expand Down Expand Up @@ -263,7 +277,9 @@ private static void SortAgentsBetweenFormationsInternal(IEnumerable<Agent> agent
int tier = agent.GetTier();
int index = bestFormationClass switch
{
FormationClass.Ranged => 3, FormationClass.HorseArcher => 7, FormationClass.Infantry => tier <= 2
FormationClass.Ranged => 3,
FormationClass.HorseArcher => 7,
FormationClass.Infantry => tier <= 2
? 0
: tier <= 4
? 1
Expand Down Expand Up @@ -358,15 +374,25 @@ private static int GetTier(this IAgent agent)
? 0
: MathF.Min(MathF.Max(MathF.Ceiling((agent.Character.Level - 5f) / 5f), 0), 7); // from Helpers.CharacterHelper.GetCharacterTier

private static IEnumerable<Agent> EnumerateAgentsInFormations(IEnumerable<Formation> formations)
{
foreach (Formation formation in formations.Where(formation => !formation.IsAIControlled))
{
foreach (Agent agent in formation.DetachedUnits.Where(CheckAgent))
yield return agent;
foreach (Agent agent in formation.Arrangement.GetAllUnits().Cast<Agent>().Where(CheckAgent))
yield return agent;
}
private static IEnumerable<Agent> EnumerateAgentsInFormations(IEnumerable<Formation> formations)
{
MethodInfo getAllUnitsMethod = typeof(Formation).GetProperty("Arrangement")?.PropertyType.GetMethod("GetAllUnits"); // dynamically get GetAllUnits

foreach (Formation formation in formations.Where(formation => !formation.IsAIControlled))
{
foreach (Agent agent in formation.DetachedUnits.Where(CheckAgent))
yield return agent;

if (getAllUnitsMethod != null)
{
IEnumerable allUnits = getAllUnitsMethod.Invoke(formation.Arrangement, null) as IEnumerable;
if (allUnits != null)
{
foreach (Agent agent in allUnits.Cast<Agent>().Where(CheckAgent))
yield return agent;
}
}
}
}

private static bool CheckAgent(Agent agent) => agent != Mission.PlayerAgent && agent.IsHuman;
Expand Down