Dimension.cpp 8.1 KB

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