Chunk.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. char type;
  290. zRequest->lese(&type, 1);
  291. switch (type)
  292. {
  293. case 0:
  294. // register observer
  295. addObserver(zSource, laterHandler);
  296. break;
  297. case 1:
  298. // unsubscribe
  299. removeObserver(zSource);
  300. break;
  301. }
  302. }
  303. void Chunk::initializeLightning()
  304. {
  305. unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
  306. unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
  307. while (true)
  308. {
  309. bool changes = false;
  310. for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
  311. {
  312. for (int x = 0; x < CHUNK_SIZE; x++)
  313. {
  314. for (int y = 0; y < CHUNK_SIZE; y++)
  315. {
  316. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  317. unsigned char* light = getLightData(Vec3<int>(x, y, z));
  318. unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
  319. for (int i = 0; i < 6; i++)
  320. {
  321. unsigned char* neighborLeight;
  322. Vec3<int> neighborPos
  323. = Vec3<int>(x, y, z)
  324. + getDirection(getDirectionFromIndex(i));
  325. if (neighborPos.z < 0 || neighborPos.x < 0
  326. || neighborPos.y < 0 || neighborPos.x >= CHUNK_SIZE
  327. || neighborPos.y >= CHUNK_SIZE)
  328. {
  329. neighborLeight = noLight;
  330. }
  331. else if (neighborPos.z >= WORLD_HEIGHT)
  332. {
  333. neighborLeight = dayLight;
  334. }
  335. else
  336. {
  337. neighborLeight = getLightData(neighborPos);
  338. }
  339. for (int j = 0; j < 3; j++)
  340. newLight[j] = (unsigned char)MAX(newLight[j],
  341. i == getDirectionIndex(TOP)
  342. ? neighborLeight[j]
  343. : (unsigned char)((float)neighborLeight[j]
  344. * 0.8f));
  345. for (int j = 3; j < 6; j++)
  346. newLight[j] = (unsigned char)MAX(newLight[j],
  347. (unsigned char)((float)neighborLeight[j]
  348. * 0.85f));
  349. }
  350. const Block* current
  351. = blocks[index] ? blocks[index]
  352. : StaticRegistry<BlockType>::INSTANCE
  353. .zElement(blockIds[index])
  354. ->zDefault();
  355. // add own light emission
  356. for (int j = 3; j < 6; j++)
  357. newLight[j] = (unsigned char)MAX(newLight[j],
  358. current->getLightEmisionColor()[j - 3]);
  359. current->filterPassingLight(newLight);
  360. current->filterPassingLight(newLight + 3);
  361. for (int i = 0; i < 6; i++)
  362. {
  363. if (newLight[i] != light[i])
  364. {
  365. changes = 1;
  366. memcpy(light, newLight, 6);
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. if (!changes) break;
  374. }
  375. }
  376. Framework::Either<Block*, int> Chunk::zBlockAt(
  377. Framework::Vec3<int> location) const
  378. {
  379. int index = Chunk::index(location);
  380. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  381. if (blocks[index])
  382. return blocks[index];
  383. else
  384. return (int)blockIds[index];
  385. }
  386. const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
  387. {
  388. auto b = zBlockAt(location);
  389. if (b.isA()) return b;
  390. return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->zDefault();
  391. }
  392. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  393. {
  394. auto b = zBlockAt(location);
  395. if (b.isA()) return;
  396. if (!b.getB()) generateBlock(location);
  397. b = zBlockAt(location);
  398. if (b.isB())
  399. putBlockAt(location,
  400. StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
  401. ->createBlockAt(
  402. {location.x + this->location.x - CHUNK_SIZE / 2,
  403. location.y + this->location.y - CHUNK_SIZE / 2,
  404. location.z}, dimensionId,
  405. 0));
  406. }
  407. void Chunk::generateBlock(Framework::Vec3<int> location)
  408. {
  409. int index = Chunk::index(location);
  410. if (blockIds[index]) return;
  411. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  412. {location.x + this->location.x - CHUNK_SIZE / 2,
  413. location.y + this->location.y - CHUNK_SIZE / 2,
  414. location.z},
  415. dimensionId);
  416. if (generated.isA())
  417. putBlockAt(location, generated);
  418. else
  419. putBlockTypeAt(location, generated);
  420. }
  421. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  422. {
  423. int index = Chunk::index(location);
  424. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
  425. Block* old = blocks[index];
  426. if (old && old->isTickSource())
  427. { // remove from tick sorces
  428. for (Framework::Iterator<Block*> obj = tickSources.begin(); obj; obj++)
  429. {
  430. if (obj.val() == old)
  431. {
  432. obj.remove();
  433. break;
  434. }
  435. }
  436. }
  437. bool change = 0;
  438. bool wasLightSource
  439. = old ? old->zBlockType()->isLightSource()
  440. : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  441. ->isLightSource();
  442. bool isLightSource = 0;
  443. if (block)
  444. {
  445. change
  446. = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  447. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  448. isLightSource = block->zBlockType()->isLightSource();
  449. }
  450. else
  451. {
  452. if (old != 0)
  453. {
  454. blockIds[index] = BlockTypeEnum::NO_BLOCK;
  455. }
  456. change = old != 0;
  457. }
  458. blocks[index] = block;
  459. for (int i = 0; i < 6; i++)
  460. {
  461. Direction d = getDirectionFromIndex(i);
  462. Either<Block*, int> neighbor
  463. = zBlockNeighbor(location + getDirection(d));
  464. if (neighbor.isA())
  465. {
  466. if (block)
  467. {
  468. ((Block*)neighbor)
  469. ->setNeighbour(getOppositeDirection(d), block);
  470. }
  471. else
  472. {
  473. ((Block*)neighbor)
  474. ->setNeighbour(getOppositeDirection(d), blockIds[index]);
  475. }
  476. }
  477. if (block) block->setNeighbour(d, neighbor);
  478. }
  479. if (old) old->release();
  480. if (block && block->isTickSource())
  481. { // add to tick sources
  482. tickSources.add(block);
  483. }
  484. if (change)
  485. {
  486. if (isLightSource != wasLightSource)
  487. {
  488. if (isLightSource)
  489. addLightSource(index);
  490. else
  491. removeLightSource(index);
  492. }
  493. if (added)
  494. {
  495. sendBlockInfo(location);
  496. if (block)
  497. {
  498. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  499. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  500. location.y + this->location.y - CHUNK_SIZE / 2,
  501. location.z));
  502. }
  503. }
  504. }
  505. if (added)
  506. {
  507. Game::INSTANCE->zDimension(dimensionId)
  508. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  509. location.y + this->location.y - CHUNK_SIZE / 2,
  510. location.z);
  511. }
  512. }
  513. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  514. {
  515. int index = Chunk::index(location);
  516. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  517. bool wasLightSource
  518. = StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  519. ->isLightSource();
  520. bool isLightSource
  521. = StaticRegistry<BlockType>::INSTANCE.zElement(type)->isLightSource();
  522. if (blockIds[index] != (unsigned short)type)
  523. {
  524. blockIds[index] = (unsigned short)type;
  525. for (int i = 0; i < 6; i++)
  526. {
  527. Direction d = getDirectionFromIndex(i);
  528. Either<Block*, int> neighbor
  529. = zBlockNeighbor(location + getDirection(d));
  530. if (neighbor.isA())
  531. ((Block*)neighbor)
  532. ->setNeighbourType(getOppositeDirection(d), type);
  533. }
  534. if (isLightSource != wasLightSource)
  535. {
  536. if (isLightSource)
  537. addLightSource(index);
  538. else
  539. removeLightSource(index);
  540. }
  541. if (added)
  542. {
  543. sendBlockInfo(location);
  544. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  545. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  546. location.y + this->location.y - CHUNK_SIZE / 2,
  547. location.z));
  548. Game::INSTANCE->zDimension(dimensionId)
  549. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  550. location.y + this->location.y - CHUNK_SIZE / 2,
  551. location.z);
  552. }
  553. }
  554. }
  555. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  556. {
  557. int index = Chunk::index(location);
  558. char* msg = new char[9];
  559. msg[0] = 0; // set block
  560. *(int*)(msg + 1) = index;
  561. *(int*)(msg + 5) = blockIds[index];
  562. NetworkMessage* message = new NetworkMessage();
  563. message->addressChunck(this);
  564. message->setMessage(msg, 9);
  565. notifyObservers(message);
  566. for (int i = 0; i < 6; i++)
  567. {
  568. Direction d = getDirectionFromIndex(i);
  569. Framework::Vec3<int> loc = location + getDirection(d);
  570. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  571. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  572. {
  573. broadcastLightData(Chunk::index(loc), true);
  574. }
  575. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  576. {
  577. NetworkMessage* msg = new NetworkMessage();
  578. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  579. char* message = new char[19];
  580. message[0] = 5;
  581. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  582. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  583. *(int*)(message + 9) = loc.z;
  584. loc -= getDirection(d) * CHUNK_SIZE;
  585. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  586. msg->setMessage(message, 19);
  587. notifyObservers(msg);
  588. }
  589. }
  590. }
  591. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  592. {
  593. zNeighbours[getDirectionIndex(dir)] = zChunk;
  594. for (int i = 0; i < CHUNK_SIZE; i++)
  595. {
  596. for (int z = 0; z < WORLD_HEIGHT; z++)
  597. {
  598. int index = 0;
  599. int j = 0;
  600. if (dir == NORTH)
  601. {
  602. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  603. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  604. }
  605. else if (dir == EAST)
  606. {
  607. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  608. j = i * WORLD_HEIGHT + z;
  609. }
  610. else if (dir == SOUTH)
  611. {
  612. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  613. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  614. }
  615. else if (dir == WEST)
  616. {
  617. index = i * WORLD_HEIGHT + z;
  618. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  619. }
  620. if (blocks[index])
  621. {
  622. if (zChunk && zChunk->blocks[j])
  623. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  624. else
  625. {
  626. blocks[index]->setNeighbour(dir, 0);
  627. blocks[index]->setNeighbourType(
  628. dir, zChunk ? zChunk->blockIds[j] : 0);
  629. }
  630. }
  631. if (zChunk)
  632. {
  633. if (!blocks[index])
  634. {
  635. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  636. && !blockIds[index])
  637. {
  638. generateBlock(
  639. Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  640. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  641. index % WORLD_HEIGHT));
  642. }
  643. }
  644. if (blockIds[index] != BlockTypeEnum::AIR
  645. && blockIds[index] != BlockTypeEnum::NO_BLOCK)
  646. {
  647. zChunk->broadcastLightData(j, true);
  648. }
  649. }
  650. }
  651. }
  652. }
  653. void Chunk::load(Framework::StreamReader* zReader)
  654. {
  655. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  656. {
  657. unsigned short blockType;
  658. zReader->lese((char*)&blockType, 2);
  659. if (blockType)
  660. {
  661. Framework::Vec3<int> pos
  662. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  663. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  664. index % WORLD_HEIGHT);
  665. bool d;
  666. zReader->lese((char*)&d, 1);
  667. if (d)
  668. {
  669. putBlockAt(pos,
  670. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  671. ->loadBlock(Framework::Vec3<int>(
  672. pos.x + location.x - CHUNK_SIZE / 2,
  673. pos.y + location.y - CHUNK_SIZE / 2,
  674. pos.z),
  675. zReader,
  676. dimensionId));
  677. }
  678. else
  679. {
  680. putBlockTypeAt(pos, blockType);
  681. }
  682. }
  683. }
  684. initializeLightning();
  685. }
  686. void Chunk::save(Framework::StreamWriter* zWriter)
  687. {
  688. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  689. {
  690. unsigned short blockType
  691. = blocks[index]
  692. ? (unsigned short)blocks[index]->zBlockType()->getId()
  693. : blockIds[index];
  694. zWriter->schreibe((char*)&blockType, 2);
  695. if (blockType)
  696. {
  697. if (blocks[index])
  698. {
  699. bool d = 1;
  700. zWriter->schreibe((char*)&d, 1);
  701. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  702. ->saveBlock(blocks[index], zWriter);
  703. }
  704. else
  705. {
  706. bool d = 0;
  707. zWriter->schreibe((char*)&d, 1);
  708. }
  709. }
  710. }
  711. }
  712. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  713. {
  714. for (int x = 0; x < CHUNK_SIZE; x++)
  715. {
  716. for (int y = 0; y < CHUNK_SIZE; y++)
  717. {
  718. for (int z = 0; z < WORLD_HEIGHT; z++)
  719. {
  720. int index = Chunk::index({x, y, z});
  721. unsigned short blockType
  722. = blocks[index]
  723. ? (unsigned short)blocks[index]->zBlockType()->getId()
  724. : blockIds[index];
  725. if (blockType)
  726. {
  727. bool visible = 0;
  728. if (!visible)
  729. {
  730. if (!blocks[index])
  731. {
  732. if (CONST_BLOCK(0, blockIds[index])->isTransparent()
  733. || CONST_BLOCK(0, blockIds[index])
  734. ->isPassable())
  735. visible = 1;
  736. else
  737. {
  738. for (int d = 0; d < 6 && !visible; d++)
  739. {
  740. Vec3<int> pos
  741. = getDirection(
  742. (Directions)getDirectionFromIndex(
  743. d))
  744. + Framework::Vec3<int>(x, y, z);
  745. auto n = zBlockNeighbor(pos);
  746. if (n.isA()
  747. && (((Block*)n)->isPassable()
  748. || ((Block*)n)->isTransparent()))
  749. visible = 1;
  750. if (n.isB()
  751. && (CONST_BLOCK(0, n)->isTransparent()
  752. || CONST_BLOCK(0, n)->isPassable()))
  753. visible = 1;
  754. if (pos.x < 0 || pos.y < 0 || pos.z < 0
  755. || pos.x >= CHUNK_SIZE
  756. || pos.y >= CHUNK_SIZE
  757. || pos.z >= WORLD_HEIGHT)
  758. visible = 1;
  759. }
  760. }
  761. }
  762. else
  763. visible = blocks[index]->isVisible();
  764. }
  765. if (visible
  766. && (blocks[index] || blockType != BlockTypeEnum::AIR))
  767. {
  768. zWriter->schreibe((char*)&blockType, 2);
  769. zWriter->schreibe((char*)&index, 4);
  770. }
  771. }
  772. }
  773. }
  774. }
  775. unsigned short end = 0;
  776. zWriter->schreibe((char*)&end, 2);
  777. }
  778. void Chunk::removeUnusedBlocks()
  779. {
  780. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  781. {
  782. if (!blocks[i] && blockIds[i])
  783. {
  784. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  785. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  786. int z = i % WORLD_HEIGHT;
  787. bool visible = 0;
  788. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  789. || CONST_BLOCK(0, blockIds[i])->isPassable())
  790. visible = 1;
  791. else
  792. {
  793. for (int d = 0; d < 6 && !visible; d++)
  794. {
  795. auto n = zBlockNeighbor(
  796. getDirection((Directions)getDirectionFromIndex(d))
  797. + Framework::Vec3<int>(x, y, z));
  798. if (n.isA()
  799. && (((Block*)n)->isPassable()
  800. || ((Block*)n)->isTransparent()))
  801. visible = 1;
  802. if (n.isB()
  803. && (CONST_BLOCK(0, n)->isTransparent()
  804. || CONST_BLOCK(0, n)->isPassable()))
  805. visible = 1;
  806. }
  807. }
  808. if (!visible)
  809. {
  810. putBlockAt({x, y, z}, 0);
  811. putBlockTypeAt({x, y, z}, 0);
  812. }
  813. }
  814. }
  815. int count = 0;
  816. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  817. {
  818. if (blockIds[i] && blockIds[i] != BlockTypeEnum::AIR) count++;
  819. }
  820. std::cout << "chunk " << location.x << ", " << location.y
  821. << " was generated with " << count << " blocks.\n";
  822. }
  823. int Chunk::getDimensionId() const
  824. {
  825. return dimensionId;
  826. }
  827. void Chunk::onLoaded()
  828. {
  829. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  830. {
  831. if (blocks[i]) blocks[i]->onLoaded();
  832. }
  833. currentlyLoading = 0;
  834. }
  835. void Chunk::onUnloaded()
  836. {
  837. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  838. {
  839. if (blocks[i]) blocks[i]->onUnloaded();
  840. }
  841. }
  842. Framework::Punkt Chunk::getCenter() const
  843. {
  844. return location;
  845. }
  846. Framework::Vec3<int> Chunk::getMin() const
  847. {
  848. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  849. }
  850. Framework::Vec3<int> Chunk::getMax() const
  851. {
  852. return {
  853. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  854. }
  855. void Chunk::prepareRemove()
  856. {
  857. added = 0;
  858. for (int i = 0; i < 4; i++)
  859. {
  860. if (zNeighbours[i])
  861. {
  862. zNeighbours[i]->setNeighbor(
  863. getOppositeDirection(getDirectionFromIndex(i)), 0);
  864. zNeighbours[i] = 0;
  865. }
  866. }
  867. }
  868. void Chunk::setAdded()
  869. {
  870. added = 1;
  871. }
  872. bool Chunk::hasObservers() const
  873. {
  874. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  875. }
  876. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  877. {
  878. int index = Chunk::index(location) * 6;
  879. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  880. return lightData + index;
  881. }
  882. void Chunk::setLightData(
  883. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  884. {
  885. int index = Chunk::index(location);
  886. memcpy(lightData + index * 6, data, 6);
  887. // check if neighbor is a visible block and send update to clients
  888. bool needSend = 0;
  889. for (int i = 0; i < 6; i++)
  890. {
  891. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  892. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  893. {
  894. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  895. && pos.y < CHUNK_SIZE)
  896. {
  897. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  898. int type = blockIds[bi];
  899. needSend |= type != BlockTypeEnum::NO_BLOCK
  900. && type != BlockTypeEnum::AIR;
  901. if (needSend) break;
  902. }
  903. else
  904. {
  905. needSend = 1; // TODO: check if the block is visible
  906. }
  907. }
  908. }
  909. if (needSend)
  910. {
  911. broadcastLightData(index, foreground);
  912. }
  913. }