CRmgTemplateZone.cpp 15 KB

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