2
0

maphandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * maphandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. //code is copied from vcmiclient/mapHandler.cpp with minimal changes
  11. #include "StdInc.h"
  12. #include "maphandler.h"
  13. #include "graphics.h"
  14. #include "../lib/RoadHandler.h"
  15. #include "../lib/RiverHandler.h"
  16. #include "../lib/TerrainHandler.h"
  17. #include "../lib/mapping/CMap.h"
  18. #include "../lib/mapObjects/CGHeroInstance.h"
  19. #include "../lib/mapObjects/ObjectTemplate.h"
  20. #include "../lib/mapObjects/MiscObjects.h"
  21. #include "../lib/GameConstants.h"
  22. namespace
  23. {
  24. const int tileSize = 32;
  25. bool objectBlitOrderSorter(const ObjectRect & a, const ObjectRect & b)
  26. {
  27. return MapHandler::compareObjectBlitOrder(a.obj, b.obj);
  28. }
  29. QImage flippedImage(const std::shared_ptr<QImage> & image, ui8 rotationFlags)
  30. {
  31. const ui8 rotation = rotationFlags % 4;
  32. const bool hflip = rotation & 0b01;
  33. const bool vflip = rotation & 0b10;
  34. #if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
  35. Qt::Orientations orientations;
  36. if(hflip)
  37. orientations |= Qt::Horizontal;
  38. if(vflip)
  39. orientations |= Qt::Vertical;
  40. return image->flipped(orientations);
  41. #else
  42. return image->mirrored(hflip, vflip);
  43. #endif
  44. }
  45. }
  46. int MapHandler::index(int x, int y, int z) const
  47. {
  48. return z * (map->width * map->height) + y * map->width + x;
  49. }
  50. int MapHandler::index(const int3 & p) const
  51. {
  52. return index(p.x, p.y, p.z);
  53. }
  54. MapHandler::MapHandler()
  55. {
  56. initTerrainGraphics();
  57. logGlobal->info("\tPreparing terrain, roads, rivers, borders");
  58. }
  59. void MapHandler::reset(const CMap * Map)
  60. {
  61. map = Map;
  62. initObjectRects();
  63. logGlobal->info("\tMaking object rects");
  64. }
  65. void MapHandler::initTerrainGraphics()
  66. {
  67. auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
  68. {
  69. for(auto & type : files)
  70. {
  71. animation[type.first] = std::make_unique<Animation>(type.second);
  72. animation[type.first]->preload();
  73. const size_t views = animation[type.first]->size(0);
  74. cache[type.first].resize(views);
  75. for(int j = 0; j < views; j++)
  76. cache[type.first][j] = animation[type.first]->getImage(j);
  77. }
  78. };
  79. std::map<std::string, std::string> terrainFiles;
  80. std::map<std::string, std::string> roadFiles;
  81. std::map<std::string, std::string> riverFiles;
  82. for(const auto & terrain : LIBRARY->terrainTypeHandler->objects)
  83. {
  84. terrainFiles[terrain->getJsonKey()] = terrain->tilesFilename.getName();
  85. }
  86. for(const auto & river : LIBRARY->riverTypeHandler->objects)
  87. {
  88. riverFiles[river->getJsonKey()] = river->tilesFilename.getName();
  89. }
  90. for(const auto & road : LIBRARY->roadTypeHandler->objects)
  91. {
  92. roadFiles[road->getJsonKey()] = road->tilesFilename.getName();
  93. }
  94. loadFlipped(terrainAnimations, terrainImages, terrainFiles);
  95. loadFlipped(riverAnimations, riverImages, riverFiles);
  96. loadFlipped(roadAnimations, roadImages, roadFiles);
  97. }
  98. void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z, QPointF offset)
  99. {
  100. const auto & tinfo = map->getTile(int3(x, y, z));
  101. auto terrainName = tinfo.getTerrain()->getJsonKey();
  102. if(terrainImages.at(terrainName).size() <= tinfo.terView)
  103. return;
  104. painter.drawImage(x * tileSize - offset.x(), y * tileSize - offset.y(), flippedImage(terrainImages.at(terrainName)[tinfo.terView], tinfo.extTileFlags));
  105. }
  106. void MapHandler::drawRoad(QPainter & painter, int x, int y, int z, QPointF offset)
  107. {
  108. const auto & tinfo = map->getTile(int3(x, y, z));
  109. auto * tinfoUpper = map->isInTheMap(int3(x, y - 1, z)) ? &map->getTile(int3(x, y - 1, z)) : nullptr;
  110. if(tinfoUpper && tinfoUpper->roadType)
  111. {
  112. auto roadName = tinfoUpper->getRoad()->getJsonKey();
  113. if(roadImages.at(roadName).size() > tinfoUpper->roadDir)
  114. {
  115. const QRect source{0, tileSize / 2, tileSize, tileSize / 2};
  116. const ui8 rotationFlags = tinfoUpper->extTileFlags >> 4;
  117. painter.drawImage(QPoint(x * tileSize - offset.x(), y * tileSize - offset.y()), flippedImage(roadImages.at(roadName)[tinfoUpper->roadDir], rotationFlags), source);
  118. }
  119. }
  120. if(tinfo.roadType) //print road from this tile
  121. {
  122. auto roadName = tinfo.getRoad()->getJsonKey();
  123. if(roadImages.at(roadName).size() > tinfo.roadDir)
  124. {
  125. const QRect source{0, 0, tileSize, tileSize / 2};
  126. const ui8 rotationFlags = tinfo.extTileFlags >> 4;
  127. painter.drawImage(QPoint(x * tileSize - offset.x(), y * tileSize + tileSize / 2 - offset.y()), flippedImage(roadImages.at(roadName)[tinfo.roadDir], rotationFlags), source);
  128. }
  129. }
  130. }
  131. void MapHandler::drawRiver(QPainter & painter, int x, int y, int z, QPointF offset)
  132. {
  133. const auto & tinfo = map->getTile(int3(x, y, z));
  134. if(!tinfo.hasRiver())
  135. return;
  136. //TODO: use ui8 instead of string key
  137. auto riverName = tinfo.getRiver()->getJsonKey();
  138. if(riverImages.at(riverName).size() <= tinfo.riverDir)
  139. return;
  140. const ui8 rotationFlags = tinfo.extTileFlags >> 2;
  141. painter.drawImage(x * tileSize - offset.x(), y * tileSize - offset.y(), flippedImage(riverImages.at(riverName)[tinfo.riverDir], rotationFlags));
  142. }
  143. void setPlayerColor(QImage * sur, PlayerColor player)
  144. {
  145. if(player == PlayerColor::UNFLAGGABLE)
  146. return;
  147. if(sur->format() == QImage::Format_Indexed8)
  148. {
  149. QRgb color = graphics->neutralColor;
  150. if(player != PlayerColor::NEUTRAL && player < PlayerColor::PLAYER_LIMIT)
  151. color = graphics->playerColors.at(player.getNum());
  152. sur->setColor(5, color);
  153. }
  154. else
  155. logGlobal->warn("Warning, setPlayerColor called on not 8bpp surface!");
  156. }
  157. std::shared_ptr<QImage> MapHandler::getObjectImage(const CGObjectInstance * obj)
  158. {
  159. if( !obj
  160. || (obj->ID==Obj::HERO && dynamic_cast<const CGHeroInstance*>(obj)->isGarrisoned()) //garrisoned hero
  161. || (obj->ID==Obj::BOAT && dynamic_cast<const CGBoat*>(obj)->getBoardedHero())) //boat with hero (hero graphics is used)
  162. {
  163. return nullptr;
  164. }
  165. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  166. //no animation at all
  167. if(!animation)
  168. return nullptr;
  169. //empty animation
  170. if(animation->size(0) == 0)
  171. return nullptr;
  172. auto image = animation->getImage(0, obj->ID == Obj::HERO ? 2 : 0);
  173. if(!image)
  174. {
  175. //workaround for prisons
  176. image = animation->getImage(0, 0);
  177. }
  178. return image;
  179. }
  180. std::set<int3> MapHandler::removeObject(const CGObjectInstance *object)
  181. {
  182. std::set<int3> result = tilesCache[object];
  183. for(auto & t : result)
  184. {
  185. auto & objects = getObjects(t);
  186. for(auto iter = objects.begin(); iter != objects.end(); ++iter)
  187. {
  188. if(iter->obj == object)
  189. {
  190. objects.erase(iter);
  191. break;
  192. }
  193. }
  194. }
  195. tilesCache.erase(object);
  196. return result;
  197. }
  198. std::set<int3> MapHandler::addObject(const CGObjectInstance * object)
  199. {
  200. auto image = getObjectImage(object);
  201. if(!image)
  202. return std::set<int3>{};
  203. for(int fx = 0; fx < object->getWidth(); ++fx)
  204. {
  205. for(int fy = 0; fy < object->getHeight(); ++fy)
  206. {
  207. int3 currTile(object->pos.x - fx, object->pos.y - fy, object->pos.z);
  208. QRect cr(image->width() - fx * tileSize - tileSize,
  209. image->height() - fy * tileSize - tileSize,
  210. tileSize,
  211. tileSize);
  212. if( map->isInTheMap(currTile) && // within map
  213. cr.x() + cr.width() > 0 && // image has data on this tile
  214. cr.y() + cr.height() > 0)
  215. {
  216. getObjects(currTile).emplace_back(object, cr);
  217. tilesCache[object].insert(currTile);
  218. }
  219. }
  220. }
  221. return tilesCache[object];
  222. }
  223. void MapHandler::initObjectRects()
  224. {
  225. tileObjects.clear();
  226. tilesCache.clear();
  227. if(!map)
  228. return;
  229. tileObjects.resize(map->width * map->height * map->mapLevels);
  230. //initializing objects / rects
  231. for(const auto & elem : map->objects)
  232. {
  233. addObject(elem.get());
  234. }
  235. for(auto & tt : tileObjects)
  236. stable_sort(tt.begin(), tt.end(), objectBlitOrderSorter);
  237. }
  238. bool MapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  239. {
  240. if (!a)
  241. return true;
  242. if (!b)
  243. return false;
  244. if (a->appearance->printPriority != b->appearance->printPriority)
  245. return a->appearance->printPriority > b->appearance->printPriority;
  246. if(a->pos.y != b->pos.y)
  247. return a->pos.y < b->pos.y;
  248. if(b->ID == Obj::HERO && a->ID != Obj::HERO)
  249. return true;
  250. if(b->ID != Obj::HERO && a->ID == Obj::HERO)
  251. return false;
  252. if(!a->isVisitable() && b->isVisitable())
  253. return true;
  254. if(!b->isVisitable() && a->isVisitable())
  255. return false;
  256. if(a->pos.x < b->pos.x)
  257. return true;
  258. return false;
  259. }
  260. ObjectRect::ObjectRect(const CGObjectInstance * obj_, QRect rect_)
  261. : obj(obj_)
  262. , rect(rect_)
  263. {
  264. }
  265. ObjectRect::~ObjectRect()
  266. {
  267. }
  268. std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor color, int group) const
  269. {
  270. if(!hero || hero->inBoat())
  271. return std::shared_ptr<QImage>();
  272. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color.getNum()), anim, group, hero->moveDir, true);
  273. }
  274. std::shared_ptr<QImage> MapHandler::findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const
  275. {
  276. size_t groupSize = animation->size(group);
  277. if(groupSize == 0)
  278. return nullptr;
  279. if(moving)
  280. return animation->getImage(anim % groupSize, group);
  281. else
  282. return animation->getImage((anim / 4) % groupSize, group);
  283. }
  284. MapHandler::BitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance * obj, int anim, int group) const
  285. {
  286. if(!obj)
  287. return MapHandler::BitmapHolder();
  288. // normal object
  289. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  290. size_t groupSize = animation->size(group);
  291. if(groupSize == 0)
  292. return MapHandler::BitmapHolder();
  293. animation->playerColored(obj->tempOwner);
  294. auto bitmap = animation->getImage(anim % groupSize, group);
  295. if(!bitmap)
  296. return MapHandler::BitmapHolder();
  297. setPlayerColor(bitmap.get(), obj->tempOwner);
  298. return MapHandler::BitmapHolder(bitmap);
  299. }
  300. std::vector<ObjectRect> & MapHandler::getObjects(const int3 & tile)
  301. {
  302. return tileObjects[index(tile)];
  303. }
  304. std::vector<ObjectRect> & MapHandler::getObjects(int x, int y, int z)
  305. {
  306. return tileObjects[index(x, y, z)];
  307. }
  308. void MapHandler::drawObjects(QPainter & painter, const QRectF & section, int z, std::set<const CGObjectInstance *> & locked)
  309. {
  310. painter.setRenderHint(QPainter::Antialiasing, false);
  311. painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
  312. std::map<int3, std::set<const CGObjectInstance *>> objectMap; //following the natural order of int3 we draw from north-west to south-east, in accordance with H3's perspective
  313. int left = static_cast<int>(std::round(section.left()))/tileSize;
  314. int right = static_cast<int>(std::round(section.right()))/tileSize;
  315. int top = static_cast<int>(std::round(section.top()))/tileSize;
  316. int bottom = static_cast<int>(std::round(section.bottom()))/tileSize;
  317. for(int x = left; x < right; ++x)
  318. {
  319. for(int y = top; y < bottom; ++y)
  320. {
  321. for(auto & object : getObjects(x, y, z))
  322. objectMap[object.obj->pos].insert(object.obj);
  323. }
  324. }
  325. for (auto const& objectsOnTile : objectMap)
  326. {
  327. auto tile = objectsOnTile.first;
  328. auto objects = objectsOnTile.second;
  329. for (const CGObjectInstance * object : objects)
  330. drawObjectAt(painter, object, tile.x, tile.y, section.topLeft(), locked.count(object));
  331. }
  332. }
  333. void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y, QPointF offset, bool locked)
  334. {
  335. if (!obj)
  336. {
  337. logGlobal->error("Stray map object that isn't fading");
  338. return;
  339. }
  340. uint8_t animationFrame = 0;
  341. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  342. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  343. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  344. if (objData.objBitmap)
  345. {
  346. QPoint point((x + 1) * tileSize - (objData.objBitmap->width() + offset.x()), (y + 1) * tileSize - (objData.objBitmap->height() + offset.y()));
  347. QRect rect(point, QSize(objData.objBitmap->width(), objData.objBitmap->height()));
  348. painter.drawImage(rect, *objData.objBitmap);
  349. if (locked)
  350. {
  351. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  352. painter.fillRect(rect, Qt::Dense4Pattern);
  353. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  354. }
  355. if (objData.flagBitmap)
  356. painter.drawImage(point, *objData.flagBitmap);
  357. }
  358. }
  359. QRgb MapHandler::getTileColor(int x, int y, int z)
  360. {
  361. // if object at tile is owned - it will be colored as its owner
  362. for(auto & object : getObjects(x, y, z))
  363. {
  364. if(!object.obj->getBlockedPos().count(int3(x, y, z)))
  365. continue;
  366. PlayerColor player = object.obj->getOwner();
  367. if(player == PlayerColor::NEUTRAL)
  368. return graphics->neutralColor;
  369. else
  370. if (player.isValidPlayer())
  371. return graphics->playerColors[player.getNum()];
  372. }
  373. // else - use terrain color (blocked version or normal)
  374. auto & tile = map->getTile(int3(x, y, z));
  375. auto color = tile.getTerrain()->minimapUnblocked;
  376. if (tile.blocked() && (!tile.visitable()))
  377. color = tile.getTerrain()->minimapBlocked;
  378. return qRgb(color.r, color.g, color.b);
  379. }
  380. void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
  381. {
  382. painter.setPen(getTileColor(x, y, z));
  383. painter.drawPoint(x, y);
  384. }
  385. std::set<int3> MapHandler::invalidate(const CGObjectInstance * obj)
  386. {
  387. auto t1 = removeObject(obj);
  388. auto t2 = addObject(obj);
  389. t1.insert(t2.begin(), t2.end());
  390. for(auto & tt : t2)
  391. stable_sort(tileObjects[index(tt)].begin(), tileObjects[index(tt)].end(), objectBlitOrderSorter);
  392. return t1;
  393. }
  394. void MapHandler::invalidateObjects()
  395. {
  396. initObjectRects();
  397. }