Nullkiller.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "StdInc.h"
  2. #include "Nullkiller.h"
  3. #include "../VCAI.h"
  4. #include "../AIHelper.h"
  5. #include "../Behaviors/CaptureObjectsBehavior.h"
  6. #include "../Behaviors/RecruitHeroBehavior.h"
  7. #include "../Behaviors/BuyArmyBehavior.h"
  8. #include "../Goals/Invalid.h"
  9. extern boost::thread_specific_ptr<CCallback> cb;
  10. extern boost::thread_specific_ptr<VCAI> ai;
  11. Nullkiller::Nullkiller()
  12. {
  13. priorityEvaluator.reset(new PriorityEvaluator());
  14. dangerHitMap.reset(new DangerHitMapAnalyzer());
  15. }
  16. Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec tasks)
  17. {
  18. Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float{
  19. return goal->priority;
  20. });
  21. return bestTask;
  22. }
  23. Goals::TSubgoal Nullkiller::choseBestTask(Behavior & behavior)
  24. {
  25. logAi->debug("Checking behavior %s", behavior.toString());
  26. auto tasks = behavior.getTasks();
  27. if(tasks.empty())
  28. {
  29. logAi->debug("Behavior %s found no tasks", behavior.toString());
  30. return Goals::sptr(Goals::Invalid());
  31. }
  32. for(auto task : tasks)
  33. {
  34. task->setpriority(priorityEvaluator->evaluate(task));
  35. }
  36. auto task = choseBestTask(tasks);
  37. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior.toString(), task->name(), task->tile.toString(), task->priority);
  38. return task;
  39. }
  40. void Nullkiller::resetAiState()
  41. {
  42. lockedHeroes.clear();
  43. dangerHitMap->updateHitMap();
  44. }
  45. void Nullkiller::updateAiState()
  46. {
  47. auto activeHeroes = ai->getMyHeroes();
  48. vstd::erase_if(activeHeroes, [&](const HeroPtr & hero) -> bool{
  49. return vstd::contains(lockedHeroes, hero);
  50. });
  51. ai->ah->updatePaths(activeHeroes, true);
  52. }
  53. void Nullkiller::makeTurn()
  54. {
  55. resetAiState();
  56. while(true)
  57. {
  58. updateAiState();
  59. Goals::TGoalVec bestTasks = {
  60. choseBestTask(BuyArmyBehavior()),
  61. choseBestTask(CaptureObjectsBehavior()),
  62. choseBestTask(RecruitHeroBehavior())
  63. };
  64. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  65. if(bestTask->invalid())
  66. {
  67. logAi->trace("No goals found. Ending turn.");
  68. return;
  69. }
  70. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  71. try
  72. {
  73. activeHero = bestTask->hero;
  74. bestTask->accept(ai.get());
  75. }
  76. catch(goalFulfilledException &)
  77. {
  78. logAi->trace(bestTask->completeMessage());
  79. }
  80. catch(std::exception & e)
  81. {
  82. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  83. logAi->debug("The error message was: %s", e.what());
  84. return;
  85. }
  86. }
  87. }