Dimension.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "Dimension.h"
  2. #include "Constants.h"
  3. #include "Datei.h"
  4. #include "Game.h"
  5. #include "Globals.h"
  6. #include "World.h"
  7. using namespace Framework;
  8. Dimension::Dimension()
  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::getAddrOf(Punkt cPos, char* addr) const
  18. {
  19. *(int*)addr = cPos.x;
  20. *((int*)addr + 1) = cPos.y;
  21. }
  22. void Dimension::getAddrOfWorld(Punkt wPos, char* addr) const
  23. {
  24. if (wPos.x < 0) wPos.x -= CHUNK_SIZE;
  25. if (wPos.y < 0) // needed because otherwise would (-8, -8) have the same
  26. // adress as (8, 8)
  27. wPos.y -= CHUNK_SIZE;
  28. wPos /= CHUNK_SIZE;
  29. getAddrOf(wPos, addr);
  30. }
  31. void Dimension::api(char* message)
  32. {
  33. switch (message[0])
  34. {
  35. case 1: // chunck
  36. {
  37. int cX = *(int*)(message + 1);
  38. int cY = *(int*)(message + 5);
  39. cs.lock();
  40. Chunk* ch = zChunk(Punkt(cX, cY));
  41. if (ch) ch->api(message + 9);
  42. cs.unlock();
  43. break;
  44. }
  45. case 2: // entity
  46. {
  47. int eId = *(int*)(message + 1);
  48. cs.lock();
  49. Entity* e = zEntity(eId);
  50. if (e) e->api(message + 5);
  51. cs.unlock();
  52. break;
  53. }
  54. case 3: // block
  55. {
  56. int px = *(int*)(message + 1);
  57. int py = *(int*)(message + 5);
  58. int pz = *(int*)(message + 9);
  59. cs.lock();
  60. Block* b = zBlock(Framework::Vec3<int>(px, py, pz));
  61. if (b) b->api(message + 13);
  62. cs.unlock();
  63. break;
  64. }
  65. case 4: // add new chunck
  66. {
  67. Punkt center;
  68. center.x = *(int*)(message + 1);
  69. center.y = *(int*)(message + 5);
  70. ByteArrayReader reader(message + 9, INT_MAX, 0);
  71. std::cout << "downloading chunk " << center.x << ", " << center.y
  72. << "\n";
  73. ZeitMesser zm;
  74. zm.messungStart();
  75. cs.lock();
  76. Chunk* chunk = new Chunk(center);
  77. chunk->load(&reader);
  78. zm.messungEnde();
  79. std::cout << "chunk loading took " << zm.getSekunden()
  80. << " seconds\n";
  81. setChunk(chunk, center);
  82. cs.unlock();
  83. World::INSTANCE->onChunkAdded(center);
  84. break;
  85. }
  86. }
  87. }
  88. Chunk* Dimension::zChunk(Punkt wPos) const
  89. {
  90. char addr[8];
  91. getAddrOfWorld(wPos, addr);
  92. return chunks->z(addr, 8);
  93. }
  94. Block* Dimension::zBlock(Vec3<int> location)
  95. {
  96. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  97. if (c) return c->zBlockAt(location);
  98. return 0;
  99. }
  100. Block* Dimension::getBlock(Vec3<int> location)
  101. {
  102. cs.lock();
  103. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  104. if (c)
  105. {
  106. Block* b = c->zBlockAt(location);
  107. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  108. cs.unlock();
  109. return b;
  110. }
  111. cs.unlock();
  112. return 0;
  113. }
  114. void Dimension::addEntity(Entity* entity)
  115. {
  116. entities->add(entity);
  117. World::INSTANCE->setVisibility(entity, 1);
  118. }
  119. void Dimension::setChunk(Chunk* chunk, Punkt center)
  120. {
  121. char addr[8];
  122. getAddrOfWorld(center, addr);
  123. Chunk* old = chunks->z(addr, 8);
  124. cs.lock();
  125. if (old)
  126. {
  127. World::INSTANCE->setVisibility(old, 0);
  128. int index = 0;
  129. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  130. {
  131. if ((Chunk*)iterator == old)
  132. {
  133. if (chunk)
  134. iterator.set(chunk);
  135. else
  136. chunkList.remove(index);
  137. break;
  138. }
  139. }
  140. }
  141. else if (chunk)
  142. chunkList.add(chunk);
  143. chunks->set(addr, 8, chunk);
  144. if (chunk) chunk->getThis();
  145. cs.unlock();
  146. if (chunk)
  147. {
  148. World::INSTANCE->setVisibility(chunk, 1);
  149. chunk->release();
  150. }
  151. }
  152. bool Dimension::hasChunck(int x, int y) const
  153. {
  154. return zChunk(Punkt(x, y));
  155. }
  156. void Dimension::removeDistantChunks(Punkt wPos)
  157. {
  158. Array<int> removed;
  159. int index = 0;
  160. for (Chunk* chunk : chunkList)
  161. {
  162. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE * 3)
  163. removed.add(index, 0);
  164. index++;
  165. }
  166. for (int i : removed)
  167. {
  168. cs.lock();
  169. Chunk* chunk = chunkList.get(i);
  170. World::INSTANCE->setVisibility(chunk, 0);
  171. setChunk(0, chunk->getCenter());
  172. cs.unlock();
  173. }
  174. }
  175. void Dimension::setBlock(Block* block)
  176. {
  177. cs.lock();
  178. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  179. (int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  180. if (c)
  181. c->setBlock(block);
  182. else
  183. block->release();
  184. cs.unlock();
  185. }
  186. void Dimension::removeBlock(Block* zBlock)
  187. {
  188. cs.lock();
  189. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  190. (int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  191. if (c) c->removeBlock(zBlock);
  192. cs.unlock();
  193. }
  194. Entity* Dimension::zEntity(int id)
  195. {
  196. cs.lock();
  197. for (Entity* e : *entities)
  198. {
  199. if (e->getId() == id)
  200. {
  201. cs.unlock();
  202. return e;
  203. }
  204. }
  205. cs.unlock();
  206. return 0;
  207. }
  208. Entity* Dimension::getEntity(int id)
  209. {
  210. cs.lock();
  211. for (Entity* e : *entities)
  212. {
  213. if (e->getId() == id)
  214. {
  215. Entity* result = dynamic_cast<Entity*>(e->getThis());
  216. cs.unlock();
  217. return result;
  218. }
  219. }
  220. cs.unlock();
  221. return 0;
  222. }
  223. void Dimension::removeEntity(int id)
  224. {
  225. World::INSTANCE->lockWorld();
  226. cs.lock();
  227. int index = 0;
  228. for (Entity* e : *entities)
  229. {
  230. if (e->getId() == id)
  231. {
  232. World::INSTANCE->setVisibility(e, 0);
  233. entities->remove(index);
  234. cs.unlock();
  235. World::INSTANCE->unlockWorld();
  236. return;
  237. }
  238. index++;
  239. }
  240. cs.unlock();
  241. World::INSTANCE->unlockWorld();
  242. }