コード例 #1
0
        public static void Randomize()
        {
            var fileNames = new List <string>(Directory.GetFiles(Environment.CurrentDirectory));

            var logicFile = ChooseLogic(fileNames.Where(x => Path.GetExtension(x) == ".lgc").ToList());

            if (logicFile == null)
            {
                // Log issue
                _logMessage += $"No logic file chosen{Environment.NewLine}";
            }

            var data = JsonConvert.DeserializeObject <SaveData>(File.ReadAllText(logicFile));

            KeyManager.Initialize(data);

            var traverser = new NodeTraverser();

            var settingsFile = ChooseSettings(fileNames.Where(x => Path.GetExtension(x) == ".txt" || Path.GetExtension(x) == ".log").ToList());

            if (settingsFile == null)
            {
                // Log issue
                _logMessage += $"No settings chosen{Environment.NewLine}";
            }

            if (!TryParseSettingsFile(startingInventory, settingsFile))
            {
                // Log issue
                _logMessage += "Failed to parse settings";
            }

            var randomizationType = ChooseRandomization();
            var gameCompletion    = ChooseGameCompletion();

            var random = new Random();
            var seed   = random.Next();
            // var seed = 1083686163;

            var count = 0;

            _Timer.Start();
            while (true)
            {
                if (randomizationType == 1)
                {
                    var placer  = new ItemPlacer();
                    var options = new FillOptions();
                    options.gameCompletion = gameCompletion;
                    options.majorSwap      = FillOptions.ItemSwap.GlobalPool;
                    options.minorSwap      = FillOptions.ItemSwap.LocalPool;
                    // options.noEarlyPbs = true;

                    var result = false;

                    var itemMap = new Dictionary <string, Guid>();

                    ItemPool pool = new ItemPool();
                    pool.CreatePool();
                    foreach (var item in itemMap.Values)
                    {
                        pool.Pull(item);
                    }

                    while (true)
                    {
                        random = new Random(seed);

                        //pool.RemoveRandomItemsExcept(90, random, new List<Guid>() { StaticKeys.Morph, StaticKeys.Missile, StaticKeys.Bombs, StaticKeys.IceBeam, StaticKeys.PlasmaBeam });

                        var testInventory = new Inventory(startingInventory);

                        testInventory.myKeys.AddRange(pool.AvailableItems().Where(key => key != StaticKeys.Nothing && (!options.noEarlyPbs || key != StaticKeys.PowerBombs)).Select(id => KeyManager.GetKey(id)));

                        if (traverser.VerifyBeatable(data, itemMap, testInventory))
                        {
                            break;
                        }

                        seed = random.Next();

                        pool.CreatePool();
                        foreach (var item in itemMap.Values)
                        {
                            pool.Pull(item);
                        }
                    }

                    random = new Random(seed);

                    var randomMap = placer.FillLocations(data, options, pool, startingInventory, random, itemMap);

                    if (gameCompletion == FillOptions.GameCompletion.Beatable)
                    {
                        result = traverser.VerifyBeatable(data, randomMap, new Inventory(startingInventory));
                    }
                    else if (gameCompletion == FillOptions.GameCompletion.AllItems)
                    {
                        result = traverser.VerifyFullCompletable(data, randomMap, new Inventory(startingInventory));
                    }

                    if (result)
                    {
                        Console.WriteLine($"{Environment.NewLine}Randomization complete after {count} attempts - Successful seed: {seed}");

                        Console.WriteLine(traverser.GetWaveLog());

                        _Timer.Stop();

                        resultItemMap = randomMap;
                        return;
                    }

                    seed = random.Next();

                    Console.WriteLine($"Attempts {count}");
                }
                else if (randomizationType == 2)
                {
                    var pool = new ItemPool();
                    pool.CreatePool();

                    random = new Random(seed);

                    var randomMap = StaticData.Locations.ToDictionary(location => location, location => pool.Pull(random));

                    var morphItem = randomMap.FirstOrDefault(x => x.Value.Equals(StaticKeys.Morph));

                    if (string.IsNullOrWhiteSpace(morphItem.Key))
                    {
                        seed = random.Next();
                        continue;
                    }

                    var morphLocation = randomMap.FirstOrDefault(x => x.Key.Equals("BrinstarMorph"));

                    randomMap[morphItem.Key]     = morphLocation.Value;
                    randomMap[morphLocation.Key] = StaticKeys.Morph;

                    var result = false;
                    if (gameCompletion == FillOptions.GameCompletion.Beatable)
                    {
                        result = traverser.VerifyBeatable(data, randomMap, new Inventory(startingInventory));
                    }
                    else if (gameCompletion == FillOptions.GameCompletion.AllItems)
                    {
                        result = traverser.VerifyFullCompletable(data, randomMap, new Inventory(startingInventory));
                    }

                    if (result)
                    {
                        Console.WriteLine($"Randomization complete after {count} attempts - Successful seed: {seed}");

                        _Timer.Stop();

                        resultItemMap = randomMap;
                        return;
                    }

                    if (count % 50 == 0)
                    {
                        Console.WriteLine($"Attempts {count}");
                    }
                }

                count++;

                seed = random.Next();
            }
        }