Dimension.cpp 5.0 KB

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