-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
285 lines (241 loc) · 7.14 KB
/
Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <queue>
#include <cassert>
#include <algorithm>
#include "Player.h"
const int INF = int(1e9);
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
//check whether coordinates is within the map boundaries (EXCLUDING the borders)
bool outOfBounds(int X, int Y, char gameMap[MAPHEIGHT][MAPWIDTH]) {
return (X <= 0 || X >= MAPHEIGHT-1 || Y <= 0 || Y >=MAPWIDTH-1);
}
//Accessors
int Player::getHealthStat() const {
return healthStat;
}
int Player::getAttackStat() const {
return attackStat;
}
int Player::getEnergyStat() const {
return energyStat;
}
int Player::getCurrency() const {
return currency;
}
int Player::getRemainingMoves() const {
return remainingMoves;
}
int Player::getKillCount() const {
return killCount;
}
const pair<int,int>& Player::getEquippedWeaponItem() const {
return equippedWeaponItem;
}
const map<int,int>& Player::getEquippedHealingItems() const {
return equippedHealingItems;
}
const pair<int,int>& Player::getEquippedEnergyItem() const {
return equippedEnergyItem;
}
//Mutators
void Player::addHealthStat(const int _health) {
healthStat += _health;
}
void Player::addAttackStat(const int _attack) {
attackStat += _attack;
}
void Player::setEnergyStat(const int _energy) {
energyStat = _energy;
}
void Player::addCurrency(const int _currency) {
currency += _currency;
}
void Player::addRemainingMoves(const int _moves) {
remainingMoves += _moves;
}
void Player::addKillCount(const int _killCount) {
killCount += _killCount;
}
void Player::addEquippedItem(const int itemID, const string itemType, const int itemStat, const int addCount) {
switch (itemType[0]) {
//Weapon
case 'W': {
assert(equippedWeaponItem.second + addCount >= 0);
//if we're adding weapons, it will be replacing the current weapon
if(addCount > 0) {
equippedWeaponItem.first = itemID;
equippedWeaponItem.second = addCount;
attackStat = itemStat;
}
else {
equippedWeaponItem.second += addCount;
//if item is exhausted, reset attack stat to default
if(equippedWeaponItem.second == 0) {
equippedWeaponItem = pair<int,int>(-1,-1);
attackStat = DEFAULT_ATTACK_DAMAGE;
}
}
return;
}
//Healing
case 'H': {
const int currentCount = (equippedHealingItems.find(itemID)==equippedHealingItems.end() ? 0 : equippedHealingItems[itemID]);
assert(currentCount + addCount >= 0);
equippedHealingItems[itemID] += addCount;
if(equippedHealingItems[itemID] == 0){
equippedHealingItems.erase(itemID);
}
return;
}
//Energy
case 'E': {
//if we're adding energy items, it will be replacing the current weapon
if(addCount > 0) {
equippedEnergyItem.first = itemID;
equippedEnergyItem.second = addCount;
energyStat = itemStat;
}
else {
equippedEnergyItem.second += addCount;
//if item is exhausted, reset energy stat to default
if(equippedEnergyItem.second == 0) {
equippedEnergyItem = pair<int,int>(-1,-1);
energyStat = DEFAULT_ENERGY;
}
}
return;
}
default: {
cout << "Error at Player.cpp::addEquippedItem(): Invalid item type." << endl;
return;
}
}
}
Player::Player(
char ID,
int healthStat,
int attackStat,
int energyStat,
int currency):
healthStat(healthStat),
attackStat(attackStat),
energyStat(energyStat),
currency(currency),
remainingMoves(0),
killCount(0),
Entity(
ID,
uniform_int_distribution<int>(1, MAPHEIGHT - 2)(rng),
uniform_int_distribution<int>(1, MAPWIDTH - 2)(rng)
),
equippedWeaponItem(pair<int,int>(-1,-1)),
equippedEnergyItem(pair<int,int>(-1,-1))
{
equippedHealingItems.clear();
//cout <<": Spawning Player "<< getID()<<" at [ " << getPosX()<<" ,"<< getPosY()<<" ]"<<endl;
}
//Move function
//Breadth-first-search to find shortest distance with map obstacles in mind
void BFS(const int sourceX, const int sourceY, int dist[MAPHEIGHT][MAPWIDTH], char gameMap[MAPHEIGHT][MAPWIDTH]);
void Player::move(const int targetX, const int targetY, char gameMap[MAPHEIGHT][MAPWIDTH]) {
int currentX = getPosX();
int currentY = getPosY();
int dist[MAPHEIGHT][MAPWIDTH];
BFS(currentX, currentY, dist, gameMap);
if(gameMap[targetX][targetY] != BLANK) {
cout << "Target space is not empty." << endl << endl;
return;
}
if(remainingMoves < dist[targetX][targetY]){
cout << "Insufficient energy." << endl << endl;
return;
}
setPosX(targetX);
setPosY(targetY);
remainingMoves -= dist[targetX][targetY];
//update gameMap
gameMap[currentX][currentY] = BLANK;
gameMap[targetX][targetY] = getID();
cout << "Moved to (" << getPosY() << ", " << getPosX() << ")." << endl << endl;
}
//Default attack function
vector<int> Player::defaultAttack(vector<Player>& playerList) {
cout << "Select target (X to go back): ";
char targetID = UI::readChar();
int targetIndex = toupper(targetID) - 'A';
while (true) {
//go back
if(targetID == 'X') {
cout << "Crisis averted." << endl;
return vector<int>();
}
//check range
if(targetIndex < 0 || targetIndex >= playerList.size()) {
cout << "Invalid player. Try again." << endl;
targetID = UI::readChar();
targetIndex = toupper(targetID) - 'A';
continue;
}
//check that target is alive
if(playerList[targetIndex].getHealthStat() <= 0) {
cout << "Target player is dead. Try again." << endl;
targetID = UI::readChar();
targetIndex = toupper(targetID) - 'A';
continue;
}
//warn the player if they are attacking themselves
if(targetID == getID()) {
if(!UI::promptYN("Are you sure that you want to target yourself?")) {
cout << "Crisis averted." << endl;
return vector<int>();
}
else {
cout << "You punch yourself in the face." << endl;
}
}
//All checks passed
break;
}
Player& target = playerList[targetIndex];
int dist = abs(getPosX() - target.getPosX()) + abs(getPosY() - target.getPosY());
if(dist > DEFAULT_ATTACK_RANGE) {
cout << "The target is not in range." << endl;
return vector<int>();
}
int oldHP = target.getHealthStat();
target.addHealthStat(-min(oldHP, DEFAULT_ATTACK_DAMAGE));
remainingMoves -= DEFAULT_ATTACK_ENERGY_COST;
vector<int> deadPlayers;
cout << attackStat << " damage dealt to Player " << target.getID() << " (" << oldHP << "->" << target.getHealthStat() << ")" << endl;
if(target.getHealthStat() <= 0) {
cout << "Player " << target.getID() << " has been slain by Player " << getID() << "!" << endl;
deadPlayers.push_back(target.getID() - 'A');
}
return deadPlayers;
}
//BFS definition
void BFS(const int sourceX, const int sourceY, int dist[MAPHEIGHT][MAPWIDTH], char gameMap[MAPHEIGHT][MAPWIDTH]) {
for(int i = 0; i < MAPHEIGHT; i++){
for(int j = 0; j < MAPWIDTH; j++){
dist[i][j] = INF;
}
}
dist[sourceX][sourceY] = 0;
bool vst[MAPHEIGHT][MAPWIDTH]{};
queue<pair<int,int>> q;
q.push({sourceX, sourceY});
while(!q.empty()){
int x = q.front().first, y=q.front().second;
q.pop();
vst[x][y] = true;
for(int i = 0; i < 4; i++){
int nextX = x + dx[i], nextY = y + dy[i];
if(outOfBounds(nextX, nextY, gameMap)) continue;
if(vst[nextX][nextY]) continue;
if(gameMap[nextX][nextY] != BLANK) continue;
dist[nextX][nextY] = dist[x][y] + 1;
vst[nextX][nextY] = true;
q.push({nextX, nextY});
}
}
}