Chunk.cpp 45 KB

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