Nullkiller.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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) const
  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(std::shared_ptr<Behavior> behavior) const
  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. logAi->trace("Evaluating priorities, tasks count %d", tasks.size());
  33. for(auto task : tasks)
  34. {
  35. task->setpriority(priorityEvaluator->evaluate(task));
  36. }
  37. auto task = choseBestTask(tasks);
  38. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior->toString(), task->name(), task->tile.toString(), task->priority);
  39. return task;
  40. }
  41. void Nullkiller::resetAiState()
  42. {
  43. lockedHeroes.clear();
  44. dangerHitMap->updateHitMap();
  45. }
  46. void Nullkiller::updateAiState()
  47. {
  48. // TODO: move to hero manager
  49. auto activeHeroes = ai->getMyHeroes();
  50. vstd::erase_if(activeHeroes, [&](const HeroPtr & hero) -> bool{
  51. return vstd::contains(lockedHeroes, hero);
  52. });
  53. ai->ah->updatePaths(activeHeroes, true);
  54. ai->ah->updateHeroRoles();
  55. }
  56. void Nullkiller::makeTurn()
  57. {
  58. resetAiState();
  59. while(true)
  60. {
  61. updateAiState();
  62. Goals::TGoalVec bestTasks = {
  63. choseBestTask(std::make_shared<BuyArmyBehavior>()),
  64. choseBestTask(std::make_shared<CaptureObjectsBehavior>()),
  65. choseBestTask(std::make_shared<RecruitHeroBehavior>())
  66. };
  67. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  68. if(bestTask->invalid())
  69. {
  70. logAi->trace("No goals found. Ending turn.");
  71. return;
  72. }
  73. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  74. try
  75. {
  76. activeHero = bestTask->hero;
  77. bestTask->accept(ai.get());
  78. }
  79. catch(goalFulfilledException &)
  80. {
  81. logAi->trace(bestTask->completeMessage());
  82. }
  83. catch(std::exception & e)
  84. {
  85. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  86. logAi->debug("The error message was: %s", e.what());
  87. return;
  88. }
  89. }
  90. }