Ejemplo n.º 1
0
        /* This creates a player and a random dungeon of the given difficulty level and node-capacity
         * The player is positioned at the dungeon's starting-node.
         * The constructor also randomly seeds monster-packs and items into the dungeon. The total
         * number of monsters are as specified. Monster-packs should be seeded as such that
         * the nodes' capacity are not violated. Furthermore the seeding of the monsters
         * and items should meet the balance requirements stated in the Project Document.
         */
        public Game(uint difficultyLevel, uint nodeCapcityMultiplier, uint numberOfMonsters)
        {
            player  = new Player();
            dungeon = new Dungeon(difficultyLevel, nodeCapcityMultiplier);
            packs   = new List <Pack>();
            items   = new List <Item>();

            // Game should have at least one monster.
            if (numberOfMonsters < 2)
            {
                throw new GameCreationException("Not enough monsters. numberOfMonsters should be >= 2");
            }

            // Check if node multiplier is bigger then 1, else dungeon cannot have any monsters.
            if (nodeCapcityMultiplier <= 0)
            {
                throw new GameCreationException("Invalid nodeCapcityMultiplier must be > 0");
            }

            // Check if dungeon is big enough to hold all the monsters.
            uint maxCapacity = 0;

            dungeon.allNodes.ForEach(node => maxCapacity += nodeCapcityMultiplier * (dungeon.level(node) + 1));
            if (numberOfMonsters > maxCapacity)
            {
                throw new GameCreationException("The number of monsters (" + numberOfMonsters + ") exceed dungeon capacity (" + maxCapacity + ")");
            }
            Logger.log("Creating a game of difficulty level " + difficultyLevel + ", node capacity multiplier "
                       + nodeCapcityMultiplier + ", and " + numberOfMonsters + " monsters.");

            player.dungeon = dungeon;
            // Player should start at the starting node.
            player.location = dungeon.startNode;

            // Get all dungeon bridges.
            List <Node> bridges = new List <Node>(dungeon.bridges);

            // Pretend the start and exit node are also a bridge.
            bridges.Insert(0, dungeon.startNode);
            bridges.Add(dungeon.exitNode);

            // The total hp off all monsters, will later be used to determine the amount of healing potions that can be spawned.
            int totalMonsterHp = 0;
            // Number of packs created.
            int packCount = 0;
            // Number of monsters created.
            int monsterCount = 0;

            // Helper function for pack creation.
            Func <uint, Node, Pack> createPack = new Func <uint, Node, Pack>((numberOfMonstersToCreate, node) =>
            {
                // Create a pack with the number of monsters.
                Pack pack       = new Pack("Pack" + packCount++, numberOfMonstersToCreate);
                pack.dungeon    = dungeon;
                pack.location   = node;
                pack.zoneNumber = pack.location.zoneNumber;
                node.packs.Add(pack);

                monsterCount += (int)numberOfMonstersToCreate;
                pack.members.ForEach(member =>
                {
                    member.pack     = pack;
                    totalMonsterHp += member.HP;
                });
                packs.Add(pack);
                return(pack);
            });

            // Distribute packs/monsters in dungeon.
            for (int level = 0; level <= difficultyLevel; level++)
            {
                // The amount of monster allowed in this zone.
                int monsterPerZone = (int)Math.Floor((2.0 * (level + 1) * numberOfMonsters) / ((difficultyLevel + 2) * (difficultyLevel + 1)));

                // The last level (zone) does not have to follow the formula above and can get the remaining monsters.
                if (level == difficultyLevel)
                {
                    monsterPerZone += ((int)numberOfMonsters - monsterCount) - monsterPerZone;
                }

                // All the nodes in the in this zone. (without the start or exit node).
                List <Node> nodes = Util.getAllNodesBetween(bridges[level], bridges[level + 1]).FindAll(node => node != dungeon.startNode && node != dungeon.exitNode);

                // For each node in level.
                for (int i = 0; i < nodes.Count && monsterPerZone > 0; i++)
                {
                    // The max number of monster that can be on this node.
                    uint nodeCapacityLimit = nodeCapcityMultiplier * (dungeon.level(nodes[i]) + 1);
                    // Subtract the monsters that are already on this node.
                    nodes[i].packs.ForEach(pack => nodeCapacityLimit -= (uint)pack.members.Count);

                    // Take a random number of monster to create for this pack on this node.
                    // But do not exceed the node capacity.
                    uint numberOfMonstersToCreate = (uint)RandomGenerator.rnd.Next((int)(Math.Min(nodeCapacityLimit, monsterPerZone) + 1));

                    // Check if we can still create a pack, we do not wish to create a pack of zero monsters.
                    if (numberOfMonstersToCreate > 0)
                    {
                        // Subtract the amount of monster spawned from the total monster pool.
                        // This pool should reach zero by the end of the zone.
                        monsterPerZone -= (int)numberOfMonstersToCreate;

                        // Put the pack on then node.
                        createPack(numberOfMonstersToCreate, nodes[i]);
                    }
                }

                // Not necessarily all monster should have been created, due to the randomness.
                // So we just distribute the rest of the monster accordingly.
                for (int i = 0; i < nodes.Count && monsterPerZone > 0; i++)
                {
                    // The number of monsters this node can still hold.
                    uint remainingNodeCapacity = nodeCapcityMultiplier * (dungeon.level(nodes[i]) + 1);
                    nodes[i].packs.ForEach(pack => remainingNodeCapacity -= (uint)pack.members.Count);
                    uint numberOfMonstersToCreate = (uint)Math.Min(monsterPerZone, remainingNodeCapacity);

                    // Check if we can still create a pack, we do not wish to create a pack of zero monsters.
                    if (numberOfMonstersToCreate > 0)
                    {
                        monsterPerZone -= (int)numberOfMonstersToCreate;
                        createPack(numberOfMonstersToCreate, nodes[i]);
                    }
                }

                // Monsters for this zone should be exactly zero.
                // If not the zone was to small and the dungeon is invalid.
                if (monsterPerZone != 0)
                {
                    throw new GameCreationException("To many monsters to fit in zone!");
                }
            }

            // Player HP
            player.HP = Math.Min(player.HPbase, (int)(0.8 * totalMonsterHp));

            // All nodes in dungeon without the start and exit node.
            List <Node> allNodes = dungeon.allNodes.FindAll(node => node != dungeon.startNode && node != dungeon.exitNode);

            // HP left to spend is the difference between 80% of the total monster HP and the player HP including all possible potions.
            int hpLeftToSpend = (int)Math.Floor(totalMonsterHp * 0.8) - player.HP;

            // Minus the HP of possible healing potion already in player bag.
            //hpLeftToSpend -= (int)player.bag.OfType<HealingPotion>().Sum(item => item.HPvalue);

            // Generate healing potions.
            for (int i = 0; ; i++)
            {
                HealingPotion potion = new HealingPotion("HP_" + i);
                // Check if the healing potion will not exceed the maximum amount of HP.
                if (hpLeftToSpend - potion.HPvalue >= 0)
                {
                    // Decrease the maximum amount of HP that can still be allocated to healing potions.
                    hpLeftToSpend -= (int)potion.HPvalue;
                    items.Add(potion);
                    // Add potion to random node in dungeon.
                    allNodes[RandomGenerator.rnd.Next(allNodes.Count)].items.Add(potion);
                }
                else
                {
                    break;
                }
            }

            // Generate crystals.
            for (int i = 0; i < allNodes.Count; i++)
            {
                // Each node has 10% chance to spawn a crystal.
                if (RandomGenerator.rnd.Next(10) == 0)
                {
                    Crystal crystal = new Crystal("Cry_" + i);
                    items.Add(crystal);
                    // Add crystal to node.
                    allNodes[i].items.Add(crystal);
                }
            }
        }
Ejemplo n.º 2
0
 public uint Capacity(Dungeon d)
 {
     return(d.M * (d.level(this) + 1));
 }
Ejemplo n.º 3
0
        //public int state;

        /* This creates a player and a random dungeon of the given difficulty level and node-capacity
         * The player is positioned at the dungeon's starting-node.
         * The constructor also randomly seeds monster-packs and items into the dungeon. The total
         * number of monsters are as specified. Monster-packs should be seeded as such that
         * the nodes' capacity are not violated. Furthermore the seeding of the monsters
         * and items should meet the balance requirements stated in the Project Document.
         */
        public Game(uint difficultyLevel, uint nodeCapacityMultiplier, uint numberOfMonsters)
        {
            try{
                Logger.log("Creating a game of difficulty level " + difficultyLevel + ", node capacity multiplier "
                           + nodeCapacityMultiplier + ", and " + numberOfMonsters + " monsters.");

                dungeon         = new Dungeon(difficultyLevel, nodeCapacityMultiplier, random);        //call dungeon constructor
                player          = new Player();
                player.location = dungeon.startNode;
                int  numberOfMonstersToPut = (int)numberOfMonsters; //a temporary variable to keep track of number of monsters to put in the dungeon
                int  min = 1, max = 1;                              //used in while loop to define number of monsters in a pack
                int  packId = 0;
                uint numberOfNodesInZone = 0;                       //a temporary variable to store number of nodes in a zone (in foreach loop)


                //Randomly seeds monsters into the dungeon
                //Currently puts all monsters in the dungeon at the creation
                Logger.log("Number of monsters to put in total : " + numberOfMonsters);
                while (numberOfMonstersToPut > 0)                   //while there are monsters to put in the dungeon
                {
                    foreach (Zone z in dungeon.zones)               //Seeds monsters zone by zone
                    {
                        int monstersInZone = -1;                    // -1 is just for control does not have any meaning
                        if (z.id == difficultyLevel)
                        {                                           //if it is the last zone
                            monstersInZone = numberOfMonstersToPut; //put remainder monsters
                        }
                        else //else every zone gets proportioned number of monsters
                        {
                            monstersInZone = getProportion(numberOfMonsters, z.id, difficultyLevel); //gets number of monsters to put in this zone
                        }
                        Logger.log("Will put " + monstersInZone + " monsters to the zone " + z.id);
                        numberOfNodesInZone = (uint)z.nodesInZone.Count;                                   //get number of nodes (N)

                        while (monstersInZone > 0)                                                         //while there are monsters to put in the zone
                        {
                            int  nodeNumber   = random.Next(0, (int)numberOfNodesInZone);                  //randomly pick which node to locate
                            Node nodeToLocate = z.nodesInZone.ElementAt <Node>(nodeNumber);                //get this node instance

                            int nodeCapacity = (int)z.capacity - (nodeToLocate.currentNumberOfMonsters()); //number of monsters that can locate in that node
                                                                                                           //check the capacity nodeCapacity, if less than 1 try another node, else create a monster pack of size min=1 max=nodeCapacity
                            Logger.log("Node to locate: " + nodeToLocate.id + " with capacity " + nodeCapacity);
                            if (nodeCapacity > 1)
                            {
                                //the upper limit for max is either the node's capacity or remaining number of monsters that should be located in this zone
                                if (nodeCapacity < monstersInZone)
                                {
                                    max = nodeCapacity;                                //if node capacity is less than remaining monsters to put, update max limit
                                }
                                else
                                {
                                    max = monstersInZone;
                                }

                                int  monstersToLocate = random.Next(min, max + 1);                                   //decide how many monsters will be in this monster-pack between this number limit
                                Pack newPack          = new Pack("" + packId, (uint)monstersToLocate, this.dungeon); //Create a pack
                                Logger.log("Putting " + monstersToLocate + " monsters in pack" + packId + " locating in " + nodeToLocate.id);
                                newPack.location = z.nodesInZone.ElementAt <Node>(nodeNumber);                       //Assign this pack's location
                                packId++;                                                                            //increase pack ID
                                z.nodesInZone.ElementAt <Node>(nodeNumber).packs.Add(newPack);                       //add pack to the node
                                monstersInZone        -= monstersToLocate;                                           //decrease number of monsters to be located in the zone
                                numberOfMonstersToPut -= monstersToLocate;                                           //decrease number of monsters to be located in the dungeon
                                Logger.log("monsters to locate in zone: " + monstersInZone + " in dungeon: " + numberOfMonstersToPut);
                            }
                        }
                    }
                }

                itemsToSeed = new List <Item>(); //stores the list of items to be seeded in the dungeon
                int itemTotal = 0;
                //There is a constraint for HP value of the player & HP values of items that player has in the bag
                //and HP values of items exist in the dungeon
                Logger.log("Upper limit " + (0.8 * getHPM()));
                while (getItemsHP() <= (0.8 * getHPM())) //while this constraint is satisfied, it creates items
                {
                    int decide = random.Next(0, 2);      //0 or 1, 0 means create healing potion, 1 means create magic crystal
                    if (decide == 0)
                    {
                        //create healing potion
                        HealingPotion healingPotion = new HealingPotion("" + itemTotal); //create it with id
                        itemTotal++;
                        itemsToSeed.Add(healingPotion);                                  //add it into the list
                        Logger.log("Created healing potion " + healingPotion.id);
                    }
                    else if (decide == 1)
                    {
                        //create crystal
                        Crystal crystal = new Crystal("" + itemTotal);
                        itemTotal++;
                        itemsToSeed.Add(crystal); //add it into the list
                        Logger.log("Created crystal " + crystal.id);
                    }
                    else
                    {
                        Logger.log("Something went wrong");
                    }
                    Logger.log("Current itemsToSeedHP " + getItemsHP());
                }
                //since it leaves the while loop just after this constraint is passed
                //remove last created item for property to hold
                itemsToSeed.RemoveAt(itemsToSeed.Count - 1);

                Logger.log("Current getITemsHP " + getItemsHP());

                //Randomly seed items in the itemsToSeed list
                //Currently puts all items in the dungeon at the creation
                int numberOfItemsToPut = itemsToSeed.Count;                               //number of items to seed is the length of the list
                Logger.log("Number of items to put in total : " + numberOfItemsToPut);
                int itemsInZone       = (int)(numberOfItemsToPut / (int)difficultyLevel); //Equally partition the number of items, except the last zone
                int normalItemsInZone = itemsInZone;                                      //used for indexing the items in itemsToSeed for the last level
                                                                                          //because itemsInZone for the last level changes, indexing changes
                int itemsIndex = 0;                                                       //index of the item in the itemsToSeed list
                while (numberOfItemsToPut > 0)                                            //while there are items to put in the dungeon
                {
                    foreach (Zone z in dungeon.zones)                                     //for each zone
                    {
                        if (z.id == difficultyLevel)
                        {                                     //if it is the last zone
                            itemsInZone = numberOfItemsToPut; //put remainder items
                        }

                        Logger.log("Will put " + itemsInZone + " items to the zone " + z.id);
                        numberOfNodesInZone = (uint)z.nodesInZone.Count; //get number of nodes (N)

                        for (int i = 0; i < itemsInZone; i++)
                        {                                                                                                //for each item to put in this zone
                            int  nodeNumber   = random.Next(0, (int)numberOfNodesInZone);                                //randomly pick which node to locate
                            Node nodeToLocate = z.nodesInZone.ElementAt <Node>(nodeNumber);                              //get this node
                            Item itemToAdd    = itemsToSeed.ElementAt <Item>((int)(itemsIndex * normalItemsInZone + i)); //starts from 0 for level 1, 0+number of items put in each zone for level 2
                                                                                                                         //increases by number of items put in zone for every level
                            Logger.log("Putting item positioned " + (itemsIndex * normalItemsInZone + i) + " to " + nodeToLocate.id);
                            nodeToLocate.items.Add(itemToAdd);                                                           //add the item to this node
                            numberOfItemsToPut--;                                                                        //Decrease number of items to put
                        }
                        itemsIndex++;                                                                                    //increase items index
                    }
                }
            }catch {
                throw new GameCreationException("Could not create the game");
            }
        }
Ejemplo n.º 4
0
 public Game(int difficultyLevel, int nodeCapacityMultiplier, int numberOfMonsters, bool testing)
 {
     Logger.log("Creating a game of difficulty level " + difficultyLevel + ", node capacity multiplier "
                + nodeCapacityMultiplier + ", and " + numberOfMonsters + " monsters.");
     dungeon = new Dungeon(difficultyLevel, nodeCapacityMultiplier, numberOfMonsters, testing);
 }
Ejemplo n.º 5
0
        public void DungeonIsValidTest_1()
        {
            Dungeon a = new Dungeon(10, 1);

            a.graph.Clear();

            Node u = new Node();
            Node v = new Node();
            Node w = new Node();
            Node x = new Node();
            Node y = new Node();
            Node z = new Node();

            u.id = ("" + 0);
            v.id = ("" + 1);
            w.id = ("" + 2);
            x.id = ("" + 3);
            y.id = ("" + 4);
            z.id = ("" + 5);

            // 0 --> 1 --> 2
            u.connect(v);
            v.connect(w);

            a.graph.Add(u);
            a.graph.Add(v);
            a.graph.Add(w);
            a.graph.Add(x);
            a.graph.Add(y);
            a.graph.Add(z);

            a.dungeon_size = a.graph.Count;
            bool result = a.dungeon_is_ok();

            Assert.AreEqual(true, result);

            a.graph.Add(y);
            u.connect(w);
            u.connect(x);
            u.connect(y);
            u.connect(z);

            a.dungeon_size = a.graph.Count;
            result         = a.dungeon_is_ok();
            Assert.AreEqual(false, result);

            //clean
            a.graph.Clear();
            u.neighbors.Clear();
            v.neighbors.Clear();
            w.neighbors.Clear();
            x.neighbors.Clear();
            y.neighbors.Clear();
            z.neighbors.Clear();

            u.connect(v);
            u.connect(w);
            u.connect(x);
            u.connect(y);

            v.connect(u);
            v.connect(w);
            v.connect(x);
            v.connect(y);

            w.connect(u);
            w.connect(v);
            w.connect(x);
            w.connect(y);

            x.connect(u);
            x.connect(w);
            x.connect(v);
            x.connect(y);

            y.connect(u);
            y.connect(w);
            y.connect(x);
            y.connect(v);

            a.graph.Add(u);
            a.graph.Add(v);
            a.graph.Add(w);
            a.graph.Add(x);
            a.graph.Add(y);

            a.dungeon_size = a.graph.Count;
            result         = a.dungeon_is_ok();
            Assert.AreEqual(false, result);
        }
Ejemplo n.º 6
0
        public void generateRandomDungeonTest_level10()
        {
            Dungeon a = new Dungeon(10, 1);

            Assert.AreEqual(Predicates.isValidDungeon(a.startNode, a.exitNode, 10), true);
        }
Ejemplo n.º 7
0
        public void Level_Test_Groot()
        {
            //test a bigger graph (figure 1 from project description)
            Dungeon a = new Dungeon(1, 1);

            a.graph.Clear();
            a.startNode = null;
            a.exitNode  = null;

            Node[] xs = new Node[10];
            //startnode
            xs[0]           = new Node("0");
            xs[0].nodeLevel = 1;
            a.startNode     = xs[0];

            xs[1]           = new Node("1");
            xs[1].nodeLevel = 1;

            xs[2]           = new Node("2");
            xs[2].nodeLevel = 1;

            xs[3]           = new Node("3");
            xs[3].nodeLevel = 1;

            xs[4]           = new Node("4");
            xs[4].nodeLevel = 1;

            Bridge b1 = new Bridge("10");

            b1.nodeLevel = 1;

            xs[0].connect(xs[1]);
            xs[0].connect(xs[3]);
            xs[0].connect(xs[2]);

            xs[2].connect(xs[1]);
            xs[2].connect(xs[3]);

            xs[3].connect(xs[4]);

            b1.connectToNodeOfSameZone(xs[1]);
            b1.connectToNodeOfSameZone(xs[2]);

            //level 2
            xs[5]           = new Node("5");
            xs[5].nodeLevel = 2;

            xs[6]           = new Node("6");
            xs[6].nodeLevel = 2;

            b1.connectToNodeOfNextZone(xs[5]);
            b1.connectToNodeOfNextZone(xs[6]);

            Bridge b2 = new Bridge("11");

            b2.nodeLevel = 2;

            xs[5].connect(xs[6]);

            b2.connectToNodeOfSameZone(xs[5]);
            b2.connectToNodeOfSameZone(xs[6]);

            //level 0
            xs[7]           = new Node("7");
            xs[7].nodeLevel = 3;
            b2.connectToNodeOfNextZone(xs[7]);

            xs[8]           = new Node("8");
            xs[8].nodeLevel = 3;
            b2.connectToNodeOfNextZone(xs[8]);

            //exit node
            xs[9]           = new Node("9");
            xs[9].nodeLevel = 3;

            xs[9].connect(xs[7]);
            xs[9].connect(xs[8]);

            a.startNode = xs[0];
            a.exitNode  = xs[9];

            a.bridges = new Bridge[] { b1, b2 };

            for (int i = 0; i < 10; i++)
            {
                a.graph.Add(xs[i]);
            }
            a.graph.Add(b1);
            a.graph.Add(b2);

            a.dungeon_size = 12;

            foreach (Node n in Predicates.reachableNodes(a.startNode))
            {
                Assert.AreEqual(n.nodeLevel, a.level(n));
            }
        }
Ejemplo n.º 8
0
 private int distance(Node u, Player p)
 {
     return(Dungeon.shortestpath(u, p.location).Count);
 }