Chunk.cpp 36 KB

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