maphandler.cpp 15 KB

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