Dimension.cpp 8.9 KB

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