Chunk.cpp 34 KB

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