From 8cc7e103bd75f19805195e9d5c712980d4a33e8d Mon Sep 17 00:00:00 2001 From: mateuszb Date: Sun, 12 Aug 2007 17:48:05 +0000 Subject: [PATCH] * some stuff with pathfinding (some functions need to be written, maybe someone help me?) * CGI is now defined in global.h * some code improvements --- CAdvmapInterface.cpp | 5 ++ CAdvmapInterface.h | 5 +- CAmbarCendamo.cpp | 13 ++- CHeroHandler.cpp | 17 +++- CHeroHandler.h | 8 +- CMessage.cpp | 1 - CMessage.h | 2 +- CObjectHandler.cpp | 2 - CPathfinder.cpp | 187 +++++++++++++++++++++++++++++++++---------- CPathfinder.h | 10 +-- CPreGame.cpp | Bin 115554 -> 115486 bytes CTownHandler.cpp | 2 - global.h | 2 + mapHandler.cpp | 40 +++++++++ mapHandler.h | 2 + stdafx.h | 1 + 16 files changed, 235 insertions(+), 62 deletions(-) diff --git a/CAdvmapInterface.cpp b/CAdvmapInterface.cpp index 01d0aa506..a2379b6df 100644 --- a/CAdvmapInterface.cpp +++ b/CAdvmapInterface.cpp @@ -1,6 +1,11 @@ #include "stdafx.h" #include "CAdvmapInterface.h" + extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX; //fonts + +using namespace boost::logic; +using namespace CSDL_Ext; + CAdvMapInt::~CAdvMapInt() { SDL_FreeSurface(bg); diff --git a/CAdvmapInterface.h b/CAdvmapInterface.h index 901c59a52..79824f1a1 100644 --- a/CAdvmapInterface.h +++ b/CAdvmapInterface.h @@ -8,9 +8,8 @@ #include "CGameInfo.h" #include "SDL_Extensions.h" #include -using namespace boost::logic; -#define CGI (CGameInfo::mainObj) -using namespace CSDL_Ext; +#include "global.h" + class AdventureMapButton : public ClickableL, public ClickableR, public Hoverable, public KeyInterested, public CButtonBase { diff --git a/CAmbarCendamo.cpp b/CAmbarCendamo.cpp index 31db3389e..799da6e28 100644 --- a/CAmbarCendamo.cpp +++ b/CAmbarCendamo.cpp @@ -9,7 +9,6 @@ #include #include #include -#define CGI (CGameInfo::mainObj) unsigned int intPow(unsigned int a, unsigned int b) { @@ -782,6 +781,18 @@ void CAmbarCendamo::deh3m() } i+=16; nobj->info = spec; + //////creating CHeroInstance + CHeroInstance * nhi = new CHeroInstance; + nhi->exp = spec->experience; + nhi->level = CGI->heroh->level(nhi->exp); + nhi->mana = spec->knowledge * 10; + nhi->movement = -1; + nhi->name = spec->name; + nhi->owner = spec->player; + nhi->pos = nobj->pos; + nhi->type = spec->type; + nhi->army = spec->garrison; + CGI->heroh->heroInstances.push_back(nhi); break; } case CREATURES_DEF: diff --git a/CHeroHandler.cpp b/CHeroHandler.cpp index 82a6a83f7..c511f29d5 100644 --- a/CHeroHandler.cpp +++ b/CHeroHandler.cpp @@ -5,8 +5,6 @@ #include "CGameInfo.h" #include "CGeneralTextHandler.h" -#define CGI (CGameInfo::mainObj) - CHeroHandler::~CHeroHandler() { for (int j=0;jheroClass = heroClasses[heroes[gg]->heroType]; } } + +unsigned int CHeroInstance::getTileCost(EterrainType & ttype, Eroad & rdtype, Eriver & rvtype) +{ + return 100; //TODO: finish it +} + +unsigned int CHeroHandler::level(unsigned int experience) +{ + return 0; //TODO: finish it +} + +unsigned int CHeroInstance::getLowestCreatureSpeed() +{ + return 1; //TODO: finish it +} \ No newline at end of file diff --git a/CHeroHandler.h b/CHeroHandler.h index a92bae64c..82856a637 100644 --- a/CHeroHandler.h +++ b/CHeroHandler.h @@ -44,7 +44,7 @@ class CHeroInstance { public: int owner; - CHero type; + CHero * type; int exp; //experience point int level; //current level of hero std::string name; //may be custom @@ -54,6 +54,10 @@ public: CCreatureSet army; //army int mana; // remaining spell points int movement; //remaining movement points + unsigned int getTileCost(EterrainType & ttype, Eroad & rdtype, Eriver & rvtype); + unsigned int getLowestCreatureSpeed(); + unsigned int getAdditiveMoveBonus(); + unsigned float getMultiplicativeMoveBonus(); //TODO: artifacts, primary and secondary skills, known spells, commander, blessings, curses, morale/luck special modifiers }; @@ -62,6 +66,8 @@ class CHeroHandler public: std::vector heroes; //było nodrze std::vector heroClasses; + std::vector heroInstances; + unsigned int level(unsigned int experience); void loadHeroes(); void loadSpecialAbilities(); void loadBiographies(); diff --git a/CMessage.cpp b/CMessage.cpp index 9352fa422..bb358cebf 100644 --- a/CMessage.cpp +++ b/CMessage.cpp @@ -5,7 +5,6 @@ #include "CDefHandler.h" #include "CGameInfo.h" #include "SDL_Extensions.h" -#define CGI (CGameInfo::mainObj) SDL_Color tytulowy, tlo, zwykly ; SDL_Rect genRect(int hh, int ww, int xx, int yy); diff --git a/CMessage.h b/CMessage.h index 3e022fbe9..f1bfb0960 100644 --- a/CMessage.h +++ b/CMessage.h @@ -1,6 +1,7 @@ #ifndef CMESSAGE_H #define CMESSAGE_H +#include "global.h" #include "SDL_TTF.h" #include "SDL.h" #include "CSemiDefHandler.h" @@ -8,7 +9,6 @@ #include "CGameInterface.h" #include "CGameInfo.h" #include "SDL_Extensions.h" -#define CGI (CGameInfo::mainObj) enum EWindowType {infoOnly, infoOK, yesOrNO}; class CPreGame; class MapSel; diff --git a/CObjectHandler.cpp b/CObjectHandler.cpp index cab2c43b3..0bf066245 100644 --- a/CObjectHandler.cpp +++ b/CObjectHandler.cpp @@ -3,8 +3,6 @@ #include "CGameInfo.h" #include "CGeneralTextHandler.h" -#define CGI (CGameInfo::mainObj) - void CObjectHandler::loadObjects() { int ID=0; diff --git a/CPathfinder.cpp b/CPathfinder.cpp index 6af933e84..8f5aef772 100644 --- a/CPathfinder.cpp +++ b/CPathfinder.cpp @@ -2,9 +2,7 @@ #include "global.h" #include "CPathfinder.h" -#define CGI (CGameInfo::mainObj) - -CPath * CPathfinder::getPath(int3 &src, int3 &dest) +CPath * CPathfinder::getPath(int3 &src, int3 &dest, CHeroInstance * hero) //TODO: test it (seems to be finished, but relies on unwritten functions :() { if(src.z!=dest.z) //first check return NULL; @@ -16,50 +14,153 @@ CPath * CPathfinder::getPath(int3 &src, int3 &dest) graph[i].resize(CGI->ac->map.height); for(int j=0; jaccesible = !CGI->mh->ttiles[i][j][src.z].blocked; + graph[i][j]->dist = -1; + graph[i][j]->theNodeBefore = NULL; + graph[i][j]->visited = false; + graph[i][j]->x = i; + graph[i][j]->y = j; } } - for(int h=0; hobjh->objInstances.size(); ++h) - { - if(CGI->objh->objInstances[h]->pos.z == src.z) - { - unsigned char blockMap[6]; - std::string ourName = CGI->ac->map.defy[CGI->objh->objInstances[h]->defNumber].name; - std::transform(ourName.begin(), ourName.end(), ourName.begin(), (int(*)(int))toupper); - for(int y=0; ydobjinfo->objs.size(); ++y) - { - std::string cName = CGI->dobjinfo->objs[y].defName; - std::transform(cName.begin(), cName.end(), cName.begin(), (int(*)(int))toupper); - if(cName==ourName) - { - for(int u=0; u<6; ++u) - { - blockMap[u] = CGI->dobjinfo->objs[y].blockMap[u]; - } - break; - } - } - for(int i=0; i<6; ++i) - { - for(int j=0; j<8; ++j) - { - int cPosX = CGI->objh->objInstances[h]->pos.x - j; - int cPosY = CGI->objh->objInstances[h]->pos.y - i; - if(cPosX>0 && cPosY>0) - { - graph[cPosX][cPosY].accesible = blockMap[i] & (128 >> j); - } - } - } - } - } //graph initialized + graph[src.x][src.y]->dist = 0; - return NULL; + std::queue mq; + mq.push(graph[src.x][src.y]); + while(!mq.empty()) + { + CPathNode * cp = mq.front(); + mq.pop(); + if(cp->x>0) + { + CPathNode * dp = graph[cp->x-1][cp->y]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + } + } + } + if(cp->y>0) + { + CPathNode * dp = graph[cp->x][cp->y-1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->x>0 && cp->y>0) + { + CPathNode * dp = graph[cp->x-1][cp->y-1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y-1, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->xx+1][cp->y]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->yx][cp->y+1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x, cp->y+1, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->xyx+1][cp->y+1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y+1, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->x>0 && cp->yx-1][cp->y+1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x-1, cp->y+1, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + if(cp->xy>0) + { + CPathNode * dp = graph[cp->x+1][cp->y-1]; + if(!dp->visited) + { + 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->dist = cp->dist + CGI->mh->getCost(int3(cp->x, cp->y, src.z), int3(cp->x+1, cp->y-1, src.z), hero); + dp->theNodeBefore = cp; + mq.push(dp); + dp->visited = true; + } + } + } + } + + CPathNode * curNode = graph[dest.x][dest.y]; + CPath * ret = new CPath; + + while(curNode!=graph[src.x][src.y] && curNode != NULL) + { + ret->nodes.push(*curNode); + curNode = curNode->theNodeBefore; + } + + for(int i=0; i > graph; + std::vector< std::vector > graph; public: - CPath * getPath(int3 & src, int3 & dest); //calculates path between src and dest; returns pointer to CPath or NULL if path does not exists - CPath * getPath(int3 & src, int3 & dest, 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(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(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 }; #endif //CPATHFINDER_H \ No newline at end of file diff --git a/CPreGame.cpp b/CPreGame.cpp index 83d201da3898ce467c0054f0878f8cd92ff5943d..eb7673999845b00b004d9da9efcb1390e255f49c 100644 GIT binary patch delta 18 ZcmaFV#y+o&eZv{1W&!5y0?dp-2LMKr28;jz delta 82 zcmbQ&#{Q^{eZv_hdu4_chE#?$hD?S$Ag#dQ%;3)838Xc^tVD)fpr|KME{!3d!3qd- Z84|&2{TY%NvKTa*k27sQ&cqmg000Uy5~~0J diff --git a/CTownHandler.cpp b/CTownHandler.cpp index 47dd65334..27b05e67a 100644 --- a/CTownHandler.cpp +++ b/CTownHandler.cpp @@ -2,8 +2,6 @@ #include "CTownHandler.h" #include "CGameInfo.h" #include -#define CGI (CGameInfo::mainObj) - CTownHandler::CTownHandler() { diff --git a/global.h b/global.h index b9bca9320..bb3ed6e6f 100644 --- a/global.h +++ b/global.h @@ -36,6 +36,8 @@ const int HEROES_QUANTITY=156; #define MARK_BLOCKED_POSITIONS false #define MARK_VISITABLE_POSITIONS false +#define CGI (CGameInfo::mainObj) + #define DEFBYPASS diff --git a/mapHandler.cpp b/mapHandler.cpp index 80ec0680a..f8a2dade3 100644 --- a/mapHandler.cpp +++ b/mapHandler.cpp @@ -824,3 +824,43 @@ char & CMapHandler::undVisAccess(int x, int y) { return undVisibility[x+Woff][y+Hoff]; } + +int CMapHandler::getCost(int3 &a, int3 &b, CHeroInstance *hero) +{ + int ret = 1500; //basic value + switch(hero->getLowestCreatureSpeed()) + { + case 0: case 1: case 2: case 3: case 4: + { + ret+=0; + break; + } + case 5: + { + ret+=100; + break; + } + case 6: case 7: + { + ret+=200; + break; + } + case 8: + { + ret+=300; + break; + } + case 9: case 10: + { + ret+=400; + break; + } + default: + { + ret+=500; + break; + } + } + + return ret; //TODO: finish it +} \ No newline at end of file diff --git a/mapHandler.h b/mapHandler.h index 03f9b29b6..ef805f555 100644 --- a/mapHandler.h +++ b/mapHandler.h @@ -55,6 +55,8 @@ public: char & undVisAccess(int x, int y); SDL_Surface mirrorImage(SDL_Surface *src); //what is this?? SDL_Surface * getVisBitmap(int x, int y, std::vector< std::vector > & visibility); + + int getCost(int3 & a, int3 & b, CHeroInstance * hero); void init(); }; diff --git a/stdafx.h b/stdafx.h index 48eaa759a..321f2cd16 100644 --- a/stdafx.h +++ b/stdafx.h @@ -12,4 +12,5 @@ #include #include #include +#include "global.h" // TODO: reference additional headers your program requires here