Dimension.cpp 8.5 KB

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