CMap.cpp 7.8 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. allowedFactions = VLC->townh->getDefaultAllowedFactions();
  18. }
  19. si8 PlayerInfo::defaultCastle() const
  20. {
  21. if(allowedFactions.size() == 1 || !isFactionRandom)
  22. {
  23. // faction can't be chosen - set to first that is marked as allowed
  24. assert(!allowedFactions.empty());
  25. return *allowedFactions.begin();
  26. }
  27. // set to random
  28. return -1;
  29. }
  30. si8 PlayerInfo::defaultHero() const
  31. {
  32. // we will generate hero in front of main town
  33. if((generateHeroAtMainTown && hasMainTown) || hasHero)
  34. {
  35. //random hero
  36. return -1;
  37. }
  38. return -2;
  39. }
  40. bool PlayerInfo::canAnyonePlay() const
  41. {
  42. return canHumanPlay || canComputerPlay;
  43. }
  44. LossCondition::LossCondition() : typeOfLossCon(ELossConditionType::LOSSSTANDARD),
  45. pos(int3(-1, -1, -1)), timeLimit(-1), obj(nullptr)
  46. {
  47. }
  48. VictoryCondition::VictoryCondition() : condition(EVictoryConditionType::WINSTANDARD),
  49. allowNormalVictory(false), appliesToAI(false), pos(int3(-1, -1, -1)), objectId(0),
  50. count(0), obj(nullptr)
  51. {
  52. }
  53. DisposedHero::DisposedHero() : heroId(0), portrait(255), players(0)
  54. {
  55. }
  56. CMapEvent::CMapEvent() : players(0), humanAffected(0), computerAffected(0),
  57. firstOccurence(0), nextOccurence(0)
  58. {
  59. }
  60. bool CMapEvent::earlierThan(const CMapEvent & other) const
  61. {
  62. return firstOccurence < other.firstOccurence;
  63. }
  64. bool CMapEvent::earlierThanOrEqual(const CMapEvent & other) const
  65. {
  66. return firstOccurence <= other.firstOccurence;
  67. }
  68. CCastleEvent::CCastleEvent() : town(nullptr)
  69. {
  70. }
  71. TerrainTile::TerrainTile() : terType(ETerrainType::BORDER), terView(0), riverType(ERiverType::NO_RIVER),
  72. riverDir(0), roadType(ERoadType::NO_ROAD), roadDir(0), extTileFlags(0), visitable(false),
  73. blocked(false)
  74. {
  75. }
  76. bool TerrainTile::entrableTerrain(const TerrainTile * from /*= NULL*/) const
  77. {
  78. return entrableTerrain(from ? from->terType != ETerrainType::WATER : true, from ? from->terType == ETerrainType::WATER : true);
  79. }
  80. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  81. {
  82. return terType != ETerrainType::ROCK
  83. && ((allowSea && terType == ETerrainType::WATER) || (allowLand && terType != ETerrainType::WATER));
  84. }
  85. bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
  86. {
  87. return entrableTerrain(from) && !blocked;
  88. }
  89. int TerrainTile::topVisitableId() const
  90. {
  91. return visitableObjects.size() ? visitableObjects.back()->ID : -1;
  92. }
  93. bool TerrainTile::isCoastal() const
  94. {
  95. return extTileFlags & 64;
  96. }
  97. bool TerrainTile::hasFavourableWinds() const
  98. {
  99. return extTileFlags & 128;
  100. }
  101. bool TerrainTile::isWater() const
  102. {
  103. return terType == ETerrainType::WATER;
  104. }
  105. CMapHeader::CMapHeader() : version(EMapFormat::SOD), height(72), width(72),
  106. twoLevel(true), difficulty(1), levelLimit(0), howManyTeams(0), areAnyPlayers(false)
  107. {
  108. allowedHeroes = VLC->heroh->getDefaultAllowedHeroes();
  109. players.resize(PlayerColor::PLAYER_LIMIT_I);
  110. }
  111. CMapHeader::~CMapHeader()
  112. {
  113. }
  114. CMap::CMap() : checksum(0), grailRadious(0), terrain(nullptr)
  115. {
  116. allowedAbilities = VLC->heroh->getDefaultAllowedAbilities();
  117. allowedArtifact = VLC->arth->getDefaultAllowedArtifacts();
  118. allowedSpell = VLC->spellh->getDefaultAllowedSpells();
  119. }
  120. CMap::~CMap()
  121. {
  122. if(terrain)
  123. {
  124. for(int ii=0;ii<width;ii++)
  125. {
  126. for(int jj=0;jj<height;jj++)
  127. delete [] terrain[ii][jj];
  128. delete [] terrain[ii];
  129. }
  130. delete [] terrain;
  131. }
  132. }
  133. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  134. {
  135. for(int fx=0; fx<8; ++fx)
  136. {
  137. for(int fy=0; fy<6; ++fy)
  138. {
  139. int xVal = obj->pos.x + fx - 7;
  140. int yVal = obj->pos.y + fy - 5;
  141. int zVal = obj->pos.z;
  142. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  143. {
  144. TerrainTile & curt = terrain[xVal][yVal][zVal];
  145. if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  146. {
  147. curt.visitableObjects -= obj;
  148. curt.visitable = curt.visitableObjects.size();
  149. }
  150. if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  151. {
  152. curt.blockingObjects -= obj;
  153. curt.blocked = curt.blockingObjects.size();
  154. }
  155. }
  156. }
  157. }
  158. }
  159. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  160. {
  161. for(int fx=0; fx<8; ++fx)
  162. {
  163. for(int fy=0; fy<6; ++fy)
  164. {
  165. int xVal = obj->pos.x + fx - 7;
  166. int yVal = obj->pos.y + fy - 5;
  167. int zVal = obj->pos.z;
  168. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  169. {
  170. TerrainTile & curt = terrain[xVal][yVal][zVal];
  171. if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  172. {
  173. curt.visitableObjects.push_back(obj);
  174. curt.visitable = true;
  175. }
  176. if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  177. {
  178. curt.blockingObjects.push_back(obj);
  179. curt.blocked = true;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. CGHeroInstance * CMap::getHero(int heroID)
  186. {
  187. for(ui32 i=0; i<heroes.size();i++)
  188. if(heroes[i]->subID == heroID)
  189. return heroes[i];
  190. return nullptr;
  191. }
  192. bool CMap::isInTheMap(const int3 & pos) const
  193. {
  194. if(pos.x < 0 || pos.y < 0 || pos.z < 0 || pos.x >= width || pos.y >= height
  195. || pos.z > (twoLevel ? 1 : 0))
  196. {
  197. return false;
  198. }
  199. else
  200. {
  201. return true;
  202. }
  203. }
  204. void CMap::getTileRangeCheck(const int3 & tile) const
  205. {
  206. if(!isInTheMap(tile))
  207. {
  208. 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));
  209. }
  210. }
  211. TerrainTile & CMap::getTile(const int3 & tile)
  212. {
  213. getTileRangeCheck(tile);
  214. return terrain[tile.x][tile.y][tile.z];
  215. }
  216. const TerrainTile & CMap::getTile(const int3 & tile) const
  217. {
  218. getTileRangeCheck(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. }