123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "DimensionMap.h"
- #include <Datei.h>
- #include "Game.h"
- DimensionMap::DimensionMap(int dimensionId)
- : ReferenceCounter(),
- dimensionId(dimensionId)
- {
- chunks = new Framework::Trie<ChunkMap>();
- }
- DimensionMap::~DimensionMap()
- {
- chunks->release();
- }
- ChunkMap* DimensionMap::load(Framework::Punkt chunkCenter)
- {
- Framework::Datei file;
- Framework::Text filePath
- = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimensionId + "/map/";
- filePath.appendHex(chunkCenter.x);
- filePath += "_";
- filePath.appendHex(chunkCenter.y);
- filePath += ".map";
- file.setDatei(filePath);
- if (file.open(Datei::Style::lesen))
- {
- ChunkMap* map = new ChunkMap(&file);
- file.close();
- return map;
- }
- return 0;
- }
- void DimensionMap::loadMap(char* addr, int addrLen, Chunk* zChunk)
- {
- cs.lock();
- if (chunks->z(addr, addrLen))
- {
- cs.unlock();
- return;
- }
- ChunkMap* map = load(zChunk->getCenter());
- if (!map) map = new ChunkMap(zChunk);
- chunks->set(addr, addrLen, map);
- cs.unlock();
- }
- void DimensionMap::saveMap(char* addr, int addrLen)
- {
- cs.lock();
- ChunkMap* map = chunks->z(addr, addrLen);
- if (map)
- {
- Framework::Datei file;
- Framework::Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/"
- + dimensionId + "/map/";
- filePath.appendHex(map->getChunkCenter().x);
- filePath += "_";
- filePath.appendHex(map->getChunkCenter().y);
- filePath += ".map";
- file.setDatei(filePath);
- file.erstellen();
- if (file.open(Datei::Style::schreiben))
- {
- map->writeTo(&file);
- file.close();
- }
- else
- {
- Game::INSTANCE->zChat()->broadcastMessage(
- "Could not save map data. The map has to be recalulated at "
- "each chunk loading.",
- Chat::CHANNEL_WARNING);
- std::cout << "WARNING: could not open file '" << filePath.getText()
- << "' for writing.";
- }
- }
- cs.unlock();
- }
- void DimensionMap::removeMap(char* addr, int addrLen)
- {
- cs.lock();
- saveMap(addr, addrLen);
- chunks->remove(addr, addrLen);
- cs.unlock();
- }
- ChunkMap* DimensionMap::getMap(
- char* addr, int addrLen, Framework::Punkt chunkCenter)
- {
- cs.lock();
- ChunkMap* map = chunks->get(addr, addrLen);
- if (!map) map = load(chunkCenter);
- cs.unlock();
- return map;
- }
|