blocks.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #pragma once
  2. #include <memory.h>
  3. #include <vector>
  4. #include <stdint.h>
  5. #include <glad/glad.h>
  6. #include <glm/vec3.hpp>
  7. constexpr static float BLOCK_DEFAULT_FRICTION = 8.f;
  8. enum BlockTypes : unsigned short
  9. {
  10. air = 0,
  11. grassBlock,
  12. dirt,
  13. stone,
  14. ice,
  15. woodLog,
  16. wooden_plank,
  17. cobblestone,
  18. gold_block,
  19. bricks,
  20. sand,
  21. sand_stone,
  22. snow_dirt,
  23. leaves,
  24. goldOre,
  25. copperOre,
  26. stoneBrick,
  27. ironOre,
  28. silverOre,
  29. bookShelf,
  30. birch_log,
  31. gravel,
  32. grass,
  33. rose,
  34. water,
  35. jungle_log,
  36. jungle_leaves,
  37. palm_log,
  38. palm_leaves,
  39. cactus_bud,
  40. dead_bush,
  41. jungle_planks,
  42. clay,
  43. clayBricks,
  44. mud,
  45. redClay,
  46. redClayBricks,
  47. control1,
  48. control2,
  49. control3,
  50. control4,
  51. snow_block,
  52. birch_leaves,
  53. spruce_log,
  54. spruce_leaves,
  55. spruce_leaves_red,
  56. glowstone,
  57. glass,
  58. testBlock,
  59. torch,
  60. leadOre,
  61. coarseDirt,
  62. birchPlanks,
  63. pathBlock,
  64. plankedWallBlock,
  65. plankedWallBlock_stairs,
  66. plankedWallBlock_wall,
  67. terracotta,
  68. terracotta_stairs,
  69. terracotta_slabs,
  70. terracotta_wall,
  71. glassNotClear,
  72. vitral1,
  73. vitral2,
  74. glassNotClear2,
  75. glass2,
  76. mossyCobblestone,
  77. magenta_stained_glass,
  78. pink_stained_glass,
  79. clothBlock,
  80. wooden_stairs,
  81. wooden_slab,
  82. wooden_wall,
  83. tiledStoneBricks,
  84. stone_stairts,
  85. stone_slabs,
  86. stone_wall,
  87. cobbleStone_stairts,
  88. cobbleStone_slabs,
  89. cobbleStone_wall,
  90. tiledStoneBricks_stairs,
  91. tiledStoneBricks_slab,
  92. tiledStoneBricks_wall,
  93. bricks_stairs,
  94. bricks_slabs,
  95. bricks_wall,
  96. stoneBricks_stairts,
  97. stoneBricks_slabs,
  98. stoneBricks_wall,
  99. structureBase,
  100. hardSandStone,
  101. hardSandStone_stairs,
  102. hardSandStone_slabs,
  103. hardSandStone_wall,
  104. sandStone_stairts,
  105. sandStone_slabs,
  106. sandStone_wall,
  107. dungeonBricks,
  108. dungeonBricks_stairts,
  109. dungeonBricks_slabs,
  110. dungeonBricks_wall,
  111. volcanicHotRock,
  112. volcanicRock,
  113. volcanicRock_stairts,
  114. volcanicRock_slabs,
  115. volcanicRock_wall,
  116. smoothStone,
  117. smoothStone_stairts,
  118. smoothStone_slabs,
  119. smoothStone_wall,
  120. limeStone,
  121. smoothLimeStone,
  122. smoothLimeStone_stairs,
  123. smoothLimeStone_slabs,
  124. smoothLimeStone_wall,
  125. marbleBlock,
  126. marbleBlock_stairs,
  127. marbleBlock_slabs,
  128. marbleBlock_wall,
  129. smoothMarbleBlock,
  130. marbleBricks,
  131. marbleBricks_stairs,
  132. marbleBricks_slabs,
  133. marbleBricks_wall,
  134. marblePillar,
  135. logWall,
  136. yellowGrass,
  137. blueBricks,
  138. blueBricks_stairs,
  139. blueBricks_slabs,
  140. blueBricks_wall,
  141. oakChair,
  142. oakLogChair,
  143. mug,
  144. goblet,
  145. wineBottle,
  146. skull,
  147. skullTorch,
  148. book,
  149. candleHolder,
  150. pot,
  151. jar,
  152. globe,
  153. keg,
  154. workBench,
  155. oakTable,
  156. oakLogTable,
  157. craftingItems,
  158. oakBigChair,
  159. oakLogBigChair,
  160. cookingPot,
  161. chickenCaracas,
  162. chickenWingsPlate,
  163. fishPlate,
  164. ladder,
  165. vines,
  166. cloth_stairs,
  167. cloth_slabs,
  168. cloth_wall,
  169. birchPlanks_stairs,
  170. birchPlanks_slabs,
  171. birchPlanks_wall,
  172. oakLogSlab,
  173. smallRock,
  174. strippedOakLog,
  175. strippedBirchLog,
  176. strippedSpruceLog,
  177. sprucePlank,
  178. sprucePlank_stairs,
  179. sprucePlank_slabs,
  180. sprucePlank_wall,
  181. dungeonStone,
  182. dungeonStone_stairs,
  183. dungeonStone_slabs,
  184. dungeonStone_wall,
  185. dungeonCobblestone,
  186. dungeonCobblestone_stairs,
  187. dungeonCobblestone_slabs,
  188. dungeonCobblestone_wall,
  189. dungeonSmoothStone,
  190. dungeonSmoothStone_stairs,
  191. dungeonSmoothStone_slabs,
  192. dungeonSmoothStone_wall,
  193. dungeonPillar,
  194. dungeonSkullBlock,
  195. chiseledDungeonBrick,
  196. dungeonGlass,
  197. woddenChest,
  198. goblinChest,
  199. copperChest,
  200. ironChest,
  201. silverChest,
  202. goldChest,
  203. crate,
  204. smallCrate,
  205. lamp,
  206. torchWood,
  207. mossyCobblestone_stairs,
  208. mossyCobblestone_slab,
  209. mossyCobblestone_wall,
  210. cobweb,
  211. hayBalde,
  212. trainingDummy,
  213. target,
  214. furnace,
  215. goblinWorkBench,
  216. goblinChair,
  217. goblinTable,
  218. goblinTorch,
  219. goblinStitchingPost,
  220. woodenFence,
  221. woodenLogFence,
  222. spruceFence,
  223. spruceLogFence,
  224. birchFence,
  225. birchLogFence,
  226. BlocksCount
  227. };
  228. int getBlockReorder(int index);
  229. using BlockType = uint16_t;
  230. //todo look into this
  231. bool isBlockMesh(BlockType type);
  232. bool canWallMountedBlocksBePlacedOn(BlockType type);
  233. bool isStairsMesh(BlockType type);
  234. bool isSlabMesh(BlockType type);
  235. bool isWallMesh(BlockType type);
  236. bool isCrossMesh(BlockType type);
  237. bool isControlBlock(BlockType type);
  238. bool isWallMountedBlock(BlockType type);
  239. bool isWallMountedOrStangingBlock(BlockType type);
  240. bool isOpaque(BlockType type);
  241. bool isDecorativeFurniture(BlockType type);
  242. bool isLightEmitor(BlockType type);
  243. bool isTransparentGeometry(BlockType type);
  244. bool isGrassMesh(BlockType type);
  245. bool isColidable(BlockType type);
  246. bool isWoodPlank(BlockType type);
  247. bool isChairMesh(BlockType type);
  248. bool isGobletMesh(BlockType type);
  249. //used for breaking
  250. bool isAnyWoddenBlock(BlockType type);
  251. bool isBricksSound(BlockType type);
  252. bool isVolcanicActiveSound(BlockType type);
  253. bool isVolcanicInActiveSound(BlockType type);
  254. bool isFenceConnectorBlock(std::uint16_t type);
  255. bool isAnyWoddenLOG(BlockType type);
  256. //used for breaking, cloth blocks
  257. bool isAnyWool(BlockType type);
  258. //used for breaking
  259. bool isAnyDirtBlock(BlockType type);
  260. //used for breaking
  261. bool isAnyClayBlock(BlockType type);
  262. //used for breaking
  263. bool isAnySandyBlock(BlockType type);
  264. //used for breaking
  265. bool isAnySemiHardBlock(BlockType type);
  266. //used for breaking
  267. bool isAnyStone(BlockType type);
  268. bool isChest(BlockType type);
  269. //block transitions
  270. bool canHaveDecals(BlockType type);
  271. //used for breaking
  272. bool isDungeonBrick(BlockType type);
  273. //used for breaking
  274. bool isAnyPlant(BlockType type);
  275. //used for breaking
  276. bool isAnyGlass(BlockType type);
  277. //used for breaking
  278. bool isTriviallyBreakable(BlockType type);
  279. //used for breaking, bedrock type blocks, that can be breaked in creative!
  280. bool isAnyUnbreakable(BlockType type);
  281. //used for sound
  282. bool isAnyHotSoundingBlock(BlockType type);
  283. //used for breaking
  284. bool isAnyLeaves(BlockType type);
  285. bool isStainedGlass(BlockType type);
  286. unsigned char isInteractable(BlockType type);
  287. bool isBlock(std::uint16_t type);
  288. bool noRotationForFurniture(std::uint16_t type);
  289. bool isFenceMesh(std::uint16_t type);
  290. bool canBeMinedByHand(std::uint16_t type);
  291. bool canBeMinedByPickaxe(std::uint16_t type);
  292. bool canBeMinedByShovel(std::uint16_t type);
  293. bool canBeMinedByAxe(std::uint16_t type);
  294. int isCraftingStation(unsigned short type);
  295. BlockType fromAnyShapeToNormalBlockType(BlockType b);
  296. //true for standing on wall!
  297. unsigned char getRotatedOrStandingForWallOrStandingBlocks(BlockType b);
  298. struct BlockCollider
  299. {
  300. glm::vec3 size = {1,1,1};
  301. glm::vec3 offset = {};
  302. };
  303. enum WorkStationType
  304. {
  305. WorkStationType_None = 0,
  306. WorkStationType_WorkBench,
  307. WorkStationType_Furnace,
  308. WorkStationType_CookingPot,
  309. WorkStationType_Anvil,
  310. WorkStationType_GoblinStitchingPost,
  311. //DONT FORGET TO ALSO UPDATE TOWARDS THE BOTTOM OF THE FILE
  312. };
  313. namespace BlockColor
  314. {
  315. enum
  316. {
  317. none,
  318. white,
  319. lightGray,
  320. darkGrayP,
  321. black,
  322. brown,
  323. red,
  324. orange,
  325. yellow,
  326. lime,
  327. green,
  328. turqoise,
  329. cyan,
  330. blue,
  331. purple,
  332. magenta,
  333. pink,
  334. };
  335. };
  336. struct Block
  337. {
  338. BlockType typeAndFlags = 0;
  339. unsigned char lightLevel = 0; //first 4 bytes represent the sun level and bottom 4 bytes the other lights level
  340. unsigned char colorAndOtherFlags = 0;
  341. bool operator==(const Block &other)
  342. {
  343. return typeAndFlags == other.typeAndFlags && lightLevel == other.lightLevel &&
  344. colorAndOtherFlags == other.colorAndOtherFlags;
  345. };
  346. unsigned short getColor()
  347. {
  348. return colorAndOtherFlags & 0b0001'1111;
  349. }
  350. void setColor(unsigned short color)
  351. {
  352. if (color >= 32) { color = 31; }
  353. colorAndOtherFlags &= 0b1110'0000;
  354. colorAndOtherFlags |= color;
  355. }
  356. bool normalize();
  357. BlockType getType()
  358. {
  359. return typeAndFlags & 0b0111'1111'1111;
  360. }
  361. unsigned char getFlagsBytes()
  362. {
  363. return typeAndFlags >> 11;
  364. }
  365. //used for stairs, or furniture type blocks
  366. unsigned char getRotationFor365RotationTypeBlocks()
  367. {
  368. return (typeAndFlags >> 11) & 0b0001'1;
  369. }
  370. //true for standing on wall!
  371. unsigned char getRotatedOrStandingForWallOrStandingBlocks()
  372. {
  373. return ::getRotatedOrStandingForWallOrStandingBlocks(typeAndFlags);
  374. }
  375. void setRotatedOrStandingForWallOrStandingBlocks(bool isOnWall)
  376. {
  377. unsigned int is = isOnWall;
  378. is <<= 13;
  379. typeAndFlags &= 0b1101'1111'1111'1111;
  380. typeAndFlags |= is;
  381. }
  382. void setRotationFor365RotationTypeBlocks(int rotation)
  383. {
  384. rotation <<= 11;
  385. typeAndFlags &= 0b1110'0111'1111'1111;
  386. typeAndFlags |= rotation;
  387. }
  388. void setTopPartForSlabs(int topPart)
  389. {
  390. topPart = (bool)topPart;
  391. topPart <<= 11;
  392. typeAndFlags &= 0b1111'0111'1111'1111;
  393. typeAndFlags |= topPart;
  394. }
  395. void rotate(int rotation)
  396. {
  397. if (rotation == 0)
  398. {
  399. return;
  400. }
  401. assert(rotation <= 3);
  402. if (hasRotationFor365RotationTypeBlocks() && !noRotationForFurniture())
  403. {
  404. if (!isWallMountedOrStangingBlock()
  405. ||
  406. getRotatedOrStandingForWallOrStandingBlocks()
  407. )
  408. {
  409. int r = getRotationFor365RotationTypeBlocks();
  410. setRotationFor365RotationTypeBlocks((r + rotation) % 4);
  411. }
  412. }
  413. }
  414. bool getTopPartForSlabs()
  415. {
  416. return (typeAndFlags >> 11) & 0b0000'1;
  417. }
  418. bool isDecorativeFurniture()
  419. {
  420. auto type = getType();
  421. return ::isDecorativeFurniture(getType());
  422. }
  423. //used for stairs, or furniture type blocks
  424. bool hasRotationFor365RotationTypeBlocks()
  425. {
  426. return isStairsMesh() || isWallMesh() || isDecorativeFurniture() || isWallMountedBlock()
  427. || isWallMountedOrStangingBlock();
  428. }
  429. bool noRotationForFurniture()
  430. {
  431. return ::noRotationForFurniture(getType());
  432. }
  433. void setType(BlockType t)
  434. {
  435. typeAndFlags = t & 0b0111'1111'1111;
  436. }
  437. bool air() { return getType() == 0; }
  438. bool canBePainted() { return getType() != 0; }
  439. bool isOpaque()
  440. {
  441. return ::isOpaque(getType());
  442. }
  443. bool isWallMountedBlock()
  444. {
  445. return ::isWallMountedBlock(getType());
  446. }
  447. bool isWallMountedOrStangingBlock()
  448. {
  449. return::isWallMountedOrStangingBlock(getType());
  450. }
  451. bool isFenceMesh()
  452. {
  453. return ::isFenceMesh(getType());
  454. }
  455. bool isChest()
  456. {
  457. return ::isChest(getType());
  458. }
  459. bool canHaveDecals()
  460. {
  461. return ::canHaveDecals(getType());
  462. }
  463. bool hasSecondCollider()
  464. {
  465. return isStairsMesh();
  466. }
  467. bool isFenceConnectorBlock()
  468. {
  469. return ::isFenceConnectorBlock(getType());
  470. }
  471. BlockCollider getCollider()
  472. {
  473. if (isChairMesh())
  474. {
  475. BlockCollider b{};
  476. b.size.y = 0.5;
  477. b.size.x = 0.8;
  478. b.size.z = 0.8;
  479. return b;
  480. }else
  481. if (isStairsMesh())
  482. {
  483. BlockCollider b{};
  484. b.size.y = 0.5;
  485. return b;
  486. }else
  487. if (isSlabMesh())
  488. {
  489. if (getTopPartForSlabs())
  490. {
  491. BlockCollider b{};
  492. b.size.y = 0.5;
  493. b.offset.y = 0.5;
  494. return b;
  495. }
  496. else
  497. {
  498. BlockCollider b{};
  499. b.size.y = 0.5;
  500. return b;
  501. }
  502. }else
  503. if (isWallMesh())
  504. {
  505. int rotation = getRotationFor365RotationTypeBlocks();
  506. if (rotation == 0)
  507. {
  508. BlockCollider b{};
  509. b.size.z = 0.5;
  510. b.offset.z = -0.25;
  511. return b;
  512. }
  513. else if (rotation == 2)
  514. {
  515. BlockCollider b{};
  516. b.size.z = 0.5;
  517. b.offset.z = 0.25;
  518. return b;
  519. }
  520. else if (rotation == 1)
  521. {
  522. BlockCollider b{};
  523. b.size.x = 0.5;
  524. b.offset.x = -0.25;
  525. return b;
  526. }
  527. else if (rotation == 3)
  528. {
  529. BlockCollider b{};
  530. b.size.x = 0.5;
  531. b.offset.x = 0.25;
  532. return b;
  533. }
  534. }
  535. else if (isFenceMesh())
  536. {
  537. BlockCollider b{};
  538. b.size.x = 1.f * (4.f / 16.f);
  539. b.size.z = 1.f * (4.f / 16.f);
  540. return b;
  541. }
  542. auto type = getType();
  543. auto sizeInPixels = [&](float pixels)
  544. {
  545. return (pixels / 16.f);
  546. };
  547. if (type == BlockTypes::trainingDummy)
  548. {
  549. BlockCollider b{};
  550. b.size.x = sizeInPixels(14);
  551. b.size.z = b.size.x;
  552. b.size.y = sizeInPixels(1);
  553. return b;
  554. }
  555. return BlockCollider{};
  556. }
  557. BlockCollider getSecondCollider()
  558. {
  559. if (isStairsMesh())
  560. {
  561. int rotation = getRotationFor365RotationTypeBlocks();
  562. if (rotation == 0)
  563. {
  564. BlockCollider b{};
  565. b.size.z = 0.5;
  566. b.offset.z = -0.25;
  567. return b;
  568. }
  569. else if (rotation == 2)
  570. {
  571. BlockCollider b{};
  572. b.size.z = 0.5;
  573. b.offset.z = 0.25;
  574. return b;
  575. }
  576. else if (rotation == 1)
  577. {
  578. BlockCollider b{};
  579. b.size.x = 0.5;
  580. b.offset.x = -0.25;
  581. return b;
  582. }
  583. else if (rotation == 3)
  584. {
  585. BlockCollider b{};
  586. b.size.x = 0.5;
  587. b.offset.x = 0.25;
  588. return b;
  589. }
  590. }
  591. BlockCollider b;
  592. b.size = {};
  593. return b;
  594. }
  595. //rename is animated leaves
  596. bool isAnimatedBlock()
  597. {
  598. auto type = getType();
  599. return
  600. type == BlockTypes::leaves || type == BlockTypes::jungle_leaves
  601. || type == BlockTypes::palm_leaves || type == BlockTypes::birch_leaves
  602. || type == BlockTypes::spruce_leaves || type == BlockTypes::spruce_leaves_red;
  603. }
  604. bool isTransparentGeometry()
  605. {
  606. return ::isTransparentGeometry(getType());
  607. }
  608. bool isStairsMesh()
  609. {
  610. return ::isStairsMesh(getType());
  611. }
  612. bool isChairMesh()
  613. {
  614. return ::isChairMesh(getType());
  615. }
  616. bool isGobletMesh()
  617. {
  618. return ::isGobletMesh(getType());
  619. }
  620. bool isWallMesh()
  621. {
  622. return ::isWallMesh(getType());
  623. }
  624. bool isSlabMesh()
  625. {
  626. return ::isSlabMesh(getType());
  627. }
  628. bool isGrassMesh()
  629. {
  630. return ::isGrassMesh(getType());
  631. }
  632. bool isLightEmitor()
  633. {
  634. return ::isLightEmitor(getType());
  635. }
  636. unsigned char getSkyLight()
  637. {
  638. return (lightLevel >> 4);
  639. }
  640. unsigned char getLight()
  641. {
  642. return lightLevel & 0b1111;
  643. }
  644. void setSkyLevel(unsigned char s)
  645. {
  646. s = s & 0b1111;
  647. s <<= 4;
  648. lightLevel &= 0b0000'1111;
  649. lightLevel |= s;
  650. }
  651. void setLightLevel(unsigned char l)
  652. {
  653. l = l & 0b1111;
  654. lightLevel &= 0b1111'0000;
  655. lightLevel |= l;
  656. }
  657. bool canWallMountedBlocksBePlacedOn()
  658. {
  659. return ::canWallMountedBlocksBePlacedOn(getType());
  660. }
  661. bool isBlockMesh()
  662. {
  663. return ::isBlockMesh(getType());
  664. }
  665. //todo, investigate where it this used and implement it corectly, and remove the grass mesh component if so
  666. bool isCrossMesh()
  667. {
  668. return ::isCrossMesh(getType());
  669. }
  670. bool isColidable()
  671. {
  672. return ::isColidable(getType());
  673. }
  674. bool stopsGrassFromGrowingIfOnTop()
  675. {
  676. return isColidable() && !isAnyLeaves(getType())
  677. && !isWallMesh() && !(isSlabMesh() && getTopPartForSlabs())
  678. && !isDecorativeFurniture()
  679. ;
  680. }
  681. float getFriction();
  682. };
  683. namespace InteractionTypes
  684. {
  685. enum
  686. {
  687. none = 0,
  688. craftingTable,
  689. furnace,
  690. cookingPot,
  691. anvil,
  692. goblinStitchingPost, //add other work stations here v...
  693. structureBaseBlock,
  694. chestInteraction,
  695. };
  696. };
  697. float getBlockBaseMineDuration(BlockType type);