Chunk.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #include "Chunk.h"
  2. #include <Shader.h>
  3. #include "ChunkFluidModel.h"
  4. #include "ChunkGroundModel.h"
  5. #include "Constants.h"
  6. #include "CustomDX11API.h"
  7. #include "FactoryCraftModel.h"
  8. #include "Globals.h"
  9. #include "TransparentChunkGroundModel.h"
  10. Chunk::Chunk(Framework::Point location)
  11. : ReferenceCounter(),
  12. location(location),
  13. isLoading(0),
  14. lightChanged(0),
  15. modelChanged(0),
  16. bLock(),
  17. vLock(),
  18. aLock()
  19. {
  20. blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  21. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  22. FactoryCraftModel* ground = new FactoryCraftModel();
  23. Model3DData* chunkModel
  24. = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  25. Text("chunk_ground_") + location.x + location.y);
  26. if (!chunkModel)
  27. {
  28. chunkModel
  29. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  30. Text("chunk_ground_") + location.x + location.y);
  31. }
  32. chunkModel->setAmbientFactor(0.f);
  33. chunkModel->setDiffusFactor(1.f);
  34. chunkModel->setSpecularFactor(0.f);
  35. chunkModel->setVertecies(0, 0);
  36. ground->setModelData(chunkModel);
  37. ground->setPosition(
  38. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  39. ground->tick(0);
  40. ChunkModelBuilder* groundModel = new ChunkGroundModel(ground, this);
  41. modelBuilders.add(groundModel);
  42. FactoryCraftModel* transparentGround = new FactoryCraftModel();
  43. chunkModel = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  44. Text("transparent_chunk_ground_") + location.x + location.y);
  45. if (!chunkModel)
  46. {
  47. chunkModel
  48. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  49. Text("transparent_chunk_ground_") + location.x + location.y);
  50. }
  51. chunkModel->setAmbientFactor(0.f);
  52. chunkModel->setDiffusFactor(1.f);
  53. chunkModel->setSpecularFactor(0.f);
  54. chunkModel->setVertecies(0, 0);
  55. transparentGround->setModelData(chunkModel);
  56. transparentGround->setPosition(
  57. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  58. transparentGround->tick(0);
  59. ChunkModelBuilder* transparentGroundModel
  60. = new TransparentChunkGroundModel(transparentGround, this);
  61. modelBuilders.add(transparentGroundModel);
  62. FactoryCraftModel* fluids = new FactoryCraftModel();
  63. chunkModel = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  64. Text("chunk_fluids_") + location.x + location.y);
  65. if (!chunkModel)
  66. {
  67. chunkModel
  68. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  69. Text("chunk_fluids_") + location.x + location.y);
  70. }
  71. chunkModel->setAmbientFactor(0.f);
  72. chunkModel->setDiffusFactor(1.f);
  73. chunkModel->setSpecularFactor(0.f);
  74. chunkModel->setVertecies(0, 0);
  75. fluids->setModelData(chunkModel);
  76. fluids->setPosition(
  77. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  78. fluids->tick(0);
  79. ChunkModelBuilder* fluidModel = new ChunkFluidModel(fluids, this);
  80. modelBuilders.add(fluidModel);
  81. }
  82. Chunk::Chunk(Framework::Point location, Framework::StreamReader* zReader)
  83. : Chunk(location)
  84. {
  85. load(zReader);
  86. for (ChunkModelBuilder* builder : modelBuilders)
  87. {
  88. buildModel(builder);
  89. }
  90. }
  91. Chunk::~Chunk()
  92. {
  93. char msg = 1; // remove observer
  94. if (World::INSTANCE)
  95. {
  96. World::INSTANCE->zClient()->chunkAPIRequest(location, &msg, 1);
  97. }
  98. bLock.lockWrite();
  99. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  100. {
  101. if (blocks[i])
  102. {
  103. blocks[i]->release();
  104. blocks[i] = 0;
  105. }
  106. }
  107. delete[] blocks;
  108. bLock.unlockWrite();
  109. }
  110. void Chunk::appendAnimation(
  111. Block* zB, int boneId, double time, Vec3<float> pos, Vec3<float> rot)
  112. {
  113. if (!zB->zSkeleton() || !zB->zSkeleton()->zBone(boneId)) return;
  114. aLock.lockRead();
  115. for (BlockAnimation* animation : animations)
  116. {
  117. if (animation->zBlock() == zB)
  118. {
  119. animation->appendAnimation(boneId, time, pos, rot);
  120. aLock.unlockRead();
  121. return;
  122. }
  123. }
  124. aLock.unlockRead();
  125. SkeletonAnimation* sa = new SkeletonAnimation();
  126. Bone* bone = zB->zSkeleton()->zBone(boneId);
  127. sa->addAnimation(boneId, bone->getPosition(), bone->getRotation());
  128. sa->addKeyFrame(boneId, time, pos, rot);
  129. aLock.lockWrite();
  130. animations.add(new BlockAnimation(dynamic_cast<Block*>(zB->getThis()), sa));
  131. aLock.unlockWrite();
  132. }
  133. void Chunk::load(Framework::StreamReader* zReader)
  134. {
  135. bLock.lockWrite();
  136. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  137. {
  138. if (blocks[i])
  139. {
  140. blocks[i]->release();
  141. blocks[i] = 0;
  142. }
  143. }
  144. bLock.unlockWrite();
  145. isLoading = 1;
  146. Framework::Vec3<int> pos = {0, 0, 0};
  147. unsigned short id;
  148. zReader->read((char*)&id, 2);
  149. int count = 0;
  150. while (id)
  151. {
  152. int index;
  153. zReader->read((char*)&index, 4);
  154. char state = 0;
  155. zReader->read(&state, 1);
  156. char flowOptions = 0;
  157. char distanceToSource = 0;
  158. if (state & 1)
  159. {
  160. zReader->read(&flowOptions, 1);
  161. zReader->read(&distanceToSource, 1);
  162. }
  163. bool passable = 0;
  164. float speedModifier = 1.f;
  165. if (state & 2)
  166. {
  167. passable = 1;
  168. zReader->read((char*)&speedModifier, 4);
  169. }
  170. pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  171. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  172. index % WORLD_HEIGHT);
  173. Direction dir = blockTypes[id]->getDefaultFrontDirection();
  174. if (state & 4)
  175. {
  176. zReader->read((char*)&dir, 1);
  177. }
  178. if (blockTypes[id]->doesNeedInstance())
  179. {
  180. Block* b = blockTypes[id]->createBlock(
  181. {pos.x + location.x - CHUNK_SIZE / 2,
  182. pos.y + location.y - CHUNK_SIZE / 2,
  183. pos.z},
  184. passable,
  185. speedModifier,
  186. dir);
  187. b->setFlow(flowOptions, distanceToSource);
  188. bLock.lockWrite();
  189. blocks[index] = b;
  190. bLock.unlockWrite();
  191. if (b->isVisible())
  192. {
  193. if (!blockTypes[id]->getModelInfo().getModelName().isEqual(
  194. "cube")
  195. && !blockTypes[id]->getModelInfo().getModelName().isEqual(
  196. "grass"))
  197. {
  198. b->tick(0);
  199. vLock.lockWrite();
  200. visibleBlocks.add(b);
  201. vLock.unlockWrite();
  202. }
  203. }
  204. count++;
  205. }
  206. zReader->read((char*)&id, 2);
  207. }
  208. std::cout << "Loaded " << count << " blocks\n";
  209. int index = 0;
  210. // light
  211. zReader->read((char*)&index, 4);
  212. char lightData[6];
  213. while (index >= -1)
  214. {
  215. if (index == -1)
  216. {
  217. int x = 0;
  218. int y = 0;
  219. int z = 0;
  220. zReader->read((char*)&x, 4);
  221. zReader->read((char*)&y, 4);
  222. zReader->read((char*)&z, 4);
  223. zReader->read(lightData, 6);
  224. if (x == -1)
  225. {
  226. int cacheIndex = y * WORLD_HEIGHT + z;
  227. bLock.lockRead();
  228. Block* zB = blocks[cacheIndex];
  229. if (zB)
  230. {
  231. zB->setLightData(WEST, (unsigned char*)lightData, 0);
  232. }
  233. bLock.unlockRead();
  234. }
  235. else if (y == -1)
  236. {
  237. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  238. bLock.lockRead();
  239. Block* zB = blocks[cacheIndex];
  240. if (zB)
  241. {
  242. zB->setLightData(NORTH, (unsigned char*)lightData, 0);
  243. }
  244. bLock.unlockRead();
  245. }
  246. else if (x == CHUNK_SIZE)
  247. {
  248. int cacheIndex
  249. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  250. bLock.lockRead();
  251. Block* zB = blocks[cacheIndex];
  252. if (zB)
  253. {
  254. zB->setLightData(EAST, (unsigned char*)lightData, 0);
  255. }
  256. bLock.unlockRead();
  257. }
  258. else if (y == CHUNK_SIZE)
  259. {
  260. int cacheIndex
  261. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  262. bLock.lockRead();
  263. Block* zB = blocks[cacheIndex];
  264. if (zB)
  265. {
  266. zB->setLightData(SOUTH, (unsigned char*)lightData, 0);
  267. }
  268. bLock.unlockRead();
  269. }
  270. }
  271. else
  272. {
  273. zReader->read(lightData, 6);
  274. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  275. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  276. index % WORLD_HEIGHT);
  277. for (int i = 0; i < 6; i++)
  278. {
  279. Framework::Vec3<int> pos
  280. = location + getDirection(getDirectionFromIndex(i));
  281. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  282. {
  283. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  284. && pos.y < CHUNK_SIZE)
  285. {
  286. int cacheIndex
  287. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  288. + pos.z;
  289. bLock.lockRead();
  290. Block* zB = blocks[cacheIndex];
  291. if (zB)
  292. {
  293. bool visible = zB->isVisible();
  294. zB->setLightData(
  295. getOppositeDirection(getDirectionFromIndex(i)),
  296. (unsigned char*)lightData,
  297. 0);
  298. if (zB->isVisible() && !visible)
  299. {
  300. zB->tick(0);
  301. vLock.lockWrite();
  302. visibleBlocks.add(zB);
  303. vLock.unlockWrite();
  304. }
  305. }
  306. bLock.unlockRead();
  307. }
  308. else
  309. {
  310. pos.x += this->location.x - CHUNK_SIZE / 2;
  311. pos.y += this->location.y - CHUNK_SIZE / 2;
  312. World::INSTANCE->getChunkReadLock().lock();
  313. Chunk* c = World::INSTANCE->zChunk(
  314. World::INSTANCE->getChunkCenter(pos.x, pos.y));
  315. if (c)
  316. {
  317. c->getBlockReadLock().lock();
  318. Block* zB = c->zBlockAt(pos);
  319. if (zB)
  320. {
  321. bool visible = zB->isVisible();
  322. zB->setLightData(getOppositeDirection(
  323. getDirectionFromIndex(i)),
  324. (unsigned char*)lightData,
  325. c);
  326. if (zB->isVisible() && !visible)
  327. {
  328. zB->tick(0);
  329. vLock.lockWrite();
  330. c->visibleBlocks.add(zB);
  331. vLock.unlockWrite();
  332. }
  333. }
  334. c->getBlockReadLock().unlock();
  335. }
  336. World::INSTANCE->getChunkReadLock().unlock();
  337. }
  338. }
  339. }
  340. }
  341. zReader->read((char*)&index, 4);
  342. }
  343. isLoading = 0;
  344. }
  345. void Chunk::buildModel(ChunkModelBuilder* builder)
  346. {
  347. modelChanged &= ~builder->getType();
  348. lightChanged &= ~builder->getType();
  349. builder->buildModel();
  350. }
  351. void Chunk::updateLight(ChunkModelBuilder* builder)
  352. {
  353. lightChanged &= ~builder->getType();
  354. builder->updateLightning();
  355. }
  356. void Chunk::renderSolid(std::function<void(Model3D*)> f)
  357. {
  358. CustomDX11API* api
  359. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  360. api->setCullBack(false);
  361. for (ChunkModelBuilder* builder : modelBuilders)
  362. {
  363. if (!builder->isTransparent())
  364. {
  365. f(builder->zModel());
  366. }
  367. }
  368. api->setCullBack(true);
  369. float dist = 0.f;
  370. vLock.lockRead();
  371. for (Block* b : visibleBlocks)
  372. {
  373. f(b);
  374. }
  375. vLock.unlockRead();
  376. }
  377. void Chunk::renderTransparent(std::function<void(Model3D*)> f)
  378. {
  379. CustomDX11API* api
  380. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  381. api->setCullBack(false);
  382. for (ChunkModelBuilder* builder : modelBuilders)
  383. {
  384. if (builder->isTransparent())
  385. {
  386. f(builder->zModel());
  387. }
  388. }
  389. api->setCullBack(true);
  390. }
  391. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  392. {
  393. for (ChunkModelBuilder* builder : modelBuilders)
  394. {
  395. if ((modelChanged | builder->getType()) == modelChanged)
  396. buildModel(builder);
  397. }
  398. for (ChunkModelBuilder* builder : modelBuilders)
  399. {
  400. if ((lightChanged | builder->getType()) == lightChanged)
  401. updateLight(builder);
  402. }
  403. bool res = 0;
  404. for (ChunkModelBuilder* builder : modelBuilders)
  405. {
  406. res |= builder->zModel()->tick(time);
  407. }
  408. aLock.lockRead();
  409. auto iterator = animations.begin();
  410. while (iterator)
  411. {
  412. if (iterator->tick(time))
  413. {
  414. res |= iterator->zBlock()->tick(time);
  415. if (iterator->isFinished())
  416. {
  417. aLock.lockWrite();
  418. iterator.remove();
  419. aLock.unlockWrite();
  420. continue;
  421. }
  422. }
  423. else
  424. {
  425. aLock.lockWrite();
  426. iterator.remove();
  427. aLock.unlockWrite();
  428. continue;
  429. }
  430. ++iterator;
  431. }
  432. aLock.unlockRead();
  433. return 1;
  434. }
  435. void Chunk::destroy()
  436. {
  437. for (ChunkModelBuilder* builder : modelBuilders)
  438. {
  439. Model3DData* chunkModel = builder->zModel()->zModelData();
  440. // remove old model
  441. while (chunkModel->getPolygonCount() > 0)
  442. {
  443. chunkModel->removePolygon(0);
  444. }
  445. chunkModel->setVertecies(0, 0);
  446. }
  447. }
  448. void Chunk::api(char* message)
  449. {
  450. switch (message[0])
  451. {
  452. case 0: // set block
  453. {
  454. unsigned short id = *(int*)(message + 1);
  455. int index = *(int*)(message + 3);
  456. char state = message[7];
  457. char flowOptions = 0;
  458. char distanceToSource = 0;
  459. int i = 8;
  460. if (state & 1)
  461. {
  462. flowOptions = message[i++];
  463. distanceToSource = message[i++];
  464. }
  465. bool passable = 0;
  466. float speedModifier = 1.f;
  467. if (state & 2)
  468. {
  469. passable = 1;
  470. speedModifier = *(float*)(message + i);
  471. i += 4;
  472. }
  473. Direction dir = blockTypes[id]->getDefaultFrontDirection();
  474. if (state & 4)
  475. {
  476. dir = (Direction)message[i++];
  477. }
  478. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  479. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  480. index % WORLD_HEIGHT);
  481. location.x += this->location.x - CHUNK_SIZE / 2;
  482. location.y += this->location.y - CHUNK_SIZE / 2;
  483. if (blockTypes[id]->doesNeedInstance())
  484. {
  485. Block* zB = blockTypes[id]->createBlock(
  486. location, passable, speedModifier, dir);
  487. zB->setFlow(flowOptions, distanceToSource);
  488. setBlock(zB);
  489. }
  490. else
  491. {
  492. bLock.lockRead();
  493. Block* zB = zBlockAt(location);
  494. if (zB) removeBlock(zB);
  495. bLock.unlockRead();
  496. }
  497. break;
  498. }
  499. case 1: // animate block
  500. {
  501. int index = *(int*)(message + 1);
  502. int boneId = *(int*)(message + 5);
  503. double time = *(double*)(message + 9);
  504. Framework::Vec3<float> pos;
  505. pos.x = *(float*)(message + 17);
  506. pos.y = *(float*)(message + 21);
  507. pos.z = *(float*)(message + 25);
  508. Framework::Vec3<float> rot;
  509. rot.x = *(float*)(message + 29);
  510. rot.y = *(float*)(message + 33);
  511. rot.z = *(float*)(message + 37);
  512. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  513. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  514. index % WORLD_HEIGHT);
  515. location.x += this->location.x - CHUNK_SIZE / 2;
  516. location.y += this->location.y - CHUNK_SIZE / 2;
  517. bLock.lockRead();
  518. Block* zB = zBlockAt(location);
  519. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  520. bLock.unlockRead();
  521. break;
  522. }
  523. }
  524. }
  525. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  526. {
  527. if (location.z < 0 || location.z >= WORLD_HEIGHT) return 0;
  528. location.x = location.x % CHUNK_SIZE;
  529. location.y = location.y % CHUNK_SIZE;
  530. if (location.x < 0) location.x += CHUNK_SIZE;
  531. if (location.y < 0) location.y += CHUNK_SIZE;
  532. int index
  533. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  534. return blocks[index];
  535. }
  536. void Chunk::setBlock(Block* block)
  537. {
  538. Framework::Vec3<int> pos = block->getLocation();
  539. pos.x = pos.x % CHUNK_SIZE;
  540. pos.y = pos.y % CHUNK_SIZE;
  541. if (pos.x < 0) pos.x += CHUNK_SIZE;
  542. if (pos.y < 0) pos.y += CHUNK_SIZE;
  543. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  544. int affectsGround = 0;
  545. int newAffectsGround = 0;
  546. bLock.lockWrite();
  547. for (ChunkModelBuilder* builder : modelBuilders)
  548. {
  549. if (block && builder->isPartOfModel(block))
  550. {
  551. newAffectsGround |= builder->getType();
  552. }
  553. if (blocks[index] && builder->isPartOfModel(blocks[index]))
  554. {
  555. affectsGround |= builder->getType();
  556. }
  557. }
  558. if (blocks[index])
  559. {
  560. vLock.lockWrite();
  561. for (Framework::ArrayIterator<Block*> vi = visibleBlocks.begin(); vi;
  562. vi++)
  563. {
  564. if (blocks[index] == (Block*)vi)
  565. {
  566. vi.remove();
  567. break;
  568. }
  569. }
  570. vLock.unlockWrite();
  571. blocks[index]->copyLightTo(block);
  572. blocks[index]->release();
  573. blocks[index] = block;
  574. bLock.unlockWrite();
  575. modelChanged |= affectsGround | newAffectsGround;
  576. if (block && block->isVisible() && !newAffectsGround)
  577. {
  578. block->tick(0);
  579. vLock.lockWrite();
  580. visibleBlocks.add(block);
  581. vLock.unlockWrite();
  582. }
  583. return;
  584. }
  585. blocks[index] = block;
  586. bLock.unlockWrite();
  587. modelChanged |= affectsGround | newAffectsGround;
  588. if (block && block->isVisible() && !newAffectsGround)
  589. {
  590. block->tick(0);
  591. vLock.lockWrite();
  592. visibleBlocks.add(block);
  593. vLock.unlockWrite();
  594. }
  595. }
  596. void Chunk::removeBlock(Block* zBlock)
  597. {
  598. vLock.lockWrite();
  599. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  600. iterator;
  601. iterator++)
  602. {
  603. if (zBlock == (Block*)iterator)
  604. {
  605. iterator.remove();
  606. break;
  607. }
  608. }
  609. vLock.unlockWrite();
  610. Vec3<int> pos = zBlock->getLocation();
  611. pos.x = pos.x % CHUNK_SIZE;
  612. pos.y = pos.y % CHUNK_SIZE;
  613. if (pos.x < 0) pos.x += CHUNK_SIZE;
  614. if (pos.y < 0) pos.y += CHUNK_SIZE;
  615. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  616. bLock.lockWrite();
  617. if (blocks[index])
  618. {
  619. modelChanged |= blocks[index]->getPartOfModels();
  620. blocks[index]->release();
  621. blocks[index] = 0;
  622. }
  623. bLock.unlockWrite();
  624. }
  625. void Chunk::blockVisibilityChanged(Block* zB)
  626. {
  627. if (zB->isVisible())
  628. {
  629. zB->tick(0);
  630. vLock.lockWrite();
  631. visibleBlocks.add(zB);
  632. vLock.unlockWrite();
  633. }
  634. else
  635. {
  636. vLock.lockWrite();
  637. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  638. iterator;
  639. iterator++)
  640. {
  641. if (zB == (Block*)iterator)
  642. {
  643. iterator.remove();
  644. break;
  645. }
  646. }
  647. vLock.unlockWrite();
  648. }
  649. }
  650. Framework::Point Chunk::getCenter() const
  651. {
  652. return location;
  653. }
  654. Framework::Vec3<int> Chunk::getMin() const
  655. {
  656. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  657. }
  658. Framework::Vec3<int> Chunk::getMax() const
  659. {
  660. return {
  661. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  662. }
  663. void Chunk::setModelChanged(int type)
  664. {
  665. modelChanged |= type;
  666. }
  667. void Chunk::setLightChanged(int type)
  668. {
  669. lightChanged |= type;
  670. }
  671. Lock& Chunk::getBlockReadLock() const
  672. {
  673. return bLock.getReadLock();
  674. }