maphandler.cpp 13 KB

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