BattleHelper.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "BattleHelper.h"
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <boost/algorithm/string/split.hpp>
  6. #include <boost/algorithm/string/classification.hpp>
  7. #include <boost/algorithm/string.hpp>
  8. using namespace GeniusAI::BattleAI;
  9. using namespace std;
  10. CBattleHelper::CBattleHelper():
  11. InfiniteDistance(0xffff),
  12. BattlefieldWidth(15),
  13. BattlefieldHeight(11),
  14. m_voteForMaxDamage(10),
  15. m_voteForMinDamage(10),
  16. m_voteForMaxSpeed(10),
  17. m_voteForDistance(10),
  18. m_voteForDistanceFromShooters(20),
  19. m_voteForHitPoints(10)
  20. {
  21. // loads votes
  22. std::fstream f;
  23. f.open("AI\\CBattleHelper.txt", std::ios::in);
  24. if (f)
  25. {
  26. //char c_line[512];
  27. std::string line;
  28. while (std::getline(f, line, '\n'))
  29. {
  30. //f.getline(c_line, sizeof(c_line), '\n');
  31. //std::string line(c_line);
  32. //std::getline(f, line);
  33. std::vector<std::string> parts;
  34. boost::algorithm::split(parts, line, boost::algorithm::is_any_of("="));
  35. if (parts.size() >= 2)
  36. {
  37. boost::algorithm::trim(parts[0]);
  38. boost::algorithm::trim(parts[1]);
  39. if (parts[0].compare("m_voteForDistance") == 0)
  40. {
  41. try
  42. {
  43. m_voteForDistance = boost::lexical_cast<int>(parts[1]);
  44. }
  45. catch (boost::bad_lexical_cast &)
  46. {}
  47. }
  48. else if (parts[0].compare("m_voteForDistanceFromShooters") == 0)
  49. {
  50. try
  51. {
  52. m_voteForDistanceFromShooters = boost::lexical_cast<int>(parts[1]);
  53. }
  54. catch (boost::bad_lexical_cast &)
  55. {}
  56. }
  57. else if (parts[0].compare("m_voteForHitPoints") == 0)
  58. {
  59. try
  60. {
  61. m_voteForHitPoints = boost::lexical_cast<int>(parts[1]);
  62. }
  63. catch (boost::bad_lexical_cast &)
  64. {}
  65. }
  66. else if (parts[0].compare("m_voteForMaxDamage") == 0)
  67. {
  68. try
  69. {
  70. m_voteForMaxDamage = boost::lexical_cast<int>(parts[1]);
  71. }
  72. catch (boost::bad_lexical_cast &)
  73. {}
  74. }
  75. else if (parts[0].compare("m_voteForMaxSpeed") == 0)
  76. {
  77. try
  78. {
  79. m_voteForMaxSpeed = boost::lexical_cast<int>(parts[1]);
  80. }
  81. catch (boost::bad_lexical_cast &)
  82. {}
  83. }
  84. else if (parts[0].compare("m_voteForMinDamage") == 0)
  85. {
  86. try
  87. {
  88. m_voteForMinDamage = boost::lexical_cast<int>(parts[1]);
  89. }
  90. catch (boost::bad_lexical_cast &)
  91. {}
  92. }
  93. }
  94. }
  95. f.close();
  96. }
  97. }
  98. CBattleHelper::~CBattleHelper()
  99. {}
  100. int CBattleHelper::GetBattleFieldPosition(int x, int y)
  101. {
  102. return x + 17 * (y - 1);
  103. }
  104. int CBattleHelper::DecodeXPosition(int battleFieldPosition)
  105. {
  106. int pos = battleFieldPosition - (DecodeYPosition(battleFieldPosition) - 1) * 17;
  107. assert(pos > 0 && pos < 16);
  108. return pos;
  109. }
  110. int CBattleHelper::DecodeYPosition(int battleFieldPosition)
  111. {
  112. double y = (double)battleFieldPosition / 17.0;
  113. if (y - (int)y > 0.0)
  114. {
  115. return (int)y + 1;
  116. }
  117. assert((int)y > 0 && (int)y <= 11);
  118. return (int)y;
  119. }
  120. int CBattleHelper::GetShortestDistance(int pointA, int pointB)
  121. {
  122. /**
  123. * TODO: function hasn't been checked!
  124. */
  125. int x1 = DecodeXPosition(pointA);
  126. int y1 = DecodeYPosition(pointA);
  127. //
  128. int x2 = DecodeXPosition(pointB);
  129. //x2 += (x2 % 2)? 0 : 1;
  130. int y2 = DecodeYPosition(pointB);
  131. //
  132. double dx = x1 - x2;
  133. double dy = y1 - y2;
  134. return (int)sqrt(dx * dx + dy * dy);
  135. }
  136. int CBattleHelper::GetDistanceWithObstacles(int pointA, int pointB)
  137. {
  138. // TODO - implement this!
  139. return GetShortestDistance(pointA, pointB);
  140. }