Dimension.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. using namespace Framework;
  6. Dimension::Dimension(int id)
  7. : dimensionId(id),
  8. gravity(9.8f),
  9. chunks(new Trie<Chunk>()),
  10. entities(new RCArray<Entity>())
  11. {}
  12. Dimension::~Dimension()
  13. {
  14. entities->release();
  15. chunks->release();
  16. }
  17. void Dimension::api(Framework::InMemoryBuffer* zRequest, NetworkMessage* zResponse, Entity* zSource)
  18. {
  19. char type;
  20. zRequest->lese(&type, 1);
  21. switch (type)
  22. {
  23. case 0: // chunk message
  24. {
  25. Punkt center;
  26. zRequest->lese((char*)&center.x, 4);
  27. zRequest->lese((char*)&center.y, 4);
  28. cs.lock();
  29. Chunk* cC = zChunk(center);
  30. if (!cC)
  31. {
  32. // TODO: have a max amount of waiting requests per player
  33. waitingRequests.add({ dynamic_cast<InMemoryBuffer*>(zRequest->getThis()), center, zSource->getId() });
  34. Game::INSTANCE->requestArea({ center.x - CHUNK_SIZE / 2, center.y - CHUNK_SIZE / 2, center.x + CHUNK_SIZE / 2 - 1, center.y + CHUNK_SIZE / 2 - 1, dimensionId });
  35. }
  36. else
  37. {
  38. cC->api(zRequest, zSource);
  39. }
  40. cs.unlock();
  41. break;
  42. }
  43. }
  44. }
  45. void Dimension::tickEntities()
  46. {
  47. for (auto entity : *entities)
  48. {
  49. if (!entity->isRemoved() && zChunk(Punkt((int)entity->getPosition().x, (int)entity->getPosition().y)))
  50. entity->prepareTick(this);
  51. }
  52. int index = 0;
  53. for (auto entity : *entities)
  54. {
  55. if (!entity->isRemoved() && zChunk(Punkt((int)entity->getPosition().x, (int)entity->getPosition().y)))
  56. entity->tick(this);
  57. index++;
  58. }
  59. }
  60. void Dimension::getAddrOf(Punkt cPos, char* addr) const
  61. {
  62. *(int*)addr = cPos.x;
  63. *((int*)addr + 1) = cPos.y;
  64. }
  65. void Dimension::getAddrOfWorld(Punkt wPos, char* addr) const
  66. {
  67. if (wPos.x < 0)
  68. wPos.x -= CHUNK_SIZE;
  69. if (wPos.y < 0) // needed because otherwise would (-8, -8) have the same adress as (8, 8)
  70. wPos.y -= CHUNK_SIZE;
  71. wPos /= CHUNK_SIZE;
  72. getAddrOf(wPos, addr);
  73. }
  74. Chunk* Dimension::zChunk(Punkt wPos) const
  75. {
  76. char addr[8];
  77. getAddrOfWorld(wPos, addr);
  78. return chunks->z(addr, 8);
  79. }
  80. Framework::Either<Block*, int> Dimension::zBlock(Vec3<int> location)
  81. {
  82. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  83. if (c)
  84. {
  85. int x = location.x % CHUNK_SIZE;
  86. int y = location.y % CHUNK_SIZE;
  87. if (x < 0)
  88. x += CHUNK_SIZE;
  89. if (y < 0)
  90. y += CHUNK_SIZE;
  91. return c->zBlockAt(Vec3<int>(x, y, location.z));
  92. }
  93. return 0;
  94. }
  95. Block* Dimension::zRealBlockInstance(Framework::Vec3<int> location)
  96. {
  97. Chunk* c = zChunk(Game::INSTANCE->getChunkCenter(location.x, location.y));
  98. if (c)
  99. {
  100. int x = location.x % CHUNK_SIZE;
  101. int y = location.y % CHUNK_SIZE;
  102. if (x < 0)
  103. x += CHUNK_SIZE;
  104. if (y < 0)
  105. y += CHUNK_SIZE;
  106. c->instantiateBlock(Vec3<int>(x, y, location.z));
  107. return c->zBlockAt(Vec3<int>(x, y, location.z));
  108. }
  109. return 0;
  110. }
  111. void Dimension::placeBlock(Framework::Vec3<int> location, Framework::Either<Block*, int> block)
  112. {
  113. Chunk* c = zChunk(Game::getChunkCenter(location.x, location.y));
  114. if (c)
  115. {
  116. int x = location.x % CHUNK_SIZE;
  117. int y = location.y % CHUNK_SIZE;
  118. if (x < 0)
  119. x += CHUNK_SIZE;
  120. if (y < 0)
  121. y += CHUNK_SIZE;
  122. if (block.isA())
  123. c->putBlockAt(Vec3<int>(x, y, location.z), block);
  124. else
  125. {
  126. c->putBlockAt(Vec3<int>(x, y, location.z), 0);
  127. c->putBlockTypeAt(Vec3<int>(x, y, location.z), block);
  128. }
  129. }
  130. else if (block.isA())
  131. block.getA()->release();
  132. }
  133. void Dimension::addEntity(Entity* entity)
  134. {
  135. entities->add(entity);
  136. }
  137. void Dimension::setChunk(Chunk* chunk, Punkt center)
  138. {
  139. char addr[8];
  140. getAddrOfWorld(center, addr);
  141. Chunk* old = chunks->z(addr, 8);
  142. if (old)
  143. {
  144. for (int i = 0; i < chunkList.getEintragAnzahl(); i++)
  145. {
  146. if (chunkList.get(i) == old)
  147. {
  148. chunkList.remove(i);
  149. break;
  150. }
  151. }
  152. }
  153. chunks->set(addr, 8, chunk);
  154. if (chunk)
  155. {
  156. chunkList.add(chunk);
  157. chunk->setAdded();
  158. }
  159. getAddrOfWorld(center + Punkt(CHUNK_SIZE, 0), addr);
  160. Chunk* zChunk = chunks->z(addr, 8);
  161. if (zChunk)
  162. {
  163. zChunk->setNeighbor(WEST, chunk);
  164. if (chunk)
  165. chunk->setNeighbor(EAST, zChunk);
  166. }
  167. getAddrOfWorld(center + Punkt(-CHUNK_SIZE, 0), addr);
  168. zChunk = chunks->z(addr, 8);
  169. if (zChunk)
  170. {
  171. zChunk->setNeighbor(EAST, chunk);
  172. if (chunk)
  173. chunk->setNeighbor(WEST, zChunk);
  174. }
  175. getAddrOfWorld(center + Punkt(0, CHUNK_SIZE), addr);
  176. zChunk = chunks->z(addr, 8);
  177. if (zChunk)
  178. {
  179. zChunk->setNeighbor(NORTH, chunk);
  180. if (chunk)
  181. chunk->setNeighbor(SOUTH, zChunk);
  182. }
  183. getAddrOfWorld(center + Punkt(0, -CHUNK_SIZE), addr);
  184. zChunk = chunks->z(addr, 8);
  185. if (zChunk)
  186. {
  187. zChunk->setNeighbor(SOUTH, chunk);
  188. if (chunk)
  189. chunk->setNeighbor(NORTH, zChunk);
  190. }
  191. if (chunk)
  192. {
  193. cs.lock();
  194. int index = 0;
  195. for (Iterator<RequestQueue> iterator = waitingRequests.begin(); iterator; )
  196. {
  197. Entity* zE = Game::INSTANCE->zEntity(iterator.val().sourceId);
  198. if (zE)
  199. {
  200. if (iterator.val().chunkCenter == chunk->getCenter())
  201. {
  202. chunk->api(iterator.val().request, zE);
  203. iterator.val().request->release();
  204. iterator.remove();
  205. continue;
  206. }
  207. }
  208. else
  209. {
  210. iterator.val().request->release();
  211. iterator.remove();
  212. continue;
  213. }
  214. iterator++;
  215. index++;
  216. }
  217. cs.unlock();
  218. }
  219. }
  220. void Dimension::save(Text worldDir) const
  221. {
  222. for (auto chunk = chunks->getIterator(); chunk; chunk++)
  223. {
  224. if (!chunk._)
  225. continue;
  226. Datei* file = new Datei();
  227. Text filePath = worldDir + "/dim/" + dimensionId + "/";
  228. filePath.appendHex(chunk->getCenter().x);
  229. filePath += "_";
  230. filePath.appendHex(chunk->getCenter().y);
  231. filePath += ".chunk";
  232. file->setDatei(filePath);
  233. if (file->open(Datei::Style::schreiben))
  234. chunk->save(file);
  235. file->close();
  236. file->release();
  237. }
  238. Text filePath = worldDir + "/dim/" + dimensionId + "/entities";
  239. Datei* file = new Datei();
  240. file->setDatei(filePath);
  241. if (file->open(Datei::Style::schreiben))
  242. {
  243. for (Entity* entity : *entities)
  244. {
  245. if (entity->zType()->getId() != PlayerEntityType::ID)
  246. {
  247. if (!entity->isRemoved())
  248. {
  249. int type = entity->zType()->getId();
  250. file->schreibe((char*)&type, 4);
  251. StaticRegistry<EntityType>::INSTANCE.zElement(type)->saveEntity(entity, file);
  252. }
  253. }
  254. else
  255. {
  256. Datei pFile;
  257. pFile.setDatei(worldDir + "/player/" + ((Player*)entity)->getName());
  258. if (pFile.open(Datei::Style::schreiben))
  259. PlayerEntityType::INSTANCE->saveEntity(entity, &pFile);
  260. }
  261. }
  262. file->close();
  263. }
  264. }
  265. int Dimension::getDimensionId() const
  266. {
  267. return dimensionId;
  268. }
  269. bool Dimension::hasChunck(int x, int y) const
  270. {
  271. return zChunk(Punkt(x, y));
  272. }
  273. float Dimension::getGravity() const
  274. {
  275. return gravity;
  276. }
  277. void Dimension::removeOldChunks()
  278. {
  279. Array<int> removed;
  280. int index = 0;
  281. for (Chunk* chunk : chunkList)
  282. {
  283. if (!chunk->hasObservers())
  284. removed.add(index, 0);
  285. index++;
  286. }
  287. for (int i : removed)
  288. {
  289. Chunk* chunk = chunkList.get(i);
  290. // TODO: save chunk in a seperate thread?
  291. Text filePath = Game::INSTANCE->getWorldDirectory() + "/dim/" + getDimensionId() + "/";
  292. filePath.appendHex(chunk->getCenter().x);
  293. filePath += "_";
  294. filePath.appendHex(chunk->getCenter().y);
  295. filePath += ".chunk";
  296. Datei d;
  297. d.setDatei(filePath);
  298. d.open(Datei::Style::schreiben);
  299. chunk->save(&d);
  300. d.close();
  301. chunk->prepareRemove();
  302. setChunk(0, chunk->getCenter());
  303. }
  304. }
  305. Entity* Dimension::zEntity(int id)
  306. {
  307. for (auto entity : *entities)
  308. {
  309. if (!entity->isRemoved() && entity->getId() == id)
  310. return entity;
  311. }
  312. return 0;
  313. }
  314. Entity* Dimension::zNearestEntity(Framework::Vec3<float> pos, std::function<bool(Entity*)> filter)
  315. {
  316. Entity* result = 0;
  317. float sqDist = 0;
  318. for (auto entity : *entities)
  319. {
  320. if (!entity->isRemoved() && filter(entity))
  321. {
  322. float d = pos.abstandSq(entity->getPosition());
  323. if (!result || d < sqDist)
  324. {
  325. result = entity;
  326. sqDist = d;
  327. }
  328. }
  329. }
  330. return result;
  331. }
  332. void Dimension::removeEntity(int id)
  333. {
  334. int index = 0;
  335. for (auto entity : *entities)
  336. {
  337. if (entity->getId() == id)
  338. {
  339. entities->remove(index);
  340. return;
  341. }
  342. index++;
  343. }
  344. }