Ejemplo n.º 1
0
        public void TestBeatable(string mapId, bool expected)
        {
            //Arrange
            var resourceReader = new System.IO.StreamReader(new System.IO.MemoryStream(Properties.Resources.TestLogic));
            var data           = JsonConvert.DeserializeObject <Common.SaveData.SaveData>(resourceReader.ReadToEnd());

            var randomMap = TestData.Data[mapId];

            var traverser = new NodeTraverser();

            // Act
            var result = traverser.VerifyBeatable(data, randomMap);

            // Assert
            Assert.Equal(expected, result);
        }
Ejemplo n.º 2
0
        public static void DoTest()
        {
            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}";
                return;
            }

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

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

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

            KeyManager.Initialize(data);

            var inventoryKeys = ParseInventory(itemFile);

            if (inventoryKeys == null)
            {
                // Error logged in parser
                return;
            }

            var randomMap = ParseItemMap(data, itemFile);

            if (randomMap == null)
            {
                // Error logged in parser
                return;
            }

            Console.WriteLine();

            var traverser = new NodeTraverser();

            var inventory = new Inventory();

            inventory.myKeys = inventoryKeys;

            _Timer.Start();
            //for (int count = 0; count < 100; count++)
            {
                _logMessage += Environment.NewLine;
                var fullCompleteResult = traverser.VerifyFullCompletable(data, randomMap, new Inventory(inventory));
                _logMessage += $"100%: {fullCompleteResult}{Environment.NewLine}{traverser.GetWaveLog()}{Environment.NewLine}{Environment.NewLine}";

                var unreachablenodes = traverser.GetUnreachable();
                if (unreachablenodes.Any())
                {
                    _logMessage += $"Unreachable: {traverser.GetUnreachable().Select(node => Utility.GetNodeName(node) + (node is RandomKeyNode keyNode? $"({(keyNode.GetKey() is BaseKey key ? key.Name : "empty")})" : string.Empty)).Aggregate((i, j) => i + ", " + j)}{Environment.NewLine}{Environment.NewLine}";
                }

                var beatableResult = traverser.VerifyBeatable(data, randomMap, new Inventory(inventory));
                _logMessage += $"Beatable: {beatableResult}{Environment.NewLine}{traverser.GetWaveLog()}";
            }

            _Timer.Stop();
        }
Ejemplo n.º 3
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();
            }
        }