CMap.cpp 7.9 KB

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