initial commit
[COMP345---Clone.git] / LevelMapBuilder.cpp
blobb882c4ed107eaeac185f0bc9b4b2757f5e031df4
1 //!@file LevelMapBuilder.cpp
2 //! @brief delcaration of the LevelMapBuilder
4 //! This class implements the build functions declared by the MapBuilder
5 //! The LevelMapBuilder builds the map based on a save file and a given level
6 //! and creates a list of monsters and treasure contained within the map based on the given level
7 #include "LevelMapBuilder.h"
8 #include <vector>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include "Helmet.h"
13 #include "Armor.h"
14 #include "Belt.h"
15 #include "Ring.h"
16 #include "Shield.h"
17 #include "Weapon.h"
18 #include "Boots.h"
19 #include "ItemContainer.h"
20 #include "ItemGenerator.h"
21 #include <random>
23 //! The default constructor sets the level to 1
24 LevelMapBuilder::LevelMapBuilder()
26 level = 1;
29 //! The constructor sets the level to the given level
30 //! @param m_level the level to be set
31 LevelMapBuilder::LevelMapBuilder(int m_level)
33 level = m_level;
36 LevelMapBuilder::~LevelMapBuilder()
40 //! Builds the monsters within the map and sets
41 //! their level to the level of the map
42 //! and stores them in the m_monsters variable
43 void LevelMapBuilder::buildMonsters()
45 Monster leveledMonster = Monster(level);
46 int rows = m_map->getRows();
47 int columns = m_map->getColumns();
48 for (int i = 0; i < rows; i++)
50 for (int j = 0; j < columns; j++)
52 if (m_map->get(i, j) == "m")
53 m_monsters->push_back(leveledMonster);
58 //! Builds the map based on the given file and stores them in the m_map variable
59 //! @param fileName: the file in which the map is saved
60 void LevelMapBuilder::buildMap(string fileName)
62 vector <vector <string> > mapElements;
63 ifstream ifFile(fileName);
65 while (ifFile)
67 string element;
68 if (!getline(ifFile, element)) break;
70 istringstream stringStream(element);
71 vector <string> record;
73 while (stringStream)
75 string element;
76 if (!getline(stringStream, element, ',')) break;
77 record.push_back(element);
80 mapElements.push_back(record);
83 int rows = mapElements.size();
84 int columns = mapElements[0].size();
85 m_map = new Map(rows, columns);
87 if (!ifFile.eof())
89 cerr << "error occured while building map\n";
92 for (int i = 0; i < rows; i++)
94 for (int j = 0; j < columns; j++)
96 m_map->fillCell(i, j, mapElements[i][j]);
101 //! Builds the treasure within the map,
102 //! sets the level of the items within the treasure
103 //! and stores them in the m_treasure variable
104 //! The amount of items and the items themselves are
105 //! generated randomly.
106 void LevelMapBuilder::buildTreasure()
108 int rows = m_map->getRows();
109 int columns = m_map->getColumns();
110 ItemGenerator itemGen = ItemGenerator();
111 int treasureCounter = 0;
112 mt19937 randomNum;
113 // Set random numbers between 0,2
114 uniform_int_distribution<uint32_t> uint_dist10(0, 2);
115 for (int i = 0; i < rows; i++)
117 // set new count for random amount of items
118 randomNum.seed(random_device()());
119 for (int j = 0; j < columns; j++)
121 if (m_map->get(i, j) == "t")
123 // Initialize another vector of items inside m_treasure because it will be filled with items
124 m_treasure->push_back(vector<Item>());
125 // Generates 1-3 random items inside the chest
126 for (int i = 0; i <= uint_dist10(randomNum); i++)
128 Item item = itemGen.generateItem(level);
129 //m_treasure->at(treasureCounter).addItem(item);
130 m_treasure->at(treasureCounter).push_back(item);
132 treasureCounter++;
138 //! prints a selected few unique values based on the given treasure list
139 //! @param treasureList the treasure list to be printed
140 void LevelMapBuilder::printTreasure(vector<vector<Item>> *treasureList)
142 int treasureCounter = 1;
143 for each (vector<Item> treasure in *treasureList)
145 cout << "\nTreasure chest " << treasureCounter++;
146 for each (Item item in treasure)
148 cout << "\nItem name: " << item.getName();
149 cout << " level: " << static_cast<Equipment&>(item).getLevel();
150 if (item.getName() == "helmet")
152 Helmet helmet = static_cast<Helmet&>(item);
153 int dex = helmet.getDexterity();
154 cout << " dex: " << dex << " armor class: " << helmet.getArmorClass() << " wisdom: " << helmet.getWisdom();
156 else if (item.getName() == "boots")
158 Boots boots = static_cast<Boots&>(item);
159 int ac = boots.getArmorClass();
160 cout << " ac: " << ac << " dexterity: " << boots.getDexterity();
162 else if (item.getName() == "armor")
164 Armor armor = static_cast<Armor&>(item);
165 int ac = armor.getArmorClass();
166 cout << " ac: " << ac;
168 else if (item.getName() == "ring")
170 Ring ring = static_cast<Ring&>(item);
171 int cha = ring.getCharisma();
172 cout << " charisma: " << cha << " armor class: " << ring.getArmorClass() << " constitution: " << ring.getConstitution() << " strength: " << ring.getStrength() << " wisdom: " << ring.getWisdom();
174 else if (item.getName() == "shield")
176 Shield shield = static_cast<Shield&>(item);
177 int ac = shield.getArmorClass();
178 cout << " ac: " << ac;
180 else if (item.getName() == "weapon")
182 Weapon weapon = static_cast<Weapon&>(item);
183 int dmg = weapon.getDamage();
184 cout << " damage: " << dmg << " attack: " << weapon.getAttack();
186 else if (item.getName() == "belt")
188 Belt belt = static_cast<Belt&>(item);
189 int str = belt.getStrength();
190 cout << " str: " << str << " constitution: " << belt.getConstitution();
196 //! prints a selected few monster stats based on the given monster list
197 //! @param monsterList the monster list to be printed
198 void LevelMapBuilder::printMonsters(vector<Monster>* monsters)
200 int monsterCounter = 1;
201 for each (Monster monster in *monsters)
203 cout << "\nMonster: " << monsterCounter;
204 cout << " level: " << monster.getLevel();
205 cout << " attack: " << monster.getAttack();
206 cout << " health: " << monster.getHealth();
207 monsterCounter++;