-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgame.cpp
More file actions
293 lines (262 loc) · 9.27 KB
/
Copy pathgame.cpp
File metadata and controls
293 lines (262 loc) · 9.27 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
#include "game.h"
game::game()
{
pathfinding = std::make_shared<pathfinder>();
setDestination(0,0);
}
std::shared_ptr<EnemyUnit> game::getClosestEnemy(){
float min_cost = std::numeric_limits<float>::infinity();
auto result = defeatableEnemies[0];
for(auto unit:defeatableEnemies){
setDestination(unit->getXPos(),unit->getYPos());
bool finished = pathfinding->calcPath_Dijkstra(xDes,yDes);
if(finished){ //path found, enemy is reachable
float my_cost = pathfinding->getMoveCost();
if(my_cost<min_cost){
min_cost = my_cost;
result = unit;
}
//clear memory created by finding the closet enemy
setStart(protagonist->getXPos(),protagonist->getYPos());
pathfinding->setMoveCost(0.0f);
}
pathfinding->screen->clearPath();
}
return result;
}
bool game::isAllDefeated()
{
bool flag = true;
for(auto& unit:enemies){
if(!unit->getDefeated()){
flag = false;
}
}
return flag;
}
std::shared_ptr<HealthModel> game::getClosestHealthpack()
{
float min_cost = 10000;
auto result = healthpacks[0];
for(auto pack:healthpacks){
setDestination(pack->getXPos(),pack->getYPos());
bool finished = pathfinding->calcPath_Dijkstra(xDes,yDes);
if(finished){ //path found, health is reachable
float my_cost = pathfinding->getMoveCost();
if(my_cost<min_cost){
min_cost = my_cost;
result = pack;
}
//clear memory created by finding the closet enemy
setStart(protagonist->getXPos(),protagonist->getYPos());
pathfinding->setMoveCost(0.0f);
}
pathfinding->screen->clearPath();
}
//healthpacks.erase(result);
return result;
}
void game::strat()
{
while(!isAllDefeated()){ //check if all enemies are defeated
QCoreApplication::processEvents();
if(!goOn){return;}
while((!isDefeatable())){//check if there is a defeatable enemy before calculate the path for closest enemy
if(healthpacks.size()==0){
qDebug()<<"Quit: NO healthpack left!";
return;
}
if(!goForHealthpack()){
return; //quit the loop
}
}
if(!goForEnemy()){
if(!goForHealthpack()){
return; //quit the loop
}
}
}
setDestination(0,0);
}
void game::setView(std::shared_ptr<view> test)
{
pathfinding->screen = test;
}
void game::setDestination(int x, int y){
xDes = x;
yDes = y;
emit destinationChanged(x,y);
}
void game::loadWorld(QString path){
//clear enemy and healthpack vectors
pathfinding->clearLists();
enemies.clear();
healthpacks.clear();
//load image and determine size
QImage image(path);
pathfinding->setXmax(image.width()-1);
pathfinding->setYmax(image.height()-1);
//modify number of enemies and healthpacks based on world size. If world is huge, don't generate objects
int objectNum = (pathfinding->getXmax()+pathfinding->getYmax())/2;
if(objectNum > 100){
objectNum = 2;
}
//Convert enemies to custom EnemyUnit
pathfinding->tiles = myWorld->createWorld(path);
auto tempenemies = myWorld->getEnemies(objectNum*4/5); //set ratio enemies/healthpacks = 4/5
for(auto& unit:tempenemies){
enemies.push_back(std::shared_ptr<EnemyUnit>(new EnemyUnit(unit->getXPos(), unit->getYPos(), unit->getValue())));
// int pos = (pathfinding->getXmax()+1)*unit->getYPos() + unit->getXPos(); // set enemies impassable
// pathfinding->tiles[pos]->setValue(std::numeric_limits<float>::infinity());
}
//Convert Healthpacks to custom HealthModel
auto temphealthpacks = myWorld->getHealthPacks(objectNum);
for(auto& pack:temphealthpacks){
healthpacks.push_back(std::shared_ptr<HealthModel>(new HealthModel(pack->getXPos(), pack->getYPos(), pack->getValue())));
}
// Load protagonist and update mainwindow to display stats
// auto tempagonist = myWorld->getProtagonist();
protagonist = std::shared_ptr<Hero>(new Hero());
// Display the world into a graphicsscene
pathfinding->screen->displayWorld(image);
}
void game::setStart(int x, int y){
protagonist->setXPos(x);
protagonist->setYPos(y);
pathfinding->clearLists();
auto pos1 = std::make_shared<Tile>(std::move(*(protagonist)));
pathfinding->myIndexes.insert((pathfinding->getXmax()+1)*y+x);
node myNode1(pos1,nullptr); //Starting point
pathfinding->currentNodes.enqueue(myNode1);
myNode1.setDistance(0);
pathfinding->availableNodes.push_back(myNode1);
}
void game::MoveProtagonist()
{
while(pathfinding->route.size()){
auto tile = pathfinding->route.pop();
protagonist->setXPos((tile->getXPos()));
protagonist->setYPos((tile->getYPos()));
float newEnergy = protagonist->getEnergy()-1 - pathfinding->getWeight()*(1-tile->getValue());
protagonist->updateEnergy(newEnergy);
}
pathfinding->clearLists();
pathfinding->setMoveCost(0.0f);
setStart(protagonist->getXPos(),protagonist->getYPos());
}
void game::checkMove(int xPos, int yPos){
if(xPos>=0 && xPos<=pathfinding->getXmax() && yPos>=0 && yPos<=pathfinding->getYmax()){
int index = yPos*(pathfinding->getXmax()+1) + xPos;
if(!(std::isinf(pathfinding->tiles[index]->getValue()))){
protagonist->setPos(xPos,yPos);
}
}
}
void game::removeHealthpack(std::shared_ptr<HealthModel> healthpack)
{
healthpack->useHealthpack();
int pos = find(healthpacks.begin(), healthpacks.end(), healthpack) - healthpacks.begin();
healthpacks.erase(healthpacks.begin()+pos);
}
void game::go(int i)
{
auto finished = false;
switch (i) {
case 3:
finished = pathfinding->calcPath_BreadthFirst(xDes,yDes);
break;
case 4:
finished = pathfinding->calcPath_BestFirst(xDes,yDes);
break;
default:
finished = pathfinding->calcPath_Dijkstra(xDes,yDes);
break;
}
// Calculate the path
// Move the protagonist based on the calculated path
if(finished){
MoveProtagonist();
}else{
setStart(protagonist->getXPos(),protagonist->getYPos());
}
}
bool game::goForHealthpack()
{
auto healthpack = getClosestHealthpack();
setDestination(healthpack->getXPos(),healthpack->getYPos());
//pathfinding->setWeight(weight);
bool find = pathfinding->calcPath_Dijkstra(xDes,yDes);
if(find){
float requiredEnergy = pathfinding->getMoveCost();
if(requiredEnergy>protagonist->getEnergy()){
return false; //quit the loop
}else{
MoveProtagonist();
float newHealth = protagonist->getHealth()+healthpack->getValue(); //multiply by a factor of 5
if(newHealth > 100) newHealth = 100;
protagonist->updateHealth(newHealth);
pathfinding->setMoveCost(0.0f);
// Move the protagonist based on the calculated path
removeHealthpack(healthpack);
return true;
}
}else{
qDebug()<<"Healthpack is not found!";
return false;
}
}
bool game::isDefeatable()
{
defeatableEnemies.clear();
for(auto& unit:enemies){
if(!unit->getDefeated()){ //when a enemy is not defeated
if(unit->getValue() < protagonist->getHealth()){ //when a enemy has a strength smaller than pro's health
//qDebug()<<"At least one enemy is defeatable with strength: "<<(*it)->getValue();
defeatableEnemies.push_back(unit);
}
}
} //qDebug()<<"NO enemy can be defeated!";
if(defeatableEnemies.size()>0){
return true;
}else{
return false;
}
}
bool game::goForEnemy()
{
auto unit = getClosestEnemy(); //calculate the path and get the closest enemy
//After the check that there is at least one defeatable enemy, which reduce the calls of getClosestEnemy();
//the closestEnemy could still be undefeatable
while(unit->getValue()>protagonist->getHealth()){
qDebug()<<"Health is not enough to defeat an enemy, go for healthpack";
if(healthpacks.size()==0){
qDebug()<<"Quit: NO healthpack left!";
return false;
}
if(!goForHealthpack()){
return false; //quit the loop
}
}
setDestination(unit->getXPos(),unit->getYPos());
bool finished = false;
finished = pathfinding->calcPath_Dijkstra(xDes,yDes);
if(finished){ //Path found
float requiredEnergy = pathfinding->getMoveCost();
if(requiredEnergy>protagonist->getEnergy()){
setStart(protagonist->getXPos(),protagonist->getYPos());
return false; //quit the loop
}else{
// Move the protagonist based on the calculated path
MoveProtagonist();
unit->kill();
float newHealth = protagonist->getHealth()-unit->getValue();
protagonist->updateHealth(newHealth);
protagonist->updateEnergy(100);
pathfinding->setMoveCost(0.0f);
}
return true;
}else{ //Path not found
setStart(protagonist->getXPos(),protagonist->getYPos());
return false;
}
}