Chunk.cpp 34 KB

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