GridPositionTool.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // GridPositionTool.cpp
  3. // auto_fill_jewel_v3
  4. //
  5. // Created by Red on 2024/12/4.
  6. //
  7. #include "GridPositionTool.hpp"
  8. #include <utility>
  9. #include <iostream>
  10. using std::pair ;
  11. #define GRIDPOSITIONTOOL_BIG_GRID_NCOLS 3
  12. #define GRIDPOSITIONTOOL_BIG_GRID_NROWS 3
  13. #define GRIDPOSITIONTOOL_GAME_WIDTH 640
  14. #define GRIDPOSITIONTOOL_GAME_HEIGHT 720
  15. #define GRIDPOSITIONTOOL_LG_W 210
  16. #define GRIDPOSITIONTOOL_LG_H 270
  17. #define GRIDPOSITIONTOOL_MD_W 210
  18. #define GRIDPOSITIONTOOL_MD_H 135
  19. #define GRIDPOSITIONTOOL_SM_W 105
  20. #define GRIDPOSITIONTOOL_SM_H 135
  21. void GridPositionTool::solve(const bool movable,const vector<int>& plateTypeIdArr , vector<vector<int>>& resultPositions )
  22. {
  23. //定义格网左上角为0,0
  24. //pair坐标是先列后行, pair<col,row>
  25. vector<vector<int>> bigIndices = {
  26. {6,4,7},
  27. {1,2,3},
  28. {8,5,9}
  29. } ;
  30. vector<vector<int>> midIndices = {
  31. {11, 7,13},
  32. {12, 8,14},
  33. { 1, 3, 5},
  34. { 2, 4, 6},
  35. {15, 9,17},
  36. {16,10,18}
  37. } ;
  38. vector<vector<int>> smlIndices = {
  39. { 21,22,13,14,25,26},
  40. { 23,24,15,16,27,28},
  41. { 1, 2, 5, 6, 9,10},
  42. { 3, 4, 7, 8,11,12},
  43. { 29,30,17,18,33,34},
  44. { 31,32,19,20,35,36}
  45. } ;
  46. if( movable ) {
  47. vector<vector<int>> bigIndices2 = {
  48. {7,5, 8,-1},
  49. {2,3, 4, 1},
  50. {9,6,10,-1}
  51. } ;
  52. vector<vector<int>> midIndices2 = {
  53. {13, 9,15,-1},
  54. {14,10,16,-1},
  55. { 3, 5, 7, 1},
  56. { 4, 6, 8, 2},
  57. {17,11,19,-1},
  58. {18,12,20,-1}
  59. } ;
  60. vector<vector<int>> smlIndices2 = {
  61. {25,26,17,18,29,30,-1,-1},
  62. {27,28,19,20,31,32,-1,-1},
  63. { 5, 6, 9,10,13,14, 1, 2},
  64. { 7, 8,11,12,15,16, 3, 4},
  65. {33,34,21,22,37,38,-1,-1},
  66. {35,36,23,24,39,40,-1,-1}
  67. } ;
  68. bigIndices = bigIndices2 ;
  69. midIndices = midIndices2;
  70. smlIndices = smlIndices2;
  71. }
  72. int currindex = 1 ;
  73. auto fgc = FillGlobalConfig::getInstance() ;
  74. //先安排大盘子
  75. for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) {
  76. int typeId = plateTypeIdArr[ijew] ;
  77. FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ;
  78. if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_LG ) {
  79. int col=0;
  80. int row=0;
  81. if( getIndexPositionInGrid(bigIndices, currindex, col, row) ) {
  82. vector<int> posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_LG, col, row) ;
  83. resultPositions.push_back(posi) ;
  84. currindex++ ;
  85. }
  86. }
  87. }
  88. //安排中盘子
  89. currindex = currindex*2 - 1 ;
  90. for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) {
  91. int typeId = plateTypeIdArr[ijew] ;
  92. FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ;
  93. if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_MD ) {
  94. int col=0;
  95. int row=0;
  96. if( getIndexPositionInGrid(midIndices, currindex, col, row) ) {
  97. vector<int> posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_MD, col, row) ;
  98. resultPositions.push_back(posi) ;
  99. currindex++ ;
  100. }
  101. }
  102. }
  103. //安排小盘子
  104. currindex = currindex*2 - 1 ;
  105. for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) {
  106. int typeId = plateTypeIdArr[ijew] ;
  107. FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ;
  108. if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_SM ) {
  109. int col=0;
  110. int row=0;
  111. if( getIndexPositionInGrid(smlIndices, currindex, col, row) ) {
  112. vector<int> posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_SM, col, row) ;
  113. resultPositions.push_back(posi) ;
  114. currindex++ ;
  115. }
  116. }
  117. }
  118. }
  119. bool GridPositionTool::getIndexPositionInGrid( const vector<vector<int>>& gridRows, const int index, int& col,int& row)
  120. {
  121. col = -1 ;
  122. row = -1 ;
  123. for(int irow = 0 ; irow < gridRows.size();++irow ) {
  124. for(int icol=0; icol < gridRows[irow].size(); ++ icol ) {
  125. if( gridRows[irow][icol] == index ) {
  126. col = icol ;
  127. row = irow ;
  128. return true ;
  129. }
  130. }
  131. }
  132. std::cout<<"error: could not found index from grid. "<<index<<std::endl;
  133. return false;
  134. }
  135. vector<int> GridPositionTool::computePositionByGridColRow(int plateSize, int col, int row )
  136. {
  137. if( plateSize == FILLGLOBALCONFIG_PLATESIZE_LG ) {
  138. //将左上角原点的网格坐标换算成左下角原点网格坐标
  139. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS - 1 - row ;
  140. int x0 = 0 ;
  141. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS*GRIDPOSITIONTOOL_LG_H/2;
  142. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ;
  143. return {
  144. (int)(x0+(col+0.5)*widthAndMargin ) ,
  145. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_LG_H) } ;
  146. }else if( plateSize==FILLGLOBALCONFIG_PLATESIZE_MD ) {
  147. //将左上角原点的网格坐标换算成左下角原点网格坐标
  148. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ;
  149. int x0 = 0 ;
  150. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_MD_H/2;
  151. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ;
  152. return {
  153. (int)(x0+(col+0.5)*widthAndMargin ) ,
  154. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_MD_H) } ;
  155. }else { // FILLGLOBALCONFIG_JEWELSIZE_SM
  156. //将左上角原点的网格坐标换算成左下角原点网格坐标
  157. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ;
  158. int x0 = 0 ;
  159. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_SM_H/2;
  160. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / (GRIDPOSITIONTOOL_BIG_GRID_NCOLS*2) ;
  161. return {
  162. (int)(x0+(col+0.5)*widthAndMargin ) ,
  163. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_SM_H) } ;
  164. }
  165. }