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/mapping/CMap.h"
  15. #include "../lib/mapObjects/CGHeroInstance.h"
  16. #include "../lib/mapObjects/CObjectClassesHandler.h"
  17. #include "../lib/CHeroHandler.h"
  18. #include "../lib/CTownHandler.h"
  19. #include "../lib/CModHandler.h"
  20. #include "../lib/mapping/CMap.h"
  21. #include "../lib/GameConstants.h"
  22. #include "../lib/JsonDetail.h"
  23. const int tileSize = 32;
  24. static bool objectBlitOrderSorter(const TileObject & a, const TileObject & b)
  25. {
  26. return MapHandler::compareObjectBlitOrder(a.obj, b.obj);
  27. }
  28. int MapHandler::index(int x, int y, int z) const
  29. {
  30. return z * (sizes.x * sizes.y) + y * sizes.x + x;
  31. }
  32. int MapHandler::index(const int3 & p) const
  33. {
  34. return index(p.x, p.y, p.z);
  35. }
  36. MapHandler::MapHandler()
  37. {
  38. initTerrainGraphics();
  39. logGlobal->info("\tPreparing terrain, roads, rivers, borders");
  40. }
  41. void MapHandler::reset(const CMap * Map)
  42. {
  43. ttiles.clear();
  44. map = Map;
  45. //sizes of terrain
  46. sizes.x = map->width;
  47. sizes.y = map->height;
  48. sizes.z = map->twoLevel ? 2 : 1;
  49. initObjectRects();
  50. logGlobal->info("\tMaking object rects");
  51. }
  52. void MapHandler::initTerrainGraphics()
  53. {
  54. auto loadFlipped = [](TFlippedAnimations & animation, TFlippedCache & cache, const std::map<std::string, std::string> & files)
  55. {
  56. for(auto & type : files)
  57. {
  58. animation[type.first] = make_unique<Animation>(type.second);
  59. animation[type.first]->preload();
  60. const size_t views = animation[type.first]->size(0);
  61. cache[type.first].resize(views);
  62. for(int j = 0; j < views; j++)
  63. cache[type.first][j] = animation[type.first]->getImage(j);
  64. }
  65. };
  66. std::map<std::string, std::string> terrainFiles;
  67. std::map<std::string, std::string> roadFiles;
  68. std::map<std::string, std::string> riverFiles;
  69. for(const auto & terrain : VLC->terrainTypeHandler->terrains())
  70. {
  71. terrainFiles[terrain.name] = terrain.tilesFilename;
  72. }
  73. for(const auto & river : VLC->terrainTypeHandler->rivers())
  74. {
  75. riverFiles[river.fileName] = river.fileName;
  76. }
  77. for(const auto & road : VLC->terrainTypeHandler->roads())
  78. {
  79. roadFiles[road.fileName] = road.fileName;
  80. }
  81. loadFlipped(terrainAnimations, terrainImages, terrainFiles);
  82. loadFlipped(riverAnimations, riverImages, riverFiles);
  83. loadFlipped(roadAnimations, roadImages, roadFiles);
  84. }
  85. void MapHandler::drawTerrainTile(QPainter & painter, int x, int y, int z)
  86. {
  87. auto & tinfo = map->getTile(int3(x, y, z));
  88. ui8 rotation = tinfo.extTileFlags % 4;
  89. //TODO: use ui8 instead of string key
  90. auto terrainName = tinfo.terType->name;
  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. //TODO: use ui8 instead of string key
  101. auto roadName = tinfo.roadType->fileName;
  102. if (tinfoUpper && tinfoUpper->roadType->id != Road::NO_ROAD)
  103. {
  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->id != Road::NO_ROAD) //print road from this tile
  113. {
  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->id == River::NO_RIVER)
  127. return;
  128. //TODO: use ui8 instead of string key
  129. auto riverName = tinfo.riverType->fileName;
  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, !hero->isStanding);
  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)
  269. {
  270. for(auto & object : getObjects(x, y, z))
  271. {
  272. const CGObjectInstance * obj = object.obj;
  273. if(!obj)
  274. {
  275. logGlobal->error("Stray map object that isn't fading");
  276. return;
  277. }
  278. uint8_t animationFrame = 0;
  279. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  280. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  281. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  282. if(objData.objBitmap)
  283. {
  284. auto pos = obj->getPosition();
  285. painter.drawImage(QPoint(x * tileSize, y * tileSize), *objData.objBitmap, object.rect);
  286. if(objData.flagBitmap)
  287. {
  288. if(x == pos.x && y == pos.y)
  289. painter.drawImage(QPoint((x - 2) * tileSize, (y - 1) * tileSize), *objData.flagBitmap);
  290. }
  291. }
  292. }
  293. }
  294. void MapHandler::drawObject(QPainter & painter, const TileObject & object)
  295. {
  296. const CGObjectInstance * obj = object.obj;
  297. if (!obj)
  298. {
  299. logGlobal->error("Stray map object that isn't fading");
  300. return;
  301. }
  302. uint8_t animationFrame = 0;
  303. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  304. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  305. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 0);
  306. if (objData.objBitmap)
  307. {
  308. auto pos = obj->getPosition();
  309. painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.objBitmap);
  310. if (objData.flagBitmap)
  311. {
  312. if(object.rect.x() == pos.x && object.rect.y() == pos.y)
  313. painter.drawImage(pos.x * tileSize - object.rect.x(), pos.y * tileSize - object.rect.y(), *objData.flagBitmap);
  314. }
  315. }
  316. }
  317. void MapHandler::drawObjectAt(QPainter & painter, const CGObjectInstance * obj, int x, int y)
  318. {
  319. if (!obj)
  320. {
  321. logGlobal->error("Stray map object that isn't fading");
  322. return;
  323. }
  324. uint8_t animationFrame = 0;
  325. auto objData = findObjectBitmap(obj, animationFrame, obj->ID == Obj::HERO ? 2 : 0);
  326. if(obj->ID == Obj::HERO && obj->tempOwner.isValidPlayer())
  327. objData.flagBitmap = findFlagBitmap(dynamic_cast<const CGHeroInstance*>(obj), 0, obj->tempOwner, 4);
  328. if (objData.objBitmap)
  329. {
  330. painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.objBitmap);
  331. if (objData.flagBitmap)
  332. painter.drawImage(QPoint((x + 1) * 32 - objData.objBitmap->width(), (y + 1) * 32 - objData.objBitmap->height()), *objData.flagBitmap);
  333. }
  334. }
  335. QRgb MapHandler::getTileColor(int x, int y, int z)
  336. {
  337. // if object at tile is owned - it will be colored as its owner
  338. for(auto & object : getObjects(x, y, z))
  339. {
  340. if(!object.obj->getBlockedPos().count(int3(x, y, z)))
  341. continue;
  342. PlayerColor player = object.obj->getOwner();
  343. if(player == PlayerColor::NEUTRAL)
  344. return graphics->neutralColor;
  345. else
  346. if (player < PlayerColor::PLAYER_LIMIT)
  347. return graphics->playerColors[player.getNum()];
  348. }
  349. // else - use terrain color (blocked version or normal)
  350. auto & tile = map->getTile(int3(x, y, z));
  351. auto color = tile.terType->minimapUnblocked;
  352. if (tile.blocked && (!tile.visitable))
  353. color = tile.terType->minimapBlocked;
  354. return qRgb(color[0], color[1], color[2]);
  355. }
  356. void MapHandler::drawMinimapTile(QPainter & painter, int x, int y, int z)
  357. {
  358. painter.setPen(getTileColor(x, y, z));
  359. painter.drawPoint(x, y);
  360. }
  361. void MapHandler::invalidate(int x, int y, int z)
  362. {
  363. auto & objects = getObjects(x, y, z);
  364. for(auto obj = objects.begin(); obj != objects.end();)
  365. {
  366. //object was removed
  367. if(std::find(map->objects.begin(), map->objects.end(), obj->obj) == map->objects.end())
  368. {
  369. obj = objects.erase(obj);
  370. continue;
  371. }
  372. //object was moved
  373. auto & pos = obj->obj->pos;
  374. if(pos.z != z || pos.x < x || pos.y < y || pos.x - obj->obj->getWidth() >= x || pos.y - obj->obj->getHeight() >= y)
  375. {
  376. obj = objects.erase(obj);
  377. continue;
  378. }
  379. ++obj;
  380. }
  381. stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
  382. }
  383. void MapHandler::invalidate(CGObjectInstance * obj)
  384. {
  385. std::shared_ptr<Animation> animation = graphics->getAnimation(obj);
  386. //no animation at all or empty animation
  387. if(!animation || animation->size(0) == 0)
  388. return;
  389. auto image = animation->getImage(0, obj->ID == Obj::HERO ? 2 : 0);
  390. if(!image)
  391. return;
  392. for(int fx=0; fx < obj->getWidth(); ++fx)
  393. {
  394. for(int fy=0; fy < obj->getHeight(); ++fy)
  395. {
  396. //object presented on the tile
  397. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  398. QRect cr(image->width() - fx * tileSize - tileSize, image->height() - fy * tileSize - tileSize, image->width(), image->height());
  399. if( map->isInTheMap(currTile) && // within map
  400. cr.x() + cr.width() > 0 && // image has data on this tile
  401. cr.y() + cr.height() > 0)
  402. {
  403. auto & objects = ttiles[index(currTile)];
  404. bool found = false;
  405. for(auto & o : objects)
  406. {
  407. if(o.obj == obj)
  408. {
  409. o.rect = cr;
  410. found = true;
  411. break;
  412. }
  413. }
  414. if(!found)
  415. objects.emplace_back(obj, cr);
  416. stable_sort(objects.begin(), objects.end(), objectBlitOrderSorter);
  417. }
  418. }
  419. }
  420. }
  421. std::vector<int3> MapHandler::getTilesUnderObject(CGObjectInstance * obj) const
  422. {
  423. std::vector<int3> result;
  424. for(int fx=0; fx < obj->getWidth(); ++fx)
  425. {
  426. for(int fy=0; fy < obj->getHeight(); ++fy)
  427. {
  428. //object presented on the tile
  429. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  430. if(map->isInTheMap(currTile)) // within map
  431. {
  432. result.push_back(currTile);
  433. }
  434. }
  435. }
  436. return result;
  437. }
  438. void MapHandler::invalidateObjects()
  439. {
  440. for(auto obj : map->objects)
  441. {
  442. invalidate(obj);
  443. }
  444. }
  445. void MapHandler::invalidate(const std::vector<int3> & tiles)
  446. {
  447. for(auto & currTile : tiles)
  448. {
  449. invalidate(currTile.x, currTile.y, currTile.z);
  450. }
  451. }