Chunk.cpp 20 KB

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