maphandler.cpp 13 KB

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