Skip to content

Commit

Permalink
Utilize a shared list of CustomNavigationPoint objects instead of o…
Browse files Browse the repository at this point in the history
…ne per bot

Te only per-bot data was whether the bot could fire, and only one bot should be on a point at a time anyways
  • Loading branch information
DrakiaXYZ committed Apr 10, 2024
1 parent ca7a8fa commit 9eb3e72
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions DrakiaXYZ-Waypoints.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<Compile Include="Patches\BotVoxelesPersonalActivatePatch.cs" />
<Compile Include="Patches\FindPathPatch.cs" />
<Compile Include="Patches\GroupPointCachePatch.cs" />
<Compile Include="Patches\GroupPointGetByIdPatch.cs" />
<Compile Include="Patches\SwitchDoorBlockerPatch.cs" />
<Compile Include="Patches\DebugPatch.cs" />
<Compile Include="Patches\DoorLinkPatch.cs" />
Expand Down
11 changes: 3 additions & 8 deletions Patches/BotVoxelesPersonalActivatePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@ protected override MethodBase GetTargetMethod()
}

[PatchPrefix]
public static bool PatchPrefix(AICoversData data, BotOwner ___botOwner_0, ref AICoversData ____data, ref List<CustomNavigationPoint> ____allPoints, out Stopwatch __state)
public static bool PatchPrefix(AICoversData data, ref AICoversData ____data, ref List<CustomNavigationPoint> ____allPoints)
{
__state = new Stopwatch();
__state.Start();

var stopwatch = new Stopwatch();
stopwatch.Start();

____data = data;
int id = ___botOwner_0.Id;

foreach (var groupPoint in GroupPointCachePatch.CachedGroupPoints)
foreach (var navPoint in GroupPointCachePatch.CachedNavPoints)
{
____allPoints.Add(groupPoint.CreateCustomNavigationPoint(id));
____allPoints.Add(navPoint);
}

stopwatch.Stop();
Expand Down
12 changes: 6 additions & 6 deletions Patches/GroupPointCachePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace DrakiaXYZ.Waypoints.Patches
{
/**
* `CoversData` is static, so isntead of iterating through the 3d array on every bot spawn,
* `CoversData` is static, so instead of iterating through the 3d array on every bot spawn,
* iterate through it once on map load and cache the results
*/
public class GroupPointCachePatch : ModulePatch
{
public static List<GroupPoint> CachedGroupPoints = new List<GroupPoint>();
public static List<CustomNavigationPoint> CachedNavPoints = new List<CustomNavigationPoint>();

protected override MethodBase GetTargetMethod()
{
Expand All @@ -23,7 +23,7 @@ protected override MethodBase GetTargetMethod()
public static void PatchPostfix(GameWorld __instance)
{
// Clear before we add anything to it
CachedGroupPoints.Clear();
CachedNavPoints.Clear();

var botGame = Singleton<IBotGame>.Instance;
var data = botGame.BotsController.CoversData;
Expand All @@ -39,16 +39,16 @@ public static void PatchPostfix(GameWorld __instance)
{
foreach (GroupPoint groupPoint in navGraphVoxelSimple.Points)
{
CachedGroupPoints.Add(groupPoint);
CachedNavPoints.Add(groupPoint.CreateCustomNavigationPoint(0));
}
}
}
}
}

foreach (GroupPoint groupPoint2 in data.AIManualPointsHolder.ManualPoints)
foreach (GroupPoint groupPoint in data.AIManualPointsHolder.ManualPoints)
{
CachedGroupPoints.Add(groupPoint2);
CachedNavPoints.Add(groupPoint.CreateCustomNavigationPoint(0));
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions Patches/GroupPointGetByIdPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Aki.Reflection.Patching;
using System.Collections.Generic;
using System.Reflection;

namespace DrakiaXYZ.Waypoints.Patches
{
public class GroupPointGetByIdPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(GroupPoint).GetMethod(nameof(GroupPoint.GetById));
}

[PatchPrefix]
public static bool PatchPrefix(Dictionary<int, CustomNavigationPoint> ____childs, ref CustomNavigationPoint __result)
{
__result = ____childs[0];

// Skip original
return false;
}
}
}
29 changes: 14 additions & 15 deletions WaypointsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void Awake()
new FindPathPatch().Enable();
new GroupPointCachePatch().Enable();
new BotVoxelesPersonalActivatePatch().Enable();
new GroupPointGetByIdPatch().Enable();

// Debug perf timing output
//new PerfTimingPatch().Enable();
Expand All @@ -64,23 +65,21 @@ public class PerfTimingPatch

public void Enable()
{
Logger.LogInfo($"Patching in {Assembly.GetExecutingAssembly()}");

var harmony = new Harmony("xyz.drakia.waypoints");

var props = AccessTools.GetDeclaredProperties(typeof(BotOwner));
foreach (var prop in props)
{
var method = prop.PropertyType.GetMethod("Activate");
if (method != null && !method.IsAbstract)
{
Logger.LogInfo($"Adding timing to {prop.PropertyType.Name}::{method.Name}");
var target = method;
var prefix = new HarmonyMethod(typeof(PerfTimingPatch).GetMethod("PatchPrefix"));
var postfix = new HarmonyMethod(typeof(PerfTimingPatch).GetMethod("PatchPostfix"));
harmony.Patch(target, prefix, postfix);
}
}
//var props = AccessTools.GetDeclaredProperties(typeof(BotOwner));
//foreach (var prop in props)
//{
// var method = prop.PropertyType.GetMethod("Activate");
// if (method != null && !method.IsAbstract)
// {
// Logger.LogInfo($"Adding timing to {prop.PropertyType.Name}::{method.Name}");
// var target = method;
// var prefix = new HarmonyMethod(typeof(PerfTimingPatch).GetMethod("PatchPrefix"));
// var postfix = new HarmonyMethod(typeof(PerfTimingPatch).GetMethod("PatchPostfix"));
// harmony.Patch(target, prefix, postfix);
// }
//}

// Time the overall activate method
{
Expand Down

0 comments on commit 9eb3e72

Please sign in to comment.