public void Write(Random random, RandomizerOptions options)
        {
            // Collect game items
            // For armor: headEquip/bodyEquip/armEquip/legEquip booleans. weight float
            // For weapons: weight float.
            // Requirements: correctStrength/correctAgility/corretMagic/corretFaith float.
            // Types: displayTypeId (maps to MenuValueTableParam, in FMGs).
            // enablePyromancy/enablePyromancy/enableMiracle bool? Check attack types other than isBlowAttackType for whether a proper weapon
            // rightHandEquipable/leftHandEquipable bool (bothHandEquipable)?
            // arrowSlotEquipable/boltSlotEquipable bool for bows. bool DisableShoot for greatbow
            // enableGuard for shield
            // Arrows/Bolts: eh a bit tricky. weaponCategory 13/14 for arrow/bolt, and bool DisableShoot for greatbow
            // Spells: in Magic table. requirementIntellect, requirementFaith. ezStateBehaviorType - 0 magic, 2 pyro, 1 miracle
            Dictionary <EquipCategory, List <ItemKey> > items = new Dictionary <EquipCategory, List <ItemKey> >();
            Dictionary <ItemKey, float>   weights             = new Dictionary <ItemKey, float>();
            Dictionary <ItemKey, StatReq> requirements        = new Dictionary <ItemKey, StatReq>();
            HashSet <ItemKey>             crossbows           = new HashSet <ItemKey>();
            PARAM magics  = game.Param("Magic");
            bool  twoHand = options["startingtwohand"];

            foreach (ItemKey key in data.Data.Keys)
            {
                if (key.Type == ItemType.WEAPON)
                {
                    PARAM.Row     row            = game.Item(key);
                    EquipCategory mainCat        = EquipCategory.WEAPON;
                    int           weaponCategory = (byte)row["weaponCategory"].Value;
                    if (weaponCategories.ContainsKey(weaponCategory))
                    {
                        mainCat = weaponCategories[weaponCategory];
                    }
                    if ((byte)row["enableGuard"].Value == 1)
                    {
                        mainCat = EquipCategory.SHIELD;
                    }
                    if (mainCat == EquipCategory.BOW || mainCat == EquipCategory.ARROW || mainCat == EquipCategory.BOLT)
                    {
                        // Disable greatbow for starting - requirements too far off
                        if ((byte)row["DisableShoot"].Value == 1)
                        {
                            continue;
                        }
                    }
                    if (mainCat == EquipCategory.BOW)
                    {
                        if ((byte)row["boltSlotEquipable"].Value == 1)
                        {
                            crossbows.Add(key);
                        }
                    }
                    if (mainCat != EquipCategory.UNSET)
                    {
                        AddMulti(items, mainCat, key);
                    }
                    if ((byte)row["enableMagic"].Value == 1)
                    {
                        AddMulti(items, EquipCategory.CATALYST, key);
                    }
                    if ((byte)row["enableMiracle"].Value == 1)
                    {
                        AddMulti(items, EquipCategory.TALISMAN, key);
                    }
                    if ((byte)row["enablePyromancy"].Value == 1)
                    {
                        AddMulti(items, EquipCategory.FLAME, key);
                    }
                    int str = (byte)row["properStrength"].Value;
                    // Add two hand adjustment for weapons. Note this doesn't work exactly for casting items, but does not affect casting.
                    if (twoHand && (byte)row["Unk14"].Value == 0 && (mainCat == EquipCategory.WEAPON || mainCat == EquipCategory.UNSET))
                    {
                        str = (int)Math.Ceiling(str / 1.5);
                    }
                    requirements[key] = new StatReq
                    {
                        Str = (sbyte)str,
                        Dex = (sbyte)(byte)row["properAgility"].Value,
                        Mag = (sbyte)(byte)row["properMagic"].Value,
                        Fai = (sbyte)(byte)row["properFaith"].Value,
                    };
                    weights[key] = (float)row["weight"].Value;
                }
                else if (key.Type == ItemType.ARMOR)
                {
                    PARAM.Row row = game.Item(key);
                    for (int i = 0; i < 4; i++)
                    {
                        if ((byte)row[armorTypes[i]].Value == 1)
                        {
                            AddMulti(items, armorCats[i], key);
                            weights[key] = (float)row["weight"].Value;
                            break;
                        }
                    }
                }
                else if (key.Type == ItemType.GOOD)
                {
                    PARAM.Row magic = magics[key.ID];
                    // Exclude Spook and Tears of Denial as they can be a key item, useful though they are
                    if (magic != null && key.ID != 1354000 && key.ID != 3520000)
                    {
                        int magicCat = (byte)magic["ezStateBehaviorType"].Value;
                        AddMulti(items, magicTypes[magicCat], key);
                        requirements[key] = new StatReq
                        {
                            Str = 0,
                            Dex = 0,
                            Mag = (sbyte)(byte)magic["requirementIntellect"].Value,
                            Fai = (sbyte)(byte)magic["requirementFaith"].Value,
                            Att = (sbyte)(byte)magic["slotLength"].Value,
                        };
                    }
                }
            }
            // Generate some armor sets. One downside of this approach is that each piece is represented only once - but it is just one shuffle per category, and tends to result in a similar distribution to normal.
            List <List <ItemKey> > weightedArmors = new List <List <ItemKey> >();

            for (int i = 0; i < 4; i++)
            {
                weightedArmors.Add(WeightedShuffle(random, items[armorCats[i]], item => 1 / weights[item]));
            }
            List <ArmorSet> armors    = new List <ArmorSet>();
            int             maxArmors = weightedArmors.Select(rank => rank.Count).Min();

            for (int num = 0; num < maxArmors; num++)
            {
                ArmorSet armor = new ArmorSet();
                for (int i = 0; i < 4; i++)
                {
                    ItemKey item = weightedArmors[i][num];
                    armor.Ids[i]  = item.ID;
                    armor.Weight += weights[item];
                }
                armors.Add(armor);
            }
            armors.Sort((a, b) => a.Weight.CompareTo(b.Weight));

            PARAM chara = game.Param("CharaInitParam");
            // Just for testing ;)
            bool cheat = false;

            for (int i = 0; i < 10; i++)
            {
                PARAM.Row row = chara[startId + i];
                // First, always fudge magic to 10, so that Orbeck quest is possible.
                if ((sbyte)row["baseMag"].Value < 10)
                {
                    row["baseMag"].Value = (sbyte)10;
                }
                if (cheat)
                {
                    foreach (string stat in stats)
                    {
                        row[$"base{stat}"].Value = (sbyte)90;
                    }
                }
                // Then, see stat diffs for weapons/spells/catalysts, and fudge if necessary
                CharacterClass chClass = classes[i];
                int            attStat = (sbyte)row["baseWil"].Value;
                StatReq        chReqs  = new StatReq
                {
                    Str = (sbyte)row["baseStr"].Value,
                    Dex = (sbyte)row["baseDex"].Value,
                    Mag = (sbyte)row["baseMag"].Value,
                    Fai = (sbyte)row["baseFai"].Value,
                    Att = (sbyte)(attStat < 10 ? 0 : attStat < 14 ? 1 : 2),
                };
                StatReq dynamicReqs      = chReqs;
                double  fudgeFactor      = 1.5;
                float   weaponWeight     = 0f;
                int     attSlots         = 0;
                bool    crossbowSelected = false;
                Console.WriteLine($"Randomizing starting equipment for {chClass.Name}");
                foreach (KeyValuePair <string, EquipCategory> entry in baseStart.Concat(chClass.Start))
                {
                    EquipCategory cat = entry.Value;
                    // TODO: If a catalyst etc also doubles as a weapon, maybe skip its slot.
                    // This crossbow/bow logic relies on iteration order - try to make the order fixed...
                    if ((cat == EquipCategory.ARROW && crossbowSelected) || (cat == EquipCategory.BOLT && !crossbowSelected))
                    {
                        continue;
                    }
                    Dictionary <ItemKey, int> statDiffs  = items[entry.Value].ToDictionary(item => item, item => requirements[item].Eligible(dynamicReqs));
                    List <ItemKey>            candidates = items[entry.Value];
                    if (cat == EquipCategory.SHIELD || chClass.Name == "Deprived")
                    {
                        candidates = candidates.Where(item => statDiffs[item] >= 0).ToList();
                    }
                    if (cat == EquipCategory.SORCERY || cat == EquipCategory.MIRACLE || cat == EquipCategory.PYROMANCY)
                    {
                        // Fit within attunement slots. Alternatively could increase attunement, but that unbalances things potentially.
                        // Unfortunately means that pyromancer can't start with Chaos Bed Vestiges. Maybe for the best.
                        if (attSlots == chReqs.Att)
                        {
                            row[entry.Key].Value = -1;
                            continue;
                        }
                        candidates = candidates.Where(item => attSlots + requirements[item].Att <= chReqs.Att).ToList();
                    }
                    // Select weapon and adjust stats if necessary
                    List <ItemKey> weightKeys = WeightedShuffle(random, candidates, item =>
                    {
                        int diff = statDiffs[item];
                        if (diff >= 4)
                        {
                            return((float)Math.Pow(2, -4 * (Math.Min(diff, 20) / 20.0)));
                        }
                        if (diff >= 0)
                        {
                            return(2);
                        }
                        return((float)Math.Pow(fudgeFactor, diff));
                    });
                    ItemKey selected = weightKeys[0];
                    items[entry.Value].Remove(selected);
                    if (statDiffs[selected] < 0)
                    {
                        dynamicReqs.Adjust(requirements[selected]);
                        fudgeFactor *= -statDiffs[selected];
                    }
                    row[entry.Key].Value = selected.ID;
                    if (weights.ContainsKey(selected))
                    {
                        weaponWeight += weights[selected];
                    }
                    attSlots = requirements[selected].Att;
                    Console.WriteLine($"  {entry.Key} is now {game.Name(selected)}, meets requirements by {statDiffs[selected]}");
                }
                int statChange = dynamicReqs.Eligible(chReqs);
                if (statChange < 0)
                {
                    row["baseStr"].Value = dynamicReqs.Str;
                    row["baseDex"].Value = dynamicReqs.Dex;
                    row["baseMag"].Value = dynamicReqs.Mag;
                    row["baseFai"].Value = dynamicReqs.Fai;
                    row["soulLvl"].Value = (short)((short)row["soulLvl"].Value - statChange);
                }
                // Armor time
                float           totalWeight   = 40 + (sbyte)row["baseDurability"].Value;
                List <ArmorSet> availableSets = armors.TakeWhile(armor => armor.Weight + weaponWeight < totalWeight * 0.69f).ToList();
                if (availableSets.Count == 0)
                {
                    availableSets = new List <ArmorSet> {
                        armors[0]
                    }
                }
                ;
                ArmorSet selectedArmor = Choice(random, availableSets);
                armors.Remove(selectedArmor);
                Console.WriteLine($"  Armor: {string.Join(", ", selectedArmor.Ids.Select(id => game.Name(new ItemKey(ItemType.ARMOR, id))))}");
                Console.WriteLine($"  Weight: weapons {weaponWeight:0.##} + armor {selectedArmor.Weight:0.##} / {totalWeight:0.##} = {100*(weaponWeight+selectedArmor.Weight)/totalWeight:0.##}%");
                for (int j = 0; j < 4; j++)
                {
                    if ((int)row[armorSlots[j]].Value != -1)
                    {
                        row[armorSlots[j]].Value = selectedArmor.Ids[j];
                    }
                }

                if (cheat)
                {
                    PARAM         reinforce       = game.Param("ReinforceParamWeapon");
                    HashSet <int> reinforceLevels = new HashSet <int>(reinforce.Rows.Select(r => (int)r.ID));
                    foreach (string wep in weaponSlots)
                    {
                        int id = (int)row[wep].Value;
                        if (id != -1)
                        {
                            id = id - (id % 100);
                            PARAM.Row item        = game.Item(new ItemKey(ItemType.WEAPON, id));
                            int       reinforceId = (short)item["reinforceTypeId"].Value;
                            while (reinforceLevels.Contains(reinforceId + 5))
                            {
                                reinforceId += 5;
                                id          += 5;
                            }
                            row[wep].Value = id;
                        }
                    }
                }
            }
            // Now, have fun with NPCs
            Dictionary <string, ArmorSet>      npcArmors  = new Dictionary <string, ArmorSet>();
            Func <ItemType, PARAM.Cell, float> cellWeight = (type, cell) =>
            {
                int id = (int)cell.Value;
                if (id == -1)
                {
                    return(0);
                }
                ItemKey key = new ItemKey(type, id);
                if (!weights.ContainsKey(key))
                {
                    return(0);
                }
                return(weights[key]);
            };

            foreach (PARAM.Row row in chara.Rows.Where(r => r.ID > startId + 10))
            {
                string name = game.CharacterName((int)row.ID);
                if (name == "?CHARACTER?")
                {
                    continue;
                }
                ArmorSet selectedArmor;
                if (!npcArmors.ContainsKey(name))
                {
                    float weaponWeight = weaponSlots.Select(slot => cellWeight(ItemType.WEAPON, row[slot])).Sum();
                    float armorWeight  = armorSlots.Select(slot => cellWeight(ItemType.ARMOR, row[slot])).Sum();
                    float weightLimit  = weaponWeight + armorWeight;
                    float totalWeight  = 40 + (sbyte)row["baseDurability"].Value;
                    int   armorLimit   = armors.FindIndex(armor => armor.Weight + weaponWeight > weightLimit);
                    if (armorLimit == -1)
                    {
                        armorLimit = armors.Count - 1;
                    }
                    armorLimit    = Math.Min(20, armorLimit);
                    selectedArmor = npcArmors[name] = armors[random.Next(armorLimit)];
                    armors.Remove(selectedArmor);
                    Console.WriteLine($"Armor for {name}: {100 * weightLimit / totalWeight:0.##}% -> {100 * (selectedArmor.Weight + weaponWeight) / totalWeight:0.##}%: {string.Join(", ", selectedArmor.Ids.Select(id => game.Name(new ItemKey(ItemType.ARMOR, id))))}");
                }
                selectedArmor = npcArmors[name];
                for (int j = 0; j < 4; j++)
                {
                    if ((int)row[armorSlots[j]].Value != -1)
                    {
                        row[armorSlots[j]].Value = selectedArmor.Ids[j];
                    }
                }
            }
        }
    }
        public LocationData FindItems(GameData game)
        {
            ItemLocs allLocs = FindItemLocs(game);
            SortedDictionary <int, List <EntityId> > usedItemLots  = allLocs.usedItemLots;
            SortedDictionary <int, List <EntityId> > usedBaseShops = allLocs.usedBaseShops;

            PARAM shops     = game.Param("ShopLineupParam");
            PARAM itemLots  = game.Param("ItemLotParam");
            PARAM materials = game.Param("EquipMtrlSetParam");
            PARAM npcs      = game.Param("NpcParam");

            // First we may have to create lots - easier to do this at the start than keep data in side channels all the way through
            int baseEvent = 52500960;

            foreach (KeyValuePair <int, int> toCreate in allLocs.baseLotsToCreate)
            {
                PARAM.Row baseRow = itemLots[toCreate.Value];
                int       newLot  = toCreate.Key;
                PARAM.Row row     = itemLots[newLot];
                if (row == null)
                {
                    row = game.AddRow("ItemLotParam", newLot);
                    foreach (PARAM.Cell newCell in row.Cells)
                    {
                        newCell.Value = baseRow[newCell.Def.InternalName].Value;
                    }
                }
                // TODO: Re-enable this with flag id validation
                row["getItemFlagId"].Value = baseEvent;
                baseEvent++;
            }

            LocationData data = new LocationData();

            data.NewEntityLots = allLocs.newEntityLots;

            LocationKey prevLocation = null;

            foreach (KeyValuePair <int, List <EntityId> > entry in usedItemLots)
            {
                int itemLot = entry.Key;
                if (prevLocation != null)
                {
                    // TODO: If event flag is tracked in script, allow 1 maxslot
                    prevLocation.MaxSlots = Math.Max(Math.Min(itemLot - prevLocation.ID - 1, 8), 1);
                    if (prevLocation.MaxSlots < 1)
                    {
                        Console.WriteLine($"XX Overlapping slots at {itemLot}");
                    }
                    prevLocation = null;
                }
                List <EntityId> entities = entry.Value;
                string          locs     = string.Join(", ", entities.Select(e => game.EntityName(e, true) + (e.MapName == "" ? "" : " " + e.MapName)));
                if (itemLots[itemLot] == null)
                {
                    string text = game.LotName(itemLot);
                    // These are fine - no-ops to game
                    if (logUnused)
                    {
                        Console.WriteLine($"MISSING connected item lot!! {itemLot}: {text} @ {locs}");
                    }
                    continue;
                }
                LocationKey baseLocation = null;
                while (itemLots[itemLot] != null)
                {
                    bool   isBase = itemLot == entry.Key;
                    string text   = game.LotName(itemLot);

                    PARAM.Row row         = itemLots[itemLot];
                    int       clearCount  = (sbyte)row["ClearCount"].Value;
                    int       eventFlag   = (int)row["getItemFlagId"].Value;
                    int       totalPoints = 0;
                    for (int i = 1; i <= 8; i++)
                    {
                        totalPoints += (short)row[$"LotItemBasePoint0{i}"].Value;
                    }
                    List <string> itemLotOutput = new List <string>();
                    for (int i = 1; i <= 8; i++)
                    {
                        int id = (int)row[$"ItemLotId{i}"].Value;
                        if (id != 0)
                        {
                            uint type     = (uint)row[$"LotItemCategory0{i}"].Value;
                            int  points   = (short)row[$"LotItemBasePoint0{i}"].Value;
                            int  quantity = (ushort)row[$"NewLotItemNum{i}"].Value;
                            if (type == 0xFFFFFFFF)
                            {
                                continue;
                            }
                            ItemKey       item     = new ItemKey(LocationData.LotTypes[type], id);
                            string        itemText = $"{itemLot}[{locs}]";
                            List <string> lotInfo  = new List <string>();
                            if (quantity > 1)
                            {
                                lotInfo.Add($"{quantity}x");
                            }
                            if (points != totalPoints)
                            {
                                lotInfo.Add($"{100.0 * points / totalPoints:0.##}%");
                            }
                            PARAM.Row itemRow = game.Item(item);
                            if (itemRow != null && item.Type == ItemType.GOOD && (byte)itemRow["goodsType"].Value == 7)
                            {
                                // Items which are not shown in inventory menu for various reasons, but having them still does something.
                                lotInfo.Add($"(hidden)");
                            }
                            itemLotOutput.Add($"{game.Name(item)} " + string.Join(" ", lotInfo));
                            if (lotInfo.Count() > 0)
                            {
                                itemText += $" {string.Join(", ", lotInfo)}";
                            }
                            if (quantity <= 0)
                            {
                                Console.WriteLine($"XX There is 0! of {itemText}");
                            }
                            ItemScope scope;
                            if (eventFlag != -1)
                            {
                                if (equivalentEvents.ContainsKey(eventFlag))
                                {
                                    eventFlag = equivalentEvents[eventFlag];
                                }
                                scope = new ItemScope(ScopeType.EVENT, eventFlag);
                                // Note this doesn't necessarily have to be slot 1. But it should be only one slot...
                                if (points != totalPoints)
                                {
                                    Console.WriteLine($"Has event flag? But random? {itemText}");
                                }
                            }
                            else
                            {
                                // One time drops that directly award, that aren't covered by event flags. Mostly crystal lizards.
                                if (entities.Count() == 1 && entityItemLots.ContainsKey(entities[0].EventEntityID) && entityItemLots[entities[0].EventEntityID] == entry.Key)
                                {
                                    scope = new ItemScope(ScopeType.ENTITY, entities[0].EventEntityID);
                                }
                                // Non-respawning enemies with drops which can be missed. These are reused between different entities, so can drop multiple times.
                                else if (entities.All(e => nonRespawningEntities.Contains(e.EventEntityID)))
                                {
                                    scope = new ItemScope(ScopeType.ENTITY, entities.Select(e => e.EventEntityID).Min());
                                }
                                else
                                {
                                    int model = entities.Select(e => e.GetModelID()).Min();
                                    // Infinite guaranteed or scripted drops are not randomized unless specifically added to entityItemLots
                                    if (model == -1 || points == totalPoints)
                                    {
                                        if (logUnused)
                                        {
                                            Console.WriteLine($"XX Item {game.Name(item)} {itemLot} has no associated event, but is guaranteed or global: {itemText}");
                                        }
                                        continue;
                                    }
                                    scope = new ItemScope(ScopeType.MODEL, model);
                                }
                            }
                            LocationKey location = new LocationKey(LocationType.LOT, itemLot, itemText, entities, quantity, baseLocation);
                            data.AddLocation(item, scope, location);
                            if (baseLocation == null)
                            {
                                baseLocation = location;
                            }
                        }
                    }
                    // Write out the info. Some deduplication of sources to make prettier output.
                    string lotOutput = string.Join(", ", itemLotOutput);
                    bool   simple    = false;
                    string text2;
                    if (simple)
                    {
                        SortedSet <string> locations = new SortedSet <string>(entities.Select(e => game.LocationNames[e.MapName]));
                        SortedSet <string> models    = new SortedSet <string>(entities.Select(e => e.EntityName.StartsWith("o") ? "Treasure" : game.EntityName(e)));
                        text2 = $"{string.Join(", ", models)}: {string.Join(", ", locations)}";
                        if (models.All(x => x == "unknown"))
                        {
                            text2 = "Unused/Unknown";
                        }
                        if (models.All(x => x == "Unused NPC"))
                        {
                            text2 = "Unused NPC";
                        }
                        else if (models.Any(x => x == "Unused NPC"))
                        {
                            models.Remove("Unused NPC"); if (locations.Count > 1)
                            {
                                locations.Remove("Global");
                            }
                            text2 = $"{string.Join(", ", models)}: {string.Join(", ", locations)}";
                        }
                    }
                    else
                    {
                        // e.NPCParamID > -1 ? $" #{e.NPCParamID}" : ""
                        // SortedSet<string> models = new SortedSet<string>(entities.Select(e => e.EntityName.StartsWith("o") ? $"Treasure in {e.MapName}" : $"{game.ModelName(e, true)} in {e.MapName}"));
                        SortedSet <string> models = new SortedSet <string>(entities.Select(e => game.EntityName(e, true) + (e.MapName == "" ? "" : $" in {e.MapName}")));
                        text2 = $"{string.Join(", ", models)}";
                    }
                    // Console.WriteLine($"{itemLot} [{text2}] {lotOutput}");

                    if (itemLot == 2014)
                    {
                        break;                   // Unused, and item lot immediately after it is used. Won't be an issue once. ... ??
                    }
                    // Try to navigate resource drops (affected by Bell Demon).
                    if ((byte)row["LotItemNum1"].Value == 1)
                    {
                        int curOffset = itemLot % 100;
                        int curBase   = itemLot / 100 * 100;
                        int offset;
                        for (offset = curOffset + 10; offset <= 50; offset += 10)
                        {
                            PARAM.Row offRow = itemLots[curBase + offset];
                            if (offRow != null && (byte)offRow["LotItemNum1"].Value == 1)
                            {
                                break;
                            }
                        }
                        if (offset <= 50)
                        {
                            itemLot = curBase + offset;
                        }
                        else
                        {
                            itemLot++;
                        }
                    }
                    else
                    {
                        itemLot++;
                    }
                }
                prevLocation = baseLocation;
            }
            if (prevLocation != null)
            {
                prevLocation.MaxSlots = 5;
            }
            SortedDictionary <int, List <string> > qwcs      = new SortedDictionary <int, List <string> >();
            Dictionary <int, LocationKey>          baseShops = new Dictionary <int, LocationKey>();

            foreach (PARAM.Row row in shops.Rows)
            {
                int    shopID   = (int)row.ID;
                int    baseShop = GetShopType(shopID);
                string shopName = shopSplits[baseShop];
                if (shopName == null)
                {
                    if (!addUnused)
                    {
                        continue;
                    }
                    shopName = "Unknown shop";
                }
                if (shopID >= 9000000)
                {
                    continue;
                }
                int qwc = (int)row["qwcID"].Value;

                int     type         = (byte)row["equipType"].Value;
                int     id           = (int)row["EquipId"].Value;
                int     quantity     = (short)row["sellQuantity"].Value;
                int     eventFlag    = (int)row["EventFlag"].Value;
                int     material     = (int)row["mtrlId"].Value;
                int     value        = (int)row["value"].Value;
                float   priceRate    = (float)row["PriceRate"].Value;
                string  quantityText = quantity == -1 ? "" : $" ({quantity})"; // (unlimited)
                string  qwcText      = qwc == -1 ? "" : $" {game.QwcName(qwc)}";
                string  costText     = "";
                ItemKey item         = new ItemKey((ItemType)type, id);
                if (material != -1)
                {
                    PARAM.Row matRow        = materials[material];
                    int       materialQuant = (sbyte)matRow["ItemNum01"].Value;
                    int       materialItem  = (int)matRow["MaterialId01"].Value;
                    costText = $" for {materialQuant} {game.Name(new ItemKey(ItemType.GOOD, materialItem))}";
                }
                if (value != 0 || costText == "")
                {
                    int actualCost = value;
                    if (actualCost == -1)
                    {
                        actualCost = (int)game.Item(item)["shopId"].Value;
                    }
                    if (priceRate != 0)
                    {
                        actualCost = (int)(actualCost * priceRate);
                    }
                    costText = costText == "" ? $" for {actualCost} Sen" : $"{costText} and {actualCost} Sen";
                }
                string shopText = $"{shopName}{qwcText}{quantityText}{costText} - event {eventFlag}";
                string text     = $"{shopID}[{shopText}]";
                // Console.WriteLine($"{shopID} [{shopName}{qwcText}] {game.Name(item)}{quantityText}{costText}");
                LocationKey location = new LocationKey(
                    LocationType.SHOP, shopID, text,
                    usedBaseShops.ContainsKey(baseShop) ? usedBaseShops[baseShop] : new List <EntityId>(),
                    quantity,
                    null); // try not to use base shops - baseShops.ContainsKey(baseShop) ? baseShops[baseShop] : null);
                if (shopID == baseShop)
                {
                    baseShops[baseShop] = location;
                }
                ItemScope scope;
                AddMulti(qwcs, qwc, $"{game.Name(item)}: {text}");
                if (eventFlag != -1)
                {
                    if (equivalentEvents.ContainsKey(eventFlag))
                    {
                        eventFlag = equivalentEvents[eventFlag];
                    }
                    if (quantity <= 0)
                    {
                        Console.WriteLine("XX No quantity for event flag shop entry {text}");
                    }
                    ScopeType scopeType = ScopeType.EVENT;
                    if (restrictiveQwcs.Contains(qwc))
                    {
                        // If item becomes unavailable at some point, it returns in infinite form
                        scopeType = ScopeType.SHOP_INFINITE_EVENT;
                    }
                    scope = new ItemScope(scopeType, eventFlag);
                }
                else if (material != -1)
                {
                    int materialItem = (int)materials[material]["MaterialId01"].Value;
                    scope = new ItemScope(ScopeType.MATERIAL, materialItem);
                }
                else
                {
                    scope = new ItemScope(ScopeType.SHOP_INFINITE, -1);
                }
                data.AddLocation(item, scope, location);
            }
            // Merge infinite and finite shops. Mostly done via heuristic (when event and infinite both exist), with exception of one event
            ItemScope infiniteKey = new ItemScope(ScopeType.SHOP_INFINITE, -1);

            foreach (ItemLocations locations in data.Data.Values)
            {
                foreach (ItemLocation restrict in locations.Locations.Values.Where(loc => loc.Scope.Type == ScopeType.SHOP_INFINITE_EVENT).ToList())
                {
                    if (locations.Locations.ContainsKey(infiniteKey))
                    {
                        // Combine infinite shops into event
                        ItemLocation infinite = locations.Locations[infiniteKey];
                        restrict.Keys.AddRange(infinite.Keys);
                        locations.Locations.Remove(infiniteKey);
                    }
                    else
                    {
                        Console.WriteLine($"XX: No 1:1 match between event shops and infinite shops for {restrict}");
                        // No infinite shops, turn this into a regular event shop. (Doesn't happen in base DS3)
                        ItemLocation eventLoc = new ItemLocation(new ItemScope(ScopeType.EVENT, restrict.Scope.ID));
                        eventLoc.Keys.AddRange(restrict.Keys);
                        locations.Locations[eventLoc.Scope] = eventLoc;
                        locations.Locations.Remove(restrict.Scope);
                    }
                }
            }
            // Now can find all location scopes
            List <ScopeType> uniqueTypes = new List <ScopeType> {
                ScopeType.EVENT, ScopeType.ENTITY, ScopeType.MATERIAL
            };

            foreach (KeyValuePair <ItemKey, ItemLocations> entry in data.Data)
            {
                int unique = 0;
                foreach (KeyValuePair <ItemScope, ItemLocation> entry2 in entry.Value.Locations)
                {
                    ItemScope    scope = entry2.Key;
                    ItemLocation loc   = entry2.Value;
                    int          id    = uniqueTypes.Contains(scope.Type) ? scope.ID : -1;
                    unique = unique == -1 ? -1 : (id == -1 ? -1 : unique + 1);
                    SortedSet <int> shopIds  = new SortedSet <int>(loc.Keys.Where(k => k.Type == LocationType.SHOP).Select(k => GetShopType(k.ID)));
                    SortedSet <int> shopQwcs = new SortedSet <int>(loc.Keys.Where(k => k.Type == LocationType.SHOP).Select(k => (int)shops[k.ID]["qwcID"].Value)
                                                                   .Select(qwc => equivalentEvents.ContainsKey(qwc) ? equivalentEvents[qwc] : qwc)
                                                                   .Where(qwc => qwc != -1 && !restrictiveQwcs.Contains(qwc)));
                    SortedSet <int> allShop = new SortedSet <int>(shopIds.Union(shopQwcs));
                    if (shopIds.Count() + shopQwcs.Count() != allShop.Count())
                    {
                        Console.WriteLine($"XX Overlapping qwc/shop ids for location {loc}");
                    }
                    SortedSet <int> modelBase     = scope.Type == ScopeType.MODEL ? new SortedSet <int>(loc.Keys.Select(k => k.BaseID)) : new SortedSet <int>();
                    bool            onlyShops     = loc.Keys.All(k => k.Type == LocationType.SHOP) && allShop.Count() > 0;
                    LocationScope   locationScope = new LocationScope(scope.Type, id, allShop, modelBase, onlyShops);
                    data.AddLocationScope(entry.Key, scope, locationScope);
                    loc.LocScope = locationScope;
                }
                entry.Value.Unique = unique > 0;
            }

            if (logUnused)
            {
                Console.WriteLine("---------------------------------------------------------------------------");
                foreach (KeyValuePair <ItemKey, string> entry in game.Names())
                {
                    ItemKey item = entry.Key;
                    if (item.Type == 0)
                    {
                        item = new ItemKey(item.Type, item.ID - (item.ID % 10000));
                    }
                    if (!data.Data.ContainsKey(item))
                    {
                        // Mostly pulls up old DS1 items, crow items, and gestures.
                        Console.WriteLine($"Unused item {item.Type}-{entry.Key.ID}: {entry.Value}");
                    }
                }
            }

            return(data);
        }
Ejemplo n.º 3
0
        public void RandomizeTrees(Random random, Permutation permutation, SkillSplitter.Assignment split)
        {
            // >= 700: prosthetics
            // < 400: skills before mushin
            GameEditor editor = game.Editor;
            PARAM      param  = game.Params["SkillParam"];

            // Orderings for skills which completely supersede each other. (For prosthetics, just use their natural id ordering)
            Dictionary <int, int> skillOrderings = new Dictionary <int, int>
            {
                [110] = 111,  // Nightjar slash
                [210] = 211,  // Ichimonji
                [310] = 311,  // Praying Strikes
            };
            Dictionary <int, ItemKey> texts = new Dictionary <int, ItemKey>
            {
                [0] = game.ItemForName("Shinobi Esoteric Text"),
                [1] = game.ItemForName("Prosthetic Esoteric Text"),
                [2] = game.ItemForName("Ashina Esoteric Text"),
                [3] = game.ItemForName("Senpou Esoteric Text"),
                // [4] = game.ItemForName("Mushin Esoteric Text"),
            };
            SortedDictionary <ItemKey, string> names = game.Names();

            string descName(int desc)
            {
                return(names[new ItemKey(ItemType.WEAPON, desc)]);
            }

            Dictionary <int, SkillData>     allData    = new Dictionary <int, SkillData>();
            Dictionary <int, SkillSlot>     allSlots   = new Dictionary <int, SkillSlot>();
            Dictionary <ItemKey, SkillData> skillItems = new Dictionary <ItemKey, SkillData>();
            List <SkillData> skills          = new List <SkillData>();
            List <SkillSlot> skillSlots      = new List <SkillSlot>();
            List <SkillData> prosthetics     = new List <SkillData>();
            List <SkillSlot> prostheticSlots = new List <SkillSlot>();

            bool explain = false;

            foreach (PARAM.Row r in param.Rows)
            {
                SkillData data = new SkillData
                {
                    ID           = (int)r.ID,
                    Item         = (int)r["SkilLDescriptionId"].Value,
                    Equip        = (int)r["Unk1"].Value,
                    Flag         = (int)r["EventFlagId"].Value,
                    Placeholder  = (int)r["Unk5"].Value,
                    SpEffects    = new[] { (int)r["Unk2"].Value, (int)r["Unk3"].Value },
                    EmblemChange = (byte)r["Unk10"].Value != 0,
                };
                data.Key             = new ItemKey(ItemType.WEAPON, data.Item);
                skillItems[data.Key] = data;
                SkillSlot slot = new SkillSlot
                {
                    ID   = (int)r.ID,
                    Col  = (short)r["MenuDisplayPositionIndexXZ"].Value,
                    Row  = (short)r["MenuDisplayPositionIndexY"].Value,
                    Text = data.ID < 400 && texts.TryGetValue((byte)r["Unk7"].Value, out ItemKey text) ? text : null,
                };
                if (explain)
                {
                    Console.WriteLine($"{r.ID}: {data.Item}, {data.Equip}, {descName(data.Item)}");
                }
                if (data.ID < 400)
                {
                    skills.Add(data);
                    skillSlots.Add(slot);
                }
                else if (data.ID >= 700)
                {
                    prosthetics.Add(data);
                    prostheticSlots.Add(slot);
                }
                allData[data.ID]  = data;
                allSlots[slot.ID] = slot;
            }
            void applyData(PARAM.Row r, SkillData data)
            {
                r["SkilLDescriptionId"].Value = data.Item;
                r["EventFlagId"].Value        = data.Flag;
                r["Unk1"].Value  = data.Equip;
                r["Unk2"].Value  = data.SpEffects[0];
                r["Unk3"].Value  = data.SpEffects[0];
                r["Unk5"].Value  = data.Placeholder;
                r["Unk10"].Value = (byte)(data.EmblemChange ? 1 : 0);
            }

            Shuffle(random, skills);
            Shuffle(random, skillSlots);
            Shuffle(random, prosthetics);
            Shuffle(random, prostheticSlots);

            // Skills rando
            if (split == null)
            {
                Dictionary <ItemKey, string> textWeight    = new Dictionary <ItemKey, string>();
                Dictionary <ItemKey, string> textLocations = texts.Values.ToDictionary(t => t, t => {
                    SlotKey target    = permutation.GetFiniteTargetKey(t);
                    textWeight[t]     = permutation.GetLogOrder(target);
                    SlotAnnotation sn = ann.Slot(data.Location(target).LocScope);
                    if (explain)
                    {
                        Console.WriteLine($"{game.Name(t)} in {sn.Area} - {sn.Text}. Lateness {(permutation.ItemLateness.TryGetValue(t, out double val) ? val : -1)}");
                    }
                    return(sn.Area);
                });