CMap.cpp 6.6 KB

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