瀏覽代碼

- some effords to get rid of bottlenecks in AI code
- fixes, probably partially #1577
- enabled code for reading map object templates from json, still not
used
- disabled PCH for launcher due to speed issues.

Ivan Savenko 11 年之前
父節點
當前提交
771c1ce255

+ 11 - 4
AI/VCAI/AIUtility.cpp

@@ -124,18 +124,25 @@ const CGHeroInstance * HeroPtr::operator*() const
 
 void foreach_tile_pos(std::function<void(const int3& pos)> foo)
 {
-	for(int i = 0; i < cb->getMapSize().x; i++)
-		for(int j = 0; j < cb->getMapSize().y; j++)
-			for(int k = 0; k < cb->getMapSize().z; k++)
+	// some micro-optimizations since this function gets called a LOT
+	// callback pointer is thread-specific and slow to retrieve -> read map size only once
+	int3 mapSize = cb->getMapSize();
+
+	for(int i = 0; i < mapSize.x; i++)
+		for(int j = 0; j < mapSize.y; j++)
+			for(int k = 0; k < mapSize.z; k++)
 				foo(int3(i,j,k));
+
 }
 
 void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo)
 {
+	CCallback * cbp = cb.get(); // avoid costly retrieval of thread-specific pointer
+
 	for(const int3 &dir : dirs)
 	{
 		const int3 n = pos + dir;
-		if(cb->isInTheMap(n))
+		if(cbp->isInTheMap(n))
 			foo(pos+dir);
 	}
 }

+ 5 - 4
AI/VCAI/VCAI.cpp

@@ -978,7 +978,6 @@ bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsi
 		return false;
 
 	TResources currentRes = cb->getResourceAmount();
-	TResources income = estimateIncome();
 	//TODO: calculate if we have enough resources to build it in maxDays
 
 	for(const auto & buildID : toBuild)
@@ -998,10 +997,12 @@ bool VCAI::tryBuildStructure(const CGTownInstance * t, BuildingID building, unsi
 		}
 		else if(canBuild == EBuildingState::NO_RESOURCES)
 		{
+			//TResources income = estimateIncome();
 			TResources cost = t->town->buildings.at(buildID)->resources;
 			for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
 			{
-				int diff = currentRes[i] - cost[i] + income[i];
+				//int diff = currentRes[i] - cost[i] + income[i];
+				int diff = currentRes[i] - cost[i];
 				if(diff < 0)
 					saving[i] = 1;
 			}
@@ -1143,13 +1144,13 @@ void VCAI::buildStructure(const CGTownInstance * t)
 	//Possible - allow "locking" on specific building (build prerequisites and then building itself)
 
 	TResources currentRes = cb->getResourceAmount();
-	TResources income = estimateIncome();
+	int townIncome = t->dailyIncome();
 
 	if (tryBuildAnyStructure(t, std::vector<BuildingID>(essential, essential + ARRAY_COUNT(essential))))
 		return;
 
 	//we're running out of gold - try to build something gold-producing. Multiplier can be tweaked, 6 is minimum due to buildings costs
-	if (currentRes[Res::GOLD] < income[Res::GOLD] * 6)
+	if (currentRes[Res::GOLD] < townIncome * 6)
 		if (tryBuildNextStructure(t, std::vector<BuildingID>(goldSource, goldSource + ARRAY_COUNT(goldSource))))
 			return;
 

+ 1 - 1
client/battle/CBattleAnimations.cpp

@@ -310,7 +310,7 @@ bool CMeleeAttackAnimation::init()
 		return false;
 	}
 
-	bool toReverse = owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStackPosBeforeReturn, dest, owner->creDir[stack->ID], attackedStack->doubleWide(), owner->creDir[attackedStack->ID]);
+	bool toReverse = owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStackPosBeforeReturn, attackedStack->position, owner->creDir[stack->ID], attackedStack->doubleWide(), owner->creDir[attackedStack->ID]);
 
 	if (toReverse)
 	{

+ 3 - 2
launcher/CMakeLists.txt

@@ -56,8 +56,9 @@ add_executable(vcmilauncher ${launcher_SRCS} ${launcher_UI_HEADERS})
 # The Qt5Widgets_LIBRARIES variable also includes QtGui and QtCore
 target_link_libraries(vcmilauncher vcmi ${Qt5Widgets_LIBRARIES} ${Qt5Network_LIBRARIES})
 
-set_target_properties(vcmilauncher PROPERTIES ${PCH_PROPERTIES})
-cotire(vcmilauncher)
+# temporary(?) disabled - generation of PCH takes too much time since cotire is trying to collect all Qt headers
+#set_target_properties(vcmilauncher PROPERTIES ${PCH_PROPERTIES})
+#cotire(vcmilauncher)
 
 if (NOT APPLE) # Already inside bundle
     install(TARGETS vcmilauncher DESTINATION ${BIN_DIR})

+ 3 - 1
launcher/modManager/cmodlistview_moc.cpp

@@ -581,7 +581,9 @@ void CModListView::installMods(QStringList archives)
 	for (int i=0; i<modNames.size(); i++)
 		manager->installMod(modNames[i], archives[i]);
 
-	std::function<void(QString)> enableMod = [&](QString modName)
+	std::function<void(QString)> enableMod;
+
+	enableMod = [&](QString modName)
 	{
 		auto mod = modModel->getMod(modName);
 		if (mod.isInstalled() && !mod.getValue("keepDisabled").toBool())

+ 8 - 2
lib/CBattleCallback.cpp

@@ -1455,8 +1455,14 @@ bool CBattleInfoCallback::isToReverse (BattleHex hexFrom, BattleHex hexTo, bool
 
 	if (toDoubleWide)
 	{
-		return (isToReverseHlp (hexFrom, hexTo, curDir)) &&
-			(toDir ? isToReverseHlp (hexFrom, hexTo-1, curDir) : isToReverseHlp (hexFrom, hexTo+1, curDir));
+		if (isToReverseHlp (hexFrom, hexTo, curDir))
+		{
+			if (toDir)
+				return isToReverseHlp (hexFrom, hexTo-1, curDir);
+			else
+				return isToReverseHlp (hexFrom, hexTo+1, curDir);
+		}
+		return false;
 	}
 	else
 	{

+ 1 - 2
lib/CDefObjInfoHandler.cpp

@@ -344,7 +344,7 @@ CDefObjInfoHandler::CDefObjInfoHandler()
 {
 	readTextFile("Data/Objects.txt");
 	readTextFile("Data/Heroes.txt");
-/*
+
 	const JsonNode node = JsonUtils::assembleFromFiles("config/objectTemplates.json");
 	std::vector<ObjectTemplate> newTemplates;
 	newTemplates.reserve(node.Struct().size());
@@ -363,7 +363,6 @@ CDefObjInfoHandler::CDefObjInfoHandler()
 
 	// merge new templates into storage
 	objects.insert(objects.end(), newTemplates.begin(), newTemplates.end());
-*/
 }
 
 void CDefObjInfoHandler::eraseAll(Obj type, si32 subtype)