| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Pathing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Tracks all authored chart cells that claim one voxel and resolves the winning effective cell. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class ResolvedChartVoxelState |
| | | 11 | | { |
| | 2941 | 12 | | private readonly SwiftDictionary<string, ChartContribution> _chartContributions = |
| | 2941 | 13 | | new(4, StringComparer.Ordinal); |
| | | 14 | | |
| | | 15 | | private int _effectivePriority; |
| | | 16 | | |
| | | 17 | | private int _effectiveRegistrationOrder; |
| | | 18 | | |
| | 2050 | 19 | | public bool HasAnyOwners => _chartContributions.Count > 0; |
| | | 20 | | |
| | | 21 | | public string? EffectiveChartOwner { get; private set; } |
| | | 22 | | |
| | | 23 | | public NavigationChartCell EffectiveCell { get; private set; } |
| | | 24 | | |
| | | 25 | | public void AddOwner( |
| | | 26 | | string chartName, |
| | | 27 | | NavigationChartCell cell, |
| | | 28 | | int priority, |
| | | 29 | | int registrationOrder) |
| | | 30 | | { |
| | 2966 | 31 | | var contribution = new ChartContribution(cell, priority, registrationOrder); |
| | 2966 | 32 | | _chartContributions[chartName] = contribution; |
| | | 33 | | |
| | 2966 | 34 | | if (EffectiveChartOwner == null |
| | 2966 | 35 | | || string.Equals(chartName, EffectiveChartOwner, StringComparison.Ordinal) |
| | 2966 | 36 | | || HasHigherPrecedence( |
| | 2966 | 37 | | chartName, |
| | 2966 | 38 | | priority, |
| | 2966 | 39 | | registrationOrder, |
| | 2966 | 40 | | EffectiveChartOwner, |
| | 2966 | 41 | | _effectivePriority, |
| | 2966 | 42 | | _effectiveRegistrationOrder)) |
| | | 43 | | { |
| | 2963 | 44 | | SetEffectiveContribution(chartName, contribution); |
| | | 45 | | } |
| | 2966 | 46 | | } |
| | | 47 | | |
| | | 48 | | public void RemoveOwner(string chartName) |
| | | 49 | | { |
| | 1922 | 50 | | if (!_chartContributions.ContainsKey(chartName)) |
| | 1 | 51 | | return; |
| | | 52 | | |
| | 1921 | 53 | | _chartContributions.Remove(chartName); |
| | 1921 | 54 | | if (string.Equals(chartName, EffectiveChartOwner, StringComparison.Ordinal)) |
| | 1920 | 55 | | ResolveEffectiveCell(); |
| | 1921 | 56 | | } |
| | | 57 | | |
| | 1985 | 58 | | public bool ContainsOwner(string chartName) => _chartContributions.ContainsKey(chartName); |
| | | 59 | | |
| | | 60 | | public void AddChartOwnersTo(SwiftHashSet<string> destination) |
| | | 61 | | { |
| | 4968 | 62 | | if (destination == null) |
| | 1 | 63 | | return; |
| | | 64 | | |
| | 19922 | 65 | | foreach (KeyValuePair<string, ChartContribution> pair in _chartContributions) |
| | 4994 | 66 | | destination.Add(pair.Key); |
| | 4967 | 67 | | } |
| | | 68 | | |
| | | 69 | | private void ResolveEffectiveCell() |
| | | 70 | | { |
| | 1920 | 71 | | EffectiveChartOwner = null; |
| | 1920 | 72 | | EffectiveCell = NavigationChartCell.Empty; |
| | 1920 | 73 | | _effectivePriority = 0; |
| | 1920 | 74 | | _effectiveRegistrationOrder = 0; |
| | | 75 | | |
| | 3860 | 76 | | foreach (KeyValuePair<string, ChartContribution> pair in _chartContributions) |
| | | 77 | | { |
| | 10 | 78 | | if (EffectiveChartOwner == null |
| | 10 | 79 | | || HasHigherPrecedence( |
| | 10 | 80 | | pair.Key, |
| | 10 | 81 | | pair.Value.Priority, |
| | 10 | 82 | | pair.Value.RegistrationOrder, |
| | 10 | 83 | | EffectiveChartOwner, |
| | 10 | 84 | | _effectivePriority, |
| | 10 | 85 | | _effectiveRegistrationOrder)) |
| | | 86 | | { |
| | 9 | 87 | | SetEffectiveContribution(pair.Key, pair.Value); |
| | | 88 | | } |
| | | 89 | | } |
| | 1920 | 90 | | } |
| | | 91 | | |
| | | 92 | | private void SetEffectiveContribution(string chartName, ChartContribution contribution) |
| | | 93 | | { |
| | 2972 | 94 | | EffectiveChartOwner = chartName; |
| | 2972 | 95 | | EffectiveCell = contribution.Cell; |
| | 2972 | 96 | | _effectivePriority = contribution.Priority; |
| | 2972 | 97 | | _effectiveRegistrationOrder = contribution.RegistrationOrder; |
| | 2972 | 98 | | } |
| | | 99 | | |
| | | 100 | | private static bool HasHigherPrecedence( |
| | | 101 | | string candidateChartName, |
| | | 102 | | int candidatePriority, |
| | | 103 | | int candidateRegistrationOrder, |
| | | 104 | | string currentChartName, |
| | | 105 | | int currentPriority, |
| | | 106 | | int currentRegistrationOrder) |
| | | 107 | | { |
| | 22 | 108 | | if (candidatePriority != currentPriority) |
| | 12 | 109 | | return candidatePriority > currentPriority; |
| | | 110 | | |
| | 10 | 111 | | if (candidateRegistrationOrder != currentRegistrationOrder) |
| | 9 | 112 | | return candidateRegistrationOrder > currentRegistrationOrder; |
| | | 113 | | |
| | 1 | 114 | | return string.CompareOrdinal(candidateChartName, currentChartName) > 0; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private readonly struct ChartContribution |
| | | 118 | | { |
| | | 119 | | public ChartContribution(NavigationChartCell cell, int priority, int registrationOrder) |
| | | 120 | | { |
| | 2966 | 121 | | Cell = cell; |
| | 2966 | 122 | | Priority = priority; |
| | 2966 | 123 | | RegistrationOrder = registrationOrder; |
| | 2966 | 124 | | } |
| | | 125 | | |
| | | 126 | | public NavigationChartCell Cell { get; } |
| | | 127 | | |
| | | 128 | | public int Priority { get; } |
| | | 129 | | |
| | | 130 | | public int RegistrationOrder { get; } |
| | | 131 | | } |
| | | 132 | | } |