BattleHelper.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(BFIELD_WIDTH-2),
  14. BattlefieldHeight(BFIELD_HEIGHT),
  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 + BFIELD_WIDTH * (y - 1);
  104. }
  105. int CBattleHelper::DecodeXPosition(int battleFieldPosition)
  106. {
  107. int x = battleFieldPosition%BFIELD_WIDTH;
  108. assert( x > 0 && x < BFIELD_WIDTH-1 );
  109. return x;
  110. }
  111. int CBattleHelper::DecodeYPosition(int battleFieldPosition)
  112. {
  113. int y=battleFieldPosition/BFIELD_WIDTH +1;
  114. assert( y > 0 && y <= BFIELD_HEIGHT);
  115. return y;
  116. }
  117. int CBattleHelper::StepRight(int pos){
  118. return pos+1;
  119. }
  120. int CBattleHelper::StepLeft(int pos){
  121. return pos-1;
  122. }
  123. int CBattleHelper::StepUpleft(int pos){
  124. if((pos/BFIELD_WIDTH)%2==0){
  125. return pos-BFIELD_WIDTH;
  126. }else{
  127. return pos-BFIELD_WIDTH-1;
  128. }
  129. }
  130. int CBattleHelper::StepUpright(int pos){
  131. if((pos/BFIELD_WIDTH)%2==0){
  132. return pos-BFIELD_WIDTH+1;
  133. }else{
  134. return pos-BFIELD_WIDTH;
  135. }
  136. }
  137. int CBattleHelper::StepDownleft(int pos){
  138. if((pos/BFIELD_WIDTH)%2==0){
  139. return pos+BFIELD_WIDTH;
  140. }else{
  141. return pos+BFIELD_WIDTH-1;
  142. }
  143. }
  144. int CBattleHelper::StepDownright(int pos){
  145. if((pos/BFIELD_WIDTH)%2==0){
  146. return pos+BFIELD_WIDTH+1;
  147. }else{
  148. return pos+BFIELD_WIDTH;
  149. }
  150. }
  151. int CBattleHelper::GetShortestDistance(int pointA, int pointB)
  152. {
  153. int x1 = DecodeXPosition(pointA);
  154. int y1 = DecodeYPosition(pointA);
  155. int x2 = DecodeXPosition(pointB);
  156. int y2 = DecodeYPosition(pointB);
  157. int dx = abs(x1 - x2);
  158. int dy = abs(y1 - y2);
  159. if( dy%2==1 ){
  160. if( y1%2==1 ){
  161. if( x2<=x1 ){dx++;}
  162. }else{
  163. if( x2>=x1 ){dx++;}
  164. }
  165. }
  166. max(dy,dx+dy/2);
  167. return 0;
  168. }
  169. int CBattleHelper::GetDistanceWithObstacles(int pointA, int pointB)
  170. {
  171. // TODO - implement this!
  172. return GetShortestDistance(pointA, pointB);
  173. }