MapRendererContextState.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * MapRendererContext.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. #include "StdInc.h"
  11. #include "MapRendererContextState.h"
  12. #include "IMapRendererContext.h"
  13. #include "mapHandler.h"
  14. #include "../../CCallback.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../GameInstance.h"
  17. #include "../adventureMap/AdventureMapInterface.h"
  18. #include "../../lib/mapObjects/CGHeroInstance.h"
  19. #include "../../lib/mapping/CMap.h"
  20. static bool compareObjectBlitOrder(ObjectInstanceID left, ObjectInstanceID right)
  21. {
  22. //FIXME: remove mh access
  23. return GAME->map().compareObjectBlitOrder(GAME->map().getMap()->objects[left.getNum()], GAME->map().getMap()->objects[right.getNum()]);
  24. }
  25. MapRendererContextState::MapRendererContextState()
  26. {
  27. auto mapSize = GAME->interface()->cb->getMapSize();
  28. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  29. logGlobal->debug("Loading map objects");
  30. for(const auto & obj : GAME->map().getMap()->objects)
  31. addObject(obj);
  32. logGlobal->debug("Done loading map objects");
  33. }
  34. void MapRendererContextState::addObject(const CGObjectInstance * obj)
  35. {
  36. if(!obj)
  37. return;
  38. for(int fx = 0; fx < obj->getWidth(); ++fx)
  39. {
  40. for(int fy = 0; fy < obj->getHeight(); ++fy)
  41. {
  42. int3 currTile(obj->anchorPos().x - fx, obj->anchorPos().y - fy, obj->anchorPos().z);
  43. if(GAME->interface()->cb->isInTheMap(currTile) && obj->coveringAt(currTile))
  44. {
  45. auto & container = objects[currTile.z][currTile.x][currTile.y];
  46. auto position = std::upper_bound(container.begin(), container.end(), obj->id, compareObjectBlitOrder);
  47. container.insert(position, obj->id);
  48. }
  49. }
  50. }
  51. }
  52. void MapRendererContextState::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  53. {
  54. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  55. int xDest = std::max(tileFrom.x, tileDest.x);
  56. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  57. int yDest = std::max(tileFrom.y, tileDest.y);
  58. for(int x = xFrom; x <= xDest; ++x)
  59. {
  60. for(int y = yFrom; y <= yDest; ++y)
  61. {
  62. int3 currTile(x, y, object->anchorPos().z);
  63. if(GAME->interface()->cb->isInTheMap(currTile))
  64. {
  65. auto & container = objects[currTile.z][currTile.x][currTile.y];
  66. container.push_back(object->id);
  67. boost::range::sort(container, compareObjectBlitOrder);
  68. }
  69. }
  70. }
  71. }
  72. void MapRendererContextState::removeObject(const CGObjectInstance * object)
  73. {
  74. for(int z = 0; z < GAME->interface()->cb->getMapSize().z; z++)
  75. for(int x = 0; x < GAME->interface()->cb->getMapSize().x; x++)
  76. for(int y = 0; y < GAME->interface()->cb->getMapSize().y; y++)
  77. vstd::erase(objects[z][x][y], object->id);
  78. }