CMap.cpp 7.3 KB

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