|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Mythril.Data; |
| 5 | + |
| 6 | +namespace Mythril.Headless.Simulation; |
| 7 | + |
| 8 | +public partial class ReachabilitySimulator( |
| 9 | + Items items, |
| 10 | + Quests quests, |
| 11 | + QuestDetails questDetails, |
| 12 | + QuestUnlocks questUnlocks, |
| 13 | + QuestToCadenceUnlocks questToCadenceUnlocks, |
| 14 | + Cadences cadences, |
| 15 | + Locations locations, |
| 16 | + ItemRefinements refinements, |
| 17 | + StatAugments statAugments, |
| 18 | + Stats stats) |
| 19 | +{ |
| 20 | + private readonly HashSet<string> _infiniteResources = []; |
| 21 | + private readonly Dictionary<string, int> _oneTimeResources = []; |
| 22 | + private readonly HashSet<string> _completedQuests = []; |
| 23 | + private readonly HashSet<string> _unlockedCadences = []; |
| 24 | + private readonly HashSet<string> _unlockedAbilities = []; // "CadenceName:AbilityName" |
| 25 | + private readonly Dictionary<string, int> _maxStats = stats.All.ToDictionary(s => s.Name, _ => 10); |
| 26 | + private int _magicCapacity = 30; |
| 27 | + |
| 28 | + private readonly Dictionary<string, double> _minTimeToReachResource = []; |
| 29 | + private readonly Dictionary<string, double> _minTimeToReachQuest = []; |
| 30 | + |
| 31 | + public void Run() |
| 32 | + { |
| 33 | + bool changed = true; |
| 34 | + int iteration = 0; |
| 35 | + |
| 36 | + foreach (var item in items.All) _minTimeToReachResource[item.Name] = double.PositiveInfinity; |
| 37 | + foreach (var quest in quests.All) _minTimeToReachQuest[quest.Name] = double.PositiveInfinity; |
| 38 | + |
| 39 | + while (changed && iteration < 1000) |
| 40 | + { |
| 41 | + changed = false; |
| 42 | + iteration++; |
| 43 | + |
| 44 | + foreach (var loc in locations.All) |
| 45 | + { |
| 46 | + if (string.IsNullOrEmpty(loc.RequiredQuest) || _completedQuests.Contains(loc.RequiredQuest)) |
| 47 | + { |
| 48 | + foreach (var quest in loc.Quests) |
| 49 | + { |
| 50 | + if (CanCompleteQuest(quest, out double time)) |
| 51 | + { |
| 52 | + if (!_completedQuests.Contains(quest.Name)) { _completedQuests.Add(quest.Name); changed = true; } |
| 53 | + if (time < _minTimeToReachQuest[quest.Name]) |
| 54 | + { |
| 55 | + _minTimeToReachQuest[quest.Name] = time; |
| 56 | + changed = true; |
| 57 | + var detail = questDetails[quest]; |
| 58 | + foreach (var reward in detail.Rewards) UpdateResourceReachability(reward.Item.Name, reward.Quantity, detail.Type == QuestType.Recurring, time); |
| 59 | + foreach (var cadence in questToCadenceUnlocks[quest]) { if (!_unlockedCadences.Contains(cadence.Name)) { _unlockedCadences.Add(cadence.Name); changed = true; } } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + foreach (var cadenceName in _unlockedCadences) |
| 67 | + { |
| 68 | + var cadence = cadences.All.First(c => c.Name == cadenceName); |
| 69 | + foreach (var unlock in cadence.Abilities) |
| 70 | + { |
| 71 | + string abilityKey = $"{cadence.Name}:{unlock.Ability.Name}"; |
| 72 | + if (!_unlockedAbilities.Contains(abilityKey) && CanAfford(unlock.Requirements, out double costTime)) |
| 73 | + { |
| 74 | + _unlockedAbilities.Add(abilityKey); changed = true; |
| 75 | + if (unlock.Ability.Name == "Magic Pocket I") _magicCapacity = Math.Max(_magicCapacity, 60); |
| 76 | + if (unlock.Ability.Name == "Magic Pocket II") _magicCapacity = Math.Max(_magicCapacity, 100); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var abilityKvp in refinements.ByKey) |
| 82 | + { |
| 83 | + if (_unlockedAbilities.Any(ua => ua.EndsWith($":{abilityKvp.Key.Name}"))) |
| 84 | + { |
| 85 | + foreach (var recipeKvp in abilityKvp.Value.Recipes) |
| 86 | + { |
| 87 | + if (IsResourceAvailable(recipeKvp.Key.Name, recipeKvp.Value.InputQuantity, out double inputTime)) |
| 88 | + { |
| 89 | + double outputTime = inputTime + (15.0 / recipeKvp.Value.OutputQuantity); |
| 90 | + if (UpdateResourceReachability(recipeKvp.Value.OutputItem.Name, recipeKvp.Value.OutputQuantity, true, outputTime)) changed = true; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (UpdateStats()) changed = true; |
| 97 | + } |
| 98 | + GenerateReport(); |
| 99 | + } |
| 100 | + |
| 101 | + private bool IsResourceAvailable(string name, int qty, out double time) |
| 102 | + { |
| 103 | + time = _minTimeToReachResource.GetValueOrDefault(name, double.PositiveInfinity); |
| 104 | + return _infiniteResources.Contains(name) || _oneTimeResources.GetValueOrDefault(name, 0) >= qty; |
| 105 | + } |
| 106 | + |
| 107 | + private bool CanAfford(ItemQuantity[] requirements, out double time) |
| 108 | + { |
| 109 | + time = 0; |
| 110 | + foreach (var req in requirements) |
| 111 | + { |
| 112 | + if (!IsResourceAvailable(req.Item.Name, req.Quantity, out double resTime)) { time = double.PositiveInfinity; return false; } |
| 113 | + time = Math.Max(time, resTime); |
| 114 | + } |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + private bool CanCompleteQuest(Quest quest, out double time) |
| 119 | + { |
| 120 | + var detail = questDetails[quest]; |
| 121 | + if (!CanAfford(detail.Requirements, out double costTime)) { time = double.PositiveInfinity; return false; } |
| 122 | + foreach (var reqQuest in questUnlocks[quest]) |
| 123 | + { |
| 124 | + if (!_completedQuests.Contains(reqQuest.Name)) { time = double.PositiveInfinity; return false; } |
| 125 | + costTime = Math.Max(costTime, _minTimeToReachQuest[reqQuest.Name]); |
| 126 | + } |
| 127 | + if (detail.RequiredStats != null) |
| 128 | + { |
| 129 | + foreach (var statReq in detail.RequiredStats) if (_maxStats[statReq.Key] < statReq.Value) { time = double.PositiveInfinity; return false; } |
| 130 | + } |
| 131 | + double duration = detail.DurationSeconds / (1.0 + (_maxStats[detail.PrimaryStat] / 100.0)); |
| 132 | + time = costTime + duration; |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + private bool UpdateResourceReachability(string name, int qty, bool infinite, double time) |
| 137 | + { |
| 138 | + bool changed = false; |
| 139 | + if (infinite && !_infiniteResources.Contains(name)) { _infiniteResources.Add(name); changed = true; } |
| 140 | + int currentQty = _oneTimeResources.GetValueOrDefault(name, 0); |
| 141 | + if (currentQty < 9999) { _oneTimeResources[name] = currentQty + qty; changed = true; } |
| 142 | + if (time < _minTimeToReachResource[name]) { _minTimeToReachResource[name] = time; changed = true; } |
| 143 | + return changed; |
| 144 | + } |
| 145 | + |
| 146 | + private bool UpdateStats() |
| 147 | + { |
| 148 | + bool changed = false; |
| 149 | + foreach (var stat in stats.All) |
| 150 | + { |
| 151 | + int bestVal = 10; |
| 152 | + foreach (var magicName in _infiniteResources) |
| 153 | + { |
| 154 | + var item = items.All.First(i => i.Name == magicName); |
| 155 | + if (item.ItemType == ItemType.Spell) |
| 156 | + { |
| 157 | + string abilityName = stat.Name switch { "Strength" => "J-Str", "Magic" => "J-Magic", "Vitality" => "J-Vit", "Speed" => "J-Speed", _ => "J-" + stat.Name }; |
| 158 | + if (_unlockedAbilities.Any(ua => ua.EndsWith($":{abilityName}"))) |
| 159 | + { |
| 160 | + var augment = statAugments[item].FirstOrDefault(a => a.Stat.Name == stat.Name); |
| 161 | + bestVal = Math.Max(bestVal, 10 + (int)(_magicCapacity * (augment.Stat.Name != null ? augment.ModifierAtFull / 100.0 : 0.1))); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + if (bestVal > _maxStats[stat.Name]) { _maxStats[stat.Name] = bestVal; changed = true; } |
| 166 | + } |
| 167 | + return changed; |
| 168 | + } |
| 169 | +} |
0 commit comments