Dimension.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. Chunk* chunk = new Chunk(center);
  70. chunk->load(&reader);
  71. setChunk(chunk, center);
  72. currentGame->onChunkAdded(center);
  73. break;
  74. }
  75. }
  76. }
  77. Chunk* Dimension::zChunk(Punkt wPos) const
  78. {
  79. char addr[8];
  80. getAddrOfWorld(wPos, addr);
  81. return chunks->z(addr, 8);
  82. }
  83. Block* Dimension::zBlock(Vec3<int> location)
  84. {
  85. Chunk* c = zChunk(currentGame->getChunkCenter(location.x, location.y));
  86. if (c)
  87. return c->zBlockAt(location);
  88. return 0;
  89. }
  90. Block* Dimension::getBlock(Vec3<int> location)
  91. {
  92. cs.lock();
  93. Chunk* c = zChunk(currentGame->getChunkCenter(location.x, location.y));
  94. if (c)
  95. {
  96. Block* b = c->zBlockAt(location);
  97. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  98. cs.unlock();
  99. return b;
  100. }
  101. cs.unlock();
  102. return 0;
  103. }
  104. void Dimension::addEntity(Entity* entity)
  105. {
  106. entities->add(entity);
  107. currentGame->setVisibility(entity, 1);
  108. }
  109. void Dimension::setChunk(Chunk* chunk, Punkt center)
  110. {
  111. char addr[8];
  112. getAddrOfWorld(center, addr);
  113. Chunk* old = chunks->z(addr, 8);
  114. cs.lock();
  115. if (old)
  116. {
  117. currentGame->setVisibility(old, 0);
  118. int index = 0;
  119. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  120. {
  121. if ((Chunk*)iterator == old)
  122. {
  123. if (chunk)
  124. iterator.set(chunk);
  125. else
  126. chunkList.remove(index);
  127. break;
  128. }
  129. }
  130. }
  131. else if (chunk)
  132. chunkList.add(chunk);
  133. chunks->set(addr, 8, chunk);
  134. if (chunk)
  135. {
  136. currentGame->setVisibility(chunk, 1);
  137. }
  138. cs.unlock();
  139. }
  140. bool Dimension::hasChunck(int x, int y) const
  141. {
  142. return zChunk(Punkt(x, y));
  143. }
  144. void Dimension::removeDistantChunks(Punkt wPos)
  145. {
  146. Array<int> removed;
  147. int index = 0;
  148. for (Chunk* chunk : chunkList)
  149. {
  150. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE * 2)
  151. removed.add(index, 0);
  152. index++;
  153. }
  154. for (int i : removed)
  155. {
  156. Chunk* chunk = chunkList.get(i);
  157. currentGame->setVisibility(chunk, 0);
  158. setChunk(0, chunk->getCenter());
  159. }
  160. }
  161. void Dimension::setBlock(Block* block)
  162. {
  163. cs.lock();
  164. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  165. if (c)
  166. c->setBlock(block);
  167. else
  168. block->release();
  169. cs.unlock();
  170. }
  171. void Dimension::removeBlock(Block* zBlock)
  172. {
  173. cs.lock();
  174. Chunk* c = zChunk(currentGame->getChunkCenter((int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  175. if (c)
  176. c->removeBlock(zBlock);
  177. cs.unlock();
  178. }
  179. Entity* Dimension::zEntity(int id)
  180. {
  181. cs.lock();
  182. for (Entity* e : *entities)
  183. {
  184. if (e->getId() == id)
  185. {
  186. cs.unlock();
  187. return e;
  188. }
  189. }
  190. cs.unlock();
  191. return 0;
  192. }
  193. Entity* Dimension::getEntity(int id)
  194. {
  195. cs.lock();
  196. for (Entity* e : *entities)
  197. {
  198. if (e->getId() == id)
  199. {
  200. Entity* result = dynamic_cast<Entity*>(e->getThis());
  201. cs.unlock();
  202. return result;
  203. }
  204. }
  205. cs.unlock();
  206. return 0;
  207. }
  208. void Dimension::removeEntity(int id)
  209. {
  210. currentGame->lockWorld();
  211. cs.lock();
  212. int index = 0;
  213. for (Entity* e : *entities)
  214. {
  215. if (e->getId() == id)
  216. {
  217. currentGame->setVisibility(e, 0);
  218. entities->remove(index);
  219. cs.unlock();
  220. currentGame->unlockWorld();
  221. return;
  222. }
  223. index++;
  224. }
  225. cs.unlock();
  226. currentGame->unlockWorld();
  227. }