Dimension.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. Framework::Vec3<int> location(x, y, z);
  97. for (int i = 0; i < 6; i++)
  98. {
  99. Framework::Vec3<int> pos
  100. = location + getDirection(getDirectionFromIndex(i));
  101. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  102. {
  103. Block* zB = zBlock(pos);
  104. if (zB)
  105. {
  106. bool visible = zB->isVisible();
  107. zB->setLightData(
  108. getOppositeDirection(getDirectionFromIndex(i)),
  109. (unsigned char*)(message + 13));
  110. if (zB->isVisible() != visible)
  111. {
  112. zChunk(World::INSTANCE->getChunkCenter(pos.x, pos.y))
  113. ->blockVisibilityChanged(zB);
  114. }
  115. }
  116. }
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. Chunk* Dimension::zChunk(Punkt wPos) const
  123. {
  124. char addr[8];
  125. getAddrOfWorld(wPos, addr);
  126. return chunks->z(addr, 8);
  127. }
  128. Block* Dimension::zBlock(Vec3<int> location)
  129. {
  130. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  131. if (c) return c->zBlockAt(location);
  132. return 0;
  133. }
  134. Block* Dimension::getBlock(Vec3<int> location)
  135. {
  136. cs.lock();
  137. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  138. if (c)
  139. {
  140. Block* b = c->zBlockAt(location);
  141. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  142. cs.unlock();
  143. return b;
  144. }
  145. cs.unlock();
  146. return 0;
  147. }
  148. void Dimension::addEntity(Entity* entity)
  149. {
  150. entities->add(entity);
  151. World::INSTANCE->setVisibility(entity, 1);
  152. }
  153. void Dimension::setChunk(Chunk* chunk, Punkt center)
  154. {
  155. char addr[8];
  156. getAddrOfWorld(center, addr);
  157. Chunk* old = chunks->z(addr, 8);
  158. cs.lock();
  159. if (old)
  160. {
  161. World::INSTANCE->setVisibility(old, 0);
  162. int index = 0;
  163. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  164. {
  165. if ((Chunk*)iterator == old)
  166. {
  167. if (chunk)
  168. iterator.set(chunk);
  169. else
  170. chunkList.remove(index);
  171. break;
  172. }
  173. }
  174. }
  175. else if (chunk)
  176. chunkList.add(chunk);
  177. chunks->set(addr, 8, chunk);
  178. if (chunk) chunk->getThis();
  179. cs.unlock();
  180. if (chunk)
  181. {
  182. World::INSTANCE->setVisibility(chunk, 1);
  183. chunk->release();
  184. }
  185. }
  186. bool Dimension::hasChunck(int x, int y) const
  187. {
  188. return zChunk(Punkt(x, y));
  189. }
  190. void Dimension::removeDistantChunks(Punkt wPos)
  191. {
  192. Array<int> removed;
  193. int index = 0;
  194. for (Chunk* chunk : chunkList)
  195. {
  196. if ((chunk->getCenter() - wPos).getLength() > MAX_VIEW_DISTANCE * 3)
  197. removed.add(index, 0);
  198. index++;
  199. }
  200. for (int i : removed)
  201. {
  202. cs.lock();
  203. Chunk* chunk = chunkList.get(i);
  204. World::INSTANCE->setVisibility(chunk, 0);
  205. setChunk(0, chunk->getCenter());
  206. cs.unlock();
  207. }
  208. }
  209. void Dimension::setBlock(Block* block)
  210. {
  211. cs.lock();
  212. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  213. (int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  214. if (c)
  215. c->setBlock(block);
  216. else
  217. block->release();
  218. cs.unlock();
  219. }
  220. void Dimension::removeBlock(Block* zBlock)
  221. {
  222. cs.lock();
  223. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  224. (int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  225. if (c) c->removeBlock(zBlock);
  226. cs.unlock();
  227. }
  228. Entity* Dimension::zEntity(int id)
  229. {
  230. cs.lock();
  231. for (Entity* e : *entities)
  232. {
  233. if (e->getId() == id)
  234. {
  235. cs.unlock();
  236. return e;
  237. }
  238. }
  239. cs.unlock();
  240. return 0;
  241. }
  242. Entity* Dimension::getEntity(int id)
  243. {
  244. cs.lock();
  245. for (Entity* e : *entities)
  246. {
  247. if (e->getId() == id)
  248. {
  249. Entity* result = dynamic_cast<Entity*>(e->getThis());
  250. cs.unlock();
  251. return result;
  252. }
  253. }
  254. cs.unlock();
  255. return 0;
  256. }
  257. void Dimension::removeEntity(int id)
  258. {
  259. World::INSTANCE->lockWorld();
  260. cs.lock();
  261. int index = 0;
  262. for (Entity* e : *entities)
  263. {
  264. if (e->getId() == id)
  265. {
  266. World::INSTANCE->setVisibility(e, 0);
  267. entities->remove(index);
  268. cs.unlock();
  269. World::INSTANCE->unlockWorld();
  270. return;
  271. }
  272. index++;
  273. }
  274. cs.unlock();
  275. World::INSTANCE->unlockWorld();
  276. }
  277. int Dimension::getId() const
  278. {
  279. return id;
  280. }