Chunk.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. #include <AsynchronCall.h>
  2. #include <InMemoryBuffer.h>
  3. #include "Chunk.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 + index * 6),
  173. 6);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. int end = -2;
  181. zWriter->schreibe((char*)&end, 4);
  182. }
  183. void Chunk::broadcastLightData(int index, bool foreground)
  184. {
  185. int x = (index / WORLD_HEIGHT) / CHUNK_SIZE;
  186. int y = (index / WORLD_HEIGHT) % CHUNK_SIZE;
  187. int z = index % WORLD_HEIGHT;
  188. NetworkMessage* msg = new NetworkMessage();
  189. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  190. char* message = new char[19];
  191. message[0] = 5;
  192. *(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
  193. *(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
  194. *(int*)(message + 9) = z;
  195. memcpy(message + 13, lightData + index * 6, 6);
  196. msg->setMessage(message, 19);
  197. if (!foreground) msg->setUseBackground();
  198. notifyObservers(msg);
  199. }
  200. Framework::Either<Block*, int> Chunk::zBlockNeighbor(
  201. Framework::Vec3<int> location)
  202. {
  203. if (location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0
  204. && location.y < CHUNK_SIZE && location.z >= 0
  205. && location.z < WORLD_HEIGHT)
  206. {
  207. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT
  208. + location.z;
  209. if (blocks[index])
  210. return blocks[index];
  211. else
  212. return (int)blockIds[index];
  213. }
  214. if (added && location.z >= 0 && location.z < WORLD_HEIGHT)
  215. return Game::INSTANCE->zBlockAt(
  216. {location.x + this->location.x - CHUNK_SIZE / 2,
  217. location.y + this->location.y - CHUNK_SIZE / 2,
  218. location.z},
  219. dimensionId);
  220. return 0;
  221. }
  222. void Chunk::notifyObservers(NetworkMessage* msg)
  223. {
  224. Array<int> remove;
  225. int index = 0;
  226. for (int id : observers)
  227. {
  228. Entity* zE = Game::INSTANCE->zEntity(id);
  229. if (!zE)
  230. remove.add(index, 0);
  231. else
  232. Game::INSTANCE->sendMessage(
  233. dynamic_cast<NetworkMessage*>(msg->getThis()), zE);
  234. index++;
  235. }
  236. for (int i : remove)
  237. observers.remove(i);
  238. msg->release();
  239. }
  240. void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
  241. {
  242. for (int id : observers)
  243. {
  244. if (id == zEntity->getId()) return;
  245. }
  246. int id = zEntity->getId();
  247. observers.add(id);
  248. laterHandler.addTodo([this, id]() {
  249. InMemoryBuffer buffer;
  250. buffer.schreibe("\4", 1);
  251. buffer.schreibe((char*)&location.x, 4);
  252. buffer.schreibe((char*)&location.y, 4);
  253. sendToClient(&buffer);
  254. sendLightToClient(&buffer);
  255. NetworkMessage* msg = new NetworkMessage();
  256. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  257. std::cout << "chunk size: " << buffer.getSize() << "b\n";
  258. char* message = new char[buffer.getSize()];
  259. buffer.lese(message, (int)buffer.getSize());
  260. msg->setMessage(message, (int)buffer.getSize());
  261. msg->setUseBackground();
  262. Entity* e = Game::INSTANCE->zEntity(id);
  263. if (e)
  264. {
  265. Game::INSTANCE->sendMessage(msg, e);
  266. }
  267. else
  268. msg->release();
  269. });
  270. }
  271. void Chunk::removeObserver(Entity* zEntity)
  272. {
  273. int index = 0;
  274. for (int id : observers)
  275. {
  276. if (id == zEntity->getId())
  277. {
  278. observers.remove(index);
  279. return;
  280. }
  281. index++;
  282. }
  283. }
  284. void Chunk::api(Framework::StreamReader* zRequest,
  285. Entity* zSource,
  286. DoLaterHandler& laterHandler)
  287. {
  288. // TODO: answer api messages
  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
  380. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  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. if (b.getB())
  392. return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
  393. ->zDefault();
  394. return 0;
  395. }
  396. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  397. {
  398. auto b = zBlockAt(location);
  399. if (b.isA()) return;
  400. if (!b.getB()) generateBlock(location);
  401. b = zBlockAt(location);
  402. if (b.isB())
  403. putBlockAt(location,
  404. StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
  405. ->createBlockAt(
  406. {location.x + this->location.x - CHUNK_SIZE / 2,
  407. location.y + this->location.y - CHUNK_SIZE / 2,
  408. location.z},
  409. 0));
  410. }
  411. void Chunk::generateBlock(Framework::Vec3<int> location)
  412. {
  413. int index
  414. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  415. if (blockIds[index]) return;
  416. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  417. {location.x + this->location.x - CHUNK_SIZE / 2,
  418. location.y + this->location.y - CHUNK_SIZE / 2,
  419. location.z},
  420. dimensionId);
  421. if (generated.isA())
  422. putBlockAt(location, generated);
  423. else
  424. putBlockTypeAt(location, generated);
  425. }
  426. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  427. {
  428. int index
  429. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  430. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
  431. Block* old = blocks[index];
  432. if (old && old->isTickSource())
  433. { // remove from tick sorces
  434. for (Framework::Iterator<Block*> obj = tickSources.begin(); obj; obj++)
  435. {
  436. if (obj.val() == old)
  437. {
  438. obj.remove();
  439. break;
  440. }
  441. }
  442. }
  443. bool change = 0;
  444. bool wasLightSource
  445. = old ? old->zBlockType()->isLightSource()
  446. : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  447. ->isLightSource();
  448. bool isLightSource = 0;
  449. if (block)
  450. {
  451. change
  452. = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  453. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  454. isLightSource = block->zBlockType()->isLightSource();
  455. }
  456. else
  457. {
  458. if (old != 0)
  459. {
  460. blockIds[index] = BlockTypeEnum::NO_BLOCK;
  461. }
  462. change = old != 0;
  463. }
  464. blocks[index] = block;
  465. for (int i = 0; i < 6; i++)
  466. {
  467. Direction d = getDirectionFromIndex(i);
  468. Either<Block*, int> neighbor
  469. = zBlockNeighbor(location + getDirection(d));
  470. if (neighbor.isA())
  471. {
  472. if (block)
  473. {
  474. ((Block*)neighbor)
  475. ->setNeighbour(getOppositeDirection(d), block);
  476. }
  477. else
  478. {
  479. ((Block*)neighbor)
  480. ->setNeighbour(getOppositeDirection(d), blockIds[index]);
  481. }
  482. }
  483. if (block) block->setNeighbour(d, neighbor);
  484. }
  485. if (old) old->release();
  486. if (block && block->isTickSource())
  487. { // add to tick sources
  488. tickSources.add(block);
  489. }
  490. if (change)
  491. {
  492. if (isLightSource != wasLightSource)
  493. {
  494. if (isLightSource)
  495. addLightSource(index);
  496. else
  497. removeLightSource(index);
  498. }
  499. if (added)
  500. {
  501. sendBlockInfo(location);
  502. if (block)
  503. {
  504. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  505. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  506. location.y + this->location.y - CHUNK_SIZE / 2,
  507. location.z));
  508. }
  509. }
  510. }
  511. }
  512. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  513. {
  514. int index
  515. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  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. }
  549. }
  550. }
  551. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  552. {
  553. int index
  554. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  555. char* msg = new char[9];
  556. msg[0] = 0; // set block
  557. *(int*)(msg + 1) = index;
  558. *(int*)(msg + 5) = blockIds[index];
  559. NetworkMessage* message = new NetworkMessage();
  560. message->addressChunck(this);
  561. message->setMessage(msg, 9);
  562. notifyObservers(message);
  563. for (int i = 0; i < 6; i++)
  564. {
  565. Direction d = getDirectionFromIndex(i);
  566. Framework::Vec3<int> loc = location + getDirection(d);
  567. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  568. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  569. {
  570. int index
  571. = (loc.x * CHUNK_SIZE + loc.y) * WORLD_HEIGHT + loc.z;
  572. broadcastLightData(index, true);
  573. }
  574. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  575. {
  576. NetworkMessage* msg = new NetworkMessage();
  577. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  578. char* message = new char[19];
  579. message[0] = 5;
  580. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  581. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  582. *(int*)(message + 9) = loc.z;
  583. loc -= getDirection(d) * CHUNK_SIZE;
  584. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  585. msg->setMessage(message, 19);
  586. notifyObservers(msg);
  587. }
  588. }
  589. }
  590. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  591. {
  592. zNeighbours[getDirectionIndex(dir)] = zChunk;
  593. for (int i = 0; i < CHUNK_SIZE; i++)
  594. {
  595. for (int z = 0; z < WORLD_HEIGHT; z++)
  596. {
  597. int index = 0;
  598. int j = 0;
  599. if (dir == NORTH)
  600. {
  601. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  602. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  603. }
  604. else if (dir == EAST)
  605. {
  606. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  607. j = i * WORLD_HEIGHT + z;
  608. }
  609. else if (dir == SOUTH)
  610. {
  611. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  612. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  613. }
  614. else if (dir == WEST)
  615. {
  616. index = i * WORLD_HEIGHT + z;
  617. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  618. }
  619. if (blocks[index])
  620. {
  621. if (zChunk && zChunk->blocks[j])
  622. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  623. else
  624. {
  625. blocks[index]->setNeighbour(dir, 0);
  626. blocks[index]->setNeighbourType(
  627. dir, zChunk ? zChunk->blockIds[j] : 0);
  628. }
  629. }
  630. if (zChunk)
  631. {
  632. if (!blocks[index])
  633. {
  634. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  635. && !blockIds[index])
  636. {
  637. generateBlock(
  638. Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  639. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  640. index % WORLD_HEIGHT));
  641. }
  642. }
  643. if (blockIds[index] != BlockTypeEnum::AIR
  644. && blockIds[index] != BlockTypeEnum::NO_BLOCK)
  645. {
  646. zChunk->broadcastLightData(j, true);
  647. }
  648. }
  649. }
  650. }
  651. }
  652. void Chunk::load(Framework::StreamReader* zReader)
  653. {
  654. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  655. {
  656. unsigned short blockType;
  657. zReader->lese((char*)&blockType, 2);
  658. if (blockType)
  659. {
  660. Framework::Vec3<int> pos
  661. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  662. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  663. index % WORLD_HEIGHT);
  664. bool d;
  665. zReader->lese((char*)&d, 1);
  666. if (d)
  667. {
  668. putBlockAt(pos,
  669. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  670. ->loadBlock(Framework::Vec3<int>(
  671. pos.x + location.x - CHUNK_SIZE / 2,
  672. pos.y + location.y - CHUNK_SIZE / 2,
  673. pos.z),
  674. zReader,
  675. dimensionId));
  676. }
  677. else
  678. {
  679. putBlockTypeAt(pos, blockType);
  680. }
  681. }
  682. }
  683. initializeLightning();
  684. }
  685. void Chunk::save(Framework::StreamWriter* zWriter)
  686. {
  687. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  688. {
  689. unsigned short blockType
  690. = blocks[index]
  691. ? (unsigned short)blocks[index]->zBlockType()->getId()
  692. : blockIds[index];
  693. zWriter->schreibe((char*)&blockType, 2);
  694. if (blockType)
  695. {
  696. if (blocks[index])
  697. {
  698. bool d = 1;
  699. zWriter->schreibe((char*)&d, 1);
  700. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  701. ->saveBlock(blocks[index], zWriter);
  702. }
  703. else
  704. {
  705. bool d = 0;
  706. zWriter->schreibe((char*)&d, 1);
  707. }
  708. }
  709. }
  710. }
  711. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  712. {
  713. for (int x = 0; x < CHUNK_SIZE; x++)
  714. {
  715. for (int y = 0; y < CHUNK_SIZE; y++)
  716. {
  717. for (int z = 0; z < WORLD_HEIGHT; z++)
  718. {
  719. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  720. unsigned short blockType
  721. = blocks[index]
  722. ? (unsigned short)blocks[index]->zBlockType()->getId()
  723. : blockIds[index];
  724. if (blockType)
  725. {
  726. bool visible = 0;
  727. if (!visible)
  728. {
  729. if (!blocks[index])
  730. {
  731. if (CONST_BLOCK(0, blockIds[index])->isTransparent()
  732. || CONST_BLOCK(0, blockIds[index])
  733. ->isPassable())
  734. visible = 1;
  735. else
  736. {
  737. for (int d = 0; d < 6 && !visible; d++)
  738. {
  739. Vec3<int> pos
  740. = getDirection(
  741. (Directions)getDirectionFromIndex(
  742. d))
  743. + Framework::Vec3<int>(x, y, z);
  744. auto n = zBlockNeighbor(pos);
  745. if (n.isA()
  746. && (((Block*)n)->isPassable()
  747. || ((Block*)n)->isTransparent()))
  748. visible = 1;
  749. if (n.isB()
  750. && (CONST_BLOCK(0, n)->isTransparent()
  751. || CONST_BLOCK(0, n)->isPassable()))
  752. visible = 1;
  753. if (pos.x < 0 || pos.y < 0 || pos.z < 0
  754. || pos.x >= CHUNK_SIZE
  755. || pos.y >= CHUNK_SIZE
  756. || pos.z >= WORLD_HEIGHT)
  757. visible = 1;
  758. }
  759. }
  760. }
  761. else
  762. visible = blocks[index]->isVisible();
  763. }
  764. if (visible
  765. && (blocks[index] || blockType != BlockTypeEnum::AIR))
  766. {
  767. zWriter->schreibe((char*)&blockType, 2);
  768. zWriter->schreibe((char*)&index, 4);
  769. }
  770. }
  771. }
  772. }
  773. }
  774. unsigned short end = 0;
  775. zWriter->schreibe((char*)&end, 2);
  776. }
  777. void Chunk::removeUnusedBlocks()
  778. {
  779. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  780. {
  781. if (!blocks[i] && blockIds[i])
  782. {
  783. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  784. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  785. int z = i % WORLD_HEIGHT;
  786. bool visible = 0;
  787. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  788. || CONST_BLOCK(0, blockIds[i])->isPassable())
  789. visible = 1;
  790. else
  791. {
  792. for (int d = 0; d < 6 && !visible; d++)
  793. {
  794. auto n = zBlockNeighbor(
  795. getDirection((Directions)getDirectionFromIndex(d))
  796. + Framework::Vec3<int>(x, y, z));
  797. if (n.isA()
  798. && (((Block*)n)->isPassable()
  799. || ((Block*)n)->isTransparent()))
  800. visible = 1;
  801. if (n.isB()
  802. && (CONST_BLOCK(0, n)->isTransparent()
  803. || CONST_BLOCK(0, n)->isPassable()))
  804. visible = 1;
  805. }
  806. }
  807. if (!visible)
  808. {
  809. putBlockAt({x, y, z}, 0);
  810. }
  811. }
  812. }
  813. int count = 0;
  814. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  815. {
  816. if (blockIds[i] && blockIds[i] != BlockTypeEnum::AIR) count++;
  817. }
  818. std::cout << "chunk " << location.x << ", " << location.y
  819. << " was generated with " << count << " blocks.\n";
  820. }
  821. int Chunk::getDimensionId() const
  822. {
  823. return dimensionId;
  824. }
  825. void Chunk::onLoaded()
  826. {
  827. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  828. {
  829. if (blocks[i]) blocks[i]->onLoaded();
  830. }
  831. currentlyLoading = 0;
  832. }
  833. void Chunk::onUnloaded()
  834. {
  835. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  836. {
  837. if (blocks[i]) blocks[i]->onUnloaded();
  838. }
  839. }
  840. Framework::Punkt Chunk::getCenter() const
  841. {
  842. return location;
  843. }
  844. Framework::Vec3<int> Chunk::getMin() const
  845. {
  846. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  847. }
  848. Framework::Vec3<int> Chunk::getMax() const
  849. {
  850. return {
  851. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  852. }
  853. void Chunk::prepareRemove()
  854. {
  855. added = 0;
  856. for (int i = 0; i < 4; i++)
  857. {
  858. if (zNeighbours[i])
  859. {
  860. zNeighbours[i]->setNeighbor(
  861. getOppositeDirection(getDirectionFromIndex(i)), 0);
  862. zNeighbours[i] = 0;
  863. }
  864. }
  865. }
  866. void Chunk::setAdded()
  867. {
  868. added = 1;
  869. }
  870. bool Chunk::hasObservers() const
  871. {
  872. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  873. }
  874. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  875. {
  876. int index
  877. = ((location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z)
  878. * 6;
  879. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  880. return lightData + index;
  881. }
  882. void Chunk::setLightData(
  883. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  884. {
  885. int index
  886. = ((location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z)
  887. * 6;
  888. memcpy(lightData + index, data, 6);
  889. // check if neighbor is a visible block and send update to clients
  890. bool needSend = 0;
  891. for (int i = 0; i < 6; i++)
  892. {
  893. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  894. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  895. {
  896. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  897. && pos.y < CHUNK_SIZE)
  898. {
  899. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  900. int type = blockIds[bi];
  901. needSend |= type != BlockTypeEnum::NO_BLOCK
  902. && type != BlockTypeEnum::AIR;
  903. if (needSend) break;
  904. }
  905. else
  906. {
  907. needSend = 1; // TODO: check if the block is visible
  908. }
  909. }
  910. }
  911. if (needSend)
  912. {
  913. broadcastLightData(index, foreground);
  914. }
  915. }