maphandler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 TileObject & a, const TileObject & 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 * (sizes.x * sizes.y) + y * sizes.x + 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. ttiles.clear();
  45. map = Map;
  46. //sizes of terrain
  47. sizes.x = map->width;
  48. sizes.y = map->height;
  49. sizes.z = map->twoLevel ? 2 : 1;
  50. initObjectRects();
  51. logGlobal->info("\tMaking object rects");
  52. }
  53. void MapHandler::initTerrainGraphics()
  54. {
  55. auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
  56. {
  57. for(auto & type : files)
  58. {
  59. animation[type.first] = std::make_unique<Animation>(type.second);
  60. animation[type.first]->preload();
  61. const size_t views = animation[type.first]->size(0);
  62. cache[type.first].resize(views);
  63. for(int j = 0; j < views; j++)
  64. cache[type.first][j] = animation[type.first]->getImage(j);
  65. }
  66. };
  67. std::map<std::string, std::string> terrainFiles;
  68. std::map<std::string, std::string> roadFiles;
  69. std::map<std::string, std::string> riverFiles;
  70. for(const auto & terrain : VLC->terrainTypeHandler->objects)
  71. {
  72. terrainFiles[terrain->getJsonKey()] = terrain->tilesFilename.getName();
  73. }
  74. for(const auto & river : VLC->riverTypeHandler->objects)
  75. {
  76. riverFiles[river->getJsonKey()] = river->tilesFilename.getName();
  77. }
  78. for(const auto & road : VLC->roadTypeHandler->objects)
  79. {
  80. roadFiles[road->getJsonKey()] = road->tilesFilename.getName();
  81. }
  82. loadFlipped(terrainAnimations, terrainImages, terrainFiles);
  83. loadFlipped(riverAnimations, riverImages, riverFiles);
  84. loadFlipped(roadAnimations, roadImages, roadFiles);
  85. }
  86. void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z)
  87. {
  88. auto & tinfo = map->getTile(int3(x, y, z));
  89. ui8 rotation = tinfo.extTileFlags % 4;
  90. auto terrainName = tinfo.terType->getJsonKey();
  91. if(terrainImages.at(terrainName).size() <= tinfo.terView)
  92. return;
  93. bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
  94. painter.drawImage(x * tileSize, y * tileSize, terrainImages.at(terrainName)[tinfo.terView]->mirrored(hflip, vflip));
  95. }
  96. void MapHandler::drawRoad(QPainter & painter, int x, int y, int z)
  97. {
  98. auto & tinfo = map->getTile(int3(x, y, z));
  99. auto * tinfoUpper = map->isInTheMap(int3(x, y - 1, z)) ? &map->getTile(int3(x, y - 1, z)) : nullptr;
  100. if(tinfoUpper && tinfoUpper->roadType)
  101. {
  102. auto roadName = tinfoUpper->roadType->getJsonKey();
  103. QRect source(0, tileSize / 2, tileSize, tileSize / 2);
  104. ui8 rotation = (tinfoUpper->extTileFlags >> 4) % 4;
  105. bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
  106. if(roadImages.at(roadName).size() > tinfoUpper->roadDir)
  107. {
  108. painter.drawImage(QPoint(x * tileSize, y * tileSize), roadImages.at(roadName)[tinfoUpper->roadDir]->mirrored(hflip, vflip), source);
  109. }
  110. }
  111. if(tinfo.roadType) //print road from this tile
  112. {
  113. auto roadName = tinfo.roadType->getJsonKey();;
  114. QRect source(0, 0, tileSize, tileSize / 2);
  115. ui8 rotation = (tinfo.extTileFlags >> 4) % 4;
  116. bool hflip = (rotation == 1 || rotation == 3), vflip = (rotation == 2 || rotation == 3);
  117. if(roadImages.at(roadName).size() > tinfo.roadDir)
  118. {
  119. painter.drawImage(QPoint(x * tileSize, y * tileSize + tileSize / 2), roadImages.at(roadName)[tinfo.roadDir]->mirrored(hflip, vflip), source);
  120. }
  121. }
  122. }
  123. void MapHandler::drawRiver(QPainter & painter, int x, int y, int z)
  124. {
  125. auto & tinfo = map->getTile(int3(x, y, z));
  126. if(tinfo.riverType->getId() == River::NO_RIVER)
  127. return;
  128. //TODO: use ui8 instead of string key
  129. auto riverName = tinfo.riverType->getJsonKey();
  130. if(riverImages.at(riverName).size() <= tinfo.riverDir)
  131. return;
  132. ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
  133. bool hflip = (rotation == 1 || rotation == 3), 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. void MapHandler::initObjectRects()
  151. {
  152. ttiles.resize(sizes.x * sizes.y * sizes.z);
  153. //initializing objects / rects
  154. for(const CGObjectInstance * elem : map->objects)
  155. {
  156. CGObjectInstance *obj = const_cast<CGObjectInstance *>(elem);
  157. if( !obj
  158. || (obj->ID==Obj::HERO && static_cast<const CGHeroInstance*>(obj)->inTownGarrison) //garrisoned hero
  159. || (obj->ID==Obj::BOAT && static_cast<const CGBoat*>(obj)->hero)) //boat with hero (hero graphics is used)
  160. {
  161. continue;
  162. }
  163. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  164. //no animation at all
  165. if(!animation)
  166. continue;
  167. //empty animation
  168. if(animation->size(0) == 0)
  169. continue;
  170. auto image = animation->getImage(0, obj->ID == Obj::HERO ? 2 : 0);
  171. if(!image)
  172. {
  173. //workaround for prisons
  174. image = animation->getImage(0, 0);
  175. if(!image)
  176. continue;
  177. }
  178. for(int fx=0; fx < obj->getWidth(); ++fx)
  179. {
  180. for(int fy=0; fy < obj->getHeight(); ++fy)
  181. {
  182. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  183. QRect cr(image->width() - fx * tileSize - tileSize,
  184. image->height() - fy * tileSize - tileSize,
  185. image->width(),
  186. image->height());
  187. TileObject toAdd(obj, cr);
  188. if( map->isInTheMap(currTile) && // within map
  189. cr.x() + cr.width() > 0 && // image has data on this tile
  190. cr.y() + cr.height() > 0)
  191. {
  192. ttiles[index(currTile)].push_back(toAdd);
  193. }
  194. }
  195. }
  196. }
  197. for(auto & tt : ttiles)
  198. {
  199. stable_sort(tt.begin(), tt.end(), objectBlitOrderSorter);
  200. }
  201. }
  202. bool MapHandler::compareObjectBlitOrder(const CGObjectInstance * a, const CGObjectInstance * b)
  203. {
  204. if (!a)
  205. return true;
  206. if (!b)
  207. return false;
  208. if (a->appearance->printPriority != b->appearance->printPriority)
  209. return a->appearance->printPriority > b->appearance->printPriority;
  210. if(a->pos.y != b->pos.y)
  211. return a->pos.y < b->pos.y;
  212. if(b->ID == Obj::HERO && a->ID != Obj::HERO)
  213. return true;
  214. if(b->ID != Obj::HERO && a->ID == Obj::HERO)
  215. return false;
  216. if(!a->isVisitable() && b->isVisitable())
  217. return true;
  218. if(!b->isVisitable() && a->isVisitable())
  219. return false;
  220. if(a->pos.x < b->pos.x)
  221. return true;
  222. return false;
  223. }
  224. TileObject::TileObject(CGObjectInstance * obj_, QRect rect_)
  225. : obj(obj_),
  226. rect(rect_)
  227. {
  228. }
  229. TileObject::~TileObject()
  230. {
  231. }
  232. std::shared_ptr<QImage> MapHandler::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor color, int group) const
  233. {
  234. if(!hero || hero->boat)
  235. return std::shared_ptr<QImage>();
  236. return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color.getNum()), anim, group, hero->moveDir, true);
  237. }
  238. std::shared_ptr<QImage> MapHandler::findFlagBitmapInternal(std::shared_ptr<Animation> animation, int anim, int group, ui8 dir, bool moving) const
  239. {
  240. size_t groupSize = animation->size(group);
  241. if(groupSize == 0)
  242. return nullptr;
  243. if(moving)
  244. return animation->getImage(anim % groupSize, group);
  245. else
  246. return animation->getImage((anim / 4) % groupSize, group);
  247. }
  248. MapHandler::AnimBitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance * obj, int anim, int group) const
  249. {
  250. if(!obj)
  251. return MapHandler::AnimBitmapHolder();
  252. // normal object
  253. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  254. size_t groupSize = animation->size(group);
  255. if(groupSize == 0)
  256. return MapHandler::AnimBitmapHolder();
  257. animation->playerColored(obj->tempOwner);
  258. auto bitmap = animation->getImage(anim % groupSize, group);
  259. if(!bitmap)
  260. return MapHandler::AnimBitmapHolder();
  261. setPlayerColor(bitmap.get(), obj->tempOwner);
  262. return MapHandler::AnimBitmapHolder(bitmap);
  263. }
  264. std::vector<TileObject> & MapHandler::getObjects(int x, int y, int z)
  265. {
  266. return ttiles[index(x, y, z)];
  267. }
  268. void MapHandler::drawObjects(QPainter & painter, int x, int y, int z, const std::set<const CGObjectInstance *> & locked)
  269. {
  270. painter.setRenderHint(QPainter::Antialiasing, false);
  271. painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
  272. for(auto & object : getObjects(x, y, z))
  273. {
  274. const CGObjectInstance * obj = object.obj;
  275. if(!obj)
  276. {
  277. logGlobal->error("Stray map object that isn't fading");
  278. return;
  279. }
  280. uint8_t animationFrame = 0;
  281. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  282. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  283. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  284. if(objData.objBitmap)
  285. {
  286. auto pos = obj->getPosition();
  287. painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect, Qt::AutoColor | Qt::NoOpaqueDetection);
  288. if(locked.count(obj))
  289. {
  290. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
  291. painter.fillRect(x * tileSize, y * tileSize, object.rect.width(), object.rect.height(), Qt::Dense4Pattern);
  292. painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
  293. }
  294. if(objData.flagBitmap)
  295. {
  296. if(x == pos.x && y == pos.y)
  297. painter.drawImage(QPoint((x - 2) * tileSize, (y - 1) * tileSize), *objData.flagBitmap);
  298. }
  299. }
  300. }
  301. }
  302. void MapHandler::drawObject(QPainter & painter, const TileObject & object)
  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, 0);
  314. if (objData.objBitmap)
  315. {
  316. auto pos = obj->getPosition();
  317. painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.objBitmap);
  318. if (objData.flagBitmap)
  319. {
  320. if(object.rect.x() == pos.x && object.rect.y() == pos.y)
  321. painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.flagBitmap);
  322. }
  323. }
  324. }
  325. void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y)
  326. {
  327. if (!obj)
  328. {
  329. logGlobal->error("Stray map object that isn't fading");
  330. return;
  331. }
  332. uint8_t animationFrame = 0;
  333. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  334. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  335. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  336. if (objData.objBitmap)
  337. {
  338. painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.objBitmap);
  339. if (objData.flagBitmap)
  340. painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.flagBitmap);
  341. }
  342. }
  343. QRgb MapHandler::getTileColor(int x, int y, int z)
  344. {
  345. // if object at tile is owned - it will be colored as its owner
  346. for(auto & object : getObjects(x, y, z))
  347. {
  348. if(!object.obj->getBlockedPos().count(int3(x, y, z)))
  349. continue;
  350. PlayerColor player = object.obj->getOwner();
  351. if(player == PlayerColor::NEUTRAL)
  352. return graphics->neutralColor;
  353. else
  354. if (player.isValidPlayer())
  355. return graphics->playerColors[player.getNum()];
  356. }
  357. // else - use terrain color (blocked version or normal)
  358. auto & tile = map->getTile(int3(x, y, z));
  359. auto color = tile.terType->minimapUnblocked;
  360. if (tile.blocked && (!tile.visitable))
  361. color = tile.terType->minimapBlocked;
  362. return qRgb(color.r, color.g, color.b);
  363. }
  364. void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
  365. {
  366. painter.setPen(getTileColor(x, y, z));
  367. painter.drawPoint(x, y);
  368. }
  369. void MapHandler::invalidate(int x, int y, int z)
  370. {
  371. auto & objects = getObjects(x, y, z);
  372. for(auto obj = objects.begin(); obj != objects.end();)
  373. {
  374. //object was removed
  375. if(std::find(map->objects.begin(), map->objects.end(), obj->obj) == map->objects.end())
  376. {
  377. obj = objects.erase(obj);
  378. continue;
  379. }
  380. //object was moved
  381. auto & pos = obj->obj->pos;
  382. if(pos.z != z || pos.x < x || pos.y < y || pos.x - obj->obj->getWidth() >= x || pos.y - obj->obj->getHeight() >= y)
  383. {
  384. obj = objects.erase(obj);
  385. continue;
  386. }
  387. ++obj;
  388. }
  389. stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
  390. }
  391. void MapHandler::invalidate(CGObjectInstance * obj)
  392. {
  393. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  394. //no animation at all or empty animation
  395. if(!animation || animation->size(0) == 0)
  396. return;
  397. auto image = animation->getImage(0, obj->ID == Obj::HERO ? 2 : 0);
  398. if(!image)
  399. return;
  400. for(int fx=0; fx < obj->getWidth(); ++fx)
  401. {
  402. for(int fy=0; fy < obj->getHeight(); ++fy)
  403. {
  404. //object presented on the tile
  405. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  406. QRect cr(image->width() - fx * tileSize - tileSize, image->height() - fy * tileSize - tileSize, image->width(), image->height());
  407. if( map->isInTheMap(currTile) && // within map
  408. cr.x() + cr.width() > 0 && // image has data on this tile
  409. cr.y() + cr.height() > 0)
  410. {
  411. auto & objects = ttiles[index(currTile)];
  412. bool found = false;
  413. for(auto & o : objects)
  414. {
  415. if(o.obj == obj)
  416. {
  417. o.rect = cr;
  418. found = true;
  419. break;
  420. }
  421. }
  422. if(!found)
  423. objects.emplace_back(obj, cr);
  424. stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
  425. }
  426. }
  427. }
  428. }
  429. std::vector<int3> MapHandler::getTilesUnderObject(CGObjectInstance * obj) const
  430. {
  431. std::vector<int3> result;
  432. for(int fx=0; fx < obj->getWidth(); ++fx)
  433. {
  434. for(int fy=0; fy < obj->getHeight(); ++fy)
  435. {
  436. //object presented on the tile
  437. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  438. if(map->isInTheMap(currTile)) // within map
  439. {
  440. result.push_back(currTile);
  441. }
  442. }
  443. }
  444. return result;
  445. }
  446. void MapHandler::invalidateObjects()
  447. {
  448. for(auto obj : map->objects)
  449. {
  450. invalidate(obj);
  451. }
  452. }
  453. void MapHandler::invalidate(const std::vector<int3> & tiles)
  454. {
  455. for(auto & currTile : tiles)
  456. {
  457. invalidate(currTile.x, currTile.y, currTile.z);
  458. }
  459. }