LevelGenerate.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. //
  2. // LevelGenerate.cpp
  3. // auto_fill_jewel_v3
  4. //
  5. // Created by Red on 2024/11/23.
  6. //
  7. #include "LevelGenerate.hpp"
  8. #include <iostream>
  9. #include <cassert>
  10. #include <unordered_map>
  11. #include "FillGlobalConfig.hpp"
  12. #include <tuple>
  13. #include "BoxPositionTool.hpp"
  14. #include "GridPositionTool.hpp"
  15. using namespace std;
  16. bool LevelGenerate::generate( FillGlobalConfig::LevelData levelData,
  17. const bool isMovable,
  18. vector<tuple<int,vector<vector<FillResult>>>>& resultPlateFillResults,
  19. vector<ContourData::Point>& resultPlateCenterPointArr
  20. )
  21. {
  22. resultPlateFillResults.clear() ;
  23. resultPlateCenterPointArr.clear() ;
  24. auto ms0 = std::chrono::system_clock::now().time_since_epoch() ;
  25. uint64_t ms00 = std::chrono::duration_cast<chrono::milliseconds>(ms0).count() ;
  26. srand(_seed);
  27. unordered_map<int,tuple<int,int> > jewIdSzCntStorageMap ;
  28. FillGlobalConfig* fgc = FillGlobalConfig::getInstance() ;
  29. int bigJewelCnt = 0 ;
  30. int midJewelCnt = 0;
  31. int smlJewelCnt = 0 ;
  32. for(int ij = 0 ; ij < levelData._jewelIds.size();++ ij ) {
  33. int jewId = levelData._jewelIds[ij] ;
  34. int cnt = levelData._cnts[ij] ;
  35. FillGlobalConfig::JewelItem* jewItem = fgc->getJewelItemById(jewId) ;
  36. jewIdSzCntStorageMap[jewId] = {jewItem->_size,cnt} ;
  37. if( jewItem->_size == FILLGLOBALCONFIG_JEWELSIZE_SM ) {
  38. smlJewelCnt+=cnt ;
  39. }else if( jewItem->_size == FILLGLOBALCONFIG_JEWELSIZE_MD ) {
  40. midJewelCnt += cnt ;
  41. }else if( jewItem->_size == FILLGLOBALCONFIG_JEWELSIZE_LG ) {
  42. bigJewelCnt += cnt ;
  43. }
  44. }
  45. //每个盘子类型小号宝石承载量
  46. //按顺序分别为大横,大竖,小。这里不考虑迷你。
  47. FillGlobalConfig::PlateItem* bigPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_LG, 0) ;
  48. FillGlobalConfig::PlateItem* midPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_MD, 0) ;
  49. FillGlobalConfig::PlateItem* smlPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_SM, 0) ;
  50. vector<FillGlobalConfig::PlateItem*> usedPlates = {bigPlate,midPlate,smlPlate} ;
  51. int maxSmlJewCapPerPlate = 0;
  52. int minSmlJewCapPerPlate = 99999 ;
  53. for(int ip=0;ip<usedPlates.size();++ip ) {
  54. maxSmlJewCapPerPlate = fmax( usedPlates[ip]->_bigJewCap*4 , maxSmlJewCapPerPlate) ;
  55. minSmlJewCapPerPlate = fmin( usedPlates[ip]->_bigJewCap*4 , minSmlJewCapPerPlate) ;
  56. }
  57. //等效小宝石数量
  58. int effSmallJewelsCount = bigJewelCnt*4 + midJewelCnt*2 + smlJewelCnt ;
  59. //cout<<"bigJewelCnt "<<bigJewelCnt<<endl;
  60. //cout<<"midJewelCnt "<<midJewelCnt<<endl;
  61. //cout<<"smlJewelCnt "<<smlJewelCnt<<endl;
  62. //cout<<"effSmallJewelsCount "<<effSmallJewelsCount<<endl;
  63. //构建符合难度条件的盘子和层数组合
  64. vector<PlateIdAndLayerCnt> resPlateNLyrCnt = generatePlateTypeAndLayerCnts2(isMovable, levelData._difficulty,effSmallJewelsCount);
  65. if( resPlateNLyrCnt.size()==0 ) {
  66. //cout<<"failed at generatePlateTypeAndLayerCnts"<<endl;
  67. return false ;
  68. }
  69. //计算盘子的最大层数
  70. int maxLyrCnt = 0;
  71. for(auto it = resPlateNLyrCnt.begin();it!=resPlateNLyrCnt.end();++it ) maxLyrCnt=fmax(maxLyrCnt,it->_layerCnt) ;
  72. //对每个盘子组合进行填充宝石
  73. RandomGridFiller filler ;
  74. filler._seed = this->_seed ;
  75. srand(filler._seed);
  76. //构建每个盘子每个层的填充结果容器
  77. vector< tuple<int,int,vector<FillResult>> > plateIdLyrFillResultsArray ;// tuple<plateId, layerIndex, fillresults_array >
  78. //从顶到底部,逐层进行填充
  79. int residues = 0 ;
  80. for(int nlayer = maxLyrCnt; nlayer > 0 ; nlayer-- ) {
  81. //cout<<"for layer "<<nlayer<<endl;
  82. //满足该层数的所有盘子ID
  83. vector<int> plateIdArr ;
  84. for(auto it = resPlateNLyrCnt.begin();it!=resPlateNLyrCnt.end();++it ) if(it->_layerCnt>=nlayer) plateIdArr.push_back(it->_plateId) ;
  85. //该层总计需要填充多少小宝石
  86. int currLyrNeedEquvSmlCnt = 0 ;
  87. for(int ip=0;ip<plateIdArr.size();++ip ) {
  88. currLyrNeedEquvSmlCnt += fgc->getPlateItemById(plateIdArr[ip])->_bigJewCap*4 ;
  89. }
  90. //cout<<"layer need eqsmCnt "<<currLyrNeedEquvSmlCnt<<endl;
  91. //根据所需数量和所需求解百分比,计算从库存取出每类宝石多少个。
  92. float solPercent = 0.7 ;//根据需求文档,每层可解百分比都是70%.
  93. unordered_map<int, int> chosenJewIdNCnt = randPickJewels(currLyrNeedEquvSmlCnt, solPercent, jewIdSzCntStorageMap) ;
  94. int choseEqSmCnt=0;
  95. for(auto itp=chosenJewIdNCnt.begin();itp!=chosenJewIdNCnt.end();++itp) {
  96. choseEqSmCnt += jewSz2SmlCnt( fgc->getJewelItemById(itp->first)->_size) * itp->second ;
  97. }
  98. //cout<<"layer choose eqsmCnt "<<choseEqSmCnt<<endl;
  99. //逐个盘子填充
  100. for(auto itpid = plateIdArr.begin();itpid!=plateIdArr.end();++itpid) {
  101. vector<FillResult> fr = fillPlateOneLayer(filler, *itpid, chosenJewIdNCnt) ;
  102. tuple<int,int,vector<FillResult>> plateLyrFillResult={*itpid,nlayer,fr} ;
  103. plateIdLyrFillResultsArray.push_back(plateLyrFillResult);
  104. }
  105. //检查为该层挑选的宝石是不是全部填完了,如果没有填完还要还给库存,给下一轮填充用
  106. residues = 0 ;
  107. for(auto itjn = chosenJewIdNCnt.begin();itjn!=chosenJewIdNCnt.end();++itjn){
  108. if( itjn->second>0 ) {
  109. residues+=itjn->second;
  110. std::get<1>(jewIdSzCntStorageMap[itjn->first]) += itjn->second ;//剩余的加回到库存里
  111. }
  112. }
  113. //cout<<"layer chosen jewels residues "<<residues<<endl;
  114. }
  115. //查看库存是否全部用掉了
  116. if( residues>0 ) {
  117. int extraPlateCnt = 0 ;
  118. //剩了,添加一个小盘子和层,仍然剩了再继续加盘子加层
  119. unordered_map<int, int> residueJewIdAndCnts = findUnfilledInStorages(jewIdSzCntStorageMap) ;
  120. while( residueJewIdAndCnts.size()>0 ) {
  121. //新建一个小盘子
  122. extraPlateCnt++;
  123. //cout<<"add extra plate "<< extraPlateCnt <<endl;
  124. int plateId = smlPlate->_id ;
  125. for(int ilyr = 1 ; ilyr <= maxLyrCnt; ++ ilyr ) {
  126. vector<FillResult> frs = fillPlateOneLayer(filler, plateId, residueJewIdAndCnts) ;
  127. plateIdLyrFillResultsArray.push_back( {plateId,ilyr,frs} );
  128. //cout<<"add extra lyr "<<endl;
  129. if( residueJewIdAndCnts.size()==0 ) break ;
  130. }
  131. if( residueJewIdAndCnts.size()==0 ) break ;
  132. }
  133. }
  134. //整理数据
  135. regroupPlateLyrFillResults(plateIdLyrFillResultsArray, resultPlateFillResults);
  136. //摆放盘子
  137. {
  138. vector<int> plateIdArr1;
  139. for(auto itp = resultPlateFillResults.begin();itp!=resultPlateFillResults.end();++itp) plateIdArr1.push_back( std::get<0>(*itp) );
  140. placePlates(isMovable, plateIdArr1, resultPlateCenterPointArr) ;
  141. }
  142. auto ms1 = std::chrono::system_clock::now().time_since_epoch() ;
  143. uint64_t ms11 = std::chrono::duration_cast<chrono::milliseconds>(ms1).count() ;
  144. //cout<<"duration "<<ms11-ms00<<endl;
  145. return true ;
  146. }
  147. // this method is deprecated, use generatePlateTypeAndLayerCnts2()
  148. [[deprecated]]
  149. vector<LevelGenerate::PlateIdAndLayerCnt> LevelGenerate::generatePlateTypeAndLayerCnts(const int difficulty,const int totEquvSmallJewelCnt)
  150. {
  151. assert(difficulty>=0&&difficulty<=2) ;
  152. vector<vector<LevelGenerate::PlateIdAndLayerCnt>> possibleResults ;
  153. vector<LevelGenerate::PlateIdAndLayerCnt> results ;
  154. assert(totEquvSmallJewelCnt>0) ;
  155. FillGlobalConfig* fgc = FillGlobalConfig::getInstance() ;
  156. FillGlobalConfig::PlateItem* bigPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_LG, 0) ;
  157. FillGlobalConfig::PlateItem* midPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_MD, 0) ;
  158. FillGlobalConfig::PlateItem* smlPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_SM, 0) ;
  159. vector<FillGlobalConfig::PlateItem*> usedPlates = {bigPlate,midPlate,smlPlate} ;
  160. //一个关卡最多出现大、中、小盘子个数
  161. const int PLATE_BG_MAX_CNT = 9 ;// 1个大盘子=2两个中盘子=6个小盘子
  162. const int PLATE_MD_MAX_CNT = 18 ;
  163. const int PLATE_SM_MAX_CNT = 54 ;
  164. //每个关卡最大容纳等效小盘子的个数 就是54个
  165. // 普通 难 极难
  166. vector< vector<float> > diffPlateCntPercent0 = { {0.3,0.3,0.0}, {0.2,0.4,0.0}, {0.1,0.5,0.0} } ;
  167. vector< vector<float> > diffPlateCntPercent1 = { {0.7,0.7,0.0}, {0.6,0.8,0.1}, {0.5,0.9,0.1} } ;
  168. bool isFirstGood = false;//由于盘子百分比计算不一定能满足盘子和宝石要求,为了保证至少有一个盘子组合可以用这里保留第一个可以装下宝石的盘子组合,如果有更优的方案替换之。
  169. vector<LevelGenerate::PlateIdAndLayerCnt> firstAvailablePlateComb ;
  170. for(int ipbig = 1; ipbig <= PLATE_BG_MAX_CNT; ++ ipbig ) {
  171. for(int ipmid = 0 ; ipmid <= PLATE_MD_MAX_CNT; ++ ipmid ) {
  172. int effsmPlateCnt = ipbig*6 + ipmid*3 ;
  173. if( effsmPlateCnt>PLATE_SM_MAX_CNT ) continue ;
  174. for(int ipsml = 0 ; ipsml <= PLATE_SM_MAX_CNT; ++ ipsml ) {
  175. //等效小盘子个数
  176. effsmPlateCnt += ipsml ;
  177. if( effsmPlateCnt>PLATE_SM_MAX_CNT ) continue ;
  178. float bigPlateCntPercent = ipbig*6.0 / effsmPlateCnt ;
  179. float midPlateCntPercent = ipmid*3.0 / effsmPlateCnt ;
  180. float smlPlateCntPercent = ipsml*1.0 / effsmPlateCnt ;
  181. vector<float> percentArr = {bigPlateCntPercent,midPlateCntPercent,smlPlateCntPercent} ;
  182. //判断每个盘子百分比是否满足难度要求
  183. bool percentOk = true ;
  184. for(int iplatetype=0;iplatetype<percentArr.size();++iplatetype) {
  185. if( diffPlateCntPercent0[difficulty][iplatetype]>percentArr[iplatetype] || diffPlateCntPercent1[difficulty][iplatetype]<percentArr[iplatetype] )
  186. percentOk=false;
  187. }
  188. if( isFirstGood == true ){
  189. //至少有一个装下宝石的方案了
  190. if( !percentOk ) continue ;//盘子百分比不符合要求,跳过
  191. }
  192. if( isFirstGood==false ) {
  193. //保证至少有一个可以满足宝石的盘子组合
  194. vector<int> pidArr ;
  195. vector<int> capArr ;
  196. vector<int> lyrArr ;
  197. for(int it=0;it<ipbig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(1); }
  198. for(int it=0;it<ipmid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(1); }
  199. for(int it=0;it<ipsml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(1); }
  200. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  201. if( lyrOk ){
  202. isFirstGood = true ;
  203. //保存结果
  204. firstAvailablePlateComb.resize(capArr.size());
  205. for(int it = 0 ; it < capArr.size(); ++ it ) {
  206. firstAvailablePlateComb[it]._plateId = pidArr[it] ;
  207. firstAvailablePlateComb[it]._layerCnt = lyrArr[it] ;
  208. }
  209. }
  210. }
  211. //计算每个类型的层数
  212. if( difficulty== FILLGLOBALCONFIG_DIFFCULTY_NORM ) {
  213. //全部两层
  214. vector<int> pidArr ;
  215. vector<int> capArr ;
  216. vector<int> lyrArr ;
  217. for(int it=0;it<ipbig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(2); }
  218. for(int it=0;it<ipmid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(2); }
  219. for(int it=0;it<ipsml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(2); }
  220. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  221. if(!lyrOk) continue;
  222. //保存结果
  223. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  224. for(int it = 0 ; it < capArr.size(); ++ it ) {
  225. tres[it]._plateId = pidArr[it] ;
  226. tres[it]._layerCnt = lyrArr[it] ;
  227. }
  228. possibleResults.push_back(tres) ;
  229. }else if( difficulty==FILLGLOBALCONFIG_DIFFCULTY_HARD2 ) {
  230. //全部3层
  231. vector<int> pidArr ;
  232. vector<int> capArr ;
  233. vector<int> lyrArr ;
  234. for(int it=0;it<ipbig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(3); }
  235. for(int it=0;it<ipmid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(3); }
  236. for(int it=0;it<ipsml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(3); }
  237. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  238. if(!lyrOk) continue;
  239. //保存结果
  240. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  241. for(int it = 0 ; it < capArr.size(); ++ it ) {
  242. tres[it]._plateId = pidArr[it] ;
  243. tres[it]._layerCnt = lyrArr[it] ;
  244. }
  245. possibleResults.push_back(tres) ;
  246. }else{
  247. //50% 2层, 50% 3层
  248. vector<int> pidArr ;
  249. vector<int> capArr ;
  250. vector<int> lyrArr ;
  251. for(int it=0;it<ipbig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  252. for(int it=0;it<ipmid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  253. for(int it=0;it<ipsml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  254. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  255. if(!lyrOk) continue;
  256. //保存结果
  257. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  258. for(int it = 0 ; it < capArr.size(); ++ it ) {
  259. tres[it]._plateId = pidArr[it] ;
  260. tres[it]._layerCnt = lyrArr[it] ;
  261. }
  262. possibleResults.push_back(tres) ;
  263. }
  264. }
  265. }
  266. }
  267. //随机挑选一个结果返回
  268. if( possibleResults.size()> 0 ) {
  269. int randindex = rand() % possibleResults.size();
  270. results = possibleResults[randindex] ;
  271. }else {
  272. results = firstAvailablePlateComb ;
  273. }
  274. return results ;
  275. }
  276. //考虑可移动关卡的条件
  277. vector<LevelGenerate::PlateIdAndLayerCnt> LevelGenerate::generatePlateTypeAndLayerCnts2(const bool isMovable,const int difficulty,const int totEquvSmallJewelCnt)
  278. {
  279. //移动关卡相当于多了一个 大盘子,整个关卡最多可以达到10个大盘子
  280. //如果是移动关卡,那么至少需要保证如下等价小盘子数量,如果减少层数仍然不能满足那么就有几个盘子输出几个
  281. const int MAX_LG_PLATE_CNT = 9 + (isMovable?1:0) ;
  282. const int MAX_SM_PLATE_CNT = MAX_LG_PLATE_CNT*4 ;
  283. const int MAX_SM_JEWEL_CNT_PER_LAYER = MAX_LG_PLATE_CNT * 24 ;
  284. const int minEquaSmPlateCntForMovable = 16 ; //
  285. const int smPlateEquaSmJewelCnt = 6 ;// 小盘子装1.5个大,3个中,6个小
  286. //一个关卡最多出现大、中、小盘子个数
  287. int bigPlateMaxCnt = 9 ;// 1个大盘子=2两个中盘子=4个小盘子
  288. if( isMovable ) {
  289. bigPlateMaxCnt = 10 ;
  290. }
  291. int midPlateMaxCnt = bigPlateMaxCnt * 2 ;
  292. int smlPlateMaxCnt = bigPlateMaxCnt * 2 ;
  293. vector<LevelGenerate::PlateIdAndLayerCnt> results ;
  294. FillGlobalConfig* fgc = FillGlobalConfig::getInstance() ;
  295. FillGlobalConfig::PlateItem* bigPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_LG, 0) ;
  296. FillGlobalConfig::PlateItem* midPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_MD, 0) ;
  297. FillGlobalConfig::PlateItem* smlPlate = fgc->getPlateItemBySzNIndex(FILLGLOBALCONFIG_PLATESIZE_SM, 0) ;
  298. vector<FillGlobalConfig::PlateItem*> usedPlates = {bigPlate,midPlate,smlPlate} ;
  299. int equvSmPlateCnt = ceil( totEquvSmallJewelCnt * 1.0 / 6) ; // 本款需要多少个一层小盘子的总数量
  300. int layerCnt0 = 1 ;//盘子层数下限
  301. int layerCnt1 = 1 ;//盘子层数上限
  302. //计算可能的层取值范围
  303. bool forceToFitMovable = false ;
  304. if( isMovable ) {
  305. if( equvSmPlateCnt <= minEquaSmPlateCntForMovable ) {
  306. //不足移动关卡所需最小的等效小盘子个数,那么直接组合盘子即可。
  307. layerCnt0 = 1 ;
  308. layerCnt1 = 1 ;
  309. forceToFitMovable = false ;
  310. }else
  311. {
  312. //10个大盘子,6个大宝石,等效于每层最多放置 10*6*4=240个小宝石
  313. layerCnt0 = fmax(1 , totEquvSmallJewelCnt / MAX_SM_JEWEL_CNT_PER_LAYER ) ;
  314. if( layerCnt0<3 ) layerCnt1 = 3 ;
  315. else layerCnt1 = layerCnt0 + 1 ;
  316. forceToFitMovable = true ;
  317. }
  318. }else {
  319. layerCnt0 = fmax(1 , totEquvSmallJewelCnt / MAX_SM_JEWEL_CNT_PER_LAYER ) ;
  320. if( layerCnt0<3 ) layerCnt1 = 3 ;
  321. else layerCnt1 = layerCnt0 + 1 ;
  322. forceToFitMovable = false ;
  323. }
  324. int bigPlateCnt0 = totEquvSmallJewelCnt * 1.0 / layerCnt1 / 24 ; //大盘子最少需要数量
  325. int bigPlateCnt1 = totEquvSmallJewelCnt * 1.0 / layerCnt0 / 24 + 1; //大盘子最多需要数量
  326. int midPlateCnt0 = bigPlateCnt0*2 ;
  327. int midPlateCnt1 = bigPlateCnt1*2 ;
  328. int smlPlateCnt0 = bigPlateCnt0*4 ;
  329. int smlPlateCnt1 = bigPlateCnt1*4 ;
  330. bigPlateCnt1 = fmin( bigPlateMaxCnt ,bigPlateCnt1 ) ;
  331. midPlateCnt1 = fmin( midPlateMaxCnt ,midPlateCnt1 ) ;
  332. smlPlateCnt1 = fmin( smlPlateMaxCnt ,smlPlateCnt1 ) ;
  333. bool findFirstAvailableComb = false ;//第一个完全放入宝石的组合,用于当不满足任何条件时,至少返回一个可装入全部宝石的默认方案
  334. vector<vector<LevelGenerate::PlateIdAndLayerCnt>> possibleResults ;//满足全部百分比要求的组合
  335. vector<LevelGenerate::PlateIdAndLayerCnt> firstAvailablePlateComb ;//第一个满足全部宝石填充的组合,如果需要移动还要考虑最小移动盘子个数的条件
  336. int firstAvailableLyrCnt = 0 ;
  337. //难度对盘子数量的限制 普通 难 极难
  338. vector< vector<float> > hardPlateCntPercent0 = { {0.3,0.3,0.0}, {0.2,0.4,0.0}, {0.1,0.5,0.0} } ;
  339. vector< vector<float> > hardPlateCntPercent1 = { {0.7,0.7,0.0}, {0.6,0.8,0.1}, {0.5,0.9,0.1} } ;
  340. //穷举全部盘子和层的组合找到符合条件的组合
  341. for(int ibig = 0; ibig <= bigPlateCnt1; ++ ibig ) {
  342. for(int imid = 0; imid <= midPlateCnt1 ; ++ imid ) {
  343. for(int isml = 0 ; isml <= smlPlateCnt1; ++ isml ) {
  344. int equvSmPlateCnt1 = isml+imid*2+ibig*4 ;
  345. if( equvSmPlateCnt1==0 || equvSmPlateCnt1 > MAX_SM_PLATE_CNT ) continue ;
  346. if( equvSmPlateCnt1 < smlPlateCnt0 ) continue ;
  347. if( isMovable && forceToFitMovable ) if( equvSmPlateCnt1<minEquaSmPlateCntForMovable ) continue ;
  348. float bigPlateCntPercent = ibig*4.0 / equvSmPlateCnt1 ;
  349. float midPlateCntPercent = imid*2.0 / equvSmPlateCnt1 ;
  350. float smlPlateCntPercent = isml*1.0 / equvSmPlateCnt1 ;
  351. vector<float> percentArr = {bigPlateCntPercent,midPlateCntPercent,smlPlateCntPercent} ;
  352. //判断每个盘子百分比是否满足难度要求
  353. bool percentOk = true ;
  354. for(int iplatetype=0;iplatetype<percentArr.size();++iplatetype) {
  355. if( hardPlateCntPercent0[difficulty][iplatetype]>percentArr[iplatetype] || hardPlateCntPercent1[difficulty][iplatetype]<percentArr[iplatetype] )
  356. percentOk=false;
  357. }
  358. if( findFirstAvailableComb == true ){
  359. //至少有一个装下宝石的方案了
  360. if( !percentOk ) continue ;//盘子百分比不符合要求,跳过
  361. }
  362. if( findFirstAvailableComb==false || (findFirstAvailableComb && firstAvailableLyrCnt>3 ) ) {
  363. //保证至少有一个可以满足宝石的盘子组合,优先保存总层数大于3的情况
  364. //当前盘子组合下最小满足的层数
  365. int upperLyrCnt = ceil( totEquvSmallJewelCnt * 1.0 / (equvSmPlateCnt1*6) ) ;
  366. bool upperLyrCntOk = true ;
  367. if( findFirstAvailableComb ) {
  368. if( upperLyrCnt >= firstAvailableLyrCnt ) {
  369. upperLyrCntOk = false ;
  370. }
  371. }
  372. vector<int> pidArr ;
  373. vector<int> capArr ;
  374. vector<int> lyrArr ;
  375. for(int it=0;it<ibig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(upperLyrCnt); }
  376. for(int it=0;it<imid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(upperLyrCnt); }
  377. for(int it=0;it<isml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(upperLyrCnt); }
  378. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  379. if( lyrOk==false && upperLyrCnt>1 ) {
  380. for(int it=0;it<lyrArr.size();++it ) {
  381. lyrArr[it] -= 1 ;
  382. lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  383. if( lyrOk ) break ;
  384. }
  385. }
  386. if( lyrOk && upperLyrCntOk ){
  387. findFirstAvailableComb = true ;
  388. firstAvailableLyrCnt = upperLyrCnt ;
  389. //保存结果
  390. firstAvailablePlateComb.resize(capArr.size());
  391. for(int it = 0 ; it < capArr.size(); ++ it ) {
  392. firstAvailablePlateComb[it]._plateId = pidArr[it] ;
  393. firstAvailablePlateComb[it]._layerCnt = lyrArr[it] ;
  394. }
  395. }
  396. }
  397. //计算每个类型的层数
  398. if( difficulty== FILLGLOBALCONFIG_DIFFCULTY_NORM ) {
  399. //全部两层
  400. vector<int> pidArr ;
  401. vector<int> capArr ;
  402. vector<int> lyrArr ;
  403. for(int it=0;it<ibig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(2); }
  404. for(int it=0;it<imid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(2); }
  405. for(int it=0;it<isml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(2); }
  406. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  407. if(!lyrOk) continue;
  408. //保存结果
  409. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  410. for(int it = 0 ; it < capArr.size(); ++ it ) {
  411. tres[it]._plateId = pidArr[it] ;
  412. tres[it]._layerCnt = lyrArr[it] ;
  413. }
  414. possibleResults.push_back(tres) ;
  415. }else if( difficulty==FILLGLOBALCONFIG_DIFFCULTY_HARD2 ) {
  416. //全部3层
  417. vector<int> pidArr ;
  418. vector<int> capArr ;
  419. vector<int> lyrArr ;
  420. for(int it=0;it<ibig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back(3); }
  421. for(int it=0;it<imid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back(3); }
  422. for(int it=0;it<isml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back(3); }
  423. //cout<<"debug big "<<ibig<<" mid "<<imid<<" sml "<<isml<<endl;
  424. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  425. if(!lyrOk) continue;
  426. //保存结果
  427. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  428. for(int it = 0 ; it < capArr.size(); ++ it ) {
  429. tres[it]._plateId = pidArr[it] ;
  430. tres[it]._layerCnt = lyrArr[it] ;
  431. }
  432. possibleResults.push_back(tres) ;
  433. }else{
  434. //50% 2层, 50% 3层
  435. vector<int> pidArr ;
  436. vector<int> capArr ;
  437. vector<int> lyrArr ;
  438. for(int it=0;it<ibig;++it) { pidArr.push_back(bigPlate->_id);capArr.push_back(bigPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  439. for(int it=0;it<imid;++it) { pidArr.push_back(midPlate->_id);capArr.push_back(midPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  440. for(int it=0;it<isml;++it) { pidArr.push_back(smlPlate->_id);capArr.push_back(smlPlate->_bigJewCap*4); lyrArr.push_back((rand()%2==0)?2:3); }
  441. bool lyrOk = isJustFilledAll2(capArr, lyrArr, totEquvSmallJewelCnt) ;
  442. if(!lyrOk) continue;
  443. //保存结果
  444. vector<LevelGenerate::PlateIdAndLayerCnt> tres(capArr.size()) ;
  445. for(int it = 0 ; it < capArr.size(); ++ it ) {
  446. tres[it]._plateId = pidArr[it] ;
  447. tres[it]._layerCnt = lyrArr[it] ;
  448. }
  449. possibleResults.push_back(tres) ;
  450. }
  451. }
  452. }
  453. }
  454. //随机挑选一个结果返回
  455. if( possibleResults.size()> 0 ) {
  456. int randindex = rand() % possibleResults.size();
  457. results = possibleResults[randindex] ;
  458. }else {
  459. results = firstAvailablePlateComb ;
  460. }
  461. return results ;
  462. }
  463. //随机排序数组索引值
  464. vector<int> LevelGenerate::randIndices(const int count0 ) {
  465. int count = count0 ;
  466. vector<int> indices ;
  467. unordered_map<int, bool> dict ;
  468. while( count > 0 ) {
  469. int r = rand()%count0;
  470. if( dict.find(r) == dict.end() ) {
  471. dict[r] = true ;
  472. indices.push_back(r) ;
  473. count-- ;
  474. }
  475. }
  476. return indices;
  477. }
  478. bool LevelGenerate::isJustFilledAll2(
  479. const vector<int>& plateCaps,//每个盘子的容量
  480. const vector<int> eachPlateLyrCnt,//对应盘子的层数
  481. const int totSmlJewCnt )
  482. {
  483. assert(plateCaps.size()>0);
  484. assert(plateCaps.size()==eachPlateLyrCnt.size());
  485. int fillcnt1 = 0;
  486. for(int ip = 0 ;ip<plateCaps.size();++ip ) {
  487. fillcnt1 += plateCaps[ip] * eachPlateLyrCnt[ip] ;
  488. }
  489. if( fillcnt1 < totSmlJewCnt ) return false ;
  490. if( fillcnt1 == totSmlJewCnt ) return true ;
  491. for(int ip = 0 ; ip < plateCaps.size();++ip ) {
  492. int fillcnt2 = fillcnt1 - plateCaps[ip] ;
  493. if( fillcnt2 < totSmlJewCnt ) {
  494. return true ;
  495. }
  496. }
  497. return false ;
  498. }
  499. unordered_map<int, int> LevelGenerate::randPickJewels( const int needEquvSmlCnt,const float solvedPercent, unordered_map<int,tuple<int,int>>& jewStorages )
  500. {
  501. int needEquvSmlCnt2 = needEquvSmlCnt ;
  502. int storageEquvSmlCnt = 0 ;
  503. int storageJewTypeCnt = jewStorages.size() ;
  504. for(auto it=jewStorages.begin();it!=jewStorages.end();++it) storageEquvSmlCnt+= std::get<1>(it->second)*jewSz2SmlCnt(std::get<0>(it->second)) ;
  505. unordered_map<int, int> res ;
  506. const float solPer = fmax(0.0,fmin(solvedPercent,1.0f)) ;
  507. //首先尝试可解的百分比部分
  508. int solEqSmGrpCnt = needEquvSmlCnt*solPer/3 ;
  509. // 随机尝试solEqSmGrpCnt*2 次取宝石,每次取一组。
  510. int loopCnt=solEqSmGrpCnt*storageJewTypeCnt*10;//随机迭代数量加大10倍,避免有始终选不到的情况
  511. while( solEqSmGrpCnt> 0 && storageEquvSmlCnt>0 && loopCnt>0) {
  512. int randJewI = rand()%storageJewTypeCnt ;
  513. auto itjew = jewStorages.begin() ;
  514. std::advance(itjew, randJewI) ;
  515. int jid = itjew->first ;
  516. int storage1 = std::get<1>( itjew->second ) ;
  517. int oneGrpEqSmGrpCnt = jewSz2SmlCnt( std::get<0>(itjew->second) ) ;
  518. if( storage1>=3 && oneGrpEqSmGrpCnt<=solEqSmGrpCnt ) {
  519. //库存有,且满足所需可解组数
  520. std::get<1>(itjew->second)-=3 ;
  521. solEqSmGrpCnt -= oneGrpEqSmGrpCnt ;
  522. storageEquvSmlCnt -= oneGrpEqSmGrpCnt*3 ;
  523. needEquvSmlCnt2 -= oneGrpEqSmGrpCnt*3 ; //本次所需的剩余等效小宝石个数
  524. res[jid] += 3 ;
  525. }
  526. --loopCnt ;
  527. }
  528. //尝试剩余百分比和上一步剩余没有符合条件的个数
  529. loopCnt = needEquvSmlCnt2*storageJewTypeCnt ;
  530. int jindex = 0 ;
  531. while( loopCnt>0 && needEquvSmlCnt2> 0 && storageEquvSmlCnt>0 ) {
  532. auto itjew = jewStorages.begin() ;
  533. std::advance(itjew, jindex) ;
  534. int jid = itjew->first;
  535. int sz = std::get<0>(itjew->second) ;
  536. int stor = std::get<1>(itjew->second) ;
  537. int eqsmCnt = jewSz2SmlCnt(sz) ;
  538. if( stor>0 && eqsmCnt <= needEquvSmlCnt2 ) {
  539. std::get<1>(itjew->second)-=1 ;
  540. needEquvSmlCnt2-=eqsmCnt ;
  541. storageEquvSmlCnt-=eqsmCnt ;
  542. res[jid] += 1 ;
  543. }
  544. ++jindex ; if(jindex>=storageJewTypeCnt) jindex=0;//循环取之,直到本层所需全部取完
  545. --loopCnt;
  546. }
  547. return res ;
  548. }
  549. int LevelGenerate::jewSz2SmlCnt(int sz)
  550. {
  551. switch (sz) {
  552. case FILLGLOBALCONFIG_JEWELSIZE_LG:
  553. return 4;break;
  554. case FILLGLOBALCONFIG_JEWELSIZE_MD:
  555. return 2;break;
  556. default:
  557. return 1;break;
  558. }
  559. return 1;
  560. }
  561. vector<FillResult> LevelGenerate::fillPlateOneLayer(RandomGridFiller& filler,const int plateId, unordered_map<int,int>& jewStoragesForUse )
  562. {
  563. auto fgc = FillGlobalConfig::getInstance() ;
  564. vector<RandomGridFiller::JewelBox> jewBoxArray ;
  565. FillGlobalConfig::PlateItem* pl = FillGlobalConfig::getInstance()->getPlateItemById(plateId) ;
  566. int plateEqSmCap = pl->_bigJewCap*4 ;
  567. int jewTypeCnt = jewStoragesForUse.size() ;
  568. int eqSmStorageCnt = 0 ;
  569. for(auto it = jewStoragesForUse.begin();it!=jewStoragesForUse.end();++it) {
  570. int jewEqSmCnt = jewSz2SmlCnt( fgc->getJewelItemById(it->first)->_size );
  571. eqSmStorageCnt+=it->second*jewEqSmCnt ;
  572. }
  573. static int s_debug_cnt = 0;
  574. //cout<<"debug "<< s_debug_cnt << " storage eqsm count "<<eqSmStorageCnt<< " currplate cap "<< plateEqSmCap <<endl;
  575. unordered_map<int, int> jewForPlateMap ;// <jewId,Cnt>
  576. int loopCnt = plateEqSmCap*jewTypeCnt*4;// 随机数量多给4倍
  577. while(plateEqSmCap>0 && eqSmStorageCnt>0 && loopCnt>0 ) {
  578. int radv = rand() % jewTypeCnt ;
  579. auto it = jewStoragesForUse.begin() ;
  580. std::advance(it, radv) ;
  581. int jewEqSmCnt = jewSz2SmlCnt( fgc->getJewelItemById(it->first)->_size );
  582. if( it->second>0 ) {
  583. if( jewEqSmCnt <= plateEqSmCap ) {
  584. plateEqSmCap -= jewEqSmCnt ;
  585. it->second -=1 ;
  586. eqSmStorageCnt -= jewEqSmCnt ;
  587. jewForPlateMap[it->first] += 1 ;
  588. if( it->second== 0 ) {
  589. jewStoragesForUse.erase(it) ;
  590. jewTypeCnt-- ;
  591. }
  592. }
  593. }else{
  594. jewStoragesForUse.erase(it) ;
  595. jewTypeCnt-- ;
  596. }
  597. --loopCnt ;
  598. }
  599. //cout<<"debug "<< s_debug_cnt ++ <<" storage eqsm count "<<eqSmStorageCnt<< " currplate cap "<< plateEqSmCap <<endl;
  600. for(auto it = jewForPlateMap.begin();it!=jewForPlateMap.end();++it) {
  601. RandomGridFiller::JewelBox jbox ;
  602. jbox._cnt = it->second ;
  603. jbox._jewelTypeId = it->first ;
  604. jbox._width = fgc->getJewelItemById(it->first)->_bbWidth ;
  605. jbox._height = fgc->getJewelItemById(it->first)->_bbHeight ;
  606. jewBoxArray.push_back(jbox) ;
  607. }
  608. vector<FillResult> fillresults ;
  609. filler.fill(jewBoxArray, pl->_blx, pl->_bly, pl->_trx, pl->_try, fillresults) ;
  610. return fillresults ;
  611. }
  612. unordered_map<int, int> LevelGenerate::findUnfilledInStorages( unordered_map<int,tuple<int,int>>& jewStorages)
  613. {
  614. unordered_map<int, int> res ;
  615. for(auto itj = jewStorages.begin();itj!=jewStorages.end();++itj) {
  616. int stor = std::get<1>(itj->second) ;
  617. int jid = itj->first ;
  618. if( stor>0 ) {
  619. res[jid] = stor ;
  620. std::get<1>(itj->second) = 0 ;
  621. }
  622. }
  623. return res ;
  624. }
  625. void LevelGenerate::regroupPlateLyrFillResults( vector<tuple<int,int,vector<FillResult>>>& pidlyrFillArray, vector<tuple<int,vector<vector<FillResult>>>>& results )
  626. {
  627. int currLayer = 1 ;//从底层到顶层构建结果数据结构
  628. int num = pidlyrFillArray.size() ;
  629. int nloop = num ;
  630. while( nloop > 0 ) {
  631. //cout<<"regroup for layer "<<currLayer<<endl;
  632. for( auto itlyr = pidlyrFillArray.begin(); itlyr!=pidlyrFillArray.end(); ++ itlyr ) {
  633. int pid = std::get<0>( *itlyr ) ;
  634. int lyr = std::get<1>( *itlyr ) ;
  635. vector<FillResult>& frs = std::get<2>(*itlyr) ;
  636. if(pid>=0) {
  637. if( lyr==currLayer ) {
  638. if( lyr==1 ) {
  639. vector<vector<FillResult>> frArr = {frs} ;
  640. results.push_back( {pid, frArr } ) ;
  641. num-- ;
  642. std::get<0>( *itlyr ) = -1 ;
  643. }else {
  644. for(auto itr = results.begin();itr!=results.end();++itr ) {
  645. int pidr = std::get<0>(*itr) ;
  646. vector<vector<FillResult>>& frArr = std::get<1>(*itr) ;
  647. if( pidr == pid && frArr.size()==currLayer-1 ) {
  648. frArr.push_back(frs);
  649. num-- ;
  650. std::get<0>( *itlyr ) = -1 ;
  651. break ;
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. currLayer++ ;
  659. if( num==0 ) break ;
  660. nloop--;
  661. }
  662. //cout<<"last plate lyr num "<<num<<endl;
  663. }
  664. void LevelGenerate::placePlates(const bool movable, const vector<int>& plateIdArr, vector<ContourData::Point>& resultPlateCenterPointArr )
  665. {
  666. GridPositionTool gpt ;
  667. vector<vector<int>> resPosis ;
  668. gpt.solve(movable, plateIdArr, resPosis) ;
  669. for(int ip=0;ip<resPosis.size();++ip ) {
  670. resultPlateCenterPointArr.push_back( { (float)resPosis[ip][0] , (float)resPosis[ip][1]} ) ;
  671. }
  672. }