Chunk.cpp 22 KB

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