Chunk.cpp 45 KB

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