Chunk.cpp 33 KB

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