Chunk.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. #include "Chunk.h"
  2. #include <AsynchronCall.h>
  3. #include <InMemoryBuffer.h>
  4. #include "Constants.h"
  5. #include "Game.h"
  6. #include "NoBlock.h"
  7. Chunk::Chunk(Framework::Punkt location, int dimensionId)
  8. : ReferenceCounter(),
  9. dimensionId(dimensionId),
  10. location(location),
  11. added(0),
  12. currentlyLoading(1)
  13. {
  14. blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  15. blockIds = new unsigned short[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  16. lightData = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6];
  17. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  18. memset(blockIds,
  19. 0,
  20. CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(unsigned short));
  21. memset(lightData, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  22. zNeighbours[0] = 0;
  23. zNeighbours[1] = 0;
  24. zNeighbours[2] = 0;
  25. zNeighbours[3] = 0;
  26. }
  27. Chunk::Chunk(Framework::Punkt location,
  28. int dimensionId,
  29. Framework::StreamReader* zReader)
  30. : Chunk(location, dimensionId)
  31. {
  32. load(zReader);
  33. }
  34. Chunk::~Chunk()
  35. {
  36. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  37. {
  38. if (blocks[i]) blocks[i]->release();
  39. }
  40. delete[] blocks;
  41. delete[] blockIds;
  42. delete[] lightData;
  43. }
  44. void Chunk::lock()
  45. {
  46. cs.lock();
  47. }
  48. void Chunk::unlock()
  49. {
  50. cs.unlock();
  51. }
  52. void Chunk::tick(TickQueue* zQueue)
  53. {
  54. for (Block* source : tickSources)
  55. zQueue->addToQueue(source);
  56. }
  57. void Chunk::postTick() {}
  58. void Chunk::addLightSource(int index)
  59. {
  60. for (int i : lightSources)
  61. {
  62. if (i == index) return;
  63. }
  64. lightSources.add(index);
  65. }
  66. void Chunk::removeLightSource(int index)
  67. {
  68. for (auto i = lightSources.begin(); i; i++)
  69. {
  70. if (i.val() == index)
  71. {
  72. i.remove();
  73. return;
  74. }
  75. }
  76. }
  77. void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
  78. {
  79. for (int z = 0; z < WORLD_HEIGHT; z++)
  80. {
  81. for (int x = -1; x <= CHUNK_SIZE; x++)
  82. {
  83. for (int y = -1; y <= CHUNK_SIZE; y++)
  84. {
  85. if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y > CHUNK_SIZE))
  86. {
  87. continue;
  88. }
  89. bool needSend = 0;
  90. for (int i = 0; i < 6; i++)
  91. {
  92. Vec3<int> pos = Vec3<int>(x, y, z)
  93. + getDirection(getDirectionFromIndex(i));
  94. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  95. {
  96. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  97. && pos.y < CHUNK_SIZE)
  98. {
  99. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  100. + pos.z;
  101. int type = blockIds[bi];
  102. needSend |= type != BlockTypeEnum::NO_BLOCK
  103. && type != BlockTypeEnum::AIR;
  104. }
  105. else
  106. {
  107. if (x >= 0 && x < CHUNK_SIZE && y >= 0
  108. && y < CHUNK_SIZE)
  109. {
  110. if (i < 4 && zNeighbours[i])
  111. {
  112. Vec3<int> offset
  113. = getDirection(getDirectionFromIndex(i))
  114. * 16;
  115. int bi = ((pos.x - offset.x) * CHUNK_SIZE
  116. + (pos.y - offset.y))
  117. * WORLD_HEIGHT
  118. + (pos.z - offset.z);
  119. int type = zNeighbours[i]->blockIds[bi];
  120. needSend |= type != BlockTypeEnum::NO_BLOCK
  121. && type != BlockTypeEnum::AIR;
  122. }
  123. }
  124. }
  125. if (needSend) break;
  126. }
  127. }
  128. if (needSend)
  129. {
  130. if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
  131. {
  132. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  133. zWriter->schreibe((char*)&index, 4);
  134. zWriter->schreibe((char*)(lightData + index * 6), 6);
  135. }
  136. else
  137. {
  138. int dir;
  139. int index = 0;
  140. if (x == -1)
  141. {
  142. dir = getDirectionIndex(WEST);
  143. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y)
  144. * WORLD_HEIGHT
  145. + z;
  146. }
  147. else if (y == -1)
  148. {
  149. dir = getDirectionIndex(NORTH);
  150. index = (x * CHUNK_SIZE + CHUNK_SIZE - 1)
  151. * WORLD_HEIGHT
  152. + z;
  153. }
  154. else if (x == CHUNK_SIZE)
  155. {
  156. dir = getDirectionIndex(EAST);
  157. index = y * WORLD_HEIGHT + z;
  158. }
  159. else if (y == CHUNK_SIZE)
  160. {
  161. dir = getDirectionIndex(SOUTH);
  162. index = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  163. }
  164. if (zNeighbours[dir])
  165. {
  166. int i = -1;
  167. zWriter->schreibe((char*)&i, 4);
  168. zWriter->schreibe((char*)&x, 4);
  169. zWriter->schreibe((char*)&y, 4);
  170. zWriter->schreibe((char*)&z, 4);
  171. zWriter->schreibe(
  172. (char*)(zNeighbours[dir]->lightData
  173. + index * 6),
  174. 6);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. int end = -2;
  182. zWriter->schreibe((char*)&end, 4);
  183. }
  184. void Chunk::broadcastLightData(int index, bool foreground)
  185. {
  186. int x = (index / WORLD_HEIGHT) / CHUNK_SIZE;
  187. int y = (index / WORLD_HEIGHT) % CHUNK_SIZE;
  188. int z = index % WORLD_HEIGHT;
  189. NetworkMessage* msg = new NetworkMessage();
  190. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  191. char* message = new char[19];
  192. message[0] = 5;
  193. *(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
  194. *(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
  195. *(int*)(message + 9) = z;
  196. memcpy(message + 13, lightData + index * 6, 6);
  197. msg->setMessage(message, 19);
  198. if (!foreground) msg->setUseBackground();
  199. notifyObservers(msg);
  200. }
  201. Framework::Either<Block*, int> Chunk::zBlockNeighbor(
  202. Framework::Vec3<int> location)
  203. {
  204. if (location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0
  205. && location.y < CHUNK_SIZE && location.z >= 0
  206. && location.z < WORLD_HEIGHT)
  207. {
  208. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT
  209. + location.z;
  210. if (blocks[index])
  211. return blocks[index];
  212. else
  213. return (int)blockIds[index];
  214. }
  215. if (added && location.z >= 0 && location.z < WORLD_HEIGHT)
  216. return Game::INSTANCE->zBlockAt(
  217. {location.x + this->location.x - CHUNK_SIZE / 2,
  218. location.y + this->location.y - CHUNK_SIZE / 2,
  219. location.z},
  220. dimensionId);
  221. return 0;
  222. }
  223. void Chunk::notifyObservers(NetworkMessage* msg)
  224. {
  225. Array<int> remove;
  226. int index = 0;
  227. for (int id : observers)
  228. {
  229. Entity* zE = Game::INSTANCE->zEntity(id);
  230. if (!zE)
  231. remove.add(index, 0);
  232. else
  233. Game::INSTANCE->sendMessage(
  234. dynamic_cast<NetworkMessage*>(msg->getThis()), zE);
  235. index++;
  236. }
  237. for (int i : remove)
  238. observers.remove(i);
  239. msg->release();
  240. }
  241. void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
  242. {
  243. for (int id : observers)
  244. {
  245. if (id == zEntity->getId()) return;
  246. }
  247. int id = zEntity->getId();
  248. observers.add(id);
  249. laterHandler.addTodo([this, id]() {
  250. InMemoryBuffer buffer;
  251. buffer.schreibe("\4", 1);
  252. buffer.schreibe((char*)&location.x, 4);
  253. buffer.schreibe((char*)&location.y, 4);
  254. sendToClient(&buffer);
  255. sendLightToClient(&buffer);
  256. NetworkMessage* msg = new NetworkMessage();
  257. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  258. std::cout << "chunk size: " << buffer.getSize() << "b\n";
  259. char* message = new char[buffer.getSize()];
  260. buffer.lese(message, (int)buffer.getSize());
  261. msg->setMessage(message, (int)buffer.getSize());
  262. msg->setUseBackground();
  263. Entity* e = Game::INSTANCE->zEntity(id);
  264. if (e)
  265. {
  266. Game::INSTANCE->sendMessage(msg, e);
  267. }
  268. else
  269. msg->release();
  270. });
  271. }
  272. void Chunk::removeObserver(Entity* zEntity)
  273. {
  274. int index = 0;
  275. for (int id : observers)
  276. {
  277. if (id == zEntity->getId())
  278. {
  279. observers.remove(index);
  280. return;
  281. }
  282. index++;
  283. }
  284. }
  285. void Chunk::api(Framework::StreamReader* zRequest,
  286. Entity* zSource,
  287. DoLaterHandler& laterHandler)
  288. {
  289. // TODO: answer api messages
  290. char type;
  291. zRequest->lese(&type, 1);
  292. switch (type)
  293. {
  294. case 0:
  295. // register observer
  296. addObserver(zSource, laterHandler);
  297. break;
  298. case 1:
  299. // unsubscribe
  300. removeObserver(zSource);
  301. break;
  302. }
  303. }
  304. void Chunk::initializeLightning()
  305. {
  306. unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
  307. unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
  308. while (true)
  309. {
  310. bool changes = false;
  311. for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
  312. {
  313. for (int x = 0; x < CHUNK_SIZE; x++)
  314. {
  315. for (int y = 0; y < CHUNK_SIZE; y++)
  316. {
  317. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  318. unsigned char* light = getLightData(Vec3<int>(x, y, z));
  319. unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
  320. for (int i = 0; i < 6; i++)
  321. {
  322. unsigned char* neighborLeight;
  323. Vec3<int> neighborPos
  324. = Vec3<int>(x, y, z)
  325. + getDirection(getDirectionFromIndex(i));
  326. if (neighborPos.z < 0 || neighborPos.x < 0
  327. || neighborPos.y < 0 || neighborPos.x >= CHUNK_SIZE
  328. || neighborPos.y >= CHUNK_SIZE)
  329. {
  330. neighborLeight = noLight;
  331. }
  332. else if (neighborPos.z >= WORLD_HEIGHT)
  333. {
  334. neighborLeight = dayLight;
  335. }
  336. else
  337. {
  338. neighborLeight = getLightData(neighborPos);
  339. }
  340. for (int j = 0; j < 3; j++)
  341. newLight[j] = (unsigned char)MAX(newLight[j],
  342. i == getDirectionIndex(TOP)
  343. ? neighborLeight[j]
  344. : (unsigned char)((float)neighborLeight[j]
  345. * 0.8f));
  346. for (int j = 3; j < 6; j++)
  347. newLight[j] = (unsigned char)MAX(newLight[j],
  348. (unsigned char)((float)neighborLeight[j]
  349. * 0.85f));
  350. }
  351. const Block* current
  352. = blocks[index] ? blocks[index]
  353. : StaticRegistry<BlockType>::INSTANCE
  354. .zElement(blockIds[index])
  355. ->zDefault();
  356. // add own light emission
  357. for (int j = 3; j < 6; j++)
  358. newLight[j] = (unsigned char)MAX(newLight[j],
  359. current->getLightEmisionColor()[j - 3]);
  360. current->filterPassingLight(newLight);
  361. current->filterPassingLight(newLight + 3);
  362. for (int i = 0; i < 6; i++)
  363. {
  364. if (newLight[i] != light[i])
  365. {
  366. changes = 1;
  367. memcpy(light, newLight, 6);
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. if (!changes) break;
  375. }
  376. }
  377. Framework::Either<Block*, int> Chunk::zBlockAt(
  378. Framework::Vec3<int> location) const
  379. {
  380. int index = Chunk::index(location);
  381. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  382. if (blocks[index])
  383. return blocks[index];
  384. else
  385. return (int)blockIds[index];
  386. }
  387. const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
  388. {
  389. auto b = zBlockAt(location);
  390. if (b.isA()) return b;
  391. return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->zDefault();
  392. }
  393. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  394. {
  395. auto b = zBlockAt(location);
  396. if (b.isA()) return;
  397. if (!b.getB()) generateBlock(location);
  398. b = zBlockAt(location);
  399. if (b.isB())
  400. putBlockAt(location,
  401. StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
  402. ->createBlockAt(
  403. {location.x + this->location.x - CHUNK_SIZE / 2,
  404. location.y + this->location.y - CHUNK_SIZE / 2,
  405. location.z},
  406. 0));
  407. }
  408. void Chunk::generateBlock(Framework::Vec3<int> location)
  409. {
  410. int index = Chunk::index(location);
  411. if (blockIds[index]) return;
  412. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  413. {location.x + this->location.x - CHUNK_SIZE / 2,
  414. location.y + this->location.y - CHUNK_SIZE / 2,
  415. location.z},
  416. dimensionId);
  417. if (generated.isA())
  418. putBlockAt(location, generated);
  419. else
  420. putBlockTypeAt(location, generated);
  421. }
  422. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  423. {
  424. int index = Chunk::index(location);
  425. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
  426. Block* old = blocks[index];
  427. if (old && old->isTickSource())
  428. { // remove from tick sorces
  429. for (Framework::Iterator<Block*> obj = tickSources.begin(); obj; obj++)
  430. {
  431. if (obj.val() == old)
  432. {
  433. obj.remove();
  434. break;
  435. }
  436. }
  437. }
  438. bool change = 0;
  439. bool wasLightSource
  440. = old ? old->zBlockType()->isLightSource()
  441. : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  442. ->isLightSource();
  443. bool isLightSource = 0;
  444. if (block)
  445. {
  446. change
  447. = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  448. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  449. isLightSource = block->zBlockType()->isLightSource();
  450. }
  451. else
  452. {
  453. if (old != 0)
  454. {
  455. blockIds[index] = BlockTypeEnum::NO_BLOCK;
  456. }
  457. change = old != 0;
  458. }
  459. blocks[index] = block;
  460. for (int i = 0; i < 6; i++)
  461. {
  462. Direction d = getDirectionFromIndex(i);
  463. Either<Block*, int> neighbor
  464. = zBlockNeighbor(location + getDirection(d));
  465. if (neighbor.isA())
  466. {
  467. if (block)
  468. {
  469. ((Block*)neighbor)
  470. ->setNeighbour(getOppositeDirection(d), block);
  471. }
  472. else
  473. {
  474. ((Block*)neighbor)
  475. ->setNeighbour(getOppositeDirection(d), blockIds[index]);
  476. }
  477. }
  478. if (block) block->setNeighbour(d, neighbor);
  479. }
  480. if (old) old->release();
  481. if (block && block->isTickSource())
  482. { // add to tick sources
  483. tickSources.add(block);
  484. }
  485. if (change)
  486. {
  487. if (isLightSource != wasLightSource)
  488. {
  489. if (isLightSource)
  490. addLightSource(index);
  491. else
  492. removeLightSource(index);
  493. }
  494. if (added)
  495. {
  496. sendBlockInfo(location);
  497. if (block)
  498. {
  499. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  500. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  501. location.y + this->location.y - CHUNK_SIZE / 2,
  502. location.z));
  503. }
  504. }
  505. }
  506. if (added)
  507. {
  508. Game::INSTANCE->zDimension(dimensionId)
  509. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  510. location.y + this->location.y - CHUNK_SIZE / 2,
  511. location.z);
  512. }
  513. }
  514. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  515. {
  516. int index = Chunk::index(location);
  517. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  518. bool wasLightSource
  519. = StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  520. ->isLightSource();
  521. bool isLightSource
  522. = StaticRegistry<BlockType>::INSTANCE.zElement(type)->isLightSource();
  523. if (blockIds[index] != (unsigned short)type)
  524. {
  525. blockIds[index] = (unsigned short)type;
  526. for (int i = 0; i < 6; i++)
  527. {
  528. Direction d = getDirectionFromIndex(i);
  529. Either<Block*, int> neighbor
  530. = zBlockNeighbor(location + getDirection(d));
  531. if (neighbor.isA())
  532. ((Block*)neighbor)
  533. ->setNeighbourType(getOppositeDirection(d), type);
  534. }
  535. if (isLightSource != wasLightSource)
  536. {
  537. if (isLightSource)
  538. addLightSource(index);
  539. else
  540. removeLightSource(index);
  541. }
  542. if (added)
  543. {
  544. sendBlockInfo(location);
  545. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  546. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  547. location.y + this->location.y - CHUNK_SIZE / 2,
  548. location.z));
  549. Game::INSTANCE->zDimension(dimensionId)
  550. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  551. location.y + this->location.y - CHUNK_SIZE / 2,
  552. location.z);
  553. }
  554. }
  555. }
  556. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  557. {
  558. int index = Chunk::index(location);
  559. char* msg = new char[9];
  560. msg[0] = 0; // set block
  561. *(int*)(msg + 1) = index;
  562. *(int*)(msg + 5) = blockIds[index];
  563. NetworkMessage* message = new NetworkMessage();
  564. message->addressChunck(this);
  565. message->setMessage(msg, 9);
  566. notifyObservers(message);
  567. for (int i = 0; i < 6; i++)
  568. {
  569. Direction d = getDirectionFromIndex(i);
  570. Framework::Vec3<int> loc = location + getDirection(d);
  571. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  572. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  573. {
  574. broadcastLightData(Chunk::index(loc), true);
  575. }
  576. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  577. {
  578. NetworkMessage* msg = new NetworkMessage();
  579. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  580. char* message = new char[19];
  581. message[0] = 5;
  582. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  583. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  584. *(int*)(message + 9) = loc.z;
  585. loc -= getDirection(d) * CHUNK_SIZE;
  586. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  587. msg->setMessage(message, 19);
  588. notifyObservers(msg);
  589. }
  590. }
  591. }
  592. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  593. {
  594. zNeighbours[getDirectionIndex(dir)] = zChunk;
  595. for (int i = 0; i < CHUNK_SIZE; i++)
  596. {
  597. for (int z = 0; z < WORLD_HEIGHT; z++)
  598. {
  599. int index = 0;
  600. int j = 0;
  601. if (dir == NORTH)
  602. {
  603. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  604. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  605. }
  606. else if (dir == EAST)
  607. {
  608. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  609. j = i * WORLD_HEIGHT + z;
  610. }
  611. else if (dir == SOUTH)
  612. {
  613. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  614. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  615. }
  616. else if (dir == WEST)
  617. {
  618. index = i * WORLD_HEIGHT + z;
  619. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  620. }
  621. if (blocks[index])
  622. {
  623. if (zChunk && zChunk->blocks[j])
  624. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  625. else
  626. {
  627. blocks[index]->setNeighbour(dir, 0);
  628. blocks[index]->setNeighbourType(
  629. dir, zChunk ? zChunk->blockIds[j] : 0);
  630. }
  631. }
  632. if (zChunk)
  633. {
  634. if (!blocks[index])
  635. {
  636. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  637. && !blockIds[index])
  638. {
  639. generateBlock(
  640. Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  641. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  642. index % WORLD_HEIGHT));
  643. }
  644. }
  645. if (blockIds[index] != BlockTypeEnum::AIR
  646. && blockIds[index] != BlockTypeEnum::NO_BLOCK)
  647. {
  648. zChunk->broadcastLightData(j, true);
  649. }
  650. }
  651. }
  652. }
  653. }
  654. void Chunk::load(Framework::StreamReader* zReader)
  655. {
  656. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  657. {
  658. unsigned short blockType;
  659. zReader->lese((char*)&blockType, 2);
  660. if (blockType)
  661. {
  662. Framework::Vec3<int> pos
  663. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  664. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  665. index % WORLD_HEIGHT);
  666. bool d;
  667. zReader->lese((char*)&d, 1);
  668. if (d)
  669. {
  670. putBlockAt(pos,
  671. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  672. ->loadBlock(Framework::Vec3<int>(
  673. pos.x + location.x - CHUNK_SIZE / 2,
  674. pos.y + location.y - CHUNK_SIZE / 2,
  675. pos.z),
  676. zReader,
  677. dimensionId));
  678. }
  679. else
  680. {
  681. putBlockTypeAt(pos, blockType);
  682. }
  683. }
  684. }
  685. initializeLightning();
  686. }
  687. void Chunk::save(Framework::StreamWriter* zWriter)
  688. {
  689. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  690. {
  691. unsigned short blockType
  692. = blocks[index]
  693. ? (unsigned short)blocks[index]->zBlockType()->getId()
  694. : blockIds[index];
  695. zWriter->schreibe((char*)&blockType, 2);
  696. if (blockType)
  697. {
  698. if (blocks[index])
  699. {
  700. bool d = 1;
  701. zWriter->schreibe((char*)&d, 1);
  702. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  703. ->saveBlock(blocks[index], zWriter);
  704. }
  705. else
  706. {
  707. bool d = 0;
  708. zWriter->schreibe((char*)&d, 1);
  709. }
  710. }
  711. }
  712. }
  713. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  714. {
  715. for (int x = 0; x < CHUNK_SIZE; x++)
  716. {
  717. for (int y = 0; y < CHUNK_SIZE; y++)
  718. {
  719. for (int z = 0; z < WORLD_HEIGHT; z++)
  720. {
  721. int index = Chunk::index({x, y, z});
  722. unsigned short blockType
  723. = blocks[index]
  724. ? (unsigned short)blocks[index]->zBlockType()->getId()
  725. : blockIds[index];
  726. if (blockType)
  727. {
  728. bool visible = 0;
  729. if (!visible)
  730. {
  731. if (!blocks[index])
  732. {
  733. if (CONST_BLOCK(0, blockIds[index])->isTransparent()
  734. || CONST_BLOCK(0, blockIds[index])
  735. ->isPassable())
  736. visible = 1;
  737. else
  738. {
  739. for (int d = 0; d < 6 && !visible; d++)
  740. {
  741. Vec3<int> pos
  742. = getDirection(
  743. (Directions)getDirectionFromIndex(
  744. d))
  745. + Framework::Vec3<int>(x, y, z);
  746. auto n = zBlockNeighbor(pos);
  747. if (n.isA()
  748. && (((Block*)n)->isPassable()
  749. || ((Block*)n)->isTransparent()))
  750. visible = 1;
  751. if (n.isB()
  752. && (CONST_BLOCK(0, n)->isTransparent()
  753. || CONST_BLOCK(0, n)->isPassable()))
  754. visible = 1;
  755. if (pos.x < 0 || pos.y < 0 || pos.z < 0
  756. || pos.x >= CHUNK_SIZE
  757. || pos.y >= CHUNK_SIZE
  758. || pos.z >= WORLD_HEIGHT)
  759. visible = 1;
  760. }
  761. }
  762. }
  763. else
  764. visible = blocks[index]->isVisible();
  765. }
  766. if (visible
  767. && (blocks[index] || blockType != BlockTypeEnum::AIR))
  768. {
  769. zWriter->schreibe((char*)&blockType, 2);
  770. zWriter->schreibe((char*)&index, 4);
  771. }
  772. }
  773. }
  774. }
  775. }
  776. unsigned short end = 0;
  777. zWriter->schreibe((char*)&end, 2);
  778. }
  779. void Chunk::removeUnusedBlocks()
  780. {
  781. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  782. {
  783. if (!blocks[i] && blockIds[i])
  784. {
  785. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  786. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  787. int z = i % WORLD_HEIGHT;
  788. bool visible = 0;
  789. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  790. || CONST_BLOCK(0, blockIds[i])->isPassable())
  791. visible = 1;
  792. else
  793. {
  794. for (int d = 0; d < 6 && !visible; d++)
  795. {
  796. auto n = zBlockNeighbor(
  797. getDirection((Directions)getDirectionFromIndex(d))
  798. + Framework::Vec3<int>(x, y, z));
  799. if (n.isA()
  800. && (((Block*)n)->isPassable()
  801. || ((Block*)n)->isTransparent()))
  802. visible = 1;
  803. if (n.isB()
  804. && (CONST_BLOCK(0, n)->isTransparent()
  805. || CONST_BLOCK(0, n)->isPassable()))
  806. visible = 1;
  807. }
  808. }
  809. if (!visible)
  810. {
  811. putBlockAt({x, y, z}, 0);
  812. putBlockTypeAt({x, y, z}, 0);
  813. }
  814. }
  815. }
  816. int count = 0;
  817. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  818. {
  819. if (blockIds[i] && blockIds[i] != BlockTypeEnum::AIR) count++;
  820. }
  821. std::cout << "chunk " << location.x << ", " << location.y
  822. << " was generated with " << count << " blocks.\n";
  823. }
  824. int Chunk::getDimensionId() const
  825. {
  826. return dimensionId;
  827. }
  828. void Chunk::onLoaded()
  829. {
  830. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  831. {
  832. if (blocks[i]) blocks[i]->onLoaded();
  833. }
  834. currentlyLoading = 0;
  835. }
  836. void Chunk::onUnloaded()
  837. {
  838. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  839. {
  840. if (blocks[i]) blocks[i]->onUnloaded();
  841. }
  842. }
  843. Framework::Punkt Chunk::getCenter() const
  844. {
  845. return location;
  846. }
  847. Framework::Vec3<int> Chunk::getMin() const
  848. {
  849. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  850. }
  851. Framework::Vec3<int> Chunk::getMax() const
  852. {
  853. return {
  854. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  855. }
  856. void Chunk::prepareRemove()
  857. {
  858. added = 0;
  859. for (int i = 0; i < 4; i++)
  860. {
  861. if (zNeighbours[i])
  862. {
  863. zNeighbours[i]->setNeighbor(
  864. getOppositeDirection(getDirectionFromIndex(i)), 0);
  865. zNeighbours[i] = 0;
  866. }
  867. }
  868. }
  869. void Chunk::setAdded()
  870. {
  871. added = 1;
  872. }
  873. bool Chunk::hasObservers() const
  874. {
  875. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  876. }
  877. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  878. {
  879. int index = Chunk::index(location) * 6;
  880. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  881. return lightData + index;
  882. }
  883. void Chunk::setLightData(
  884. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  885. {
  886. int index = Chunk::index(location);
  887. memcpy(lightData + index * 6, data, 6);
  888. // check if neighbor is a visible block and send update to clients
  889. bool needSend = 0;
  890. for (int i = 0; i < 6; i++)
  891. {
  892. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  893. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  894. {
  895. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  896. && pos.y < CHUNK_SIZE)
  897. {
  898. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  899. int type = blockIds[bi];
  900. needSend |= type != BlockTypeEnum::NO_BLOCK
  901. && type != BlockTypeEnum::AIR;
  902. if (needSend) break;
  903. }
  904. else
  905. {
  906. needSend = 1; // TODO: check if the block is visible
  907. }
  908. }
  909. }
  910. if (needSend)
  911. {
  912. broadcastLightData(index, foreground);
  913. }
  914. }