Chunk.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. #include "Chunk.h"
  2. #include <Shader.h>
  3. #include "Constants.h"
  4. #include "CustomDX11API.h"
  5. #include "FactoryCraftModel.h"
  6. #include "Globals.h"
  7. #include "Registries.h"
  8. Chunk::Chunk(Framework::Punkt location)
  9. : ReferenceCounter(),
  10. location(location),
  11. isLoading(0),
  12. lightChanged(0)
  13. {
  14. blocks = new Block*[CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT];
  15. memset(blocks, 0, CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT * sizeof(Block*));
  16. groundModel = new FactoryCraftModel();
  17. Model3DData* chunkModel
  18. = uiFactory.initParam.bildschirm->zGraphicsApi()->getModel(
  19. Text("chunk_ground_") + location.x + location.y);
  20. if (!chunkModel)
  21. {
  22. chunkModel
  23. = uiFactory.initParam.bildschirm->zGraphicsApi()->createModel(
  24. Text("chunk_ground_") + location.x + location.y);
  25. }
  26. chunkModel->setAmbientFactor(0.f);
  27. chunkModel->setDiffusFactor(1.f);
  28. chunkModel->setSpecularFactor(0.f);
  29. chunkModel->setVertecies(0, 0);
  30. groundModel->setModelDaten(chunkModel);
  31. groundModel->setPosition(
  32. (float)location.x, (float)location.y, (float)WORLD_HEIGHT / 2.f);
  33. groundModel->tick(0);
  34. }
  35. Chunk::Chunk(Framework::Punkt location, Framework::StreamReader* zReader)
  36. : Chunk(location)
  37. {
  38. load(zReader);
  39. buildGroundModel();
  40. }
  41. Chunk::~Chunk()
  42. {
  43. char msg = 1; // remove observer
  44. if (World::INSTANCE)
  45. {
  46. World::INSTANCE->zClient()->chunkAPIRequest(location, &msg, 1);
  47. }
  48. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  49. {
  50. if (blocks[i])
  51. {
  52. blocks[i]->release();
  53. blocks[i] = 0;
  54. }
  55. }
  56. delete[] blocks;
  57. groundModel->release();
  58. }
  59. void Chunk::appendAnimation(
  60. Block* zB, int boneId, double time, Vec3<float> pos, Vec3<float> rot)
  61. {
  62. if (!zB->zSkeleton() || !zB->zSkeleton()->zBone(boneId)) return;
  63. acs.lock();
  64. for (BlockAnimation* animation : animations)
  65. {
  66. if (animation->zBlock() == zB)
  67. {
  68. animation->appendAnimation(boneId, time, pos, rot);
  69. acs.unlock();
  70. return;
  71. }
  72. }
  73. SkeletonAnimation* sa = new SkeletonAnimation();
  74. Bone* bone = zB->zSkeleton()->zBone(boneId);
  75. sa->addAnimation(boneId, bone->getPosition(), bone->getRotation());
  76. sa->addKeyFrame(boneId, time, pos, rot);
  77. animations.add(new BlockAnimation(dynamic_cast<Block*>(zB->getThis()), sa));
  78. acs.unlock();
  79. }
  80. void Chunk::load(Framework::StreamReader* zReader)
  81. {
  82. cs.lock();
  83. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  84. {
  85. if (blocks[i])
  86. {
  87. blocks[i]->release();
  88. blocks[i] = 0;
  89. }
  90. }
  91. cs.unlock();
  92. isLoading = 1;
  93. Framework::Vec3<int> pos = {0, 0, 0};
  94. unsigned short id;
  95. zReader->lese((char*)&id, 2);
  96. int count = 0;
  97. while (id)
  98. {
  99. int index;
  100. zReader->lese((char*)&index, 4);
  101. pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE,
  102. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  103. index % WORLD_HEIGHT);
  104. if (blockTypes[id]->doesNeedInstance())
  105. {
  106. cs.lock();
  107. Block* b = blockTypes[id]->createBlock(
  108. {pos.x + location.x - CHUNK_SIZE / 2,
  109. pos.y + location.y - CHUNK_SIZE / 2,
  110. pos.z});
  111. blocks[index] = b;
  112. cs.unlock();
  113. vcs.lock();
  114. if (b->isVisible())
  115. {
  116. if (!blockTypes[id]->getModelInfo().getModelName().istGleich(
  117. "cube"))
  118. {
  119. b->tick(0);
  120. visibleBlocks.add(b);
  121. }
  122. }
  123. count++;
  124. vcs.unlock();
  125. }
  126. zReader->lese((char*)&id, 2);
  127. }
  128. std::cout << "Loaded " << count << " blocks\n";
  129. int index = 0;
  130. // light
  131. zReader->lese((char*)&index, 4);
  132. char lightData[6];
  133. while (index >= -1)
  134. {
  135. if (index == -1)
  136. {
  137. int x = 0;
  138. int y = 0;
  139. int z = 0;
  140. zReader->lese((char*)&x, 4);
  141. zReader->lese((char*)&y, 4);
  142. zReader->lese((char*)&z, 4);
  143. zReader->lese(lightData, 6);
  144. if (x == -1)
  145. {
  146. int cacheIndex = y * WORLD_HEIGHT + z;
  147. Block* zB = blocks[cacheIndex];
  148. if (zB)
  149. {
  150. zB->setLightData(WEST, (unsigned char*)lightData);
  151. }
  152. }
  153. else if (y == -1)
  154. {
  155. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  156. Block* zB = blocks[cacheIndex];
  157. if (zB)
  158. {
  159. zB->setLightData(NORTH, (unsigned char*)lightData);
  160. }
  161. }
  162. else if (x == CHUNK_SIZE)
  163. {
  164. int cacheIndex
  165. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  166. Block* zB = blocks[cacheIndex];
  167. if (zB)
  168. {
  169. zB->setLightData(EAST, (unsigned char*)lightData);
  170. }
  171. }
  172. else if (y == CHUNK_SIZE)
  173. {
  174. int cacheIndex
  175. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  176. Block* zB = blocks[cacheIndex];
  177. if (zB)
  178. {
  179. zB->setLightData(SOUTH, (unsigned char*)lightData);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. zReader->lese(lightData, 6);
  186. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  187. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  188. index % WORLD_HEIGHT);
  189. for (int i = 0; i < 6; i++)
  190. {
  191. Framework::Vec3<int> pos
  192. = location + getDirection(getDirectionFromIndex(i));
  193. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  194. {
  195. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  196. && pos.y < CHUNK_SIZE)
  197. {
  198. int cacheIndex
  199. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  200. + pos.z;
  201. Block* zB = blocks[cacheIndex];
  202. if (zB)
  203. {
  204. bool visible = zB->isVisible();
  205. zB->setLightData(
  206. getOppositeDirection(getDirectionFromIndex(i)),
  207. (unsigned char*)lightData);
  208. if (zB->isVisible() && !visible)
  209. {
  210. vcs.lock();
  211. zB->tick(0);
  212. visibleBlocks.add(zB);
  213. vcs.unlock();
  214. }
  215. }
  216. }
  217. else
  218. {
  219. pos.x += this->location.x - CHUNK_SIZE / 2;
  220. pos.y += this->location.y - CHUNK_SIZE / 2;
  221. Block* zB = World::INSTANCE->zBlockAt(pos);
  222. if (zB)
  223. {
  224. bool visible = zB->isVisible();
  225. zB->setLightData(
  226. getOppositeDirection(getDirectionFromIndex(i)),
  227. (unsigned char*)lightData);
  228. if (zB->isVisible() && !visible)
  229. {
  230. Chunk* c = World::INSTANCE->zChunk(
  231. World::INSTANCE->getChunkCenter(
  232. pos.x, pos.y));
  233. c->vcs.lock();
  234. zB->tick(0);
  235. c->visibleBlocks.add(zB);
  236. c->vcs.unlock();
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. zReader->lese((char*)&index, 4);
  244. }
  245. isLoading = 0;
  246. }
  247. void Chunk::buildGroundModel()
  248. {
  249. vcs.lock();
  250. lightChanged = 0;
  251. Model3DData* chunkModel = groundModel->zModelData();
  252. // remove old model
  253. while (chunkModel->getPolygonAnzahl() > 0)
  254. {
  255. chunkModel->removePolygon(0);
  256. }
  257. // calculate verticies
  258. Trie<GroundModelPart*> groundModelBuidler;
  259. Array<GroundModelPart*> groundPartArray;
  260. Vertex3D* groundVerticies = new Vertex3D[10000];
  261. __int64* lightBuffer = new __int64[10000];
  262. int groundVertexCount = 0;
  263. int groundVertexArraySize = 10000;
  264. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  265. {
  266. if (blocks[i]
  267. && blocks[i]->zBlockType()->getModelInfo().getModelName().istGleich(
  268. "cube"))
  269. {
  270. int index = 0;
  271. for (Text* textureName :
  272. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  273. {
  274. Framework::Vec3<int> location((i / WORLD_HEIGHT) / CHUNK_SIZE,
  275. (i / WORLD_HEIGHT) % CHUNK_SIZE,
  276. i % WORLD_HEIGHT);
  277. if (isPartOfGroundModel(location, index))
  278. {
  279. if (!groundModelBuidler.get(
  280. *textureName, textureName->getLength()))
  281. {
  282. GroundModelPart* part = new GroundModelPart();
  283. part->indexList = new int[10000];
  284. part->indexCount = 0;
  285. part->indexArraySize = 10000;
  286. part->name = *textureName;
  287. groundModelBuidler.set(
  288. *textureName, textureName->getLength(), part);
  289. groundPartArray.add(part);
  290. }
  291. GroundModelPart* part = groundModelBuidler.get(
  292. *textureName, textureName->getLength());
  293. const Vertex3D* vBuffer
  294. = blocks[i]->zModelData()->zVertexBuffer();
  295. Polygon3D* polygon
  296. = blocks[i]->zModelData()->getPolygon(index);
  297. if (part->indexCount + polygon->indexAnz
  298. > part->indexArraySize)
  299. {
  300. int* tmp = new int[part->indexArraySize + 10000];
  301. memcpy(tmp, part->indexList, part->indexCount * 4);
  302. delete[] part->indexList;
  303. part->indexList = tmp;
  304. part->indexArraySize += 10000;
  305. }
  306. if (groundVertexCount + polygon->indexAnz
  307. > groundVertexArraySize)
  308. {
  309. Vertex3D* tmp
  310. = new Vertex3D[groundVertexArraySize + 10000];
  311. memcpy(tmp,
  312. groundVerticies,
  313. groundVertexCount * sizeof(Vertex3D));
  314. delete[] groundVerticies;
  315. groundVerticies = tmp;
  316. groundVertexArraySize += 10000;
  317. __int64* lTmp = new __int64[groundVertexArraySize];
  318. memcpy(lTmp,
  319. lightBuffer,
  320. groundVertexCount * sizeof(__int64));
  321. delete[] lightBuffer;
  322. lightBuffer = lTmp;
  323. }
  324. for (int vi = 0; vi < polygon->indexAnz; vi++)
  325. {
  326. lightBuffer[groundVertexCount] = calculateLight(
  327. vBuffer[polygon->indexList[vi]].pos,
  328. location,
  329. getDirectionFromIndex(index));
  330. part->indexList[part->indexCount++] = groundVertexCount;
  331. groundVerticies[groundVertexCount++]
  332. = vBuffer[polygon->indexList[vi]];
  333. groundVerticies[groundVertexCount - 1].pos
  334. += blocks[i]->getPos()
  335. - Vec3<float>((float)this->location.x,
  336. (float)this->location.y,
  337. (float)WORLD_HEIGHT / 2.f);
  338. groundVerticies[groundVertexCount - 1].id
  339. = groundVertexCount - 1;
  340. }
  341. }
  342. index++;
  343. }
  344. }
  345. }
  346. Model3DTextur* textur = new Model3DTextur();
  347. int pi = 0;
  348. for (GroundModelPart* part : groundPartArray)
  349. {
  350. Polygon3D* polygon = new Polygon3D();
  351. polygon->indexAnz = part->indexCount;
  352. polygon->indexList = part->indexList;
  353. groundModel->zModelData()->addPolygon(polygon);
  354. textur->setPolygonTextur(pi,
  355. uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur(
  356. part->name));
  357. pi++;
  358. delete part;
  359. }
  360. groundModel->zModelData()->setVertecies(groundVerticies, groundVertexCount);
  361. groundModel->setModelTextur(textur);
  362. groundModel->setVertexLightBuffer(lightBuffer, groundVertexCount);
  363. vcs.unlock();
  364. }
  365. void Chunk::updateGroundLight()
  366. {
  367. vcs.lock();
  368. __int64* lightBuffer = groundModel->zLightBuffer();
  369. int groundVertexCount = 0;
  370. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  371. {
  372. if (blocks[i]
  373. && blocks[i]->zBlockType()->getModelInfo().getModelName().istGleich(
  374. "cube"))
  375. {
  376. int index = 0;
  377. for (Text* textureName :
  378. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  379. {
  380. Framework::Vec3<int> location((i / WORLD_HEIGHT) / CHUNK_SIZE,
  381. (i / WORLD_HEIGHT) % CHUNK_SIZE,
  382. i % WORLD_HEIGHT);
  383. if (isPartOfGroundModel(location, index))
  384. {
  385. const Vertex3D* vBuffer
  386. = blocks[i]->zModelData()->zVertexBuffer();
  387. Polygon3D* polygon
  388. = blocks[i]->zModelData()->getPolygon(index);
  389. for (int vi = 0; vi < polygon->indexAnz; vi++)
  390. {
  391. lightBuffer[groundVertexCount++] = calculateLight(
  392. vBuffer[polygon->indexList[vi]].pos,
  393. location,
  394. getDirectionFromIndex(index));
  395. }
  396. }
  397. index++;
  398. }
  399. }
  400. }
  401. groundModel->copyLightToGPU();
  402. vcs.unlock();
  403. }
  404. __int64 Chunk::calculateLight(
  405. Vec3<float> vertexPos, Vec3<int> blockPos, Direction direction)
  406. {
  407. __int64 result = 0;
  408. int sumCount = 1;
  409. short lightSum[6];
  410. Block* current = blocks[index(blockPos)];
  411. const unsigned char* light = current->getLightData(direction);
  412. for (int i = 0; i < 6; i++)
  413. {
  414. lightSum[i] = (short)light[i];
  415. }
  416. Vec3<int> vertexDirs(vertexPos.x < 0 ? -1 : 1,
  417. vertexPos.y < 0 ? -1 : 1,
  418. vertexPos.z < 0 ? -1 : 1);
  419. Directions dirs = getDirectionsFromVector(vertexDirs) & ~direction;
  420. Vec3<int> neighborDirs[3];
  421. int neighborIndex = 0;
  422. for (int i = 0; i < 6; i++)
  423. {
  424. Direction dir = getDirectionFromIndex(i);
  425. if ((dirs | dir) == dirs)
  426. {
  427. neighborDirs[neighborIndex++] = getDirection(dir);
  428. if (neighborIndex == 2) break;
  429. }
  430. }
  431. neighborDirs[2] = neighborDirs[0] + neighborDirs[1];
  432. for (int i = 0; i < 3; i++)
  433. {
  434. neighborDirs[i] += blockPos;
  435. if (neighborDirs[i].x >= 0 && neighborDirs[i].y >= 0
  436. && neighborDirs[i].z >= 0 && neighborDirs[i].x < CHUNK_SIZE
  437. && neighborDirs[i].y < CHUNK_SIZE
  438. && neighborDirs[i].z < WORLD_HEIGHT)
  439. {
  440. int neighborIndex = index(neighborDirs[i]);
  441. Block* neighbor = blocks[neighborIndex];
  442. if (neighbor)
  443. {
  444. const unsigned char* neighborLight
  445. = neighbor->getLightData(direction);
  446. if ((neighborLight[0] | neighborLight[1] | neighborLight[2]
  447. | neighborLight[3] | neighborLight[4]
  448. | neighborLight[5])
  449. != 0)
  450. {
  451. sumCount++;
  452. for (int j = 0; j < 6; j++)
  453. {
  454. lightSum[j] += (short)neighborLight[j];
  455. }
  456. }
  457. }
  458. }
  459. else
  460. { // TODO: get light from neighbor chunk
  461. }
  462. }
  463. for (int i = 0; i < 6; i++)
  464. {
  465. lightSum[i] = (lightSum[i] / sumCount) & 0xFF;
  466. }
  467. result = ((__int64)lightSum[0] << 24) | ((__int64)lightSum[1] << 16)
  468. | ((__int64)lightSum[2] << 8) | ((__int64)lightSum[3] << 56)
  469. | ((__int64)lightSum[4] << 48) | ((__int64)lightSum[5] << 40);
  470. return result;
  471. }
  472. bool Chunk::isPartOfGroundModel(
  473. Framework::Vec3<int> location, int directionIndex)
  474. {
  475. Framework::Vec3<int> neighborLocation
  476. = location + getDirection(getDirectionFromIndex(directionIndex));
  477. bool needed = 0;
  478. if (neighborLocation.x < 0 || neighborLocation.y < 0
  479. || neighborLocation.z < 0 || neighborLocation.x >= CHUNK_SIZE
  480. || neighborLocation.y >= CHUNK_SIZE
  481. || neighborLocation.z >= WORLD_HEIGHT)
  482. {
  483. needed = 1;
  484. }
  485. else
  486. {
  487. int naighborIndex = index(neighborLocation);
  488. if (!blocks[naighborIndex]
  489. || !blocks[naighborIndex]
  490. ->zBlockType()
  491. ->getModelInfo()
  492. .getModelName()
  493. .istGleich("cube"))
  494. {
  495. needed = 1;
  496. }
  497. }
  498. return needed;
  499. }
  500. void Chunk::destroy()
  501. {
  502. Model3DData* chunkModel = groundModel->zModelData();
  503. // remove old model
  504. while (chunkModel->getPolygonAnzahl() > 0)
  505. {
  506. chunkModel->removePolygon(0);
  507. }
  508. chunkModel->setVertecies(0, 0);
  509. }
  510. void Chunk::api(char* message)
  511. {
  512. switch (message[0])
  513. {
  514. case 0: // set block
  515. {
  516. int index = *(int*)(message + 1);
  517. int id = *(int*)(message + 5);
  518. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  519. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  520. index % WORLD_HEIGHT);
  521. location.x += this->location.x - CHUNK_SIZE / 2;
  522. location.y += this->location.y - CHUNK_SIZE / 2;
  523. if (blockTypes[id]->doesNeedInstance())
  524. {
  525. Block* zB = blockTypes[id]->createBlock(location);
  526. setBlock(zB);
  527. }
  528. else
  529. {
  530. Block* zB = zBlockAt(location);
  531. if (zB) removeBlock(zB);
  532. }
  533. break;
  534. }
  535. case 1: // animate block
  536. {
  537. int index = *(int*)(message + 1);
  538. int boneId = *(int*)(message + 5);
  539. double time = *(double*)(message + 9);
  540. Framework::Vec3<float> pos;
  541. pos.x = *(float*)(message + 17);
  542. pos.y = *(float*)(message + 21);
  543. pos.z = *(float*)(message + 25);
  544. Framework::Vec3<float> rot;
  545. rot.x = *(float*)(message + 29);
  546. rot.y = *(float*)(message + 33);
  547. rot.z = *(float*)(message + 37);
  548. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  549. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  550. index % WORLD_HEIGHT);
  551. location.x += this->location.x - CHUNK_SIZE / 2;
  552. location.y += this->location.y - CHUNK_SIZE / 2;
  553. Block* zB = zBlockAt(location);
  554. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  555. break;
  556. }
  557. }
  558. }
  559. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  560. {
  561. location.x = location.x % CHUNK_SIZE;
  562. location.y = location.y % CHUNK_SIZE;
  563. if (location.x < 0) location.x += CHUNK_SIZE;
  564. if (location.y < 0) location.y += CHUNK_SIZE;
  565. int index
  566. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  567. return blocks[index];
  568. }
  569. void Chunk::setBlock(Block* block)
  570. {
  571. cs.lock();
  572. Framework::Vec3<int> pos = block->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. bool newAffectsGround
  579. = block
  580. && block->zBlockType()->getModelInfo().getModelName().istGleich("cube");
  581. bool affectsGround
  582. = blocks[index]
  583. && blocks[index]->zBlockType()->getModelInfo().getModelName().istGleich(
  584. "cube");
  585. if (blocks[index])
  586. {
  587. vcs.lock();
  588. for (Framework::Iterator<Block*> vi = visibleBlocks.begin(); vi; vi++)
  589. {
  590. if (blocks[index] == (Block*)vi)
  591. {
  592. vi.remove();
  593. break;
  594. }
  595. }
  596. vcs.unlock();
  597. blocks[index]->copyLightTo(block);
  598. blocks[index]->release();
  599. blocks[index] = block;
  600. cs.unlock();
  601. vcs.lock();
  602. if (affectsGround || newAffectsGround)
  603. {
  604. modelChanged = 1;
  605. }
  606. if (block && block->isVisible() && !newAffectsGround)
  607. {
  608. block->tick(0);
  609. visibleBlocks.add(block);
  610. }
  611. vcs.unlock();
  612. return;
  613. }
  614. blocks[index] = block;
  615. cs.unlock();
  616. vcs.lock();
  617. if (affectsGround || newAffectsGround)
  618. {
  619. modelChanged = 1;
  620. }
  621. if (block && block->isVisible() && !newAffectsGround)
  622. {
  623. block->tick(0);
  624. visibleBlocks.add(block);
  625. }
  626. vcs.unlock();
  627. }
  628. void Chunk::removeBlock(Block* zBlock)
  629. {
  630. cs.lock();
  631. vcs.lock();
  632. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator;
  633. iterator++)
  634. {
  635. if (zBlock == (Block*)iterator)
  636. {
  637. iterator.remove();
  638. break;
  639. }
  640. }
  641. vcs.unlock();
  642. Vec3<int> pos = zBlock->getLocation();
  643. pos.x = pos.x % CHUNK_SIZE;
  644. pos.y = pos.y % CHUNK_SIZE;
  645. if (pos.x < 0) pos.x += CHUNK_SIZE;
  646. if (pos.y < 0) pos.y += CHUNK_SIZE;
  647. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  648. if (blocks[index])
  649. {
  650. bool affectsGround = blocks[index]
  651. ->zBlockType()
  652. ->getModelInfo()
  653. .getModelName()
  654. .istGleich("cube");
  655. blocks[index]->release();
  656. blocks[index] = 0;
  657. if (affectsGround) modelChanged = 1;
  658. }
  659. cs.unlock();
  660. }
  661. void Chunk::blockVisibilityChanged(Block* zB)
  662. {
  663. vcs.lock();
  664. if (zB->isVisible())
  665. {
  666. zB->tick(0);
  667. visibleBlocks.add(zB);
  668. }
  669. else
  670. {
  671. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin();
  672. iterator;
  673. iterator++)
  674. {
  675. if (zB == (Block*)iterator)
  676. {
  677. iterator.remove();
  678. break;
  679. }
  680. }
  681. }
  682. vcs.unlock();
  683. }
  684. Framework::Punkt Chunk::getCenter() const
  685. {
  686. return location;
  687. }
  688. Framework::Vec3<int> Chunk::getMin() const
  689. {
  690. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  691. }
  692. Framework::Vec3<int> Chunk::getMax() const
  693. {
  694. return {
  695. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  696. }
  697. void Chunk::forAll(std::function<void(Model3D*)> f)
  698. {
  699. vcs.lock();
  700. f(groundModel);
  701. float dist = 0.f;
  702. CustomDX11API* api
  703. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  704. if (api->isInFrustrum(groundModel->getPos(),
  705. (CHUNK_SIZE / 2.f, CHUNK_SIZE / 2.f, WORLD_HEIGHT / 2.f),
  706. &dist))
  707. {
  708. api->setCullBack(false);
  709. //int index = 0;
  710. //int filter = 1 + (int)(dist / 100.f);
  711. for (Block* b : visibleBlocks)
  712. {
  713. // if (index % filter == 0)
  714. // {
  715. f(b);
  716. // }
  717. // index++;
  718. }
  719. api->setCullBack(true);
  720. }
  721. vcs.unlock();
  722. }
  723. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  724. {
  725. acs.lock();
  726. vcs.lock(); // TODO: enshure no dead lock occures
  727. if (modelChanged)
  728. {
  729. modelChanged = 0;
  730. buildGroundModel();
  731. }
  732. if (lightChanged)
  733. {
  734. lightChanged = 0;
  735. updateGroundLight();
  736. }
  737. bool res = groundModel->tick(time);
  738. auto iterator = animations.begin();
  739. while (iterator)
  740. {
  741. if (iterator->tick(time))
  742. {
  743. res |= iterator->zBlock()->tick(time);
  744. if (iterator->isFinished())
  745. {
  746. iterator.remove();
  747. continue;
  748. }
  749. }
  750. else
  751. {
  752. iterator.remove();
  753. continue;
  754. }
  755. ++iterator;
  756. }
  757. vcs.unlock();
  758. acs.unlock();
  759. return 1;
  760. }
  761. void Chunk::setLightChanged()
  762. {
  763. lightChanged = 1;
  764. }