Chunk.cpp 39 KB

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