Chunk.cpp 30 KB

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