Chunk.cpp 43 KB

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