Chunk.cpp 36 KB

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