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 "TransparentChunkGroundModel.h"
  10. Chunk::Chunk(Framework::Punkt location)
  11. : ReferenceCounter(),
  12. location(location),
  13. isLoading(0),
  14. lightChanged(0)
  15. {
  16. blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  17. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  18. FactoryCraftModel* ground = new FactoryCraftModel();
  19. Model3DData* chunkModel
  20. = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  21. Text("chunk_ground_") + location.x + location.y);
  22. if (!chunkModel)
  23. {
  24. chunkModel
  25. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  26. Text("chunk_ground_") + location.x + location.y);
  27. }
  28. chunkModel->setAmbientFactor(0.f);
  29. chunkModel->setDiffusFactor(1.f);
  30. chunkModel->setSpecularFactor(0.f);
  31. chunkModel->setVertecies(0, 0);
  32. ground->setModelDaten(chunkModel);
  33. ground->setPosition(
  34. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  35. ground->tick(0);
  36. ChunkModelBuilder* groundModel = new ChunkGroundModel(ground, this);
  37. modelBuilders.add(groundModel);
  38. FactoryCraftModel* transparentGround = new FactoryCraftModel();
  39. chunkModel = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  40. Text("transparent_chunk_ground_") + location.x + location.y);
  41. if (!chunkModel)
  42. {
  43. chunkModel
  44. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  45. Text("transparent_chunk_ground_") + location.x + location.y);
  46. }
  47. chunkModel->setAmbientFactor(0.f);
  48. chunkModel->setDiffusFactor(1.f);
  49. chunkModel->setSpecularFactor(0.f);
  50. chunkModel->setVertecies(0, 0);
  51. transparentGround->setModelDaten(chunkModel);
  52. transparentGround->setPosition(
  53. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  54. transparentGround->tick(0);
  55. ChunkModelBuilder* transparentGroundModel
  56. = new TransparentChunkGroundModel(transparentGround, this);
  57. modelBuilders.add(transparentGroundModel);
  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. ChunkModelBuilder* fluidModel = new ChunkFluidModel(fluids, this);
  76. modelBuilders.add(fluidModel);
  77. }
  78. Chunk::Chunk(Framework::Punkt location, Framework::StreamReader* zReader)
  79. : Chunk(location)
  80. {
  81. load(zReader);
  82. for (ChunkModelBuilder* builder : modelBuilders)
  83. {
  84. buildModel(builder);
  85. }
  86. }
  87. Chunk::~Chunk()
  88. {
  89. char msg = 1; // remove observer
  90. if (World::INSTANCE)
  91. {
  92. World::INSTANCE->zClient()->chunkAPIRequest(location, &msg, 1);
  93. }
  94. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  95. {
  96. if (blocks[i])
  97. {
  98. blocks[i]->release();
  99. blocks[i] = 0;
  100. }
  101. }
  102. delete[] blocks;
  103. }
  104. void Chunk::appendAnimation(
  105. Block* zB, int boneId, double time, Vec3<float> pos, Vec3<float> rot)
  106. {
  107. if (!zB->zSkeleton() || !zB->zSkeleton()->zBone(boneId)) return;
  108. acs.lock();
  109. for (BlockAnimation* animation : animations)
  110. {
  111. if (animation->zBlock() == zB)
  112. {
  113. animation->appendAnimation(boneId, time, pos, rot);
  114. acs.unlock();
  115. return;
  116. }
  117. }
  118. SkeletonAnimation* sa = new SkeletonAnimation();
  119. Bone* bone = zB->zSkeleton()->zBone(boneId);
  120. sa->addAnimation(boneId, bone->getPosition(), bone->getRotation());
  121. sa->addKeyFrame(boneId, time, pos, rot);
  122. animations.add(new BlockAnimation(dynamic_cast<Block*>(zB->getThis()), sa));
  123. acs.unlock();
  124. }
  125. void Chunk::load(Framework::StreamReader* zReader)
  126. {
  127. cs.lock();
  128. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  129. {
  130. if (blocks[i])
  131. {
  132. blocks[i]->release();
  133. blocks[i] = 0;
  134. }
  135. }
  136. cs.unlock();
  137. isLoading = 1;
  138. Framework::Vec3<int> pos = {0, 0, 0};
  139. unsigned short id;
  140. zReader->lese((char*)&id, 2);
  141. int count = 0;
  142. while (id)
  143. {
  144. int index;
  145. zReader->lese((char*)&index, 4);
  146. char state = 0;
  147. zReader->lese(&state, 1);
  148. char flowOptions = 0;
  149. char distanceToSource = 0;
  150. if ((state | 1) == state)
  151. {
  152. zReader->lese(&flowOptions, 1);
  153. zReader->lese(&distanceToSource, 1);
  154. }
  155. bool passable = 0;
  156. float speedModifier = 1.f;
  157. if ((state | 2) == state)
  158. {
  159. passable = 1;
  160. zReader->lese((char*)&speedModifier, 4);
  161. }
  162. pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  163. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  164. index % WORLD_HEIGHT);
  165. if (blockTypes[id]->doesNeedInstance())
  166. {
  167. cs.lock();
  168. Block* b = blockTypes[id]->createBlock(
  169. {pos.x + location.x - CHUNK_SIZE / 2,
  170. pos.y + location.y - CHUNK_SIZE / 2,
  171. pos.z},
  172. passable,
  173. speedModifier);
  174. b->setFlow(flowOptions, distanceToSource);
  175. blocks[index] = b;
  176. cs.unlock();
  177. vcs.lock();
  178. if (b->isVisible())
  179. {
  180. if (!blockTypes[id]->getModelInfo().getModelName().istGleich(
  181. "cube")
  182. && !blockTypes[id]->getModelInfo().getModelName().istGleich(
  183. "grass"))
  184. {
  185. b->tick(0);
  186. visibleBlocks.add(b);
  187. }
  188. }
  189. count++;
  190. vcs.unlock();
  191. }
  192. zReader->lese((char*)&id, 2);
  193. }
  194. std::cout << "Loaded " << count << " blocks\n";
  195. int index = 0;
  196. // light
  197. zReader->lese((char*)&index, 4);
  198. char lightData[6];
  199. while (index >= -1)
  200. {
  201. if (index == -1)
  202. {
  203. int x = 0;
  204. int y = 0;
  205. int z = 0;
  206. zReader->lese((char*)&x, 4);
  207. zReader->lese((char*)&y, 4);
  208. zReader->lese((char*)&z, 4);
  209. zReader->lese(lightData, 6);
  210. if (x == -1)
  211. {
  212. int cacheIndex = y * WORLD_HEIGHT + z;
  213. Block* zB = blocks[cacheIndex];
  214. if (zB)
  215. {
  216. zB->setLightData(WEST, (unsigned char*)lightData, 0);
  217. }
  218. }
  219. else if (y == -1)
  220. {
  221. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  222. Block* zB = blocks[cacheIndex];
  223. if (zB)
  224. {
  225. zB->setLightData(NORTH, (unsigned char*)lightData, 0);
  226. }
  227. }
  228. else if (x == CHUNK_SIZE)
  229. {
  230. int cacheIndex
  231. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  232. Block* zB = blocks[cacheIndex];
  233. if (zB)
  234. {
  235. zB->setLightData(EAST, (unsigned char*)lightData, 0);
  236. }
  237. }
  238. else if (y == CHUNK_SIZE)
  239. {
  240. int cacheIndex
  241. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  242. Block* zB = blocks[cacheIndex];
  243. if (zB)
  244. {
  245. zB->setLightData(SOUTH, (unsigned char*)lightData, 0);
  246. }
  247. }
  248. }
  249. else
  250. {
  251. zReader->lese(lightData, 6);
  252. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  253. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  254. index % WORLD_HEIGHT);
  255. for (int i = 0; i < 6; i++)
  256. {
  257. Framework::Vec3<int> pos
  258. = location + getDirection(getDirectionFromIndex(i));
  259. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  260. {
  261. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  262. && pos.y < CHUNK_SIZE)
  263. {
  264. int cacheIndex
  265. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  266. + pos.z;
  267. Block* zB = blocks[cacheIndex];
  268. if (zB)
  269. {
  270. bool visible = zB->isVisible();
  271. zB->setLightData(
  272. getOppositeDirection(getDirectionFromIndex(i)),
  273. (unsigned char*)lightData,
  274. 0);
  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. Chunk* c = World::INSTANCE->zChunk(
  289. World::INSTANCE->getChunkCenter(pos.x, pos.y));
  290. Block* zB = c ? c->zBlockAt(pos) : 0;
  291. if (zB)
  292. {
  293. bool visible = zB->isVisible();
  294. zB->setLightData(
  295. getOppositeDirection(getDirectionFromIndex(i)),
  296. (unsigned char*)lightData,
  297. c);
  298. if (zB->isVisible() && !visible)
  299. {
  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()) == lightChanged)
  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. }