maphandler.cpp 15 KB

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