Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

LandcellManager.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "LandcellManager.h"
  19. #include "Config.h"
  20. #include "Core.h"
  21. #include "DatFile.h"
  22. #include "Land.h"
  23. #include "Log.h"
  24. #include "Structure.h"
  25. #include <algorithm>
  26. LandcellManager::LandcellManager()
  27. {
  28. radius_ = Core::get().config().getInt("LandcellManager.radius", 5);
  29. }
  30. void LandcellManager::setCenter(LandcellId c)
  31. {
  32. if(c != center_)
  33. {
  34. LOG(Misc, Info) << "new center=" << c.x() << ", " << c.y() << "\n";
  35. center_ = c;
  36. load();
  37. }
  38. }
  39. LandcellId LandcellManager::center() const
  40. {
  41. return center_;
  42. }
  43. LandcellManager::iterator LandcellManager::find(LandcellId id)
  44. {
  45. return data_.find(id);
  46. }
  47. LandcellManager::iterator LandcellManager::begin()
  48. {
  49. return data_.begin();
  50. }
  51. LandcellManager::iterator LandcellManager::end()
  52. {
  53. return data_.end();
  54. }
  55. void LandcellManager::load()
  56. {
  57. // we grab more landblocks than we need so the ones that actually
  58. // get initialized have all their neighbors
  59. int sloppyRadius = radius_ + 2;
  60. for(int x = max(center_.x() - sloppyRadius, 0); x <= min(center_.x() + sloppyRadius, 0xFF); x++)
  61. {
  62. for(int y = max(center_.y() - sloppyRadius, 0); y <= min(center_.y() + sloppyRadius, 0xFF); y++)
  63. {
  64. LandcellId landId(x, y);
  65. if(center_.calcSquareDistance(landId) > sloppyRadius * sloppyRadius)
  66. {
  67. continue;
  68. }
  69. if(data_.find(landId) != data_.end())
  70. {
  71. continue;
  72. }
  73. vector<uint8_t> data = Core::get().cellDat().read(landId.value());
  74. if(data.empty())
  75. {
  76. continue;
  77. }
  78. data_[landId].reset(new Land(data.data(), data.size()));
  79. }
  80. }
  81. for(auto it = data_.begin(); it != data_.end(); ++it)
  82. {
  83. if(center_.calcSquareDistance(it->first) <= radius_ * radius_)
  84. {
  85. if(it->first.isStructure())
  86. {
  87. continue;
  88. }
  89. Land& land = static_cast<Land&>(*it->second);
  90. land.init();
  91. for(uint32_t i = 0; i < land.numStructures(); i++)
  92. {
  93. LandcellId structId(land.id().x(), land.id().y(), (uint16_t)(0x0100 + i));
  94. if(data_.find(structId) != data_.end())
  95. {
  96. continue;
  97. }
  98. vector<uint8_t> data = Core::get().cellDat().read(structId.value());
  99. if(data.empty())
  100. {
  101. throw runtime_error("Structure not found");
  102. }
  103. data_[structId].reset(new Structure(data.data(), data.size()));
  104. }
  105. }
  106. }
  107. for(auto it = data_.begin(); it != data_.end(); /**/)
  108. {
  109. if(center_.calcSquareDistance(it->first) > radius_ * radius_)
  110. {
  111. it = data_.erase(it);
  112. }
  113. else
  114. {
  115. ++it;
  116. }
  117. }
  118. }