maphandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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), 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), 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.roadType->getJsonKey();;
  109. QRect source(0, 0, tileSize, tileSize / 2);
  110. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  111. bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
  112. if(roadImages.at(roadName).size() > tinfo.roadDir)
  113. {
  114. painter.drawImage(QPoint(x * tileSize, y * tileSize + tileSize / 2), roadImages.at(roadName)[tinfo.roadDir]->mirrored(hflip, vflip), source);
  115. }
  116. }
  117. }
  118. void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
  119. {
  120. auto & tinfo = map->getTile(int3(x, y, z));
  121. if(tinfo.riverType->getId() == River::NO_RIVER)
  122. return;
  123. //TODO: use ui8 instead of string key
  124. auto riverName = tinfo.riverType->getJsonKey();
  125. if(riverImages.at(riverName).size() <= tinfo.riverDir)
  126. return;
  127. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  128. bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
  129. painter.drawImage(x * tileSize, y * tileSize, riverImages.at(riverName)[tinfo.riverDir]->mirrored(hflip, vflip));
  130. }
  131. void setPlayerColor(QImage * sur, PlayerColor player)
  132. {
  133. if(player == PlayerColor::UNFLAGGABLE)
  134. return;
  135. if(sur->format() == QImage::Format_Indexed8)
  136. {
  137. QRgb color = graphics->neutralColor;
  138. if(player != PlayerColor::NEUTRAL && player < PlayerColor::PLAYER_LIMIT)
  139. color = graphics->playerColors.at(player.getNum());
  140. sur->setColor(5, color);
  141. }
  142. else
  143. logGlobal->warn("Warning, setPlayerColor called on not 8bpp surface!");
  144. }
  145. std::shared_ptr<QImage> MapHandler::getObjectImage(const CGObjectInstance * obj)
  146. {
  147. if( !obj
  148. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  149. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  150. {
  151. return nullptr;
  152. }
  153. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  154. //no animation at all
  155. if(!animation)
  156. return nullptr;
  157. //empty animation
  158. if(animation->size(0) == 0)
  159. return nullptr;
  160. auto image = animation->getImage(0, obj->ID == Obj::HERO ? 2 : 0);
  161. if(!image)
  162. {
  163. //workaround for prisons
  164. image = animation->getImage(0, 0);
  165. }
  166. return image;
  167. }
  168. std::set<int3> MapHandler::removeObject(const CGObjectInstance *object)
  169. {
  170. std::set<int3> result = tilesCache[object];
  171. for(auto & t : result)
  172. {
  173. auto & objects = getObjects(t);
  174. for(auto iter = objects.begin(); iter != objects.end(); ++iter)
  175. {
  176. if(iter->obj == object)
  177. {
  178. objects.erase(iter);
  179. break;
  180. }
  181. }
  182. }
  183. tilesCache.erase(object);
  184. return result;
  185. }
  186. std::set<int3> MapHandler::addObject(const CGObjectInstance * object)
  187. {
  188. auto image = getObjectImage(object);
  189. if(!image)
  190. return std::set<int3>{};
  191. for(int fx = 0; fx < object->getWidth(); ++fx)
  192. {
  193. for(int fy = 0; fy < object->getHeight(); ++fy)
  194. {
  195. int3 currTile(object->pos.x - fx, object->pos.y - fy, object->pos.z);
  196. QRect cr(image->width() - fx * tileSize - tileSize,
  197. image->height() - fy * tileSize - tileSize,
  198. tileSize,
  199. tileSize);
  200. if( map->isInTheMap(currTile) && // within map
  201. cr.x() + cr.width() > 0 && // image has data on this tile
  202. cr.y() + cr.height() > 0)
  203. {
  204. getObjects(currTile).emplace_back(object, cr);
  205. tilesCache[object].insert(currTile);
  206. }
  207. }
  208. }
  209. return tilesCache[object];
  210. }
  211. void MapHandler::initObjectRects()
  212. {
  213. tileObjects.clear();
  214. tilesCache.clear();
  215. if(!map)
  216. return;
  217. tileObjects.resize(map->width * map->height * (map->twoLevel ? 2 : 1));
  218. //initializing objects / rects
  219. for(const CGObjectInstance * elem : map->objects)
  220. {
  221. addObject(elem);
  222. }
  223. for(auto & tt : tileObjects)
  224. stable_sort(tt.begin(), tt.end(), objectBlitOrderSorter);
  225. }
  226. bool MapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  227. {
  228. if (!a)
  229. return true;
  230. if (!b)
  231. return false;
  232. if (a->appearance->printPriority != b->appearance->printPriority)
  233. return a->appearance->printPriority > b->appearance->printPriority;
  234. if(a->pos.y != b->pos.y)
  235. return a->pos.y < b->pos.y;
  236. if(b->ID == Obj::HERO && a->ID != Obj::HERO)
  237. return true;
  238. if(b->ID != Obj::HERO && a->ID == Obj::HERO)
  239. return false;
  240. if(!a->isVisitable() && b->isVisitable())
  241. return true;
  242. if(!b->isVisitable() && a->isVisitable())
  243. return false;
  244. if(a->pos.x < b->pos.x)
  245. return true;
  246. return false;
  247. }
  248. ObjectRect::ObjectRect(const CGObjectInstance * obj_, QRect rect_)
  249. : obj(obj_),
  250. rect(rect_)
  251. {
  252. }
  253. ObjectRect::~ObjectRect()
  254. {
  255. }
  256. std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor color, int group) const
  257. {
  258. if(!hero || hero->boat)
  259. return std::shared_ptr<QImage>();
  260. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color.getNum()), anim, group, hero->moveDir, true);
  261. }
  262. std::shared_ptr<QImage> MapHandler::findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const
  263. {
  264. size_t groupSize = animation->size(group);
  265. if(groupSize == 0)
  266. return nullptr;
  267. if(moving)
  268. return animation->getImage(anim % groupSize, group);
  269. else
  270. return animation->getImage((anim / 4) % groupSize, group);
  271. }
  272. MapHandler::BitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance * obj, int anim, int group) const
  273. {
  274. if(!obj)
  275. return MapHandler::BitmapHolder();
  276. // normal object
  277. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  278. size_t groupSize = animation->size(group);
  279. if(groupSize == 0)
  280. return MapHandler::BitmapHolder();
  281. animation->playerColored(obj->tempOwner);
  282. auto bitmap = animation->getImage(anim % groupSize, group);
  283. if(!bitmap)
  284. return MapHandler::BitmapHolder();
  285. setPlayerColor(bitmap.get(), obj->tempOwner);
  286. return MapHandler::BitmapHolder(bitmap);
  287. }
  288. std::vector<ObjectRect> & MapHandler::getObjects(const int3 & tile)
  289. {
  290. return tileObjects[index(tile)];
  291. }
  292. std::vector<ObjectRect> & MapHandler::getObjects(int x, int y, int z)
  293. {
  294. return tileObjects[index(x, y, z)];
  295. }
  296. void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked)
  297. {
  298. painter.setRenderHint(QPainter::Antialiasing, false);
  299. painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
  300. for(auto & object : getObjects(x, y, z))
  301. {
  302. const CGObjectInstance * obj = object.obj;
  303. if(!obj)
  304. {
  305. logGlobal->error("Stray map object that isn't fading");
  306. return;
  307. }
  308. uint8_t animationFrame = 0;
  309. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  310. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  311. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  312. if(objData.objBitmap)
  313. {
  314. auto pos = obj->getPosition();
  315. painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect, Qt::AutoColor | Qt::NoOpaqueDetection);
  316. if(locked.count(obj))
  317. {
  318. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  319. painter.fillRect(x * tileSize, y * tileSize, object.rect.width(), object.rect.height(), Qt::Dense4Pattern);
  320. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  321. }
  322. if(objData.flagBitmap)
  323. {
  324. if(x == pos.x && y == pos.y)
  325. painter.drawImage(QPoint((x - 2) * tileSize, (y - 1) * tileSize), *objData.flagBitmap);
  326. }
  327. }
  328. }
  329. }
  330. void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y)
  331. {
  332. if (!obj)
  333. {
  334. logGlobal->error("Stray map object that isn't fading");
  335. return;
  336. }
  337. uint8_t animationFrame = 0;
  338. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  339. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  340. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  341. if (objData.objBitmap)
  342. {
  343. painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width(), (y + 1) * tileSize - objData.objBitmap->height()), *objData.objBitmap);
  344. if (objData.flagBitmap)
  345. painter.drawImage(QPoint((x + 1) * tileSize - objData.objBitmap->width(), (y + 1) * tileSize - objData.objBitmap->height()), *objData.flagBitmap);
  346. }
  347. }
  348. QRgb MapHandler::getTileColor(int x, int y, int z)
  349. {
  350. // if object at tile is owned - it will be colored as its owner
  351. for(auto & object : getObjects(x, y, z))
  352. {
  353. if(!object.obj->getBlockedPos().count(int3(x, y, z)))
  354. continue;
  355. PlayerColor player = object.obj->getOwner();
  356. if(player == PlayerColor::NEUTRAL)
  357. return graphics->neutralColor;
  358. else
  359. if (player.isValidPlayer())
  360. return graphics->playerColors[player.getNum()];
  361. }
  362. // else - use terrain color (blocked version or normal)
  363. auto & tile = map->getTile(int3(x, y, z));
  364. auto color = tile.terType->minimapUnblocked;
  365. if (tile.blocked && (!tile.visitable))
  366. color = tile.terType->minimapBlocked;
  367. return qRgb(color.r, color.g, color.b);
  368. }
  369. void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
  370. {
  371. painter.setPen(getTileColor(x, y, z));
  372. painter.drawPoint(x, y);
  373. }
  374. std::set<int3> MapHandler::invalidate(const CGObjectInstance * obj)
  375. {
  376. auto t1 = removeObject(obj);
  377. auto t2 = addObject(obj);
  378. t1.insert(t2.begin(), t2.end());
  379. for(auto & tt : t2)
  380. stable_sort(tileObjects[index(tt)].begin(), tileObjects[index(tt)].end(), objectBlitOrderSorter);
  381. return t1;
  382. }
  383. void MapHandler::invalidateObjects()
  384. {
  385. initObjectRects();
  386. }