Dimension.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. Chunk* c
  131. = zChunk(World::INSTANCE->getChunkCenter(pos.x, pos.y));
  132. Block* zB = c ? c->zBlockAt(pos) : 0;
  133. if (zB)
  134. {
  135. bool visible = zB->isVisible();
  136. zB->setLightData(
  137. getOppositeDirection(getDirectionFromIndex(i)),
  138. (unsigned char*)(message + 13),
  139. c);
  140. if (zB->isVisible() != visible)
  141. {
  142. zChunk(
  143. World::INSTANCE->getChunkCenter(pos.x, pos.y))
  144. ->blockVisibilityChanged(zB);
  145. }
  146. }
  147. cs.unlock();
  148. }
  149. }
  150. break;
  151. }
  152. case 7: // add entity
  153. {
  154. int type = *(int*)(message + 1);
  155. ByteArrayReader reader(message + 5, INT_MAX, 0);
  156. Entity* entity = entityTypes[type]->loadEntity(&reader);
  157. addEntity(entity);
  158. break;
  159. }
  160. case 8: // remove entity
  161. {
  162. int id = *(int*)(message + 1);
  163. removeEntity(id);
  164. break;
  165. }
  166. }
  167. }
  168. Chunk* Dimension::zChunk(Punkt wPos) const
  169. {
  170. char addr[8];
  171. getAddrOfWorld(wPos, addr);
  172. return chunks->z(addr, 8);
  173. }
  174. Block* Dimension::zBlock(Vec3<int> location)
  175. {
  176. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  177. if (c) return c->zBlockAt(location);
  178. return 0;
  179. }
  180. Block* Dimension::getBlock(Vec3<int> location)
  181. {
  182. cs.lock();
  183. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(location.x, location.y));
  184. if (c)
  185. {
  186. Block* b = c->zBlockAt(location);
  187. b = b ? dynamic_cast<Block*>(b->getThis()) : 0;
  188. cs.unlock();
  189. return b;
  190. }
  191. cs.unlock();
  192. return 0;
  193. }
  194. void Dimension::addEntity(Entity* entity)
  195. {
  196. entities->add(entity);
  197. World::INSTANCE->setVisibility(entity, 1);
  198. }
  199. void Dimension::setChunk(Chunk* chunk, Punkt center)
  200. {
  201. char addr[8];
  202. getAddrOfWorld(center, addr);
  203. Chunk* old = chunks->z(addr, 8);
  204. cs.lock();
  205. if (old)
  206. {
  207. int index = 0;
  208. for (auto iterator = chunkList.begin(); iterator; ++iterator, ++index)
  209. {
  210. if ((Chunk*)iterator == old)
  211. {
  212. if (chunk)
  213. iterator.set(chunk);
  214. else
  215. chunkList.remove(index);
  216. break;
  217. }
  218. }
  219. }
  220. else if (chunk)
  221. chunkList.add(chunk);
  222. chunks->set(addr, 8, chunk);
  223. cs.unlock();
  224. }
  225. bool Dimension::hasChunck(int x, int y) const
  226. {
  227. return zChunk(Punkt(x, y));
  228. }
  229. void Dimension::removeDistantChunks(Punkt wPos)
  230. {
  231. Array<int> removed;
  232. int index = 0;
  233. for (Chunk* chunk : chunkList)
  234. {
  235. if (abs(chunk->getCenter().x - wPos.x) > MAX_VIEW_DISTANCE + CHUNK_SIZE
  236. || abs(chunk->getCenter().y - wPos.y)
  237. > MAX_VIEW_DISTANCE + CHUNK_SIZE)
  238. removed.add(index, 0);
  239. index++;
  240. }
  241. for (int i : removed)
  242. {
  243. cs.lock();
  244. Chunk* chunk = chunkList.get(i);
  245. chunk->destroy();
  246. setChunk(0, chunk->getCenter());
  247. cs.unlock();
  248. }
  249. }
  250. void Dimension::setBlock(Block* block)
  251. {
  252. cs.lock();
  253. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  254. (int)floor(block->getPos().x), (int)floor(block->getPos().y)));
  255. if (c)
  256. c->setBlock(block);
  257. else
  258. block->release();
  259. cs.unlock();
  260. }
  261. void Dimension::removeBlock(Block* zBlock)
  262. {
  263. cs.lock();
  264. Chunk* c = zChunk(World::INSTANCE->getChunkCenter(
  265. (int)floor(zBlock->getPos().x), (int)floor(zBlock->getPos().y)));
  266. if (c) c->removeBlock(zBlock);
  267. cs.unlock();
  268. }
  269. Entity* Dimension::zEntity(int id)
  270. {
  271. cs.lock();
  272. for (Entity* e : *entities)
  273. {
  274. if (e->getId() == id)
  275. {
  276. cs.unlock();
  277. return e;
  278. }
  279. }
  280. cs.unlock();
  281. return 0;
  282. }
  283. Entity* Dimension::getEntity(int id)
  284. {
  285. cs.lock();
  286. for (Entity* e : *entities)
  287. {
  288. if (e->getId() == id)
  289. {
  290. Entity* result = dynamic_cast<Entity*>(e->getThis());
  291. cs.unlock();
  292. return result;
  293. }
  294. }
  295. cs.unlock();
  296. return 0;
  297. }
  298. void Dimension::removeEntity(int id)
  299. {
  300. World::INSTANCE->lockWorld();
  301. cs.lock();
  302. int index = 0;
  303. for (Entity* e : *entities)
  304. {
  305. if (e->getId() == id)
  306. {
  307. World::INSTANCE->setVisibility(e, 0);
  308. entities->remove(index);
  309. cs.unlock();
  310. World::INSTANCE->unlockWorld();
  311. return;
  312. }
  313. index++;
  314. }
  315. cs.unlock();
  316. World::INSTANCE->unlockWorld();
  317. }
  318. int Dimension::getId() const
  319. {
  320. return id;
  321. }