Chunk.cpp 34 KB

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