Dimension.cpp 8.7 KB

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