From: <ave...@us...> - 2003-12-31 13:45:43
|
Update of /cvsroot/gemrb/gemrb/gemrb/plugins/Core In directory sc8-pr-cvs1:/tmp/cvs-serv21228 Modified Files: Makefile.am Map.cpp Removed Files: PathFinder.cpp Log Message: Fix: mouse cursor is changed according to the (custom) Passable data, merged the source files for Map.cpp and PathFinder.cpp Index: Makefile.am =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Makefile.am,v retrieving revision 1.30 retrieving revision 1.31 diff -C2 -d -r1.30 -r1.31 *** Makefile.am 25 Dec 2003 13:10:04 -0000 1.30 --- Makefile.am 31 Dec 2003 13:45:37 -0000 1.31 *************** *** 3,6 **** libcore_la_LDFLAGS = -version-info 0:0:0 @LIBDL@ @LIBPTHREAD@ libcore_la_METASOURCES = AUTO ! libcore_la_SOURCES = ScriptedAnimation.cpp GameScript.cpp GlobalTimer.cpp PathFinder.cpp Polygon.cpp Game.cpp GameControl.cpp Variables.cpp SaveGameIterator.cpp DataFileMgr.cpp TableMgr.cpp MusicMgr.cpp SoundMgr.cpp Console.cpp ScriptEngine.cpp TextEdit.cpp EventMgr.cpp ScrollBar.cpp TextArea.cpp Slider.cpp Label.cpp WindowMgr.cpp Window.cpp VideoModes.cpp VideoMode.cpp Video.cpp TileSetMgr.cpp TileOverlay.cpp TileMapMgr.cpp TileMap.cpp Tile.cpp SymbolMgr.cpp StringMgr.cpp Sprite2D.cpp ResourceMgr.cpp Region.cpp PluginMgr.cpp Plugin.cpp MoviePlayer.cpp MemoryStream.cpp MapMgr.cpp Map.cpp InterfaceDesc.cpp Interface.cpp ImageMgr.cpp Font.cpp FileStream.cpp FactoryObject.cpp Factory.cpp DataStream.cpp Core.cpp Control.cpp Compressor.cpp Class_ID.cpp ClassDesc.cpp CharAnimations.cpp CachedFileStream.cpp Button.cpp ArchiveImporter.cpp AnimationMgr.cpp AnimationFactory.cpp Animation.cpp ActorMgr.cpp Actor.cpp ActorBlock.cpp SaveGameMgr.cpp DialogMgr.cpp Dialog.cpp EXTRA_DIST = *.cpp *.h --- 3,6 ---- libcore_la_LDFLAGS = -version-info 0:0:0 @LIBDL@ @LIBPTHREAD@ libcore_la_METASOURCES = AUTO ! libcore_la_SOURCES = ScriptedAnimation.cpp GameScript.cpp GlobalTimer.cpp Polygon.cpp Game.cpp GameControl.cpp Variables.cpp SaveGameIterator.cpp DataFileMgr.cpp TableMgr.cpp MusicMgr.cpp SoundMgr.cpp Console.cpp ScriptEngine.cpp TextEdit.cpp EventMgr.cpp ScrollBar.cpp TextArea.cpp Slider.cpp Label.cpp WindowMgr.cpp Window.cpp VideoModes.cpp VideoMode.cpp Video.cpp TileSetMgr.cpp TileOverlay.cpp TileMapMgr.cpp TileMap.cpp Tile.cpp SymbolMgr.cpp StringMgr.cpp Sprite2D.cpp ResourceMgr.cpp Region.cpp PluginMgr.cpp Plugin.cpp MoviePlayer.cpp MemoryStream.cpp MapMgr.cpp Map.cpp InterfaceDesc.cpp Interface.cpp ImageMgr.cpp Font.cpp FileStream.cpp FactoryObject.cpp Factory.cpp DataStream.cpp Core.cpp Control.cpp Compressor.cpp Class_ID.cpp ClassDesc.cpp CharAnimations.cpp CachedFileStream.cpp Button.cpp ArchiveImporter.cpp AnimationMgr.cpp AnimationFactory.cpp Animation.cpp ActorMgr.cpp Actor.cpp ActorBlock.cpp SaveGameMgr.cpp DialogMgr.cpp Dialog.cpp EXTRA_DIST = *.cpp *.h Index: Map.cpp =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Map.cpp,v retrieving revision 1.53 retrieving revision 1.54 diff -C2 -d -r1.53 -r1.54 *** Map.cpp 30 Dec 2003 21:50:35 -0000 1.53 --- Map.cpp 31 Dec 2003 13:45:37 -0000 1.54 *************** *** 23,26 **** --- 23,28 ---- #include "Map.h" #include "Interface.h" + #include "PathFinder.h" + //#include <stdlib.h> #ifndef WIN32 #include <sys/time.h> *************** *** 28,34 **** --- 30,66 ---- extern Interface * core; + static int NormalCost=10; + static int AdditionalCost=4; + static int Passable[16]={0,1,1,1,1,1,1,1,0,1,0,0,0,0,2,1}; + static bool PathFinderInited=false; #define STEP_TIME 150 + void InitPathFinder() + { + PathFinderInited=true; + int passabletable = core->LoadTable("pathfind"); + if(passabletable >= 0) { + TableMgr * tm = core->GetTable(passabletable); + if(tm) { + char *poi; + + for(int i=0;i<16;i++) { + poi=tm->QueryField(0,i); + if(*poi!='*') + Passable[i]=atoi(poi); + printf("PAssable: %d, %d\n",i,Passable[i]); + } + poi=tm->QueryField(1,0); + if(*poi!='*') + NormalCost=atoi(poi); + poi=tm->QueryField(1,1); + if(*poi!='*') + AdditionalCost=atoi(poi); + core->DelTable(passabletable); + } + } + } + Map::Map(void) { *************** *** 39,42 **** --- 71,76 ---- lastActorCount = 0; justCreated = true; + if(!PathFinderInited) + InitPathFinder(); } *************** *** 64,77 **** } - /*static Color green = {0x00, 0xff, 0x00, 0xff}; - static Color red = {0xff, 0x00, 0x00, 0xff}; - static Color yellow = {0xff, 0xff, 0x00, 0xff}; - static Color cyan = {0x00, 0xff, 0xff, 0xff}; - static Color green_dark = {0x00, 0x80, 0x00, 0xff}; - static Color red_dark = {0x80, 0x00, 0x00, 0xff}; - static Color yellow_dark = {0x80, 0x80, 0x00, 0xff}; - static Color cyan_dark = {0x00, 0x80, 0x80, 0xff}; - static Color magenta = {0xff, 0x00, 0xff, 0xff};*/ - void Map::DrawMap(Region viewport) { --- 98,101 ---- *************** *** 325,346 **** int Map::GetBlocked(int cx, int cy) { int block = SearchMap->GetPixelIndex(cx/16, cy/12); ! switch(block) { ! case 1: ! case 2: ! case 3: ! case 4: ! case 5: ! case 6: ! case 7: ! case 9: ! case 15: ! return 1; ! break; ! ! case 14: ! return 2; ! break; ! } ! return 0; } --- 349,353 ---- int Map::GetBlocked(int cx, int cy) { int block = SearchMap->GetPixelIndex(cx/16, cy/12); ! return Passable[block]; } *************** *** 430,432 **** --- 437,662 ---- } return NULL; + } + + PathFinder::PathFinder(void) + { + MapSet = NULL; + sMap = NULL; + if(!PathFinderInited) + InitPathFinder(); + } + + + PathFinder::~PathFinder(void) + { + if(MapSet) + delete [] MapSet; + } + + void PathFinder::SetMap(ImageMgr * sMap, unsigned int Width, unsigned int Height) + { + if(MapSet) + delete [] MapSet; + this->Width = Width; + this->Height = Height; + //Filling Matrices + MapSet = new unsigned short[Width*Height]; + this->sMap = sMap; + } + + void PathFinder::Leveldown(unsigned int px, unsigned int py, unsigned int &level, unsigned int &nx, unsigned int &ny, unsigned int &diff) + { + int pos; + unsigned int nlevel; + + if((px>=Width) || (py>=Height)) return; //walked off the map + pos=py*Width+px; + nlevel=MapSet[pos]; + if(!nlevel) return; //not even considered + if(level<=nlevel) return; + unsigned int ndiff=level-nlevel; + if(ndiff>diff) + { + level=nlevel; + diff=ndiff; + nx=px; + ny=py; + } + } + + void PathFinder::SetupNode(unsigned int x,unsigned int y, unsigned int Cost) + { + unsigned int pos; + + if((x>=Width) || (y>=Height)) + return; + pos=y*Width+x; + if(MapSet[pos]) + return; + if(!Passable[sMap->GetPixelIndex(x,y)]) + { + MapSet[pos]=65535; + return; + } + MapSet[pos] = Cost; + InternalStack.push((x<<16) |y); + } + + void PathFinder::AdjustPosition(unsigned int &goalX, unsigned int &goalY) + { + unsigned int maxr=Width; + if(maxr<Height) maxr=Height; + for(unsigned int radius=1; radius < maxr; radius++) { + unsigned int minx=0; + if(goalX>radius) minx=goalX-radius; + unsigned int maxx=goalX+radius+1; + if(maxx>Width) maxx=Width; + + for(unsigned int scanx=minx;scanx<maxx;scanx++) { + if(goalY>=radius) { + if(Passable[sMap->GetPixelIndex(scanx,goalY-radius)]) { + goalX=scanx; + goalY-=radius; + return; + } + } + if(goalY+radius<Height) { + if(Passable[sMap->GetPixelIndex(scanx,goalY+radius)]) { + goalX=scanx; + goalY+=radius; + return; + } + } + } + unsigned int miny=0; + if(goalY>radius) miny=goalY-radius; + unsigned int maxy=goalY+radius+1; + if(maxy>Height) maxy=Height; + for(unsigned int scany=miny;scany<maxy;scany++) { + if(goalX>=radius) { + if(Passable[sMap->GetPixelIndex(goalX-radius,scany)]) { + goalX-=radius; + goalY=scany; + return; + } + } + if(goalX+radius<Width) { + if(Passable[sMap->GetPixelIndex(goalX-radius,scany)]) { + goalX+=radius; + goalY=scany; + return; + } + } + } + } + } + + PathNode * PathFinder::FindPath(short sX, short sY, short dX, short dY) + { + unsigned int startX = sX/16, startY = sY/12, goalX = dX/16, goalY = dY/12; + memset(MapSet, 0, Width*Height*sizeof(unsigned short) ); + while(InternalStack.size()) + InternalStack.pop(); + + if(!Passable[sMap->GetPixelIndex(goalX,goalY)]) { + AdjustPosition(goalX,goalY); + } + unsigned int pos=(goalX<<16)|goalY; + unsigned int pos2=(startX<<16)|startY; + InternalStack.push(pos); + MapSet[goalY*Width+goalX] = 1; + + while(InternalStack.size()) { + pos = InternalStack.front(); + InternalStack.pop(); + unsigned int x = pos >> 16; + unsigned int y = pos & 0xffff; + + if(pos==pos2) {//We've found _a_ path + //printf("GOAL!!!\n"); + break; + } + unsigned int Cost = MapSet[y*Width+x]+NormalCost; + if(Cost>65500) { + //printf("Path not found!\n"); + break; + } + SetupNode(x-1, y-1, Cost); + SetupNode(x+1, y-1, Cost); + SetupNode(x+1, y+1, Cost); + SetupNode(x-1, y+1, Cost); + + Cost += AdditionalCost; + SetupNode(x, y-1, Cost); + SetupNode(x+1, y, Cost); + SetupNode(x, y+1, Cost); + SetupNode(x-1, y, Cost); + } + PathNode *StartNode = new PathNode; + PathNode *Return = StartNode; + StartNode->Next = NULL; + StartNode->Parent = NULL; + StartNode->x=startX; + StartNode->y=startY; + StartNode->orient=GetOrient(goalX, goalY, startX, startY); + if(pos!=pos2) + return Return; + unsigned int px = startX; + unsigned int py = startY; + pos2 = goalY * Width + goalX; + while( (pos=py*Width+px) !=pos2) { + StartNode->Next = new PathNode; + StartNode->Next->Parent = StartNode; + StartNode=StartNode->Next; + StartNode->Next=NULL; + unsigned int level = MapSet[pos]; + unsigned int diff = 0; + unsigned int nx, ny; + Leveldown(px,py+1,level,nx,ny, diff); + Leveldown(px+1,py,level,nx,ny, diff); + Leveldown(px-1,py,level,nx,ny, diff); + Leveldown(px,py-1,level,nx,ny, diff); + Leveldown(px-1,py+1,level,nx,ny, diff); + Leveldown(px+1,py+1,level,nx,ny, diff); + Leveldown(px+1,py-1,level,nx,ny, diff); + Leveldown(px-1,py-1,level,nx,ny, diff); + if(!diff) return Return; + StartNode->x=nx; + StartNode->y=ny; + StartNode->orient=GetOrient(nx,ny,px,py); + px=nx; + py=ny; + } + return Return; + } + + unsigned char PathFinder::GetOrient(short sX, short sY, short dX, short dY) + { + short deltaX = (dX-sX), deltaY = (dY-sY); + if(deltaX > 0) { + if(deltaY > 0) { + return 6; + } else if(deltaY == 0) { + return 4; + } else { + return 2; + } + } + else if(deltaX == 0) { + if(deltaY > 0) { + return 8; + } else { + return 0; + } + } + else { + if(deltaY > 0) { + return 10; + } else if(deltaY == 0) { + return 12; + } else { + return 14; + } + } + return 0; } --- PathFinder.cpp DELETED --- |