MapRendererContextState.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../adventureMap/CAdvMapInt.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 CGI->mh->compareObjectBlitOrder(CGI->mh->getMap()->objects[left.getNum()], CGI->mh->getMap()->objects[right.getNum()]);
  24. }
  25. MapRendererContextState::MapRendererContextState()
  26. {
  27. auto mapSize = LOCPLINT->cb->getMapSize();
  28. objects.resize(boost::extents[mapSize.z][mapSize.x][mapSize.y]);
  29. for(const auto & obj : CGI->mh->getMap()->objects)
  30. addObject(obj);
  31. }
  32. void MapRendererContextState::addObject(const CGObjectInstance * obj)
  33. {
  34. if(!obj)
  35. return;
  36. for(int fx = 0; fx < obj->getWidth(); ++fx)
  37. {
  38. for(int fy = 0; fy < obj->getHeight(); ++fy)
  39. {
  40. int3 currTile(obj->pos.x - fx, obj->pos.y - fy, obj->pos.z);
  41. if(LOCPLINT->cb->isInTheMap(currTile) && obj->coveringAt(currTile.x, currTile.y))
  42. {
  43. auto & container = objects[currTile.z][currTile.x][currTile.y];
  44. container.push_back(obj->id);
  45. boost::range::sort(container, compareObjectBlitOrder);
  46. }
  47. }
  48. }
  49. }
  50. void MapRendererContextState::addMovingObject(const CGObjectInstance * object, const int3 & tileFrom, const int3 & tileDest)
  51. {
  52. int xFrom = std::min(tileFrom.x, tileDest.x) - object->getWidth();
  53. int xDest = std::max(tileFrom.x, tileDest.x);
  54. int yFrom = std::min(tileFrom.y, tileDest.y) - object->getHeight();
  55. int yDest = std::max(tileFrom.y, tileDest.y);
  56. for(int x = xFrom; x <= xDest; ++x)
  57. {
  58. for(int y = yFrom; y <= yDest; ++y)
  59. {
  60. int3 currTile(x, y, object->pos.z);
  61. if(LOCPLINT->cb->isInTheMap(currTile))
  62. {
  63. auto & container = objects[currTile.z][currTile.x][currTile.y];
  64. container.push_back(object->id);
  65. boost::range::sort(container, compareObjectBlitOrder);
  66. }
  67. }
  68. }
  69. }
  70. void MapRendererContextState::removeObject(const CGObjectInstance * object)
  71. {
  72. for(int z = 0; z < LOCPLINT->cb->getMapSize().z; z++)
  73. for(int x = 0; x < LOCPLINT->cb->getMapSize().x; x++)
  74. for(int y = 0; y < LOCPLINT->cb->getMapSize().y; y++)
  75. vstd::erase(objects[z][x][y], object->id);
  76. }