Dimension.cpp 7.5 KB

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