Dimension.cpp 7.5 KB

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