diff --git a/CAdvmapInterface.cpp b/CAdvmapInterface.cpp index a0fb3a56a..03521e2a1 100644 --- a/CAdvmapInterface.cpp +++ b/CAdvmapInterface.cpp @@ -403,7 +403,6 @@ endchkpt: currentPath=NULL; LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.getPosOfHero(LOCPLINT->adventureInt->selection)].second = NULL; } - return; } const CGHeroInstance * currentHero = (LOCPLINT->adventureInt->heroList.items.size())?(LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected].first):(NULL); if(currentHero) @@ -488,62 +487,77 @@ void CTerrainRect::showPath() } else { + /* + * notation of arrow direction: + * 1 2 3 + * 4 5 6 + * 7 8 9 + * ie. 157 means an arrow from left upper tile to left bottom tile through 5 (all arrows go through 5 in this notation) + */ std::vector & cv = currentPath->nodes; - if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) + if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) //15x { - if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) + if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //156 { pn = 3; } - else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) + else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //159 { pn = 12; } - else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) + else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //158 { pn = 21; } - else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) + else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //157 { pn = 22; } - else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) + else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //153 { pn = 2; } - } - else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) - { - if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) - { - pn = 2; - } - if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) - { - pn = 3; - } - if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) - { - pn = 4; - } - else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) - { - pn = 13; - } - else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) - { - pn = 22; - } - if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) + else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //154 { pn = 23; } - if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) + else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //152 + { + pn = 1; + } + } + else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) //25x + { + if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //253 + { + pn = 2; + } + if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //256 + { + pn = 3; + } + if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //259 + { + pn = 4; + } + else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //258 + { + pn = 13; + } + else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //257 + { + pn = 22; + } + if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //254 + { + pn = 23; + } + if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //251 { pn = 24; } } - else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) + else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) //35x { if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) { @@ -566,7 +580,7 @@ void CTerrainRect::showPath() pn = 4; } } - else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) + else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) //65x { if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) { @@ -581,7 +595,7 @@ void CTerrainRect::showPath() pn = 24; } } - else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) + else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) //95x { if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) { @@ -604,7 +618,7 @@ void CTerrainRect::showPath() pn = 18; } } - else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) + else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) //85x { if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) { @@ -635,7 +649,7 @@ void CTerrainRect::showPath() pn = 20; } } - else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) + else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) //75x { if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) { @@ -658,7 +672,7 @@ void CTerrainRect::showPath() pn = 20; } } - else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) + else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) //45x { if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) { diff --git a/CPathfinder.cpp b/CPathfinder.cpp index 9de77c3b3..5300818fa 100644 --- a/CPathfinder.cpp +++ b/CPathfinder.cpp @@ -157,7 +157,7 @@ void CPathfinder::AddNeighbors(vector* branch) { for(int j = node.y-1; j < node.y+2; j++) { - if(i >= 0 && j >= 0 && i < CGI->mh->sizes.x && j < CGI->mh->sizes.y) + if(i >= 0 && j >= 0 && i < CGI->mh->sizes.x && j < CGI->mh->sizes.y && (i!=node.x || j!=node.y)) { c = Coordinate(i,j,node.z); @@ -167,6 +167,11 @@ void CPathfinder::AddNeighbors(vector* branch) //Calculate the movement cost CalcH(&c); + if(i!=node.x && j!=node.y) + c.diagonal = true; + else + c.diagonal = false; + if(c.g != -1 && c.h != -1) { bool pass = true; //checking for allowed visiting direction @@ -255,7 +260,6 @@ void CPathfinder::AddNeighbors(vector* branch) Open.push(toAdd); } } - //delete c; } } } @@ -401,11 +405,17 @@ bool Compare::operator()(const vector& a, const vector& for(int i = 0; i < a.size(); i++) { - aTotal += a[i].g*a[i].h; + double add = a[i].g*a[i].h; + if(a[i].diagonal) + add*=1.41421356; + aTotal += add; } for(int i = 0; i < b.size(); i++) { - bTotal += b[i].g*b[i].h; + double add = b[i].g*b[i].h; + if(b[i].diagonal) + add*=1.41421356; + bTotal += add; } return aTotal > bTotal; @@ -418,4 +428,5 @@ void Coordinate::operator =(const Coordinate &other) this->z = other.z; this->g = other.g; this->h = other.h; + this->diagonal = other.diagonal; } diff --git a/CPathfinder.h b/CPathfinder.h index 25480737b..f8ad2db34 100644 --- a/CPathfinder.h +++ b/CPathfinder.h @@ -16,11 +16,12 @@ public: int z; float g; //Distance from goal int h; //Movement cost + bool diagonal; //if true, this tile will be crossed by diagonal - Coordinate() : x(NULL), y(NULL), z(NULL), g(NULL), h(NULL) {} - Coordinate(int3 xyz) : x(xyz.x), y(xyz.y), z(xyz.z){} - Coordinate(int xCor, int yCor, int zCor) : x(xCor), y(yCor), z(zCor){} - Coordinate(int xCor, int yCor, int zCor, int dist, int penalty) : x(xCor), y(yCor), z(zCor), g(dist), h(penalty){} + Coordinate() : x(NULL), y(NULL), z(NULL), g(NULL), h(NULL), diagonal(false) {} + Coordinate(int3 xyz) : x(xyz.x), y(xyz.y), z(xyz.z), diagonal(false){} + Coordinate(int xCor, int yCor, int zCor) : x(xCor), y(yCor), z(zCor), diagonal(false){} + Coordinate(int xCor, int yCor, int zCor, int dist, int penalty) : x(xCor), y(yCor), z(zCor), g(dist), h(penalty), diagonal(false){} void operator=(const Coordinate& other); }; diff --git a/hch/CLodHandler.cpp b/hch/CLodHandler.cpp index 3e3c77788..8d2e845bd 100644 --- a/hch/CLodHandler.cpp +++ b/hch/CLodHandler.cpp @@ -42,7 +42,8 @@ unsigned char * CLodHandler::giveFile(std::string defName, int * length) { unsigned char * outp = new unsigned char[ourEntry->realSize]; char name[30];memset(name,0,30); - strcat(name,"Data/"); + strcat(name, myDir.c_str()); + strcat(name, PATHSEPARATOR); strcat(name,(char*)ourEntry->name); FILE * f = fopen(name,"rb"); int result = fread(outp,1,ourEntry->realSize,f); @@ -327,6 +328,7 @@ int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic) void CLodHandler::init(std::string lodFile, std::string dirName) { + myDir = dirName; mutex = new boost::mutex; std::string Ts; FLOD = fopen(lodFile.c_str(), "rb"); diff --git a/hch/CLodHandler.h b/hch/CLodHandler.h index 75378b239..6a4faf44e 100644 --- a/hch/CLodHandler.h +++ b/hch/CLodHandler.h @@ -46,6 +46,7 @@ public: nodrze entries; unsigned int totalFiles; boost::mutex *mutex; + std::string myDir; //load files from this dir instead of .lod file int readNormalNr (unsigned char* bufor, int bytCon, bool cyclic=false); //lod header reading helper int infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits=15); //zlib fast handler