-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cpp
450 lines (398 loc) · 10.2 KB
/
Model.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "Model.h"
Model* Model::ptr = 0;
void Model::go()
{
time++;
for (shared_ptr<Agent>temp : agents) {
temp->go(); //update for all agents
}
vector<shared_ptr<Farm>> temp2 = get_farmS();
for (shared_ptr<Farm>temp : temp2) {
temp->go();
}
for (int i = 0; i < attacksHeld.size(); i++) {
attack(attacksHeld[i]);
}
attacksHeld.clear();
}
Model& Model::get_Instance()
{
if (!ptr)
ptr = new Model();
return *ptr;
}
void Model::set_on_course(vector<string> tokens)
{
if (tokens.size() == 4)//thug
{
int deg = stoi(tokens[2]);
float spd = stof(tokens[3]);
shared_ptr<Thug> thug = get_thug_given_name(tokens[0]);
if (thug->getType() != "Thug")
throw string(tokens[0] + "is not a thug, cannot get speed");
thug->course(deg, spd);
}
else if (tokens.size() == 3)// knight/peasant
{
if (!check_if_peasant(tokens[0])) {
float deg = stof(tokens[2]);
auto st = get_knight_given_name(tokens[0]);
st->course(deg, 1);
}
else {
float deg = stof(tokens[2]);
auto st = get_peasant_given_name(tokens[0]);
st->course(deg, 1);
}
}
}
void Model::initModel(int argc, char* argv[])
{
if(argc != 3)
throw string("Invalid arguments in init");
FileOpener fo;
fo.open_file_castle(argv[1]);
fo.open_file_farm(argv[2]);
this->zones = move(fo.getZones());
time = 0;
}
void Model::set_to_default()
{
for (unsigned int i = 0; i < zones.size(); i++)
{
zones[i]->restore_default();
}
agents.resize(0);
}
void Model::print_status()
{
for (unsigned int i = 0; i < zones.size(); i++)
{
cout <<zones[i]->getStatus() << endl;
}
for (unsigned int i = 0; i < agents.size(); i++)
{
cout << agents[i]->getStatus() << endl;
}
}
bool Model::agent_exist(string str)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == str)
return true;
}
return false;
}
shared_ptr<Agent> Model::find_agent(string str)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == str)
return agents[i];
}
throw string("Agent wasn't found in database");
}
void Model::create_agent(AgentFactory::TYPE type, vector<string> tokens)
{
int last_added = agents.size();
shared_ptr<Agent> temp = af.createVehicle(type, tokens);
if (tokens.size() == 3)
{
string first = zones[0]->getName();
swapForFirst(tokens[2]);
temp->set_zone_at(this->zones);
swapForFirst(first);
}
agents.push_back(temp);
}
void Model::swapForFirst(string a) {
for (int i = 0; i < zones.size(); i++) {
if (zones[i]->getName() == a)
std::swap(zones[i], zones[0]);
}
}
void Model::position(vector<string> tokens)
{
shared_ptr<Agent> temp = find_agent(tokens[0]);
if (tokens.size() == 5)
{
if (temp->getType() != "Thug")
{
throw string(tokens[0] + "is not a thug can't have speed");
}
if (!Coordinate::check_correct_float_string(tokens[4]))
throw string("given speed incorrect");
temp->position(create_coordinate(tokens[2]," "+tokens[3]), stof(tokens[4]));
return;
}
else //its a knight
if (temp->getType() != "Knight" && temp->getType() != "Peasant")
{
throw string(tokens[0] + "is a knight/peasant can't have speed");
}
temp->position(create_coordinate(tokens[2], " " + tokens[3]), 1);
}
void Model::stop_agent(string name)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == name)
{
agents[i]->stop();
return;
}
}
throw string("name wasn't found in database");
}
shared_ptr<Castle> Model::get_castle(string name)
{
for (unsigned int i = 0; i < castles.size(); i++)
{
if (castles[i]->getName() == name)
return castles[i];
}
return nullptr;
}
vector<shared_ptr<Castle>> Model::get_castleS()
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getType() == "Castle")
castles.push_back(dynamic_pointer_cast<Castle>(zones[i]));
}
return move(this->castles);
}
shared_ptr<Farm> Model::get_farm(string name)
{
for (unsigned int i = 0; i < farms.size(); i++)
{
if (farms[i]->getName() == name)
return farms[i];
}
return nullptr;
}
vector<shared_ptr<Farm>> Model::get_farmS()
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getType() == "Farm")
farms.push_back(dynamic_pointer_cast<Farm>(zones[i]));
}
return move(this->farms);
}
bool Model::is_castle_exist(string name)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getName() == name)
return true;
}
return false;
}
bool Model::is_farm_exist(string name)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getName() == name)
return true;
}
return false;
}
Coordinate Model::create_coordinate(string str_x, string str_y)
{
float x, y;
string temp("(");
if (str_x.at(0) != temp[0])
throw string("wrong input in setting Coordinate");
temp = ",";
if (str_x.at(str_x.size()-1) != temp[0])
throw string("wrong input in setting Coordinate");
temp = " ";
if (str_y[0] != temp[0])
throw string("wrong input in setting Coordinate");
int last_index = str_y.size() - 1;
temp = ")";
if (str_y[last_index] != temp[0])
throw string("wrong input in setting Coordinate");
////////////////////////////////////////////////////delete first too charters and last then do split then stof em to x and y.
str_x.erase(str_x.begin());
str_x.erase(str_x.end()-1);
str_y.erase(str_y.begin());
str_y.erase(str_y.end() - 1);
if (!Coordinate::check_correct_float_string(str_x) || !Coordinate::check_correct_float_string(str_y))
throw string("wrong input in setting Coordinate");
x = stof(str_x);
y = stof(str_y);
return Coordinate(x, y);
}
bool Model::is_castle_exist(Coordinate pos)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getLocation() == pos)
return true;
}
return false;
}
bool Model::is_farm_exist(Coordinate pos)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getLocation() == pos)
return true;
}
return false;
}
void Model::set_destination_for_knight(vector<string> tokens)
{
if (tokens.size() != 3)
throw string("not given enough arguments");
string name_st = tokens[0];
string dest = tokens[2];
if (!is_castle_exist(dest))
throw string("Error castle does not exist");
get_knight_given_name(name_st)->set_destination(dest);
}
shared_ptr<Thug> Model::get_thug_given_name(string name)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == name)
{
if (agents[i]->getType() == "Thug")
return dynamic_pointer_cast<Thug>(agents[i]);
}
}
throw string(name + " thug doesnt exist");
}
shared_ptr<Knight> Model::get_knight_given_name(string name)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == name)
{
if (agents[i]->getType() == "Knight")
return dynamic_pointer_cast<Knight>(agents[i]);
}
}
throw string(name + " knight doesnt exist");
}
bool Model::check_if_peasant(string name)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == name)
{
if (agents[i]->getType() == "Peasant")
return true;
}
}
return false;
}
void Model::addAttacks(vector<string> tokens) {
if (tokens.size() != 3)
throw string("not given enough arguments");
get_thug_given_name(tokens[0]);
get_peasant_given_name(tokens[2]);
for (unsigned int i = 0; i < attacksHeld.size(); i++)
{
if (attacksHeld[i][0] == tokens[0])
{
throw string("ERROR: attack was set for given thug");
}
}
attacksHeld.push_back(tokens);
}
void Model::attack(vector<string> tokens)// Thug attacking peasant
{
shared_ptr<Thug> thug = get_thug_given_name(tokens[0]);
shared_ptr<Peasant> peasant = get_peasant_given_name(tokens[2]);
float distnace_peasant_n_thug = Coordinate::DistanceCoordinate(thug->getLocation(), peasant->getLocation());
if (distnace_peasant_n_thug < thug->get_atkRange())
{
if (safe_to_attack(peasant->getLocation()))
{ //peasant hijacked most lose all cargo and set to stopped or dead, thug must increase lp by 1 and status stopped
peasant->attack_successful();
thug->attack_succsess();
}
else { // attack failed peasant continues thug loses 1 LP and status becomes stopped
thug->attack_failed();
peasant->attack_failed();
}
}
else {
thug->attack_failed();
}
}
bool Model::safe_to_attack(Coordinate crdnt_peasant)
{
for (shared_ptr<Agent> temp : agents)
{
if (temp->getType() == "Knight")
{
float distance = Coordinate::DistanceCoordinate(crdnt_peasant, temp->getLocation());
if (distance < 0.25)
return false;
}
if (temp->getType() == "Peasant") {
if (temp->isUnpacking()) return false; //this means the peasant is at a castle/farm and cannot be attacked
}
}
return true;
}
shared_ptr<Peasant> Model::get_peasant_given_name(string name)
{
for (unsigned int i = 0; i < agents.size(); i++)
{
if (agents[i]->getName() == name)
{
if (agents[i]->getType() == "Peasant")
return dynamic_pointer_cast<Peasant>(agents[i]);
}
}
throw string(name + " peasant is not in database");
}
shared_ptr<Farm> Model::get_farm_given_name(string name)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getName() == name)
{
if (zones[i]->getType() == "Farm")
return dynamic_pointer_cast<Farm>(zones[i]);
}
}
throw string(name + " farm is not in database");
}
shared_ptr<Castle> Model::get_castle_given_name(string name)
{
for (unsigned int i = 0; i < zones.size(); i++)
{
if (zones[i]->getName() == name)
{
if (zones[i]->getType() == "Castle")
return dynamic_pointer_cast<Castle>(zones[i]);
}
}
throw string(name + " Castle is not in database");
}
void Model::updateView() {
for (int i = 0; i < zones.size(); i++) {
View::get_Instance().updates(zones[i]->getLocation().GetX(), zones[i]->getLocation().GetY(), zones[i]->getName().substr(0, 2));
}
for (unsigned int i = 0; i < agents.size(); i++)
View::get_Instance().updates(agents[i]->getLocation().GetX(), agents[i]->getLocation().GetY(), agents[i]->getName().substr(0,2));
}
void Model::start_peasant_work(vector<string> tokens)
{
shared_ptr<Peasant> peasant = get_peasant_given_name(tokens[0]);
shared_ptr<Castle> castle = get_castle_given_name(tokens[3]);
shared_ptr<Farm> farm = get_farm_given_name(tokens[2]);
peasant->setRoute(farm,castle);
}
void Model::peasant_status(string tokens)
{
shared_ptr<Peasant> peasant = get_peasant_given_name(tokens);
cout << *peasant;
}