Chunk.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. // TODO: initialize only daylight with a more efficient algorithm here and
  406. // add all light sources to the light update thread when the chunk is added
  407. // to the map
  408. unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
  409. unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
  410. while (true)
  411. {
  412. bool changes = false;
  413. for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
  414. {
  415. for (int x = 0; x < CHUNK_SIZE; x++)
  416. {
  417. for (int y = 0; y < CHUNK_SIZE; y++)
  418. {
  419. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  420. unsigned char* light
  421. = getLightData(Framework::Vec3<int>(x, y, z));
  422. unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
  423. for (int i = 0; i < 6; i++)
  424. {
  425. unsigned char* neighborLeight;
  426. Framework::Vec3<int> neighborPos
  427. = Framework::Vec3<int>(x, y, z)
  428. + getDirection(getDirectionFromIndex(i));
  429. if (neighborPos.z < 0 || neighborPos.x < 0
  430. || neighborPos.y < 0 || neighborPos.x >= CHUNK_SIZE
  431. || neighborPos.y >= CHUNK_SIZE)
  432. {
  433. neighborLeight = noLight;
  434. }
  435. else if (neighborPos.z >= WORLD_HEIGHT)
  436. {
  437. neighborLeight = dayLight;
  438. }
  439. else
  440. {
  441. neighborLeight = getLightData(neighborPos);
  442. }
  443. for (int j = 0; j < 3; j++)
  444. newLight[j] = (unsigned char)MAX(newLight[j],
  445. i == getDirectionIndex(TOP)
  446. ? neighborLeight[j]
  447. : (unsigned char)((float)neighborLeight[j]
  448. * 0.8f));
  449. for (int j = 3; j < 6; j++)
  450. newLight[j] = (unsigned char)MAX(newLight[j],
  451. (unsigned char)((float)neighborLeight[j]
  452. * 0.85f));
  453. }
  454. const Block* current
  455. = blocks[index]
  456. ? blocks[index]
  457. : Game::INSTANCE->zBlockType(blockIds[index])
  458. ->zDefault();
  459. // add own light emission
  460. for (int j = 3; j < 6; j++)
  461. newLight[j] = (unsigned char)MAX(newLight[j],
  462. current->getLightEmisionColor()[j - 3]);
  463. current->filterPassingLight(newLight);
  464. current->filterPassingLight(newLight + 3);
  465. for (int i = 0; i < 6; i++)
  466. {
  467. if (newLight[i] != light[i])
  468. {
  469. changes = 1;
  470. memcpy(light, newLight, 6);
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. if (!changes) break;
  478. }
  479. }
  480. Framework::Either<Block*, int> Chunk::zBlockAt(
  481. Framework::Vec3<int> location) const
  482. {
  483. if (location.z < 0 || location.z >= WORLD_HEIGHT) return 0;
  484. int index = Chunk::index(location);
  485. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  486. if (blocks[index])
  487. return blocks[index];
  488. else
  489. return (int)blockIds[index];
  490. }
  491. const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
  492. {
  493. auto b = zBlockAt(location);
  494. if (b.isA()) return b;
  495. return Game::INSTANCE->zBlockType(b.getB())->zDefault();
  496. }
  497. const Block* Chunk::zBlockConst(int index) const
  498. {
  499. if (blocks[index])
  500. return blocks[index];
  501. else
  502. return Game::INSTANCE->zBlockType(blockIds[index])->zDefault();
  503. }
  504. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  505. {
  506. auto b = zBlockAt(location);
  507. if (b.isA()) return;
  508. if (!b.getB()) generateBlock(location);
  509. b = zBlockAt(location);
  510. if (b.isB())
  511. putBlockAt(location,
  512. Game::INSTANCE->zBlockType(b.getB())->createBlockAt(
  513. {location.x + this->location.x - CHUNK_SIZE / 2,
  514. location.y + this->location.y - CHUNK_SIZE / 2,
  515. location.z},
  516. dimensionId,
  517. 0));
  518. }
  519. void Chunk::generateBlock(Framework::Vec3<int> location)
  520. {
  521. int index = Chunk::index(location);
  522. if (blockIds[index]) return;
  523. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  524. {location.x + this->location.x - CHUNK_SIZE / 2,
  525. location.y + this->location.y - CHUNK_SIZE / 2,
  526. location.z},
  527. dimensionId);
  528. if (generated.isA())
  529. putBlockAt(location, generated);
  530. else
  531. putBlockTypeAt(location, generated);
  532. }
  533. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  534. {
  535. int index = Chunk::index(location);
  536. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
  537. Block* old = blocks[index];
  538. if (old && old->isTickSource() != TickSourceType::NONE)
  539. { // remove from tick sorces
  540. if (old->isTickSource() == TickSourceType::EACH_TICK)
  541. {
  542. for (Framework::ArrayIterator<Block*> obj
  543. = tickSourcesEachTick.begin();
  544. obj;
  545. obj++)
  546. {
  547. if (obj.val() == old)
  548. {
  549. obj.remove();
  550. break;
  551. }
  552. }
  553. }
  554. else if (old->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  555. {
  556. for (Framework::ArrayIterator<Block*> obj
  557. = tickSourcesAfterUpdate.begin();
  558. obj;
  559. obj++)
  560. {
  561. if (obj.val() == old)
  562. {
  563. obj.remove();
  564. break;
  565. }
  566. }
  567. }
  568. }
  569. bool change = 0;
  570. bool wasLightSource
  571. = old ? old->zBlockType()->isLightSource()
  572. : Game::INSTANCE->zBlockType(blockIds[index])->isLightSource();
  573. bool isLightSource = 0;
  574. if (block)
  575. {
  576. change
  577. = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  578. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  579. isLightSource = block->zBlockType()->isLightSource();
  580. }
  581. else
  582. {
  583. if (old != 0)
  584. {
  585. blockIds[index] = BlockTypeEnum::NO_BLOCK;
  586. }
  587. change = old != 0;
  588. }
  589. blocks[index] = block;
  590. for (int i = 0; i < 6; i++)
  591. {
  592. Direction d = getDirectionFromIndex(i);
  593. Chunk* zNeighborChunk = 0;
  594. Framework::Either<Block*, int> neighbor
  595. = zBlockNeighbor(location + getDirection(d), &zNeighborChunk);
  596. if (neighbor.isA())
  597. {
  598. if (block)
  599. {
  600. ((Block*)neighbor)
  601. ->setNeighbour(getOppositeDirection(d), block);
  602. }
  603. else
  604. {
  605. ((Block*)neighbor)
  606. ->setNeighbour(getOppositeDirection(d), blockIds[index]);
  607. }
  608. }
  609. if (block) block->setNeighbour(d, neighbor);
  610. if (zNeighborChunk && zNeighborChunk != this)
  611. {
  612. zNeighborChunk->worldUpdated = 1;
  613. }
  614. }
  615. if (old) old->release();
  616. if (block && block->isTickSource() != TickSourceType::NONE)
  617. { // add to tick sources
  618. if (block->isTickSource() == TickSourceType::EACH_TICK)
  619. {
  620. tickSourcesEachTick.add(block);
  621. }
  622. else if (block->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  623. {
  624. tickSourcesAfterUpdate.add(block);
  625. }
  626. }
  627. worldUpdated = 1;
  628. if (change)
  629. {
  630. if (isLightSource != wasLightSource)
  631. {
  632. if (isLightSource)
  633. addLightSource(index);
  634. else
  635. removeLightSource(index);
  636. }
  637. if (added)
  638. {
  639. sendBlockInfo(location);
  640. if (block)
  641. {
  642. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  643. Framework::Vec3<int>(
  644. location.x + this->location.x - CHUNK_SIZE / 2,
  645. location.y + this->location.y - CHUNK_SIZE / 2,
  646. location.z));
  647. }
  648. }
  649. }
  650. if (added)
  651. {
  652. Game::INSTANCE->zDimension(dimensionId)
  653. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  654. location.y + this->location.y - CHUNK_SIZE / 2,
  655. location.z);
  656. }
  657. }
  658. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  659. {
  660. int index = Chunk::index(location);
  661. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  662. bool wasLightSource
  663. = Game::INSTANCE->zBlockType(blockIds[index])->isLightSource();
  664. bool isLightSource = Game::INSTANCE->zBlockType(type)->isLightSource();
  665. if (blockIds[index] != (unsigned short)type)
  666. {
  667. blockIds[index] = (unsigned short)type;
  668. for (int i = 0; i < 6; i++)
  669. {
  670. Direction d = getDirectionFromIndex(i);
  671. Chunk* zNeighborChunk = 0;
  672. Framework::Either<Block*, int> neighbor
  673. = zBlockNeighbor(location + getDirection(d), &zNeighborChunk);
  674. if (neighbor.isA())
  675. ((Block*)neighbor)
  676. ->setNeighbourType(getOppositeDirection(d), type);
  677. if (zNeighborChunk && zNeighborChunk != this)
  678. {
  679. zNeighborChunk->worldUpdated = 1;
  680. }
  681. }
  682. if (isLightSource != wasLightSource)
  683. {
  684. if (isLightSource)
  685. addLightSource(index);
  686. else
  687. removeLightSource(index);
  688. }
  689. if (added)
  690. {
  691. sendBlockInfo(location);
  692. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  693. Framework::Vec3<int>(
  694. location.x + this->location.x - CHUNK_SIZE / 2,
  695. location.y + this->location.y - CHUNK_SIZE / 2,
  696. location.z));
  697. Game::INSTANCE->zDimension(dimensionId)
  698. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  699. location.y + this->location.y - CHUNK_SIZE / 2,
  700. location.z);
  701. }
  702. worldUpdated = 1;
  703. }
  704. }
  705. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  706. {
  707. int index = Chunk::index(location);
  708. char* msg = new char[14];
  709. msg[0] = 0; // set block
  710. *(unsigned short*)(msg + 1) = blockIds[index];
  711. *(int*)(msg + 3) = index;
  712. char state = 0;
  713. const BlockType* type = Game::INSTANCE->zBlockType(blockIds[index]);
  714. if (type->isFluid())
  715. {
  716. state |= 1;
  717. }
  718. if ((blocks[index] && blocks[index]->isPassable())
  719. || (type->zDefault()->isPassable()))
  720. {
  721. state |= 2;
  722. }
  723. msg[7] = state;
  724. if ((state | 1) == state)
  725. {
  726. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(blocks[index]);
  727. msg[8] = fluidBlock ? fluidBlock->getFlowOptions() : 0;
  728. msg[9] = fluidBlock ? fluidBlock->getDistanceToSource() : 0;
  729. }
  730. if ((state | 2) == state)
  731. {
  732. *(float*)(msg + 10) = blocks[index]
  733. ? blocks[index]->getSpeedModifier()
  734. : type->zDefault()->getSpeedModifier();
  735. }
  736. NetworkMessage* message = new NetworkMessage();
  737. message->addressChunck(this);
  738. message->setMessage(msg, 14);
  739. notifyObservers(message);
  740. if (blocks[index])
  741. {
  742. NetworkMessage* message = new NetworkMessage();
  743. blocks[index]->sendModelInfo(message);
  744. if (message->isEmpty())
  745. {
  746. message->release();
  747. }
  748. else
  749. {
  750. notifyObservers(message);
  751. }
  752. }
  753. cs.lock();
  754. for (int i = 0; i < 6; i++)
  755. {
  756. Direction d = getDirectionFromIndex(i);
  757. Framework::Vec3<int> loc = location + getDirection(d);
  758. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  759. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  760. {
  761. broadcastLightData(Chunk::index(loc), true);
  762. }
  763. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  764. {
  765. NetworkMessage* msg = new NetworkMessage();
  766. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  767. char* message = new char[19];
  768. message[0] = 5;
  769. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  770. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  771. *(int*)(message + 9) = loc.z;
  772. loc -= getDirection(d) * CHUNK_SIZE;
  773. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  774. msg->setMessage(message, 19);
  775. notifyObservers(msg);
  776. }
  777. }
  778. cs.unlock();
  779. }
  780. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  781. {
  782. cs.lock();
  783. int dirIndex = getDirectionIndex(dir);
  784. Chunk* old = zNeighbours[dirIndex];
  785. zNeighbours[dirIndex] = zChunk;
  786. for (int i = 0; i < CHUNK_SIZE; i++)
  787. {
  788. for (int z = 0; z < WORLD_HEIGHT; z++)
  789. {
  790. int index = 0;
  791. int j = 0;
  792. if (dir == NORTH)
  793. {
  794. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  795. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  796. }
  797. else if (dir == EAST)
  798. {
  799. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  800. j = i * WORLD_HEIGHT + z;
  801. }
  802. else if (dir == SOUTH)
  803. {
  804. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  805. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  806. }
  807. else if (dir == WEST)
  808. {
  809. index = i * WORLD_HEIGHT + z;
  810. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  811. }
  812. bool needsTransmission = 0;
  813. if (blocks[index])
  814. {
  815. bool visible = blocks[index]->isVisible();
  816. if (zChunk && zChunk->blocks[j])
  817. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  818. else
  819. {
  820. blocks[index]->setNeighbour(dir, 0);
  821. blocks[index]->setNeighbourType(
  822. dir, zChunk ? zChunk->blockIds[j] : 0);
  823. }
  824. if (!visible && blocks[index]->isVisible())
  825. {
  826. needsTransmission = 1;
  827. }
  828. }
  829. else
  830. {
  831. zNeighbours[dirIndex] = old;
  832. bool visible = isVisible(index);
  833. zNeighbours[dirIndex] = zChunk;
  834. if (!visible && isVisible(index))
  835. {
  836. needsTransmission = 1;
  837. }
  838. }
  839. if (zChunk)
  840. {
  841. if (!blocks[index])
  842. {
  843. if (zChunk->zBlockConst(j)->isTransparent()
  844. && !blockIds[index])
  845. {
  846. generateBlock(Framework::Vec3<int>(
  847. (index / WORLD_HEIGHT) / CHUNK_SIZE,
  848. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  849. index % WORLD_HEIGHT));
  850. }
  851. }
  852. }
  853. if (needsTransmission && added)
  854. {
  855. sendBlockInfo(
  856. Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  857. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  858. index % WORLD_HEIGHT));
  859. }
  860. }
  861. }
  862. cs.unlock();
  863. }
  864. void Chunk::load(Framework::StreamReader* zReader)
  865. {
  866. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  867. {
  868. unsigned short blockType;
  869. zReader->lese((char*)&blockType, 2);
  870. if (blockType)
  871. {
  872. Framework::Vec3<int> pos
  873. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  874. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  875. index % WORLD_HEIGHT);
  876. bool d;
  877. zReader->lese((char*)&d, 1);
  878. if (d)
  879. {
  880. putBlockAt(pos,
  881. Game::INSTANCE->zBlockType(blockType)->loadBlock(
  882. Framework::Vec3<int>(
  883. pos.x + location.x - CHUNK_SIZE / 2,
  884. pos.y + location.y - CHUNK_SIZE / 2,
  885. pos.z),
  886. zReader,
  887. dimensionId));
  888. }
  889. else
  890. {
  891. putBlockTypeAt(pos, blockType);
  892. }
  893. }
  894. }
  895. initializeLightning();
  896. }
  897. void Chunk::save(Framework::StreamWriter* zWriter)
  898. {
  899. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  900. {
  901. unsigned short blockType
  902. = blocks[index]
  903. ? (unsigned short)blocks[index]->zBlockType()->getId()
  904. : blockIds[index];
  905. zWriter->schreibe((char*)&blockType, 2);
  906. if (blockType)
  907. {
  908. if (blocks[index])
  909. {
  910. bool d = 1;
  911. zWriter->schreibe((char*)&d, 1);
  912. Game::INSTANCE->zBlockType(blockType)->saveBlock(
  913. blocks[index], zWriter);
  914. }
  915. else
  916. {
  917. bool d = 0;
  918. zWriter->schreibe((char*)&d, 1);
  919. }
  920. }
  921. }
  922. }
  923. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  924. {
  925. for (int x = 0; x < CHUNK_SIZE; x++)
  926. {
  927. for (int y = 0; y < CHUNK_SIZE; y++)
  928. {
  929. for (int z = 0; z < WORLD_HEIGHT; z++)
  930. {
  931. int index = Chunk::index({x, y, z});
  932. const BlockType* type
  933. = Game::INSTANCE->zBlockType(blockIds[index]);
  934. if (isVisible(index) && type->doesNeedClientInstance())
  935. {
  936. zWriter->schreibe((char*)&blockIds[index], 2);
  937. zWriter->schreibe((char*)&index, 4);
  938. char state = 0;
  939. if (type->isFluid())
  940. {
  941. state |= 1;
  942. }
  943. if ((blocks[index] && blocks[index]->isPassable())
  944. || (type->zDefault()->isPassable()))
  945. {
  946. state |= 2;
  947. }
  948. zWriter->schreibe((char*)&state, 1);
  949. if ((state | 1) == state)
  950. {
  951. FluidBlock* fluidBlock
  952. = dynamic_cast<FluidBlock*>(blocks[index]);
  953. char data
  954. = fluidBlock ? fluidBlock->getFlowOptions() : 0;
  955. zWriter->schreibe(&data, 1);
  956. data = fluidBlock ? fluidBlock->getDistanceToSource()
  957. : 0;
  958. zWriter->schreibe(&data, 1);
  959. }
  960. if ((state | 2) == state)
  961. {
  962. float speedModifier
  963. = blocks[index]
  964. ? blocks[index]->getSpeedModifier()
  965. : type->zDefault()->getSpeedModifier();
  966. zWriter->schreibe((char*)&speedModifier, 4);
  967. }
  968. }
  969. }
  970. }
  971. }
  972. unsigned short end = 0;
  973. zWriter->schreibe((char*)&end, 2);
  974. }
  975. void Chunk::removeUnusedBlocks()
  976. {
  977. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  978. {
  979. if (!blocks[i] && blockIds[i])
  980. {
  981. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  982. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  983. int z = i % WORLD_HEIGHT;
  984. bool visible = 0;
  985. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  986. || CONST_BLOCK(0, blockIds[i])->isPassable())
  987. visible = 1;
  988. else
  989. {
  990. for (int d = 0; d < 6 && !visible; d++)
  991. {
  992. auto n = zBlockNeighbor(
  993. getDirection((Directions)getDirectionFromIndex(d))
  994. + Framework::Vec3<int>(x, y, z),
  995. 0);
  996. if (n.isA()
  997. && (((Block*)n)->isPassable()
  998. || ((Block*)n)->isTransparent()))
  999. visible = 1;
  1000. if (n.isB()
  1001. && (CONST_BLOCK(0, n)->isTransparent()
  1002. || CONST_BLOCK(0, n)->isPassable()))
  1003. visible = 1;
  1004. }
  1005. }
  1006. if (!visible)
  1007. {
  1008. putBlockAt({x, y, z}, 0);
  1009. putBlockTypeAt({x, y, z}, 0);
  1010. }
  1011. }
  1012. }
  1013. int count = 0;
  1014. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  1015. {
  1016. if (Game::INSTANCE->zBlockType(blockIds[i])->doesNeedClientInstance())
  1017. count++;
  1018. }
  1019. Framework::Logging::debug()
  1020. << "chunk " << location.x << ", " << location.y
  1021. << " was generated with " << count << " blocks.";
  1022. }
  1023. int Chunk::getDimensionId() const
  1024. {
  1025. return dimensionId;
  1026. }
  1027. void Chunk::onLoaded()
  1028. {
  1029. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  1030. {
  1031. if (blocks[i]) blocks[i]->onLoaded();
  1032. }
  1033. currentlyLoading = 0;
  1034. }
  1035. void Chunk::onUnloaded()
  1036. {
  1037. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  1038. {
  1039. if (blocks[i]) blocks[i]->onUnloaded();
  1040. }
  1041. }
  1042. Framework::Punkt Chunk::getCenter() const
  1043. {
  1044. return location;
  1045. }
  1046. Framework::Vec3<int> Chunk::getMin() const
  1047. {
  1048. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  1049. }
  1050. Framework::Vec3<int> Chunk::getMax() const
  1051. {
  1052. return {
  1053. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  1054. }
  1055. void Chunk::prepareRemove()
  1056. {
  1057. added = 0;
  1058. cs.lock();
  1059. for (int i = 0; i < 4; i++)
  1060. {
  1061. if (zNeighbours[i])
  1062. {
  1063. zNeighbours[i]->setNeighbor(
  1064. getOppositeDirection(getDirectionFromIndex(i)), 0);
  1065. zNeighbours[i] = 0;
  1066. }
  1067. }
  1068. cs.unlock();
  1069. }
  1070. void Chunk::setAdded()
  1071. {
  1072. added = 1;
  1073. }
  1074. bool Chunk::hasObservers() const
  1075. {
  1076. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  1077. }
  1078. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  1079. {
  1080. int index = Chunk::index(location) * 6;
  1081. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  1082. return lightData + index;
  1083. }
  1084. void Chunk::setLightData(
  1085. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  1086. {
  1087. int index = Chunk::index(location);
  1088. memcpy(lightData + index * 6, data, 6);
  1089. // check if neighbor is a visible block and send update to clients
  1090. bool needSend = 0;
  1091. for (int i = 0; i < 6; i++)
  1092. {
  1093. Framework::Vec3<int> pos
  1094. = location + getDirection(getDirectionFromIndex(i));
  1095. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  1096. {
  1097. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  1098. && pos.y < CHUNK_SIZE)
  1099. {
  1100. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  1101. int type = blockIds[bi];
  1102. needSend |= Game::INSTANCE->zBlockType(type)
  1103. ->doesNeedClientInstance();
  1104. if (needSend) break;
  1105. }
  1106. else
  1107. {
  1108. int type = Game::INSTANCE->getBlockType(
  1109. pos
  1110. + Framework::Vec3<int>(
  1111. this->location.x - CHUNK_SIZE / 2,
  1112. this->location.y - CHUNK_SIZE / 2,
  1113. 0),
  1114. dimensionId);
  1115. needSend |= Game::INSTANCE->zBlockType(type)
  1116. ->doesNeedClientInstance();
  1117. if (needSend) break;
  1118. }
  1119. }
  1120. }
  1121. if (needSend)
  1122. {
  1123. broadcastLightData(index, foreground);
  1124. }
  1125. }
  1126. int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
  1127. {
  1128. return blockIds[index(location)];
  1129. }
  1130. void Chunk::onEntityEnters(Entity* zEntity, Chunk* lastChunk)
  1131. {
  1132. NetworkMessage* msg = 0;
  1133. for (InformationObserver* observer : observers)
  1134. {
  1135. if (!lastChunk || !lastChunk->hasObserver(zEntity->getId()))
  1136. {
  1137. if (!msg)
  1138. {
  1139. msg = new NetworkMessage();
  1140. msg->addEntityMessage(zEntity);
  1141. if (msg->isEmpty()) break;
  1142. }
  1143. observer->sendMessage(msg);
  1144. }
  1145. }
  1146. if (msg) msg->release();
  1147. }
  1148. void Chunk::onEntityLeaves(Entity* zEntity, Chunk* zNextChunk)
  1149. {
  1150. NetworkMessage* msg = 0;
  1151. for (InformationObserver* observer : observers)
  1152. {
  1153. if (!zNextChunk || !zNextChunk->hasObserver(zEntity->getId()))
  1154. {
  1155. if (!msg)
  1156. {
  1157. msg = new NetworkMessage();
  1158. msg->removeEntityMessage(zEntity);
  1159. if (msg->isEmpty()) break;
  1160. }
  1161. observer->sendMessage(msg);
  1162. }
  1163. }
  1164. if (msg) msg->release();
  1165. }
  1166. bool Chunk::hasObserver(int entityId) const
  1167. {
  1168. for (InformationObserver* observer : observers)
  1169. {
  1170. if (observer->getEntityId() == entityId) return 1;
  1171. }
  1172. return 0;
  1173. }