maphandler.cpp 14 KB

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