Nullkiller.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. logAi->debug("Checking behavior %s", behavior.toString());
  25. auto tasks = behavior.getTasks();
  26. if(tasks.empty())
  27. {
  28. logAi->debug("Behavior %s found no tasks", behavior.toString());
  29. return Goals::sptr(Goals::Invalid());
  30. }
  31. for(auto task : tasks)
  32. {
  33. task->setpriority(priorityEvaluator->evaluate(task));
  34. }
  35. auto task = choseBestTask(tasks);
  36. logAi->debug("Behavior %s returns %s(%s), priority %f", behavior.toString(), task->name(), task->tile.toString(), task->priority);
  37. return task;
  38. }
  39. void Nullkiller::makeTurn()
  40. {
  41. while(true)
  42. {
  43. ai->ah->updatePaths(ai->getMyHeroes(), true);
  44. Goals::TGoalVec bestTasks = {
  45. choseBestTask(CaptureObjectsBehavior()),
  46. choseBestTask(RecruitHeroBehavior())
  47. };
  48. Goals::TSubgoal bestTask = choseBestTask(bestTasks);
  49. if(bestTask->invalid())
  50. {
  51. logAi->trace("No goals found. Ending turn.");
  52. return;
  53. }
  54. logAi->debug("Trying to realize %s (value %2.3f)", bestTask->name(), bestTask->priority);
  55. try
  56. {
  57. activeHero = bestTask->hero;
  58. bestTask->accept(ai.get());
  59. }
  60. catch(goalFulfilledException &)
  61. {
  62. logAi->trace(bestTask->completeMessage());
  63. }
  64. catch(std::exception & e)
  65. {
  66. logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->name());
  67. logAi->debug("The error message was: %s", e.what());
  68. return;
  69. }
  70. }
  71. }