Chunk.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include <InMemoryBuffer.h>
  2. #include "Chunk.h"
  3. #include "Constants.h"
  4. #include "Game.h"
  5. #include "NoBlock.h"
  6. Chunk::Chunk(Framework::Punkt location, int dimensionId)
  7. : ReferenceCounter(),
  8. dimensionId(dimensionId),
  9. location(location),
  10. added(0)
  11. {
  12. blocks = new Block * [CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  13. blockIds = new unsigned short[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  14. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  15. memset(blockIds, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(unsigned short));
  16. zNeighbours[0] = 0;
  17. zNeighbours[1] = 0;
  18. zNeighbours[2] = 0;
  19. zNeighbours[3] = 0;
  20. }
  21. Chunk::Chunk(Framework::Punkt location, int dimensionId, Framework::StreamReader* zReader)
  22. : Chunk(location, dimensionId)
  23. {
  24. load(zReader);
  25. }
  26. Chunk::~Chunk()
  27. {
  28. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  29. {
  30. if (blocks[i])
  31. blocks[i]->release();
  32. }
  33. delete[] blocks;
  34. delete[] blockIds;
  35. }
  36. Framework::Either<Block*, int> Chunk::zBlockNeighbor(Framework::Vec3<int> location)
  37. {
  38. if (location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0 && location.y < CHUNK_SIZE && location.z >= 0 && location.z < WORLD_HEIGHT)
  39. {
  40. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  41. if (blocks[index])
  42. return blocks[index];
  43. else
  44. return (int)blockIds[index];
  45. }
  46. if (added && location.z >= 0 && location.z < WORLD_HEIGHT)
  47. return Game::INSTANCE->zBlockAt({ location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId);
  48. return 0;
  49. }
  50. void Chunk::notifyObservers(NetworkMessage& msg)
  51. {
  52. Array<int> remove;
  53. int index = 0;
  54. for (int id : observers)
  55. {
  56. Entity* zE = Game::INSTANCE->zEntity(id);
  57. if (!zE)
  58. remove.add(index, 0);
  59. else
  60. Game::INSTANCE->sendMessage(&msg, zE);
  61. index++;
  62. }
  63. for (int i : remove)
  64. observers.remove(i);
  65. }
  66. void Chunk::addObserver(Entity* zEntity)
  67. {
  68. for (int id : observers)
  69. {
  70. if (id == zEntity->getId())
  71. return;
  72. }
  73. observers.add(zEntity->getId());
  74. InMemoryBuffer buffer;
  75. buffer.schreibe("\4", 1);
  76. buffer.schreibe((char*)&location.x, 4);
  77. buffer.schreibe((char*)&location.y, 4);
  78. sendToClient(&buffer);
  79. NetworkMessage msg;
  80. msg.addressDimension();
  81. char* message = new char[buffer.getSize()];
  82. buffer.lese(message, (int)buffer.getSize());
  83. msg.setMessage(message, (int)buffer.getSize(), 1);
  84. msg.setUseBackground();
  85. Game::INSTANCE->sendMessage(&msg, zEntity);
  86. }
  87. void Chunk::removeObserver(Entity* zEntity)
  88. {
  89. int index = 0;
  90. for (int id : observers)
  91. {
  92. if (id == zEntity->getId())
  93. {
  94. observers.remove(index);
  95. return;
  96. }
  97. index++;
  98. }
  99. }
  100. void Chunk::api(Framework::StreamReader* zRequest, Entity* zSource)
  101. {
  102. // TODO: answer api messages
  103. char type;
  104. zRequest->lese(&type, 1);
  105. switch (type)
  106. {
  107. case 0:
  108. // register observer
  109. addObserver(zSource);
  110. break;
  111. case 1:
  112. // unsubscribe
  113. removeObserver(zSource);
  114. break;
  115. }
  116. }
  117. Framework::Either<Block*, int> Chunk::zBlockAt(Framework::Vec3<int> location) const
  118. {
  119. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  120. assert(index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT);
  121. if (blocks[index])
  122. return blocks[index];
  123. else
  124. return (int)blockIds[index];
  125. }
  126. const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
  127. {
  128. auto b = zBlockAt(location);
  129. if (b.isA())
  130. return b;
  131. if (b.getB())
  132. return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->zDefault();
  133. return 0;
  134. }
  135. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  136. {
  137. auto b = zBlockAt(location);
  138. if (b.isA())
  139. return;
  140. if (!b.getB())
  141. generateBlock(location);
  142. b = zBlockAt(location);
  143. if (b.isB())
  144. putBlockAt(location, StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->createBlockAt({ location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, 0));
  145. }
  146. void Chunk::generateBlock(Framework::Vec3<int> location)
  147. {
  148. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  149. if (blockIds[index])
  150. return;
  151. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock({ location.x + this->location.x - CHUNK_SIZE / 2, location.y + this->location.y - CHUNK_SIZE / 2, location.z }, dimensionId);
  152. if (generated.isA())
  153. putBlockAt(location, generated);
  154. else
  155. putBlockTypeAt(location, generated);
  156. }
  157. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  158. {
  159. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  160. assert(index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT&& index >= 0);
  161. Block* old = blocks[index];
  162. bool change = 0;
  163. if (block)
  164. {
  165. change = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  166. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  167. }
  168. else
  169. {
  170. change = blocks[index] != 0;
  171. }
  172. blocks[index] = block;
  173. Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(NORTH));
  174. if (neighbor.isA())
  175. ((Block*)neighbor)->setNeighbour(SOUTH, block);
  176. if (block)
  177. block->setNeighbour(NORTH, neighbor);
  178. neighbor = zBlockNeighbor(location + getDirection(EAST));
  179. if (neighbor.isA())
  180. ((Block*)neighbor)->setNeighbour(WEST, block);
  181. if (block)
  182. block->setNeighbour(EAST, neighbor);
  183. neighbor = zBlockNeighbor(location + getDirection(SOUTH));
  184. if (neighbor.isA())
  185. ((Block*)neighbor)->setNeighbour(NORTH, block);
  186. if (block)
  187. block->setNeighbour(SOUTH, neighbor);
  188. neighbor = zBlockNeighbor(location + getDirection(WEST));
  189. if (neighbor.isA())
  190. ((Block*)neighbor)->setNeighbour(EAST, block);
  191. if (block)
  192. block->setNeighbour(WEST, neighbor);
  193. neighbor = zBlockNeighbor(location + getDirection(TOP));
  194. if (neighbor.isA())
  195. ((Block*)neighbor)->setNeighbour(BOTTOM, block);
  196. if (block)
  197. block->setNeighbour(TOP, neighbor);
  198. neighbor = zBlockNeighbor(location + getDirection(BOTTOM));
  199. if (neighbor.isA())
  200. ((Block*)neighbor)->setNeighbour(TOP, block);
  201. if (block)
  202. block->setNeighbour(BOTTOM, neighbor);
  203. if (old)
  204. old->release();
  205. if (change)
  206. {
  207. char msg[9];
  208. msg[0] = 0; // set block
  209. *(int*)(msg + 1) = index;
  210. *(int*)(msg + 5) = block ? block->zBlockType()->getId() : NoBlockBlockType::ID;
  211. NetworkMessage message;
  212. message.addressChunck(this);
  213. message.setMessage(msg, 9, 0);
  214. notifyObservers(message);
  215. }
  216. }
  217. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  218. {
  219. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  220. assert(index < CHUNK_SIZE* CHUNK_SIZE* WORLD_HEIGHT);
  221. blockIds[index] = (unsigned short)type;
  222. Either<Block*, int> neighbor = zBlockNeighbor(location + getDirection(NORTH));
  223. if (neighbor.isA())
  224. ((Block*)neighbor)->setNeighbourType(SOUTH, type);
  225. neighbor = zBlockNeighbor(location + getDirection(EAST));
  226. if (neighbor.isA())
  227. ((Block*)neighbor)->setNeighbourType(WEST, type);
  228. neighbor = zBlockNeighbor(location + getDirection(SOUTH));
  229. if (neighbor.isA())
  230. ((Block*)neighbor)->setNeighbourType(NORTH, type);
  231. neighbor = zBlockNeighbor(location + getDirection(WEST));
  232. if (neighbor.isA())
  233. ((Block*)neighbor)->setNeighbourType(EAST, type);
  234. neighbor = zBlockNeighbor(location + getDirection(TOP));
  235. if (neighbor.isA())
  236. ((Block*)neighbor)->setNeighbourType(BOTTOM, type);
  237. neighbor = zBlockNeighbor(location + getDirection(BOTTOM));
  238. if (neighbor.isA())
  239. ((Block*)neighbor)->setNeighbourType(TOP, type);
  240. char msg[9];
  241. msg[0] = 0; // set block
  242. *(int*)(msg + 1) = index;
  243. *(int*)(msg + 5) = type;
  244. NetworkMessage message;
  245. message.addressChunck(this);
  246. message.setMessage(msg, 9, 0);
  247. notifyObservers(message);
  248. }
  249. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  250. {
  251. zNeighbours[getDirectionIndex(dir)] = zChunk;
  252. for (int i = 0; i < CHUNK_SIZE; i++)
  253. {
  254. for (int z = 0; z < WORLD_HEIGHT; z++)
  255. {
  256. if (dir == NORTH)
  257. {
  258. int index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  259. if (blocks[index])
  260. {
  261. int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  262. if (zChunk && zChunk->blocks[j])
  263. blocks[index]->setNeighbour(NORTH, zChunk->blocks[j]);
  264. else
  265. {
  266. blocks[index]->setNeighbour(NORTH, 0);
  267. blocks[index]->setNeighbourType(NORTH, zChunk ? zChunk->blockIds[j] : 0);
  268. }
  269. }
  270. }
  271. else if (dir == EAST)
  272. {
  273. int index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  274. if (blocks[index])
  275. {
  276. int j = i * WORLD_HEIGHT + z;
  277. if (zChunk && zChunk->blocks[j])
  278. blocks[index]->setNeighbour(EAST, zChunk->blocks[j]);
  279. else
  280. {
  281. blocks[index]->setNeighbour(EAST, 0);
  282. blocks[index]->setNeighbourType(EAST, zChunk ? zChunk->blockIds[j] : 0);
  283. }
  284. }
  285. }
  286. else if (dir == SOUTH)
  287. {
  288. int index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  289. if (blocks[index])
  290. {
  291. int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  292. if (zChunk && zChunk->blocks[j])
  293. blocks[index]->setNeighbour(SOUTH, zChunk->blocks[j]);
  294. else
  295. {
  296. blocks[index]->setNeighbour(SOUTH, 0);
  297. blocks[index]->setNeighbourType(SOUTH, zChunk ? zChunk->blockIds[j] : 0);
  298. }
  299. }
  300. }
  301. else if (dir == WEST)
  302. {
  303. int index = i * WORLD_HEIGHT + z;
  304. if (blocks[index])
  305. {
  306. int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  307. if (zChunk && zChunk->blocks[j])
  308. blocks[index]->setNeighbour(WEST, zChunk->blocks[j]);
  309. else
  310. {
  311. blocks[index]->setNeighbour(WEST, 0);
  312. blocks[index]->setNeighbourType(WEST, zChunk ? zChunk->blockIds[j] : 0);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. void Chunk::load(Framework::StreamReader* zReader)
  320. {
  321. unsigned short id = 0;
  322. zReader->lese((char*)&id, 2);
  323. Framework::Vec3<int> pos;
  324. bool d = 0;
  325. while (id)
  326. {
  327. zReader->lese((char*)&pos.x, 4);
  328. zReader->lese((char*)&pos.y, 4);
  329. zReader->lese((char*)&pos.z, 4);
  330. zReader->lese((char*)&d, 1);
  331. if (d)
  332. putBlockAt(pos, StaticRegistry<BlockType>::INSTANCE.zElement(id)->loadBlock(Framework::Vec3<int>(pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z), zReader));
  333. else
  334. putBlockTypeAt(pos, id);
  335. zReader->lese((char*)&id, 2);
  336. }
  337. }
  338. void Chunk::save(Framework::StreamWriter* zWriter)
  339. {
  340. for (int x = 0; x < CHUNK_SIZE; x++)
  341. {
  342. for (int y = 0; y < CHUNK_SIZE; y++)
  343. {
  344. for (int z = 0; z < WORLD_HEIGHT; z++)
  345. {
  346. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  347. unsigned short blockType = blocks[index] ? (unsigned short)blocks[index]->zBlockType()->getId() : blockIds[index];
  348. if (blockType)
  349. {
  350. zWriter->schreibe((char*)&blockType, 2);
  351. zWriter->schreibe((char*)&x, 4);
  352. zWriter->schreibe((char*)&y, 4);
  353. zWriter->schreibe((char*)&z, 4);
  354. if (blocks[index])
  355. {
  356. bool d = 1;
  357. zWriter->schreibe((char*)&d, 1);
  358. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)->saveBlock(blocks[index], zWriter);
  359. }
  360. else
  361. {
  362. bool d = 0;
  363. zWriter->schreibe((char*)&d, 1);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. unsigned short end = 0;
  370. zWriter->schreibe((char*)&end, 2);
  371. }
  372. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  373. {
  374. for (int x = 0; x < CHUNK_SIZE; x++)
  375. {
  376. for (int y = 0; y < CHUNK_SIZE; y++)
  377. {
  378. for (int z = 0; z < WORLD_HEIGHT; z++)
  379. {
  380. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  381. unsigned short blockType = blocks[index] ? (unsigned short)blocks[index]->zBlockType()->getId() : blockIds[index];
  382. if (blockType)
  383. {
  384. bool visible = 0;
  385. if (!visible)
  386. {
  387. if (!blocks[index])
  388. {
  389. if (CONST_BLOCK(0, blockIds[index])->isTransparent() || CONST_BLOCK(0, blockIds[index])->isPassable())
  390. visible = 1;
  391. else
  392. {
  393. for (int d = 0; d < 6 && !visible; d++)
  394. {
  395. auto n = zBlockNeighbor(getDirection((Directions)getDirectionFromIndex(d)) + Framework::Vec3<int>(x, y, z));
  396. if (n.isA() && (((Block*)n)->isPassable() || ((Block*)n)->isTransparent()))
  397. visible = 1;
  398. if (n.isB() && (CONST_BLOCK(0, n)->isTransparent() || CONST_BLOCK(0, n)->isPassable()))
  399. visible = 1;
  400. }
  401. }
  402. }
  403. else
  404. visible = blocks[index]->isVisible();
  405. }
  406. if (visible && (blocks[index] || blockType != AirBlockBlockType::ID))
  407. {
  408. zWriter->schreibe((char*)&blockType, 2);
  409. zWriter->schreibe((char*)&x, 4);
  410. zWriter->schreibe((char*)&y, 4);
  411. zWriter->schreibe((char*)&z, 4);
  412. }
  413. }
  414. }
  415. }
  416. }
  417. unsigned short end = 0;
  418. zWriter->schreibe((char*)&end, 2);
  419. }
  420. void Chunk::removeUnusedBlocks()
  421. {
  422. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  423. {
  424. if (blocks[i])
  425. {
  426. if (!blocks[i]->isVisible())
  427. {
  428. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  429. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  430. int z = i % WORLD_HEIGHT;
  431. putBlockAt({ x,y,z }, 0);
  432. putBlockTypeAt({ x, y, z }, NoBlockBlockType::ID);
  433. }
  434. }
  435. else if (blockIds[i])
  436. {
  437. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  438. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  439. int z = i % WORLD_HEIGHT;
  440. bool visible = 0;
  441. if (CONST_BLOCK(0, blockIds[i])->isTransparent() || CONST_BLOCK(0, blockIds[i])->isPassable())
  442. visible = 1;
  443. else
  444. {
  445. for (int d = 0; d < 6 && !visible; d++)
  446. {
  447. auto n = zBlockNeighbor(getDirection((Directions)getDirectionFromIndex(d)) + Framework::Vec3<int>(x, y, z));
  448. if (n.isA() && (((Block*)n)->isPassable() || ((Block*)n)->isTransparent()))
  449. visible = 1;
  450. if (n.isB() && (CONST_BLOCK(0, n)->isTransparent() || CONST_BLOCK(0, n)->isPassable()))
  451. visible = 1;
  452. }
  453. }
  454. if (!visible)
  455. {
  456. putBlockAt({ x,y,z }, 0);
  457. putBlockTypeAt({ x, y, z }, NoBlockBlockType::ID);
  458. }
  459. }
  460. }
  461. int count = 0;
  462. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  463. {
  464. if (blockIds[i] && blockIds[i] != AirBlockBlockType::ID)
  465. count++;
  466. }
  467. std::cout << "chunk " << location.x << ", " << location.y << " was generated with " << count << " blocks.\n";
  468. }
  469. int Chunk::getDimensionId() const
  470. {
  471. return dimensionId;
  472. }
  473. Framework::Punkt Chunk::getCenter() const
  474. {
  475. return location;
  476. }
  477. Framework::Vec3<int> Chunk::getMin() const
  478. {
  479. return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
  480. }
  481. Framework::Vec3<int> Chunk::getMax() const
  482. {
  483. return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
  484. }
  485. void Chunk::prepareRemove()
  486. {
  487. added = 0;
  488. for (int i = 0; i < 4; i++)
  489. {
  490. if (zNeighbours[i])
  491. {
  492. zNeighbours[i]->setNeighbor(getOppositeDirection(getDirectionFromIndex(i)), 0);
  493. zNeighbours[i] = 0;
  494. }
  495. }
  496. }
  497. void Chunk::setAdded()
  498. {
  499. added = 1;
  500. }
  501. bool Chunk::hasObservers() const
  502. {
  503. return observers.getEintragAnzahl() > 0;
  504. }