Chunk.cpp 20 KB

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