2
0

CMap.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 /*= nullptr*/) 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 /*= nullptr*/) 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. allHeroes.resize(allowedHeroes.size());
  124. allowedAbilities = VLC->heroh->getDefaultAllowedAbilities();
  125. allowedArtifact = VLC->arth->getDefaultAllowed();
  126. allowedSpell = VLC->spellh->getDefaultAllowed();
  127. }
  128. CMap::~CMap()
  129. {
  130. if(terrain)
  131. {
  132. for(int ii=0;ii<width;ii++)
  133. {
  134. for(int jj=0;jj<height;jj++)
  135. delete [] terrain[ii][jj];
  136. delete [] terrain[ii];
  137. }
  138. delete [] terrain;
  139. }
  140. }
  141. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  142. {
  143. for(int fx=0; fx<8; ++fx)
  144. {
  145. for(int fy=0; fy<6; ++fy)
  146. {
  147. int xVal = obj->pos.x + fx - 7;
  148. int yVal = obj->pos.y + fy - 5;
  149. int zVal = obj->pos.z;
  150. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  151. {
  152. TerrainTile & curt = terrain[xVal][yVal][zVal];
  153. if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  154. {
  155. curt.visitableObjects -= obj;
  156. curt.visitable = curt.visitableObjects.size();
  157. }
  158. if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  159. {
  160. curt.blockingObjects -= obj;
  161. curt.blocked = curt.blockingObjects.size();
  162. }
  163. }
  164. }
  165. }
  166. }
  167. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  168. {
  169. for(int fx=0; fx<8; ++fx)
  170. {
  171. for(int fy=0; fy<6; ++fy)
  172. {
  173. int xVal = obj->pos.x + fx - 7;
  174. int yVal = obj->pos.y + fy - 5;
  175. int zVal = obj->pos.z;
  176. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  177. {
  178. TerrainTile & curt = terrain[xVal][yVal][zVal];
  179. if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  180. {
  181. curt.visitableObjects.push_back(obj);
  182. curt.visitable = true;
  183. }
  184. if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  185. {
  186. curt.blockingObjects.push_back(obj);
  187. curt.blocked = true;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. CGHeroInstance * CMap::getHero(int heroID)
  194. {
  195. for(auto & elem : heroesOnMap)
  196. if(elem->subID == heroID)
  197. return elem;
  198. return nullptr;
  199. }
  200. bool CMap::isInTheMap(const int3 & pos) const
  201. {
  202. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  203. || pos.z > (twoLevel ? 1 : 0))
  204. {
  205. return false;
  206. }
  207. else
  208. {
  209. return true;
  210. }
  211. }
  212. TerrainTile & CMap::getTile(const int3 & tile)
  213. {
  214. assert(isInTheMap(tile));
  215. return terrain[tile.x][tile.y][tile.z];
  216. }
  217. const TerrainTile & CMap::getTile(const int3 & tile) const
  218. {
  219. assert(isInTheMap(tile));
  220. return terrain[tile.x][tile.y][tile.z];
  221. }
  222. bool CMap::isWaterTile(const int3 &pos) const
  223. {
  224. return isInTheMap(pos) && getTile(pos).terType == ETerrainType::WATER;
  225. }
  226. const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, bool lookForHero)
  227. {
  228. const std::vector<CGObjectInstance *> & objs = getTile(pos).visitableObjects;
  229. assert(objs.size());
  230. if(objs.size() > 1 && lookForHero && objs.front()->ID != Obj::HERO)
  231. {
  232. assert(objs.back()->ID == Obj::HERO);
  233. return objs.back();
  234. }
  235. else
  236. return objs.front();
  237. }
  238. void CMap::checkForObjectives()
  239. {
  240. if(isInTheMap(victoryCondition.pos))
  241. {
  242. victoryCondition.obj = getObjectiveObjectFrom(victoryCondition.pos, victoryCondition.condition == EVictoryConditionType::BEATHERO);
  243. }
  244. if(isInTheMap(lossCondition.pos))
  245. {
  246. lossCondition.obj = getObjectiveObjectFrom(lossCondition.pos, lossCondition.typeOfLossCon == ELossConditionType::LOSSHERO);
  247. }
  248. }
  249. void CMap::addNewArtifactInstance(CArtifactInstance * art)
  250. {
  251. art->id = ArtifactInstanceID(artInstances.size());
  252. artInstances.push_back(art);
  253. }
  254. void CMap::eraseArtifactInstance(CArtifactInstance * art)
  255. {
  256. assert(artInstances[art->id.getNum()] == art);
  257. artInstances[art->id.getNum()].dellNull();
  258. }
  259. void CMap::addQuest(CGObjectInstance * quest)
  260. {
  261. auto q = dynamic_cast<IQuestObject *>(quest);
  262. q->quest->qid = quests.size();
  263. quests.push_back(q->quest);
  264. }
  265. void CMap::initTerrain()
  266. {
  267. terrain = new TerrainTile**[width];
  268. for(int i = 0; i < width; ++i)
  269. {
  270. terrain[i] = new TerrainTile*[height];
  271. for(int j = 0; j < height; ++j)
  272. {
  273. terrain[i][j] = new TerrainTile[twoLevel ? 2 : 1];
  274. }
  275. }
  276. }
  277. CMapEditManager * CMap::getEditManager()
  278. {
  279. if(!editManager) editManager = make_unique<CMapEditManager>(this);
  280. return editManager.get();
  281. }