protected void InventoryItemSetup(ShopFilling filling) { FillShopInventoryWithEmptyItem(); int items = Game.random.Next(GetLowerFillLimit(filling), GetUpperFillLimit(filling)); for (int n = 0; n < items; n++) { if (n < mandatoryShipItems.Count) { ShopInventoryEntry entry = mandatoryShipItems[n]; ShipPart part = RetrievePartFromEnum(entry.ShipPartType, Game); part.SetShipPartVariety(entry.ItemVariety); InsertPartAt(part, n); } else if (!inventoryIsFixed) { ShipPart part = ExtractShipPartFromItemFrequencies(); InsertPartAt(part, n); } } }
private ShipPart ExtractShipPartFromItemFrequencies() { ShipPart part = null; int summedItemFrequency = 0; // Stores an accumulated value for each ship part // The size of the ship parts range to previous value determines its probability for creation SortedDictionary <String, int> probabilityTable = new SortedDictionary <String, int>(); foreach (ShopInventoryEntry entry in shopInventoryEntries) { switch (entry.Availability) { case ShipPartAvailability.rare: { summedItemFrequency += 1; probabilityTable.Add(entry.GetId(), summedItemFrequency); break; } case ShipPartAvailability.uncommon: { summedItemFrequency += 2; probabilityTable.Add(entry.GetId(), summedItemFrequency); break; } case ShipPartAvailability.common: { summedItemFrequency += 3; probabilityTable.Add(entry.GetId(), summedItemFrequency); break; } case ShipPartAvailability.ubiquitous: { summedItemFrequency += 4; probabilityTable.Add(entry.GetId(), summedItemFrequency); break; } default: { throw new ArgumentException("Not implemented!"); } } } int randItemValue = Game.random.Next(summedItemFrequency); foreach (KeyValuePair <String, int> pair in probabilityTable.OrderBy(key => key.Value)) { if (pair.Value > randItemValue) { ShopInventoryEntry entry = new ShopInventoryEntry(pair.Key); part = RetrievePartFromEnum(entry.ShipPartType, Game); part.SetShipPartVariety(entry.ItemVariety); break; } } return(part); }