CMap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "StdInc.h"
  2. #include "CMap.h"
  3. #include "../CArtHandler.h"
  4. #include "../VCMI_Lib.h"
  5. #include "../CCreatureHandler.h"
  6. #include "../CTownHandler.h"
  7. #include "../CHeroHandler.h"
  8. #include "../CDefObjInfoHandler.h"
  9. #include "../CGeneralTextHandler.h"
  10. #include "../CSpellHandler.h"
  11. #include "CMapEditManager.h"
  12. SHeroName::SHeroName() : heroId(-1)
  13. {
  14. }
  15. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  16. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  17. generateHeroAtMainTown(false), team(255), hasRandomHero(false), /* following are unused */ generateHero(false), p7(0), powerPlaceholders(-1)
  18. {
  19. auto allowed = VLC->townh->getDefaultAllowed();
  20. for (size_t i=0; i<allowed.size(); i++)
  21. if (allowed[i])
  22. allowedFactions.insert(i);
  23. }
  24. si8 PlayerInfo::defaultCastle() const
  25. {
  26. if(allowedFactions.size() == 1 || !isFactionRandom)
  27. {
  28. // faction can't be chosen - set to first that is marked as allowed
  29. assert(!allowedFactions.empty());
  30. return *allowedFactions.begin();
  31. }
  32. // set to random
  33. return -1;
  34. }
  35. si8 PlayerInfo::defaultHero() const
  36. {
  37. // we will generate hero in front of main town
  38. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  39. {
  40. //random hero
  41. return -1;
  42. }
  43. return -2;
  44. }
  45. bool PlayerInfo::canAnyonePlay() const
  46. {
  47. return canHumanPlay || canComputerPlay;
  48. }
  49. bool PlayerInfo::hasCustomMainHero() const
  50. {
  51. return !mainCustomHeroName.empty() && mainCustomHeroPortrait != -1;
  52. }
  53. EventCondition::EventCondition(EWinLoseType condition):
  54. object(nullptr),
  55. value(-1),
  56. objectType(-1),
  57. position(-1, -1, -1),
  58. condition(condition)
  59. {
  60. }
  61. DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
  62. {
  63. }
  64. CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
  65. firstOccurence(0), nextOccurence(0)
  66. {
  67. }
  68. bool CMapEvent::earlierThan(const CMapEvent & other) const
  69. {
  70. return firstOccurence < other.firstOccurence;
  71. }
  72. bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
  73. {
  74. return firstOccurence <= other.firstOccurence;
  75. }
  76. CCastleEvent::CCastleEvent() : town(nullptr)
  77. {
  78. }
  79. TerrainTile::TerrainTile() : terType(ETerrainType::BORDER), terView(0), riverType(ERiverType::NO_RIVER),
  80. riverDir(0), roadType(ERoadType::NO_ROAD), roadDir(0), extTileFlags(0), visitable(false),
  81. blocked(false)
  82. {
  83. }
  84. bool TerrainTile::entrableTerrain(const TerrainTile * from /*= nullptr*/) const
  85. {
  86. return entrableTerrain(from ? from->terType != ETerrainType::WATER : true, from ? from->terType == ETerrainType::WATER : true);
  87. }
  88. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  89. {
  90. return terType != ETerrainType::ROCK
  91. && ((allowSea && terType == ETerrainType::WATER) || (allowLand && terType != ETerrainType::WATER));
  92. }
  93. bool TerrainTile::isClear(const TerrainTile *from /*= nullptr*/) const
  94. {
  95. return entrableTerrain(from) && !blocked;
  96. }
  97. int TerrainTile::topVisitableId() const
  98. {
  99. return visitableObjects.size() ? visitableObjects.back()->ID : -1;
  100. }
  101. bool TerrainTile::isCoastal() const
  102. {
  103. return extTileFlags & 64;
  104. }
  105. bool TerrainTile::hasFavourableWinds() const
  106. {
  107. return extTileFlags & 128;
  108. }
  109. bool TerrainTile::isWater() const
  110. {
  111. return terType == ETerrainType::WATER;
  112. }
  113. const int CMapHeader::MAP_SIZE_SMALL = 36;
  114. const int CMapHeader::MAP_SIZE_MIDDLE = 72;
  115. const int CMapHeader::MAP_SIZE_LARGE = 108;
  116. const int CMapHeader::MAP_SIZE_XLARGE = 144;
  117. void CMapHeader::setupEvents()
  118. {
  119. EventCondition victoryCondition(EventCondition::STANDARD_WIN);
  120. EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);
  121. defeatCondition.value = 7;
  122. //Victory condition - defeat all
  123. TriggeredEvent standardVictory;
  124. standardVictory.effect.type = EventEffect::VICTORY;
  125. standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];
  126. standardVictory.identifier = "standardVictory";
  127. standardVictory.description = ""; // TODO: display in quest window
  128. standardVictory.onFulfill = VLC->generaltexth->allTexts[659];
  129. standardVictory.trigger = EventExpression(victoryCondition);
  130. //Loss condition - 7 days without town
  131. TriggeredEvent standardDefeat;
  132. standardDefeat.effect.type = EventEffect::DEFEAT;
  133. standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];
  134. standardDefeat.identifier = "standardDefeat";
  135. standardDefeat.description = ""; // TODO: display in quest window
  136. standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];
  137. standardDefeat.trigger = EventExpression(defeatCondition);
  138. triggeredEvents.push_back(standardVictory);
  139. triggeredEvents.push_back(standardDefeat);
  140. victoryIconIndex = 11;
  141. victoryMessage = VLC->generaltexth->victoryConditions[0];
  142. defeatIconIndex = 3;
  143. defeatMessage = VLC->generaltexth->lossCondtions[0];
  144. }
  145. CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
  146. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  147. {
  148. setupEvents();
  149. allowedHeroes = VLC->heroh->getDefaultAllowed();
  150. players.resize(PlayerColor::PLAYER_LIMIT_I);
  151. }
  152. CMapHeader::~CMapHeader()
  153. {
  154. }
  155. CMap::CMap() : checksum(0), grailPos(-1, -1, -1), grailRadious(0), terrain(nullptr)
  156. {
  157. allHeroes.resize(allowedHeroes.size());
  158. allowedAbilities = VLC->heroh->getDefaultAllowedAbilities();
  159. allowedArtifact = VLC->arth->getDefaultAllowed();
  160. allowedSpell = VLC->spellh->getDefaultAllowed();
  161. }
  162. CMap::~CMap()
  163. {
  164. if(terrain)
  165. {
  166. for(int ii=0;ii<width;ii++)
  167. {
  168. for(int jj=0;jj<height;jj++)
  169. delete [] terrain[ii][jj];
  170. delete [] terrain[ii];
  171. }
  172. delete [] terrain;
  173. }
  174. }
  175. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  176. {
  177. for(int fx=0; fx<obj->getWidth(); ++fx)
  178. {
  179. for(int fy=0; fy<obj->getHeight(); ++fy)
  180. {
  181. int xVal = obj->pos.x - fx;
  182. int yVal = obj->pos.y - fy;
  183. int zVal = obj->pos.z;
  184. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  185. {
  186. TerrainTile & curt = terrain[xVal][yVal][zVal];
  187. if(total || obj->visitableAt(xVal, yVal))
  188. {
  189. curt.visitableObjects -= obj;
  190. curt.visitable = curt.visitableObjects.size();
  191. }
  192. if(total || obj->blockingAt(xVal, yVal))
  193. {
  194. curt.blockingObjects -= obj;
  195. curt.blocked = curt.blockingObjects.size();
  196. }
  197. }
  198. }
  199. }
  200. }
  201. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  202. {
  203. for(int fx=0; fx<obj->getWidth(); ++fx)
  204. {
  205. for(int fy=0; fy<obj->getHeight(); ++fy)
  206. {
  207. int xVal = obj->pos.x - fx;
  208. int yVal = obj->pos.y - fy;
  209. int zVal = obj->pos.z;
  210. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  211. {
  212. TerrainTile & curt = terrain[xVal][yVal][zVal];
  213. if( obj->visitableAt(xVal, yVal))
  214. {
  215. curt.visitableObjects.push_back(obj);
  216. curt.visitable = true;
  217. }
  218. if( obj->blockingAt(xVal, yVal))
  219. {
  220. curt.blockingObjects.push_back(obj);
  221. curt.blocked = true;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. CGHeroInstance * CMap::getHero(int heroID)
  228. {
  229. for(auto & elem : heroesOnMap)
  230. if(elem->subID == heroID)
  231. return elem;
  232. return nullptr;
  233. }
  234. bool CMap::isInTheMap(const int3 & pos) const
  235. {
  236. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  237. || pos.z > (twoLevel ? 1 : 0))
  238. {
  239. return false;
  240. }
  241. else
  242. {
  243. return true;
  244. }
  245. }
  246. TerrainTile & CMap::getTile(const int3 & tile)
  247. {
  248. assert(isInTheMap(tile));
  249. return terrain[tile.x][tile.y][tile.z];
  250. }
  251. const TerrainTile & CMap::getTile(const int3 & tile) const
  252. {
  253. assert(isInTheMap(tile));
  254. return terrain[tile.x][tile.y][tile.z];
  255. }
  256. bool CMap::isWaterTile(const int3 &pos) const
  257. {
  258. return isInTheMap(pos) && getTile(pos).terType == ETerrainType::WATER;
  259. }
  260. const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, Obj::EObj type)
  261. {
  262. for (CGObjectInstance * object : getTile(pos).visitableObjects)
  263. {
  264. if (object->ID == type)
  265. return object;
  266. }
  267. // There is weird bug because of which sometimes heroes will not be found properly despite having correct position
  268. // Try to workaround that and find closest object that we can use
  269. logGlobal->errorStream() << "Failed to find object of type " << int(type) << " at " << pos;
  270. logGlobal->errorStream() << "Will try to find closest matching object";
  271. CGObjectInstance * bestMatch = nullptr;
  272. for (CGObjectInstance * object : objects)
  273. {
  274. if (object->ID == type)
  275. {
  276. if (bestMatch == nullptr)
  277. bestMatch = object;
  278. else
  279. {
  280. if (object->pos.dist2d(pos) < bestMatch->pos.dist2d(pos))
  281. bestMatch = object;// closer than one we already found
  282. }
  283. }
  284. }
  285. assert(bestMatch != nullptr); // if this happens - victory conditions or map itself is very, very broken
  286. logGlobal->errorStream() << "Will use " << bestMatch->getHoverText() << " from " << bestMatch->pos;
  287. return bestMatch;
  288. }
  289. void CMap::checkForObjectives()
  290. {
  291. // NOTE: probably should be moved to MapFormatH3M.cpp
  292. for (TriggeredEvent & event : triggeredEvents)
  293. {
  294. auto patcher = [&](EventCondition & cond)
  295. {
  296. switch (cond.condition)
  297. {
  298. break; case EventCondition::HAVE_ARTIFACT:
  299. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->arth->artifacts[cond.objectType]->Name());
  300. break; case EventCondition::HAVE_CREATURES:
  301. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->creh->creatures[cond.objectType]->nameSing);
  302. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  303. break; case EventCondition::HAVE_RESOURCES:
  304. boost::algorithm::replace_first(event.onFulfill, "%s", VLC->generaltexth->restypes[cond.objectType]);
  305. boost::algorithm::replace_first(event.onFulfill, "%d", boost::lexical_cast<std::string>(cond.value));
  306. break; case EventCondition::HAVE_BUILDING:
  307. if (isInTheMap(cond.position))
  308. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  309. break; case EventCondition::CONTROL:
  310. if (isInTheMap(cond.position))
  311. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  312. if (cond.object)
  313. {
  314. const CGTownInstance *town = dynamic_cast<const CGTownInstance*>(cond.object);
  315. if (town)
  316. boost::algorithm::replace_first(event.onFulfill, "%s", town->name);
  317. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  318. if (hero)
  319. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  320. }
  321. break; case EventCondition::DESTROY:
  322. if (isInTheMap(cond.position))
  323. cond.object = getObjectiveObjectFrom(cond.position, Obj::EObj(cond.objectType));
  324. if (cond.object)
  325. {
  326. const CGHeroInstance *hero = dynamic_cast<const CGHeroInstance*>(cond.object);
  327. if (hero)
  328. boost::algorithm::replace_first(event.onFulfill, "%s", hero->name);
  329. }
  330. break; case EventCondition::TRANSPORT:
  331. cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
  332. //break; case EventCondition::DAYS_PASSED:
  333. //break; case EventCondition::IS_HUMAN:
  334. //break; case EventCondition::DAYS_WITHOUT_TOWN:
  335. //break; case EventCondition::STANDARD_WIN:
  336. }
  337. };
  338. event.trigger.forEach(patcher);
  339. }
  340. }
  341. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  342. {
  343. art->id = ArtifactInstanceID(artInstances.size());
  344. artInstances.push_back(art);
  345. }
  346. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  347. {
  348. assert(artInstances[art->id.getNum()] == art);
  349. artInstances[art->id.getNum()].dellNull();
  350. }
  351. void CMap::addQuest(CGObjectInstance * quest)
  352. {
  353. auto q = dynamic_cast<IQuestObject *>(quest);
  354. q->quest->qid = quests.size();
  355. quests.push_back(q->quest);
  356. }
  357. void CMap::initTerrain()
  358. {
  359. terrain = new TerrainTile**[width];
  360. for(int i = 0; i < width; ++i)
  361. {
  362. terrain[i] = new TerrainTile*[height];
  363. for(int j = 0; j < height; ++j)
  364. {
  365. terrain[i][j] = new TerrainTile[twoLevel ? 2 : 1];
  366. }
  367. }
  368. }
  369. CMapEditManager * CMap::getEditManager()
  370. {
  371. if(!editManager) editManager = make_unique<CMapEditManager>(this);
  372. return editManager.get();
  373. }