Skip to content

Commit

Permalink
Fix placement bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sourpuh committed Dec 8, 2024
1 parent df3d134 commit df70d2e
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions WaymarkStudio/WaymarkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class WaymarkManager
internal Dictionary<Waymark, Vector3> placeholders = new();
internal IReadOnlyDictionary<Waymark, Vector3> hoverPreviews = EmptyWaymarks;
private readonly Stopwatch lastPlacementTimer;
private List<(Waymark waymark, Vector3 wPos)> safePlaceQueue = new();

private unsafe delegate byte PlaceWaymark(MarkingController* markingController, uint marker, Vector3 wPos);
private readonly PlaceWaymark placeWaymarkFn;
Expand Down Expand Up @@ -74,6 +75,7 @@ internal void OnTerritoryChange(TerritoryType territory)
hoverPreviews = EmptyWaymarks;
showGuide = false;
guide = new CircleGuide(radius: 1);
safePlaceQueue.Clear();
}
internal void ClearPlaceholders()
{
Expand Down Expand Up @@ -103,7 +105,7 @@ private bool IsPointGrounded(Vector3 point)
Vector3 castOrigin = point + castOffset;
if (BGCollisionModule.RaycastMaterialFilter(castOrigin, -Vector3.UnitY, out RaycastHit hitInfo, castHeight))
{
return point == hitInfo.Point.Round();
return point.Y == hitInfo.Point.Round().Y;
}
return false;
}
Expand Down Expand Up @@ -166,25 +168,6 @@ internal void ClearWaymarkPlaceholder(Waymark waymark)
placeholders.Remove(waymark);
}

internal async void PlaceWaymarkWithRetries(Waymark waymark, Vector3 wPos, bool clearPlaceholder = true)
{
ushort territoryId = this.territoryId;
await Plugin.Framework.Run(async () =>
{
// TODO tidy
while (!SafePlaceWaymark(waymark, wPos))
{
await Plugin.Framework.DelayTicks(100);
if (territoryId != this.territoryId)
{
return;
}
}
if (clearPlaceholder && placeholders.GetValueOrDefault(waymark) == wPos)
placeholders.Remove(waymark);
});
}

internal bool SafePlaceWaymark(Waymark waymark, Vector3 wPos)
{
var reason = WaymarkPlacementStatus(wPos);
Expand Down Expand Up @@ -257,11 +240,36 @@ public void SafePlacePreset(WaymarkPreset preset, bool clearPlaceholder = true,
foreach (Waymark w in Enum.GetValues<Waymark>())
{
if (preset.MarkerPositions.TryGetValue(w, out var wPos))
Plugin.WaymarkManager.PlaceWaymarkWithRetries(w, wPos, clearPlaceholder);
safePlaceQueue.Add((w, wPos));
}
processSafePlaceQueue(clearPlaceholder);
}
}

internal async void processSafePlaceQueue(bool clearPlaceholder = true)
{
await Plugin.Framework.Run(async () =>
{
var territoryId = this.territoryId;
int attempts = safePlaceQueue.Count + 2;
while (safePlaceQueue.Count > 0 && territoryId == this.territoryId && attempts-- > 0)
{
(Waymark waymark, Vector3 wPos) = safePlaceQueue[0];
safePlaceQueue.RemoveAt(0);
if (SafePlaceWaymark(waymark, wPos))
if (clearPlaceholder && placeholders.GetValueOrDefault(waymark) == wPos)
placeholders.Remove(waymark);
else
// requeue failure in case it was retriable
// TODO actually differentiate between retriable and not
safePlaceQueue.Add((waymark, wPos));
await Plugin.Framework.DelayTicks(50);
}
safePlaceQueue.Clear();
Plugin.Chat.Print("Placement complete");
});
}

private unsafe bool UnsafeNativePlacePreset(FieldMarkerPreset preset)
{
var placementStruct = preset.ToMarkerPresetPlacement();
Expand Down

0 comments on commit df70d2e

Please sign in to comment.