ArmyFormation.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * ArmyFormation.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ArmyFormation.h"
  12. #include "../../../lib/mapObjects/CGTownInstance.h"
  13. namespace NKAI
  14. {
  15. void ArmyFormation::rearrangeArmyForSiege(const CGTownInstance * town, const CGHeroInstance * attacker)
  16. {
  17. auto freeSlots = attacker->getFreeSlotsQueue();
  18. while(!freeSlots.empty())
  19. {
  20. auto weakestCreature = vstd::minElementByFun(attacker->Slots(), [](const std::pair<SlotID, CStackInstance *> & slot) -> int
  21. {
  22. return slot.second->getCount() == 1
  23. ? std::numeric_limits<int>::max()
  24. : slot.second->getCreatureID().toCreature()->getAIValue();
  25. });
  26. if(weakestCreature == attacker->Slots().end() || weakestCreature->second->getCount() == 1)
  27. {
  28. break;
  29. }
  30. cb->splitStack(attacker, attacker, weakestCreature->first, freeSlots.front(), 1);
  31. freeSlots.pop();
  32. }
  33. if(town->fortLevel() > CGTownInstance::FORT)
  34. {
  35. std::vector<CStackInstance *> stacks;
  36. for(auto slot : attacker->Slots())
  37. stacks.push_back(slot.second);
  38. boost::sort(
  39. stacks,
  40. [](CStackInstance * slot1, CStackInstance * slot2) -> bool
  41. {
  42. auto cre1 = slot1->getCreatureID().toCreature();
  43. auto cre2 = slot2->getCreatureID().toCreature();
  44. auto flying = cre1->hasBonusOfType(BonusType::FLYING) - cre2->hasBonusOfType(BonusType::FLYING);
  45. if(flying != 0) return flying < 0;
  46. else return cre1->getAIValue() < cre2->getAIValue();
  47. });
  48. for(int i = 0; i < stacks.size(); i++)
  49. {
  50. auto pos = vstd::findKey(attacker->Slots(), stacks[i]);
  51. if(pos.getNum() != i)
  52. cb->swapCreatures(attacker, attacker, static_cast<SlotID>(i), pos);
  53. }
  54. }
  55. }
  56. }