maphandler.cpp 13 KB

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