Chunk.cpp 31 KB

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