Chunk.cpp 36 KB

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