Chunk.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. #include "Chunk.h"
  2. #include <InMemoryBuffer.h>
  3. #include <Logging.h>
  4. #include "BlockType.h"
  5. #include "Constants.h"
  6. #include "Dimension.h"
  7. #include "Entity.h"
  8. #include "EntityType.h"
  9. #include "FluidBlock.h"
  10. #include "Game.h"
  11. #include "WorldGenerator.h"
  12. Chunk::Chunk(Framework::Punkt location, int dimensionId)
  13. : ReferenceCounter(),
  14. dimensionId(dimensionId),
  15. location(location),
  16. added(0),
  17. worldUpdated(1),
  18. currentlyLoading(1)
  19. {
  20. blocks = new Block**[WORLD_HEIGHT];
  21. blockIds = new unsigned short*[WORLD_HEIGHT];
  22. lightData = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6];
  23. memset(blocks, 0, WORLD_HEIGHT * sizeof(Block**));
  24. memset(blockIds, 0, WORLD_HEIGHT * sizeof(unsigned short*));
  25. memset(lightData, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  26. zNeighbours[0] = 0;
  27. zNeighbours[1] = 0;
  28. zNeighbours[2] = 0;
  29. zNeighbours[3] = 0;
  30. }
  31. Chunk::Chunk(Framework::Punkt location,
  32. int dimensionId,
  33. Framework::StreamReader* zReader)
  34. : Chunk(location, dimensionId)
  35. {
  36. load(zReader);
  37. }
  38. Chunk::~Chunk()
  39. {
  40. for (int h = 0; h < WORLD_HEIGHT; h++)
  41. {
  42. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  43. {
  44. if (blocks[h] && blocks[h][i]) blocks[h][i]->release();
  45. }
  46. delete[] blocks[h];
  47. blocks[h] = 0;
  48. delete[] blockIds[h];
  49. blockIds[h] = 0;
  50. }
  51. delete[] blocks;
  52. delete[] blockIds;
  53. delete[] lightData;
  54. }
  55. void Chunk::lock()
  56. {
  57. cs.lock();
  58. }
  59. void Chunk::unlock()
  60. {
  61. cs.unlock();
  62. }
  63. void Chunk::tick(TickQueue* zQueue)
  64. {
  65. for (Block* source : tickSourcesEachTick)
  66. zQueue->addToQueue(source);
  67. if (worldUpdated)
  68. {
  69. worldUpdated = 0;
  70. for (Block* source : tickSourcesAfterUpdate)
  71. {
  72. if (source->needsTick())
  73. {
  74. zQueue->addToQueue(source);
  75. worldUpdated = 1;
  76. }
  77. }
  78. }
  79. }
  80. void Chunk::postTick() {}
  81. void Chunk::addLightSource(int z, int index)
  82. {
  83. for (int i : lightSources)
  84. {
  85. if (i == index * WORLD_HEIGHT + z) return;
  86. }
  87. lightSources.add(index * WORLD_HEIGHT + z);
  88. }
  89. void Chunk::removeLightSource(int z, int index)
  90. {
  91. for (auto i = lightSources.begin(); i; i++)
  92. {
  93. if (i.val() == index * WORLD_HEIGHT + z)
  94. {
  95. i.remove();
  96. return;
  97. }
  98. }
  99. }
  100. void Chunk::sendToClient(Framework::StreamWriter* zWriter, bool* instanceMap)
  101. {
  102. for (int x = 0; x < CHUNK_SIZE; x++)
  103. {
  104. for (int y = 0; y < CHUNK_SIZE; y++)
  105. {
  106. for (int z = 0; z < WORLD_HEIGHT; z++)
  107. {
  108. int index = Chunk::index(x, y);
  109. const BlockType* type = Game::INSTANCE->zBlockType(
  110. blockIds[z] ? blockIds[z][index] : 0);
  111. if (isVisible(z, index) && type->doesNeedClientInstance())
  112. {
  113. int mI = index * WORLD_HEIGHT + z;
  114. if (z < WORLD_HEIGHT - 1)
  115. {
  116. instanceMap[mI + (CHUNK_SIZE + 1) * WORLD_HEIGHT + 1]
  117. = 1;
  118. }
  119. if (z > 0)
  120. {
  121. instanceMap[mI + (CHUNK_SIZE + 1) * WORLD_HEIGHT - 1]
  122. = 1;
  123. }
  124. instanceMap[mI + WORLD_HEIGHT] = 1;
  125. instanceMap[mI + (2 * CHUNK_SIZE + 1) * WORLD_HEIGHT] = 1;
  126. instanceMap[mI + CHUNK_SIZE * WORLD_HEIGHT] = 1;
  127. instanceMap[mI + (CHUNK_SIZE + 2) * WORLD_HEIGHT] = 1;
  128. assert(blockIds[z]);
  129. zWriter->schreibe((char*)&blockIds[z][index], 2);
  130. zWriter->schreibe((char*)&mI, 4);
  131. char state = 0;
  132. if (type->isFluid())
  133. {
  134. state |= 1;
  135. }
  136. if ((blocks[z] && blocks[z][index]
  137. && blocks[z][index]->isPassable())
  138. || (type->zDefault()->isPassable()))
  139. {
  140. state |= 2;
  141. }
  142. zWriter->schreibe((char*)&state, 1);
  143. if ((state | 1) == state)
  144. {
  145. FluidBlock* fluidBlock
  146. = blocks[z]
  147. ? dynamic_cast<FluidBlock*>(blocks[z][index])
  148. : 0;
  149. char data
  150. = fluidBlock ? fluidBlock->getFlowOptions() : 0;
  151. zWriter->schreibe(&data, 1);
  152. data = fluidBlock ? fluidBlock->getDistanceToSource()
  153. : 0;
  154. zWriter->schreibe(&data, 1);
  155. }
  156. if ((state | 2) == state)
  157. {
  158. float speedModifier
  159. = blocks[z] && blocks[z][index]
  160. ? blocks[z][index]->getSpeedModifier()
  161. : type->zDefault()->getSpeedModifier();
  162. zWriter->schreibe((char*)&speedModifier, 4);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. unsigned short end = 0;
  169. zWriter->schreibe((char*)&end, 2);
  170. }
  171. void Chunk::sendLightToClient(
  172. Framework::StreamWriter* zWriter, bool* instanceMap)
  173. {
  174. cs.lock();
  175. Chunk* zNeighbours[4];
  176. for (int i = 0; i < 4; i++)
  177. {
  178. zNeighbours[i]
  179. = this->zNeighbours[i]
  180. ? dynamic_cast<Chunk*>(this->zNeighbours[i]->getThis())
  181. : 0;
  182. if (zNeighbours[i])
  183. {
  184. Direction dir = getDirectionFromIndex(i);
  185. switch (dir)
  186. {
  187. case WEST:
  188. for (int z = 0; z < WORLD_HEIGHT; z++)
  189. {
  190. for (int j = 0; j < CHUNK_SIZE; j++)
  191. {
  192. int type = zNeighbours[i]->getBlockTypeAt(
  193. {CHUNK_SIZE - 1, j, z});
  194. if (type
  195. && Game::INSTANCE->zBlockType(type)
  196. ->doesNeedClientInstance())
  197. {
  198. instanceMap[(CHUNK_SIZE + j + 1) * WORLD_HEIGHT + z]
  199. = 1;
  200. }
  201. }
  202. }
  203. break;
  204. case NORTH:
  205. for (int z = 0; z < WORLD_HEIGHT; z++)
  206. {
  207. for (int j = 0; j < CHUNK_SIZE; j++)
  208. {
  209. int type = zNeighbours[i]->getBlockTypeAt(
  210. {j, CHUNK_SIZE - 1, z});
  211. if (type
  212. && Game::INSTANCE->zBlockType(type)
  213. ->doesNeedClientInstance())
  214. {
  215. instanceMap[((j + 1) * CHUNK_SIZE + 1)
  216. * WORLD_HEIGHT
  217. + z]
  218. = 1;
  219. }
  220. }
  221. }
  222. break;
  223. case EAST:
  224. for (int z = 0; z < WORLD_HEIGHT; z++)
  225. {
  226. for (int j = 0; j < CHUNK_SIZE; j++)
  227. {
  228. int type = zNeighbours[i]->getBlockTypeAt({0, j, z});
  229. if (type
  230. && Game::INSTANCE->zBlockType(type)
  231. ->doesNeedClientInstance())
  232. {
  233. instanceMap[((CHUNK_SIZE)*CHUNK_SIZE + j + 1)
  234. * WORLD_HEIGHT
  235. + z]
  236. = 1;
  237. }
  238. }
  239. }
  240. break;
  241. case SOUTH:
  242. for (int z = 0; z < WORLD_HEIGHT; z++)
  243. {
  244. for (int j = 0; j < CHUNK_SIZE; j++)
  245. {
  246. int type = zNeighbours[i]->getBlockTypeAt({j, 0, z});
  247. if (type
  248. && Game::INSTANCE->zBlockType(type)
  249. ->doesNeedClientInstance())
  250. {
  251. instanceMap[((j + 1) * CHUNK_SIZE + CHUNK_SIZE)
  252. * WORLD_HEIGHT
  253. + z]
  254. = 1;
  255. }
  256. }
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. cs.unlock();
  263. for (int z = 0; z < WORLD_HEIGHT; z++)
  264. {
  265. for (int x = -1; x <= CHUNK_SIZE; x++)
  266. {
  267. for (int y = -1; y <= CHUNK_SIZE; y++)
  268. {
  269. if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y == CHUNK_SIZE))
  270. {
  271. continue;
  272. }
  273. if (instanceMap[((x + 1) * CHUNK_SIZE + y + 1) * WORLD_HEIGHT
  274. + z])
  275. {
  276. if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
  277. {
  278. int index = Chunk::index(x, y) * WORLD_HEIGHT + z;
  279. zWriter->schreibe((char*)&index, 4);
  280. zWriter->schreibe((char*)(lightData + index * 6), 6);
  281. }
  282. else
  283. {
  284. int dir;
  285. int index = 0;
  286. int tmpX = x;
  287. int tmpY = y;
  288. index = (((x + CHUNK_SIZE) % CHUNK_SIZE) * CHUNK_SIZE
  289. + ((y + CHUNK_SIZE) % CHUNK_SIZE))
  290. * WORLD_HEIGHT
  291. + z;
  292. if (x == -1)
  293. {
  294. dir = getDirectionIndex(WEST);
  295. }
  296. else if (y == -1)
  297. {
  298. dir = getDirectionIndex(NORTH);
  299. }
  300. else if (x == CHUNK_SIZE)
  301. {
  302. dir = getDirectionIndex(EAST);
  303. }
  304. else if (y == CHUNK_SIZE)
  305. {
  306. dir = getDirectionIndex(SOUTH);
  307. }
  308. if (zNeighbours[dir])
  309. {
  310. int i = -1;
  311. zWriter->schreibe((char*)&i, 4);
  312. zWriter->schreibe((char*)&x, 4);
  313. zWriter->schreibe((char*)&y, 4);
  314. zWriter->schreibe((char*)&z, 4);
  315. zWriter->schreibe(
  316. (char*)(zNeighbours[dir]->lightData
  317. + index * 6),
  318. 6);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. int end = -2;
  326. zWriter->schreibe((char*)&end, 4);
  327. for (int i = 0; i < 4; i++)
  328. {
  329. if (zNeighbours[i])
  330. {
  331. zNeighbours[i]->release();
  332. }
  333. }
  334. }
  335. bool Chunk::isVisible(int z, int index) const
  336. {
  337. unsigned short blockType = blockIds[z] ? blockIds[z][index] : 0;
  338. if (blockType)
  339. {
  340. if (CONST_BLOCK(0, blockType)->isTransparent()
  341. || CONST_BLOCK(0, blockType)->isPassable())
  342. return 1;
  343. else
  344. {
  345. Framework::Vec3<int> indexPos
  346. = {(index) / CHUNK_SIZE, (index) % CHUNK_SIZE, z};
  347. for (int d = 0; d < 6; d++)
  348. {
  349. Framework::Either<Block*, int> n = BlockTypeEnum::NO_BLOCK;
  350. Framework::Vec3<int> pos
  351. = getDirection((Directions)getDirectionFromIndex(d))
  352. + indexPos;
  353. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  354. && pos.y < CHUNK_SIZE && pos.z >= 0 && pos.z < WORLD_HEIGHT)
  355. {
  356. n = zBlockAt(pos);
  357. }
  358. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
  359. && zNeighbours[d])
  360. {
  361. if (pos.x < 0) pos.x += CHUNK_SIZE;
  362. if (pos.x >= CHUNK_SIZE) pos.x -= CHUNK_SIZE;
  363. if (pos.y < 0) pos.y += CHUNK_SIZE;
  364. if (pos.y >= CHUNK_SIZE) pos.y -= CHUNK_SIZE;
  365. n = zNeighbours[d]->zBlockAt(pos);
  366. }
  367. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
  368. && !zNeighbours[d])
  369. {
  370. return 1;
  371. }
  372. if (n.isA()
  373. && (((Block*)n)->isPassable()
  374. || ((Block*)n)->isTransparent()))
  375. return 1;
  376. if (n.isB()
  377. && (CONST_BLOCK(0, n)->isTransparent()
  378. || CONST_BLOCK(0, n)->isPassable()))
  379. return 1;
  380. }
  381. }
  382. }
  383. return 0;
  384. }
  385. void Chunk::broadcastLightData(int z, int index, bool foreground)
  386. {
  387. int x = index / CHUNK_SIZE;
  388. int y = index % CHUNK_SIZE;
  389. NetworkMessage* msg = new NetworkMessage();
  390. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  391. char* message = new char[19];
  392. message[0] = 5;
  393. *(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
  394. *(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
  395. *(int*)(message + 9) = z;
  396. memcpy(message + 13, lightData + (index * WORLD_HEIGHT + z) * 6, 6);
  397. msg->setMessage(message, 19);
  398. if (!foreground) msg->setUseBackground();
  399. notifyObservers(msg);
  400. }
  401. Framework::Either<Block*, int> Chunk::zBlockNeighbor(
  402. Framework::Vec3<int> location, OUT Chunk** zNeighborChunk)
  403. {
  404. if (location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0
  405. && location.y < CHUNK_SIZE && location.z >= 0
  406. && location.z < WORLD_HEIGHT)
  407. {
  408. int index = Chunk::index(location.x, location.y);
  409. if (zNeighborChunk)
  410. {
  411. *zNeighborChunk = this;
  412. }
  413. if (blocks[location.z] && blocks[location.z][index])
  414. return blocks[location.z][index];
  415. else
  416. return blockIds[location.z] ? (int)blockIds[location.z][index] : 0;
  417. }
  418. if (location.z >= 0 && location.z < WORLD_HEIGHT)
  419. return Game::INSTANCE->zBlockAt(
  420. {location.x + this->location.x - CHUNK_SIZE / 2,
  421. location.y + this->location.y - CHUNK_SIZE / 2,
  422. location.z},
  423. dimensionId,
  424. zNeighborChunk);
  425. return 0;
  426. }
  427. void Chunk::notifyObservers(NetworkMessage* msg)
  428. {
  429. Framework::Array<int> remove;
  430. int index = 0;
  431. for (InformationObserver* observer : observers)
  432. {
  433. if (!observer->sendMessage(
  434. dynamic_cast<NetworkMessage*>(msg->getThis())))
  435. {
  436. remove.add(index, 0);
  437. }
  438. index++;
  439. }
  440. for (int i : remove)
  441. observers.remove(i);
  442. msg->release();
  443. }
  444. void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
  445. {
  446. for (InformationObserver* observer : observers)
  447. {
  448. if (observer->getEntityId() == zEntity->getId()) return;
  449. }
  450. 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.schreibe("\4", 1);
  457. buffer.schreibe((char*)&location.x, 4);
  458. buffer.schreibe((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.lese(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->lese(&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::zBlockConst(int z, int index) const
  732. {
  733. if (blocks[z] && blocks[z][index])
  734. return blocks[z][index];
  735. else
  736. return Game::INSTANCE->zBlockType(blockIds[z] ? blockIds[z][index] : 0)
  737. ->zDefault();
  738. }
  739. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  740. {
  741. auto b = zBlockAt(location);
  742. if (b.isA()) return;
  743. if (!b.getB()) generateBlock(location);
  744. b = zBlockAt(location);
  745. if (b.isB())
  746. putBlockAt(location,
  747. Game::INSTANCE->zBlockType(b.getB())->createBlockAt(
  748. {location.x + this->location.x - CHUNK_SIZE / 2,
  749. location.y + this->location.y - CHUNK_SIZE / 2,
  750. location.z},
  751. dimensionId,
  752. 0));
  753. }
  754. void Chunk::generateBlock(Framework::Vec3<int> location)
  755. {
  756. int index = Chunk::index(location.x, location.y);
  757. if (blockIds[location.z] && blockIds[location.z][index]) return;
  758. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  759. {location.x + this->location.x - CHUNK_SIZE / 2,
  760. location.y + this->location.y - CHUNK_SIZE / 2,
  761. location.z},
  762. dimensionId);
  763. if (generated.isA())
  764. putBlockAt(location, generated);
  765. else
  766. putBlockTypeAt(location, generated);
  767. }
  768. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  769. {
  770. int index = Chunk::index(location.x, location.y);
  771. assert(index < CHUNK_SIZE * CHUNK_SIZE && index >= 0);
  772. Block* old = blocks[location.z] ? blocks[location.z][index] : 0;
  773. if (old && old->isTickSource() != TickSourceType::NONE)
  774. { // remove from tick sorces
  775. if (old->isTickSource() == TickSourceType::EACH_TICK)
  776. {
  777. for (Framework::ArrayIterator<Block*> obj
  778. = tickSourcesEachTick.begin();
  779. obj;
  780. obj++)
  781. {
  782. if (obj.val() == old)
  783. {
  784. obj.remove();
  785. break;
  786. }
  787. }
  788. }
  789. else if (old->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  790. {
  791. for (Framework::ArrayIterator<Block*> obj
  792. = tickSourcesAfterUpdate.begin();
  793. obj;
  794. obj++)
  795. {
  796. if (obj.val() == old)
  797. {
  798. obj.remove();
  799. break;
  800. }
  801. }
  802. }
  803. }
  804. if (!blockIds[location.z])
  805. {
  806. blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
  807. memset(blockIds[location.z],
  808. 0,
  809. CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
  810. }
  811. if (!blocks[location.z])
  812. {
  813. blocks[location.z] = new Block*[CHUNK_SIZE * CHUNK_SIZE];
  814. memset(blocks[location.z], 0, CHUNK_SIZE * CHUNK_SIZE * sizeof(Block*));
  815. }
  816. bool change = 0;
  817. bool wasLightSource
  818. = old ? old->zBlockType()->isLightSource()
  819. : Game::INSTANCE->zBlockType(blockIds[location.z][index])
  820. ->isLightSource();
  821. bool isLightSource = 0;
  822. if (block)
  823. {
  824. change = blockIds[location.z][index]
  825. != (unsigned short)block->zBlockType()->getId();
  826. blockIds[location.z][index]
  827. = (unsigned short)block->zBlockType()->getId();
  828. isLightSource = block->zBlockType()->isLightSource();
  829. }
  830. else
  831. {
  832. if (old != 0)
  833. {
  834. blockIds[location.z][index] = BlockTypeEnum::NO_BLOCK;
  835. }
  836. change = old != 0;
  837. }
  838. blocks[location.z][index] = block;
  839. if (old) old->release();
  840. if (block && block->isTickSource() != TickSourceType::NONE)
  841. { // add to tick sources
  842. if (block->isTickSource() == TickSourceType::EACH_TICK)
  843. {
  844. tickSourcesEachTick.add(block);
  845. }
  846. else if (block->isTickSource() == TickSourceType::AFTER_WORLD_UPDATE)
  847. {
  848. tickSourcesAfterUpdate.add(block);
  849. }
  850. }
  851. worldUpdated = 1;
  852. if (change)
  853. {
  854. if (isLightSource != wasLightSource)
  855. {
  856. if (isLightSource)
  857. addLightSource(location.z, index);
  858. else
  859. removeLightSource(location.z, index);
  860. }
  861. if (added)
  862. {
  863. sendBlockInfo(location);
  864. if (block)
  865. {
  866. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  867. Framework::Vec3<int>(
  868. location.x + this->location.x - CHUNK_SIZE / 2,
  869. location.y + this->location.y - CHUNK_SIZE / 2,
  870. location.z));
  871. }
  872. }
  873. }
  874. if (added)
  875. {
  876. Game::INSTANCE->zDimension(dimensionId)
  877. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  878. location.y + this->location.y - CHUNK_SIZE / 2,
  879. location.z);
  880. }
  881. }
  882. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  883. {
  884. int index = Chunk::index(location.x, location.y);
  885. assert(index < CHUNK_SIZE * CHUNK_SIZE);
  886. int oldType = blockIds[location.z] ? blockIds[location.z][index] : 0;
  887. bool wasLightSource = Game::INSTANCE->zBlockType(oldType)->isLightSource();
  888. bool isLightSource = Game::INSTANCE->zBlockType(type)->isLightSource();
  889. if (oldType != (unsigned short)type)
  890. {
  891. if (!blockIds[location.z])
  892. {
  893. blockIds[location.z] = new unsigned short[CHUNK_SIZE * CHUNK_SIZE];
  894. memset(blockIds[location.z],
  895. 0,
  896. CHUNK_SIZE * CHUNK_SIZE * sizeof(unsigned short));
  897. }
  898. blockIds[location.z][index] = (unsigned short)type;
  899. if (isLightSource != wasLightSource)
  900. {
  901. if (isLightSource)
  902. addLightSource(location.z, index);
  903. else
  904. removeLightSource(location.z, index);
  905. }
  906. if (added)
  907. {
  908. sendBlockInfo(location);
  909. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  910. Framework::Vec3<int>(
  911. location.x + this->location.x - CHUNK_SIZE / 2,
  912. location.y + this->location.y - CHUNK_SIZE / 2,
  913. location.z));
  914. Game::INSTANCE->zDimension(dimensionId)
  915. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  916. location.y + this->location.y - CHUNK_SIZE / 2,
  917. location.z);
  918. }
  919. worldUpdated = 1;
  920. }
  921. }
  922. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  923. {
  924. int index = Chunk::index(location.x, location.y);
  925. char* msg = new char[14];
  926. msg[0] = 0; // set block
  927. int typeId = blockIds[location.z] ? blockIds[location.z][index] : 0;
  928. *(unsigned short*)(msg + 1) = typeId;
  929. *(int*)(msg + 3) = index * WORLD_HEIGHT + location.z;
  930. char state = 0;
  931. const BlockType* type = Game::INSTANCE->zBlockType(typeId);
  932. if (type->isFluid())
  933. {
  934. state |= 1;
  935. }
  936. if ((blocks[location.z] && blocks[location.z][index]
  937. && blocks[location.z][index]->isPassable())
  938. || (type->zDefault()->isPassable()))
  939. {
  940. state |= 2;
  941. }
  942. msg[7] = state;
  943. if ((state | 1) == state)
  944. {
  945. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(
  946. blocks[location.z] ? blocks[location.z][index] : 0);
  947. msg[8] = fluidBlock ? fluidBlock->getFlowOptions() : 0;
  948. msg[9] = fluidBlock ? fluidBlock->getDistanceToSource() : 0;
  949. }
  950. if ((state | 2) == state)
  951. {
  952. *(float*)(msg + 10) = blocks[location.z] && blocks[location.z][index]
  953. ? blocks[location.z][index]->getSpeedModifier()
  954. : type->zDefault()->getSpeedModifier();
  955. }
  956. NetworkMessage* message = new NetworkMessage();
  957. message->addressChunck(this);
  958. message->setMessage(msg, 14);
  959. notifyObservers(message);
  960. if (blocks[location.z] && blocks[location.z][index])
  961. {
  962. NetworkMessage* message = new NetworkMessage();
  963. blocks[location.z][index]->sendModelInfo(message);
  964. if (message->isEmpty())
  965. {
  966. message->release();
  967. }
  968. else
  969. {
  970. notifyObservers(message);
  971. }
  972. }
  973. cs.lock();
  974. for (int i = 0; i < 6; i++)
  975. {
  976. Direction d = getDirectionFromIndex(i);
  977. Framework::Vec3<int> loc = location + getDirection(d);
  978. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  979. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  980. {
  981. broadcastLightData(loc.z, Chunk::index(loc.x, loc.y), true);
  982. }
  983. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  984. {
  985. NetworkMessage* msg = new NetworkMessage();
  986. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  987. char* message = new char[19];
  988. message[0] = 5;
  989. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  990. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  991. *(int*)(message + 9) = loc.z;
  992. loc -= getDirection(d) * CHUNK_SIZE;
  993. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  994. msg->setMessage(message, 19);
  995. notifyObservers(msg);
  996. }
  997. }
  998. cs.unlock();
  999. }
  1000. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  1001. {
  1002. cs.lock();
  1003. int dirIndex = getDirectionIndex(dir);
  1004. zNeighbours[dirIndex] = zChunk;
  1005. cs.unlock();
  1006. }
  1007. Chunk* Chunk::zNeighbor(Direction dir) const
  1008. {
  1009. return zNeighbours[getDirectionIndex(dir)];
  1010. }
  1011. void Chunk::load(Framework::StreamReader* zReader)
  1012. {
  1013. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  1014. {
  1015. unsigned short blockType;
  1016. zReader->lese((char*)&blockType, 2);
  1017. if (blockType)
  1018. {
  1019. Framework::Vec3<int> pos
  1020. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  1021. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  1022. index % WORLD_HEIGHT);
  1023. bool d;
  1024. zReader->lese((char*)&d, 1);
  1025. if (d)
  1026. {
  1027. putBlockAt(pos,
  1028. Game::INSTANCE->zBlockType(blockType)->loadBlock(
  1029. Framework::Vec3<int>(
  1030. pos.x + location.x - CHUNK_SIZE / 2,
  1031. pos.y + location.y - CHUNK_SIZE / 2,
  1032. pos.z),
  1033. zReader,
  1034. dimensionId));
  1035. }
  1036. else
  1037. {
  1038. putBlockTypeAt(pos, blockType);
  1039. }
  1040. }
  1041. }
  1042. initializeLightning();
  1043. unsigned short entityCount;
  1044. zReader->lese((char*)&entityCount, 2);
  1045. lastSavedEntityIds.leeren();
  1046. entitiesInChunk.leeren();
  1047. for (int i = 0; i < entityCount; i++)
  1048. {
  1049. int type;
  1050. zReader->lese((char*)&type, 4);
  1051. Entity* entity = Game::INSTANCE->zEntityType(type)->loadEntity(zReader);
  1052. entitiesInChunk.add(entity);
  1053. lastSavedEntityIds.add(entity->getId());
  1054. }
  1055. }
  1056. void Chunk::save(Framework::StreamWriter* zWriter,
  1057. Framework::Array<Framework::Punkt>& otherChunksToSave)
  1058. {
  1059. for (int id : lastSavedEntityIds)
  1060. {
  1061. if (!entitiesInChunk.anyMatch(
  1062. [id](const Entity* e) { return e->getId() == id; }))
  1063. {
  1064. Entity* old = Game::INSTANCE->zEntity(id);
  1065. if (old && old->getDimensionId() == getDimensionId()
  1066. && !old->isRemoved())
  1067. {
  1068. otherChunksToSave.add(Game::getChunkCenter(
  1069. (int)old->getPosition().x, (int)old->getPosition().y));
  1070. }
  1071. }
  1072. }
  1073. for (int index = 0; index < CHUNK_SIZE * CHUNK_SIZE; index++)
  1074. {
  1075. for (int z = 0; z < WORLD_HEIGHT; z++)
  1076. {
  1077. unsigned short blockType = blockIds[z] ? blockIds[z][index] : 0;
  1078. zWriter->schreibe((char*)&blockType, 2);
  1079. if (blockType)
  1080. {
  1081. if (blocks[z] && blocks[z][index])
  1082. {
  1083. bool d = 1;
  1084. zWriter->schreibe((char*)&d, 1);
  1085. Game::INSTANCE->zBlockType(blockType)->saveBlock(
  1086. blocks[z][index], zWriter);
  1087. }
  1088. else
  1089. {
  1090. bool d = 0;
  1091. zWriter->schreibe((char*)&d, 1);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. unsigned short len = 0;
  1097. for (Entity* entity : entitiesInChunk)
  1098. {
  1099. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1100. {
  1101. len++;
  1102. }
  1103. }
  1104. zWriter->schreibe((char*)&len, 2);
  1105. for (Entity* entity : entitiesInChunk)
  1106. {
  1107. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1108. {
  1109. int type = entity->zType()->getId();
  1110. zWriter->schreibe((char*)&type, 4);
  1111. entity->zType()->saveEntity(entity, zWriter);
  1112. if (lastSavedEntityIds.getWertIndex(entity->getId()) < 0)
  1113. {
  1114. entity->getLastSavedChunkCenter().ifPresent(
  1115. [&otherChunksToSave](
  1116. Framework::Punkt p) { otherChunksToSave.add(p); });
  1117. }
  1118. entity->setLastSavedChunkCenter(getCenter());
  1119. }
  1120. }
  1121. lastSavedEntityIds.leeren();
  1122. for (Entity* entity : entitiesInChunk)
  1123. {
  1124. if (entity->zType()->getId() != EntityTypeEnum::PLAYER)
  1125. {
  1126. lastSavedEntityIds.add(entity->getId());
  1127. }
  1128. }
  1129. }
  1130. void Chunk::removeUnusedBlocks()
  1131. {
  1132. // no longer needed becaus only used blocks are generated in the first place
  1133. #ifdef _DEBUG
  1134. int count = 0;
  1135. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1136. {
  1137. for (int z = 0; z < WORLD_HEIGHT; z++)
  1138. {
  1139. if (Game::INSTANCE->zBlockType(blockIds[z] ? blockIds[z][i] : 0)
  1140. ->doesNeedClientInstance())
  1141. count++;
  1142. }
  1143. }
  1144. Framework::Logging::debug()
  1145. << "chunk " << location.x << ", " << location.y
  1146. << " was generated with " << count << " blocks.";
  1147. #endif
  1148. }
  1149. int Chunk::getDimensionId() const
  1150. {
  1151. return dimensionId;
  1152. }
  1153. void Chunk::onLoaded()
  1154. {
  1155. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1156. {
  1157. for (int z = 0; z < WORLD_HEIGHT; z++)
  1158. {
  1159. if (blocks[z] && blocks[z][i]) blocks[z][i]->onLoaded();
  1160. }
  1161. }
  1162. currentlyLoading = 0;
  1163. }
  1164. void Chunk::onUnloaded()
  1165. {
  1166. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  1167. {
  1168. for (int z = 0; z < WORLD_HEIGHT; z++)
  1169. {
  1170. if (blocks[z] && blocks[z][i]) blocks[z][i]->onUnloaded();
  1171. }
  1172. }
  1173. }
  1174. Framework::Punkt Chunk::getCenter() const
  1175. {
  1176. return location;
  1177. }
  1178. Framework::Vec3<int> Chunk::getMin() const
  1179. {
  1180. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  1181. }
  1182. Framework::Vec3<int> Chunk::getMax() const
  1183. {
  1184. return {
  1185. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  1186. }
  1187. void Chunk::prepareRemove()
  1188. {
  1189. added = 0;
  1190. cs.lock();
  1191. for (int i = 0; i < 4; i++)
  1192. {
  1193. if (zNeighbours[i])
  1194. {
  1195. zNeighbours[i]->setNeighbor(
  1196. getOppositeDirection(getDirectionFromIndex(i)), 0);
  1197. zNeighbours[i] = 0;
  1198. }
  1199. }
  1200. for (Entity* entity : entitiesInChunk)
  1201. {
  1202. entity->setRemoved();
  1203. }
  1204. cs.unlock();
  1205. }
  1206. void Chunk::setAdded()
  1207. {
  1208. added = 1;
  1209. }
  1210. bool Chunk::hasObservers() const
  1211. {
  1212. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  1213. }
  1214. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  1215. {
  1216. int index
  1217. = (Chunk::index(location.x, location.y) * WORLD_HEIGHT + location.z)
  1218. * 6;
  1219. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  1220. return lightData + index;
  1221. }
  1222. void Chunk::setLightData(
  1223. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  1224. {
  1225. int index = Chunk::index(location.x, location.y);
  1226. memcpy(lightData + (index * WORLD_HEIGHT + location.z) * 6, data, 6);
  1227. // check if neighbor is a visible block and send update to clients
  1228. bool needSend = 0;
  1229. for (int i = 0; i < 6; i++)
  1230. {
  1231. Framework::Vec3<int> pos
  1232. = location + getDirection(getDirectionFromIndex(i));
  1233. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  1234. {
  1235. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  1236. && pos.y < CHUNK_SIZE)
  1237. {
  1238. int bi = (pos.x * CHUNK_SIZE + pos.y);
  1239. int type = blockIds[pos.z] ? blockIds[pos.z][bi] : 0;
  1240. needSend |= Game::INSTANCE->zBlockType(type)
  1241. ->doesNeedClientInstance();
  1242. if (needSend) break;
  1243. }
  1244. else
  1245. {
  1246. int type = Game::INSTANCE->getBlockType(
  1247. pos
  1248. + Framework::Vec3<int>(
  1249. this->location.x - CHUNK_SIZE / 2,
  1250. this->location.y - CHUNK_SIZE / 2,
  1251. 0),
  1252. dimensionId);
  1253. needSend |= Game::INSTANCE->zBlockType(type)
  1254. ->doesNeedClientInstance();
  1255. if (needSend) break;
  1256. }
  1257. }
  1258. }
  1259. if (needSend)
  1260. {
  1261. broadcastLightData(location.z, index, foreground);
  1262. }
  1263. }
  1264. int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
  1265. {
  1266. return blockIds[location.z]
  1267. ? blockIds[location.z][index(location.x, location.y)]
  1268. : 0;
  1269. }
  1270. int Chunk::getBlockTypeAtWC(int x, int y, int z) const
  1271. {
  1272. auto pos = Dimension::chunkCoordinates({x, y, z});
  1273. return blockIds[pos.z] ? blockIds[pos.z][index(pos.x, pos.y)] : 0;
  1274. }
  1275. void Chunk::onEntityEnters(Entity* zEntity, Chunk* lastChunk)
  1276. {
  1277. cs.lock();
  1278. this->entitiesInChunk.add(dynamic_cast<Entity*>(zEntity->getThis()));
  1279. NetworkMessage* msg = 0;
  1280. for (InformationObserver* observer : observers)
  1281. {
  1282. if (!lastChunk || !lastChunk->hasObserver(zEntity->getId()))
  1283. {
  1284. if (!msg)
  1285. {
  1286. msg = new NetworkMessage();
  1287. msg->addEntityMessage(zEntity);
  1288. if (msg->isEmpty()) break;
  1289. }
  1290. observer->sendMessage(
  1291. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1292. }
  1293. }
  1294. if (msg) msg->release();
  1295. cs.unlock();
  1296. }
  1297. void Chunk::onEntityLeaves(Entity* zEntity, Chunk* zNextChunk)
  1298. {
  1299. cs.lock();
  1300. this->entitiesInChunk.remove(this->entitiesInChunk.indexOf(zEntity));
  1301. NetworkMessage* msg = 0;
  1302. for (InformationObserver* observer : observers)
  1303. {
  1304. if (!zNextChunk || !zNextChunk->hasObserver(zEntity->getId()))
  1305. {
  1306. if (!msg)
  1307. {
  1308. msg = new NetworkMessage();
  1309. msg->removeEntityMessage(zEntity);
  1310. if (msg->isEmpty()) break;
  1311. }
  1312. observer->sendMessage(
  1313. dynamic_cast<NetworkMessage*>(msg->getThis()));
  1314. }
  1315. }
  1316. if (msg) msg->release();
  1317. cs.unlock();
  1318. }
  1319. bool Chunk::hasObserver(int entityId) const
  1320. {
  1321. for (InformationObserver* observer : observers)
  1322. {
  1323. if (observer->getEntityId() == entityId) return 1;
  1324. }
  1325. return 0;
  1326. }
  1327. void Chunk::addGeneratedEntity(Entity* entity)
  1328. {
  1329. entitiesInChunk.add(entity);
  1330. lastSavedEntityIds.add(entity->getId());
  1331. entity->setLastSavedChunkCenter(getCenter());
  1332. }
  1333. const Framework::RCArray<Entity>& Chunk::getEntitiesInChunk() const
  1334. {
  1335. return entitiesInChunk;
  1336. }