Chunk.cpp 43 KB

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