1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

* added selecting path by clicking on terrain (experimental)

* coordinates in CPathNode are stored in int3
* minor improvements
This commit is contained in:
Michał W. Urbańczyk
2007-09-22 01:16:31 +00:00
parent 6f4156c901
commit cfc94847a1
7 changed files with 134 additions and 91 deletions

View File

@ -552,7 +552,7 @@ CTerrainRect::CTerrainRect():currentPath(NULL)
tilesh=18; tilesh=18;
pos.x=7; pos.x=7;
pos.y=6; pos.y=6;
pos.w=594; pos.w=593;
pos.h=547; pos.h=547;
arrows = CGI->spriteh->giveDef("ADAG.DEF"); arrows = CGI->spriteh->giveDef("ADAG.DEF");
for(int y=0; y<arrows->ourImages.size(); ++y) for(int y=0; y<arrows->ourImages.size(); ++y)
@ -574,8 +574,35 @@ void CTerrainRect::deactivate()
Hoverable::deactivate(); Hoverable::deactivate();
KeyInterested::deactivate(); KeyInterested::deactivate();
}; };
void CTerrainRect::clickLeft(tribool down){} void CTerrainRect::clickLeft(tribool down)
void CTerrainRect::clickRight(tribool down){} {
if ((down==false) || indeterminate(down))
return;
int3 mp;
mp.x = LOCPLINT->adventureInt->position.x + ((LOCPLINT->current->motion.x-pos.x)/32);
mp.y = LOCPLINT->adventureInt->position.y + ((LOCPLINT->current->motion.y-pos.y)/32);
mp.z = LOCPLINT->adventureInt->position.z;
if (currentPath)
{
if ( (currentPath->endPos()) == mp)
{ //move
return;
}
else
{
delete currentPath;
}
}
const CHeroInstance * currentHero = LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected];
currentPath = CGI->pathf->getPath(currentHero->pos,mp,currentHero);
}
void CTerrainRect::clickRight(tribool down)
{
}
void CTerrainRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
{
//TODO: print names of objects in toolbar
}
void CTerrainRect::hover(bool on){} void CTerrainRect::hover(bool on){}
void CTerrainRect::keyPressed (SDL_KeyboardEvent & key){} void CTerrainRect::keyPressed (SDL_KeyboardEvent & key){}
void CTerrainRect::show() void CTerrainRect::show()
@ -592,8 +619,8 @@ void CTerrainRect::show()
int pn=-1;//number of picture int pn=-1;//number of picture
if (i==0) //last tile if (i==0) //last tile
{ {
int x = 32*(currentPath->nodes[i].x-LOCPLINT->adventureInt->position.x)+7, int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+7,
y = 32*(currentPath->nodes[i].y-LOCPLINT->adventureInt->position.y)+6; y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+6;
if (x<0 || y<0 || x>pos.w || y>pos.h) if (x<0 || y<0 || x>pos.w || y>pos.h)
continue; continue;
pn=0; pn=0;
@ -601,154 +628,154 @@ void CTerrainRect::show()
else else
{ {
std::vector<CPathNode> & cv = currentPath->nodes; std::vector<CPathNode> & cv = currentPath->nodes;
if (cv[i+1].x == cv[i].x-1 && cv[i+1].y == cv[i].y-1) 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].x == cv[i].x+1 && cv[i-1].y == cv[i].y) if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 3; pn = 3;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 12; pn = 12;
} }
else if(cv[i-1].x == cv[i].x && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 21; pn = 21;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 22; pn = 22;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 2; pn = 2;
} }
} }
else if (cv[i+1].x == cv[i].x && cv[i+1].y == cv[i].y-1) 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].x == cv[i].x+1 && cv[i-1].y == cv[i].y+1) if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 4; pn = 4;
} }
else if(cv[i-1].x == cv[i].x && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 13; pn = 13;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 22; pn = 22;
} }
} }
else if (cv[i+1].x == cv[i].x+1 && cv[i+1].y == cv[i].y-1) else 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].x == cv[i].x && cv[i-1].y == cv[i].y+1) if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 5; pn = 5;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 14; pn = 14;
} }
else if(cv[i-1].x-1 == cv[i].x && cv[i-1].y == cv[i].y) else if(cv[i-1].coord.x-1 == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 23; pn = 23;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 24; pn = 24;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 4; pn = 4;
} }
} }
else if (cv[i+1].x == cv[i].x+1 && cv[i+1].y == cv[i].y) else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y)
{ {
if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y+1) if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 6; pn = 6;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 15; pn = 15;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 24; pn = 24;
} }
} }
else if (cv[i+1].x == cv[i].x+1 && cv[i+1].y == cv[i].y+1) else 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].x == cv[i].x-1 && cv[i-1].y == cv[i].y) if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 7; pn = 7;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 16; pn = 16;
} }
else if(cv[i-1].x == cv[i].x && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 17; pn = 17;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 6; pn = 6;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 18; pn = 18;
} }
} }
else if (cv[i+1].x == cv[i].x && cv[i+1].y == cv[i].y+1) 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].x == cv[i].x-1 && cv[i-1].y == cv[i].y-1) if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 8; pn = 8;
} }
else if(cv[i-1].x == cv[i].x && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 9; pn = 9;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 18; pn = 18;
} }
} }
else if (cv[i+1].x == cv[i].x-1 && cv[i+1].y == cv[i].y+1) else 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].x == cv[i].x && cv[i-1].y == cv[i].y-1) if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 1; pn = 1;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 10; pn = 10;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 19; pn = 19;
} }
else if(cv[i-1].x == cv[i].x-1 && cv[i-1].y == cv[i].y-1) else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1)
{ {
pn = 8; pn = 8;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 20; pn = 20;
} }
} }
else if (cv[i+1].x == cv[i].x-1 && cv[i+1].y == cv[i].y) else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y)
{ {
if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].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; pn = 2;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y)
{ {
pn = 11; pn = 11;
} }
else if(cv[i-1].x == cv[i].x+1 && cv[i-1].y == cv[i].y+1) else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1)
{ {
pn = 20; pn = 20;
} }
@ -756,8 +783,8 @@ void CTerrainRect::show()
} }
if (pn>=0) if (pn>=0)
{ {
int x = 32*(currentPath->nodes[i].x-LOCPLINT->adventureInt->position.x)+7, int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+7,
y = 32*(currentPath->nodes[i].y-LOCPLINT->adventureInt->position.y)+6; y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+6;
if (x<0 || y<0 || x>pos.w || y>pos.h) if (x<0 || y<0 || x>pos.w || y>pos.h)
continue; continue;
int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w), int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
@ -967,6 +994,7 @@ void CAdvMapInt::show()
heroList.draw(); heroList.draw();
townList.activate(); townList.activate();
townList.draw(); townList.draw();
terrain.activate();
resdatabar.draw(); resdatabar.draw();

View File

@ -144,7 +144,8 @@ public:
void deactivate(); // makes button inactive (but don't deletes) void deactivate(); // makes button inactive (but don't deletes)
}; };
class CTerrainRect class CTerrainRect
: public ClickableL, public ClickableR, public Hoverable, public virtual CIntObject, public KeyInterested : public ClickableL, public ClickableR, public Hoverable, public virtual CIntObject, public KeyInterested,
public MotionInterested
{ {
public: public:
int tilesw, tilesh; int tilesw, tilesh;
@ -156,6 +157,7 @@ public:
void clickLeft(tribool down); void clickLeft(tribool down);
void clickRight(tribool down); void clickRight(tribool down);
void hover(bool on); void hover(bool on);
void mouseMoved (SDL_MouseMotionEvent & sEvent);
void keyPressed (SDL_KeyboardEvent & key); void keyPressed (SDL_KeyboardEvent & key);
void show(); void show();
}; };

View File

@ -25,8 +25,8 @@ bool CCallback::moveHero(int ID, int3 destPoint)
for(int i=ourPath->nodes.size()-1; i>0; i--) for(int i=ourPath->nodes.size()-1; i>0; i--)
{ {
int3 stpos, endpos; int3 stpos, endpos;
stpos = int3(ourPath->nodes[i].x, ourPath->nodes[i].y, CGI->heroh->heroInstances[ID]->pos.z); stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, CGI->heroh->heroInstances[ID]->pos.z);
endpos = int3(ourPath->nodes[i-1].x, ourPath->nodes[i-1].y, CGI->heroh->heroInstances[ID]->pos.z); endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, CGI->heroh->heroInstances[ID]->pos.z);
HeroMoveDetails curd; HeroMoveDetails curd;
curd.src = stpos; curd.src = stpos;
curd.dst = endpos; curd.dst = endpos;

View File

@ -4,7 +4,16 @@
#include "CGameInfo.h" #include "CGameInfo.h"
#include "hch\CAmbarCendamo.h" #include "hch\CAmbarCendamo.h"
#include "mapHandler.h" #include "mapHandler.h"
CPath * CPathfinder::getPath(int3 &src, int3 &dest, CHeroInstance * hero) //TODO: test it (seems to be finished, but relies on unwritten functions :() int3 CPath::startPos()
{
return int3(nodes[0].coord.x,nodes[0].coord.y,nodes[0].coord.z);
}
int3 CPath::endPos()
{
return int3(nodes[nodes.size()-1].coord.x,nodes[nodes.size()-1].coord.y,nodes[nodes.size()-1].coord.z);
}
CPath * CPathfinder::getPath(const int3 &src, const int3 &dest, const CHeroInstance * hero) //TODO: test it (seems to be finished, but relies on unwritten functions :()
{ {
if(src.z!=dest.z) //first check if(src.z!=dest.z) //first check
return NULL; return NULL;
@ -23,8 +32,9 @@ CPath * CPathfinder::getPath(int3 &src, int3 &dest, CHeroInstance * hero) //TODO
graph[i][j]->dist = -1; graph[i][j]->dist = -1;
graph[i][j]->theNodeBefore = NULL; graph[i][j]->theNodeBefore = NULL;
graph[i][j]->visited = false; graph[i][j]->visited = false;
graph[i][j]->x = i; graph[i][j]->coord.x = i;
graph[i][j]->y = j; graph[i][j]->coord.y = j;
graph[i][j]->coord.z = dest.z;
} }
} }
@ -41,7 +51,7 @@ CPath * CPathfinder::getPath(int3 &src, int3 &dest, CHeroInstance * hero) //TODO
{ {
CPathNode * cp = mq.front(); CPathNode * cp = mq.front();
mq.pop(); mq.pop();
if ((cp->x == dest.x) && (cp->y==dest.y)) if ((cp->coord.x == dest.x) && (cp->coord.y==dest.y))
{ {
if (cp->dist < curDist) if (cp->dist < curDist)
curDist=cp->dist; curDist=cp->dist;
@ -51,82 +61,82 @@ CPath * CPathfinder::getPath(int3 &src, int3 &dest, CHeroInstance * hero) //TODO
if (cp->dist > curDist) if (cp->dist > curDist)
continue; continue;
} }
if(cp->x>0) if(cp->coord.x>0)
{ {
CPathNode * dp = graph[cp->x-1][cp->y]; CPathNode * dp = graph[cp->coord.x-1][cp->coord.y];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->y>0) if(cp->coord.y>0)
{ {
CPathNode * dp = graph[cp->x][cp->y-1]; CPathNode * dp = graph[cp->coord.x][cp->coord.y-1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x, cp->y-1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y-1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->x>0 && cp->y>0) if(cp->coord.x>0 && cp->coord.y>0)
{ {
CPathNode * dp = graph[cp->x-1][cp->y-1]; CPathNode * dp = graph[cp->coord.x-1][cp->coord.y-1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y-1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y-1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y-1, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y-1, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->x<graph.size()-1) if(cp->coord.x<graph.size()-1)
{ {
CPathNode * dp = graph[cp->x+1][cp->y]; CPathNode * dp = graph[cp->coord.x+1][cp->coord.y];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->y<graph[0].size()-1) if(cp->coord.y<graph[0].size()-1)
{ {
CPathNode * dp = graph[cp->x][cp->y+1]; CPathNode * dp = graph[cp->coord.x][cp->coord.y+1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x, cp->y+1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y+1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x, cp->y+1, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x, cp->coord.y+1, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->x<graph.size()-1 && cp->y<graph[0].size()-1) if(cp->coord.x<graph.size()-1 && cp->coord.y<graph[0].size()-1)
{ {
CPathNode * dp = graph[cp->x+1][cp->y+1]; CPathNode * dp = graph[cp->coord.x+1][cp->coord.y+1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y+1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y+1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y+1, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y+1, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->x>0 && cp->y<graph[0].size()-1) if(cp->coord.x>0 && cp->coord.y<graph[0].size()-1)
{ {
CPathNode * dp = graph[cp->x-1][cp->y+1]; CPathNode * dp = graph[cp->coord.x-1][cp->coord.y+1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y+1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y+1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y+1, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x-1, cp->coord.y+1, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }
} }
if(cp->x<graph.size()-1 && cp->y>0) if(cp->coord.x<graph.size()-1 && cp->coord.y>0)
{ {
CPathNode * dp = graph[cp->x+1][cp->y-1]; CPathNode * dp = graph[cp->coord.x+1][cp->coord.y-1];
if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y-1, src.z), hero))) && dp->accesible) if((dp->dist==-1 || (dp->dist > cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y-1, src.z), hero))) && dp->accesible)
{ {
dp->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y-1, src.z), hero); dp->dist = cp->dist + CGI->mh->getCost(int3(cp->coord.x, cp->coord.y, src.z), int3(cp->coord.x+1, cp->coord.y-1, src.z), hero);
dp->theNodeBefore = cp; dp->theNodeBefore = cp;
mq.push(dp); mq.push(dp);
} }

View File

@ -9,13 +9,16 @@ struct CPathNode
bool accesible; //true if a hero can be on this node bool accesible; //true if a hero can be on this node
int dist; //distance from the first node of searching; -1 is infinity int dist; //distance from the first node of searching; -1 is infinity
CPathNode * theNodeBefore; CPathNode * theNodeBefore;
int x, y; //coordiantes int3 coord; //coordiantes
bool visited; bool visited;
}; };
struct CPath struct CPath
{ {
std::vector<CPathNode> nodes; //just get node by node std::vector<CPathNode> nodes; //just get node by node
int3 startPos(); // start point
int3 endPos(); //destination point
}; };
/** /**
@ -26,8 +29,8 @@ class CPathfinder
private: private:
std::vector< std::vector<CPathNode *> > graph; std::vector< std::vector<CPathNode *> > graph;
public: public:
CPath * getPath(int3 & src, int3 & dest, CHeroInstance * hero); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists CPath * getPath(const int3 & src, const int3 & dest, const CHeroInstance * hero); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists
CPath * getPath(int3 & src, int3 & dest, CHeroInstance * hero, int (*getDist)(int3 & a, int3 & b)); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists; uses getDist to calculate distance CPath * getPath(const int3 & src, const int3 & dest, const CHeroInstance * hero, int (*getDist)(int3 & a, int3 & b)); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists; uses getDist to calculate distance
}; };
#endif //CPATHFINDER_H #endif //CPATHFINDER_H

View File

@ -937,7 +937,7 @@ char & CMapHandler::undVisAccess(int x, int y)
return undVisibility[x+Woff][y+Hoff]; return undVisibility[x+Woff][y+Hoff];
} }
int CMapHandler::getCost(int3 &a, int3 &b, CHeroInstance *hero) int CMapHandler::getCost(int3 &a, int3 &b, const CHeroInstance *hero)
{ {
int ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle]; int ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle];
if(!(a.x==b.x || a.y==b.y)) if(!(a.x==b.x || a.y==b.y))

View File

@ -76,7 +76,7 @@ public:
SDL_Surface mirrorImage(SDL_Surface *src); //what is this?? SDL_Surface mirrorImage(SDL_Surface *src); //what is this??
SDL_Surface * getVisBitmap(int x, int y, std::vector< std::vector<char> > & visibility); SDL_Surface * getVisBitmap(int x, int y, std::vector< std::vector<char> > & visibility);
int getCost(int3 & a, int3 & b, CHeroInstance * hero); int getCost(int3 & a, int3 & b, const CHeroInstance * hero);
void init(); void init();
SDL_Surface * terrainRect(int x, int y, int dx, int dy, int level=0, unsigned char anim=0); SDL_Surface * terrainRect(int x, int y, int dx, int dy, int level=0, unsigned char anim=0);
SDL_Surface * terrBitmap(int x, int y); SDL_Surface * terrBitmap(int x, int y);