Chunk.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. #include "Chunk.h"
  2. #include <AsynchronCall.h>
  3. #include <InMemoryBuffer.h>
  4. #include "Constants.h"
  5. #include "FluidBlock.h"
  6. #include "Game.h"
  7. #include "NoBlock.h"
  8. Chunk::Chunk(Framework::Punkt location, int dimensionId)
  9. : ReferenceCounter(),
  10. dimensionId(dimensionId),
  11. location(location),
  12. added(0),
  13. currentlyLoading(1)
  14. {
  15. blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  16. blockIds = new unsigned short[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  17. lightData = new unsigned char[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6];
  18. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  19. memset(blockIds,
  20. 0,
  21. CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(unsigned short));
  22. memset(lightData, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  23. zNeighbours[0] = 0;
  24. zNeighbours[1] = 0;
  25. zNeighbours[2] = 0;
  26. zNeighbours[3] = 0;
  27. }
  28. Chunk::Chunk(Framework::Punkt location,
  29. int dimensionId,
  30. Framework::StreamReader* zReader)
  31. : Chunk(location, dimensionId)
  32. {
  33. load(zReader);
  34. }
  35. Chunk::~Chunk()
  36. {
  37. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  38. {
  39. if (blocks[i]) blocks[i]->release();
  40. }
  41. delete[] blocks;
  42. delete[] blockIds;
  43. delete[] lightData;
  44. }
  45. void Chunk::lock()
  46. {
  47. cs.lock();
  48. }
  49. void Chunk::unlock()
  50. {
  51. cs.unlock();
  52. }
  53. void Chunk::tick(TickQueue* zQueue)
  54. {
  55. for (Block* source : tickSources)
  56. zQueue->addToQueue(source);
  57. }
  58. void Chunk::postTick() {}
  59. void Chunk::addLightSource(int index)
  60. {
  61. for (int i : lightSources)
  62. {
  63. if (i == index) return;
  64. }
  65. lightSources.add(index);
  66. }
  67. void Chunk::removeLightSource(int index)
  68. {
  69. for (auto i = lightSources.begin(); i; i++)
  70. {
  71. if (i.val() == index)
  72. {
  73. i.remove();
  74. return;
  75. }
  76. }
  77. }
  78. void Chunk::sendLightToClient(Framework::StreamWriter* zWriter)
  79. {
  80. for (int z = 0; z < WORLD_HEIGHT; z++)
  81. {
  82. for (int x = -1; x <= CHUNK_SIZE; x++)
  83. {
  84. for (int y = -1; y <= CHUNK_SIZE; y++)
  85. {
  86. if ((x < 0 || x == CHUNK_SIZE) && (y < 0 || y > CHUNK_SIZE))
  87. {
  88. continue;
  89. }
  90. bool needSend = 0;
  91. for (int i = 0; i < 6; i++)
  92. {
  93. Vec3<int> pos = Vec3<int>(x, y, z)
  94. + getDirection(getDirectionFromIndex(i));
  95. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  96. {
  97. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  98. && pos.y < CHUNK_SIZE)
  99. {
  100. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  101. + pos.z;
  102. int type = blockIds[bi];
  103. needSend |= type != BlockTypeEnum::NO_BLOCK
  104. && StaticRegistry<BlockType>::INSTANCE
  105. .zElement(type)
  106. ->doesNeedClientInstance();
  107. }
  108. else
  109. {
  110. if (x >= 0 && x < CHUNK_SIZE && y >= 0
  111. && y < CHUNK_SIZE)
  112. {
  113. cs.lock();
  114. if (i < 4 && zNeighbours[i])
  115. {
  116. Vec3<int> offset
  117. = getDirection(getDirectionFromIndex(i))
  118. * 16;
  119. int bi = ((pos.x - offset.x) * CHUNK_SIZE
  120. + (pos.y - offset.y))
  121. * WORLD_HEIGHT
  122. + (pos.z - offset.z);
  123. int type = zNeighbours[i]->blockIds[bi];
  124. needSend
  125. |= StaticRegistry<BlockType>::INSTANCE
  126. .zElement(type)
  127. ->doesNeedClientInstance();
  128. }
  129. cs.unlock();
  130. }
  131. }
  132. if (needSend) break;
  133. }
  134. }
  135. if (needSend)
  136. {
  137. if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE)
  138. {
  139. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  140. zWriter->schreibe((char*)&index, 4);
  141. zWriter->schreibe((char*)(lightData + index * 6), 6);
  142. }
  143. else
  144. {
  145. int dir;
  146. int index = 0;
  147. if (x == -1)
  148. {
  149. dir = getDirectionIndex(WEST);
  150. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y)
  151. * WORLD_HEIGHT
  152. + z;
  153. }
  154. else if (y == -1)
  155. {
  156. dir = getDirectionIndex(NORTH);
  157. index = (x * CHUNK_SIZE + CHUNK_SIZE - 1)
  158. * WORLD_HEIGHT
  159. + z;
  160. }
  161. else if (x == CHUNK_SIZE)
  162. {
  163. dir = getDirectionIndex(EAST);
  164. index = y * WORLD_HEIGHT + z;
  165. }
  166. else if (y == CHUNK_SIZE)
  167. {
  168. dir = getDirectionIndex(SOUTH);
  169. index = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  170. }
  171. cs.lock();
  172. if (zNeighbours[dir])
  173. {
  174. int i = -1;
  175. zWriter->schreibe((char*)&i, 4);
  176. zWriter->schreibe((char*)&x, 4);
  177. zWriter->schreibe((char*)&y, 4);
  178. zWriter->schreibe((char*)&z, 4);
  179. zWriter->schreibe(
  180. (char*)(zNeighbours[dir]->lightData
  181. + index * 6),
  182. 6);
  183. }
  184. cs.unlock();
  185. }
  186. }
  187. }
  188. }
  189. }
  190. int end = -2;
  191. zWriter->schreibe((char*)&end, 4);
  192. }
  193. bool Chunk::isVisible(int index) const
  194. {
  195. if (!blocks[index])
  196. {
  197. unsigned short blockType
  198. = blocks[index]
  199. ? (unsigned short)blocks[index]->zBlockType()->getId()
  200. : blockIds[index];
  201. if (blockType)
  202. {
  203. if (CONST_BLOCK(0, blockIds[index])->isTransparent()
  204. || CONST_BLOCK(0, blockIds[index])->isPassable())
  205. return 1;
  206. else
  207. {
  208. Vec3<int> indexPos = {(index / WORLD_HEIGHT) / CHUNK_SIZE,
  209. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  210. index % WORLD_HEIGHT};
  211. for (int d = 0; d < 6; d++)
  212. {
  213. Either<Block*, int> n = BlockTypeEnum::NO_BLOCK;
  214. Vec3<int> pos
  215. = getDirection((Directions)getDirectionFromIndex(d))
  216. + indexPos;
  217. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  218. && pos.y < CHUNK_SIZE && pos.z >= 0
  219. && pos.z < WORLD_HEIGHT)
  220. {
  221. n = zBlockAt(pos);
  222. }
  223. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
  224. && zNeighbours[d])
  225. {
  226. if (pos.x < 0) pos.x += CHUNK_SIZE;
  227. if (pos.x >= CHUNK_SIZE) pos.x -= CHUNK_SIZE;
  228. if (pos.y < 0) pos.y += CHUNK_SIZE;
  229. if (pos.y >= CHUNK_SIZE) pos.y -= CHUNK_SIZE;
  230. n = zNeighbours[d]->zBlockAt(pos);
  231. }
  232. else if (pos.z >= 0 && pos.z < WORLD_HEIGHT && d < 4
  233. && !zNeighbours[d])
  234. {
  235. return 1;
  236. }
  237. if (n.isA()
  238. && (((Block*)n)->isPassable()
  239. || ((Block*)n)->isTransparent()))
  240. return 1;
  241. if (n.isB()
  242. && (CONST_BLOCK(0, n)->isTransparent()
  243. || CONST_BLOCK(0, n)->isPassable()))
  244. return 1;
  245. }
  246. }
  247. }
  248. return 0;
  249. }
  250. else
  251. return blocks[index]->isVisible();
  252. }
  253. void Chunk::broadcastLightData(int index, bool foreground)
  254. {
  255. int x = (index / WORLD_HEIGHT) / CHUNK_SIZE;
  256. int y = (index / WORLD_HEIGHT) % CHUNK_SIZE;
  257. int z = index % WORLD_HEIGHT;
  258. NetworkMessage* msg = new NetworkMessage();
  259. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  260. char* message = new char[19];
  261. message[0] = 5;
  262. *(int*)(message + 1) = x + this->location.x - CHUNK_SIZE / 2;
  263. *(int*)(message + 5) = y + this->location.y - CHUNK_SIZE / 2;
  264. *(int*)(message + 9) = z;
  265. memcpy(message + 13, lightData + index * 6, 6);
  266. msg->setMessage(message, 19);
  267. if (!foreground) msg->setUseBackground();
  268. notifyObservers(msg);
  269. }
  270. Framework::Either<Block*, int> Chunk::zBlockNeighbor(
  271. Framework::Vec3<int> location)
  272. {
  273. if (location.x >= 0 && location.x < CHUNK_SIZE && location.y >= 0
  274. && location.y < CHUNK_SIZE && location.z >= 0
  275. && location.z < WORLD_HEIGHT)
  276. {
  277. int index = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT
  278. + location.z;
  279. if (blocks[index])
  280. return blocks[index];
  281. else
  282. return (int)blockIds[index];
  283. }
  284. if (added && location.z >= 0 && location.z < WORLD_HEIGHT)
  285. return Game::INSTANCE->zBlockAt(
  286. {location.x + this->location.x - CHUNK_SIZE / 2,
  287. location.y + this->location.y - CHUNK_SIZE / 2,
  288. location.z},
  289. dimensionId);
  290. return 0;
  291. }
  292. void Chunk::notifyObservers(NetworkMessage* msg)
  293. {
  294. Array<int> remove;
  295. int index = 0;
  296. for (InformationObserver* observer : observers)
  297. {
  298. if (!observer->sendMessage(
  299. dynamic_cast<NetworkMessage*>(msg->getThis())))
  300. {
  301. remove.add(index, 0);
  302. }
  303. index++;
  304. }
  305. for (int i : remove)
  306. observers.remove(i);
  307. msg->release();
  308. }
  309. void Chunk::addObserver(Entity* zEntity, DoLaterHandler& laterHandler)
  310. {
  311. for (InformationObserver* observer : observers)
  312. {
  313. if (observer->getEntityId() == zEntity->getId()) return;
  314. }
  315. int id = zEntity->getId();
  316. observers.add(new InformationObserver(id));
  317. laterHandler.addTodo([this, id]() {
  318. InMemoryBuffer buffer;
  319. buffer.schreibe("\4", 1);
  320. buffer.schreibe((char*)&location.x, 4);
  321. buffer.schreibe((char*)&location.y, 4);
  322. sendToClient(&buffer);
  323. sendLightToClient(&buffer);
  324. NetworkMessage* msg = new NetworkMessage();
  325. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  326. std::cout << "chunk size: " << buffer.getSize() << "b\n";
  327. char* message = new char[buffer.getSize()];
  328. buffer.lese(message, (int)buffer.getSize());
  329. msg->setMessage(message, (int)buffer.getSize());
  330. msg->setUseBackground();
  331. Entity* e = Game::INSTANCE->zEntity(id);
  332. if (e)
  333. {
  334. Game::INSTANCE->sendMessage(msg, e);
  335. }
  336. else
  337. msg->release();
  338. });
  339. }
  340. void Chunk::removeObserver(Entity* zEntity)
  341. {
  342. int index = 0;
  343. for (InformationObserver* observer : observers)
  344. {
  345. if (observer->getEntityId() == zEntity->getId())
  346. {
  347. observers.remove(index);
  348. return;
  349. }
  350. index++;
  351. }
  352. }
  353. void Chunk::api(Framework::StreamReader* zRequest,
  354. Entity* zSource,
  355. DoLaterHandler& laterHandler)
  356. {
  357. char type;
  358. zRequest->lese(&type, 1);
  359. switch (type)
  360. {
  361. case 0:
  362. // register observer
  363. addObserver(zSource, laterHandler);
  364. break;
  365. case 1:
  366. // unsubscribe
  367. removeObserver(zSource);
  368. break;
  369. case 2:
  370. // observer ready
  371. for (InformationObserver* observer : observers)
  372. {
  373. if (observer->getEntityId() == zSource->getId())
  374. {
  375. observer->setReady();
  376. }
  377. }
  378. }
  379. }
  380. void Chunk::initializeLightning()
  381. {
  382. unsigned char dayLight[6] = {255, 255, 255, 0, 0, 0};
  383. unsigned char noLight[6] = {0, 0, 0, 0, 0, 0};
  384. while (true)
  385. {
  386. bool changes = false;
  387. for (int z = WORLD_HEIGHT - 1; z >= 0; z--)
  388. {
  389. for (int x = 0; x < CHUNK_SIZE; x++)
  390. {
  391. for (int y = 0; y < CHUNK_SIZE; y++)
  392. {
  393. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  394. unsigned char* light = getLightData(Vec3<int>(x, y, z));
  395. unsigned char newLight[6] = {0, 0, 0, 0, 0, 0};
  396. for (int i = 0; i < 6; i++)
  397. {
  398. unsigned char* neighborLeight;
  399. Vec3<int> neighborPos
  400. = Vec3<int>(x, y, z)
  401. + getDirection(getDirectionFromIndex(i));
  402. if (neighborPos.z < 0 || neighborPos.x < 0
  403. || neighborPos.y < 0 || neighborPos.x >= CHUNK_SIZE
  404. || neighborPos.y >= CHUNK_SIZE)
  405. {
  406. neighborLeight = noLight;
  407. }
  408. else if (neighborPos.z >= WORLD_HEIGHT)
  409. {
  410. neighborLeight = dayLight;
  411. }
  412. else
  413. {
  414. neighborLeight = getLightData(neighborPos);
  415. }
  416. for (int j = 0; j < 3; j++)
  417. newLight[j] = (unsigned char)MAX(newLight[j],
  418. i == getDirectionIndex(TOP)
  419. ? neighborLeight[j]
  420. : (unsigned char)((float)neighborLeight[j]
  421. * 0.8f));
  422. for (int j = 3; j < 6; j++)
  423. newLight[j] = (unsigned char)MAX(newLight[j],
  424. (unsigned char)((float)neighborLeight[j]
  425. * 0.85f));
  426. }
  427. const Block* current
  428. = blocks[index] ? blocks[index]
  429. : StaticRegistry<BlockType>::INSTANCE
  430. .zElement(blockIds[index])
  431. ->zDefault();
  432. // add own light emission
  433. for (int j = 3; j < 6; j++)
  434. newLight[j] = (unsigned char)MAX(newLight[j],
  435. current->getLightEmisionColor()[j - 3]);
  436. current->filterPassingLight(newLight);
  437. current->filterPassingLight(newLight + 3);
  438. for (int i = 0; i < 6; i++)
  439. {
  440. if (newLight[i] != light[i])
  441. {
  442. changes = 1;
  443. memcpy(light, newLight, 6);
  444. break;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. if (!changes) break;
  451. }
  452. }
  453. Framework::Either<Block*, int> Chunk::zBlockAt(
  454. Framework::Vec3<int> location) const
  455. {
  456. int index = Chunk::index(location);
  457. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  458. if (blocks[index])
  459. return blocks[index];
  460. else
  461. return (int)blockIds[index];
  462. }
  463. const Block* Chunk::zBlockConst(Framework::Vec3<int> location) const
  464. {
  465. auto b = zBlockAt(location);
  466. if (b.isA()) return b;
  467. return StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())->zDefault();
  468. }
  469. const Block* Chunk::zBlockConst(int index) const
  470. {
  471. if (blocks[index])
  472. return blocks[index];
  473. else
  474. return StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  475. ->zDefault();
  476. }
  477. void Chunk::instantiateBlock(Framework::Vec3<int> location)
  478. {
  479. auto b = zBlockAt(location);
  480. if (b.isA()) return;
  481. if (!b.getB()) generateBlock(location);
  482. b = zBlockAt(location);
  483. if (b.isB())
  484. putBlockAt(location,
  485. StaticRegistry<BlockType>::INSTANCE.zElement(b.getB())
  486. ->createBlockAt(
  487. {location.x + this->location.x - CHUNK_SIZE / 2,
  488. location.y + this->location.y - CHUNK_SIZE / 2,
  489. location.z},
  490. dimensionId,
  491. 0));
  492. }
  493. void Chunk::generateBlock(Framework::Vec3<int> location)
  494. {
  495. int index = Chunk::index(location);
  496. if (blockIds[index]) return;
  497. auto generated = Game::INSTANCE->zGenerator()->generateSingleBlock(
  498. {location.x + this->location.x - CHUNK_SIZE / 2,
  499. location.y + this->location.y - CHUNK_SIZE / 2,
  500. location.z},
  501. dimensionId);
  502. if (generated.isA())
  503. putBlockAt(location, generated);
  504. else
  505. putBlockTypeAt(location, generated);
  506. }
  507. void Chunk::putBlockAt(Framework::Vec3<int> location, Block* block)
  508. {
  509. int index = Chunk::index(location);
  510. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT && index >= 0);
  511. Block* old = blocks[index];
  512. if (old && old->isTickSource())
  513. { // remove from tick sorces
  514. for (Framework::Iterator<Block*> obj = tickSources.begin(); obj; obj++)
  515. {
  516. if (obj.val() == old)
  517. {
  518. obj.remove();
  519. break;
  520. }
  521. }
  522. }
  523. bool change = 0;
  524. bool wasLightSource
  525. = old ? old->zBlockType()->isLightSource()
  526. : StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  527. ->isLightSource();
  528. bool isLightSource = 0;
  529. if (block)
  530. {
  531. change
  532. = blockIds[index] != (unsigned short)block->zBlockType()->getId();
  533. blockIds[index] = (unsigned short)block->zBlockType()->getId();
  534. isLightSource = block->zBlockType()->isLightSource();
  535. }
  536. else
  537. {
  538. if (old != 0)
  539. {
  540. blockIds[index] = BlockTypeEnum::NO_BLOCK;
  541. }
  542. change = old != 0;
  543. }
  544. blocks[index] = block;
  545. for (int i = 0; i < 6; i++)
  546. {
  547. Direction d = getDirectionFromIndex(i);
  548. Either<Block*, int> neighbor
  549. = zBlockNeighbor(location + getDirection(d));
  550. if (neighbor.isA())
  551. {
  552. if (block)
  553. {
  554. ((Block*)neighbor)
  555. ->setNeighbour(getOppositeDirection(d), block);
  556. }
  557. else
  558. {
  559. ((Block*)neighbor)
  560. ->setNeighbour(getOppositeDirection(d), blockIds[index]);
  561. }
  562. }
  563. if (block) block->setNeighbour(d, neighbor);
  564. }
  565. if (old) old->release();
  566. if (block && block->isTickSource())
  567. { // add to tick sources
  568. tickSources.add(block);
  569. }
  570. if (change)
  571. {
  572. if (isLightSource != wasLightSource)
  573. {
  574. if (isLightSource)
  575. addLightSource(index);
  576. else
  577. removeLightSource(index);
  578. }
  579. if (added)
  580. {
  581. sendBlockInfo(location);
  582. if (block)
  583. {
  584. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  585. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  586. location.y + this->location.y - CHUNK_SIZE / 2,
  587. location.z));
  588. }
  589. }
  590. }
  591. if (added)
  592. {
  593. Game::INSTANCE->zDimension(dimensionId)
  594. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  595. location.y + this->location.y - CHUNK_SIZE / 2,
  596. location.z);
  597. }
  598. }
  599. void Chunk::putBlockTypeAt(Framework::Vec3<int> location, int type)
  600. {
  601. int index = Chunk::index(location);
  602. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  603. bool wasLightSource
  604. = StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[index])
  605. ->isLightSource();
  606. bool isLightSource
  607. = StaticRegistry<BlockType>::INSTANCE.zElement(type)->isLightSource();
  608. if (blockIds[index] != (unsigned short)type)
  609. {
  610. blockIds[index] = (unsigned short)type;
  611. for (int i = 0; i < 6; i++)
  612. {
  613. Direction d = getDirectionFromIndex(i);
  614. Either<Block*, int> neighbor
  615. = zBlockNeighbor(location + getDirection(d));
  616. if (neighbor.isA())
  617. ((Block*)neighbor)
  618. ->setNeighbourType(getOppositeDirection(d), type);
  619. }
  620. if (isLightSource != wasLightSource)
  621. {
  622. if (isLightSource)
  623. addLightSource(index);
  624. else
  625. removeLightSource(index);
  626. }
  627. if (added)
  628. {
  629. sendBlockInfo(location);
  630. Game::INSTANCE->updateLightningWithoutWait(getDimensionId(),
  631. Vec3<int>(location.x + this->location.x - CHUNK_SIZE / 2,
  632. location.y + this->location.y - CHUNK_SIZE / 2,
  633. location.z));
  634. Game::INSTANCE->zDimension(dimensionId)
  635. ->updateMap(location.x + this->location.x - CHUNK_SIZE / 2,
  636. location.y + this->location.y - CHUNK_SIZE / 2,
  637. location.z);
  638. }
  639. }
  640. }
  641. void Chunk::sendBlockInfo(Framework::Vec3<int> location)
  642. {
  643. int index = Chunk::index(location);
  644. char* msg = new char[9];
  645. msg[0] = 0; // set block
  646. *(int*)(msg + 1) = index;
  647. *(int*)(msg + 5) = blockIds[index];
  648. NetworkMessage* message = new NetworkMessage();
  649. message->addressChunck(this);
  650. message->setMessage(msg, 9);
  651. notifyObservers(message);
  652. if (blocks[index])
  653. {
  654. NetworkMessage* message = new NetworkMessage();
  655. blocks[index]->sendModelInfo(message);
  656. if (message->isEmpty())
  657. {
  658. message->release();
  659. }
  660. else
  661. {
  662. notifyObservers(message);
  663. }
  664. }
  665. cs.lock();
  666. for (int i = 0; i < 6; i++)
  667. {
  668. Direction d = getDirectionFromIndex(i);
  669. Framework::Vec3<int> loc = location + getDirection(d);
  670. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  671. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  672. {
  673. broadcastLightData(Chunk::index(loc), true);
  674. }
  675. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  676. {
  677. NetworkMessage* msg = new NetworkMessage();
  678. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  679. char* message = new char[19];
  680. message[0] = 5;
  681. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  682. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  683. *(int*)(message + 9) = loc.z;
  684. loc -= getDirection(d) * CHUNK_SIZE;
  685. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  686. msg->setMessage(message, 19);
  687. notifyObservers(msg);
  688. }
  689. }
  690. cs.unlock();
  691. }
  692. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  693. {
  694. cs.lock();
  695. int dirIndex = getDirectionIndex(dir);
  696. Chunk* old = zNeighbours[dirIndex];
  697. zNeighbours[dirIndex] = zChunk;
  698. for (int i = 0; i < CHUNK_SIZE; i++)
  699. {
  700. for (int z = 0; z < WORLD_HEIGHT; z++)
  701. {
  702. int index = 0;
  703. int j = 0;
  704. if (dir == NORTH)
  705. {
  706. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  707. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  708. }
  709. else if (dir == EAST)
  710. {
  711. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  712. j = i * WORLD_HEIGHT + z;
  713. }
  714. else if (dir == SOUTH)
  715. {
  716. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  717. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  718. }
  719. else if (dir == WEST)
  720. {
  721. index = i * WORLD_HEIGHT + z;
  722. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  723. }
  724. bool needsTransmission = 0;
  725. if (blocks[index])
  726. {
  727. bool visible = blocks[index]->isVisible();
  728. if (zChunk && zChunk->blocks[j])
  729. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  730. else
  731. {
  732. blocks[index]->setNeighbour(dir, 0);
  733. blocks[index]->setNeighbourType(
  734. dir, zChunk ? zChunk->blockIds[j] : 0);
  735. }
  736. if (!visible && blocks[index]->isVisible())
  737. {
  738. needsTransmission = 1;
  739. }
  740. }
  741. else
  742. {
  743. zNeighbours[dirIndex] = old;
  744. bool visible = isVisible(index);
  745. zNeighbours[dirIndex] = zChunk;
  746. if (!visible && isVisible(index))
  747. {
  748. needsTransmission = 1;
  749. }
  750. }
  751. if (zChunk)
  752. {
  753. if (!blocks[index])
  754. {
  755. if (zChunk->zBlockConst(j)->isTransparent()
  756. && !blockIds[index])
  757. {
  758. generateBlock(
  759. Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  760. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  761. index % WORLD_HEIGHT));
  762. }
  763. }
  764. }
  765. if (needsTransmission && added)
  766. {
  767. sendBlockInfo(Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  768. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  769. index % WORLD_HEIGHT));
  770. }
  771. }
  772. }
  773. cs.unlock();
  774. }
  775. void Chunk::load(Framework::StreamReader* zReader)
  776. {
  777. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  778. {
  779. unsigned short blockType;
  780. zReader->lese((char*)&blockType, 2);
  781. if (blockType)
  782. {
  783. Framework::Vec3<int> pos
  784. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  785. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  786. index % WORLD_HEIGHT);
  787. bool d;
  788. zReader->lese((char*)&d, 1);
  789. if (d)
  790. {
  791. putBlockAt(pos,
  792. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  793. ->loadBlock(Framework::Vec3<int>(
  794. pos.x + location.x - CHUNK_SIZE / 2,
  795. pos.y + location.y - CHUNK_SIZE / 2,
  796. pos.z),
  797. zReader,
  798. dimensionId));
  799. }
  800. else
  801. {
  802. putBlockTypeAt(pos, blockType);
  803. }
  804. }
  805. }
  806. initializeLightning();
  807. }
  808. void Chunk::save(Framework::StreamWriter* zWriter)
  809. {
  810. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  811. {
  812. unsigned short blockType
  813. = blocks[index]
  814. ? (unsigned short)blocks[index]->zBlockType()->getId()
  815. : blockIds[index];
  816. zWriter->schreibe((char*)&blockType, 2);
  817. if (blockType)
  818. {
  819. if (blocks[index])
  820. {
  821. bool d = 1;
  822. zWriter->schreibe((char*)&d, 1);
  823. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  824. ->saveBlock(blocks[index], zWriter);
  825. }
  826. else
  827. {
  828. bool d = 0;
  829. zWriter->schreibe((char*)&d, 1);
  830. }
  831. }
  832. }
  833. }
  834. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  835. {
  836. for (int x = 0; x < CHUNK_SIZE; x++)
  837. {
  838. for (int y = 0; y < CHUNK_SIZE; y++)
  839. {
  840. for (int z = 0; z < WORLD_HEIGHT; z++)
  841. {
  842. int index = Chunk::index({x, y, z});
  843. if (isVisible(index)
  844. && StaticRegistry<BlockType>::INSTANCE
  845. .zElement(blockIds[index])
  846. ->doesNeedClientInstance())
  847. {
  848. unsigned short blockType = blocks[index]
  849. ? (unsigned short)blocks[index]
  850. ->zBlockType()
  851. ->getId()
  852. : blockIds[index];
  853. zWriter->schreibe((char*)&blockType, 2);
  854. zWriter->schreibe((char*)&index, 4);
  855. if (StaticRegistry<BlockType>::INSTANCE
  856. .zElement(blockIds[index])
  857. ->isFluid())
  858. {
  859. FluidBlock* fluidBlock
  860. = dynamic_cast<FluidBlock*>(blocks[index]);
  861. char data = fluidBlock->getFlowOptions();
  862. zWriter->schreibe(&data, 1);
  863. data = fluidBlock->getDistanceToSource();
  864. zWriter->schreibe(&data, 1);
  865. }
  866. }
  867. }
  868. }
  869. }
  870. unsigned short end = 0;
  871. zWriter->schreibe((char*)&end, 2);
  872. }
  873. void Chunk::removeUnusedBlocks()
  874. {
  875. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  876. {
  877. if (!blocks[i] && blockIds[i])
  878. {
  879. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  880. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  881. int z = i % WORLD_HEIGHT;
  882. bool visible = 0;
  883. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  884. || CONST_BLOCK(0, blockIds[i])->isPassable())
  885. visible = 1;
  886. else
  887. {
  888. for (int d = 0; d < 6 && !visible; d++)
  889. {
  890. auto n = zBlockNeighbor(
  891. getDirection((Directions)getDirectionFromIndex(d))
  892. + Framework::Vec3<int>(x, y, z));
  893. if (n.isA()
  894. && (((Block*)n)->isPassable()
  895. || ((Block*)n)->isTransparent()))
  896. visible = 1;
  897. if (n.isB()
  898. && (CONST_BLOCK(0, n)->isTransparent()
  899. || CONST_BLOCK(0, n)->isPassable()))
  900. visible = 1;
  901. }
  902. }
  903. if (!visible)
  904. {
  905. putBlockAt({x, y, z}, 0);
  906. putBlockTypeAt({x, y, z}, 0);
  907. }
  908. }
  909. }
  910. int count = 0;
  911. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  912. {
  913. if (StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[i])
  914. ->doesNeedClientInstance())
  915. count++;
  916. }
  917. std::cout << "chunk " << location.x << ", " << location.y
  918. << " was generated with " << count << " blocks.\n";
  919. }
  920. int Chunk::getDimensionId() const
  921. {
  922. return dimensionId;
  923. }
  924. void Chunk::onLoaded()
  925. {
  926. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  927. {
  928. if (blocks[i]) blocks[i]->onLoaded();
  929. }
  930. currentlyLoading = 0;
  931. }
  932. void Chunk::onUnloaded()
  933. {
  934. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  935. {
  936. if (blocks[i]) blocks[i]->onUnloaded();
  937. }
  938. }
  939. Framework::Punkt Chunk::getCenter() const
  940. {
  941. return location;
  942. }
  943. Framework::Vec3<int> Chunk::getMin() const
  944. {
  945. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  946. }
  947. Framework::Vec3<int> Chunk::getMax() const
  948. {
  949. return {
  950. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  951. }
  952. void Chunk::prepareRemove()
  953. {
  954. added = 0;
  955. cs.lock();
  956. for (int i = 0; i < 4; i++)
  957. {
  958. if (zNeighbours[i])
  959. {
  960. zNeighbours[i]->setNeighbor(
  961. getOppositeDirection(getDirectionFromIndex(i)), 0);
  962. zNeighbours[i] = 0;
  963. }
  964. }
  965. cs.unlock();
  966. }
  967. void Chunk::setAdded()
  968. {
  969. added = 1;
  970. }
  971. bool Chunk::hasObservers() const
  972. {
  973. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  974. }
  975. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  976. {
  977. int index = Chunk::index(location) * 6;
  978. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  979. return lightData + index;
  980. }
  981. void Chunk::setLightData(
  982. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  983. {
  984. int index = Chunk::index(location);
  985. memcpy(lightData + index * 6, data, 6);
  986. // check if neighbor is a visible block and send update to clients
  987. bool needSend = 0;
  988. for (int i = 0; i < 6; i++)
  989. {
  990. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  991. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  992. {
  993. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  994. && pos.y < CHUNK_SIZE)
  995. {
  996. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  997. int type = blockIds[bi];
  998. needSend |= StaticRegistry<BlockType>::INSTANCE.zElement(type)
  999. ->doesNeedClientInstance();
  1000. if (needSend) break;
  1001. }
  1002. else
  1003. {
  1004. int type = Game::INSTANCE->getBlockType(
  1005. pos
  1006. + Vec3<int>(this->location.x - CHUNK_SIZE / 2,
  1007. this->location.y - CHUNK_SIZE / 2,
  1008. 0),
  1009. dimensionId);
  1010. needSend |= StaticRegistry<BlockType>::INSTANCE.zElement(type)
  1011. ->doesNeedClientInstance();
  1012. if (needSend) break;
  1013. }
  1014. }
  1015. }
  1016. if (needSend)
  1017. {
  1018. broadcastLightData(index, foreground);
  1019. }
  1020. }
  1021. int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
  1022. {
  1023. return blockIds[index(location)];
  1024. }