Chunk.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 (dx12Data->isSimpleBlock(b))
  131. {
  132. dx12Data->setBlock(index, b);
  133. }
  134. else
  135. {
  136. vLock.lockWrite();
  137. visibleBlocks.add(b);
  138. vLock.unlockWrite();
  139. }
  140. }
  141. count++;
  142. }
  143. zReader->read((char*)&id, 2);
  144. }
  145. std::cout << "Loaded " << count << " blocks\n";
  146. int index = 0;
  147. // light
  148. zReader->read((char*)&index, 4);
  149. char lightData[6];
  150. while (index >= -1)
  151. {
  152. if (index == -1)
  153. {
  154. int x = 0;
  155. int y = 0;
  156. int z = 0;
  157. zReader->read((char*)&x, 4);
  158. zReader->read((char*)&y, 4);
  159. zReader->read((char*)&z, 4);
  160. zReader->read(lightData, 6);
  161. if (x == -1)
  162. {
  163. int cacheIndex = y * WORLD_HEIGHT + z;
  164. bLock.lockRead();
  165. Block* zB = blocks[cacheIndex];
  166. if (zB)
  167. {
  168. zB->setLightData(WEST, (unsigned char*)lightData, 0);
  169. }
  170. bLock.unlockRead();
  171. }
  172. else if (y == -1)
  173. {
  174. int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
  175. bLock.lockRead();
  176. Block* zB = blocks[cacheIndex];
  177. if (zB)
  178. {
  179. zB->setLightData(NORTH, (unsigned char*)lightData, 0);
  180. }
  181. bLock.unlockRead();
  182. }
  183. else if (x == CHUNK_SIZE)
  184. {
  185. int cacheIndex
  186. = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
  187. bLock.lockRead();
  188. Block* zB = blocks[cacheIndex];
  189. if (zB)
  190. {
  191. zB->setLightData(EAST, (unsigned char*)lightData, 0);
  192. }
  193. bLock.unlockRead();
  194. }
  195. else if (y == CHUNK_SIZE)
  196. {
  197. int cacheIndex
  198. = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
  199. bLock.lockRead();
  200. Block* zB = blocks[cacheIndex];
  201. if (zB)
  202. {
  203. zB->setLightData(SOUTH, (unsigned char*)lightData, 0);
  204. }
  205. bLock.unlockRead();
  206. }
  207. }
  208. else
  209. {
  210. zReader->read(lightData, 6);
  211. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  212. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  213. index % WORLD_HEIGHT);
  214. for (int i = 0; i < 6; i++)
  215. {
  216. Framework::Vec3<int> pos
  217. = location + getDirection(getDirectionFromIndex(i));
  218. if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
  219. {
  220. if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
  221. && pos.y < CHUNK_SIZE)
  222. {
  223. int cacheIndex
  224. = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
  225. + pos.z;
  226. bLock.lockRead();
  227. Block* zB = blocks[cacheIndex];
  228. if (zB)
  229. {
  230. bool visible = zB->isVisible();
  231. zB->setLightData(
  232. getOppositeDirection(getDirectionFromIndex(i)),
  233. (unsigned char*)lightData,
  234. 0);
  235. if (zB->isVisible() && !visible)
  236. {
  237. zB->tick(0);
  238. vLock.lockWrite();
  239. visibleBlocks.add(zB);
  240. vLock.unlockWrite();
  241. }
  242. }
  243. bLock.unlockRead();
  244. }
  245. else
  246. {
  247. pos.x += this->location.x - CHUNK_SIZE / 2;
  248. pos.y += this->location.y - CHUNK_SIZE / 2;
  249. World::INSTANCE->getChunkReadLock().lock();
  250. Chunk* c = World::INSTANCE->zChunk(
  251. World::INSTANCE->getChunkCenter(pos.x, pos.y));
  252. if (c)
  253. {
  254. c->getBlockReadLock().lock();
  255. Block* zB = c->zBlockAt(pos);
  256. if (zB)
  257. {
  258. bool visible = zB->isVisible();
  259. zB->setLightData(getOppositeDirection(
  260. getDirectionFromIndex(i)),
  261. (unsigned char*)lightData,
  262. c);
  263. if (zB->isVisible() && !visible)
  264. {
  265. zB->tick(0);
  266. vLock.lockWrite();
  267. c->visibleBlocks.add(zB);
  268. vLock.unlockWrite();
  269. }
  270. }
  271. c->getBlockReadLock().unlock();
  272. }
  273. World::INSTANCE->getChunkReadLock().unlock();
  274. }
  275. }
  276. }
  277. }
  278. zReader->read((char*)&index, 4);
  279. }
  280. isLoading = 0;
  281. }
  282. void Chunk::forAll(std::function<void(Model3D*)> f)
  283. {
  284. vLock.lockRead();
  285. for (Block* b : visibleBlocks)
  286. {
  287. f(b);
  288. }
  289. vLock.unlockRead();
  290. }
  291. bool Chunk::tick(std::function<void(Model3D*)> f, double time)
  292. {
  293. bool res = 0;
  294. aLock.lockRead();
  295. auto iterator = animations.begin();
  296. while (iterator)
  297. {
  298. if (iterator->tick(time))
  299. {
  300. res |= iterator->zBlock()->tick(time);
  301. if (iterator->isFinished())
  302. {
  303. aLock.lockWrite();
  304. iterator.remove();
  305. aLock.unlockWrite();
  306. continue;
  307. }
  308. }
  309. else
  310. {
  311. aLock.lockWrite();
  312. iterator.remove();
  313. aLock.unlockWrite();
  314. continue;
  315. }
  316. ++iterator;
  317. }
  318. aLock.unlockRead();
  319. vLock.lockRead();
  320. for (Block* b : visibleBlocks)
  321. {
  322. res |= b->tick(time);
  323. }
  324. vLock.unlockRead();
  325. return 1;
  326. }
  327. void Chunk::destroy() {}
  328. void Chunk::api(char* message)
  329. {
  330. switch (message[0])
  331. {
  332. case 0: // set block
  333. {
  334. unsigned short id = *(int*)(message + 1);
  335. int index = *(int*)(message + 3);
  336. char state = message[7];
  337. char flowOptions = 0;
  338. char distanceToSource = 0;
  339. int i = 8;
  340. if (state & 1)
  341. {
  342. flowOptions = message[i++];
  343. distanceToSource = message[i++];
  344. }
  345. bool passable = 0;
  346. float speedModifier = 1.f;
  347. if (state & 2)
  348. {
  349. passable = 1;
  350. speedModifier = *(float*)(message + i);
  351. i += 4;
  352. }
  353. Direction dir = blockTypes[id]->getDefaultFrontDirection();
  354. if (state & 4)
  355. {
  356. dir = (Direction)message[i++];
  357. }
  358. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  359. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  360. index % WORLD_HEIGHT);
  361. location.x += this->location.x - CHUNK_SIZE / 2;
  362. location.y += this->location.y - CHUNK_SIZE / 2;
  363. if (blockTypes[id]->doesNeedInstance())
  364. {
  365. Block* zB = blockTypes[id]->createBlock(
  366. location, passable, speedModifier, dir);
  367. zB->setFlow(flowOptions, distanceToSource);
  368. setBlock(zB);
  369. }
  370. else
  371. {
  372. bLock.lockRead();
  373. Block* zB = zBlockAt(location);
  374. if (zB) removeBlock(zB);
  375. bLock.unlockRead();
  376. }
  377. break;
  378. }
  379. case 1: // animate block
  380. {
  381. int index = *(int*)(message + 1);
  382. int boneId = *(int*)(message + 5);
  383. double time = *(double*)(message + 9);
  384. Framework::Vec3<float> pos;
  385. pos.x = *(float*)(message + 17);
  386. pos.y = *(float*)(message + 21);
  387. pos.z = *(float*)(message + 25);
  388. Framework::Vec3<float> rot;
  389. rot.x = *(float*)(message + 29);
  390. rot.y = *(float*)(message + 33);
  391. rot.z = *(float*)(message + 37);
  392. Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE,
  393. (index / WORLD_HEIGHT) % CHUNK_SIZE,
  394. index % WORLD_HEIGHT);
  395. location.x += this->location.x - CHUNK_SIZE / 2;
  396. location.y += this->location.y - CHUNK_SIZE / 2;
  397. bLock.lockRead();
  398. Block* zB = zBlockAt(location);
  399. if (zB) appendAnimation(zB, boneId, time, pos, rot);
  400. bLock.unlockRead();
  401. break;
  402. }
  403. }
  404. }
  405. Block* Chunk::zBlockAt(Framework::Vec3<int> location)
  406. {
  407. if (location.z < 0 || location.z >= WORLD_HEIGHT) return 0;
  408. location.x = location.x % CHUNK_SIZE;
  409. location.y = location.y % CHUNK_SIZE;
  410. if (location.x < 0) location.x += CHUNK_SIZE;
  411. if (location.y < 0) location.y += CHUNK_SIZE;
  412. int index
  413. = (location.x * CHUNK_SIZE + location.y) * WORLD_HEIGHT + location.z;
  414. return blocks[index];
  415. }
  416. void Chunk::setBlock(Block* block)
  417. {
  418. Framework::Vec3<int> pos = block->getLocation();
  419. pos.x = pos.x % CHUNK_SIZE;
  420. pos.y = pos.y % CHUNK_SIZE;
  421. if (pos.x < 0) pos.x += CHUNK_SIZE;
  422. if (pos.y < 0) pos.y += CHUNK_SIZE;
  423. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  424. bLock.lockWrite();
  425. if (blocks[index])
  426. {
  427. if (!dx12Data->isSimpleBlock(blocks[index]))
  428. {
  429. vLock.lockWrite();
  430. for (Framework::ArrayIterator<Block*> vi = visibleBlocks.begin();
  431. vi;
  432. vi++)
  433. {
  434. if (blocks[index] == (Block*)vi)
  435. {
  436. vi.remove();
  437. break;
  438. }
  439. }
  440. vLock.unlockWrite();
  441. }
  442. blocks[index]->copyLightTo(block);
  443. blocks[index]->release();
  444. }
  445. blocks[index] = block;
  446. bLock.unlockWrite();
  447. if (block && block->isVisible())
  448. {
  449. if (dx12Data->isSimpleBlock(block))
  450. {
  451. dx12Data->setBlock(index, block);
  452. }
  453. else
  454. {
  455. dx12Data->setBlock(index, 0);
  456. vLock.lockWrite();
  457. visibleBlocks.add(block);
  458. vLock.unlockWrite();
  459. }
  460. }
  461. else
  462. {
  463. dx12Data->setBlock(index, 0);
  464. }
  465. }
  466. void Chunk::removeBlock(Block* zBlock)
  467. {
  468. if (!dx12Data->isSimpleBlock(zBlock))
  469. {
  470. vLock.lockWrite();
  471. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  472. iterator;
  473. iterator++)
  474. {
  475. if (zBlock == (Block*)iterator)
  476. {
  477. iterator.remove();
  478. break;
  479. }
  480. }
  481. vLock.unlockWrite();
  482. }
  483. Vec3<int> pos = zBlock->getLocation();
  484. pos.x = pos.x % CHUNK_SIZE;
  485. pos.y = pos.y % CHUNK_SIZE;
  486. if (pos.x < 0) pos.x += CHUNK_SIZE;
  487. if (pos.y < 0) pos.y += CHUNK_SIZE;
  488. int index = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
  489. if (dx12Data->isSimpleBlock(zBlock))
  490. {
  491. dx12Data->setBlock(index, 0);
  492. }
  493. bLock.lockWrite();
  494. if (blocks[index])
  495. {
  496. blocks[index]->release();
  497. blocks[index] = 0;
  498. }
  499. bLock.unlockWrite();
  500. }
  501. void Chunk::blockVisibilityChanged(Block* zB)
  502. {
  503. if (zB->isVisible())
  504. {
  505. zB->tick(0);
  506. vLock.lockWrite();
  507. visibleBlocks.add(zB);
  508. vLock.unlockWrite();
  509. }
  510. else
  511. {
  512. vLock.lockWrite();
  513. for (Framework::ArrayIterator<Block*> iterator = visibleBlocks.begin();
  514. iterator;
  515. iterator++)
  516. {
  517. if (zB == (Block*)iterator)
  518. {
  519. iterator.remove();
  520. break;
  521. }
  522. }
  523. vLock.unlockWrite();
  524. }
  525. }
  526. Framework::Point Chunk::getCenter() const
  527. {
  528. return location;
  529. }
  530. Framework::Vec3<int> Chunk::getMin() const
  531. {
  532. return {location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0};
  533. }
  534. Framework::Vec3<int> Chunk::getMax() const
  535. {
  536. return {
  537. location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT};
  538. }
  539. void Chunk::setLightChanged(int type)
  540. {
  541. lightChanged |= type;
  542. }
  543. Lock& Chunk::getBlockReadLock() const
  544. {
  545. return bLock.getReadLock();
  546. }
  547. DX12ChunkData* Chunk::zDX12Data() const
  548. {
  549. return dx12Data;
  550. }