Chunk.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  751. {
  752. auto b = zBlockAt(location);
  753. if (b.isA()) return;
  754. if (!b.getB()) generateBlock(location);
  755. b = zBlockAt(location);
  756. if (b.isB())
  757. putBlockAt(location,
  758. Game::INSTANCE->zBlockType(b.getB())->createBlockAt(
  759. {location.x + this->location.x - CHUNK_SIZE / 2,
  760. location.y + this->location.y - CHUNK_SIZE / 2,
  761. location.z},
  762. dimensionId,
  763. 0));
  764. }
  765. void Chunk::generateBlock(Framework::Vec3<int> location)
  766. {
  767. int index = Chunk::index(location.x, location.y);
  768. if (blockIds[location.z] && blockIds[location.z][index]) return;
  769. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  770. {location.x + this->location.x - CHUNK_SIZE / 2,
  771. location.y + this->location.y - CHUNK_SIZE / 2,
  772. location.z},
  773. dimensionId);
  774. if (generated.isA())
  775. putBlockAt(location, generated);
  776. else
  777. putBlockTypeAt(location, generated);
  778. }
  779. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  780. {
  781. int index = Chunk::index(location.x, location.y);
  782. assert(index < CHUNK_SIZE * CHUNK_SIZE && index >= 0);
  783. Block* old = blocks[location.z] ? blocks[location.z][index] : 0;
  784. if (old && old->isTickSource() != TickSourceType::NONE)
  785. { // remove from tick sorces
  786. if (old->isTickSource() == TickSourceType::EACH_TICK)
  787. {
  788. for (Framework::ArrayIterator<Block*> obj
  789. = tickSourcesEachTick.begin();
  790. obj;
  791. obj++)
  792. {
  793. if (obj.val() == old)
  794. {
  795. obj.remove();
  796. break;
  797. }
  798. }
  799. }
  800. else if (old->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  801. {
  802. for (Framework::ArrayIterator<Block*> obj
  803. = tickSourcesAfterUpdate.begin();
  804. obj;
  805. obj++)
  806. {
  807. if (obj.val() == old)
  808. {
  809. obj.remove();
  810. break;
  811. }
  812. }
  813. }
  814. }
  815. if (!blockIds[location.z])
  816. {
  817. blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
  818. memset(blockIds[location.z],
  819. 0,
  820. CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
  821. }
  822. if (!blocks[location.z])
  823. {
  824. blocks[location.z] = new Block*[CHUNK_SIZE * CHUNK_SIZE];
  825. memset(blocks[location.z], 0, CHUNK_SIZE * CHUNK_SIZE * sizeof(Block*));
  826. }
  827. bool change = 0;
  828. bool wasLightSource
  829. = old ? old->zBlockType()->isLightSource()
  830. : Game::INSTANCE->zBlockType(blockIds[location.z][index])
  831. ->isLightSource();
  832. bool isLightSource = 0;
  833. if (block)
  834. {
  835. change = blockIds[location.z][index]
  836. != (unsigned short)block->zBlockType()->getId();
  837. blockIds[location.z][index]
  838. = (unsigned short)block->zBlockType()->getId();
  839. isLightSource = block->zBlockType()->isLightSource();
  840. }
  841. else
  842. {
  843. if (old != 0)
  844. {
  845. blockIds[location.z][index] = BlockTypeEnum::NO_BLOCK;
  846. }
  847. change = old != 0;
  848. }
  849. blocks[location.z][index] = block;
  850. if (old) old->release();
  851. if (block && block->isTickSource() != TickSourceType::NONE)
  852. { // add to tick sources
  853. if (block->isTickSource() == TickSourceType::EACH_TICK)
  854. {
  855. tickSourcesEachTick.add(block);
  856. }
  857. else if (block->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  858. {
  859. tickSourcesAfterUpdate.add(block);
  860. }
  861. }
  862. worldUpdated = 1;
  863. if (change)
  864. {
  865. if (isLightSource != wasLightSource)
  866. {
  867. if (isLightSource)
  868. addLightSource(location.z, index);
  869. else
  870. removeLightSource(location.z, index);
  871. }
  872. if (added)
  873. {
  874. sendBlockInfo(location);
  875. if (block)
  876. {
  877. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  878. Framework::Vec3<int>(
  879. location.x + this->location.x - CHUNK_SIZE / 2,
  880. location.y + this->location.y - CHUNK_SIZE / 2,
  881. location.z));
  882. }
  883. }
  884. }
  885. if (added)
  886. {
  887. Game::INSTANCE->zDimension(dimensionId)
  888. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  889. location.y + this->location.y - CHUNK_SIZE / 2,
  890. location.z);
  891. }
  892. }
  893. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  894. {
  895. int index = Chunk::index(location.x, location.y);
  896. assert(index < CHUNK_SIZE * CHUNK_SIZE);
  897. int oldType = blockIds[location.z] ? blockIds[location.z][index] : 0;
  898. bool wasLightSource = Game::INSTANCE->zBlockType(oldType)->isLightSource();
  899. bool isLightSource = Game::INSTANCE->zBlockType(type)->isLightSource();
  900. if (oldType != (unsigned short)type)
  901. {
  902. if (!blockIds[location.z])
  903. {
  904. blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
  905. memset(blockIds[location.z],
  906. 0,
  907. CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
  908. }
  909. blockIds[location.z][index] = (unsigned short)type;
  910. if (isLightSource != wasLightSource)
  911. {
  912. if (isLightSource)
  913. addLightSource(location.z, index);
  914. else
  915. removeLightSource(location.z, index);
  916. }
  917. if (added)
  918. {
  919. sendBlockInfo(location);
  920. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  921. Framework::Vec3<int>(
  922. location.x + this->location.x - CHUNK_SIZE / 2,
  923. location.y + this->location.y - CHUNK_SIZE / 2,
  924. location.z));
  925. Game::INSTANCE->zDimension(dimensionId)
  926. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  927. location.y + this->location.y - CHUNK_SIZE / 2,
  928. location.z);
  929. }
  930. worldUpdated = 1;
  931. }
  932. }
  933. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  934. {
  935. int index = Chunk::index(location.x, location.y);
  936. int typeId = blockIds[location.z] ? blockIds[location.z][index] : 0;
  937. char state = 0;
  938. int size = 8;
  939. const BlockType* type = Game::INSTANCE->zBlockType(typeId);
  940. if (type->isFluid())
  941. {
  942. state |= 1;
  943. size += 2;
  944. }
  945. if ((blocks[location.z] && blocks[location.z][index]
  946. && blocks[location.z][index]->isPassable())
  947. || (type->zDefault()->isPassable()))
  948. {
  949. state |= 2;
  950. size += 4;
  951. }
  952. if (blocks[location.z] && blocks[location.z][index]
  953. && blocks[location.z][index]->getFrontDirection()
  954. != type->getDefaultFrontDirection())
  955. {
  956. state |= 4;
  957. size += 1;
  958. }
  959. char* msg = new char[size];
  960. msg[0] = 0; // set block
  961. *(unsigned short*)(msg + 1) = (unsigned short)typeId;
  962. *(int*)(msg + 3) = index * WORLD_HEIGHT + location.z;
  963. msg[7] = state;
  964. int i = 8;
  965. if (state & 1)
  966. {
  967. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(
  968. blocks[location.z] ? blocks[location.z][index] : 0);
  969. msg[i++] = fluidBlock ? fluidBlock->getFlowOptions() : 0;
  970. msg[i++] = fluidBlock ? fluidBlock->getDistanceToSource() : 0;
  971. }
  972. if (state & 2)
  973. {
  974. *(float*)(msg + i) = blocks[location.z] && blocks[location.z][index]
  975. ? blocks[location.z][index]->getSpeedModifier()
  976. : type->zDefault()->getSpeedModifier();
  977. index += 4;
  978. }
  979. if (state & 4)
  980. {
  981. *(msg + i++) = blocks[location.z][index]->getFrontDirection();
  982. }
  983. NetworkMessage* message = new NetworkMessage();
  984. message->addressChunck(this);
  985. message->setMessage(msg, size);
  986. notifyObservers(message);
  987. if (blocks[location.z] && blocks[location.z][index])
  988. {
  989. NetworkMessage* message = new NetworkMessage();
  990. blocks[location.z][index]->sendModelInfo(message);
  991. if (message->isEmpty())
  992. {
  993. message->release();
  994. }
  995. else
  996. {
  997. notifyObservers(message);
  998. }
  999. }
  1000. cs.lock();
  1001. for (int i = 0; i < 6; i++)
  1002. {
  1003. Direction d = getDirectionFromIndex(i);
  1004. Framework::Vec3<int> loc = location + getDirection(d);
  1005. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  1006. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  1007. {
  1008. broadcastLightData(loc.z, Chunk::index(loc.x, loc.y), true);
  1009. }
  1010. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  1011. {
  1012. NetworkMessage* msg = new NetworkMessage();
  1013. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  1014. char* message = new char[19];
  1015. message[0] = 5;
  1016. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  1017. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  1018. *(int*)(message + 9) = loc.z;
  1019. loc -= getDirection(d) * CHUNK_SIZE;
  1020. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  1021. msg->setMessage(message, 19);
  1022. notifyObservers(msg);
  1023. }
  1024. }
  1025. cs.unlock();
  1026. }
  1027. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  1028. {
  1029. cs.lock();
  1030. int dirIndex = getDirectionIndex(dir);
  1031. zNeighbours[dirIndex] = zChunk;
  1032. cs.unlock();
  1033. }
  1034. Chunk* Chunk::zNeighbor(Direction dir) const
  1035. {
  1036. return zNeighbours[getDirectionIndex(dir)];
  1037. }
  1038. void Chunk::load(Framework::StreamReader* zReader)
  1039. {
  1040. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  1041. {
  1042. unsigned short blockType;
  1043. zReader->read((char*)&blockType, 2);
  1044. if (blockType)
  1045. {
  1046. Framework::Vec3<int> pos
  1047. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  1048. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  1049. index % WORLD_HEIGHT);
  1050. bool d;
  1051. zReader->read((char*)&d, 1);
  1052. if (d)
  1053. {
  1054. putBlockAt(pos,
  1055. Game::INSTANCE->zBlockType(blockType)->loadBlock(
  1056. Framework::Vec3<int>(
  1057. pos.x + location.x - CHUNK_SIZE / 2,
  1058. pos.y + location.y - CHUNK_SIZE / 2,
  1059. pos.z),
  1060. zReader,
  1061. dimensionId));
  1062. }
  1063. else
  1064. {
  1065. putBlockTypeAt(pos, blockType);
  1066. }
  1067. }
  1068. }
  1069. initializeLightning();
  1070. unsigned short entityCount;
  1071. zReader->read((char*)&entityCount, 2);
  1072. lastSavedEntityIds.clear();
  1073. entitiesInChunk.clear();
  1074. for (int i = 0; i < entityCount; i++)
  1075. {
  1076. int type;
  1077. zReader->read((char*)&type, 4);
  1078. Entity* entity = Game::INSTANCE->zEntityType(type)->loadEntity(zReader);
  1079. addGeneratedEntity(entity);
  1080. }
  1081. }
  1082. void Chunk::save(Framework::StreamWriter* zWriter,
  1083. Framework::Array<Framework::Point>& otherChunksToSave)
  1084. {
  1085. for (int id : lastSavedEntityIds)
  1086. {
  1087. if (!entitiesInChunk.anyMatch(
  1088. [id](const Entity* e) { return e->getId() == id; }))
  1089. {
  1090. Entity* old = Game::INSTANCE->zEntity(id);
  1091. if (old && old->getDimensionId() == getDimensionId()
  1092. && !old->isRemoved())
  1093. {
  1094. otherChunksToSave.add(Game::getChunkCenter(
  1095. (int)old->getPosition().x, (int)old->getPosition().y));
  1096. }
  1097. }
  1098. }
  1099. for (int index = 0; index < CHUNK_SIZE * CHUNK_SIZE; index++)
  1100. {
  1101. for (int z = 0; z < WORLD_HEIGHT; z++)
  1102. {
  1103. unsigned short blockType = blockIds[z] ? blockIds[z][index] : 0;
  1104. zWriter->write((char*)&blockType, 2);
  1105. if (blockType)
  1106. {
  1107. if (blocks[z] && blocks[z][index])
  1108. {
  1109. bool d = 1;
  1110. zWriter->write((char*)&d, 1);
  1111. Game::INSTANCE->zBlockType(blockType)->saveBlock(
  1112. blocks[z][index], zWriter);
  1113. }
  1114. else
  1115. {
  1116. bool d = 0;
  1117. zWriter->write((char*)&d, 1);
  1118. }
  1119. }
  1120. }
  1121. }
  1122. unsigned short len = 0;
  1123. for (Entity* entity : entitiesInChunk)
  1124. {
  1125. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1126. {
  1127. len++;
  1128. }
  1129. }
  1130. zWriter->write((char*)&len, 2);
  1131. for (Entity* entity : entitiesInChunk)
  1132. {
  1133. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1134. {
  1135. int type = entity->zType()->getId();
  1136. zWriter->write((char*)&type, 4);
  1137. entity->zType()->saveEntity(entity, zWriter);
  1138. if (lastSavedEntityIds.getValueIndex(entity->getId()) < 0)
  1139. {
  1140. entity->getLastSavedChunkCenter().ifPresent(
  1141. [&otherChunksToSave](
  1142. Framework::Point p) { otherChunksToSave.add(p); });
  1143. }
  1144. entity->setLastSavedChunkCenter(getCenter());
  1145. }
  1146. }
  1147. lastSavedEntityIds.clear();
  1148. for (Entity* entity : entitiesInChunk)
  1149. {
  1150. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1151. {
  1152. lastSavedEntityIds.add(entity->getId());
  1153. }
  1154. }
  1155. }
  1156. void Chunk::removeUnusedBlocks()
  1157. {
  1158. // no longer needed becaus only used blocks are generated in the first place
  1159. #ifdef _DEBUG
  1160. int count = 0;
  1161. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1162. {
  1163. for (int z = 0; z < WORLD_HEIGHT; z++)
  1164. {
  1165. if (Game::INSTANCE->zBlockType(blockIds[z] ? blockIds[z][i] : 0)
  1166. ->doesNeedClientInstance())
  1167. count++;
  1168. }
  1169. }
  1170. Framework::Logging::debug()
  1171. << "chunk " << location.x << ", " << location.y
  1172. << " was generated with " << count << " blocks.";
  1173. #endif
  1174. }
  1175. int Chunk::getDimensionId() const
  1176. {
  1177. return dimensionId;
  1178. }
  1179. void Chunk::onLoaded()
  1180. {
  1181. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1182. {
  1183. for (int z = 0; z < WORLD_HEIGHT; z++)
  1184. {
  1185. if (blocks[z] && blocks[z][i]) blocks[z][i]->onLoaded();
  1186. }
  1187. }
  1188. currentlyLoading = 0;
  1189. }
  1190. void Chunk::onUnloaded()
  1191. {
  1192. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1193. {
  1194. for (int z = 0; z < WORLD_HEIGHT; z++)
  1195. {
  1196. if (blocks[z] && blocks[z][i]) blocks[z][i]->onUnloaded();
  1197. }
  1198. }
  1199. }
  1200. Framework::Point Chunk::getCenter() const
  1201. {
  1202. return location;
  1203. }
  1204. Framework::Vec3<int> Chunk::getMin() const
  1205. {
  1206. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  1207. }
  1208. Framework::Vec3<int> Chunk::getMax() const
  1209. {
  1210. return {
  1211. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  1212. }
  1213. void Chunk::prepareRemove()
  1214. {
  1215. added = 0;
  1216. cs.lock();
  1217. for (int i = 0; i < 4; i++)
  1218. {
  1219. if (zNeighbours[i])
  1220. {
  1221. zNeighbours[i]->setNeighbor(
  1222. getOppositeDirection(getDirectionFromIndex(i)), 0);
  1223. zNeighbours[i] = 0;
  1224. }
  1225. }
  1226. for (Entity* entity : entitiesInChunk)
  1227. {
  1228. entity->setRemoved();
  1229. }
  1230. cs.unlock();
  1231. }
  1232. void Chunk::setAdded()
  1233. {
  1234. added = 1;
  1235. }
  1236. bool Chunk::hasObservers() const
  1237. {
  1238. return observers.getEntryCount() > 0 || currentlyLoading;
  1239. }
  1240. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  1241. {
  1242. int index
  1243. = (Chunk::index(location.x, location.y) * WORLD_HEIGHT + location.z)
  1244. * 6;
  1245. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  1246. return lightData + index;
  1247. }
  1248. void Chunk::setLightData(
  1249. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  1250. {
  1251. int index = Chunk::index(location.x, location.y);
  1252. memcpy(lightData + (index * WORLD_HEIGHT + location.z) * 6, data, 6);
  1253. // check if neighbor is a visible block and send update to clients
  1254. bool needSend = 0;
  1255. for (int i = 0; i < 6; i++)
  1256. {
  1257. Framework::Vec3<int> pos
  1258. = location + getDirection(getDirectionFromIndex(i));
  1259. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  1260. {
  1261. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  1262. && pos.y < CHUNK_SIZE)
  1263. {
  1264. int bi = (pos.x * CHUNK_SIZE + pos.y);
  1265. int type = blockIds[pos.z] ? blockIds[pos.z][bi] : 0;
  1266. needSend |= Game::INSTANCE->zBlockType(type)
  1267. ->doesNeedClientInstance();
  1268. if (needSend) break;
  1269. }
  1270. else
  1271. {
  1272. int type = Game::INSTANCE->getBlockType(
  1273. pos
  1274. + Framework::Vec3<int>(
  1275. this->location.x - CHUNK_SIZE / 2,
  1276. this->location.y - CHUNK_SIZE / 2,
  1277. 0),
  1278. dimensionId);
  1279. needSend |= Game::INSTANCE->zBlockType(type)
  1280. ->doesNeedClientInstance();
  1281. if (needSend) break;
  1282. }
  1283. }
  1284. }
  1285. if (needSend)
  1286. {
  1287. broadcastLightData(location.z, index, foreground);
  1288. }
  1289. }
  1290. int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
  1291. {
  1292. return blockIds[location.z]
  1293. ? blockIds[location.z][index(location.x, location.y)]
  1294. : 0;
  1295. }
  1296. int Chunk::getBlockTypeAtWC(int x, int y, int z) const
  1297. {
  1298. auto pos = Dimension::chunkCoordinates({x, y, z});
  1299. return blockIds[pos.z] ? blockIds[pos.z][index(pos.x, pos.y)] : 0;
  1300. }
  1301. void Chunk::onEntityEnters(Entity* zEntity, Chunk* lastChunk)
  1302. {
  1303. cs.lock();
  1304. this->entitiesInChunk.add(dynamic_cast<Entity*>(zEntity->getThis()));
  1305. NetworkMessage* msg = 0;
  1306. for (InformationObserver* observer : observers)
  1307. {
  1308. if (!lastChunk || !lastChunk->hasObserver(zEntity->getId()))
  1309. {
  1310. if (!msg)
  1311. {
  1312. msg = new NetworkMessage();
  1313. msg->addEntityMessage(zEntity);
  1314. if (msg->isEmpty()) break;
  1315. }
  1316. observer->sendMessage(
  1317. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1318. }
  1319. }
  1320. if (msg) msg->release();
  1321. cs.unlock();
  1322. }
  1323. void Chunk::onEntityLeaves(Entity* zEntity, Chunk* zNextChunk)
  1324. {
  1325. cs.lock();
  1326. this->entitiesInChunk.remove(this->entitiesInChunk.indexOf(zEntity));
  1327. NetworkMessage* msg = 0;
  1328. for (InformationObserver* observer : observers)
  1329. {
  1330. if (!zNextChunk || !zNextChunk->hasObserver(zEntity->getId()))
  1331. {
  1332. if (!msg)
  1333. {
  1334. msg = new NetworkMessage();
  1335. msg->removeEntityMessage(zEntity);
  1336. if (msg->isEmpty()) break;
  1337. }
  1338. observer->sendMessage(
  1339. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1340. }
  1341. }
  1342. if (msg) msg->release();
  1343. cs.unlock();
  1344. }
  1345. bool Chunk::hasObserver(int entityId) const
  1346. {
  1347. for (InformationObserver* observer : observers)
  1348. {
  1349. if (observer->getEntityId() == entityId) return 1;
  1350. }
  1351. return 0;
  1352. }
  1353. void Chunk::addGeneratedEntity(Entity* entity)
  1354. {
  1355. entitiesInChunk.add(entity);
  1356. lastSavedEntityIds.add(entity->getId());
  1357. entity->setLastSavedChunkCenter(getCenter());
  1358. entity->setLastChunk(dimensionId, getCenter());
  1359. }
  1360. const Framework::RCArray<Entity>& Chunk::getEntitiesInChunk() const
  1361. {
  1362. return entitiesInChunk;
  1363. }