maphandler.cpp 14 KB

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