Chunk.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. int j = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  556. if (blocks[index])
  557. {
  558. if (zChunk && zChunk->blocks[j])
  559. blocks[index]->setNeighbour(NORTH, zChunk->blocks[j]);
  560. else
  561. {
  562. blocks[index]->setNeighbour(NORTH, 0);
  563. blocks[index]->setNeighbourType(
  564. NORTH, zChunk ? zChunk->blockIds[j] : 0);
  565. }
  566. }
  567. else if (zChunk)
  568. {
  569. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  570. && !blockIds[index])
  571. {
  572. generateBlock(Vec3<int>(i, 0, z));
  573. }
  574. }
  575. }
  576. else if (dir == EAST)
  577. {
  578. int index
  579. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  580. int j = i * WORLD_HEIGHT + z;
  581. if (blocks[index])
  582. {
  583. if (zChunk && zChunk->blocks[j])
  584. blocks[index]->setNeighbour(EAST, zChunk->blocks[j]);
  585. else
  586. {
  587. blocks[index]->setNeighbour(EAST, 0);
  588. blocks[index]->setNeighbourType(
  589. EAST, zChunk ? zChunk->blockIds[j] : 0);
  590. }
  591. }
  592. else if (zChunk)
  593. {
  594. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  595. && !blockIds[index])
  596. {
  597. generateBlock(Vec3<int>(CHUNK_SIZE - 1, i, z));
  598. }
  599. }
  600. }
  601. else if (dir == SOUTH)
  602. {
  603. int index
  604. = (i * CHUNK_SIZE + CHUNK_SIZE - 1) * WORLD_HEIGHT + z;
  605. int j = i * CHUNK_SIZE * WORLD_HEIGHT + z;
  606. if (blocks[index])
  607. {
  608. if (zChunk && zChunk->blocks[j])
  609. blocks[index]->setNeighbour(SOUTH, zChunk->blocks[j]);
  610. else
  611. {
  612. blocks[index]->setNeighbour(SOUTH, 0);
  613. blocks[index]->setNeighbourType(
  614. SOUTH, zChunk ? zChunk->blockIds[j] : 0);
  615. }
  616. }
  617. else if (zChunk)
  618. {
  619. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  620. && !blockIds[index])
  621. {
  622. generateBlock(Vec3<int>(i, CHUNK_SIZE - 1, z));
  623. }
  624. }
  625. }
  626. else if (dir == WEST)
  627. {
  628. int index = i * WORLD_HEIGHT + z;
  629. int j = ((CHUNK_SIZE - 1) * CHUNK_SIZE + i) * WORLD_HEIGHT + z;
  630. if (blocks[index])
  631. {
  632. if (zChunk && zChunk->blocks[j])
  633. blocks[index]->setNeighbour(WEST, zChunk->blocks[j]);
  634. else
  635. {
  636. blocks[index]->setNeighbour(WEST, 0);
  637. blocks[index]->setNeighbourType(
  638. WEST, zChunk ? zChunk->blockIds[j] : 0);
  639. }
  640. }
  641. else if (zChunk)
  642. {
  643. if (zChunk->blockIds[j] == BlockTypeEnum::AIR
  644. && !blockIds[index])
  645. {
  646. generateBlock(Vec3<int>(0, i, z));
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. void Chunk::load(Framework::StreamReader* zReader)
  654. {
  655. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  656. {
  657. unsigned short blockType;
  658. zReader->lese((char*)&blockType, 2);
  659. if (blockType)
  660. {
  661. Framework::Vec3<int> pos
  662. = Framework::Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  663. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  664. index % WORLD_HEIGHT);
  665. bool d;
  666. zReader->lese((char*)&d, 1);
  667. if (d)
  668. {
  669. putBlockAt(pos,
  670. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  671. ->loadBlock(Framework::Vec3<int>(
  672. pos.x + location.x - CHUNK_SIZE / 2,
  673. pos.y + location.y - CHUNK_SIZE / 2,
  674. pos.z),
  675. zReader,
  676. dimensionId));
  677. }
  678. else
  679. {
  680. putBlockTypeAt(pos, blockType);
  681. }
  682. }
  683. }
  684. initializeLightning();
  685. }
  686. void Chunk::save(Framework::StreamWriter* zWriter)
  687. {
  688. for (int index = 0; index < WORLD_HEIGHT * CHUNK_SIZE * CHUNK_SIZE; index++)
  689. {
  690. unsigned short blockType
  691. = blocks[index]
  692. ? (unsigned short)blocks[index]->zBlockType()->getId()
  693. : blockIds[index];
  694. zWriter->schreibe((char*)&blockType, 2);
  695. if (blockType)
  696. {
  697. if (blocks[index])
  698. {
  699. bool d = 1;
  700. zWriter->schreibe((char*)&d, 1);
  701. StaticRegistry<BlockType>::INSTANCE.zElement(blockType)
  702. ->saveBlock(blocks[index], zWriter);
  703. }
  704. else
  705. {
  706. bool d = 0;
  707. zWriter->schreibe((char*)&d, 1);
  708. }
  709. }
  710. }
  711. }
  712. void Chunk::sendToClient(Framework::StreamWriter* zWriter)
  713. {
  714. for (int x = 0; x < CHUNK_SIZE; x++)
  715. {
  716. for (int y = 0; y < CHUNK_SIZE; y++)
  717. {
  718. for (int z = 0; z < WORLD_HEIGHT; z++)
  719. {
  720. int index = (x * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  721. unsigned short blockType
  722. = blocks[index]
  723. ? (unsigned short)blocks[index]->zBlockType()->getId()
  724. : blockIds[index];
  725. if (blockType)
  726. {
  727. bool visible = 0;
  728. if (!visible)
  729. {
  730. if (!blocks[index])
  731. {
  732. if (CONST_BLOCK(0, blockIds[index])->isTransparent()
  733. || CONST_BLOCK(0, blockIds[index])
  734. ->isPassable())
  735. visible = 1;
  736. else
  737. {
  738. for (int d = 0; d < 6 && !visible; d++)
  739. {
  740. auto n = zBlockNeighbor(
  741. getDirection((
  742. Directions)getDirectionFromIndex(d))
  743. + Framework::Vec3<int>(x, y, z));
  744. if (n.isA()
  745. && (((Block*)n)->isPassable()
  746. || ((Block*)n)->isTransparent()))
  747. visible = 1;
  748. if (n.isB()
  749. && (CONST_BLOCK(0, n)->isTransparent()
  750. || CONST_BLOCK(0, n)->isPassable()))
  751. visible = 1;
  752. }
  753. }
  754. }
  755. else
  756. visible = blocks[index]->isVisible();
  757. }
  758. if (visible
  759. && (blocks[index] || blockType != BlockTypeEnum::AIR))
  760. {
  761. zWriter->schreibe((char*)&blockType, 2);
  762. zWriter->schreibe((char*)&index, 4);
  763. }
  764. }
  765. }
  766. }
  767. }
  768. unsigned short end = 0;
  769. zWriter->schreibe((char*)&end, 2);
  770. }
  771. void Chunk::removeUnusedBlocks()
  772. {
  773. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  774. {
  775. if (!blocks[i] && blockIds[i])
  776. {
  777. int x = (i / WORLD_HEIGHT) / CHUNK_SIZE;
  778. int y = (i / WORLD_HEIGHT) % CHUNK_SIZE;
  779. int z = i % WORLD_HEIGHT;
  780. bool visible = 0;
  781. if (CONST_BLOCK(0, blockIds[i])->isTransparent()
  782. || CONST_BLOCK(0, blockIds[i])->isPassable())
  783. visible = 1;
  784. else
  785. {
  786. for (int d = 0; d < 6 && !visible; d++)
  787. {
  788. auto n = zBlockNeighbor(
  789. getDirection((Directions)getDirectionFromIndex(d))
  790. + Framework::Vec3<int>(x, y, z));
  791. if (n.isA()
  792. && (((Block*)n)->isPassable()
  793. || ((Block*)n)->isTransparent()))
  794. visible = 1;
  795. if (n.isB()
  796. && (CONST_BLOCK(0, n)->isTransparent()
  797. || CONST_BLOCK(0, n)->isPassable()))
  798. visible = 1;
  799. }
  800. }
  801. if (!visible)
  802. {
  803. putBlockAt({x, y, z}, 0);
  804. putBlockTypeAt({x, y, z}, BlockTypeEnum::NO_BLOCK);
  805. }
  806. }
  807. }
  808. int count = 0;
  809. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  810. {
  811. if (blockIds[i] && blockIds[i] != BlockTypeEnum::AIR) count++;
  812. }
  813. std::cout << "chunk " << location.x << ", " << location.y
  814. << " was generated with " << count << " blocks.\n";
  815. }
  816. int Chunk::getDimensionId() const
  817. {
  818. return dimensionId;
  819. }
  820. void Chunk::onLoaded()
  821. {
  822. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  823. {
  824. if (blocks[i]) blocks[i]->onLoaded();
  825. }
  826. currentlyLoading = 0;
  827. }
  828. void Chunk::onUnloaded()
  829. {
  830. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  831. {
  832. if (blocks[i]) blocks[i]->onUnloaded();
  833. }
  834. }
  835. Framework::Punkt Chunk::getCenter() const
  836. {
  837. return location;
  838. }
  839. Framework::Vec3<int> Chunk::getMin() const
  840. {
  841. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  842. }
  843. Framework::Vec3<int> Chunk::getMax() const
  844. {
  845. return {
  846. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  847. }
  848. void Chunk::prepareRemove()
  849. {
  850. added = 0;
  851. for (int i = 0; i < 4; i++)
  852. {
  853. if (zNeighbours[i])
  854. {
  855. zNeighbours[i]->setNeighbor(
  856. getOppositeDirection(getDirectionFromIndex(i)), 0);
  857. zNeighbours[i] = 0;
  858. }
  859. }
  860. }
  861. void Chunk::setAdded()
  862. {
  863. added = 1;
  864. }
  865. bool Chunk::hasObservers() const
  866. {
  867. return observers.getEintragAnzahl() > 0 || currentlyLoading;
  868. }
  869. unsigned char* Chunk::getLightData(Framework::Vec3<int> location) const
  870. {
  871. int index
  872. = ((location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z)
  873. * 6;
  874. assert(index < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT);
  875. return lightData + index;
  876. }
  877. void Chunk::setLightData(
  878. Framework::Vec3<int> location, unsigned char* data, bool foreground)
  879. {
  880. int index
  881. = ((location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z)
  882. * 6;
  883. memcpy(lightData + index, data, 6);
  884. // check if neighbor is a visible block and send update to clients
  885. bool needSend = 0;
  886. for (int i = 0; i < 6; i++)
  887. {
  888. Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
  889. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  890. {
  891. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  892. && pos.y < CHUNK_SIZE)
  893. {
  894. int bi = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  895. int type = blockIds[bi];
  896. needSend |= type != BlockTypeEnum::NO_BLOCK
  897. && type != BlockTypeEnum::AIR;
  898. if (needSend) break;
  899. }
  900. else
  901. {
  902. needSend = 1; // TODO: check if the block is visible
  903. }
  904. }
  905. }
  906. if (needSend)
  907. {
  908. NetworkMessage* msg = new NetworkMessage();
  909. msg->addressChunck(this);
  910. char* message = new char[11];
  911. message[0] = 1;
  912. *(int*)(message + 1) = index / 6;
  913. memcpy(message + 5, data, 6);
  914. msg->setMessage(message, 11);
  915. if (!foreground) msg->setUseBackground();
  916. notifyObservers(msg);
  917. }
  918. }