-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.cpp
More file actions
executable file
·147 lines (115 loc) · 4.13 KB
/
unit.cpp
File metadata and controls
executable file
·147 lines (115 loc) · 4.13 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
#include "unit.h"
#include "map.h"
#include "place.h"
Unit::Unit(Map* _map):GameObject(_map){
myRoad.clear();
health=max_health;
}
int Unit::getSpeed(){
return speed;
}
struct Parent_road{
int x;
int y;
int parent_x;
int parent_y;
};
void Unit::moveToBlock(Block* _block){
if(_block->Empty_Of_Building()==false){
int number=0;//number of game object in the block
vector <GameObject*> temp_move;
temp_move=_block->getObjects();
for(unsigned int k=0; k<temp_move.size(); k++)
if(temp_move[k]!=NULL)
number=k;
this->setTargetObject(temp_move[number]);
return;
}
myRoad.clear();
//-------------------------------------------------build temp Map
block=_block;
int** map_array=NULL;
map_array=new int*[map->GetRowcount()];
for(int i=0; i<map->GetRowcount(); i++)
map_array[i]=new int[map->GetColcount()];
//-------------------------------------------------- fill the map with empty or not empty
for(int i=0; i<map->GetRowcount(); i++)
for(int j=0; j<map->GetColcount(); j++)
if(map->getBlock(i,j)->Empty_Of_Building()==true)
map_array[i][j]=-1;
else
map_array[i][j]=-2;
//---------------------------------------------------
vector <Parent_road> road;//road is a queue of block
Parent_road temp;
temp.x=place_x;
temp.y=place_y;
temp.parent_x=0;
temp.parent_y=0;
road.push_back(temp);
map_array[place_y][place_x]=0;
for(int k=0; true; k++){
int j=road[k].x;
int i=road[k].y;
//----------------------set all neighberhood with K+1
BfsHelper(k,i,j,i,j+1,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i,j-1,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i-1,j,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i+1,j,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i-1,j-1,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i-1,j+1,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i+1,j-1,map_array,map->GetRowcount(),map->GetColcount(),road);
BfsHelper(k,i,j,i+1,j+1,map_array,map->GetRowcount(),map->GetColcount(),road);
if(i==block->getRow() && j==block->getCol())//we arrive end block
break;
}
//---------------------------------------------------- find road
myRoad.clear();
int i=block->getRow();//set i equal end block
int j=block->getCol();//set j equal end block
while(true){
for(unsigned int k=0; k<road.size(); k++)
if(i==road[k].y && j==road[k].x){
place temp;
temp.y=i;
temp.x=j;
myRoad.push_back(temp);
i=road[k].parent_y;
j=road[k].parent_x;
}
if(i==this->place_y && j==this->place_x){// we arrive start block
place temp;
temp.y=i;
temp.x=j;
myRoad.push_back(temp);
break; //break from while
}
}
for(int i=0; i<map->GetRowcount(); i++)//delete array of map
delete []map_array[i];
delete []map_array;
}
void Unit::moveToBlock(int row, int col){
block=map->getBlock(row,col);
this->moveToBlock(block);
}
void Unit::stop(){
target_object=NULL;
myRoad.clear();
}
vector <place> Unit::GetRoad(){
return myRoad;
}
void Unit::BfsHelper(int &k,int i,int j,int _i,int _j,int** _map_array,int rowcount,int colcount,vector<Parent_road> &_road){
Parent_road temp;
if(_i>=0 && _i<rowcount && _j>=0 && _j<colcount)
if(_map_array[_i][_j]==-1){
_map_array[_i][_j]=k+1;
Parent_road temp;
temp.x=_j;
temp.y=_i;
temp.parent_x=j;
temp.parent_y=i;
_road.push_back(temp);
}
}