maphandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 CMap::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->levels());
  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. ObjectRect::ObjectRect(const CGObjectInstance * obj_, QRect rect_)
  239. : obj(obj_)
  240. , rect(rect_)
  241. {
  242. }
  243. ObjectRect::~ObjectRect()
  244. {
  245. }
  246. std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor color, int group) const
  247. {
  248. if(!hero || hero->inBoat())
  249. return std::shared_ptr<QImage>();
  250. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color.getNum()), anim, group, hero->moveDir, true);
  251. }
  252. std::shared_ptr<QImage> MapHandler::findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const
  253. {
  254. size_t groupSize = animation->size(group);
  255. if(groupSize == 0)
  256. return nullptr;
  257. if(moving)
  258. return animation->getImage(anim % groupSize, group);
  259. else
  260. return animation->getImage((anim / 4) % groupSize, group);
  261. }
  262. MapHandler::BitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance * obj, int anim, int group) const
  263. {
  264. if(!obj)
  265. return MapHandler::BitmapHolder();
  266. // normal object
  267. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  268. size_t groupSize = animation->size(group);
  269. if(groupSize == 0)
  270. return MapHandler::BitmapHolder();
  271. animation->playerColored(obj->tempOwner);
  272. auto bitmap = animation->getImage(anim % groupSize, group);
  273. if(!bitmap)
  274. return MapHandler::BitmapHolder();
  275. setPlayerColor(bitmap.get(), obj->tempOwner);
  276. return MapHandler::BitmapHolder(bitmap);
  277. }
  278. std::vector<ObjectRect> & MapHandler::getObjects(const int3 & tile)
  279. {
  280. return tileObjects[index(tile)];
  281. }
  282. std::vector<ObjectRect> & MapHandler::getObjects(int x, int y, int z)
  283. {
  284. return tileObjects[index(x, y, z)];
  285. }
  286. void MapHandler::drawObjects(QPainter & painter, const QRectF & section, int z, std::set<const CGObjectInstance *> & locked)
  287. {
  288. painter.setRenderHint(QPainter::Antialiasing, false);
  289. painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
  290. auto blitOrder = [](const CGObjectInstance * a, const CGObjectInstance * b) { return CMap::compareObjectBlitOrder(a, b);};
  291. std::set<const CGObjectInstance *, decltype(blitOrder)> objects;
  292. int left = static_cast<int>(std::round(section.left()))/tileSize;
  293. int right = static_cast<int>(std::round(section.right()))/tileSize;
  294. int top = static_cast<int>(std::round(section.top()))/tileSize;
  295. int bottom = static_cast<int>(std::round(section.bottom()))/tileSize;
  296. for(int x = left; x < right; ++x)
  297. {
  298. for(int y = top; y < bottom; ++y)
  299. {
  300. for(auto & object : getObjects(x, y, z))
  301. {
  302. if (!objects.contains(object.obj))
  303. objects.insert(object.obj);
  304. }
  305. }
  306. }
  307. for (auto const& object : objects)
  308. {
  309. int3 pos = object->pos;
  310. drawObjectAt(painter, object, pos.x, pos.y, section.topLeft(), locked.count(object));
  311. }
  312. }
  313. void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y, QPointF offset, bool locked)
  314. {
  315. if (!obj)
  316. {
  317. logGlobal->error("Stray map object that isn't fading");
  318. return;
  319. }
  320. uint8_t animationFrame = 0;
  321. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  322. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  323. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  324. if (objData.objBitmap)
  325. {
  326. QPoint point((x + 1) * tileSize - (objData.objBitmap->width() + offset.x()), (y + 1) * tileSize - (objData.objBitmap->height() + offset.y()));
  327. QRect rect(point, QSize(objData.objBitmap->width(), objData.objBitmap->height()));
  328. painter.drawImage(rect, *objData.objBitmap);
  329. if (locked)
  330. {
  331. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  332. painter.fillRect(rect, Qt::Dense4Pattern);
  333. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  334. }
  335. if (objData.flagBitmap)
  336. painter.drawImage(point, *objData.flagBitmap);
  337. }
  338. }
  339. QRgb MapHandler::getTileColor(int x, int y, int z)
  340. {
  341. // if object at tile is owned - it will be colored as its owner
  342. for(auto & object : getObjects(x, y, z))
  343. {
  344. if(!object.obj->getBlockedPos().count(int3(x, y, z)))
  345. continue;
  346. PlayerColor player = object.obj->getOwner();
  347. if(player == PlayerColor::NEUTRAL)
  348. return graphics->neutralColor;
  349. else
  350. if (player.isValidPlayer())
  351. return graphics->playerColors[player.getNum()];
  352. }
  353. // else - use terrain color (blocked version or normal)
  354. auto & tile = map->getTile(int3(x, y, z));
  355. auto color = tile.getTerrain()->minimapUnblocked;
  356. if (tile.blocked() && (!tile.visitable()))
  357. color = tile.getTerrain()->minimapBlocked;
  358. return qRgb(color.r, color.g, color.b);
  359. }
  360. void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
  361. {
  362. painter.setPen(getTileColor(x, y, z));
  363. painter.drawPoint(x, y);
  364. }
  365. std::set<int3> MapHandler::invalidate(const CGObjectInstance * obj)
  366. {
  367. auto t1 = removeObject(obj);
  368. auto t2 = addObject(obj);
  369. t1.insert(t2.begin(), t2.end());
  370. for(auto & tt : t2)
  371. stable_sort(tileObjects[index(tt)].begin(), tileObjects[index(tt)].end(), objectBlitOrderSorter);
  372. return t1;
  373. }
  374. void MapHandler::invalidateObjects()
  375. {
  376. initObjectRects();
  377. }