| | | 1 | | namespace RaidLoop.Core; |
| | | 2 | | |
| | | 3 | | public static class RaidEngine |
| | | 4 | | { |
| | | 5 | | public static OpeningPhaseResult ResolveOpeningPhase(OpeningContactState contactState, int playerInitiative, int ene |
| | | 6 | | { |
| | 3 | 7 | | return ResolveOpeningPhase(new OpeningPhaseContext(contactState, playerInitiative, enemyInitiative)); |
| | | 8 | | } |
| | | 9 | | |
| | | 10 | | public static OpeningPhaseResult ResolveOpeningPhase(OpeningPhaseContext context) |
| | | 11 | | { |
| | 4 | 12 | | return context.ContactState switch |
| | 4 | 13 | | { |
| | 1 | 14 | | OpeningContactState.PlayerAmbush => new OpeningPhaseResult( |
| | 1 | 15 | | context.ContactState, |
| | 1 | 16 | | OpeningSide.Player, |
| | 1 | 17 | | OpeningSide.None, |
| | 1 | 18 | | OpeningActionsRemaining: 1, |
| | 1 | 19 | | SurprisePersistenceEligible: false), |
| | 1 | 20 | | OpeningContactState.EnemyAmbush => new OpeningPhaseResult( |
| | 1 | 21 | | context.ContactState, |
| | 1 | 22 | | OpeningSide.Enemy, |
| | 1 | 23 | | OpeningSide.None, |
| | 1 | 24 | | OpeningActionsRemaining: 1, |
| | 1 | 25 | | SurprisePersistenceEligible: false), |
| | 2 | 26 | | _ => new OpeningPhaseResult( |
| | 2 | 27 | | context.ContactState, |
| | 2 | 28 | | OpeningSide.None, |
| | 2 | 29 | | context.PlayerInitiative >= context.EnemyInitiative ? OpeningSide.Player : OpeningSide.Enemy, |
| | 2 | 30 | | OpeningActionsRemaining: 1, |
| | 2 | 31 | | SurprisePersistenceEligible: false) |
| | 4 | 32 | | }; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public static RaidState StartRaid(GameState game, List<Item> loadout, int backpackCapacity, int startingHealth) |
| | | 36 | | { |
| | 18 | 37 | | foreach (var item in loadout) |
| | | 38 | | { |
| | 6 | 39 | | game.Stash.Remove(item); |
| | | 40 | | } |
| | | 41 | | |
| | 9 | 42 | | var equippedBackpack = loadout.FirstOrDefault(x => x.Type == ItemType.Backpack); |
| | 3 | 43 | | var inventory = RaidInventory.FromItems([.. loadout], [], CombatBalance.GetBackpackCapacity(equippedBackpack)); |
| | 3 | 44 | | return new RaidState(startingHealth, inventory); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public static void ApplyCombatDamage(RaidState state, int damage) |
| | | 48 | | { |
| | 1 | 49 | | state.Health = Math.Max(0, state.Health - Math.Max(0, damage)); |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static bool TryAddLoot(RaidState state, Item item) |
| | | 53 | | { |
| | 5 | 54 | | return TryAddCarriedItem(state, item); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public static void FinalizeRaid(GameState game, RaidState raid, bool extracted) |
| | | 58 | | { |
| | 2 | 59 | | if (!extracted || raid.IsDead) |
| | | 60 | | { |
| | 1 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | game.Stash.AddRange(raid.Inventory.GetExtractableItems()); |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | public static void StartDiscoveredLootEncounter(RaidState state, IEnumerable<Item> items) |
| | | 68 | | { |
| | 5 | 69 | | EncounterLoot.StartLootEncounter(state.Inventory.DiscoveredLoot, items); |
| | 5 | 70 | | } |
| | | 71 | | |
| | | 72 | | public static void AppendDiscoveredLoot(RaidState state, IEnumerable<Item> items) |
| | | 73 | | { |
| | 0 | 74 | | EncounterLoot.AppendDiscoveredLoot(state.Inventory.DiscoveredLoot, items); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | public static void ClearDiscoveredLoot(RaidState state) |
| | | 78 | | { |
| | 0 | 79 | | state.Inventory.DiscoveredLoot.Clear(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public static bool TryLootFromDiscovered(RaidState state, Item item) |
| | | 83 | | { |
| | 2 | 84 | | if (!state.Inventory.DiscoveredLoot.Remove(item)) |
| | | 85 | | { |
| | 0 | 86 | | return false; |
| | | 87 | | } |
| | | 88 | | |
| | 2 | 89 | | if (!TryAddCarriedItem(state, item)) |
| | | 90 | | { |
| | 1 | 91 | | state.Inventory.DiscoveredLoot.Add(item); |
| | 1 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public static bool TryDropCarriedToDiscovered(RaidState state, Item item) |
| | | 99 | | { |
| | 0 | 100 | | if (!state.Inventory.CarriedItems.Remove(item)) |
| | | 101 | | { |
| | 0 | 102 | | return false; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | state.Inventory.DiscoveredLoot.Add(item); |
| | 0 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public static bool TryDropEquippedToDiscovered(RaidState state, ItemType slotType) |
| | | 110 | | { |
| | 1 | 111 | | Item? item = slotType switch |
| | 1 | 112 | | { |
| | 0 | 113 | | ItemType.Weapon => state.Inventory.EquippedWeapon, |
| | 0 | 114 | | ItemType.Armor => state.Inventory.EquippedArmor, |
| | 1 | 115 | | ItemType.Backpack => state.Inventory.EquippedBackpack, |
| | 0 | 116 | | _ => null |
| | 1 | 117 | | }; |
| | | 118 | | |
| | 1 | 119 | | if (item is null) |
| | | 120 | | { |
| | 0 | 121 | | return false; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | switch (slotType) |
| | | 125 | | { |
| | | 126 | | case ItemType.Weapon: |
| | 0 | 127 | | state.Inventory.EquippedWeapon = null; |
| | 0 | 128 | | break; |
| | | 129 | | case ItemType.Armor: |
| | 0 | 130 | | state.Inventory.EquippedArmor = null; |
| | 0 | 131 | | break; |
| | | 132 | | case ItemType.Backpack: |
| | 1 | 133 | | state.Inventory.EquippedBackpack = null; |
| | 1 | 134 | | state.Inventory.BackpackCapacity = CombatBalance.GetBackpackCapacity((string?)null); |
| | | 135 | | // Requested behavior: dropping backpack drops everything carried. |
| | 1 | 136 | | state.Inventory.DiscoveredLoot.AddRange(state.Inventory.CarriedItems); |
| | 1 | 137 | | state.Inventory.CarriedItems.Clear(); |
| | 1 | 138 | | break; |
| | | 139 | | default: |
| | 0 | 140 | | return false; |
| | | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | state.BackpackCapacity = state.Inventory.BackpackCapacity; |
| | 1 | 144 | | state.Inventory.DiscoveredLoot.Add(item); |
| | 1 | 145 | | return true; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | public static bool TryEquipFromDiscovered(RaidState state, Item item) |
| | | 149 | | { |
| | 2 | 150 | | if (!state.Inventory.DiscoveredLoot.Remove(item)) |
| | | 151 | | { |
| | 0 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 2 | 155 | | if (!TryEquipItem(state, item)) |
| | | 156 | | { |
| | 1 | 157 | | state.Inventory.DiscoveredLoot.Add(item); |
| | 1 | 158 | | return false; |
| | | 159 | | } |
| | | 160 | | |
| | 1 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | public static bool TryEquipFromCarried(RaidState state, Item item) |
| | | 165 | | { |
| | 2 | 166 | | if (!state.Inventory.CarriedItems.Remove(item)) |
| | | 167 | | { |
| | 0 | 168 | | return false; |
| | | 169 | | } |
| | | 170 | | |
| | 2 | 171 | | if (!TryEquipItem(state, item)) |
| | | 172 | | { |
| | 1 | 173 | | state.Inventory.CarriedItems.Add(item); |
| | 1 | 174 | | return false; |
| | | 175 | | } |
| | | 176 | | |
| | 1 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private static bool TryEquipItem(RaidState state, Item item) |
| | | 181 | | { |
| | 4 | 182 | | if (item.Type is not (ItemType.Weapon or ItemType.Armor or ItemType.Backpack)) |
| | | 183 | | { |
| | 0 | 184 | | return false; |
| | | 185 | | } |
| | | 186 | | |
| | 4 | 187 | | if (!CanEquipItemByWeight(state, item)) |
| | | 188 | | { |
| | 2 | 189 | | return false; |
| | | 190 | | } |
| | | 191 | | |
| | 2 | 192 | | Item? previous = item.Type switch |
| | 2 | 193 | | { |
| | 1 | 194 | | ItemType.Weapon => state.Inventory.EquippedWeapon, |
| | 0 | 195 | | ItemType.Armor => state.Inventory.EquippedArmor, |
| | 1 | 196 | | ItemType.Backpack => state.Inventory.EquippedBackpack, |
| | 0 | 197 | | _ => null |
| | 2 | 198 | | }; |
| | | 199 | | |
| | 2 | 200 | | switch (item.Type) |
| | | 201 | | { |
| | | 202 | | case ItemType.Weapon: |
| | 1 | 203 | | state.Inventory.EquippedWeapon = item; |
| | 1 | 204 | | break; |
| | | 205 | | case ItemType.Armor: |
| | 0 | 206 | | state.Inventory.EquippedArmor = item; |
| | 0 | 207 | | break; |
| | | 208 | | case ItemType.Backpack: |
| | 1 | 209 | | state.Inventory.EquippedBackpack = item; |
| | 1 | 210 | | state.Inventory.BackpackCapacity = CombatBalance.GetBackpackCapacity(item); |
| | | 211 | | break; |
| | | 212 | | } |
| | | 213 | | |
| | 2 | 214 | | state.BackpackCapacity = state.Inventory.BackpackCapacity; |
| | 2 | 215 | | if (previous is not null) |
| | | 216 | | { |
| | 2 | 217 | | state.Inventory.DiscoveredLoot.Add(previous); |
| | | 218 | | } |
| | | 219 | | |
| | 2 | 220 | | SpillOverflowToDiscovered(state); |
| | 2 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private static bool TryAddCarriedItem(RaidState state, Item item) |
| | | 225 | | { |
| | 7 | 226 | | if (CombatBalance.IsMedkit(item)) |
| | | 227 | | { |
| | 3 | 228 | | if (!CanFitMedkitByWeight(state)) |
| | | 229 | | { |
| | 1 | 230 | | return false; |
| | | 231 | | } |
| | | 232 | | |
| | 2 | 233 | | state.Inventory.MedkitCount++; |
| | 2 | 234 | | return true; |
| | | 235 | | } |
| | | 236 | | |
| | 5 | 237 | | var currentSlots = state.Inventory.CarriedItems.Sum(x => x.Slots); |
| | 4 | 238 | | if (currentSlots + item.Slots > state.Inventory.BackpackCapacity) |
| | | 239 | | { |
| | 1 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | |
| | 3 | 243 | | if (!CanFitCarriedItemByWeight(state, item)) |
| | | 244 | | { |
| | 0 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 3 | 248 | | state.Inventory.CarriedItems.Add(item); |
| | 3 | 249 | | return true; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | private static void SpillOverflowToDiscovered(RaidState state) |
| | | 253 | | { |
| | 5 | 254 | | var currentSlots = state.Inventory.CarriedItems.Sum(x => x.Slots); |
| | 3 | 255 | | while (currentSlots > state.Inventory.BackpackCapacity && state.Inventory.CarriedItems.Count > 0) |
| | | 256 | | { |
| | 1 | 257 | | var spill = state.Inventory.CarriedItems[^1]; |
| | 1 | 258 | | state.Inventory.CarriedItems.RemoveAt(state.Inventory.CarriedItems.Count - 1); |
| | 1 | 259 | | state.Inventory.DiscoveredLoot.Add(spill); |
| | 1 | 260 | | currentSlots -= spill.Slots; |
| | | 261 | | } |
| | 2 | 262 | | } |
| | | 263 | | |
| | | 264 | | private static bool CanFitMedkitByWeight(RaidState state) |
| | | 265 | | { |
| | 3 | 266 | | return GetEncumbranceWithAdditionalItems(state, [ItemCatalog.Create("Medkit")]) <= state.Inventory.MaxEncumbranc |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | private static bool CanFitCarriedItemByWeight(RaidState state, Item item) |
| | | 270 | | { |
| | 3 | 271 | | return GetEncumbranceWithAdditionalItems(state, [item]) <= state.Inventory.MaxEncumbrance; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | private static bool CanEquipItemByWeight(RaidState state, Item item) |
| | | 275 | | { |
| | 4 | 276 | | var equippedItems = GetEquippedItems(state) |
| | 7 | 277 | | .Where(existing => existing.Type != item.Type) |
| | 4 | 278 | | .ToList(); |
| | 4 | 279 | | equippedItems.Add(item); |
| | | 280 | | |
| | 4 | 281 | | var carriedItems = state.Inventory.CarriedItems.ToList(); |
| | 4 | 282 | | if (item.Type == ItemType.Backpack) |
| | | 283 | | { |
| | 1 | 284 | | var backpackCapacity = CombatBalance.GetBackpackCapacity(item); |
| | 4 | 285 | | var currentSlots = carriedItems.Sum(x => x.Slots); |
| | 2 | 286 | | while (currentSlots > backpackCapacity && carriedItems.Count > 0) |
| | | 287 | | { |
| | 1 | 288 | | var spill = carriedItems[^1]; |
| | 1 | 289 | | carriedItems.RemoveAt(carriedItems.Count - 1); |
| | 1 | 290 | | currentSlots -= spill.Slots; |
| | | 291 | | } |
| | | 292 | | } |
| | | 293 | | |
| | 4 | 294 | | var prospectiveItems = equippedItems.Concat(carriedItems).ToList(); |
| | 8 | 295 | | for (var i = 0; i < state.Inventory.MedkitCount; i++) |
| | | 296 | | { |
| | 0 | 297 | | prospectiveItems.Add(ItemCatalog.Create("Medkit")); |
| | | 298 | | } |
| | | 299 | | |
| | 4 | 300 | | return CombatBalance.GetTotalEncumbrance(prospectiveItems) <= state.Inventory.MaxEncumbrance; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private static int GetEncumbranceWithAdditionalItems(RaidState state, IEnumerable<Item> additionalItems) |
| | | 304 | | { |
| | 6 | 305 | | var prospectiveItems = GetEquippedItems(state).ToList(); |
| | 6 | 306 | | prospectiveItems.AddRange(state.Inventory.CarriedItems); |
| | 12 | 307 | | for (var i = 0; i < state.Inventory.MedkitCount; i++) |
| | | 308 | | { |
| | 0 | 309 | | prospectiveItems.Add(ItemCatalog.Create("Medkit")); |
| | | 310 | | } |
| | | 311 | | |
| | 6 | 312 | | prospectiveItems.AddRange(additionalItems); |
| | 6 | 313 | | return CombatBalance.GetTotalEncumbrance(prospectiveItems); |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | private static IEnumerable<Item> GetEquippedItems(RaidState state) |
| | | 317 | | { |
| | 10 | 318 | | if (state.Inventory.EquippedWeapon is not null) |
| | | 319 | | { |
| | 7 | 320 | | yield return state.Inventory.EquippedWeapon; |
| | | 321 | | } |
| | | 322 | | |
| | 10 | 323 | | if (state.Inventory.EquippedArmor is not null) |
| | | 324 | | { |
| | 1 | 325 | | yield return state.Inventory.EquippedArmor; |
| | | 326 | | } |
| | | 327 | | |
| | 10 | 328 | | if (state.Inventory.EquippedBackpack is not null) |
| | | 329 | | { |
| | 4 | 330 | | yield return state.Inventory.EquippedBackpack; |
| | | 331 | | } |
| | 10 | 332 | | } |
| | | 333 | | } |