CMap.cpp 7.9 KB

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