-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhero.cpp
More file actions
343 lines (319 loc) · 10.5 KB
/
hero.cpp
File metadata and controls
343 lines (319 loc) · 10.5 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Implementation file for the Hero class.
#include "macros.h"
#include <iostream>
#include <string>
#include <vector>
Hero::Hero() : ptr_city(NULL), ptr_world(NULL), hero_id(-1), spec("Empty Hero Constructor"), moves(-1)
{}
Hero::Hero(City* _ptr_city, World* _ptr_world, int _hid, std::string _spec) :
ptr_city(_ptr_city), ptr_world(_ptr_world), hero_id(_hid), spec(_spec), moves(0)
{}
Hero::Hero(const Hero& _copy)
{
ptr_city = _copy.ptr_city;
ptr_world = _copy.ptr_world;
hero_id = _copy.hero_id;
spec = _copy.spec;
moves = _copy.moves;
}
Hero& Hero::operator =(const Hero& _assign)
{
ptr_city = _assign.ptr_city;
ptr_world = _assign.ptr_world;
hero_id = _assign.hero_id;
spec = _assign.spec;
moves = _assign.moves;
}
std::string Hero::get_spec() { return spec; }
int Hero::get_heroid() { return hero_id; }
bool Hero::charter_flight(City& _to, Hero* _user)
{
std::cout << "Calling Hero::charter_flight().\n";
// _user specifies the person who provides cards/moves for this action.
// This is ALWAYS the hero itself unless the hero has been dispatched
// by someone else.
if (_user == NULL) {
_user = this;
}
std::vector<PCard>::iterator it;
for (it = _user->hand.begin(); it != _user->hand.end(); it++) {
if (it->city_id == ptr_city->city_id) {
ptr_city->depart_hero(hero_id);
ptr_city = &_to;
ptr_city->arrive_hero(hero_id);
ptr_world->player_discard.push_back(*it);
_user->hand.erase(it);
// If Medic and the disease is cured, the arrive_hero function in City will auto-disinfect blocks of that disease.
_user->moves--;
return true;
}
}
return false;
}
bool Hero::direct_flight(City& _to, Hero* _user)
{
std::cout << "Calling Hero::direct_flight().\n";
if (_user == NULL) {
_user = this;
}
std::vector<PCard>::iterator it;
for (it = _user->hand.begin(); it != _user->hand.end(); it++) {
if (it->city_id == _to.city_id) {
ptr_city->depart_hero(hero_id); // First remove hero_id from the city's heroes.
ptr_city = &_to; // Point hero's ptr_city to the new city.
ptr_city->arrive_hero(hero_id); // Now add hero_id to the new city's heroes.
ptr_world->player_discard.push_back(*it);
_user->hand.erase(it);
// ADD EXCEPTION: if hero is a Medic, and if a disease is cured, remove all of that disease from destination.
_user->moves--;
return true;
}
}
return false;
}
bool Hero::shuttle_flight(City& _to, Hero* _user)
{
std::cout << "Calling Hero::shuttle_flight().\n";
if (_user == NULL) {
_user = this;
}
if (ptr_city->has_rc()) {
if (_to.has_rc()) {
ptr_city->depart_hero(hero_id);
ptr_city = &_to;
ptr_city->arrive_hero(hero_id);
_user->moves--;
return true;
}
}
return false;
}
bool Hero::move(City& _to, Hero* _user)
{
std::cout << "Calling Hero::move().\n";
if (_user == NULL) {
_user = this;
}
// Override function for Medic so that city from/to which he moves lose all CURED diseases.
int cid_to = _to.get_cityid();
std::vector<int>::iterator it;
for (it = ptr_city->neighbours.begin(); it != ptr_city->neighbours.end(); it++) {
if (*it == cid_to) {
ptr_city->depart_hero(hero_id);
ptr_city = &_to;
ptr_city->arrive_hero(hero_id);
_user->moves--;
std::cout << "Hero " << spec << " moved to " << _to.name << ".\n";
return true;
}
}
return false;
}
bool Hero::disinfect(int _did)
{
if (_did < 0 || _did >= 4) {
std::cout << "You cannot disinfect a disease ID that is not between 0 to 3 inclusive.\n";
return false;
}
std::cout << "Calling Hero::disinfect() on disease ID " << _did << ".\n";
int existing_cubes = ptr_city->disease_counters[_did];
if (ptr_city->disease_counters[_did] > 0) {
// If Medic, set to 0. If disease cured, set to 0. Else, decrement by 1.
if (spec == "Medic") {
std::cout << "Medic cleans up all of that disease in the city.\n";
ptr_city->disease_counters[_did] = 0;
ptr_world->disease_blocks[_did] += existing_cubes;
}
else if (ptr_world->disease_status[_did] >= CURED) {
std::cout << "Since disease is cured, all of that disease is removed.\n";
ptr_city->disease_counters[_did] = 0;
ptr_world->disease_blocks[_did] += existing_cubes;
}
else {
std::cout << "Hero cleans up 1 cube of that disease in the city.\n";
ptr_city->disease_counters[_did]--;
ptr_world->disease_blocks[_did]++;
}
moves--;
ptr_world->check_eradication(_did);
return true;
}
else {
std::cout << "This disease ID is not present in the city!\n";
return false;
}
}
bool Hero::build_centre(City& _city)
{
if (ptr_world->centres_remaining <= 0) {
std::cout << "There are no more Research Centres that may be built.\n";
return false;
}
// First check if the Hero is in the City.
if (ptr_city->get_cityid() == _city.get_cityid()) {
std::vector<PCard>::iterator it;
if (ptr_world->centres_remaining > 0 && _city.has_rc() == false)
{
if (spec == "Operations Expert") {
_city.build_rc();
ptr_world->centres_remaining--;
moves--;
std::cout << "Built RC in " << _city.name << ".\n";
return true;
}
for (it = hand.begin(); it != hand.end(); it++) {
if (it->city_id == _city.city_id)
{
hand.erase(it);
_city.build_rc();
ptr_world->centres_remaining--;
moves--;
std::cout << "Built RC in " << _city.name << ".\n";
return true;
}
}
}
}
return false;
}
bool Hero::give_card(std::string _card, Hero& _to)
{
std::vector<PCard>::iterator it;
// If the _card string matches the name of the city that hero is currently in and the receiver is also there:
if ((_card == ptr_city->name && _card == _to.ptr_city->name) || spec == "Researcher")
{
// Search for the card that you would like to give.
for (it = hand.begin(); it != hand.end(); ) {
if (it->name == _card) {
_to.hand.push_back(*it); // Dereference iterator to get the object to push onto the receiver's hand.
hand.erase(it);
moves--;
return true;
}
else
it++;
}
}
else
std::cout << "City name does not match the card's name.\n";
return false;
}
bool Hero::take_card(std::string _card, Hero& _from)
{
std::vector<PCard>::iterator it;
if ((_card == ptr_city->name && _card == _from.ptr_city->name) || _from.spec == "Researcher")
{
for (it = _from.hand.begin(); it != _from.hand.end(); )
{
if (it->name == _card)
{
hand.push_back(*it);
_from.hand.erase(it);
moves--;
return true;
}
else
it++;
}
}
return false;
}
bool Hero::cure(int _did, std::string _one, std::string _two, std::string _three, std::string _four, std::string _five)
{
std::cout << "Calling Hero::cure() on disease ID " << _did << ".\n";
if (spec == "Scientist") {
std::cout << "Do not use the regular cure function, instead use Scientist::scientist_cure().\n";
return false;
}
// Player should be able to specify exactly which cards he wants to use.
// First, verify that every card he wants to use exists in his hand.
// Then, erase each card from his hand and CURE THE DISEASE!
std::vector<PCard>::iterator it;
int count_matches = 0;
for (it = hand.begin(); it != hand.end(); it++)
if (it->disease_id == _did &&
(it->name == _one || it->name == _two || it->name == _three || it->name == _four || it->name == _five))
count_matches++;
if (count_matches == 5) {
for (int i = 0; i < count_matches; i++) {
std::vector<PCard>::iterator it;
for (it = hand.begin(); it!= hand.end(); it++) {
if (it->name==_one || it->name==_two || it->name==_three || it->name==_four || it->name==_five) {
hand.erase(it);
break;
}
}
}
ptr_world->disease_status[_did] = CURED; // CURED is a macro for 1.
moves--;
ptr_world->check_eradication(_did);
// Final check: wherever Medic is, needs to be wiped of the cured disease.
std::vector<Hero*>::iterator it;
int medic_id = -1;
City* medic_city = NULL;
for (it = ptr_world->heroes.begin(); it != ptr_world->heroes.end(); it++) {
if ((*it)->get_spec() == "Medic")
{
medic_id = (*it)->get_heroid();
medic_city = (*it)->ptr_city;
}
}
// If there is a medic in the game, remove all disease cubes of the cured colour in city medic is in.
if (medic_id > -1) {
int cubes_to_putback = medic_city->disease_counters[_did];
medic_city->disease_counters[_did] = 0;
ptr_world->disease_blocks[_did] += cubes_to_putback;
}
// Finally check if this was the final cure needed to win the game.
if (ptr_world->victory()) {
exit(1);
}
return true;
}
else
{
std::cout << "Not all 5 cards matching the disease colour for the cure are in the hero's hand.\n";
return false;
}
}
void Hero::start_turn()
{
std::cout << "Hero::start_turn called.\n";
moves = 4;
}
bool Hero::play_special_eventcard(std::string _arguments)
{
std::cout << "Hero::play_special_eventcard is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as ContPlanner::play_special_eventcard.\n";
exit(1);
}
bool Hero::get_special_eventcard(std::string _eventname)
{
std::cout << "Hero::get_special_eventcard is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as ContPlanner::get_special_eventcard.\n";
exit(1);
}
bool Hero::dispatch_control(int _hid, std::string _arguments)
{
std::cout << "Hero::dispatch_control is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as Dispatcher::dispatch_control.\n";
exit(1);
}
bool Hero::dispatch_move(int _hidfrom, int _hidto)
{
std::cout << "Hero::dispatch_move is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as Dispatcher::dispatch_move.\n";
exit(1);
}
bool Hero::opex_flight(City& _to, std::string _card)
{
std::cout << "Hero::opex_flight is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as OpExpert::opex_flight.\n";
exit(1);
}
bool Hero::scientist_cure(int _did, std::string _one, std::string _two, std::string _three, std::string _four)
{
std::cout << "Hero::scientist_cure is a purely virtual function. It should not have been possible to "
<< "call this function. This should be called only as Scientist::scientist_cure.\n";
exit(1);
}