Chunk.cpp 20 KB

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