Chunk.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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, 0);
  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, 0);
  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, 0);
  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, 0);
  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. 0);
  276. if (zB->isVisible() && !visible)
  277. {
  278. vcs.lock();
  279. zB->tick(0);
  280. visibleBlocks.add(zB);
  281. vcs.unlock();
  282. }
  283. }
  284. }
  285. else
  286. {
  287. pos.x += this->location.x - CHUNK_SIZE / 2;
  288. pos.y += this->location.y - CHUNK_SIZE / 2;
  289. Chunk* c = World::INSTANCE->zChunk(
  290. World::INSTANCE->getChunkCenter(pos.x, pos.y));
  291. Block* zB = c ? c->zBlockAt(pos) : 0;
  292. if (zB)
  293. {
  294. bool visible = zB->isVisible();
  295. zB->setLightData(
  296. getOppositeDirection(getDirectionFromIndex(i)),
  297. (unsigned char*)lightData,
  298. c);
  299. if (zB->isVisible() && !visible)
  300. {
  301. c->vcs.lock();
  302. zB->tick(0);
  303. c->visibleBlocks.add(zB);
  304. c->vcs.unlock();
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. zReader->lese((char*)&index, 4);
  312. }
  313. isLoading = 0;
  314. }
  315. void Chunk::buildModel(ChunkModelBuilder* builder)
  316. {
  317. vcs.lock();
  318. modelChanged &= ~builder->getType();
  319. lightChanged &= ~builder->getType();
  320. builder->buildModel();
  321. vcs.unlock();
  322. }
  323. void Chunk::updateLight(ChunkModelBuilder* builder)
  324. {
  325. vcs.lock();
  326. lightChanged &= ~builder->getType();
  327. builder->updateLightning();
  328. vcs.unlock();
  329. }
  330. void Chunk::renderSolid(std::function<void(Model3D*)> f)
  331. {
  332. vcs.lock();
  333. CustomDX11API* api
  334. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  335. api->setCullBack(false);
  336. for (ChunkModelBuilder* builder : modelBuilders)
  337. {
  338. if (!builder->isTransparent())
  339. {
  340. f(builder->zModel());
  341. }
  342. }
  343. api->setCullBack(true);
  344. float dist = 0.f;
  345. for (Block* b : visibleBlocks)
  346. {
  347. f(b);
  348. }
  349. vcs.unlock();
  350. }
  351. void Chunk::renderTransparent(std::function<void(Model3D*)> f)
  352. {
  353. CustomDX11API* api
  354. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  355. api->setCullBack(false);
  356. for (ChunkModelBuilder* builder : modelBuilders)
  357. {
  358. if (builder->isTransparent())
  359. {
  360. f(builder->zModel());
  361. }
  362. }
  363. api->setCullBack(true);
  364. }
  365. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  366. {
  367. acs.lock();
  368. vcs.lock(); // TODO: enshure no dead lock occures
  369. for (ChunkModelBuilder* builder : modelBuilders)
  370. {
  371. if ((modelChanged | builder->getType()) == modelChanged)
  372. buildModel(builder);
  373. }
  374. for (ChunkModelBuilder* builder : modelBuilders)
  375. {
  376. if ((lightChanged | builder->getType()) == lightChanged)
  377. updateLight(builder);
  378. }
  379. bool res = 0;
  380. for (ChunkModelBuilder* builder : modelBuilders)
  381. {
  382. res |= builder->zModel()->tick(time);
  383. }
  384. auto iterator = animations.begin();
  385. while (iterator)
  386. {
  387. if (iterator->tick(time))
  388. {
  389. res |= iterator->zBlock()->tick(time);
  390. if (iterator->isFinished())
  391. {
  392. iterator.remove();
  393. continue;
  394. }
  395. }
  396. else
  397. {
  398. iterator.remove();
  399. continue;
  400. }
  401. ++iterator;
  402. }
  403. vcs.unlock();
  404. acs.unlock();
  405. return 1;
  406. }
  407. void Chunk::destroy()
  408. {
  409. for (ChunkModelBuilder* builder : modelBuilders)
  410. {
  411. Model3DData* chunkModel = builder->zModel()->zModelData();
  412. // remove old model
  413. while (chunkModel->getPolygonAnzahl() > 0)
  414. {
  415. chunkModel->removePolygon(0);
  416. }
  417. chunkModel->setVertecies(0, 0);
  418. }
  419. }
  420. void Chunk::api(char* message)
  421. {
  422. switch (message[0])
  423. {
  424. case 0: // set block
  425. {
  426. unsigned short id = *(int*)(message + 1);
  427. int index = *(int*)(message + 3);
  428. char state = message[7];
  429. char flowOptions = 0;
  430. char distanceToSource = 0;
  431. if ((state | 1) == state)
  432. {
  433. flowOptions = message[8];
  434. distanceToSource = message[9];
  435. }
  436. bool passable = 0;
  437. float speedModifier = 1.f;
  438. if ((state | 2) == state)
  439. {
  440. passable = 1;
  441. speedModifier = *(float*)(message + 10);
  442. }
  443. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  444. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  445. index % WORLD_HEIGHT);
  446. location.x += this->location.x - CHUNK_SIZE / 2;
  447. location.y += this->location.y - CHUNK_SIZE / 2;
  448. if (blockTypes[id]->doesNeedInstance())
  449. {
  450. Block* zB = blockTypes[id]->createBlock(
  451. location, passable, speedModifier);
  452. zB->setFlow(flowOptions, distanceToSource);
  453. setBlock(zB);
  454. }
  455. else
  456. {
  457. Block* zB = zBlockAt(location);
  458. if (zB) removeBlock(zB);
  459. }
  460. break;
  461. }
  462. case 1: // animate block
  463. {
  464. int index = *(int*)(message + 1);
  465. int boneId = *(int*)(message + 5);
  466. double time = *(double*)(message + 9);
  467. Framework::Vec3<float> pos;
  468. pos.x = *(float*)(message + 17);
  469. pos.y = *(float*)(message + 21);
  470. pos.z = *(float*)(message + 25);
  471. Framework::Vec3<float> rot;
  472. rot.x = *(float*)(message + 29);
  473. rot.y = *(float*)(message + 33);
  474. rot.z = *(float*)(message + 37);
  475. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  476. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  477. index % WORLD_HEIGHT);
  478. location.x += this->location.x - CHUNK_SIZE / 2;
  479. location.y += this->location.y - CHUNK_SIZE / 2;
  480. Block* zB = zBlockAt(location);
  481. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  482. break;
  483. }
  484. }
  485. }
  486. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  487. {
  488. if (location.z < 0 || location.z >= WORLD_HEIGHT) return 0;
  489. location.x = location.x % CHUNK_SIZE;
  490. location.y = location.y % CHUNK_SIZE;
  491. if (location.x < 0) location.x += CHUNK_SIZE;
  492. if (location.y < 0) location.y += CHUNK_SIZE;
  493. int index
  494. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  495. return blocks[index];
  496. }
  497. void Chunk::setBlock(Block* block)
  498. {
  499. cs.lock();
  500. Framework::Vec3<int> pos = block->getLocation();
  501. pos.x = pos.x % CHUNK_SIZE;
  502. pos.y = pos.y % CHUNK_SIZE;
  503. if (pos.x < 0) pos.x += CHUNK_SIZE;
  504. if (pos.y < 0) pos.y += CHUNK_SIZE;
  505. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  506. int affectsGround = 0;
  507. int newAffectsGround = 0;
  508. for (ChunkModelBuilder* builder : modelBuilders)
  509. {
  510. if (block && builder->isPartOfModel(block))
  511. {
  512. newAffectsGround |= builder->getType();
  513. }
  514. if (blocks[index] && builder->isPartOfModel(blocks[index]))
  515. {
  516. affectsGround |= builder->getType();
  517. }
  518. }
  519. if (blocks[index])
  520. {
  521. vcs.lock();
  522. for (Framework::ArrayIterator<Block*> vi = visibleBlocks.begin(); vi;
  523. vi++)
  524. {
  525. if (blocks[index] == (Block*)vi)
  526. {
  527. vi.remove();
  528. break;
  529. }
  530. }
  531. vcs.unlock();
  532. blocks[index]->copyLightTo(block);
  533. blocks[index]->release();
  534. blocks[index] = block;
  535. cs.unlock();
  536. vcs.lock();
  537. modelChanged |= affectsGround | newAffectsGround;
  538. if (block && block->isVisible() && !newAffectsGround)
  539. {
  540. block->tick(0);
  541. visibleBlocks.add(block);
  542. }
  543. vcs.unlock();
  544. return;
  545. }
  546. blocks[index] = block;
  547. cs.unlock();
  548. vcs.lock();
  549. modelChanged |= affectsGround | newAffectsGround;
  550. if (block && block->isVisible() && !newAffectsGround)
  551. {
  552. block->tick(0);
  553. visibleBlocks.add(block);
  554. }
  555. vcs.unlock();
  556. }
  557. void Chunk::removeBlock(Block* zBlock)
  558. {
  559. cs.lock();
  560. vcs.lock();
  561. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  562. iterator;
  563. iterator++)
  564. {
  565. if (zBlock == (Block*)iterator)
  566. {
  567. iterator.remove();
  568. break;
  569. }
  570. }
  571. vcs.unlock();
  572. Vec3<int> pos = zBlock->getLocation();
  573. pos.x = pos.x % CHUNK_SIZE;
  574. pos.y = pos.y % CHUNK_SIZE;
  575. if (pos.x < 0) pos.x += CHUNK_SIZE;
  576. if (pos.y < 0) pos.y += CHUNK_SIZE;
  577. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  578. if (blocks[index])
  579. {
  580. modelChanged |= blocks[index]->getPartOfModels();
  581. blocks[index]->release();
  582. blocks[index] = 0;
  583. }
  584. cs.unlock();
  585. }
  586. void Chunk::blockVisibilityChanged(Block* zB)
  587. {
  588. vcs.lock();
  589. if (zB->isVisible())
  590. {
  591. zB->tick(0);
  592. visibleBlocks.add(zB);
  593. }
  594. else
  595. {
  596. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  597. iterator;
  598. iterator++)
  599. {
  600. if (zB == (Block*)iterator)
  601. {
  602. iterator.remove();
  603. break;
  604. }
  605. }
  606. }
  607. vcs.unlock();
  608. }
  609. Framework::Punkt Chunk::getCenter() const
  610. {
  611. return location;
  612. }
  613. Framework::Vec3<int> Chunk::getMin() const
  614. {
  615. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  616. }
  617. Framework::Vec3<int> Chunk::getMax() const
  618. {
  619. return {
  620. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  621. }
  622. void Chunk::setModelChanged(int type)
  623. {
  624. modelChanged |= type;
  625. }
  626. void Chunk::setLightChanged(int type)
  627. {
  628. lightChanged |= type;
  629. }