-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSiegeControl.cs
More file actions
60 lines (57 loc) · 2.04 KB
/
SiegeControl.cs
File metadata and controls
60 lines (57 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using HarmonyLib;
using TaleWorlds.MountAndBlade;
namespace FixSiegeAI
{
// Start using machine
[HarmonyPatch(typeof(ModuleExtensions), "StartUsingMachine")]
public static class Prefix_StartUsingMachine
{
public static bool Prefix(this Formation formation, UsableMachine usable, bool isPlayerOrder) // variable names must match exactly
{
if (formation.CountOfUnits < 1) { return true; }
if (!Main.IsPIC(formation) | formation.IsAIControlled) { return true; }
SiegeWeapon sw = (usable as SiegeWeapon);
Traverse.Create(formation).Method("JoinDetachment", usable).GetValue();
Main.Log("Formation starting using: " + sw.GetSiegeEngineType().ToString(), true);
return false;
}
}
// Stop using machine
[HarmonyPatch(typeof(ModuleExtensions), "StopUsingMachine")]
public static class Prefix_StopUsingMachine
{
public static bool Prefix(this Formation formation, UsableMachine usable, bool isPlayerOrder) // variable names must match exactly
{
if (formation.CountOfUnits < 1) { return true; }
if (!Main.IsPIC(formation) | formation.IsAIControlled) { return true; }
SiegeWeapon sw = (usable as SiegeWeapon);
Main.Log("Formation stopping using: " + sw.GetSiegeEngineType().ToString(), true);
Traverse.Create(formation).Method("LeaveDetachment", usable).GetValue();
return false;
}
}
// Stop troops using/following siege weapons
[HarmonyPatch(typeof(MovementOrder), "OnApply")]
public static class Patch_OnApply
{
public static bool Prefix(this Formation formation)
{
if (formation.CountOfUnits < 1) { return true; }
if (!Main.IsPIC(formation) | formation.IsAIControlled) { return true; }
foreach (SiegeWeapon sw in Mission.Current.ActiveMissionObjects.FindAllWithType<SiegeWeapon>())
{
if (sw.Side == Mission.Current.PlayerTeam.Side)
{
bool isUsing = formation.IsUsingMachine(sw);
if (formation.MovementOrder.OrderType == OrderType.StandYourGround & isUsing)
{
sw.ForcedUse = false;
formation.StopUsingMachine(sw, true);
continue;
};
}
}
return true;
}
}
}