Nullkiller.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "../Goals/Invalid.h"
  8. extern boost::thread_specific_ptr<CCallback> cb;
  9. extern boost::thread_specific_ptr<VCAI> ai;
  10. Nullkiller::Nullkiller()
  11. {
  12. priorityEvaluator.reset(new PriorityEvaluator());
  13. }
  14. Goals::TSubgoal Nullkiller::choseBestTask(Goals::TGoalVec tasks)
  15. {
  16. Goals::TSubgoal bestTask = *vstd::maxElementByFun(tasks, [](Goals::TSubgoal goal) -> float
  17. {
  18. return goal->priority;
  19. });
  20. return bestTask;
  21. }
  22. Goals::TSubgoal Nullkiller::choseBestTask(Behavior & behavior)
  23. {
  24. auto tasks = behavior.getTasks();
  25. if(tasks.empty())
  26. {
  27. logAi->debug("Behavior %s found no tasks", behavior.toString());
  28. return Goals::sptr(Goals::Invalid());
  29. }
  30. for(auto task : tasks)
  31. {
  32. task->setpriority(priorityEvaluator->evaluate(task));
  33. }
  34. auto task = choseBestTask(tasks);
  35. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior.toString(), task->name(), task->tile.toString(), task->priority);
  36. return task;
  37. }
  38. void Nullkiller::makeTurn()
  39. {
  40. while(true)
  41. {
  42. ai->ah->updatePaths(ai->getMyHeroes());
  43. Goals::TGoalVec bestTasks = {
  44. choseBestTask(CaptureObjectsBehavior()),
  45. choseBestTask(RecruitHeroBehavior())
  46. };
  47. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  48. if(bestTask->invalid())
  49. {
  50. logAi->trace("No goals found. Ending turn.");
  51. return;
  52. }
  53. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  54. try
  55. {
  56. bestTask->accept(ai.get());
  57. }
  58. catch(goalFulfilledException &)
  59. {
  60. logAi->trace(bestTask->completeMessage());
  61. }
  62. catch(std::exception & e)
  63. {
  64. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  65. logAi->debug("The error message was: %s", e.what());
  66. return;
  67. }
  68. }
  69. }