Browse Source

Workaround for hero & town issue. Game still crashes elsewhere, though.

DjWarmonger 11 years ago
parent
commit
bae9f2083f
4 changed files with 24 additions and 8 deletions
  1. 7 0
      lib/CGameState.cpp
  2. 8 1
      lib/mapObjects/CGTownInstance.cpp
  3. 8 6
      lib/rmg/CMapGenerator.cpp
  4. 1 1
      lib/rmg/CMapGenerator.h

+ 7 - 0
lib/CGameState.cpp

@@ -1899,6 +1899,13 @@ void CGameState::initVisitingAndGarrisonedHeroes()
 			}
 		}
 	}
+	for (auto hero : map->heroesOnMap)
+	{
+		if (hero->visitedTown)
+		{
+			assert (hero->visitedTown->visitingHero == hero);
+		}
+	}
 }
 
 BFieldType CGameState::battleGetBattlefieldType(int3 tile)

+ 8 - 1
lib/mapObjects/CGTownInstance.cpp

@@ -523,7 +523,14 @@ void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
 
 void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
 {
-	cb->stopHeroVisitCastle(this, h);
+	//FIXME: find out why this issue appears on random maps
+	if (visitingHero == h)
+	{
+		cb->stopHeroVisitCastle(this, h);
+		logGlobal->warnStream() << h->name << " correctly left town " << name;
+	}
+	else
+		logGlobal->warnStream() << "Warning, " << h->name << " tries to leave the town " << name << " but hero is not inside.";
 }
 
 std::string CGTownInstance::getObjectName() const

+ 8 - 6
lib/rmg/CMapGenerator.cpp

@@ -23,8 +23,8 @@ void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(int3&
 }
 
 
-CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
-	mapGenOptions(mapGenOptions), randomSeed(randomSeed), monolithIndex(0)
+CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int RandomSeed /*= std::time(nullptr)*/) :
+	mapGenOptions(mapGenOptions), randomSeed(RandomSeed), monolithIndex(0)
 {
 	rand.setSeed(randomSeed);
 }
@@ -50,12 +50,13 @@ void CMapGenerator::initTiles()
 
 CMapGenerator::~CMapGenerator()
 {
-	//FIXME: what if map is not present anymore?
-	if (tiles && map)
+	if (tiles)
 	{
-		for (int i=0; i < map->width; i++)
+		int width = mapGenOptions->getWidth();
+		int height = mapGenOptions->getHeight();
+		for (int i=0; i < width; i++)
 		{
-			for(int j=0; j < map->height; j++)
+			for(int j=0; j < height; j++)
 			{
 				delete [] tiles[i][j];
 			}
@@ -80,6 +81,7 @@ std::unique_ptr<CMap> CMapGenerator::generate()
 		genZones();
 		map->calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
 		fillZones();
+		//updated fuarded tiles will be calculated in CGameState::initMapObjects()
 	}
 	catch (rmgException &e)
 	{

+ 1 - 1
lib/rmg/CMapGenerator.h

@@ -51,7 +51,7 @@ public:
 class DLL_LINKAGE CMapGenerator
 {
 public:
-	explicit CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed = std::time(nullptr));
+	explicit CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int RandomSeed = std::time(nullptr));
 	~CMapGenerator(); // required due to unique_ptr
 
 	std::unique_ptr<CMap> generate();