DimensionMap.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. void DimensionMap::api(Framework::InMemoryBuffer* zRequest,
  15. NetworkMessage* zResponse,
  16. Entity* zSource,
  17. Dimension* zDimension)
  18. {
  19. char type;
  20. zRequest->lese(&type, 1);
  21. switch (type)
  22. {
  23. case 0: // request chunk
  24. {
  25. Framework::Punkt location;
  26. zRequest->lese((char*)&location.x, 4);
  27. zRequest->lese((char*)&location.y, 4);
  28. location = Game::getChunkCenter(location.x, location.y);
  29. char addr[8];
  30. zDimension->getAddrOfWorld(location, addr);
  31. ChunkMap* res = getMap(addr, 8, location);
  32. // create an empty map for a chunk that does not yet exist
  33. if (!res) res = new ChunkMap(location);
  34. zResponse->sendMap(res);
  35. zResponse->setUseBackground();
  36. res->release();
  37. break;
  38. }
  39. case 1: // subscribe to changes
  40. {
  41. cs.lock();
  42. observers.add(zSource->getId());
  43. cs.unlock();
  44. break;
  45. }
  46. case 2: // unsubscribe from changes
  47. {
  48. cs.lock();
  49. observers.removeValue(zSource->getId());
  50. cs.unlock();
  51. break;
  52. }
  53. case 3: // player list request
  54. {
  55. InMemoryBuffer buff;
  56. int count = 0;
  57. for (Entity* entity : *zDimension->entities)
  58. {
  59. if (entity->zType()->getId() == EntityTypeEnum::PLAYER)
  60. {
  61. Player* p = dynamic_cast<Player*>(entity);
  62. char len = (char)textLength(p->getName());
  63. buff.schreibe(&len, 1);
  64. buff.schreibe(p->getName(), len);
  65. Vec3<float> pos = p->getPosition();
  66. buff.schreibe((char*)&pos.x, 4);
  67. buff.schreibe((char*)&pos.y, 4);
  68. buff.schreibe((char*)&pos.z, 4);
  69. count++;
  70. }
  71. }
  72. char* msg = new char[4 + buff.getSize()];
  73. *(int*)msg = count;
  74. buff.lese(msg + 4, (int)buff.getSize());
  75. zResponse->sendPlayerPositions(msg, 4 + (int)buff.getSize());
  76. break;
  77. }
  78. }
  79. }
  80. ChunkMap* DimensionMap::load(Framework::Punkt chunkCenter)
  81. {
  82. Framework::Datei file;
  83. Framework::Text filePath
  84. = Game::INSTANCE->getWorldDirectory() + "/dim/" + dimensionId + "/map/";
  85. filePath.appendHex(chunkCenter.x);
  86. filePath += "_";
  87. filePath.appendHex(chunkCenter.y);
  88. filePath += ".map";
  89. file.setDatei(filePath);
  90. if (file.open(Datei::Style::lesen))
  91. {
  92. ChunkMap* map = new ChunkMap(&file);
  93. file.close();
  94. return map;
  95. }
  96. return 0;
  97. }
  98. void DimensionMap::loadMap(char* addr, int addrLen, Chunk* zChunk)
  99. {
  100. cs.lock();
  101. if (chunks->z(addr, addrLen))
  102. {
  103. cs.unlock();
  104. return;
  105. }
  106. ChunkMap* map = load(zChunk->getCenter());
  107. if (!map)
  108. {
  109. map = new ChunkMap(zChunk);
  110. for (auto iterator = observers.begin(); iterator;)
  111. {
  112. Entity* e = Game::INSTANCE->zEntity(iterator.val());
  113. if (!e)
  114. {
  115. iterator.remove();
  116. continue;
  117. }
  118. else
  119. {
  120. NetworkMessage* msg = new NetworkMessage();
  121. msg->sendMap(map);
  122. msg->setUseBackground();
  123. Game::INSTANCE->sendMessage(msg, e);
  124. }
  125. iterator++;
  126. }
  127. }
  128. chunks->set(addr, addrLen, map);
  129. cs.unlock();
  130. }
  131. void DimensionMap::onMapUpdated(char* addr, int addrLen)
  132. {
  133. cs.lock();
  134. ChunkMap* map = chunks->z(addr, addrLen);
  135. if (map)
  136. {
  137. for (auto iterator = observers.begin(); iterator;)
  138. {
  139. Entity* e = Game::INSTANCE->zEntity(iterator.val());
  140. if (!e)
  141. {
  142. iterator.remove();
  143. continue;
  144. }
  145. else
  146. {
  147. NetworkMessage* msg = new NetworkMessage();
  148. msg->sendMap(map);
  149. msg->setUseBackground();
  150. Game::INSTANCE->sendMessage(msg, e);
  151. }
  152. iterator++;
  153. }
  154. }
  155. cs.unlock();
  156. }
  157. void DimensionMap::saveMap(char* addr, int addrLen)
  158. {
  159. cs.lock();
  160. ChunkMap* map = chunks->z(addr, addrLen);
  161. if (map)
  162. {
  163. Framework::Datei file;
  164. Framework::Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/"
  165. + dimensionId + "/map/";
  166. filePath.appendHex(map->getChunkCenter().x);
  167. filePath += "_";
  168. filePath.appendHex(map->getChunkCenter().y);
  169. filePath += ".map";
  170. file.setDatei(filePath);
  171. file.erstellen();
  172. if (file.open(Datei::Style::schreiben))
  173. {
  174. map->writeTo(&file);
  175. file.close();
  176. }
  177. else
  178. {
  179. Game::INSTANCE->zChat()->broadcastMessage(
  180. "Could not save map data. The map has to be recalulated at "
  181. "each chunk loading.",
  182. Chat::CHANNEL_WARNING);
  183. std::cout << "WARNING: could not open file '" << filePath.getText()
  184. << "' for writing.";
  185. }
  186. }
  187. cs.unlock();
  188. }
  189. void DimensionMap::removeMap(char* addr, int addrLen)
  190. {
  191. cs.lock();
  192. saveMap(addr, addrLen);
  193. chunks->remove(addr, addrLen);
  194. cs.unlock();
  195. }
  196. ChunkMap* DimensionMap::getMap(
  197. char* addr, int addrLen, Framework::Punkt chunkCenter)
  198. {
  199. cs.lock();
  200. ChunkMap* map = chunks->get(addr, addrLen);
  201. if (!map) map = load(chunkCenter);
  202. cs.unlock();
  203. return map;
  204. }