-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
233 lines (219 loc) · 7.46 KB
/
map.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
#include "map.h"
#include <map>
#include <string>
#include <chrono>
#include <cstdlib>
Map::Map(int l,int a,int b,const std::string& seed):RoomNumber(0),pixmap(new char[a*b]),x(a),y(b){
for(int i=0;i<a*b;i++){
pixmap[i]='0';
}
for(int i=0;i<l;i++){
addRandomRoom(seed);
}
addPaths();
}
Map::~Map(){
delete [] pixmap;
for(Room* i:areas)
delete i;
}
/*bool Map::IsInBounds(const Room& r) const{
Room metaroom(0,0,x,y);
return metaroom.isInArea(r.getVertex(0)) && metaroom.isInArea(r.getVertex(1)) && metaroom.isInArea(r.getVertex(2)) && metaroom.isInArea(r.getVertex(3));
}*/
bool Map::IsInBounds(const Coordinates& c) const{
return c.getX()>=0 && c.getX()<x && c.getY()>=0 && c.getY()<y;
}
void Map::addRandomRoom(std::string code){
std::seed_seq seed(code.begin(),code.end());
std::default_random_engine enginex(seed);
std::uniform_int_distribution<int> distributionx(0,x-7),distributiony(0,y-7);
bool roomcreated=false;
while(!roomcreated){
auto seed1 = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine enginey(seed1);
int newx=distributionx(enginex);
int newy=distributiony(enginey);
bool vertexcol=false;
for(unsigned int i=0;i<areas.size() && !vertexcol;i++){
if(areas[i]->isInArea(Coordinates(newx,newy))){
vertexcol=true;
}
}
if(!vertexcol){
int maxxl=x-newx;
int maxyl=y-newy;
bool validdim=false;
while(!validdim && maxxl>=7 && maxyl>=7){
std::uniform_int_distribution<int> distributionxl(7,maxxl),distributionyl(7,maxyl);
int newxl=distributionxl(enginex);
int newyl=distributionyl(enginey);
Room newroom(newx,newy,newxl,newyl);
validdim=true;
for(unsigned int i=0;i<areas.size() && validdim;i++){
if(areas[i]->isColliding(newroom)){
validdim=false;
maxyl--;
maxxl--;
}
}
if(validdim){
drawRoom(newroom);
areas.push_back(new Room(newroom));
roomcreated=true;
}
}
}
}
RoomNumber++;
}
//areas
void Map::drawRoom(const Room& todraw){
Coordinates v=todraw.getVertex();
for(int i=0;i<todraw.getLengthX();i++){
for(int j=0;j<todraw.getLengthY();j++){
char toinsert= i==0 || j==0 || i==todraw.getLengthX()-1 || j==todraw.getLengthY()-1?'X':'-';
pixmap[v.getX()+i+(v.getY()+j)*y]=toinsert;
}
}
}
void Map::addPaths(){
std::vector<bool> trace(areas.size(),false);
unsigned int i=0;
bool roomleft=true;
while(roomleft){
Coordinates croom=areas[i]->getCenter();
unsigned int minroom=i;
int mindistance=INT_MAX;
roomleft=false;
for(unsigned int j=0;j<areas.size();j++){
if(i!=j && !trace[j]){
roomleft=true;
int cdist=croom.distance(areas[j]->getCenter());
if(cdist<mindistance){
minroom=j;
mindistance=cdist;
}
}
}
if(roomleft){
std::vector<Coordinates> path=pathfind(i,minroom);
for(Coordinates c:path){
if(pixmap[x*c.getY()+c.getX()]!='-'){
pixmap[x*c.getY()+c.getX()]='=';
}
}
trace[minroom]=true;
i=minroom;
}
else{}
}
}
std::vector<Coordinates> Map::pathfind(unsigned int start,unsigned int goal) const{
std::vector<Coordinates> out;
Coordinates cstart=areas[start]->getCenter();
Coordinates cgoal=areas[goal]->getCenter();
std::vector<Coordinates> checked;
std::multimap<int,Coordinates>frontier;
frontier.insert(std::make_pair(cstart.distance(cgoal),cstart));
char* field=getPixmapExcluding(start,goal);
Coordinates* track=new Coordinates[x*y];
int* g=new int [x*y];
for(int i=0;i<x*y;i++){
track[i]={-1,-1};
g[i]=INT_MAX;
}
g[x*cstart.getY()+cstart.getX()]=0;
while(frontier.size()){
Coordinates current=frontier.begin()->second;
if(current==cgoal){
out=buildpath(current,track);
delete [] field; delete [] track; delete [] g;
return out;
}
//
checked.push_back(frontier.begin()->second);
frontier.erase(frontier.cbegin());
for(int i=0;i<4;i++){
Coordinates toinsert=current.neighbour(i);
if(IsInBounds(toinsert) && field[x*toinsert.getY()+toinsert.getX()]=='0'){
bool inChecked=false;
for(Coordinates i:checked){
if(i==toinsert){
inChecked=true;
}
}
if(!inChecked){
bool updateFrontier=false;
bool inFrontier=false;
int newg=g[x*current.getY()+current.getX()]+1;
std::multimap<int,Coordinates>::const_iterator posfront;
for(auto it=frontier.cbegin();it!=frontier.cend() && !inFrontier;it++){
if(it->second==toinsert){
inFrontier=true;
posfront=it;
}
}
if(inFrontier){
if(newg<g[x*toinsert.getY()+toinsert.getX()]){
frontier.erase(posfront);
updateFrontier=true;
}
}
else{
updateFrontier=true;
}
if(updateFrontier){
frontier.insert(std::make_pair(newg+toinsert.distance(cgoal),toinsert));
g[x*toinsert.getY()+toinsert.getX()]=newg;
track[x*toinsert.getY()+toinsert.getX()]=current;
}
}
}
}
//
}
delete [] field; delete [] track; delete [] g;
return out;
}
std::vector<Coordinates> Map::buildpath(Coordinates current,Coordinates* trace) const{//tocheck
std::vector<Coordinates> out;
Coordinates ancestor=trace[x*current.getY()+current.getX()];
if(ancestor!=Coordinates(-1,-1)){
out=buildpath(ancestor,trace);
}
out.push_back(current);
return out;
}
char* Map::getPixmapExcluding(unsigned int first,unsigned int last) const{
char* npix=new char[x*y];
for(int i=0;i<x*y;i++){
npix[i]=pixmap[i];
}
Room* vfirst=areas[first];
Room* vlast=areas[last];
Room* erasing=vfirst;
while(erasing){
for(int i=0;i<erasing->getLengthY();i++)
for(int j=0;j<erasing->getLengthX();j++)
npix[erasing->getVertex().getX()+j+(erasing->getVertex().getY()+i)*x]='0';
erasing=(erasing==vfirst)?vlast:nullptr;
}
return npix;
}
int Map::getDimX() const{return x;}
int Map::getDimY() const{return y;}
std::vector<Room*> Map::getAreas() const{return areas;}
std::ostream& operator<<(std::ostream& os,const Map& m){
for(auto it:m.areas){
os<<*it<<std::endl;
}
os<<std::endl;
for(int i=0;i<m.y;i++){
for(int j=0;j<m.x;j++){
os<<m.pixmap[j+i*m.x]<<" ";
}
os<<std::endl;
}
return os;
}