Browse Source

Further refactoring: merged terrain and undergroundTerrain in Mapa.

Michał W. Urbańczyk 17 năm trước cách đây
mục cha
commit
add34b26ed
5 tập tin đã thay đổi với 47 bổ sung67 xóa
  1. 1 1
      CPathfinder.cpp
  2. 23 25
      map.cpp
  3. 1 2
      map.h
  4. 20 37
      mapHandler.cpp
  5. 2 2
      server/CGameHandler.cpp

+ 1 - 1
CPathfinder.cpp

@@ -213,7 +213,7 @@ void CPathfinder::CalcH(Coordinate* node)
 		y = CGI->mh->map->height-1;
 
 	//Get the movement cost.
-	ret = Hero->getTileCost(CGI->mh->ttiles[x][y][node->z].tileInfo->tertype, CGI->mh->map->terrain[x][y].malle,CGI->mh->map->terrain[x][y].nuine);
+	ret = Hero->getTileCost(CGI->mh->ttiles[x][y][node->z].tileInfo->tertype, CGI->mh->map->terrain[x][y][0].malle,CGI->mh->map->terrain[x][y][0].nuine);
 	
 	node->h = ret;
 }

+ 23 - 25
map.cpp

@@ -451,14 +451,12 @@ void Mapa::initFromBytes(unsigned char * bufor)
 	areAnyPLayers = readChar(bufor,i); //invalid on some maps
 	height = width = (readNormalNr(bufor,i)); i+=4; // wymiary mapy
 	twoLevel = readChar(bufor,i); //czy sa lochy
-	terrain = new TerrainTile*[width]; // allocate memory 
+	terrain = new TerrainTile**[width]; // allocate memory 
 	for (int ii=0;ii<width;ii++)
-		terrain[ii] = new TerrainTile[height]; // allocate memory 
-	if (twoLevel)
 	{
-		undergroungTerrain = new TerrainTile*[width]; // allocate memory 
-		for (int ii=0;ii<width;ii++)
-			undergroungTerrain[ii] = new TerrainTile[height]; // allocate memory 
+		terrain[ii] = new TerrainTile*[height]; // allocate memory 
+		for(int jj=0;jj<=height;jj++)
+			terrain[ii][jj] = new TerrainTile[twoLevel+1];
 	}
 	int pom;
 	name = readString(bufor,i);
@@ -919,15 +917,15 @@ void Mapa::initFromBytes(unsigned char * bufor)
 	{
 		for (int z=0; z<height; z++)
 		{
-			terrain[z][c].tertype = (EterrainType)(bufor[i++]);
-			terrain[z][c].terview = bufor[i++];
-			terrain[z][c].nuine = (Eriver)bufor[i++];
-			terrain[z][c].rivDir = bufor[i++];
-			terrain[z][c].malle = (Eroad)bufor[i++];
-			terrain[z][c].roadDir = bufor[i++];
-			terrain[z][c].siodmyTajemniczyBajt = bufor[i++];
-			terrain[z][c].blocked = 0;
-			terrain[z][c].visitable = 0;
+			terrain[z][c][0].tertype = (EterrainType)(bufor[i++]);
+			terrain[z][c][0].terview = bufor[i++];
+			terrain[z][c][0].nuine = (Eriver)bufor[i++];
+			terrain[z][c][0].rivDir = bufor[i++];
+			terrain[z][c][0].malle = (Eroad)bufor[i++];
+			terrain[z][c][0].roadDir = bufor[i++];
+			terrain[z][c][0].siodmyTajemniczyBajt = bufor[i++];
+			terrain[z][c][0].blocked = 0;
+			terrain[z][c][0].visitable = 0;
 		}
 	}
 	if (twoLevel) // read underground terrain
@@ -936,15 +934,15 @@ void Mapa::initFromBytes(unsigned char * bufor)
 		{
 			for (int z=0; z<height; z++)
 			{
-				undergroungTerrain[z][c].tertype = (EterrainType)(bufor[i++]);
-				undergroungTerrain[z][c].terview = bufor[i++];
-				undergroungTerrain[z][c].nuine = (Eriver)bufor[i++];
-				undergroungTerrain[z][c].rivDir = bufor[i++];
-				undergroungTerrain[z][c].malle = (Eroad)bufor[i++];
-				undergroungTerrain[z][c].roadDir = bufor[i++];
-				undergroungTerrain[z][c].siodmyTajemniczyBajt = bufor[i++];
-				undergroungTerrain[z][c].blocked = 0;
-				undergroungTerrain[z][c].visitable = 0;
+				terrain[z][c][1].tertype = (EterrainType)(bufor[i++]);
+				terrain[z][c][1].terview = bufor[i++];
+				terrain[z][c][1].nuine = (Eriver)bufor[i++];
+				terrain[z][c][1].rivDir = bufor[i++];
+				terrain[z][c][1].malle = (Eroad)bufor[i++];
+				terrain[z][c][1].roadDir = bufor[i++];
+				terrain[z][c][1].siodmyTajemniczyBajt = bufor[i++];
+				terrain[z][c][1].blocked = 0;
+				terrain[z][c][1].visitable = 0;
 			}
 		}
 	}
@@ -2309,7 +2307,7 @@ borderguardend:
 				int zVal = objects[f]->pos.z;
 				if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
 				{
-					TerrainTile & curt = (zVal) ? (undergroungTerrain[xVal][yVal]) : (terrain[xVal][yVal]);
+					TerrainTile & curt = terrain[xVal][yVal][zVal];
 					if(((objects[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
 					{
 						curt.visitableObjects.push_back(objects[f]);

+ 1 - 2
map.h

@@ -462,8 +462,7 @@ struct DLL_EXPORT Mapa
 	std::string name;  //name of map
 	std::string description;  //and description
 	int height, width; 
-	TerrainTile** terrain; 
-	TerrainTile** undergroungTerrain; // used only if there is underground level
+	TerrainTile*** terrain; 
 	std::vector<Rumor> rumors;
 	std::vector<DisposedHero> disposedHeroes;
 	std::vector<CGHeroInstance*> predefinedHeroes;

+ 20 - 37
mapHandler.cpp

@@ -229,7 +229,7 @@ void CMapHandler::roadsRiverTerrainInit()
 			{
 				TerrainTile2 &pom(ttiles[i][j][k]);
 				pom.pos = int3(i, j, k);
-				pom.tileInfo = &( k  ?  map->undergroungTerrain[i][j]  :  map->terrain[i][j] );
+				pom.tileInfo = &(map->terrain[i][j][k]);
 				if(pom.tileInfo->malle)
 				{
 					int cDir;
@@ -267,33 +267,16 @@ void CMapHandler::roadsRiverTerrainInit()
 		{
 			for(int k=0; k<=map->twoLevel; ++k)
 			{
-				TerrainTile** pomm = map->terrain;
-				if(k==0)
-				{
-					pomm = map->terrain;
-				}
-				else
-				{
-					pomm = map->undergroungTerrain;
-				}
-				if(pomm[i][j].nuine)
+				if(map->terrain[i][j][k].nuine)
 				{
 					int cDir;
 					bool rotH, rotV;
-					if(k==0)
-					{
-						ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->terrain[i][j].nuine-1]->ourImages[map->terrain[i][j].rivDir].bitmap);
-						cDir = map->terrain[i][j].rivDir;
-						rotH = (map->terrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
-						rotV = (map->terrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
-					}
-					else
-					{
-						ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->undergroungTerrain[i][j].nuine-1]->ourImages[map->undergroungTerrain[i][j].rivDir].bitmap);
-						cDir = map->undergroungTerrain[i][j].rivDir;
-						rotH = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
-						rotV = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
-					}
+
+					ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->terrain[i][j][k].nuine-1]->ourImages[map->terrain[i][j][k].rivDir].bitmap);
+					cDir = map->terrain[i][j][k].rivDir;
+					rotH = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 3) & 1;
+					rotV = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 2) & 1;
+
 					if(rotH)
 					{
 						ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].rivbitmap[0]);
@@ -372,9 +355,9 @@ void CMapHandler::borderAndTerrainBitmapInit()
 				//TerrainTile zz = map->terrain[i-Woff][j-Hoff];
 				std::string name;
 				if (k>0)
-					name = nameFromType(map->undergroungTerrain[i][j].tertype);
+					name = nameFromType(map->terrain[i][j][1].tertype);
 				else
-					name = nameFromType(map->terrain[i][j].tertype);
+					name = nameFromType(map->terrain[i][j][0].tertype);
 				for (unsigned int m=0; m<defs.size(); m++)
 				{
 					try
@@ -385,15 +368,15 @@ void CMapHandler::borderAndTerrainBitmapInit()
 						{
 							int ktora;
 							if (k==0)
-								ktora = map->terrain[i][j].terview;
+								ktora = map->terrain[i][j][0].terview;
 							else
-								ktora = map->undergroungTerrain[i][j].terview;
+								ktora = map->terrain[i][j][1].terview;
 							ttiles[i][j][k].terbitmap.push_back(defs[m]->ourImages[ktora].bitmap);
 							int zz;
 							if (k==0)
-								zz = (map->terrain[i][j].siodmyTajemniczyBajt)%4;
+								zz = (map->terrain[i][j][0].siodmyTajemniczyBajt)%4;
 							else
-								zz = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt)%4;
+								zz = (map->terrain[i][j][1].siodmyTajemniczyBajt)%4;
 							switch (zz)
 							{
 							case 1:
@@ -1380,16 +1363,16 @@ void CMapHandler::loadDefs()
 	{
 		for (int j=0; j<map->width; j++)
 		{
-			if (loadedTypes.find(map->terrain[i][j].tertype)==loadedTypes.end())
+			if (loadedTypes.find(map->terrain[i][j][0].tertype)==loadedTypes.end())
 			{
-				CDefHandler  *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j].tertype).c_str());
-				loadedTypes.insert(map->terrain[i][j].tertype);
+				CDefHandler  *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j][0].tertype).c_str());
+				loadedTypes.insert(map->terrain[i][j][0].tertype);
 				defs.push_back(sdh);
 			}
-			if (map->twoLevel && loadedTypes.find(map->undergroungTerrain[i][j].tertype)==loadedTypes.end())
+			if (map->twoLevel && loadedTypes.find(map->terrain[i][j][1].tertype)==loadedTypes.end())
 			{
-				CDefHandler  *sdh = CDefHandler::giveDef(nameFromType(map->undergroungTerrain[i][j].tertype).c_str());
-				loadedTypes.insert(map->undergroungTerrain[i][j].tertype);
+				CDefHandler  *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j][1].tertype).c_str());
+				loadedTypes.insert(map->terrain[i][j][1].tertype);
 				defs.push_back(sdh);
 			}
 		}

+ 2 - 2
server/CGameHandler.cpp

@@ -49,7 +49,7 @@ void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
 					si32 id;
 					c >> id >> start >> end;
 					int3 hmpos = end + int3(-1,0,0);
-					TerrainTile t = (hmpos.z) ? (gs->map->undergroungTerrain[hmpos.x][hmpos.y]) : (gs->map->terrain[hmpos.x][hmpos.y]);
+					TerrainTile t = gs->map->terrain[hmpos.x][hmpos.y][hmpos.z];
 					CGHeroInstance *h = static_cast<CGHeroInstance *>(gs->map->objects[id]);
 					int cost = (double)h->getTileCost(t.tertype,t.malle,t.nuine) * distance(start,end);
 
@@ -103,7 +103,7 @@ void CGameHandler::handleConnection(std::set<int> players, CConnection &c)
 					{
 						tmh.result = 1;
 
-						BOOST_FOREACH(CGObjectInstance *obj, ((start.z) ? (gs->map->undergroungTerrain[start.x][start.y]) : (gs->map->terrain[start.x][start.y])).visitableObjects)
+						BOOST_FOREACH(CGObjectInstance *obj, gs->map->terrain[start.x][start.y][start.z].visitableObjects)
 						{
 							//TODO: allow to handle this in script-languages
 							if(obj->state) //hard-coded function