DimensionMap.cpp 6.0 KB

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