Chunk.cpp 32 KB

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