Chunk.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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(
  1230. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1231. }
  1232. }
  1233. if (msg) msg->release();
  1234. }
  1235. void Chunk::onEntityLeaves(Entity* zEntity, Chunk* zNextChunk)
  1236. {
  1237. NetworkMessage* msg = 0;
  1238. for (InformationObserver* observer : observers)
  1239. {
  1240. if (!zNextChunk || !zNextChunk->hasObserver(zEntity->getId()))
  1241. {
  1242. if (!msg)
  1243. {
  1244. msg = new NetworkMessage();
  1245. msg->removeEntityMessage(zEntity);
  1246. if (msg->isEmpty()) break;
  1247. }
  1248. observer->sendMessage(
  1249. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1250. }
  1251. }
  1252. if (msg) msg->release();
  1253. }
  1254. bool Chunk::hasObserver(int entityId) const
  1255. {
  1256. for (InformationObserver* observer : observers)
  1257. {
  1258. if (observer->getEntityId() == entityId) return 1;
  1259. }
  1260. return 0;
  1261. }