Chunk.cpp 42 KB

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