Chunk.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. && !blockTypes[id]->getModelInfo().getModelName().istGleich(
  119. "grass"))
  120. {
  121. b->tick(0);
  122. visibleBlocks.add(b);
  123. }
  124. }
  125. count++;
  126. vcs.unlock();
  127. }
  128. zReader->lese((char*)&id, 2);
  129. }
  130. std::cout << "Loaded " << count << " blocks\n";
  131. int index = 0;
  132. // light
  133. zReader->lese((char*)&index, 4);
  134. char lightData[6];
  135. while (index >= -1)
  136. {
  137. if (index == -1)
  138. {
  139. int x = 0;
  140. int y = 0;
  141. int z = 0;
  142. zReader->lese((char*)&x, 4);
  143. zReader->lese((char*)&y, 4);
  144. zReader->lese((char*)&z, 4);
  145. zReader->lese(lightData, 6);
  146. if (x == -1)
  147. {
  148. int cacheIndex = y * WORLD_HEIGHT + z;
  149. Block* zB = blocks[cacheIndex];
  150. if (zB)
  151. {
  152. zB->setLightData(WEST, (unsigned char*)lightData);
  153. }
  154. }
  155. else if (y == -1)
  156. {
  157. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  158. Block* zB = blocks[cacheIndex];
  159. if (zB)
  160. {
  161. zB->setLightData(NORTH, (unsigned char*)lightData);
  162. }
  163. }
  164. else if (x == CHUNK_SIZE)
  165. {
  166. int cacheIndex
  167. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  168. Block* zB = blocks[cacheIndex];
  169. if (zB)
  170. {
  171. zB->setLightData(EAST, (unsigned char*)lightData);
  172. }
  173. }
  174. else if (y == CHUNK_SIZE)
  175. {
  176. int cacheIndex
  177. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  178. Block* zB = blocks[cacheIndex];
  179. if (zB)
  180. {
  181. zB->setLightData(SOUTH, (unsigned char*)lightData);
  182. }
  183. }
  184. }
  185. else
  186. {
  187. zReader->lese(lightData, 6);
  188. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  189. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  190. index % WORLD_HEIGHT);
  191. for (int i = 0; i < 6; i++)
  192. {
  193. Framework::Vec3<int> pos
  194. = location + getDirection(getDirectionFromIndex(i));
  195. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  196. {
  197. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  198. && pos.y < CHUNK_SIZE)
  199. {
  200. int cacheIndex
  201. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  202. + pos.z;
  203. Block* zB = blocks[cacheIndex];
  204. if (zB)
  205. {
  206. bool visible = zB->isVisible();
  207. zB->setLightData(
  208. getOppositeDirection(getDirectionFromIndex(i)),
  209. (unsigned char*)lightData);
  210. if (zB->isVisible() && !visible)
  211. {
  212. vcs.lock();
  213. zB->tick(0);
  214. visibleBlocks.add(zB);
  215. vcs.unlock();
  216. }
  217. }
  218. }
  219. else
  220. {
  221. pos.x += this->location.x - CHUNK_SIZE / 2;
  222. pos.y += this->location.y - CHUNK_SIZE / 2;
  223. Block* zB = World::INSTANCE->zBlockAt(pos);
  224. if (zB)
  225. {
  226. bool visible = zB->isVisible();
  227. zB->setLightData(
  228. getOppositeDirection(getDirectionFromIndex(i)),
  229. (unsigned char*)lightData);
  230. if (zB->isVisible() && !visible)
  231. {
  232. Chunk* c = World::INSTANCE->zChunk(
  233. World::INSTANCE->getChunkCenter(
  234. pos.x, pos.y));
  235. c->vcs.lock();
  236. zB->tick(0);
  237. c->visibleBlocks.add(zB);
  238. c->vcs.unlock();
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. zReader->lese((char*)&index, 4);
  246. }
  247. isLoading = 0;
  248. }
  249. void Chunk::buildGroundModel()
  250. {
  251. vcs.lock();
  252. visibleBlocks.leeren();
  253. lightChanged = 0;
  254. Model3DData* chunkModel = groundModel->zModelData();
  255. // remove old model
  256. while (chunkModel->getPolygonAnzahl() > 0)
  257. {
  258. chunkModel->removePolygon(0);
  259. }
  260. // calculate verticies
  261. Trie<GroundModelPart*> groundModelBuidler;
  262. Array<GroundModelPart*> groundPartArray;
  263. Vertex3D* groundVerticies = new Vertex3D[10000];
  264. __int64* lightBuffer = new __int64[10000];
  265. int groundVertexCount = 0;
  266. int groundVertexArraySize = 10000;
  267. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  268. {
  269. if (blocks[i])
  270. {
  271. if (blocks[i]
  272. ->zBlockType()
  273. ->getModelInfo()
  274. .getModelName()
  275. .istGleich("cube"))
  276. {
  277. blocks[i]->setPartOfGround(1);
  278. int index = 0;
  279. for (Text* textureName :
  280. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  281. {
  282. Framework::Vec3<int> location(
  283. (i / WORLD_HEIGHT) / CHUNK_SIZE,
  284. (i / WORLD_HEIGHT) % CHUNK_SIZE,
  285. i % WORLD_HEIGHT);
  286. if (isPartOfGroundModel(location, index))
  287. {
  288. if (!groundModelBuidler.get(
  289. *textureName, textureName->getLength()))
  290. {
  291. GroundModelPart* part = new GroundModelPart();
  292. part->indexList = new int[10000];
  293. part->indexCount = 0;
  294. part->indexArraySize = 10000;
  295. part->name = *textureName;
  296. groundModelBuidler.set(
  297. *textureName, textureName->getLength(), part);
  298. groundPartArray.add(part);
  299. }
  300. GroundModelPart* part = groundModelBuidler.get(
  301. *textureName, textureName->getLength());
  302. const Vertex3D* vBuffer
  303. = blocks[i]->zModelData()->zVertexBuffer();
  304. Polygon3D* polygon
  305. = blocks[i]->zModelData()->getPolygon(index);
  306. if (part->indexCount + polygon->indexAnz
  307. > part->indexArraySize)
  308. {
  309. int* tmp = new int[part->indexArraySize + 10000];
  310. memcpy(tmp, part->indexList, part->indexCount * 4);
  311. delete[] part->indexList;
  312. part->indexList = tmp;
  313. part->indexArraySize += 10000;
  314. }
  315. if (groundVertexCount + polygon->indexAnz
  316. > groundVertexArraySize)
  317. {
  318. Vertex3D* tmp
  319. = new Vertex3D[groundVertexArraySize + 10000];
  320. memcpy(tmp,
  321. groundVerticies,
  322. groundVertexCount * sizeof(Vertex3D));
  323. delete[] groundVerticies;
  324. groundVerticies = tmp;
  325. groundVertexArraySize += 10000;
  326. __int64* lTmp = new __int64[groundVertexArraySize];
  327. memcpy(lTmp,
  328. lightBuffer,
  329. groundVertexCount * sizeof(__int64));
  330. delete[] lightBuffer;
  331. lightBuffer = lTmp;
  332. }
  333. for (int vi = 0; vi < polygon->indexAnz; vi++)
  334. {
  335. lightBuffer[groundVertexCount] = calculateLight(
  336. vBuffer[polygon->indexList[vi]].pos,
  337. location,
  338. getDirectionFromIndex(index));
  339. part->indexList[part->indexCount++]
  340. = groundVertexCount;
  341. groundVerticies[groundVertexCount++]
  342. = vBuffer[polygon->indexList[vi]];
  343. groundVerticies[groundVertexCount - 1].pos
  344. += blocks[i]->getPos()
  345. - Vec3<float>((float)this->location.x,
  346. (float)this->location.y,
  347. (float)WORLD_HEIGHT / 2.f);
  348. groundVerticies[groundVertexCount - 1].id
  349. = groundVertexCount - 1;
  350. }
  351. }
  352. index++;
  353. }
  354. }
  355. else if (blocks[i]
  356. ->zBlockType()
  357. ->getModelInfo()
  358. .getModelName()
  359. .istGleich("grass"))
  360. {
  361. blocks[i]->setPartOfGround(1);
  362. __int64 light = blocks[i]->getMaxLight();
  363. int index = 0;
  364. for (Text* textureName :
  365. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  366. {
  367. if (!groundModelBuidler.get(
  368. *textureName, textureName->getLength()))
  369. {
  370. GroundModelPart* part = new GroundModelPart();
  371. part->indexList = new int[10000];
  372. part->indexCount = 0;
  373. part->indexArraySize = 10000;
  374. part->name = *textureName;
  375. groundModelBuidler.set(
  376. *textureName, textureName->getLength(), part);
  377. groundPartArray.add(part);
  378. }
  379. GroundModelPart* part = groundModelBuidler.get(
  380. *textureName, textureName->getLength());
  381. const Vertex3D* vBuffer
  382. = blocks[i]->zModelData()->zVertexBuffer();
  383. Polygon3D* polygon
  384. = blocks[i]->zModelData()->getPolygon(index);
  385. if (part->indexCount + polygon->indexAnz
  386. > part->indexArraySize)
  387. {
  388. int* tmp = new int[part->indexArraySize + 10000];
  389. memcpy(tmp, part->indexList, part->indexCount * 4);
  390. delete[] part->indexList;
  391. part->indexList = tmp;
  392. part->indexArraySize += 10000;
  393. }
  394. if (groundVertexCount + polygon->indexAnz
  395. > groundVertexArraySize)
  396. {
  397. Vertex3D* tmp
  398. = new Vertex3D[groundVertexArraySize + 10000];
  399. memcpy(tmp,
  400. groundVerticies,
  401. groundVertexCount * sizeof(Vertex3D));
  402. delete[] groundVerticies;
  403. groundVerticies = tmp;
  404. groundVertexArraySize += 10000;
  405. __int64* lTmp = new __int64[groundVertexArraySize];
  406. memcpy(lTmp,
  407. lightBuffer,
  408. groundVertexCount * sizeof(__int64));
  409. delete[] lightBuffer;
  410. lightBuffer = lTmp;
  411. }
  412. for (int vi = 0; vi < polygon->indexAnz; vi++)
  413. {
  414. lightBuffer[groundVertexCount] = light;
  415. part->indexList[part->indexCount++] = groundVertexCount;
  416. groundVerticies[groundVertexCount++]
  417. = vBuffer[polygon->indexList[vi]];
  418. groundVerticies[groundVertexCount - 1].pos
  419. += blocks[i]->getPos()
  420. - Vec3<float>((float)this->location.x,
  421. (float)this->location.y,
  422. (float)WORLD_HEIGHT / 2.f);
  423. groundVerticies[groundVertexCount - 1].id
  424. = groundVertexCount - 1;
  425. }
  426. index++;
  427. }
  428. }
  429. else
  430. {
  431. blocks[i]->setPartOfGround(0);
  432. visibleBlocks.add(blocks[i]);
  433. }
  434. }
  435. }
  436. Model3DTextur* textur = new Model3DTextur();
  437. int pi = 0;
  438. for (GroundModelPart* part : groundPartArray)
  439. {
  440. Polygon3D* polygon = new Polygon3D();
  441. polygon->indexAnz = part->indexCount;
  442. polygon->indexList = part->indexList;
  443. groundModel->zModelData()->addPolygon(polygon);
  444. textur->setPolygonTextur(pi,
  445. uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur(
  446. part->name));
  447. pi++;
  448. delete part;
  449. }
  450. groundModel->zModelData()->setVertecies(groundVerticies, groundVertexCount);
  451. groundModel->setModelTextur(textur);
  452. groundModel->setVertexLightBuffer(lightBuffer, groundVertexCount);
  453. vcs.unlock();
  454. }
  455. void Chunk::updateGroundLight()
  456. {
  457. vcs.lock();
  458. __int64* lightBuffer = groundModel->zLightBuffer();
  459. int groundVertexCount = 0;
  460. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE * WORLD_HEIGHT; i++)
  461. {
  462. if (blocks[i])
  463. {
  464. if (blocks[i]
  465. ->zBlockType()
  466. ->getModelInfo()
  467. .getModelName()
  468. .istGleich("cube"))
  469. {
  470. int index = 0;
  471. for (Text* textureName :
  472. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  473. {
  474. Framework::Vec3<int> location(
  475. (i / WORLD_HEIGHT) / CHUNK_SIZE,
  476. (i / WORLD_HEIGHT) % CHUNK_SIZE,
  477. i % WORLD_HEIGHT);
  478. if (isPartOfGroundModel(location, index))
  479. {
  480. const Vertex3D* vBuffer
  481. = blocks[i]->zModelData()->zVertexBuffer();
  482. Polygon3D* polygon
  483. = blocks[i]->zModelData()->getPolygon(index);
  484. for (int vi = 0; vi < polygon->indexAnz; vi++)
  485. {
  486. lightBuffer[groundVertexCount++] = calculateLight(
  487. vBuffer[polygon->indexList[vi]].pos,
  488. location,
  489. getDirectionFromIndex(index));
  490. }
  491. }
  492. index++;
  493. }
  494. }
  495. else if (blocks[i]
  496. ->zBlockType()
  497. ->getModelInfo()
  498. .getModelName()
  499. .istGleich("grass"))
  500. {
  501. __int64 light = blocks[i]->getMaxLight();
  502. int index = 0;
  503. for (Text* textureName :
  504. *blocks[i]->zBlockType()->getModelInfo().getTexturNames())
  505. {
  506. const Vertex3D* vBuffer
  507. = blocks[i]->zModelData()->zVertexBuffer();
  508. Polygon3D* polygon
  509. = blocks[i]->zModelData()->getPolygon(index);
  510. for (int vi = 0; vi < polygon->indexAnz; vi++)
  511. {
  512. lightBuffer[groundVertexCount++] = light;
  513. }
  514. }
  515. }
  516. }
  517. }
  518. groundModel->copyLightToGPU();
  519. vcs.unlock();
  520. }
  521. __int64 Chunk::calculateLight(
  522. Vec3<float> vertexPos, Vec3<int> blockPos, Direction direction)
  523. {
  524. __int64 result = 0;
  525. int sumCount = 1;
  526. short lightSum[6];
  527. Block* current = blocks[index(blockPos)];
  528. const unsigned char* light = current->getLightData(direction);
  529. for (int i = 0; i < 6; i++)
  530. {
  531. lightSum[i] = (short)light[i];
  532. }
  533. Vec3<int> vertexDirs(vertexPos.x < 0 ? -1 : 1,
  534. vertexPos.y < 0 ? -1 : 1,
  535. vertexPos.z < 0 ? -1 : 1);
  536. Directions dirs = getDirectionsFromVector(vertexDirs) & ~direction;
  537. Vec3<int> neighborDirs[3];
  538. int neighborIndex = 0;
  539. for (int i = 0; i < 6; i++)
  540. {
  541. Direction dir = getDirectionFromIndex(i);
  542. if ((dirs | dir) == dirs)
  543. {
  544. neighborDirs[neighborIndex++] = getDirection(dir);
  545. if (neighborIndex == 2) break;
  546. }
  547. }
  548. neighborDirs[2] = neighborDirs[0] + neighborDirs[1];
  549. for (int i = 0; i < 3; i++)
  550. {
  551. neighborDirs[i] += blockPos;
  552. if (neighborDirs[i].x >= 0 && neighborDirs[i].y >= 0
  553. && neighborDirs[i].z >= 0 && neighborDirs[i].x < CHUNK_SIZE
  554. && neighborDirs[i].y < CHUNK_SIZE
  555. && neighborDirs[i].z < WORLD_HEIGHT)
  556. {
  557. int neighborIndex = index(neighborDirs[i]);
  558. Block* neighbor = blocks[neighborIndex];
  559. if (neighbor)
  560. {
  561. const unsigned char* neighborLight
  562. = neighbor->getLightData(direction);
  563. if ((neighborLight[0] | neighborLight[1] | neighborLight[2]
  564. | neighborLight[3] | neighborLight[4]
  565. | neighborLight[5])
  566. != 0)
  567. {
  568. sumCount++;
  569. for (int j = 0; j < 6; j++)
  570. {
  571. lightSum[j] += (short)neighborLight[j];
  572. }
  573. }
  574. }
  575. }
  576. else
  577. { // TODO: get light from neighbor chunk
  578. }
  579. }
  580. for (int i = 0; i < 6; i++)
  581. {
  582. lightSum[i] = (lightSum[i] / sumCount) & 0xFF;
  583. }
  584. result = ((__int64)lightSum[0] << 24) | ((__int64)lightSum[1] << 16)
  585. | ((__int64)lightSum[2] << 8) | ((__int64)lightSum[3] << 56)
  586. | ((__int64)lightSum[4] << 48) | ((__int64)lightSum[5] << 40);
  587. return result;
  588. }
  589. bool Chunk::isPartOfGroundModel(
  590. Framework::Vec3<int> location, int directionIndex)
  591. {
  592. Framework::Vec3<int> neighborLocation
  593. = location + getDirection(getDirectionFromIndex(directionIndex));
  594. bool needed = 0;
  595. if (neighborLocation.x < 0 || neighborLocation.y < 0
  596. || neighborLocation.z < 0 || neighborLocation.x >= CHUNK_SIZE
  597. || neighborLocation.y >= CHUNK_SIZE
  598. || neighborLocation.z >= WORLD_HEIGHT)
  599. {
  600. needed = 1;
  601. }
  602. else
  603. {
  604. int naighborIndex = index(neighborLocation);
  605. if (!blocks[naighborIndex]
  606. || !blocks[naighborIndex]
  607. ->zBlockType()
  608. ->getModelInfo()
  609. .getModelName()
  610. .istGleich("cube"))
  611. {
  612. needed = 1;
  613. }
  614. }
  615. return needed;
  616. }
  617. void Chunk::renderSolid(std::function<void(Model3D*)> f)
  618. {
  619. vcs.lock();
  620. CustomDX11API* api
  621. = (CustomDX11API*)uiFactory.initParam.bildschirm->zGraphicsApi();
  622. api->setCullBack(false);
  623. f(groundModel);
  624. api->setCullBack(true);
  625. float dist = 0.f;
  626. if (api->isInFrustrum(groundModel->getPos(),
  627. (CHUNK_SIZE / 2.f, CHUNK_SIZE / 2.f, WORLD_HEIGHT / 2.f),
  628. &dist))
  629. {
  630. for (Block* b : visibleBlocks)
  631. {
  632. f(b);
  633. }
  634. }
  635. vcs.unlock();
  636. }
  637. void Chunk::renderTransparent(std::function<void(Model3D*)> f) {}
  638. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  639. {
  640. acs.lock();
  641. vcs.lock(); // TODO: enshure no dead lock occures
  642. if (modelChanged)
  643. {
  644. modelChanged = 0;
  645. buildGroundModel();
  646. }
  647. if (lightChanged)
  648. {
  649. lightChanged = 0;
  650. updateGroundLight();
  651. }
  652. bool res = groundModel->tick(time);
  653. auto iterator = animations.begin();
  654. while (iterator)
  655. {
  656. if (iterator->tick(time))
  657. {
  658. res |= iterator->zBlock()->tick(time);
  659. if (iterator->isFinished())
  660. {
  661. iterator.remove();
  662. continue;
  663. }
  664. }
  665. else
  666. {
  667. iterator.remove();
  668. continue;
  669. }
  670. ++iterator;
  671. }
  672. vcs.unlock();
  673. acs.unlock();
  674. return 1;
  675. }
  676. void Chunk::destroy()
  677. {
  678. Model3DData* chunkModel = groundModel->zModelData();
  679. // remove old model
  680. while (chunkModel->getPolygonAnzahl() > 0)
  681. {
  682. chunkModel->removePolygon(0);
  683. }
  684. chunkModel->setVertecies(0, 0);
  685. }
  686. void Chunk::api(char* message)
  687. {
  688. switch (message[0])
  689. {
  690. case 0: // set block
  691. {
  692. int index = *(int*)(message + 1);
  693. int id = *(int*)(message + 5);
  694. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  695. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  696. index % WORLD_HEIGHT);
  697. location.x += this->location.x - CHUNK_SIZE / 2;
  698. location.y += this->location.y - CHUNK_SIZE / 2;
  699. if (blockTypes[id]->doesNeedInstance())
  700. {
  701. Block* zB = blockTypes[id]->createBlock(location);
  702. setBlock(zB);
  703. }
  704. else
  705. {
  706. Block* zB = zBlockAt(location);
  707. if (zB) removeBlock(zB);
  708. }
  709. break;
  710. }
  711. case 1: // animate block
  712. {
  713. int index = *(int*)(message + 1);
  714. int boneId = *(int*)(message + 5);
  715. double time = *(double*)(message + 9);
  716. Framework::Vec3<float> pos;
  717. pos.x = *(float*)(message + 17);
  718. pos.y = *(float*)(message + 21);
  719. pos.z = *(float*)(message + 25);
  720. Framework::Vec3<float> rot;
  721. rot.x = *(float*)(message + 29);
  722. rot.y = *(float*)(message + 33);
  723. rot.z = *(float*)(message + 37);
  724. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  725. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  726. index % WORLD_HEIGHT);
  727. location.x += this->location.x - CHUNK_SIZE / 2;
  728. location.y += this->location.y - CHUNK_SIZE / 2;
  729. Block* zB = zBlockAt(location);
  730. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  731. break;
  732. }
  733. }
  734. }
  735. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  736. {
  737. location.x = location.x % CHUNK_SIZE;
  738. location.y = location.y % CHUNK_SIZE;
  739. if (location.x < 0) location.x += CHUNK_SIZE;
  740. if (location.y < 0) location.y += CHUNK_SIZE;
  741. int index
  742. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  743. return blocks[index];
  744. }
  745. void Chunk::setBlock(Block* block)
  746. {
  747. cs.lock();
  748. Framework::Vec3<int> pos = block->getLocation();
  749. pos.x = pos.x % CHUNK_SIZE;
  750. pos.y = pos.y % CHUNK_SIZE;
  751. if (pos.x < 0) pos.x += CHUNK_SIZE;
  752. if (pos.y < 0) pos.y += CHUNK_SIZE;
  753. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  754. bool newAffectsGround
  755. = block
  756. && (block->zBlockType()->getModelInfo().getModelName().istGleich("cube")
  757. || block->zBlockType()->getModelInfo().getModelName().istGleich(
  758. "grass"));
  759. bool affectsGround
  760. = blocks[index]
  761. && (blocks[index]->zBlockType()->getModelInfo().getModelName().istGleich(
  762. "cube")
  763. || blocks[index]
  764. ->zBlockType()
  765. ->getModelInfo()
  766. .getModelName()
  767. .istGleich("grass"));
  768. if (blocks[index])
  769. {
  770. vcs.lock();
  771. for (Framework::Iterator<Block*> vi = visibleBlocks.begin(); vi; vi++)
  772. {
  773. if (blocks[index] == (Block*)vi)
  774. {
  775. vi.remove();
  776. break;
  777. }
  778. }
  779. vcs.unlock();
  780. blocks[index]->copyLightTo(block);
  781. blocks[index]->release();
  782. blocks[index] = block;
  783. cs.unlock();
  784. vcs.lock();
  785. if (affectsGround || newAffectsGround)
  786. {
  787. modelChanged = 1;
  788. }
  789. if (block && block->isVisible() && !newAffectsGround)
  790. {
  791. block->tick(0);
  792. visibleBlocks.add(block);
  793. }
  794. vcs.unlock();
  795. return;
  796. }
  797. blocks[index] = block;
  798. cs.unlock();
  799. vcs.lock();
  800. if (affectsGround || newAffectsGround)
  801. {
  802. modelChanged = 1;
  803. }
  804. if (block && block->isVisible() && !newAffectsGround)
  805. {
  806. block->tick(0);
  807. visibleBlocks.add(block);
  808. }
  809. vcs.unlock();
  810. }
  811. void Chunk::removeBlock(Block* zBlock)
  812. {
  813. cs.lock();
  814. vcs.lock();
  815. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator;
  816. iterator++)
  817. {
  818. if (zBlock == (Block*)iterator)
  819. {
  820. iterator.remove();
  821. break;
  822. }
  823. }
  824. vcs.unlock();
  825. Vec3<int> pos = zBlock->getLocation();
  826. pos.x = pos.x % CHUNK_SIZE;
  827. pos.y = pos.y % CHUNK_SIZE;
  828. if (pos.x < 0) pos.x += CHUNK_SIZE;
  829. if (pos.y < 0) pos.y += CHUNK_SIZE;
  830. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  831. if (blocks[index])
  832. {
  833. bool affectsGround = blocks[index]
  834. ->zBlockType()
  835. ->getModelInfo()
  836. .getModelName()
  837. .istGleich("cube")
  838. || blocks[index]
  839. ->zBlockType()
  840. ->getModelInfo()
  841. .getModelName()
  842. .istGleich("grass");
  843. blocks[index]->release();
  844. blocks[index] = 0;
  845. if (affectsGround) modelChanged = 1;
  846. }
  847. cs.unlock();
  848. }
  849. void Chunk::blockVisibilityChanged(Block* zB)
  850. {
  851. vcs.lock();
  852. if (zB->isVisible())
  853. {
  854. zB->tick(0);
  855. visibleBlocks.add(zB);
  856. }
  857. else
  858. {
  859. for (Framework::Iterator<Block*> iterator = visibleBlocks.begin();
  860. iterator;
  861. iterator++)
  862. {
  863. if (zB == (Block*)iterator)
  864. {
  865. iterator.remove();
  866. break;
  867. }
  868. }
  869. }
  870. vcs.unlock();
  871. }
  872. Framework::Punkt Chunk::getCenter() const
  873. {
  874. return location;
  875. }
  876. Framework::Vec3<int> Chunk::getMin() const
  877. {
  878. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  879. }
  880. Framework::Vec3<int> Chunk::getMax() const
  881. {
  882. return {
  883. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  884. }
  885. void Chunk::setGroundChanged()
  886. {
  887. modelChanged = 1;
  888. }
  889. void Chunk::setLightChanged()
  890. {
  891. lightChanged = 1;
  892. }