Skip to content

Commit bc37530

Browse files
committed
- Removed ___ fields from various classes
1 parent e98d843 commit bc37530

5 files changed

Lines changed: 91 additions & 87 deletions

File tree

CleverGirl/CleverGirl/Helper/BehaviorHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private static BehaviorVariableValue GetBehaviorVariableValueDirectly(BehaviorT
8080

8181
if (bTree.unit.team != null)
8282
{
83-
Traverse bvT = Traverse.Create(bTree.unit.team).Field("BehaviorVariables");
84-
BehaviorVariableScope bvs = bvT.GetValue<BehaviorVariableScope>();
83+
BehaviorVariableScope bvs = bTree.unit.team.BehaviorVariables;
8584
behaviorVariableValue = bvs.GetVariable(name);
8685
if (behaviorVariableValue != null)
8786
{

CleverGirl/CleverGirl/InfluenceMap/CompoundInfluenceMapEvaluator.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public CompoundInfluenceMapEvaluator(InfluenceMapEvaluator hbsEvaluator)
2020
public IEnumerable<Instruction> IncrementalEvaluate()
2121
{
2222
// Set the unit field, which is used by most methods
23-
Traverse unitT = Traverse.Create(hbsIME).Field("unit");
24-
this.unit = unitT.GetValue<AbstractActor>();
23+
this.unit = hbsIME.unit;
2524

2625
Mod.Log.Info?.Write($"Initializing the evaluation");
2726
yield return ControlFlow.Call(Eval_Initialize());
@@ -40,8 +39,7 @@ public IEnumerable<Instruction> IncrementalEvaluate()
4039

4140
hbsIME.expectedDamageFactor.LogEvaluation();
4241

43-
Traverse evaluationCompleteT = Traverse.Create(hbsIME).Field("evaluationComplete");
44-
evaluationCompleteT.SetValue(true);
42+
hbsIME.evaluationComplete = true;
4543

4644
yield return null;
4745
}
@@ -145,8 +143,7 @@ private IEnumerable<Instruction> Eval_PositionalFactors()
145143
workspaceIndex = num;
146144
}
147145

148-
Traverse positionalFactorsT = Traverse.Create(hbsIME).Field("positionalFactors");
149-
InfluenceMapPositionFactor[] positionalFactors = positionalFactorsT.GetValue<InfluenceMapPositionFactor[]>();
146+
InfluenceMapPositionFactor[] positionalFactors = hbsIME.positionalFactors;
150147
Mod.Log.Info?.Write($"Evaluating {positionalFactors.Length} position factors");
151148

152149
int posFactorIndex = 0;
@@ -254,8 +251,7 @@ private IEnumerable<Instruction> Eval_HostileFactors()
254251
List<ICombatant> hostiles = getNClosestCombatants(unit.BehaviorTree.enemyUnits, intVal);
255252
AIUtil.LogAI($"evaluating vs {hostiles.Count} hostiles");
256253

257-
Traverse hostileFactorsT = Traverse.Create(hbsIME).Field("hostileFactors");
258-
InfluenceMapHostileFactor[] hostileFactors = hostileFactorsT.GetValue<InfluenceMapHostileFactor[]>();
254+
InfluenceMapHostileFactor[] hostileFactors = hbsIME.hostileFactors;
259255
Mod.Log.Info?.Write($"Evaluating {hostileFactors.Length} hostile factors");
260256

261257
int hostileFactorIndex = 0;
@@ -350,8 +346,7 @@ private IEnumerable<Instruction> Eval_AllyFactors()
350346
List<ICombatant> allies = getNClosestCombatants(unit.BehaviorTree.GetAllyUnits().ConvertAll((Converter<AbstractActor, ICombatant>)((AbstractActor X) => X)), intVal);
351347
AIUtil.LogAI($"evaluating vs {allies.Count} allies");
352348

353-
Traverse allyFactorsT = Traverse.Create(hbsIME).Field("allyFactors");
354-
InfluenceMapAllyFactor[] allyFactors = allyFactorsT.GetValue<InfluenceMapAllyFactor[]>();
349+
InfluenceMapAllyFactor[] allyFactors = hbsIME.allyFactors;
355350
Mod.Log.Info?.Write($"Evaluating {allyFactors.Length} ally factors");
356351

357352
int allyFactorIndex = 0;

CleverGirl/CleverGirl/Patches/BehaviorNodes/GenerateJumpMoveCandidatesNodePatch.cs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,60 @@
22
using System.Reflection;
33
using UnityEngine;
44

5-
namespace CleverGirl.Patches {
5+
namespace CleverGirl.Patches
6+
{
67

7-
[HarmonyPatch]
8-
static class GenerateJumpMoveCandidatesNode_Tick {
8+
[HarmonyPatch(typeof(GenerateJumpMoveCandidatesNode), nameof(GenerateJumpMoveCandidatesNode.Tick))]
9+
static class GenerateJumpMoveCandidatesNode_Tick
10+
{
911

10-
static MethodBase TargetMethod() {
11-
var type = AccessTools.TypeByName("GenerateJumpMoveCandidatesNode");
12-
return AccessTools.Method(type, "Tick");
13-
}
1412

1513
// Duplication of HBS code, avoiding prefix=true for now.
1614
[HarmonyPostfix]
17-
static void Postfix(ref BehaviorTreeResults __result, string ___name, BehaviorTree ___tree, AbstractActor ___unit) {
15+
static void Postfix(GenerateJumpMoveCandidatesNode __instance, ref BehaviorTreeResults __result)
16+
{
1817
Mod.Log.Trace?.Write("CJMCN:T - entered");
1918

20-
Mech mech = ___unit as Mech;
21-
if (mech != null && mech.WorkingJumpjets > 0) {
22-
string stayInsideRegionGUID = RegionUtil.GetStayInsideRegionGUID(___unit);
23-
19+
BehaviorTree nodeBehaviorTree = __instance.tree;
20+
AbstractActor nodeContextUnit = __instance.unit;
21+
22+
Mech mech = nodeContextUnit as Mech;
23+
if (mech != null && mech.WorkingJumpjets > 0)
24+
{
25+
string stayInsideRegionGUID = RegionUtil.GetStayInsideRegionGUID(nodeContextUnit);
26+
2427
float acceptableHeat = AIUtil.GetAcceptableHeatLevelForMech(mech);
2528
float currentHeat = (float)mech.CurrentHeat;
2629
Mod.Log.Info?.Write($"CJMCN:T - === actor:{mech.DistinctId()} has currentHeat:{currentHeat} and acceptableHeat:{acceptableHeat}");
2730

28-
List<PathNode> sampledPathNodes = ___unit.JumpPathing.GetSampledPathNodes();
31+
List<PathNode> sampledPathNodes = nodeContextUnit.JumpPathing.GetSampledPathNodes();
2932
Mod.Log.Info?.Write($"CJMCN:T - calculating {sampledPathNodes.Count} nodes");
30-
for (int i = 0; i < sampledPathNodes.Count; i++) {
33+
for (int i = 0; i < sampledPathNodes.Count; i++)
34+
{
3135
Vector3 candidatePos = sampledPathNodes[i].Position;
32-
float distanceBetween2D = AIUtil.Get2DDistanceBetweenVector3s(candidatePos, ___unit.CurrentPosition);
33-
float distanceBetween3D = Vector3.Distance(candidatePos, ___unit.CurrentPosition);
36+
float distanceBetween2D = AIUtil.Get2DDistanceBetweenVector3s(candidatePos, nodeContextUnit.CurrentPosition);
37+
float distanceBetween3D = Vector3.Distance(candidatePos, nodeContextUnit.CurrentPosition);
3438
Mod.Log.Info?.Write($"CJMCN:T - calculated distances 2D:'{distanceBetween2D}' 3D:'{distanceBetween3D} ");
35-
if (distanceBetween2D >= 1f) {
36-
float magnitude = (candidatePos - ___unit.CurrentPosition).magnitude;
39+
if (distanceBetween2D >= 1f)
40+
{
41+
float magnitude = (candidatePos - nodeContextUnit.CurrentPosition).magnitude;
3742
float jumpHeat = (float)mech.CalcJumpHeat(magnitude);
3843
Mod.Log.Info?.Write($"CJMCN:T - calculated jumpHeat:'{jumpHeat}' from magnitude:'{magnitude}. ");
3944

4045
Mod.Log.Info?.Write($"CJMCN:T - comparing heat: [jumpHeat:'{jumpHeat}' + currentHeat:'{currentHeat}'] <= acceptableHeat:'{acceptableHeat}. ");
41-
if (jumpHeat + (float)mech.CurrentHeat <= acceptableHeat) {
46+
if (jumpHeat + (float)mech.CurrentHeat <= acceptableHeat)
47+
{
4248

43-
if (stayInsideRegionGUID != null) {
44-
MapTerrainDataCell cellAt = ___unit.Combat.MapMetaData.GetCellAt(candidatePos);
45-
if (cellAt != null) {
49+
if (stayInsideRegionGUID != null)
50+
{
51+
MapTerrainDataCell cellAt = nodeContextUnit.Combat.MapMetaData.GetCellAt(candidatePos);
52+
if (cellAt != null)
53+
{
4654
MapEncounterLayerDataCell mapEncounterLayerDataCell = cellAt.MapEncounterLayerDataCell;
47-
if (mapEncounterLayerDataCell != null
48-
&& mapEncounterLayerDataCell.regionGuidList != null
49-
&& !mapEncounterLayerDataCell.regionGuidList.Contains(stayInsideRegionGUID)) {
55+
if (mapEncounterLayerDataCell != null
56+
&& mapEncounterLayerDataCell.regionGuidList != null
57+
&& !mapEncounterLayerDataCell.regionGuidList.Contains(stayInsideRegionGUID))
58+
{
5059

5160
// Skip this loop iteration if
5261
Mod.Log.Info?.Write($"CJMCN:T - candidate outside of constraint region, ignoring.");
@@ -56,7 +65,7 @@ static void Postfix(ref BehaviorTreeResults __result, string ___name, BehaviorTr
5665
}
5766

5867
Mod.Log.Info?.Write($"CJMCN:T - adding candidate position:{candidatePos}");
59-
___tree.movementCandidateLocations.Add(new MoveDestination(sampledPathNodes[i], MoveType.Jumping));
68+
nodeBehaviorTree.movementCandidateLocations.Add(new MoveDestination(sampledPathNodes[i], MoveType.Jumping));
6069
}
6170
}
6271

CleverGirl/CleverGirl/Patches/BehaviorNodes/MoveTowardsHighestPriorityMoveCandidateNodePatch.cs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,41 @@
44

55
namespace CleverGirl.Patches.BehaviorNodes
66
{
7-
[HarmonyPatch]
7+
[HarmonyPatch(typeof(MoveTowardsHighestPriorityMoveCandidateNode), nameof(MoveTowardsHighestPriorityMoveCandidateNode.Tick))]
88
static class MoveTowardsHighestPriorityMoveCandidateNode_Tick
9-
{
10-
11-
static MethodBase TargetMethod()
12-
{
13-
var type = AccessTools.TypeByName("MoveTowardsHighestPriorityMoveCandidateNode");
14-
return AccessTools.Method(type, "Tick");
15-
}
9+
{
1610

1711
[HarmonyPostfix]
18-
static void Postfix(ref BehaviorTreeResults __result, string ___name, BehaviorTree ___tree, AbstractActor ___unit, bool ___useSprintJuice)
12+
static void Postfix(MoveTowardsHighestPriorityMoveCandidateNode __instance, ref BehaviorTreeResults __result)
1913
{
20-
if (__result == null || !(__result.orderInfo is AttackOrderInfo)) return; // Nothing to do
21-
22-
Mod.Log.Info?.Write($"MoveTowardsHighestPriorityMoveCandidateNode generated an AttackOrder for unit: {___unit.DistinctId()}, without evaluating CBTBE options!");
23-
Mod.Log.Info?.Write($" Disabling the attack but leaving the move oder intact.");
14+
if (__result == null || !(__result.orderInfo is AttackOrderInfo)) return; // Nothing to do
2415

25-
// Recalculate the best movement type
26-
WorkspaceEvaluationEntry workspaceEvaluationEntry = ___tree.influenceMapEvaluator.WorkspaceEvaluationEntries[0];
27-
MoveType bestMoveType = workspaceEvaluationEntry.GetBestMoveType();
28-
Vector3 vector = workspaceEvaluationEntry.Position;
29-
BehaviorVariableValue behaviorVariableValue = BehaviorHelper.GetCachedBehaviorVariableValue(___tree, BehaviorVariableName.String_StayInsideRegionGUID);
30-
if (behaviorVariableValue != null || behaviorVariableValue.StringVal.Length == 0)
31-
{
32-
vector = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(___unit, vector);
33-
}
34-
float angle = workspaceEvaluationEntry.Angle;
35-
Vector3 vector2 = Quaternion.Euler(0f, angle, 0f) * Vector3.forward;
36-
Vector3 lookAt = vector + vector2;
16+
BehaviorTree NodeBehaviorTree = __instance.tree;
17+
AbstractActor NodeContextUnit = __instance.unit;
3718

38-
MovementOrderInfo orderInfo = new MovementOrderInfo(vector, lookAt);
39-
orderInfo.IsReverse = bestMoveType == MoveType.Backward;
40-
orderInfo.IsJumping = bestMoveType == MoveType.Jumping;
41-
orderInfo.IsSprinting = bestMoveType == MoveType.Sprinting;
19+
Mod.Log.Info?.Write($"MoveTowardsHighestPriorityMoveCandidateNode generated an AttackOrder for unit: {NodeContextUnit.DistinctId()}, without evaluating CBTBE options!");
20+
Mod.Log.Info?.Write($" Disabling the attack but leaving the move oder intact.");
4221

43-
Mod.Log.Info?.Write($" Returning MovementOrderInfo with isReverse: {orderInfo.IsReverse} isJumping: {orderInfo.IsJumping} isSprinting: {orderInfo.IsSprinting}.");
44-
__result.orderInfo = orderInfo;
45-
}
22+
// Recalculate the best movement type
23+
WorkspaceEvaluationEntry workspaceEvaluationEntry = NodeBehaviorTree.influenceMapEvaluator.WorkspaceEvaluationEntries[0];
24+
MoveType bestMoveType = workspaceEvaluationEntry.GetBestMoveType();
25+
Vector3 vector = workspaceEvaluationEntry.Position;
26+
BehaviorVariableValue behaviorVariableValue = BehaviorHelper.GetCachedBehaviorVariableValue(NodeBehaviorTree, BehaviorVariableName.String_StayInsideRegionGUID);
27+
if (behaviorVariableValue != null || behaviorVariableValue.StringVal.Length == 0)
28+
{
29+
vector = RegionUtil.MaybeClipMovementDestinationToStayInsideRegion(NodeContextUnit, vector);
30+
}
31+
float angle = workspaceEvaluationEntry.Angle;
32+
Vector3 vector2 = Quaternion.Euler(0f, angle, 0f) * Vector3.forward;
33+
Vector3 lookAt = vector + vector2;
34+
35+
MovementOrderInfo orderInfo = new MovementOrderInfo(vector, lookAt);
36+
orderInfo.IsReverse = bestMoveType == MoveType.Backward;
37+
orderInfo.IsJumping = bestMoveType == MoveType.Jumping;
38+
orderInfo.IsSprinting = bestMoveType == MoveType.Sprinting;
39+
40+
Mod.Log.Info?.Write($" Returning MovementOrderInfo with isReverse: {orderInfo.IsReverse} isJumping: {orderInfo.IsJumping} isSprinting: {orderInfo.IsSprinting}.");
41+
__result.orderInfo = orderInfo;
42+
}
4643
}
4744
}

CleverGirl/CleverGirl/Patches/InfluenceMapEvaluatorPatches.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@ static class InfluenceMapEvaluator_ctor
1212
{
1313
// Add custom
1414
[HarmonyPostfix]
15-
static void Postfix(InfluenceMapEvaluator __instance, ref InfluenceMapAllyFactor[] ___allyFactors,
16-
ref InfluenceMapHostileFactor[] ___hostileFactors, ref InfluenceMapPositionFactor[] ___positionalFactors,
17-
ref PreferHigherExpectedDamageToHostileFactor ___expectedDamageFactor)
15+
static void Postfix(InfluenceMapEvaluator __instance)
1816
{
19-
List<InfluenceMapAllyFactor> allyFactors = new List<InfluenceMapAllyFactor>(___allyFactors);
17+
InfluenceMapAllyFactor[] ___allyFactors = __instance.allyFactors;
18+
InfluenceMapHostileFactor[] ___hostileFactors = __instance.hostileFactors;
19+
InfluenceMapPositionFactor[] ___positionalFactors = __instance.positionalFactors;
20+
21+
List<InfluenceMapAllyFactor> mergedAllyFactors = new List<InfluenceMapAllyFactor>(___allyFactors);
2022
Mod.Log.Info?.Write($"Adding {ModState.CustomAllyFactors.Count} custom ally factors to influence map.");
21-
allyFactors.AddRange(ModState.CustomAllyFactors);
22-
___allyFactors = allyFactors.ToArray();
23+
mergedAllyFactors.AddRange(ModState.CustomAllyFactors);
24+
__instance.allyFactors = mergedAllyFactors.ToArray();
2325

24-
List<InfluenceMapHostileFactor> hostileFactors = new List<InfluenceMapHostileFactor>(___hostileFactors);
26+
List<InfluenceMapHostileFactor> mergedHostileFactors = new List<InfluenceMapHostileFactor>(___hostileFactors);
2527
Mod.Log.Info?.Write($"Adding {ModState.CustomHostileFactors.Count} custom hostile factors to influence map.");
26-
hostileFactors.AddRange(ModState.CustomHostileFactors);
27-
___hostileFactors = hostileFactors.ToArray();
28+
mergedHostileFactors.AddRange(ModState.CustomHostileFactors);
29+
__instance.hostileFactors = mergedHostileFactors.ToArray();
2830

29-
List<InfluenceMapPositionFactor> positionFactors = new List<InfluenceMapPositionFactor>(___positionalFactors);
31+
List<InfluenceMapPositionFactor> mergedPositionalFactors = new List<InfluenceMapPositionFactor>(___positionalFactors);
3032
Mod.Log.Info?.Write($"Adding {ModState.CustomPositionFactors.Count} custom position factors to influence map.");
31-
positionFactors.AddRange(ModState.CustomPositionFactors);
32-
___positionalFactors = positionFactors.ToArray();
33+
mergedPositionalFactors.AddRange(ModState.CustomPositionFactors);
34+
__instance.positionalFactors = mergedPositionalFactors.ToArray();
3335

34-
}
36+
}
3537
}
3638

3739

@@ -40,27 +42,29 @@ static void Postfix(InfluenceMapEvaluator __instance, ref InfluenceMapAllyFactor
4042
static class InfluenceMapEvaluator_RunEvaluationForSeconds
4143
{
4244
[HarmonyPrefix]
43-
static void Prefix(ref bool __runOriginal, InfluenceMapEvaluator __instance, float seconds, ref bool __result,
44-
ref GraphCoroutine ___evaluationCoroutine, bool ___evaluationComplete)
45+
static void Prefix(ref bool __runOriginal, InfluenceMapEvaluator __instance, float seconds, ref bool __result)
4546
{
4647
if (!__runOriginal) return;
4748

49+
GraphCoroutine ___evaluationCoroutine = __instance.evaluationCoroutine;
50+
bool ___evaluationComplete = __instance.evaluationComplete;
51+
4852
Mod.Log.Trace?.Write("AIU:CDT:Post");
4953

5054
float realtimeSinceStartup = Time.realtimeSinceStartup;
5155
if (___evaluationCoroutine == null)
5256
{
5357
Mod.Log.Info?.Write("Generating new CompoundInfluenceMapEvaluator");
5458
CompoundInfluenceMapEvaluator cime = new CompoundInfluenceMapEvaluator(__instance);
55-
___evaluationCoroutine = new GraphCoroutine(cime.IncrementalEvaluate());
59+
__instance.evaluationCoroutine = new GraphCoroutine(cime.IncrementalEvaluate());
5660
}
5761

5862
while (Time.realtimeSinceStartup - realtimeSinceStartup <= seconds)
5963
{
6064
___evaluationCoroutine.Update();
6165
if (___evaluationComplete)
6266
{
63-
___evaluationCoroutine = null;
67+
__instance.evaluationCoroutine = null;
6468
break;
6569
}
6670
}

0 commit comments

Comments
 (0)