CMap.cpp 6.2 KB

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