Chunk.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. 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. groundModel = new ChunkGroundModel(ground, this);
  37. FactoryCraftModel* fluids = new FactoryCraftModel();
  38. chunkModel = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  39. Text("chunk_fluids_") + location.x + location.y);
  40. if (!chunkModel)
  41. {
  42. chunkModel
  43. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  44. Text("chunk_fluids_") + location.x + location.y);
  45. }
  46. chunkModel->setAmbientFactor(0.f);
  47. chunkModel->setDiffusFactor(1.f);
  48. chunkModel->setSpecularFactor(0.f);
  49. chunkModel->setVertecies(0, 0);
  50. fluids->setModelDaten(chunkModel);
  51. fluids->setPosition(
  52. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  53. fluids->tick(0);
  54. fluidModel = new ChunkFluidModel(fluids, this);
  55. }
  56. Chunk::Chunk(Framework::Punkt location, Framework::StreamReader* zReader)
  57. : Chunk(location)
  58. {
  59. load(zReader);
  60. buildModel(groundModel);
  61. buildModel(fluidModel);
  62. }
  63. Chunk::~Chunk()
  64. {
  65. char msg = 1; // remove observer
  66. if (World::INSTANCE)
  67. {
  68. World::INSTANCE->zClient()->chunkAPIRequest(location, &msg, 1);
  69. }
  70. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  71. {
  72. if (blocks[i])
  73. {
  74. blocks[i]->release();
  75. blocks[i] = 0;
  76. }
  77. }
  78. delete[] blocks;
  79. groundModel->release();
  80. fluidModel->release();
  81. }
  82. void Chunk::appendAnimation(
  83. Block* zB, int boneId, double time, Vec3<float> pos, Vec3<float> rot)
  84. {
  85. if (!zB->zSkeleton() || !zB->zSkeleton()->zBone(boneId)) return;
  86. acs.lock();
  87. for (BlockAnimation* animation : animations)
  88. {
  89. if (animation->zBlock() == zB)
  90. {
  91. animation->appendAnimation(boneId, time, pos, rot);
  92. acs.unlock();
  93. return;
  94. }
  95. }
  96. SkeletonAnimation* sa = new SkeletonAnimation();
  97. Bone* bone = zB->zSkeleton()->zBone(boneId);
  98. sa->addAnimation(boneId, bone->getPosition(), bone->getRotation());
  99. sa->addKeyFrame(boneId, time, pos, rot);
  100. animations.add(new BlockAnimation(dynamic_cast<Block*>(zB->getThis()), sa));
  101. acs.unlock();
  102. }
  103. void Chunk::load(Framework::StreamReader* zReader)
  104. {
  105. cs.lock();
  106. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  107. {
  108. if (blocks[i])
  109. {
  110. blocks[i]->release();
  111. blocks[i] = 0;
  112. }
  113. }
  114. cs.unlock();
  115. isLoading = 1;
  116. Framework::Vec3<int> pos = {0, 0, 0};
  117. unsigned short id;
  118. zReader->lese((char*)&id, 2);
  119. int count = 0;
  120. while (id)
  121. {
  122. int index;
  123. zReader->lese((char*)&index, 4);
  124. short fill = 0;
  125. if (blockTypes[id]->isFluid())
  126. {
  127. zReader->lese((char*)&fill, 2);
  128. }
  129. pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  130. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  131. index % WORLD_HEIGHT);
  132. if (blockTypes[id]->doesNeedInstance())
  133. {
  134. cs.lock();
  135. Block* b = blockTypes[id]->createBlock(
  136. {pos.x + location.x - CHUNK_SIZE / 2,
  137. pos.y + location.y - CHUNK_SIZE / 2,
  138. pos.z});
  139. b->setFill(fill);
  140. blocks[index] = b;
  141. cs.unlock();
  142. vcs.lock();
  143. if (b->isVisible())
  144. {
  145. if (!blockTypes[id]->getModelInfo().getModelName().istGleich(
  146. "cube")
  147. && !blockTypes[id]->getModelInfo().getModelName().istGleich(
  148. "grass"))
  149. {
  150. b->tick(0);
  151. visibleBlocks.add(b);
  152. }
  153. }
  154. count++;
  155. vcs.unlock();
  156. }
  157. zReader->lese((char*)&id, 2);
  158. }
  159. std::cout << "Loaded " << count << " blocks\n";
  160. int index = 0;
  161. // light
  162. zReader->lese((char*)&index, 4);
  163. char lightData[6];
  164. while (index >= -1)
  165. {
  166. if (index == -1)
  167. {
  168. int x = 0;
  169. int y = 0;
  170. int z = 0;
  171. zReader->lese((char*)&x, 4);
  172. zReader->lese((char*)&y, 4);
  173. zReader->lese((char*)&z, 4);
  174. zReader->lese(lightData, 6);
  175. if (x == -1)
  176. {
  177. int cacheIndex = y * WORLD_HEIGHT + z;
  178. Block* zB = blocks[cacheIndex];
  179. if (zB)
  180. {
  181. zB->setLightData(WEST, (unsigned char*)lightData);
  182. }
  183. }
  184. else if (y == -1)
  185. {
  186. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  187. Block* zB = blocks[cacheIndex];
  188. if (zB)
  189. {
  190. zB->setLightData(NORTH, (unsigned char*)lightData);
  191. }
  192. }
  193. else if (x == CHUNK_SIZE)
  194. {
  195. int cacheIndex
  196. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  197. Block* zB = blocks[cacheIndex];
  198. if (zB)
  199. {
  200. zB->setLightData(EAST, (unsigned char*)lightData);
  201. }
  202. }
  203. else if (y == CHUNK_SIZE)
  204. {
  205. int cacheIndex
  206. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  207. Block* zB = blocks[cacheIndex];
  208. if (zB)
  209. {
  210. zB->setLightData(SOUTH, (unsigned char*)lightData);
  211. }
  212. }
  213. }
  214. else
  215. {
  216. zReader->lese(lightData, 6);
  217. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  218. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  219. index % WORLD_HEIGHT);
  220. for (int i = 0; i < 6; i++)
  221. {
  222. Framework::Vec3<int> pos
  223. = location + getDirection(getDirectionFromIndex(i));
  224. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  225. {
  226. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  227. && pos.y < CHUNK_SIZE)
  228. {
  229. int cacheIndex
  230. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  231. + pos.z;
  232. Block* zB = blocks[cacheIndex];
  233. if (zB)
  234. {
  235. bool visible = zB->isVisible();
  236. zB->setLightData(
  237. getOppositeDirection(getDirectionFromIndex(i)),
  238. (unsigned char*)lightData);
  239. if (zB->isVisible() && !visible)
  240. {
  241. vcs.lock();
  242. zB->tick(0);
  243. visibleBlocks.add(zB);
  244. vcs.unlock();
  245. }
  246. }
  247. }
  248. else
  249. {
  250. pos.x += this->location.x - CHUNK_SIZE / 2;
  251. pos.y += this->location.y - CHUNK_SIZE / 2;
  252. Block* zB = World::INSTANCE->zBlockAt(pos);
  253. if (zB)
  254. {
  255. bool visible = zB->isVisible();
  256. zB->setLightData(
  257. getOppositeDirection(getDirectionFromIndex(i)),
  258. (unsigned char*)lightData);
  259. if (zB->isVisible() && !visible)
  260. {
  261. Chunk* c = World::INSTANCE->zChunk(
  262. World::INSTANCE->getChunkCenter(
  263. pos.x, pos.y));
  264. c->vcs.lock();
  265. zB->tick(0);
  266. c->visibleBlocks.add(zB);
  267. c->vcs.unlock();
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. zReader->lese((char*)&index, 4);
  275. }
  276. isLoading = 0;
  277. }
  278. void Chunk::buildModel(ChunkModelBuilder* builder)
  279. {
  280. vcs.lock();
  281. modelChanged &= ~builder->getType();
  282. lightChanged &= ~builder->getType();
  283. builder->buildModel();
  284. vcs.unlock();
  285. }
  286. void Chunk::updateLight(ChunkModelBuilder* builder)
  287. {
  288. vcs.lock();
  289. lightChanged &= ~builder->getType();
  290. builder->updateLightning();
  291. vcs.unlock();
  292. }
  293. void Chunk::renderSolid(std::function<void(Model3D*)> f)
  294. {
  295. vcs.lock();
  296. CustomDX11API* api
  297. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  298. api->setCullBack(false);
  299. f(groundModel->zModel());
  300. api->setCullBack(true);
  301. float dist = 0.f;
  302. if (api->isInFrustrum(groundModel->zModel()->getPos(),
  303. (CHUNK_SIZE / 2.f, CHUNK_SIZE / 2.f, WORLD_HEIGHT / 2.f),
  304. &dist))
  305. {
  306. for (Block* b : visibleBlocks)
  307. {
  308. f(b);
  309. }
  310. }
  311. vcs.unlock();
  312. }
  313. void Chunk::renderTransparent(std::function<void(Model3D*)> f)
  314. {
  315. CustomDX11API* api
  316. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  317. api->setCullBack(false);
  318. f(fluidModel->zModel());
  319. api->setCullBack(true);
  320. }
  321. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  322. {
  323. acs.lock();
  324. vcs.lock(); // TODO: enshure no dead lock occures
  325. if ((modelChanged | CombinedModels::GROUND) == modelChanged)
  326. buildModel(groundModel);
  327. if ((modelChanged | CombinedModels::FLUID) == modelChanged)
  328. buildModel(fluidModel);
  329. if ((lightChanged | CombinedModels::GROUND) == lightChanged)
  330. updateLight(groundModel);
  331. if ((lightChanged | CombinedModels::FLUID) == lightChanged)
  332. updateLight(fluidModel);
  333. bool res = groundModel->zModel()->tick(time);
  334. res |= fluidModel->zModel()->tick(time);
  335. auto iterator = animations.begin();
  336. while (iterator)
  337. {
  338. if (iterator->tick(time))
  339. {
  340. res |= iterator->zBlock()->tick(time);
  341. if (iterator->isFinished())
  342. {
  343. iterator.remove();
  344. continue;
  345. }
  346. }
  347. else
  348. {
  349. iterator.remove();
  350. continue;
  351. }
  352. ++iterator;
  353. }
  354. vcs.unlock();
  355. acs.unlock();
  356. return 1;
  357. }
  358. void Chunk::destroy()
  359. {
  360. Model3DData* chunkModel = groundModel->zModel()->zModelData();
  361. // remove old model
  362. while (chunkModel->getPolygonAnzahl() > 0)
  363. {
  364. chunkModel->removePolygon(0);
  365. }
  366. chunkModel->setVertecies(0, 0);
  367. chunkModel = fluidModel->zModel()->zModelData();
  368. // remove old model
  369. while (chunkModel->getPolygonAnzahl() > 0)
  370. {
  371. chunkModel->removePolygon(0);
  372. }
  373. chunkModel->setVertecies(0, 0);
  374. }
  375. void Chunk::api(char* message)
  376. {
  377. switch (message[0])
  378. {
  379. case 0: // set block
  380. {
  381. int index = *(int*)(message + 1);
  382. int id = *(int*)(message + 5);
  383. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  384. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  385. index % WORLD_HEIGHT);
  386. location.x += this->location.x - CHUNK_SIZE / 2;
  387. location.y += this->location.y - CHUNK_SIZE / 2;
  388. if (blockTypes[id]->doesNeedInstance())
  389. {
  390. Block* zB = blockTypes[id]->createBlock(location);
  391. setBlock(zB);
  392. }
  393. else
  394. {
  395. Block* zB = zBlockAt(location);
  396. if (zB) removeBlock(zB);
  397. }
  398. break;
  399. }
  400. case 1: // animate block
  401. {
  402. int index = *(int*)(message + 1);
  403. int boneId = *(int*)(message + 5);
  404. double time = *(double*)(message + 9);
  405. Framework::Vec3<float> pos;
  406. pos.x = *(float*)(message + 17);
  407. pos.y = *(float*)(message + 21);
  408. pos.z = *(float*)(message + 25);
  409. Framework::Vec3<float> rot;
  410. rot.x = *(float*)(message + 29);
  411. rot.y = *(float*)(message + 33);
  412. rot.z = *(float*)(message + 37);
  413. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  414. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  415. index % WORLD_HEIGHT);
  416. location.x += this->location.x - CHUNK_SIZE / 2;
  417. location.y += this->location.y - CHUNK_SIZE / 2;
  418. Block* zB = zBlockAt(location);
  419. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  420. break;
  421. }
  422. }
  423. }
  424. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  425. {
  426. location.x = location.x % CHUNK_SIZE;
  427. location.y = location.y % CHUNK_SIZE;
  428. if (location.x < 0) location.x += CHUNK_SIZE;
  429. if (location.y < 0) location.y += CHUNK_SIZE;
  430. int index
  431. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  432. return blocks[index];
  433. }
  434. void Chunk::setBlock(Block* block)
  435. {
  436. cs.lock();
  437. Framework::Vec3<int> pos = block->getLocation();
  438. pos.x = pos.x % CHUNK_SIZE;
  439. pos.y = pos.y % CHUNK_SIZE;
  440. if (pos.x < 0) pos.x += CHUNK_SIZE;
  441. if (pos.y < 0) pos.y += CHUNK_SIZE;
  442. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  443. bool newAffectsGround
  444. = block
  445. && (block->zBlockType()->getModelInfo().getModelName().istGleich("cube")
  446. || block->zBlockType()->getModelInfo().getModelName().istGleich(
  447. "grass"));
  448. bool affectsGround
  449. = blocks[index]
  450. && (blocks[index]->zBlockType()->getModelInfo().getModelName().istGleich(
  451. "cube")
  452. || blocks[index]
  453. ->zBlockType()
  454. ->getModelInfo()
  455. .getModelName()
  456. .istGleich("grass"));
  457. if (blocks[index])
  458. {
  459. vcs.lock();
  460. for (Framework::Iterator<Block*> vi = visibleBlocks.begin(); vi; vi++)
  461. {
  462. if (blocks[index] == (Block*)vi)
  463. {
  464. vi.remove();
  465. break;
  466. }
  467. }
  468. vcs.unlock();
  469. blocks[index]->copyLightTo(block);
  470. blocks[index]->release();
  471. blocks[index] = block;
  472. cs.unlock();
  473. vcs.lock();
  474. if (affectsGround || newAffectsGround)
  475. {
  476. modelChanged = 1;
  477. }
  478. if (block && block->isVisible() && !newAffectsGround)
  479. {
  480. block->tick(0);
  481. visibleBlocks.add(block);
  482. }
  483. vcs.unlock();
  484. return;
  485. }
  486. blocks[index] = block;
  487. cs.unlock();
  488. vcs.lock();
  489. if (affectsGround || newAffectsGround)
  490. {
  491. modelChanged = 1;
  492. }
  493. if (block && block->isVisible() && !newAffectsGround)
  494. {
  495. block->tick(0);
  496. visibleBlocks.add(block);
  497. }
  498. vcs.unlock();
  499. }
  500. void Chunk::removeBlock(Block* zBlock)
  501. {
  502. cs.lock();
  503. vcs.lock();
  504. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator;
  505. iterator++)
  506. {
  507. if (zBlock == (Block*)iterator)
  508. {
  509. iterator.remove();
  510. break;
  511. }
  512. }
  513. vcs.unlock();
  514. Vec3<int> pos = zBlock->getLocation();
  515. pos.x = pos.x % CHUNK_SIZE;
  516. pos.y = pos.y % CHUNK_SIZE;
  517. if (pos.x < 0) pos.x += CHUNK_SIZE;
  518. if (pos.y < 0) pos.y += CHUNK_SIZE;
  519. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  520. if (blocks[index])
  521. {
  522. bool affectsGround = blocks[index]
  523. ->zBlockType()
  524. ->getModelInfo()
  525. .getModelName()
  526. .istGleich("cube")
  527. || blocks[index]
  528. ->zBlockType()
  529. ->getModelInfo()
  530. .getModelName()
  531. .istGleich("grass");
  532. blocks[index]->release();
  533. blocks[index] = 0;
  534. if (affectsGround) modelChanged = 1;
  535. }
  536. cs.unlock();
  537. }
  538. void Chunk::blockVisibilityChanged(Block* zB)
  539. {
  540. vcs.lock();
  541. if (zB->isVisible())
  542. {
  543. zB->tick(0);
  544. visibleBlocks.add(zB);
  545. }
  546. else
  547. {
  548. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin();
  549. iterator;
  550. iterator++)
  551. {
  552. if (zB == (Block*)iterator)
  553. {
  554. iterator.remove();
  555. break;
  556. }
  557. }
  558. }
  559. vcs.unlock();
  560. }
  561. Framework::Punkt Chunk::getCenter() const
  562. {
  563. return location;
  564. }
  565. Framework::Vec3<int> Chunk::getMin() const
  566. {
  567. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  568. }
  569. Framework::Vec3<int> Chunk::getMax() const
  570. {
  571. return {
  572. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  573. }
  574. void Chunk::setModelChanged(int type)
  575. {
  576. modelChanged |= type;
  577. }
  578. void Chunk::setLightChanged(int type)
  579. {
  580. lightChanged |= type;
  581. }