Dimension.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. currentGame->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(currentGame->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(currentGame->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. currentGame->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. currentGame->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. {
  140. currentGame->setVisibility(chunk, 1);
  141. }
  142. cs.unlock();
  143. }
  144. bool Dimension::hasChunck(int x, int y) const
  145. {
  146. return zChunk(Punkt(x, y));
  147. }
  148. void Dimension::removeDistantChunks(Punkt wPos)
  149. {
  150. Array<int> removed;
  151. int index = 0;
  152. for (Chunk* chunk : chunkList)
  153. {
  154. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE * 2)
  155. removed.add(index, 0);
  156. index++;
  157. }
  158. for (int i : removed)
  159. {
  160. Chunk* chunk = chunkList.get(i);
  161. currentGame->setVisibility(chunk, 0);
  162. setChunk(0, chunk->getCenter());
  163. }
  164. }
  165. void Dimension::setBlock(Block* block)
  166. {
  167. cs.lock();
  168. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  169. if (c)
  170. c->setBlock(block);
  171. else
  172. block->release();
  173. cs.unlock();
  174. }
  175. void Dimension::removeBlock(Block* zBlock)
  176. {
  177. cs.lock();
  178. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  179. if (c)
  180. c->removeBlock(zBlock);
  181. cs.unlock();
  182. }
  183. Entity* Dimension::zEntity(int id)
  184. {
  185. cs.lock();
  186. for (Entity* e : *entities)
  187. {
  188. if (e->getId() == id)
  189. {
  190. cs.unlock();
  191. return e;
  192. }
  193. }
  194. cs.unlock();
  195. return 0;
  196. }
  197. Entity* Dimension::getEntity(int id)
  198. {
  199. cs.lock();
  200. for (Entity* e : *entities)
  201. {
  202. if (e->getId() == id)
  203. {
  204. Entity* result = dynamic_cast<Entity*>(e->getThis());
  205. cs.unlock();
  206. return result;
  207. }
  208. }
  209. cs.unlock();
  210. return 0;
  211. }
  212. void Dimension::removeEntity(int id)
  213. {
  214. currentGame->lockWorld();
  215. cs.lock();
  216. int index = 0;
  217. for (Entity* e : *entities)
  218. {
  219. if (e->getId() == id)
  220. {
  221. currentGame->setVisibility(e, 0);
  222. entities->remove(index);
  223. cs.unlock();
  224. currentGame->unlockWorld();
  225. return;
  226. }
  227. index++;
  228. }
  229. cs.unlock();
  230. currentGame->unlockWorld();
  231. }