DimensionMap.cpp 5.8 KB

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