CRmgTemplateZone.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * CRmgTemplateZone.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CRmgTemplateZone.h"
  12. #include "../mapping/CMapEditManager.h"
  13. #include "../mapping/CMap.h"
  14. #include "../VCMI_Lib.h"
  15. #include "../CTownHandler.h"
  16. class CMap;
  17. class CMapEditManager;
  18. CRmgTemplateZone::CTownInfo::CTownInfo() : townCount(0), castleCount(0), townDensity(0), castleDensity(0)
  19. {
  20. }
  21. int CRmgTemplateZone::CTownInfo::getTownCount() const
  22. {
  23. return townCount;
  24. }
  25. void CRmgTemplateZone::CTownInfo::setTownCount(int value)
  26. {
  27. if(value < 0)
  28. throw rmgException("Negative value for town count not allowed.");
  29. townCount = value;
  30. }
  31. int CRmgTemplateZone::CTownInfo::getCastleCount() const
  32. {
  33. return castleCount;
  34. }
  35. void CRmgTemplateZone::CTownInfo::setCastleCount(int value)
  36. {
  37. if(value < 0)
  38. throw rmgException("Negative value for castle count not allowed.");
  39. castleCount = value;
  40. }
  41. int CRmgTemplateZone::CTownInfo::getTownDensity() const
  42. {
  43. return townDensity;
  44. }
  45. void CRmgTemplateZone::CTownInfo::setTownDensity(int value)
  46. {
  47. if(value < 0)
  48. throw rmgException("Negative value for town density not allowed.");
  49. townDensity = value;
  50. }
  51. int CRmgTemplateZone::CTownInfo::getCastleDensity() const
  52. {
  53. return castleDensity;
  54. }
  55. void CRmgTemplateZone::CTownInfo::setCastleDensity(int value)
  56. {
  57. if(value < 0)
  58. throw rmgException("Negative value for castle density not allowed.");
  59. castleDensity = value;
  60. }
  61. CRmgTemplateZone::CTileInfo::CTileInfo():nearestObjectDistance(INT_MAX), obstacle(false), occupied(false), terrain(ETerrainType::WRONG)
  62. {
  63. }
  64. int CRmgTemplateZone::CTileInfo::getNearestObjectDistance() const
  65. {
  66. return nearestObjectDistance;
  67. }
  68. void CRmgTemplateZone::CTileInfo::setNearestObjectDistance(int value)
  69. {
  70. if(value < 0)
  71. throw rmgException(boost::to_string(boost::format("Negative value %d for nearest object distance not allowed.") %value));
  72. nearestObjectDistance = value;
  73. }
  74. bool CRmgTemplateZone::CTileInfo::isObstacle() const
  75. {
  76. return obstacle;
  77. }
  78. void CRmgTemplateZone::CTileInfo::setObstacle(bool value)
  79. {
  80. obstacle = value;
  81. }
  82. bool CRmgTemplateZone::CTileInfo::isOccupied() const
  83. {
  84. return occupied;
  85. }
  86. void CRmgTemplateZone::CTileInfo::setOccupied(bool value)
  87. {
  88. occupied = value;
  89. }
  90. ETerrainType CRmgTemplateZone::CTileInfo::getTerrainType() const
  91. {
  92. return terrain;
  93. }
  94. void CRmgTemplateZone::CTileInfo::setTerrainType(ETerrainType value)
  95. {
  96. terrain = value;
  97. }
  98. CRmgTemplateZone::CRmgTemplateZone() : id(0), type(ETemplateZoneType::PLAYER_START), size(1),
  99. townsAreSameType(false), matchTerrainToTown(true)
  100. {
  101. townTypes = getDefaultTownTypes();
  102. terrainTypes = getDefaultTerrainTypes();
  103. }
  104. TRmgTemplateZoneId CRmgTemplateZone::getId() const
  105. {
  106. return id;
  107. }
  108. void CRmgTemplateZone::setId(TRmgTemplateZoneId value)
  109. {
  110. if(value <= 0)
  111. throw rmgException(boost::to_string(boost::format("Zone %d id should be greater than 0.") %id));
  112. id = value;
  113. }
  114. ETemplateZoneType::ETemplateZoneType CRmgTemplateZone::getType() const
  115. {
  116. return type;
  117. }
  118. void CRmgTemplateZone::setType(ETemplateZoneType::ETemplateZoneType value)
  119. {
  120. type = value;
  121. }
  122. int CRmgTemplateZone::getSize() const
  123. {
  124. return size;
  125. }
  126. void CRmgTemplateZone::setSize(int value)
  127. {
  128. if(value <= 0)
  129. throw rmgException(boost::to_string(boost::format("Zone %d size needs to be greater than 0.") % id));
  130. size = value;
  131. }
  132. boost::optional<int> CRmgTemplateZone::getOwner() const
  133. {
  134. return owner;
  135. }
  136. void CRmgTemplateZone::setOwner(boost::optional<int> value)
  137. {
  138. if(!(*value >= 0 && *value <= PlayerColor::PLAYER_LIMIT_I))
  139. throw rmgException(boost::to_string(boost::format ("Owner of zone %d has to be in range 0 to max player count.") %id));
  140. owner = value;
  141. }
  142. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getPlayerTowns() const
  143. {
  144. return playerTowns;
  145. }
  146. void CRmgTemplateZone::setPlayerTowns(const CTownInfo & value)
  147. {
  148. playerTowns = value;
  149. }
  150. const CRmgTemplateZone::CTownInfo & CRmgTemplateZone::getNeutralTowns() const
  151. {
  152. return neutralTowns;
  153. }
  154. void CRmgTemplateZone::setNeutralTowns(const CTownInfo & value)
  155. {
  156. neutralTowns = value;
  157. }
  158. bool CRmgTemplateZone::getTownsAreSameType() const
  159. {
  160. return townsAreSameType;
  161. }
  162. void CRmgTemplateZone::setTownsAreSameType(bool value)
  163. {
  164. townsAreSameType = value;
  165. }
  166. const std::set<TFaction> & CRmgTemplateZone::getTownTypes() const
  167. {
  168. return townTypes;
  169. }
  170. void CRmgTemplateZone::setTownTypes(const std::set<TFaction> & value)
  171. {
  172. townTypes = value;
  173. }
  174. std::set<TFaction> CRmgTemplateZone::getDefaultTownTypes() const
  175. {
  176. std::set<TFaction> defaultTowns;
  177. auto towns = VLC->townh->getDefaultAllowed();
  178. for(int i = 0; i < towns.size(); ++i)
  179. {
  180. if(towns[i]) defaultTowns.insert(i);
  181. }
  182. return defaultTowns;
  183. }
  184. bool CRmgTemplateZone::getMatchTerrainToTown() const
  185. {
  186. return matchTerrainToTown;
  187. }
  188. void CRmgTemplateZone::setMatchTerrainToTown(bool value)
  189. {
  190. matchTerrainToTown = value;
  191. }
  192. const std::set<ETerrainType> & CRmgTemplateZone::getTerrainTypes() const
  193. {
  194. return terrainTypes;
  195. }
  196. void CRmgTemplateZone::setTerrainTypes(const std::set<ETerrainType> & value)
  197. {
  198. assert(value.find(ETerrainType::WRONG) == value.end() && value.find(ETerrainType::BORDER) == value.end() &&
  199. value.find(ETerrainType::WATER) == value.end() && value.find(ETerrainType::ROCK) == value.end());
  200. terrainTypes = value;
  201. }
  202. std::set<ETerrainType> CRmgTemplateZone::getDefaultTerrainTypes() const
  203. {
  204. std::set<ETerrainType> terTypes;
  205. static const ETerrainType::EETerrainType allowedTerTypes[] = { ETerrainType::DIRT, ETerrainType::SAND, ETerrainType::GRASS, ETerrainType::SNOW,
  206. ETerrainType::SWAMP, ETerrainType::ROUGH, ETerrainType::SUBTERRANEAN, ETerrainType::LAVA };
  207. for(auto & allowedTerType : allowedTerTypes) terTypes.insert(allowedTerType);
  208. return terTypes;
  209. }
  210. boost::optional<TRmgTemplateZoneId> CRmgTemplateZone::getTerrainTypeLikeZone() const
  211. {
  212. return terrainTypeLikeZone;
  213. }
  214. void CRmgTemplateZone::setTerrainTypeLikeZone(boost::optional<TRmgTemplateZoneId> value)
  215. {
  216. terrainTypeLikeZone = value;
  217. }
  218. boost::optional<TRmgTemplateZoneId> CRmgTemplateZone::getTownTypeLikeZone() const
  219. {
  220. return townTypeLikeZone;
  221. }
  222. void CRmgTemplateZone::setTownTypeLikeZone(boost::optional<TRmgTemplateZoneId> value)
  223. {
  224. townTypeLikeZone = value;
  225. }
  226. bool CRmgTemplateZone::pointIsIn(int x, int y)
  227. {
  228. int i, j;
  229. bool c = false;
  230. int nvert = shape.size();
  231. for (i = 0, j = nvert-1; i < nvert; j = i++) {
  232. if ( ((shape[i].y>y) != (shape[j].y>y)) &&
  233. (x < (shape[j].x-shape[i].x) * (y-shape[i].y) / (shape[j].y-shape[i].y) + shape[i].x) )
  234. c = !c;
  235. }
  236. return c;
  237. }
  238. void CRmgTemplateZone::setShape(std::vector<int3> shape)
  239. {
  240. int z = -1;
  241. si32 minx = INT_MAX;
  242. si32 maxx = -1;
  243. si32 miny = INT_MAX;
  244. si32 maxy = -1;
  245. for(auto &point : shape)
  246. {
  247. if (z == -1)
  248. z = point.z;
  249. if (point.z != z)
  250. throw rmgException("Zone shape points should lie on same z.");
  251. minx = std::min(minx, point.x);
  252. maxx = std::max(maxx, point.x);
  253. miny = std::min(miny, point.y);
  254. maxy = std::max(maxy, point.y);
  255. }
  256. this->shape = shape;
  257. for(int x = minx; x <= maxx; ++x)
  258. {
  259. for(int y = miny; y <= maxy; ++y)
  260. {
  261. if (pointIsIn(x, y))
  262. {
  263. tileinfo[int3(x,y,z)] = CTileInfo();
  264. }
  265. }
  266. }
  267. }
  268. int3 CRmgTemplateZone::getCenter()
  269. {
  270. si32 cx = 0;
  271. si32 cy = 0;
  272. si32 area = 0;
  273. si32 sz = shape.size();
  274. //include last->first too
  275. for(si32 i = 0, j = sz-1; i < sz; j = i++) {
  276. si32 sf = (shape[i].x * shape[j].y - shape[j].x * shape[i].y);
  277. cx += (shape[i].x + shape[j].x) * sf;
  278. cy += (shape[i].y + shape[j].y) * sf;
  279. area += sf;
  280. }
  281. area /= 2;
  282. return int3(std::abs(cx/area/6), std::abs(cy/area/6), shape[0].z);
  283. }
  284. bool CRmgTemplateZone::fill(CMapGenerator* gen)
  285. {
  286. std::vector<CGObjectInstance*> required_objects;
  287. if ((type == ETemplateZoneType::CPU_START) || (type == ETemplateZoneType::PLAYER_START))
  288. {
  289. logGlobal->infoStream() << "Preparing playing zone";
  290. int player_id = *owner - 1;
  291. auto & playerInfo = gen->map->players[player_id];
  292. if (playerInfo.canAnyonePlay())
  293. {
  294. PlayerColor player(player_id);
  295. auto town = new CGTownInstance();
  296. town->ID = Obj::TOWN;
  297. int townId = gen->mapGenOptions->getPlayersSettings().find(player)->second.getStartingTown();
  298. if(townId == CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  299. townId = *RandomGeneratorUtil::nextItem(VLC->townh->getAllowedFactions(), gen->rand); // all possible towns, skip neutral
  300. town->subID = townId;
  301. town->tempOwner = player;
  302. town->builtBuildings.insert(BuildingID::FORT);
  303. town->builtBuildings.insert(BuildingID::DEFAULT);
  304. placeObject(gen, town, getCenter());
  305. logGlobal->infoStream() << "Placed object";
  306. logGlobal->infoStream() << "Fill player info " << player_id;
  307. auto & playerInfo = gen->map->players[player_id];
  308. // Update player info
  309. playerInfo.allowedFactions.clear();
  310. playerInfo.allowedFactions.insert(town->subID);
  311. playerInfo.hasMainTown = true;
  312. playerInfo.posOfMainTown = town->pos - int3(2, 0, 0);
  313. playerInfo.generateHeroAtMainTown = true;
  314. //required_objects.push_back(town);
  315. std::vector<Res::ERes> required_mines;
  316. required_mines.push_back(Res::ERes::WOOD);
  317. required_mines.push_back(Res::ERes::ORE);
  318. for(const auto res : required_mines)
  319. {
  320. auto mine = new CGMine();
  321. mine->ID = Obj::MINE;
  322. mine->subID = static_cast<si32>(res);
  323. mine->producedResource = res;
  324. mine->producedQuantity = mine->defaultResProduction();
  325. required_objects.push_back(mine);
  326. }
  327. }
  328. else
  329. {
  330. type = ETemplateZoneType::TREASURE;
  331. logGlobal->infoStream() << "Skipping this zone cause no player";
  332. }
  333. }
  334. logGlobal->infoStream() << "Creating required objects";
  335. for(const auto &obj : required_objects)
  336. {
  337. int3 pos;
  338. logGlobal->infoStream() << "Looking for place";
  339. if ( ! findPlaceForObject(gen, obj, 3, pos))
  340. {
  341. logGlobal->errorStream() << boost::format("Failed to fill zone %d due to lack of space") %id;
  342. //TODO CLEANUP!
  343. return false;
  344. }
  345. logGlobal->infoStream() << "Place found";
  346. placeObject(gen, obj, pos);
  347. logGlobal->infoStream() << "Placed object";
  348. }
  349. std::vector<CGObjectInstance*> guarded_objects;
  350. static auto res_gen = gen->rand.getIntRange(Res::ERes::WOOD, Res::ERes::GOLD);
  351. const double res_mindist = 5;
  352. do {
  353. auto obj = new CGResource();
  354. auto restype = static_cast<Res::ERes>(res_gen());
  355. obj->ID = Obj::RESOURCE;
  356. obj->subID = static_cast<si32>(restype);
  357. obj->amount = 0;
  358. int3 pos;
  359. if ( ! findPlaceForObject(gen, obj, res_mindist, pos))
  360. {
  361. delete obj;
  362. break;
  363. }
  364. placeObject(gen, obj, pos);
  365. if ((restype != Res::ERes::WOOD) && (restype != Res::ERes::ORE))
  366. {
  367. guarded_objects.push_back(obj);
  368. }
  369. } while(true);
  370. for(const auto &obj : guarded_objects)
  371. {
  372. if ( ! guardObject(gen, obj, 500))
  373. {
  374. //TODO, DEL obj from map
  375. }
  376. }
  377. auto sel = gen->editManager->getTerrainSelection();
  378. sel.clearSelection();
  379. for(auto it = tileinfo.begin(); it != tileinfo.end(); ++it)
  380. {
  381. if (it->second.isObstacle())
  382. {
  383. auto obj = new CGObjectInstance();
  384. obj->ID = static_cast<Obj>(130);
  385. obj->subID = 0;
  386. placeObject(gen, obj, it->first);
  387. }
  388. }
  389. logGlobal->infoStream() << boost::format("Filling %d with ROCK") % sel.getSelectedItems().size();
  390. //gen->editManager->drawTerrain(ETerrainType::ROCK, &gen->gen);
  391. logGlobal->infoStream() << boost::format ("Zone %d filled successfully") %id;
  392. return true;
  393. }
  394. bool CRmgTemplateZone::findPlaceForObject(CMapGenerator* gen, CGObjectInstance* obj, si32 min_dist, int3 &pos)
  395. {
  396. //si32 min_dist = sqrt(tileinfo.size()/density);
  397. int best_distance = 0;
  398. bool result = false;
  399. si32 w = gen->map->width;
  400. si32 h = gen->map->height;
  401. auto ow = obj->getWidth();
  402. auto oh = obj->getHeight();
  403. //logGlobal->infoStream() << boost::format("Min dist for density %f is %d") % density % min_dist;
  404. for(auto it = tileinfo.begin(); it != tileinfo.end(); ++it)
  405. {
  406. auto &ti = it->second;
  407. auto p = it->first;
  408. auto dist = ti.getNearestObjectDistance();
  409. //avoid borders
  410. if ((p.x < 3) || (w - p.x < 3) || (p.y < 3) || (h - p.y < 3))
  411. continue;
  412. if (!ti.isOccupied() && !ti.isObstacle() && (dist >= min_dist) && (dist > best_distance))
  413. {
  414. best_distance = dist;
  415. pos = p;
  416. result = true;
  417. }
  418. }
  419. return result;
  420. }
  421. void CRmgTemplateZone::checkAndPlaceObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  422. {
  423. object->pos = pos;
  424. if (!gen->map->isInTheMap(object->visitablePos()))
  425. throw rmgException(boost::to_string(boost::format("Visitable tile %s of object %d at %s is outside the map") % object->visitablePos() % object->id % object->pos()));
  426. for (auto tile : object->getBlockedPos())
  427. {
  428. if (!gen->map->isInTheMap(tile))
  429. throw rmgException(boost::to_string(boost::format("Tile %s of object %d at %s is outside the map") % tile() % object->id % object->pos()));
  430. }
  431. gen->editManager->insertObject(object, pos);
  432. logGlobal->infoStream() << boost::format ("Successfully inserted object (%d,%d) at pos %s") %object->ID %object->id %pos();
  433. }
  434. void CRmgTemplateZone::placeObject(CMapGenerator* gen, CGObjectInstance* object, const int3 &pos)
  435. {
  436. logGlobal->infoStream() << boost::format("Inserting object at %d %d") % pos.x % pos.y;
  437. checkAndPlaceObject (gen, object, pos);
  438. auto points = object->getBlockedPos();
  439. if (object->isVisitable())
  440. points.emplace(pos + object->getVisitableOffset());
  441. points.emplace(pos);
  442. for(auto const &p : points)
  443. {
  444. if (tileinfo.find(pos + p) != tileinfo.end())
  445. {
  446. tileinfo[pos + p].setOccupied(true);
  447. }
  448. }
  449. for(auto it = tileinfo.begin(); it != tileinfo.end(); ++it)
  450. {
  451. si32 d = pos.dist2d(it->first);
  452. it->second.setNearestObjectDistance(std::min(d, it->second.getNearestObjectDistance()));
  453. }
  454. }
  455. bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object, si32 str)
  456. {
  457. logGlobal->infoStream() << boost::format("Guard object at %d %d") % object->pos.x % object->pos.y;
  458. int3 visitable = object->pos + object->getVisitableOffset();
  459. std::vector<int3> tiles;
  460. for(int i = -1; i < 2; ++i)
  461. {
  462. for(int j = -1; j < 2; ++j)
  463. {
  464. auto it = tileinfo.find(visitable + int3(i, j, 0));
  465. if (it != tileinfo.end())
  466. {
  467. logGlobal->infoStream() << boost::format("Block at %d %d") % it->first.x % it->first.y;
  468. if ( ! it->second.isOccupied() && ! it->second.isObstacle())
  469. {
  470. tiles.push_back(it->first);
  471. it->second.setObstacle(true);
  472. }
  473. }
  474. }
  475. }
  476. if ( ! tiles.size())
  477. {
  478. logGlobal->infoStream() << "Failed";
  479. return false;
  480. }
  481. auto guard_tile = *std::next(tiles.begin(), gen->rand.nextInt(tiles.size()));
  482. tileinfo[guard_tile].setObstacle(false);
  483. auto guard = new CGCreature();
  484. guard->ID = Obj::RANDOM_MONSTER;
  485. guard->subID = 0;
  486. auto hlp = new CStackInstance();
  487. hlp->count = 10;
  488. //type will be set during initialization
  489. guard->putStack(SlotID(0), hlp);
  490. checkAndPlaceObject(gen, guard, guard_tile);
  491. return true;
  492. }