BattleHelper.cpp 3.5 KB

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