Chunk.cpp 18 KB

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