Chunk.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. 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->setModelDaten(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->setModelDaten(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::Punkt 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. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  96. {
  97. if (blocks[i])
  98. {
  99. blocks[i]->release();
  100. blocks[i] = 0;
  101. }
  102. }
  103. delete[] blocks;
  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. char flowOptions = 0;
  148. char distanceToSource = 0;
  149. if (blockTypes[id]->isFluid())
  150. {
  151. zReader->lese(&flowOptions, 1);
  152. zReader->lese(&distanceToSource, 1);
  153. }
  154. pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  155. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  156. index % WORLD_HEIGHT);
  157. if (blockTypes[id]->doesNeedInstance())
  158. {
  159. cs.lock();
  160. Block* b = blockTypes[id]->createBlock(
  161. {pos.x + location.x - CHUNK_SIZE / 2,
  162. pos.y + location.y - CHUNK_SIZE / 2,
  163. pos.z});
  164. b->setFlow(flowOptions, distanceToSource);
  165. blocks[index] = b;
  166. cs.unlock();
  167. vcs.lock();
  168. if (b->isVisible())
  169. {
  170. if (!blockTypes[id]->getModelInfo().getModelName().istGleich(
  171. "cube")
  172. && !blockTypes[id]->getModelInfo().getModelName().istGleich(
  173. "grass"))
  174. {
  175. b->tick(0);
  176. visibleBlocks.add(b);
  177. }
  178. }
  179. count++;
  180. vcs.unlock();
  181. }
  182. zReader->lese((char*)&id, 2);
  183. }
  184. std::cout << "Loaded " << count << " blocks\n";
  185. int index = 0;
  186. // light
  187. zReader->lese((char*)&index, 4);
  188. char lightData[6];
  189. while (index >= -1)
  190. {
  191. if (index == -1)
  192. {
  193. int x = 0;
  194. int y = 0;
  195. int z = 0;
  196. zReader->lese((char*)&x, 4);
  197. zReader->lese((char*)&y, 4);
  198. zReader->lese((char*)&z, 4);
  199. zReader->lese(lightData, 6);
  200. if (x == -1)
  201. {
  202. int cacheIndex = y * WORLD_HEIGHT + z;
  203. Block* zB = blocks[cacheIndex];
  204. if (zB)
  205. {
  206. zB->setLightData(WEST, (unsigned char*)lightData);
  207. }
  208. }
  209. else if (y == -1)
  210. {
  211. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  212. Block* zB = blocks[cacheIndex];
  213. if (zB)
  214. {
  215. zB->setLightData(NORTH, (unsigned char*)lightData);
  216. }
  217. }
  218. else if (x == CHUNK_SIZE)
  219. {
  220. int cacheIndex
  221. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  222. Block* zB = blocks[cacheIndex];
  223. if (zB)
  224. {
  225. zB->setLightData(EAST, (unsigned char*)lightData);
  226. }
  227. }
  228. else if (y == CHUNK_SIZE)
  229. {
  230. int cacheIndex
  231. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  232. Block* zB = blocks[cacheIndex];
  233. if (zB)
  234. {
  235. zB->setLightData(SOUTH, (unsigned char*)lightData);
  236. }
  237. }
  238. }
  239. else
  240. {
  241. zReader->lese(lightData, 6);
  242. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  243. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  244. index % WORLD_HEIGHT);
  245. for (int i = 0; i < 6; i++)
  246. {
  247. Framework::Vec3<int> pos
  248. = location + getDirection(getDirectionFromIndex(i));
  249. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  250. {
  251. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  252. && pos.y < CHUNK_SIZE)
  253. {
  254. int cacheIndex
  255. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  256. + pos.z;
  257. Block* zB = blocks[cacheIndex];
  258. if (zB)
  259. {
  260. bool visible = zB->isVisible();
  261. zB->setLightData(
  262. getOppositeDirection(getDirectionFromIndex(i)),
  263. (unsigned char*)lightData);
  264. if (zB->isVisible() && !visible)
  265. {
  266. vcs.lock();
  267. zB->tick(0);
  268. visibleBlocks.add(zB);
  269. vcs.unlock();
  270. }
  271. }
  272. }
  273. else
  274. {
  275. pos.x += this->location.x - CHUNK_SIZE / 2;
  276. pos.y += this->location.y - CHUNK_SIZE / 2;
  277. Block* zB = World::INSTANCE->zBlockAt(pos);
  278. if (zB)
  279. {
  280. bool visible = zB->isVisible();
  281. zB->setLightData(
  282. getOppositeDirection(getDirectionFromIndex(i)),
  283. (unsigned char*)lightData);
  284. if (zB->isVisible() && !visible)
  285. {
  286. Chunk* c = World::INSTANCE->zChunk(
  287. World::INSTANCE->getChunkCenter(
  288. pos.x, pos.y));
  289. c->vcs.lock();
  290. zB->tick(0);
  291. c->visibleBlocks.add(zB);
  292. c->vcs.unlock();
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. zReader->lese((char*)&index, 4);
  300. }
  301. isLoading = 0;
  302. }
  303. void Chunk::buildModel(ChunkModelBuilder* builder)
  304. {
  305. vcs.lock();
  306. modelChanged &= ~builder->getType();
  307. lightChanged &= ~builder->getType();
  308. builder->buildModel();
  309. vcs.unlock();
  310. }
  311. void Chunk::updateLight(ChunkModelBuilder* builder)
  312. {
  313. vcs.lock();
  314. lightChanged &= ~builder->getType();
  315. builder->updateLightning();
  316. vcs.unlock();
  317. }
  318. void Chunk::renderSolid(std::function<void(Model3D*)> f)
  319. {
  320. vcs.lock();
  321. CustomDX11API* api
  322. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  323. api->setCullBack(false);
  324. for (ChunkModelBuilder* builder : modelBuilders)
  325. {
  326. if (!builder->isTransparent())
  327. {
  328. f(builder->zModel());
  329. }
  330. }
  331. api->setCullBack(true);
  332. float dist = 0.f;
  333. for (Block* b : visibleBlocks)
  334. {
  335. f(b);
  336. }
  337. vcs.unlock();
  338. }
  339. void Chunk::renderTransparent(std::function<void(Model3D*)> f)
  340. {
  341. CustomDX11API* api
  342. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  343. api->setCullBack(false);
  344. for (ChunkModelBuilder* builder : modelBuilders)
  345. {
  346. if (builder->isTransparent())
  347. {
  348. f(builder->zModel());
  349. }
  350. }
  351. api->setCullBack(true);
  352. }
  353. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  354. {
  355. acs.lock();
  356. vcs.lock(); // TODO: enshure no dead lock occures
  357. for (ChunkModelBuilder* builder : modelBuilders)
  358. {
  359. if ((modelChanged | builder->getType()) == modelChanged)
  360. buildModel(builder);
  361. }
  362. for (ChunkModelBuilder* builder : modelBuilders)
  363. {
  364. if ((lightChanged | builder->getType()) == modelChanged)
  365. updateLight(builder);
  366. }
  367. bool res = 0;
  368. for (ChunkModelBuilder* builder : modelBuilders)
  369. {
  370. res |= builder->zModel()->tick(time);
  371. }
  372. auto iterator = animations.begin();
  373. while (iterator)
  374. {
  375. if (iterator->tick(time))
  376. {
  377. res |= iterator->zBlock()->tick(time);
  378. if (iterator->isFinished())
  379. {
  380. iterator.remove();
  381. continue;
  382. }
  383. }
  384. else
  385. {
  386. iterator.remove();
  387. continue;
  388. }
  389. ++iterator;
  390. }
  391. vcs.unlock();
  392. acs.unlock();
  393. return 1;
  394. }
  395. void Chunk::destroy()
  396. {
  397. for (ChunkModelBuilder* builder : modelBuilders)
  398. {
  399. Model3DData* chunkModel = builder->zModel()->zModelData();
  400. // remove old model
  401. while (chunkModel->getPolygonAnzahl() > 0)
  402. {
  403. chunkModel->removePolygon(0);
  404. }
  405. chunkModel->setVertecies(0, 0);
  406. }
  407. }
  408. void Chunk::api(char* message)
  409. {
  410. switch (message[0])
  411. {
  412. case 0: // set block
  413. {
  414. int index = *(int*)(message + 1);
  415. int id = *(int*)(message + 5);
  416. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  417. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  418. index % WORLD_HEIGHT);
  419. location.x += this->location.x - CHUNK_SIZE / 2;
  420. location.y += this->location.y - CHUNK_SIZE / 2;
  421. if (blockTypes[id]->doesNeedInstance())
  422. {
  423. Block* zB = blockTypes[id]->createBlock(location);
  424. setBlock(zB);
  425. }
  426. else
  427. {
  428. Block* zB = zBlockAt(location);
  429. if (zB) removeBlock(zB);
  430. }
  431. break;
  432. }
  433. case 1: // animate block
  434. {
  435. int index = *(int*)(message + 1);
  436. int boneId = *(int*)(message + 5);
  437. double time = *(double*)(message + 9);
  438. Framework::Vec3<float> pos;
  439. pos.x = *(float*)(message + 17);
  440. pos.y = *(float*)(message + 21);
  441. pos.z = *(float*)(message + 25);
  442. Framework::Vec3<float> rot;
  443. rot.x = *(float*)(message + 29);
  444. rot.y = *(float*)(message + 33);
  445. rot.z = *(float*)(message + 37);
  446. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  447. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  448. index % WORLD_HEIGHT);
  449. location.x += this->location.x - CHUNK_SIZE / 2;
  450. location.y += this->location.y - CHUNK_SIZE / 2;
  451. Block* zB = zBlockAt(location);
  452. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  453. break;
  454. }
  455. }
  456. }
  457. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  458. {
  459. location.x = location.x % CHUNK_SIZE;
  460. location.y = location.y % CHUNK_SIZE;
  461. if (location.x < 0) location.x += CHUNK_SIZE;
  462. if (location.y < 0) location.y += CHUNK_SIZE;
  463. int index
  464. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  465. return blocks[index];
  466. }
  467. void Chunk::setBlock(Block* block)
  468. {
  469. cs.lock();
  470. Framework::Vec3<int> pos = block->getLocation();
  471. pos.x = pos.x % CHUNK_SIZE;
  472. pos.y = pos.y % CHUNK_SIZE;
  473. if (pos.x < 0) pos.x += CHUNK_SIZE;
  474. if (pos.y < 0) pos.y += CHUNK_SIZE;
  475. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  476. int affectsGround = 0;
  477. int newAffectsGround = 0;
  478. for (ChunkModelBuilder* builder : modelBuilders)
  479. {
  480. if (block && builder->isPartOfModel(block))
  481. {
  482. newAffectsGround |= builder->getType();
  483. }
  484. if (blocks[index] && builder->isPartOfModel(blocks[index]))
  485. {
  486. affectsGround |= builder->getType();
  487. }
  488. }
  489. if (blocks[index])
  490. {
  491. vcs.lock();
  492. for (Framework::Iterator<Block*> vi = visibleBlocks.begin(); vi; vi++)
  493. {
  494. if (blocks[index] == (Block*)vi)
  495. {
  496. vi.remove();
  497. break;
  498. }
  499. }
  500. vcs.unlock();
  501. blocks[index]->copyLightTo(block);
  502. blocks[index]->release();
  503. blocks[index] = block;
  504. cs.unlock();
  505. vcs.lock();
  506. modelChanged |= affectsGround | newAffectsGround;
  507. if (block && block->isVisible() && !newAffectsGround)
  508. {
  509. block->tick(0);
  510. visibleBlocks.add(block);
  511. }
  512. vcs.unlock();
  513. return;
  514. }
  515. blocks[index] = block;
  516. cs.unlock();
  517. vcs.lock();
  518. modelChanged |= affectsGround | newAffectsGround;
  519. if (block && block->isVisible() && !newAffectsGround)
  520. {
  521. block->tick(0);
  522. visibleBlocks.add(block);
  523. }
  524. vcs.unlock();
  525. }
  526. void Chunk::removeBlock(Block* zBlock)
  527. {
  528. cs.lock();
  529. vcs.lock();
  530. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator;
  531. iterator++)
  532. {
  533. if (zBlock == (Block*)iterator)
  534. {
  535. iterator.remove();
  536. break;
  537. }
  538. }
  539. vcs.unlock();
  540. Vec3<int> pos = zBlock->getLocation();
  541. pos.x = pos.x % CHUNK_SIZE;
  542. pos.y = pos.y % CHUNK_SIZE;
  543. if (pos.x < 0) pos.x += CHUNK_SIZE;
  544. if (pos.y < 0) pos.y += CHUNK_SIZE;
  545. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  546. if (blocks[index])
  547. {
  548. modelChanged |= blocks[index]->getPartOfModels();
  549. blocks[index]->release();
  550. blocks[index] = 0;
  551. }
  552. cs.unlock();
  553. }
  554. void Chunk::blockVisibilityChanged(Block* zB)
  555. {
  556. vcs.lock();
  557. if (zB->isVisible())
  558. {
  559. zB->tick(0);
  560. visibleBlocks.add(zB);
  561. }
  562. else
  563. {
  564. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin();
  565. iterator;
  566. iterator++)
  567. {
  568. if (zB == (Block*)iterator)
  569. {
  570. iterator.remove();
  571. break;
  572. }
  573. }
  574. }
  575. vcs.unlock();
  576. }
  577. Framework::Punkt Chunk::getCenter() const
  578. {
  579. return location;
  580. }
  581. Framework::Vec3<int> Chunk::getMin() const
  582. {
  583. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  584. }
  585. Framework::Vec3<int> Chunk::getMax() const
  586. {
  587. return {
  588. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  589. }
  590. void Chunk::setModelChanged(int type)
  591. {
  592. modelChanged |= type;
  593. }
  594. void Chunk::setLightChanged(int type)
  595. {
  596. lightChanged |= type;
  597. }