DimensionMap.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "DimensionMap.h"
  2. #include <Datei.h>
  3. #include "Game.h"
  4. DimensionMap::DimensionMap(int dimensionId)
  5. : ReferenceCounter(),
  6. dimensionId(dimensionId)
  7. {
  8. chunks = new Framework::Trie<ChunkMap>();
  9. }
  10. DimensionMap::~DimensionMap()
  11. {
  12. chunks->release();
  13. }
  14. ChunkMap* DimensionMap::load(Framework::Punkt chunkCenter)
  15. {
  16. Framework::Datei file;
  17. Framework::Text filePath
  18. = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimensionId + "/map/";
  19. filePath.appendHex(chunkCenter.x);
  20. filePath += "_";
  21. filePath.appendHex(chunkCenter.y);
  22. filePath += ".map";
  23. file.setDatei(filePath);
  24. if (file.open(Datei::Style::lesen))
  25. {
  26. ChunkMap* map = new ChunkMap(&file);
  27. file.close();
  28. return map;
  29. }
  30. return 0;
  31. }
  32. void DimensionMap::loadMap(char* addr, int addrLen, Chunk* zChunk)
  33. {
  34. cs.lock();
  35. if (chunks->z(addr, addrLen))
  36. {
  37. cs.unlock();
  38. return;
  39. }
  40. ChunkMap* map = load(zChunk->getCenter());
  41. if (!map) map = new ChunkMap(zChunk);
  42. chunks->set(addr, addrLen, map);
  43. cs.unlock();
  44. }
  45. void DimensionMap::saveMap(char* addr, int addrLen)
  46. {
  47. cs.lock();
  48. ChunkMap* map = chunks->z(addr, addrLen);
  49. if (map)
  50. {
  51. Framework::Datei file;
  52. Framework::Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/"
  53. + dimensionId + "/map/";
  54. filePath.appendHex(map->getChunkCenter().x);
  55. filePath += "_";
  56. filePath.appendHex(map->getChunkCenter().y);
  57. filePath += ".map";
  58. file.setDatei(filePath);
  59. file.erstellen();
  60. if (file.open(Datei::Style::schreiben))
  61. {
  62. map->writeTo(&file);
  63. file.close();
  64. }
  65. else
  66. {
  67. Game::INSTANCE->zChat()->broadcastMessage(
  68. "Could not save map data. The map has to be recalulated at "
  69. "each chunk loading.",
  70. Chat::CHANNEL_WARNING);
  71. std::cout << "WARNING: could not open file '" << filePath.getText()
  72. << "' for writing.";
  73. }
  74. }
  75. cs.unlock();
  76. }
  77. void DimensionMap::removeMap(char* addr, int addrLen)
  78. {
  79. cs.lock();
  80. saveMap(addr, addrLen);
  81. chunks->remove(addr, addrLen);
  82. cs.unlock();
  83. }
  84. ChunkMap* DimensionMap::getMap(
  85. char* addr, int addrLen, Framework::Punkt chunkCenter)
  86. {
  87. cs.lock();
  88. ChunkMap* map = chunks->get(addr, addrLen);
  89. if (!map) map = load(chunkCenter);
  90. cs.unlock();
  91. return map;
  92. }