Chunk.cpp 17 KB

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