Dimension.cpp 6.0 KB

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