Chunk.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. cs.lock();
  653. for (int i = 0; i < 6; i++)
  654. {
  655. Direction d = getDirectionFromIndex(i);
  656. Framework::Vec3<int> loc = location + getDirection(d);
  657. if (loc.x >= 0 && loc.x < CHUNK_SIZE && loc.y >= 0 && loc.y < CHUNK_SIZE
  658. && loc.z >= 0 && loc.z < WORLD_HEIGHT)
  659. {
  660. broadcastLightData(Chunk::index(loc), true);
  661. }
  662. else if (loc.z >= 0 && loc.z < WORLD_HEIGHT && i < 4 && zNeighbours[i])
  663. {
  664. NetworkMessage* msg = new NetworkMessage();
  665. msg->addressDimension(Game::INSTANCE->zDimension(dimensionId));
  666. char* message = new char[19];
  667. message[0] = 5;
  668. *(int*)(message + 1) = loc.x + this->location.x - CHUNK_SIZE / 2;
  669. *(int*)(message + 5) = loc.y + this->location.y - CHUNK_SIZE / 2;
  670. *(int*)(message + 9) = loc.z;
  671. loc -= getDirection(d) * CHUNK_SIZE;
  672. memcpy(message + 13, zNeighbours[i]->getLightData(loc), 6);
  673. msg->setMessage(message, 19);
  674. notifyObservers(msg);
  675. }
  676. }
  677. cs.unlock();
  678. }
  679. void Chunk::setNeighbor(Direction dir, Chunk* zChunk)
  680. {
  681. cs.lock();
  682. int dirIndex = getDirectionIndex(dir);
  683. Chunk* old = zNeighbours[dirIndex];
  684. zNeighbours[dirIndex] = zChunk;
  685. for (int i = 0; i < CHUNK_SIZE; i++)
  686. {
  687. for (int z = 0; z < WORLD_HEIGHT; z++)
  688. {
  689. int index = 0;
  690. int j = 0;
  691. if (dir == NORTH)
  692. {
  693. index = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  694. j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  695. }
  696. else if (dir == EAST)
  697. {
  698. index = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  699. j = i * WORLD_HEIGHT + z;
  700. }
  701. else if (dir == SOUTH)
  702. {
  703. index = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  704. j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  705. }
  706. else if (dir == WEST)
  707. {
  708. index = i * WORLD_HEIGHT + z;
  709. j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  710. }
  711. bool needsTransmission = 0;
  712. if (blocks[index])
  713. {
  714. bool visible = blocks[index]->isVisible();
  715. if (zChunk && zChunk->blocks[j])
  716. blocks[index]->setNeighbour(dir, zChunk->blocks[j]);
  717. else
  718. {
  719. blocks[index]->setNeighbour(dir, 0);
  720. blocks[index]->setNeighbourType(
  721. dir, zChunk ? zChunk->blockIds[j] : 0);
  722. }
  723. if (!visible && blocks[index]->isVisible())
  724. {
  725. needsTransmission = 1;
  726. }
  727. }
  728. else
  729. {
  730. zNeighbours[dirIndex] = old;
  731. bool visible = isVisible(index);
  732. zNeighbours[dirIndex] = zChunk;
  733. if (!visible && isVisible(index))
  734. {
  735. needsTransmission = 1;
  736. }
  737. }
  738. if (zChunk)
  739. {
  740. if (!blocks[index])
  741. {
  742. if (zChunk->zBlockConst(j)->isTransparent()
  743. && !blockIds[index])
  744. {
  745. generateBlock(
  746. Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  747. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  748. index % WORLD_HEIGHT));
  749. }
  750. }
  751. }
  752. if (needsTransmission && added)
  753. {
  754. sendBlockInfo(Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  755. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  756. index % WORLD_HEIGHT));
  757. }
  758. }
  759. }
  760. cs.unlock();
  761. }
  762. void Chunk::load(Framework::StreamReader* zReader)
  763. {
  764. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  765. {
  766. unsigned short blockType;
  767. zReader->lese((char*)&blockType, 2);
  768. if (blockType)
  769. {
  770. Framework::Vec3<int> pos
  771. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  772. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  773. index % WORLD_HEIGHT);
  774. bool d;
  775. zReader->lese((char*)&d, 1);
  776. if (d)
  777. {
  778. putBlockAt(pos,
  779. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  780. ->loadBlock(Framework::Vec3<int>(
  781. pos.x + location.x - CHUNK_SIZE / 2,
  782. pos.y + location.y - CHUNK_SIZE / 2,
  783. pos.z),
  784. zReader,
  785. dimensionId));
  786. }
  787. else
  788. {
  789. putBlockTypeAt(pos, blockType);
  790. }
  791. }
  792. }
  793. initializeLightning();
  794. }
  795. void Chunk::save(Framework::StreamWriter* zWriter)
  796. {
  797. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  798. {
  799. unsigned short blockType
  800. = blocks[index]
  801. ? (unsigned short)blocks[index]->zBlockType()->getId()
  802. : blockIds[index];
  803. zWriter->schreibe((char*)&blockType, 2);
  804. if (blockType)
  805. {
  806. if (blocks[index])
  807. {
  808. bool d = 1;
  809. zWriter->schreibe((char*)&d, 1);
  810. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  811. ->saveBlock(blocks[index], zWriter);
  812. }
  813. else
  814. {
  815. bool d = 0;
  816. zWriter->schreibe((char*)&d, 1);
  817. }
  818. }
  819. }
  820. }
  821. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  822. {
  823. for (int x = 0; x < CHUNK_SIZE; x++)
  824. {
  825. for (int y = 0; y < CHUNK_SIZE; y++)
  826. {
  827. for (int z = 0; z < WORLD_HEIGHT; z++)
  828. {
  829. int index = Chunk::index({x, y, z});
  830. if (isVisible(index)
  831. && StaticRegistry<BlockType>::INSTANCE
  832. .zElement(blockIds[index])
  833. ->doesNeedClientInstance())
  834. {
  835. unsigned short blockType = blocks[index]
  836. ? (unsigned short)blocks[index]
  837. ->zBlockType()
  838. ->getId()
  839. : blockIds[index];
  840. zWriter->schreibe((char*)&blockType, 2);
  841. zWriter->schreibe((char*)&index, 4);
  842. if (StaticRegistry<BlockType>::INSTANCE
  843. .zElement(blockIds[index])
  844. ->isFluid())
  845. {
  846. short amount = 1000;
  847. if (blocks[index])
  848. {
  849. amount = dynamic_cast<FluidBlock*>(blocks[index])
  850. ->getFluidAmount();
  851. }
  852. zWriter->schreibe((char*)&amount, 2);
  853. }
  854. }
  855. }
  856. }
  857. }
  858. unsigned short end = 0;
  859. zWriter->schreibe((char*)&end, 2);
  860. }
  861. void Chunk::removeUnusedBlocks()
  862. {
  863. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  864. {
  865. if (!blocks[i] && blockIds[i])
  866. {
  867. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  868. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  869. int z = i % WORLD_HEIGHT;
  870. bool visible = 0;
  871. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  872. || CONST_BLOCK(0, blockIds[i])->isPassable())
  873. visible = 1;
  874. else
  875. {
  876. for (int d = 0; d < 6 && !visible; d++)
  877. {
  878. auto n = zBlockNeighbor(
  879. getDirection((Directions)getDirectionFromIndex(d))
  880. + Framework::Vec3<int>(x, y, z));
  881. if (n.isA()
  882. && (((Block*)n)->isPassable()
  883. || ((Block*)n)->isTransparent()))
  884. visible = 1;
  885. if (n.isB()
  886. && (CONST_BLOCK(0, n)->isTransparent()
  887. || CONST_BLOCK(0, n)->isPassable()))
  888. visible = 1;
  889. }
  890. }
  891. if (!visible)
  892. {
  893. putBlockAt({x, y, z}, 0);
  894. putBlockTypeAt({x, y, z}, 0);
  895. }
  896. }
  897. }
  898. int count = 0;
  899. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  900. {
  901. if (StaticRegistry<BlockType>::INSTANCE.zElement(blockIds[i])
  902. ->doesNeedClientInstance())
  903. count++;
  904. }
  905. std::cout << "chunk " << location.x << ", " << location.y
  906. << " was generated with " << count << " blocks.\n";
  907. }
  908. int Chunk::getDimensionId() const
  909. {
  910. return dimensionId;
  911. }
  912. void Chunk::onLoaded()
  913. {
  914. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  915. {
  916. if (blocks[i]) blocks[i]->onLoaded();
  917. }
  918. currentlyLoading = 0;
  919. }
  920. void Chunk::onUnloaded()
  921. {
  922. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  923. {
  924. if (blocks[i]) blocks[i]->onUnloaded();
  925. }
  926. }
  927. Framework::Punkt Chunk::getCenter() const
  928. {
  929. return location;
  930. }
  931. Framework::Vec3<int> Chunk::getMin() const
  932. {
  933. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  934. }
  935. Framework::Vec3<int> Chunk::getMax() const
  936. {
  937. return {
  938. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  939. }
  940. void Chunk::prepareRemove()
  941. {
  942. added = 0;
  943. cs.lock();
  944. for (int i = 0; i < 4; i++)
  945. {
  946. if (zNeighbours[i])
  947. {
  948. zNeighbours[i]->setNeighbor(
  949. getOppositeDirection(getDirectionFromIndex(i)), 0);
  950. zNeighbours[i] = 0;
  951. }
  952. }
  953. cs.unlock();
  954. }
  955. void Chunk::setAdded()
  956. {
  957. added = 1;
  958. }
  959. bool Chunk::hasObservers() const
  960. {
  961. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  962. }
  963. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  964. {
  965. int index = Chunk::index(location) * 6;
  966. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * 6);
  967. return lightData + index;
  968. }
  969. void Chunk::setLightData(
  970. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  971. {
  972. int index = Chunk::index(location);
  973. memcpy(lightData + index * 6, data, 6);
  974. // check if neighbor is a visible block and send update to clients
  975. bool needSend = 0;
  976. for (int i = 0; i < 6; i++)
  977. {
  978. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  979. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  980. {
  981. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  982. && pos.y < CHUNK_SIZE)
  983. {
  984. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  985. int type = blockIds[bi];
  986. needSend |= StaticRegistry<BlockType>::INSTANCE.zElement(type)
  987. ->doesNeedClientInstance();
  988. if (needSend) break;
  989. }
  990. else
  991. {
  992. int type = Game::INSTANCE->getBlockType(
  993. pos
  994. + Vec3<int>(this->location.x - CHUNK_SIZE / 2,
  995. this->location.y - CHUNK_SIZE / 2,
  996. 0),
  997. dimensionId);
  998. needSend |= StaticRegistry<BlockType>::INSTANCE.zElement(type)
  999. ->doesNeedClientInstance();
  1000. if (needSend) break;
  1001. }
  1002. }
  1003. }
  1004. if (needSend)
  1005. {
  1006. broadcastLightData(index, foreground);
  1007. }
  1008. }
  1009. int Chunk::getBlockTypeAt(Framework::Vec3<int> location) const
  1010. {
  1011. return blockIds[index(location)];
  1012. }