Chunk.cpp 38 KB

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