Chunk.cpp 34 KB

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