CMap.cpp 12 KB

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