CMap.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "StdInc.h"
  2. #include "CMap.h"
  3. #include "../CArtHandler.h"
  4. #include "../VCMI_Lib.h"
  5. #include "../CTownHandler.h"
  6. #include "../CHeroHandler.h"
  7. #include "../CDefObjInfoHandler.h"
  8. #include "../CSpellHandler.h"
  9. #include "CMapEditManager.h"
  10. SHeroName::SHeroName() : heroId(-1)
  11. {
  12. }
  13. PlayerInfo::PlayerInfo(): canHumanPlay(false), canComputerPlay(false),
  14. aiTactic(EAiTactic::RANDOM), isFactionRandom(false), mainCustomHeroPortrait(-1), mainCustomHeroId(-1), hasMainTown(false),
  15. generateHeroAtMainTown(false), team(255), hasRandomHero(false), /* following are unused */ generateHero(false), p7(0), powerPlaceholders(-1)
  16. {
  17. auto allowed = VLC->townh->getDefaultAllowed();
  18. for (size_t i=0; i<allowed.size(); i++)
  19. if (allowed[i])
  20. allowedFactions.insert(i);
  21. }
  22. si8 PlayerInfo::defaultCastle() const
  23. {
  24. if(allowedFactions.size() == 1 || !isFactionRandom)
  25. {
  26. // faction can't be chosen - set to first that is marked as allowed
  27. assert(!allowedFactions.empty());
  28. return *allowedFactions.begin();
  29. }
  30. // set to random
  31. return -1;
  32. }
  33. si8 PlayerInfo::defaultHero() const
  34. {
  35. // we will generate hero in front of main town
  36. if((generateHeroAtMainTown && hasMainTown) || hasRandomHero)
  37. {
  38. //random hero
  39. return -1;
  40. }
  41. return -2;
  42. }
  43. bool PlayerInfo::canAnyonePlay() const
  44. {
  45. return canHumanPlay || canComputerPlay;
  46. }
  47. bool PlayerInfo::hasCustomMainHero() const
  48. {
  49. return !mainCustomHeroName.empty() && mainCustomHeroPortrait != -1;
  50. }
  51. LossCondition::LossCondition() : typeOfLossCon(ELossConditionType::LOSSSTANDARD),
  52. pos(int3(-1, -1, -1)), timeLimit(-1), obj(nullptr)
  53. {
  54. }
  55. VictoryCondition::VictoryCondition() : condition(EVictoryConditionType::WINSTANDARD),
  56. allowNormalVictory(false), appliesToAI(false), pos(int3(-1, -1, -1)), objectId(0),
  57. count(0), obj(nullptr)
  58. {
  59. }
  60. DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
  61. {
  62. }
  63. CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
  64. firstOccurence(0), nextOccurence(0)
  65. {
  66. }
  67. bool CMapEvent::earlierThan(const CMapEvent & other) const
  68. {
  69. return firstOccurence < other.firstOccurence;
  70. }
  71. bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
  72. {
  73. return firstOccurence <= other.firstOccurence;
  74. }
  75. CCastleEvent::CCastleEvent() : town(nullptr)
  76. {
  77. }
  78. TerrainTile::TerrainTile() : terType(ETerrainType::BORDER), terView(0), riverType(ERiverType::NO_RIVER),
  79. riverDir(0), roadType(ERoadType::NO_ROAD), roadDir(0), extTileFlags(0), visitable(false),
  80. blocked(false)
  81. {
  82. }
  83. bool TerrainTile::entrableTerrain(const TerrainTile * from /*= nullptr*/) const
  84. {
  85. return entrableTerrain(from ? from->terType != ETerrainType::WATER : true, from ? from->terType == ETerrainType::WATER : true);
  86. }
  87. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  88. {
  89. return terType != ETerrainType::ROCK
  90. && ((allowSea && terType == ETerrainType::WATER) || (allowLand && terType != ETerrainType::WATER));
  91. }
  92. bool TerrainTile::isClear(const TerrainTile *from /*= nullptr*/) const
  93. {
  94. return entrableTerrain(from) && !blocked;
  95. }
  96. int TerrainTile::topVisitableId() const
  97. {
  98. return visitableObjects.size() ? visitableObjects.back()->ID : -1;
  99. }
  100. bool TerrainTile::isCoastal() const
  101. {
  102. return extTileFlags & 64;
  103. }
  104. bool TerrainTile::hasFavourableWinds() const
  105. {
  106. return extTileFlags & 128;
  107. }
  108. bool TerrainTile::isWater() const
  109. {
  110. return terType == ETerrainType::WATER;
  111. }
  112. const int CMapHeader::MAP_SIZE_SMALL = 36;
  113. const int CMapHeader::MAP_SIZE_MIDDLE = 72;
  114. const int CMapHeader::MAP_SIZE_LARGE = 108;
  115. const int CMapHeader::MAP_SIZE_XLARGE = 144;
  116. CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
  117. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  118. {
  119. allowedHeroes = VLC->heroh->getDefaultAllowed();
  120. players.resize(PlayerColor::PLAYER_LIMIT_I);
  121. }
  122. CMapHeader::~CMapHeader()
  123. {
  124. }
  125. CMap::CMap() : checksum(0), grailPos(-1, -1, -1), grailRadious(0), terrain(nullptr)
  126. {
  127. allHeroes.resize(allowedHeroes.size());
  128. allowedAbilities = VLC->heroh->getDefaultAllowedAbilities();
  129. allowedArtifact = VLC->arth->getDefaultAllowed();
  130. allowedSpell = VLC->spellh->getDefaultAllowed();
  131. }
  132. CMap::~CMap()
  133. {
  134. if(terrain)
  135. {
  136. for(int ii=0;ii<width;ii++)
  137. {
  138. for(int jj=0;jj<height;jj++)
  139. delete [] terrain[ii][jj];
  140. delete [] terrain[ii];
  141. }
  142. delete [] terrain;
  143. }
  144. }
  145. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  146. {
  147. for(int fx=0; fx<8; ++fx)
  148. {
  149. for(int fy=0; fy<6; ++fy)
  150. {
  151. int xVal = obj->pos.x + fx - 7;
  152. int yVal = obj->pos.y + fy - 5;
  153. int zVal = obj->pos.z;
  154. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  155. {
  156. TerrainTile & curt = terrain[xVal][yVal][zVal];
  157. if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  158. {
  159. curt.visitableObjects -= obj;
  160. curt.visitable = curt.visitableObjects.size();
  161. }
  162. if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  163. {
  164. curt.blockingObjects -= obj;
  165. curt.blocked = curt.blockingObjects.size();
  166. }
  167. }
  168. }
  169. }
  170. }
  171. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  172. {
  173. for(int fx=0; fx<8; ++fx)
  174. {
  175. for(int fy=0; fy<6; ++fy)
  176. {
  177. int xVal = obj->pos.x + fx - 7;
  178. int yVal = obj->pos.y + fy - 5;
  179. int zVal = obj->pos.z;
  180. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  181. {
  182. TerrainTile & curt = terrain[xVal][yVal][zVal];
  183. if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  184. {
  185. curt.visitableObjects.push_back(obj);
  186. curt.visitable = true;
  187. }
  188. if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  189. {
  190. curt.blockingObjects.push_back(obj);
  191. curt.blocked = true;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. CGHeroInstance * CMap::getHero(int heroID)
  198. {
  199. for(auto & elem : heroesOnMap)
  200. if(elem->subID == heroID)
  201. return elem;
  202. return nullptr;
  203. }
  204. bool CMap::isInTheMap(const int3 & pos) const
  205. {
  206. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  207. || pos.z > (twoLevel ? 1 : 0))
  208. {
  209. return false;
  210. }
  211. else
  212. {
  213. return true;
  214. }
  215. }
  216. TerrainTile & CMap::getTile(const int3 & tile)
  217. {
  218. assert(isInTheMap(tile));
  219. return terrain[tile.x][tile.y][tile.z];
  220. }
  221. const TerrainTile & CMap::getTile(const int3 & tile) const
  222. {
  223. assert(isInTheMap(tile));
  224. return terrain[tile.x][tile.y][tile.z];
  225. }
  226. bool CMap::isWaterTile(const int3 &pos) const
  227. {
  228. return isInTheMap(pos) && getTile(pos).terType == ETerrainType::WATER;
  229. }
  230. const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, bool lookForHero)
  231. {
  232. const std::vector<CGObjectInstance *> & objs = getTile(pos).visitableObjects;
  233. assert(objs.size());
  234. if(objs.size() > 1 && lookForHero && objs.front()->ID != Obj::HERO)
  235. {
  236. assert(objs.back()->ID == Obj::HERO);
  237. return objs.back();
  238. }
  239. else
  240. return objs.front();
  241. }
  242. void CMap::checkForObjectives()
  243. {
  244. if(isInTheMap(victoryCondition.pos))
  245. {
  246. victoryCondition.obj = getObjectiveObjectFrom(victoryCondition.pos, victoryCondition.condition == EVictoryConditionType::BEATHERO);
  247. }
  248. if(isInTheMap(lossCondition.pos))
  249. {
  250. lossCondition.obj = getObjectiveObjectFrom(lossCondition.pos, lossCondition.typeOfLossCon == ELossConditionType::LOSSHERO);
  251. }
  252. }
  253. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  254. {
  255. art->id = ArtifactInstanceID(artInstances.size());
  256. artInstances.push_back(art);
  257. }
  258. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  259. {
  260. assert(artInstances[art->id.getNum()] == art);
  261. artInstances[art->id.getNum()].dellNull();
  262. }
  263. void CMap::addQuest(CGObjectInstance * quest)
  264. {
  265. auto q = dynamic_cast<IQuestObject *>(quest);
  266. q->quest->qid = quests.size();
  267. quests.push_back(q->quest);
  268. }
  269. void CMap::initTerrain()
  270. {
  271. terrain = new TerrainTile**[width];
  272. for(int i = 0; i < width; ++i)
  273. {
  274. terrain[i] = new TerrainTile*[height];
  275. for(int j = 0; j < height; ++j)
  276. {
  277. terrain[i][j] = new TerrainTile[twoLevel ? 2 : 1];
  278. }
  279. }
  280. }
  281. CMapEditManager * CMap::getEditManager()
  282. {
  283. if(!editManager) editManager = make_unique<CMapEditManager>(this);
  284. return editManager.get();
  285. }