CMap.cpp 7.3 KB

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