Chunk.cpp 34 KB

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