maphandler.cpp 13 KB

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