-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeasant.cpp
372 lines (321 loc) · 10.7 KB
/
Peasant.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
#include "Peasant.h"
Peasant::Peasant(vector<string> parmaters) :Agent(parmaters),carrying_stock(0)
{
status = Unpacking;
speed = 0.5;
if (parmaters.size() == 4)// create at coordinate
{
status = Unpacking;
speed = 0.5;
degree = 0;
status = Stopped;
location = create_coordinate(parmaters[2], parmaters[3]);
starting_location = location;
moving_by_coordinate = false;
moving_by_degree = false;
lifePoints = 10;
//castle_at = nullptr;
}
else
throw string("Invalid arguments to peasant con'");
}
Peasant::Peasant(string s) :Agent(s), carrying_stock(0) {
if (s.size() > 12)
throw string("Peasant name is too long");
status = Unpacking;
speed = 0.5;
}
void Peasant::attack_failed() {
lifePoints -= 1;
if (lifePoints <= 0) {
status = Dead; //dead reset all locations etc
}
}
void Peasant::attack_successful()
{
status = Stopped;
carrying_stock = 0;
lifePoints -= 1;
if (lifePoints <= 0) {
status = Dead; //dead
}
//nullptr to the location peasant was going to
}
void Peasant::course(int dg, float spd)
{
moving_by_degree = true;
moving_by_coordinate = false;
degree = dg;
speed = 0.5;
status = Moving_to;
}
void Peasant::position(Coordinate pst, float speed) {
//this->speed = 1;
move_to = pst;
moving_by_coordinate = true;
moving_by_degree = false;
status = Moving_to;
this->speed = 0.5;
}
Peasant::~Peasant(){}
string Peasant::getStatus()
{
if (status == Stopped)
return string("Peasant " + name + " stopped at " + location.to_string());
if(status == Dead)
return string("Peasant " + name + " is dead");
if (status == Moving_to) {
if (moving_by_degree)
return string("Peasant " + name + " at " + location.to_string() + ", Heading on course " + std::to_string(degree).substr(0, (to_string(degree).at(1) == '.' || to_string(degree).at(2) == '.') ? 2 : 3) + " deg, speed " + std::to_string((int)(speed*10.0)) + " Km/h");
else if (moving_by_coordinate)
return string("Peasant " + name + " at " + location.to_string() + ", Heading to position " + move_to.to_string() + ", speed " + std::to_string((int)(speed * 10.0)) + " Km/h");
else
if ((zoneList[0] == nullptr))
return string("Peasant " + name + " at " + location.to_string() + " heading to " + (zoneList[1]->getCastle()->getName()) + ", speed " + std::to_string((int)(speed * 10.0)) + " Km/h");
else
return string("Peasant " + name + " at " + location.to_string() + " heading to " + (zoneList[0]->getFarm()->getName()) + ", speed " + std::to_string((int)(speed * 10.0)) + " Km/h");
}
if (status == Unpacking)
return string("Peasant " + name + " at " + zoneList[1]->getCastle()->getName());
}
ostream& operator<<(ostream& os, const Peasant& pt) {
os << string("Peasant " + pt.name + " is currently at " + pt.location.to_string() + " with status " + ((pt.status == Agent::Stopped)? "Stopped ": (pt.status == Agent::Dead) ? "Dead ": "Moving to ") + " carrying " + std::to_string(pt.carrying_stock) + " stock " + "with " + std::to_string(pt.lifePoints) + " life points") << endl;
return os;
}
void Peasant::setRoute(shared_ptr<Farm> farm,shared_ptr<Castle> castle)
{
if (currently_working) { //either deny and keep working on current move, or accepet and remove current work, or push back the work given,we choose push back.
zoneList.push_back(make_shared<CastleFarmRoute>(CastleFarmRoute(nullptr, farm)));
zoneList.push_back(make_shared<CastleFarmRoute>(CastleFarmRoute(castle, nullptr)));
return;
}
zoneList.push_back(make_shared<CastleFarmRoute>(CastleFarmRoute(nullptr, farm)));
zoneList.push_back(make_shared<CastleFarmRoute>(CastleFarmRoute(castle, nullptr)));
zoneList[0]->getFarm() = farm;
zoneList[1]->getCastle() = castle;
currently_working = true;
status = Moving_to;
}
void Peasant::restore_default()
{
location = starting_location;
carrying_stock = 0;
lifePoints = 10;
}
float Peasant::getSpeed() {
return speed;
}
void Peasant::go() {
if (status == Dead || status == Stopped ) return;
if (moving_by_coordinate) {
if (location == move_to) {
//either stop or start going back to castle
location = move_to;
status == Stopped;
}
if (location == move_to) {
location = move_to;
status = Stopped;
}
float defreeFix = 0;
float degreeio = Coordinate::getDegree(move_to, location);
float temp = degreeio;
degree = degreeio;
float pi = 3.14159;
temp = (temp)*pi / 180;
float cos1 = (speed)*cos(temp);
float sin1 = (speed)*sin(temp);
if (move_to.GetX() == location.GetX()) {
cos1 = 0;
sin1 *= -1;
}
if (move_to.GetY() == location.GetY()) {
sin1 = 0;
//cos1 *= -1;
}
//20,10
if (move_to.GetX() >= location.GetX()
&& move_to.GetY() >= location.GetY())
{ //up + right -----------------------------
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else if (move_to.GetX() >= location.GetX()
&& move_to.GetY() <= location.GetY())
{ //down + right
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else if (move_to.GetX() <= location.GetX()
&& move_to.GetY() <= location.GetY())
{ //down + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1);
}
else { //up + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1); //this is the range of 270 - 360 which is 270 to 360
}
return;
} else if (moving_by_degree) {
float temp = degree;
float pi = 3.14159;
temp = (temp + 90) * pi / 180;
float cos1 = (speed)*cos(temp);
float sin1 = (speed)*sin(temp);
if (degree == 90 || degree == 180 || degree == 270 || degree == 0) {
if (degree == 90) { //right
location.SetX(location.GetX() - cos1);
}
if (degree == 180) { //down
location.SetY(location.GetY() + sin1);
}
if (degree == 270) {//left
location.SetX(location.GetX() - cos1);
}
if (degree == 0) {//up
location.SetY(location.GetY() + sin1);
}
}
else {
if (degree > 0 && degree < 90) { //up + right
location.SetX(location.GetX() + cos1 * -1);
location.SetY(location.GetY() + sin1);
}
else if (degree > 90 && degree < 180) { //down + right
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() + sin1);
}
else if (degree > 180 && degree < 270) { //down + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - fabs(speed * sin1));
}
else { //up + left
location.SetX(location.GetX() + speed * cos1 * -1);
location.SetY(location.GetY() + speed * sin1); //this is the range of 270 - 360 which is 270 to 360
}
}
return;
}
setNextInRoute();
if (status == Moving_to && zoneList[0] != nullptr) {
float speedo = getSpeed() * 10;
float degreeio = Coordinate::getDegree(zoneList[0]->getFarm()->getLocation(), location);
float pi = 3.14159;
float temp = degreeio;
temp = (temp)*pi / 180;
float cos1 = (speed ) * cos(temp);
float sin1 = (speed ) * sin(temp);
if (zoneList[0]->getFarm()->getLocation().GetX() == location.GetX()) {
cos1 = 0;
//sin1 *= -1;
}
if (zoneList[0]->getFarm()->getLocation().GetY() == location.GetY()) {
sin1 = 0;
//cos1 *= -1;
}
if (zoneList[0]->getFarm()->getLocation().GetX() >= location.GetX()
&& zoneList[0]->getFarm()->getLocation().GetY() >= location.GetY())
{ //up + right
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else if (zoneList[0]->getFarm()->getLocation().GetX() <= location.GetX()
&& zoneList[0]->getFarm()->getLocation().GetY() <= location.GetY())
{ //down + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1);
}
else if (zoneList[0]->getFarm()->getLocation().GetX() >= location.GetX()
&& zoneList[0]->getFarm()->getLocation().GetY() <= location.GetY())
{ //down + right
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else { //up + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1); //this is the range of 270 - 360 which is 270 to 360
}
}
else if (status == Moving_to && zoneList[0] == nullptr && zoneList[1] != nullptr) {
//float speedo = getSpeed();
float defreeFix = 0;
float degreeio = Coordinate::getDegree(zoneList[1]->getCastle()->getLocation(), location);
float speedo = getSpeed() * 10;
//float degreeio = Coordinate::getDegree(zoneList[0]->getFarm()->getLocation(), location);
float pi = 3.14159;
float temp = degreeio;
temp = (temp)*pi / 180;
float cos1 = (speed ) * cos(temp);
float sin1 = (speed ) * sin(temp);
if (zoneList[1]->getCastle()->getLocation().GetX() == location.GetX()) {
cos1 = 0;
//sin1 *= -1;
}
if (zoneList[1]->getCastle()->getLocation().GetY() == location.GetY()) {
sin1 = 0;
//cos1 *= -1;
}
if (zoneList[1]->getCastle()->getLocation().GetX() >= location.GetX()
&& zoneList[1]->getCastle()->getLocation().GetY() >= location.GetY())
{ //up + right
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else if (zoneList[1]->getCastle()->getLocation().GetX() <= location.GetX()
&& zoneList[1]->getCastle()->getLocation().GetY() <= location.GetY())
{ //down + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1);
}
else if (zoneList[1]->getCastle()->getLocation().GetX() >= location.GetX()
&& zoneList[1]->getCastle()->getLocation().GetY() <= location.GetY())
{ //down + right
location.SetX(location.GetX() + cos1);
location.SetY(location.GetY() + sin1);
}
else { //up + left
location.SetX(location.GetX() - cos1);
location.SetY(location.GetY() - sin1); //this is the range of 270 - 360 which is 270 to 360
}
}
}
void Peasant::setNextInRoute()
{
if (zoneList[0] != nullptr) {
if (this->location == zoneList[0]->getFarm()->getLocation() && currently_working == true && status == Moving_to) { //we stopping at a farm
//status = Stopped;
if (zoneList[0]->getFarm()->get_stock() < 5) {
carrying_stock = zoneList[0]->getFarm()->get_stock();
zoneList[0]->getFarm()->set_stock(0);
}
else {
carrying_stock = 5;
zoneList[0]->getFarm()->set_stock(zoneList[0]->getFarm()->get_stock() - carrying_stock);
}
zoneList[0] = nullptr;
}
}
else if(zoneList[1] != nullptr) {
if (this->location == zoneList[1]->getCastle()->getLocation() && currently_working == true) { //we reached the castle!
zoneList[1]->getCastle()->set_stock(zoneList[1]->getCastle()->get_stock() + carrying_stock);
carrying_stock = 0;
lifePoints += 1;
if (zoneList.size() > 2) {
zoneList[1] = zoneList.back();
zoneList.pop_back();
zoneList[0] = zoneList.back();
zoneList.pop_back();
return;
}
status = Stopped;
currently_working = false;
zoneList[1] = nullptr;
}
}
}
bool Peasant::isUnpacking()
{
if (status == Unpacking) return true;
return false;
}