Chunk.cpp 47 KB

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