protected virtual void SpawnItems(Map map, Rectangle room)
        {
            int numberOfItems = Program.Game.Random.Next(MaximumItemsPerRoom + 1);

            var itemPool = new WeightedPool <Entity.EntityFactoryDelegate>();

            itemPool.Add(ItemFactory.CreatePotion, 70);
            itemPool.Add(ItemFactory.CreateLightningScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> {
                { 2, 10 }, { 4, 25 }
            }));
            itemPool.Add(ItemFactory.CreateConfuseScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> {
                { 2, 25 }
            }));
            itemPool.Add(ItemFactory.CreateFireballScroll, GetWeightFromDungeonLevel(new SortedDictionary <int, int> {
                { 4, 20 }
            }));
            itemPool.Add(ItemFactory.CreateSword, 30);

            for (int i = 0; i < numberOfItems; i++)
            {
                int x = Program.Game.Random.Next(room.Left + 1, room.Right);
                int y = Program.Game.Random.Next(room.Top + 1, room.Bottom);

                if (map.IsWalkable(x, y))
                {
                    var itemFactory = itemPool.Pick();

                    var item = itemFactory(x, y, Program.Game.DungeonLevel);
                    Program.Game.Entities.Add(item);
                }
            }
        }
Exemplo n.º 2
0
        public static void AssignPools()
        {
            //Stage lengths against stage number. (i.e. chances of a generated stage being
            //  a certain length for a certain difficulty are specified below.)

            //Somewhat verbose, but allows for complete control of probabilities here for
            // more consistent stage generation.
            WeightedPool <int> poolOne = new WeightedPool <int>();

            poolOne.Add(2, 60);
            poolOne.Add(3, 40);

            WeightedPool <int> poolTwo = new WeightedPool <int>();

            poolTwo.Add(3, 20);
            poolTwo.Add(4, 60);
            poolTwo.Add(5, 20);

            WeightedPool <int> poolThree = new WeightedPool <int>();

            poolThree.Add(4, 40);
            poolThree.Add(5, 40);
            poolThree.Add(6, 20);

            WeightedPool <int> poolFour = new WeightedPool <int>();

            poolFour.Add(4, 30);
            poolFour.Add(5, 50);
            poolFour.Add(6, 20);

            WeightedPool <int> poolFive = new WeightedPool <int>();

            poolFive.Add(4, 10);
            poolFive.Add(5, 30);
            poolFive.Add(6, 40);
            poolFive.Add(7, 20);

            WeightedPool <int> poolSix = new WeightedPool <int>();

            poolSix.Add(5, 30);
            poolSix.Add(6, 40);
            poolSix.Add(7, 30);

            WeightedPool <int> poolSeven = new WeightedPool <int>();

            poolSeven.Add(6, 20);
            poolSeven.Add(7, 30);
            poolSeven.Add(8, 50);

            stageLengthPools = new Dictionary <int, WeightedPool <int> >()
            {
                { 1, poolOne },
                { 2, poolTwo },
                { 3, poolThree },
                { 4, poolFour },
                { 5, poolFive },
                { 6, poolSix },
                { 7, poolSeven },
            };
        }
Exemplo n.º 3
0
        public void Count_WhenTwoItemsAddedToEmptyPool_WillBe2()
        {
            WeightedPool <string> pool = new WeightedPool <string>();

            pool.Add("Thing 1", 1);
            pool.Add("Thing 2", 1);

            Assert.AreEqual(2, pool.Count);
        }
Exemplo n.º 4
0
        public void Add_WhenTotalWeightGoesOverMaximumIntValue_WillThrowOverflowException()
        {
            WeightedPool <int> pool = new WeightedPool <int>();

            pool.Add(1, int.MaxValue - 10);
            pool.Add(2, 10);

            Assert.ThrowsException <OverflowException>(() => pool.Add(3, 1));
        }
Exemplo n.º 5
0
        public void Clear_WhenPoolHas2Items_WillHaveCount0()
        {
            WeightedPool <string> pool = new WeightedPool <string>();

            pool.Add("Thing 1", 1);
            pool.Add("Thing 2", 1);

            pool.Clear();

            Assert.AreEqual(0, pool.Count);
        }
Exemplo n.º 6
0
        public void Choose_WhenCloneFuncWasNotDefined_WillThrowInvalidOperationException()
        {
            WeightedPool <int> pool = new WeightedPool <int>(Singleton.DefaultRandom);

            pool.Add(1, 1);

            Assert.ThrowsException <InvalidOperationException>(() => pool.Choose());
        }
Exemplo n.º 7
0
        public void Draw_WhenUsingRandomBiggerThanTotalWeight_WillThrowInvalidOperationException()
        {
            WeightedPool <int> pool = new WeightedPool <int>(new BadRandom(13));

            pool.Add(1, 12);

            Assert.ThrowsException <InvalidOperationException>(() => pool.Draw());
        }
Exemplo n.º 8
0
        public void Draw_Called7TimesOnPoolOf7Items_WillDrawAllOfThemAndCountWillBe0()
        {
            WeightedPool <string> pool = new WeightedPool <string>(Singleton.DefaultRandom);

            pool.Add("White", 5);
            pool.Add("Blue", 5);
            pool.Add("Black", 5);
            pool.Add("Red", 5);
            pool.Add("Green", 5);
            pool.Add("Artifact", 3);
            pool.Add("DualColor", 1);

            string[] drawn = new string[7];
            drawn[0] = pool.Draw();
            drawn[1] = pool.Draw();
            drawn[2] = pool.Draw();
            drawn[3] = pool.Draw();
            drawn[4] = pool.Draw();
            drawn[5] = pool.Draw();
            drawn[6] = pool.Draw();

            Assert.IsTrue(drawn.Contains("White"));
            Assert.IsTrue(drawn.Contains("Blue"));
            Assert.IsTrue(drawn.Contains("Black"));
            Assert.IsTrue(drawn.Contains("Red"));
            Assert.IsTrue(drawn.Contains("Green"));
            Assert.IsTrue(drawn.Contains("Artifact"));
            Assert.IsTrue(drawn.Contains("DualColor"));
            Assert.AreEqual(0, pool.Count);
        }
Exemplo n.º 9
0
        public void Draw_CalledTwiceWhenPoolHas1Item_WillThrowInvalidOperationException()
        {
            WeightedPool <int> pool = new WeightedPool <int>(Singleton.DefaultRandom);

            pool.Add(1, 12);
            int drawnItem = pool.Draw();

            Assert.AreEqual(1, drawnItem);
            Assert.ThrowsException <InvalidOperationException>(() => pool.Draw());
        }
        protected virtual void SpawnMonsters(Rectangle room)
        {
            int numberOfMonsters = Program.Game.Random.Next(MaximumMonstersPerRoom + 1);

            var monsterPool = new WeightedPool <Entity.EntityFactoryDelegate>();

            monsterPool.Add(MonsterFactory.CreateRat, 50);
            monsterPool.Add(MonsterFactory.CreateHound, GetWeightFromDungeonLevel(new SortedDictionary <int, int> {
                { 1, 10 }, { 3, 30 }, { 5, 50 }
            }));

            for (int i = 0; i < numberOfMonsters; i++)
            {
                int x = Program.Game.Random.Next(room.Left + 1, room.Right);
                int y = Program.Game.Random.Next(room.Top + 1, room.Bottom);

                var monsterFactory = monsterPool.Pick();

                var monster = monsterFactory(x, y, Program.Game.DungeonLevel);
                Program.Game.Entities.Add(monster);
            }
        }
Exemplo n.º 11
0
        public void Choose_WhenPoolHas1Item_WillGetCloneOfItemWithDifferentReference()
        {
            WeightedPool <PlayingCard> pool = new WeightedPool <PlayingCard>(Singleton.DefaultRandom, PlayingCard.Clone);
            PlayingCard kingOfHearts        = new PlayingCard
            {
                DisplayName = "King of Hearts", FaceValue = 13, Suit = PlayingCard.Suits.Hearts
            };

            pool.Add(kingOfHearts, 1);

            PlayingCard selectedCard = pool.Choose();

            Assert.AreNotEqual(kingOfHearts, selectedCard);
            Assert.AreEqual(kingOfHearts.FaceValue, selectedCard.FaceValue);
            Assert.AreEqual(kingOfHearts.DisplayName, selectedCard.DisplayName);
            Assert.AreEqual(kingOfHearts.Suit, selectedCard.Suit);
            Assert.AreEqual(1, pool.Count);
        }
Exemplo n.º 12
0
        public void Add_WhenWeightIsNegative_WillThrowArgumentException()
        {
            WeightedPool <int> pool = new WeightedPool <int>();

            Assert.ThrowsException <ArgumentException>(() => pool.Add(12, -5));
        }
Exemplo n.º 13
0
        public void Add_WhenItemArgumentIsNull_WillThrowArgumentNullException()
        {
            WeightedPool <string> pool = new WeightedPool <string>();

            Assert.ThrowsException <ArgumentNullException>(() => pool.Add(null, 1));
        }