// // GridPositionTool.cpp // auto_fill_jewel_v3 // // Created by Red on 2024/12/4. // #include "GridPositionTool.hpp" #include #include using std::pair ; #define GRIDPOSITIONTOOL_BIG_GRID_NCOLS 3 #define GRIDPOSITIONTOOL_BIG_GRID_NROWS 3 #define GRIDPOSITIONTOOL_GAME_WIDTH 640 #define GRIDPOSITIONTOOL_GAME_HEIGHT 720 #define GRIDPOSITIONTOOL_LG_W 210 #define GRIDPOSITIONTOOL_LG_H 270 #define GRIDPOSITIONTOOL_MD_W 210 #define GRIDPOSITIONTOOL_MD_H 135 #define GRIDPOSITIONTOOL_SM_W 105 #define GRIDPOSITIONTOOL_SM_H 135 void GridPositionTool::solve(const bool movable,const vector& plateTypeIdArr , vector>& resultPositions ) { //定义格网左上角为0,0 //pair坐标是先列后行, pair vector> bigIndices = { {6,4,7}, {1,2,3}, {8,5,9} } ; vector> midIndices = { {11, 7,13}, {12, 8,14}, { 1, 3, 5}, { 2, 4, 6}, {15, 9,17}, {16,10,18} } ; vector> smlIndices = { { 21,22,13,14,25,26}, { 23,24,15,16,27,28}, { 1, 2, 5, 6, 9,10}, { 3, 4, 7, 8,11,12}, { 29,30,17,18,33,34}, { 31,32,19,20,35,36} } ; if( movable ) { vector> bigIndices2 = { {7,5, 8,-1}, {2,3, 4, 1}, {9,6,10,-1} } ; vector> midIndices2 = { {13, 9,15,-1}, {14,10,16,-1}, { 3, 5, 7, 1}, { 4, 6, 8, 2}, {17,11,19,-1}, {18,12,20,-1} } ; vector> smlIndices2 = { {25,26,17,18,29,30,-1,-1}, {27,28,19,20,31,32,-1,-1}, { 5, 6, 9,10,13,14, 1, 2}, { 7, 8,11,12,15,16, 3, 4}, {33,34,21,22,37,38,-1,-1}, {35,36,23,24,39,40,-1,-1} } ; bigIndices = bigIndices2 ; midIndices = midIndices2; smlIndices = smlIndices2; } int currindex = 1 ; auto fgc = FillGlobalConfig::getInstance() ; //先安排大盘子 for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) { int typeId = plateTypeIdArr[ijew] ; FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ; if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_LG ) { int col=0; int row=0; if( getIndexPositionInGrid(bigIndices, currindex, col, row) ) { vector posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_LG, col, row) ; resultPositions.push_back(posi) ; currindex++ ; } } } //安排中盘子 currindex = currindex*2 - 1 ; for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) { int typeId = plateTypeIdArr[ijew] ; FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ; if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_MD ) { int col=0; int row=0; if( getIndexPositionInGrid(midIndices, currindex, col, row) ) { vector posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_MD, col, row) ; resultPositions.push_back(posi) ; currindex++ ; } } } //安排小盘子 currindex = currindex*2 - 1 ; for(int ijew = 0 ; ijew < plateTypeIdArr.size(); ++ ijew ) { int typeId = plateTypeIdArr[ijew] ; FillGlobalConfig::PlateItem* plate = fgc->getPlateItemById(typeId) ; if( plate->_size == FILLGLOBALCONFIG_PLATESIZE_SM ) { int col=0; int row=0; if( getIndexPositionInGrid(smlIndices, currindex, col, row) ) { vector posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_SM, col, row) ; resultPositions.push_back(posi) ; currindex++ ; } } } } bool GridPositionTool::getIndexPositionInGrid( const vector>& gridRows, const int index, int& col,int& row) { col = -1 ; row = -1 ; for(int irow = 0 ; irow < gridRows.size();++irow ) { for(int icol=0; icol < gridRows[irow].size(); ++ icol ) { if( gridRows[irow][icol] == index ) { col = icol ; row = irow ; return true ; } } } std::cout<<"error: could not found index from grid. "< GridPositionTool::computePositionByGridColRow(int plateSize, int col, int row ) { if( plateSize == FILLGLOBALCONFIG_PLATESIZE_LG ) { //将左上角原点的网格坐标换算成左下角原点网格坐标 row = GRIDPOSITIONTOOL_BIG_GRID_NROWS - 1 - row ; int x0 = 0 ; int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS*GRIDPOSITIONTOOL_LG_H/2; int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ; return { (int)(x0+(col+0.5)*widthAndMargin ) , (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_LG_H) } ; }else if( plateSize==FILLGLOBALCONFIG_PLATESIZE_MD ) { //将左上角原点的网格坐标换算成左下角原点网格坐标 row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ; int x0 = 0 ; int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_MD_H/2; int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ; return { (int)(x0+(col+0.5)*widthAndMargin ) , (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_MD_H) } ; }else { // FILLGLOBALCONFIG_JEWELSIZE_SM //将左上角原点的网格坐标换算成左下角原点网格坐标 row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ; int x0 = 0 ; int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_SM_H/2; int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / (GRIDPOSITIONTOOL_BIG_GRID_NCOLS*2) ; return { (int)(x0+(col+0.5)*widthAndMargin ) , (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_SM_H) } ; } }