Dimension.cpp 8.3 KB

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