Dimension.cpp 9.1 KB

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