BattleHelper.cpp 4.2 KB

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