Chunk.cpp 25 KB

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