CMap.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "StdInc.h"
  2. #include "CMap.h"
  3. #include "../CObjectHandler.h"
  4. #include "../CArtHandler.h"
  5. PlayerInfo::PlayerInfo(): p7(0), p8(0), p9(0), canHumanPlay(0), canComputerPlay(0),
  6. AITactic(0), isFactionRandom(0),
  7. mainHeroPortrait(0), hasMainTown(0), generateHeroAtMainTown(0),
  8. team(255), generateHero(0)
  9. {
  10. }
  11. si8 PlayerInfo::defaultCastle() const
  12. {
  13. assert(!allowedFactions.empty()); // impossible?
  14. if(allowedFactions.size() == 1)
  15. {
  16. // only one faction is available - pick it
  17. return *allowedFactions.begin();
  18. }
  19. // set to random
  20. return -1;
  21. }
  22. si8 PlayerInfo::defaultHero() const
  23. {
  24. // we will generate hero in front of main town
  25. if((generateHeroAtMainTown && hasMainTown) || p8)
  26. {
  27. //random hero
  28. return -1;
  29. }
  30. return -2;
  31. }
  32. CMapHeader::CMapHeader() : version(EMapFormat::INVALID)
  33. {
  34. areAnyPLayers = difficulty = levelLimit = howManyTeams = 0;
  35. height = width = twoLevel = -1;
  36. }
  37. CMapHeader::~CMapHeader()
  38. {
  39. }
  40. void CMap::removeBlockVisTiles(CGObjectInstance * obj, bool total)
  41. {
  42. for(int fx=0; fx<8; ++fx)
  43. {
  44. for(int fy=0; fy<6; ++fy)
  45. {
  46. int xVal = obj->pos.x + fx - 7;
  47. int yVal = obj->pos.y + fy - 5;
  48. int zVal = obj->pos.z;
  49. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  50. {
  51. TerrainTile & curt = terrain[xVal][yVal][zVal];
  52. if(total || ((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  53. {
  54. curt.visitableObjects -= obj;
  55. curt.visitable = curt.visitableObjects.size();
  56. }
  57. if(total || !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  58. {
  59. curt.blockingObjects -= obj;
  60. curt.blocked = curt.blockingObjects.size();
  61. }
  62. }
  63. }
  64. }
  65. }
  66. void CMap::addBlockVisTiles(CGObjectInstance * obj)
  67. {
  68. for(int fx=0; fx<8; ++fx)
  69. {
  70. for(int fy=0; fy<6; ++fy)
  71. {
  72. int xVal = obj->pos.x + fx - 7;
  73. int yVal = obj->pos.y + fy - 5;
  74. int zVal = obj->pos.z;
  75. if(xVal>=0 && xVal<width && yVal>=0 && yVal<height)
  76. {
  77. TerrainTile & curt = terrain[xVal][yVal][zVal];
  78. if(((obj->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  79. {
  80. curt.visitableObjects.push_back(obj);
  81. curt.visitable = true;
  82. }
  83. if(!((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  84. {
  85. curt.blockingObjects.push_back(obj);
  86. curt.blocked = true;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. CMap::CMap() : terrain(nullptr)
  93. {
  94. }
  95. CMap::~CMap()
  96. {
  97. if(terrain)
  98. {
  99. for(int ii=0;ii<width;ii++)
  100. {
  101. for(int jj=0;jj<height;jj++)
  102. delete [] terrain[ii][jj];
  103. delete [] terrain[ii];
  104. }
  105. delete [] terrain;
  106. }
  107. for(std::list<ConstTransitivePtr<CMapEvent> >::iterator i = events.begin(); i != events.end(); i++)
  108. {
  109. i->dellNull();
  110. }
  111. }
  112. CGHeroInstance * CMap::getHero(int heroID)
  113. {
  114. for(ui32 i=0; i<heroes.size();i++)
  115. if(heroes[i]->subID == heroID)
  116. return heroes[i];
  117. return nullptr;
  118. }
  119. bool CMap::isInTheMap(const int3 &pos) const
  120. {
  121. if(pos.x<0 || pos.y<0 || pos.z<0 || pos.x >= width || pos.y >= height || pos.z > twoLevel)
  122. return false;
  123. else return true;
  124. }
  125. TerrainTile & CMap::getTile( const int3 & tile )
  126. {
  127. return terrain[tile.x][tile.y][tile.z];
  128. }
  129. const TerrainTile & CMap::getTile( const int3 & tile ) const
  130. {
  131. return terrain[tile.x][tile.y][tile.z];
  132. }
  133. bool CMap::isWaterTile(const int3 &pos) const
  134. {
  135. return isInTheMap(pos) && getTile(pos).tertype == ETerrainType::WATER;
  136. }
  137. const CGObjectInstance *CMap::getObjectiveObjectFrom(int3 pos, bool lookForHero)
  138. {
  139. const std::vector <CGObjectInstance *> & objs = getTile(pos).visitableObjects;
  140. assert(objs.size());
  141. if(objs.size() > 1 && lookForHero && objs.front()->ID != Obj::HERO)
  142. {
  143. assert(objs.back()->ID == Obj::HERO);
  144. return objs.back();
  145. }
  146. else
  147. return objs.front();
  148. }
  149. void CMap::checkForObjectives()
  150. {
  151. if(isInTheMap(victoryCondition.pos))
  152. victoryCondition.obj = getObjectiveObjectFrom(victoryCondition.pos, victoryCondition.condition == EVictoryConditionType::BEATHERO);
  153. if(isInTheMap(lossCondition.pos))
  154. lossCondition.obj = getObjectiveObjectFrom(lossCondition.pos, lossCondition.typeOfLossCon == ELossConditionType::LOSSHERO);
  155. }
  156. void CMap::addNewArtifactInstance( CArtifactInstance *art )
  157. {
  158. art->id = artInstances.size();
  159. artInstances.push_back(art);
  160. }
  161. void CMap::eraseArtifactInstance(CArtifactInstance *art)
  162. {
  163. assert(artInstances[art->id] == art);
  164. artInstances[art->id].dellNull();
  165. }
  166. LossCondition::LossCondition()
  167. {
  168. obj = NULL;
  169. timeLimit = -1;
  170. pos = int3(-1,-1,-1);
  171. }
  172. VictoryCondition::VictoryCondition()
  173. {
  174. pos = int3(-1,-1,-1);
  175. obj = NULL;
  176. ID = allowNormalVictory = appliesToAI = count = 0;
  177. }
  178. bool TerrainTile::entrableTerrain(const TerrainTile * from /*= NULL*/) const
  179. {
  180. return entrableTerrain(from ? from->tertype != ETerrainType::WATER : true, from ? from->tertype == ETerrainType::WATER : true);
  181. }
  182. bool TerrainTile::entrableTerrain(bool allowLand, bool allowSea) const
  183. {
  184. return tertype != ETerrainType::ROCK
  185. && ((allowSea && tertype == ETerrainType::WATER) || (allowLand && tertype != ETerrainType::WATER));
  186. }
  187. bool TerrainTile::isClear(const TerrainTile *from /*= NULL*/) const
  188. {
  189. return entrableTerrain(from) && !blocked;
  190. }
  191. int TerrainTile::topVisitableID() const
  192. {
  193. return visitableObjects.size() ? visitableObjects.back()->ID : -1;
  194. }
  195. bool TerrainTile::isCoastal() const
  196. {
  197. return extTileFlags & 64;
  198. }
  199. bool TerrainTile::hasFavourableWinds() const
  200. {
  201. return extTileFlags & 128;
  202. }
  203. bool TerrainTile::isWater() const
  204. {
  205. return tertype == ETerrainType::WATER;
  206. }