Chunk.cpp 46 KB

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