Dimension.cpp 7.8 KB

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