2
0
Эх сурвалжийг харах

Nulkiller tracing player status method;
minor renaming

Mircea TheHonestCTO 4 сар өмнө
parent
commit
87417296aa

+ 2 - 2
AI/Nullkiller2/Engine/DeepDecomposer.cpp

@@ -37,7 +37,7 @@ void DeepDecomposer::reset()
 	goals.clear();
 }
 
-void DeepDecomposer::decompose(TGoalVec & result, TSubgoal behavior, int depthLimit)
+void DeepDecomposer::decompose(TGoalVec & results, TSubgoal behavior, int depthLimit)
 {
 	goals.clear();
 	goals.resize(depthLimit);
@@ -77,7 +77,7 @@ void DeepDecomposer::decompose(TGoalVec & result, TSubgoal behavior, int depthLi
 #endif
 				if(!isCompositionLoop(subgoal))
 				{
-					result.push_back(task);
+					results.push_back(task);
 
 					if(!fromCache)
 					{

+ 1 - 1
AI/Nullkiller2/Engine/DeepDecomposer.h

@@ -35,7 +35,7 @@ private:
 public:
 	DeepDecomposer(const Nullkiller * ai);
 	void reset();
-	void decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int depthLimit);
+	void decompose(Goals::TGoalVec & results, Goals::TSubgoal behavior, int depthLimit);
 
 private:
 	Goals::TSubgoal aggregateGoals(int startDepth, Goals::TSubgoal last);

+ 38 - 56
AI/Nullkiller2/Engine/Nullkiller.cpp

@@ -216,7 +216,7 @@ Goals::TTaskVec Nullkiller::buildPlan(TGoalVec & tasks, int priorityTier) const
 	return taskPlan.getTasks();
 }
 
-void Nullkiller::decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int decompositionMaxDepth) const
+void Nullkiller::decompose(Goals::TGoalVec & results, Goals::TSubgoal behavior, int decompositionMaxDepth) const
 {
 	makingTurnInterrupption.interruptionPoint();
 
@@ -224,7 +224,7 @@ void Nullkiller::decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, i
 
 	auto start = std::chrono::high_resolution_clock::now();
 	
-	decomposer->decompose(result, behavior, decompositionMaxDepth);
+	decomposer->decompose(results, behavior, decompositionMaxDepth);
 
 	makingTurnInterrupption.interruptionPoint();
 
@@ -374,39 +374,23 @@ void Nullkiller::makeTurn()
 	std::lock_guard<std::mutex> sharedStorageLock(AISharedStorage::locker);
 	const int MAX_DEPTH = 10;
 	resetState();
-	Goals::TGoalVec bestTasks;
-
-#if NKAI_TRACE_LEVEL >= 1
-	float totalHeroesStrength = 0;
-	int totalTownsLevel = 0;
-	for (const auto *heroInfo : cb->getHeroesInfo())
-	{
-		totalHeroesStrength += heroInfo->getTotalStrength();
-	}
-	for (const auto *townInfo : cb->getTownsInfo())
-	{
-		totalTownsLevel += townInfo->getTownLevel();
-	}
-	logAi->info("Beginning: totalHeroesStrength: %f, totalTownsLevel: %d, resources: %s", totalHeroesStrength, totalTownsLevel, cb->getResourceAmount().toString());
-#endif
+	Goals::TGoalVec tasks;
+	tracePlayerStatus(true);
 
 	for(int i = 1; i <= settings->getMaxPass() && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME; i++)
 	{
-		auto start = std::chrono::high_resolution_clock::now();
 		updateState();
-
 		Goals::TTask bestTask = taskptr(Goals::Invalid());
 
 		for(int j = 1; j <= settings->getMaxPriorityPass() && cb->getPlayerStatus(playerID) == EPlayerStatus::INGAME; j++)
 		{
-			bestTasks.clear();
+			tasks.clear();
 
-			decompose(bestTasks, sptr(RecruitHeroBehavior()), 1);
-			decompose(bestTasks, sptr(BuyArmyBehavior()), 1);
-			decompose(bestTasks, sptr(BuildingBehavior()), 1);
-
-			bestTask = choseBestTask(bestTasks);
+			decompose(tasks, sptr(RecruitHeroBehavior()), 1);
+			decompose(tasks, sptr(BuyArmyBehavior()), 1);
+			decompose(tasks, sptr(BuildingBehavior()), 1);
 
+			bestTask = choseBestTask(tasks);
 			if(bestTask->priority > 0)
 			{
 #if NKAI_TRACE_LEVEL >= 1
@@ -416,27 +400,23 @@ void Nullkiller::makeTurn()
 				if(!executeTask(bestTask))
 					return;
 
-				bool partialUpdate = true;
-
-				if (bestTask->getHero() != nullptr)
-					partialUpdate = false;
-
-				updateState(partialUpdate);
+				updateState(bestTask->getHero() == nullptr);
 			}
 			else
 			{
+				tasks.clear();
 				break;
 			}
 		}
 
-		decompose(bestTasks, sptr(CaptureObjectsBehavior()), 1);
-		decompose(bestTasks, sptr(ClusterBehavior()), MAX_DEPTH);
-		decompose(bestTasks, sptr(DefenceBehavior()), MAX_DEPTH);
-		decompose(bestTasks, sptr(GatherArmyBehavior()), MAX_DEPTH);
-		decompose(bestTasks, sptr(StayAtTownBehavior()), MAX_DEPTH);
+		decompose(tasks, sptr(CaptureObjectsBehavior()), 1);
+		decompose(tasks, sptr(ClusterBehavior()), MAX_DEPTH);
+		decompose(tasks, sptr(DefenceBehavior()), MAX_DEPTH);
+		decompose(tasks, sptr(GatherArmyBehavior()), MAX_DEPTH);
+		decompose(tasks, sptr(StayAtTownBehavior()), MAX_DEPTH);
 
 		if(!isOpenMap())
-			decompose(bestTasks, sptr(ExplorationBehavior()), MAX_DEPTH);
+			decompose(tasks, sptr(ExplorationBehavior()), MAX_DEPTH);
 
 		TTaskVec selectedTasks;
 
@@ -450,7 +430,7 @@ void Nullkiller::makeTurn()
 			prioOfTask = prio;
 #endif
 
-			selectedTasks = buildPlan(bestTasks, prio);
+			selectedTasks = buildPlan(tasks, prio);
 			if (!selectedTasks.empty() || settings->isUseFuzzy())
 				break;
 		}
@@ -460,8 +440,6 @@ void Nullkiller::makeTurn()
 			return a->priority > b->priority;
 		});
 
-		logAi->debug("Decision made in %ld", timeElapsed(start));
-
 		if(selectedTasks.empty())
 		{
 			selectedTasks.push_back(taskptr(Goals::Invalid()));
@@ -543,25 +521,10 @@ void Nullkiller::makeTurn()
 		}
 
 		hasAnySuccess |= handleTrading();
-
 		if(!hasAnySuccess)
 		{
 			logAi->trace("Nothing was done this turn. Ending turn.");
-
-#if NKAI_TRACE_LEVEL >= 1
-			totalHeroesStrength = 0;
-			totalTownsLevel = 0;
-			for (const auto *heroInfo : cb->getHeroesInfo())
-			{
-				totalHeroesStrength += heroInfo->getTotalStrength();
-			}
-			for (const auto *townInfo : cb->getTownsInfo())
-			{
-				totalTownsLevel += townInfo->getTownLevel();
-			}
-			logAi->info("End: totalHeroesStrength: %f, totalTownsLevel: %d, resources: %s", totalHeroesStrength, totalTownsLevel, cb->getResourceAmount().toString());
-#endif
-
+			tracePlayerStatus(false);
 			return;
 		}
 
@@ -748,4 +711,23 @@ void Nullkiller::invalidatePaths()
 	pathfinderCache->invalidatePaths();
 }
 
+void Nullkiller::tracePlayerStatus(bool beginning) const
+{
+#if NKAI_TRACE_LEVEL >= 1
+	float totalHeroesStrength = 0;
+	int totalTownsLevel = 0;
+	for (const auto *heroInfo : cb->getHeroesInfo())
+	{
+		totalHeroesStrength += heroInfo->getTotalStrength();
+	}
+	for (const auto *townInfo : cb->getTownsInfo())
+	{
+		totalTownsLevel += townInfo->getTownLevel();
+	}
+
+	const auto *firstWord = beginning ? "Beginning:" : "End:";
+	logAi->info("%s totalHeroesStrength: %f, totalTownsLevel: %d, resources: %s", firstWord, totalHeroesStrength, totalTownsLevel, cb->getResourceAmount().toString());
+#endif
+}
+
 }

+ 2 - 1
AI/Nullkiller2/Engine/Nullkiller.h

@@ -142,12 +142,13 @@ public:
 private:
 	void resetState();
 	void updateState(bool partialUpdate = false);
-	void decompose(Goals::TGoalVec & result, Goals::TSubgoal behavior, int decompositionMaxDepth) const;
+	void decompose(Goals::TGoalVec & results, Goals::TSubgoal behavior, int decompositionMaxDepth) const;
 	Goals::TTask choseBestTask(Goals::TGoalVec & tasks) const;
 	Goals::TTaskVec buildPlan(Goals::TGoalVec & tasks, int priorityTier) const;
 	bool executeTask(Goals::TTask task);
 	bool areAffectedObjectsPresent(Goals::TTask task) const;
 	HeroRole getTaskRole(Goals::TTask task) const;
+	void tracePlayerStatus(bool beginning) const;
 };
 
 }