initial commit
[COMP345---Clone.git] / MapSaveBuilder.cpp
blobc748928f3a9947f2f38106344e83d94ab8947b26
1 //!@file MapSaveBuilder.cpp
2 //! @brief implementation of the MapSaveBuilder
4 //! This class implements the build functions declared by the MapBuilder
5 //! The MapSaveBuilder builds the map based on a save file and creates a list of
6 //! monsters and treasure contained within the map
7 #include "MapSaveBuilder.h"
8 #include <vector>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include "Helmet.h"
13 #include "ItemContainer.h"
14 #include "ItemGenerator.h"
15 #include <random>
16 #include "Helmet.h"
17 #include "Armor.h"
18 #include "Belt.h"
19 #include "Ring.h"
20 #include "Shield.h"
21 #include "Weapon.h"
22 #include "Boots.h"
24 MapSaveBuilder::MapSaveBuilder()
29 MapSaveBuilder::~MapSaveBuilder()
33 //! Builds the map based on the given file and stores them in the m_map variable
34 //! @param fileName: the file in which the map is saved
35 void MapSaveBuilder::buildMap(string fileName)
37 vector <vector <string> > mapElements;
38 ifstream ifFile(fileName);
40 while (ifFile)
42 string element;
43 if (!getline(ifFile, element)) break;
45 istringstream stringStream(element);
46 vector <string> record;
48 while (stringStream)
50 string element;
51 if (!getline(stringStream, element, ',')) break;
52 record.push_back(element);
55 mapElements.push_back(record);
58 int rows = mapElements.size();
59 int columns = mapElements[0].size();
61 m_map = new Map(rows, columns);
63 if (!ifFile.eof())
65 cerr << "error occured while building map\n";
68 for (int i = 0; i < rows; i++)
70 for (int j = 0; j < columns; j++)
72 char* object = strdup((mapElements[i][j]).c_str());
73 m_map->fillCell(i, j, *object);
78 //! Builds the monsters within the map
79 //! and stores them in the m_monsters variable
80 void MapSaveBuilder::buildMonsters()
82 Monster leveledMonster = Monster();
83 int rows = m_map->getRows();
84 int columns = m_map->getColumns();
85 for (int i = 0; i < rows; i++)
87 for (int j = 0; j < columns; j++)
89 if (m_map->getCharObject(i, j) == 'm')
90 m_monsters->push_back(leveledMonster);
95 //! Builds the treasure within the map
96 //! and stores them in the m_treasure variable
97 //! The amount of items and the items themselves are
98 //! generated randomly.
99 void MapSaveBuilder::buildTreasure()
101 int rows = m_map->getRows();
102 int columns = m_map->getColumns();
103 ItemGenerator itemGen = ItemGenerator();
104 int treasureCounter = 0;
105 mt19937 randomNum;
106 // Set random numbers between 0,2
107 uniform_int_distribution<uint32_t> uint_dist10(0, 2);
108 for (int i = 0; i < rows; i++)
110 // set new count for random amount of items
111 randomNum.seed(random_device()());
112 for (int j = 0; j < columns; j++)
114 if (m_map->getCharObject(i, j) == 't')
116 // Initialize another vector of items inside m_treasure because it will be filled with items
117 m_treasure->push_back(vector<Item>());
118 // Generates 1-3 random items inside the chest
119 for (int i = 0; i <= uint_dist10(randomNum); i++)
121 Item item = itemGen.generateItem(1);
122 m_treasure->at(treasureCounter).push_back(item);
124 treasureCounter++;
130 //! prints a selected few unique values based on the given treasure list
131 //! @param treasureList the treasure list to be printed
132 void MapSaveBuilder::printTreasure(vector<vector<Item>>* treasureList)
134 int treasureCounter = 1;
135 for each (vector<Item> treasure in *treasureList)
137 cout << "\nTreasure chest " << treasureCounter++;
138 for each (Item item in treasure)
140 cout << "\nItem name: " << item.getName();
141 cout << " level: " << static_cast<Equipment&>(item).getLevel();
142 if (item.getName() == "helmet")
144 Helmet helmet = static_cast<Helmet&>(item);
145 int dex = helmet.getDexterity();
146 cout << " dex: " << dex << " armor class: " << helmet.getArmorClass() << " wisdom: " << helmet.getWisdom();
148 else if (item.getName() == "boots")
150 Boots boots = static_cast<Boots&>(item);
151 int ac = boots.getArmorClass();
152 cout << " ac: " << ac << " dexterity: " << boots.getDexterity();
154 else if (item.getName() == "armor")
156 Armor armor = static_cast<Armor&>(item);
157 int ac = armor.getArmorClass();
158 cout << " ac: " << ac;
160 else if (item.getName() == "ring")
162 Ring ring = static_cast<Ring&>(item);
163 int cha = ring.getCharisma();
164 cout << " charisma: " << cha << " armor class: " << ring.getArmorClass() << " constitution: " << ring.getConstitution() << " strength: " << ring.getStrength() << " wisdom: " << ring.getWisdom();
166 else if (item.getName() == "shield")
168 Shield shield = static_cast<Shield&>(item);
169 int ac = shield.getArmorClass();
170 cout << " ac: " << ac;
172 else if (item.getName() == "weapon")
174 Weapon weapon = static_cast<Weapon&>(item);
175 int dmg = weapon.getDamage();
176 cout << " damage: " << dmg << " attack: " << weapon.getAttack();
178 else if (item.getName() == "belt")
180 Belt belt = static_cast<Belt&>(item);
181 int str = belt.getStrength();
182 cout << " str: " << str << " constitution: " << belt.getConstitution();
188 //! prints a selected few monster stats based on the given monster list
189 //! @param monsterList the monster list to be printed
190 void MapSaveBuilder::printMonsters(vector<Monster>* monsters)
192 int monsterCounter = 1;
193 for each (Monster monster in *monsters)
195 cout << "\nMonster: " << monsterCounter;
196 cout << " level: " << monster.getLevel();
197 cout << " attack: " << monster.getAttack();
198 cout << " health: " << monster.getHealth();
199 monsterCounter++;