123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // GridPositionTool.cpp
- // auto_fill_jewel_v3
- //
- // Created by Red on 2024/12/4.
- //
- #include "GridPositionTool.hpp"
- #include <utility>
- #include <iostream>
- 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<int>& plateTypeIdArr , vector<vector<int>>& resultPositions )
- {
- //定义格网左上角为0,0
- //pair坐标是先列后行, pair<col,row>
- vector<vector<int>> bigIndices = {
- {6,4,7},
- {1,2,3},
- {8,5,9}
- } ;
-
- vector<vector<int>> midIndices = {
- {11, 7,13},
- {12, 8,14},
- { 1, 3, 5},
- { 2, 4, 6},
- {15, 9,17},
- {16,10,18}
- } ;
-
- vector<vector<int>> 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<vector<int>> bigIndices2 = {
- {7,5, 8,-1},
- {2,3, 4, 1},
- {9,6,10,-1}
- } ;
-
- vector<vector<int>> 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<vector<int>> 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<int> 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<int> 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<int> posi = computePositionByGridColRow(FILLGLOBALCONFIG_PLATESIZE_SM, col, row) ;
- resultPositions.push_back(posi) ;
- currindex++ ;
- }
- }
- }
-
- }
- bool GridPositionTool::getIndexPositionInGrid( const vector<vector<int>>& 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. "<<index<<std::endl;
- return false;
- }
- vector<int> 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) } ;
- }
- }
|