Dimension.cpp 8.7 KB

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