Chunk.cpp 47 KB

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