TreeSeblingBlock.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "TreeSeblingBlock.h"
  2. #include "BasicBlocks.h"
  3. #include "TreeTemplate.h"
  4. #include "RandNoise.h"
  5. #include "Game.h"
  6. #include "NoBlock.h"
  7. TreeSeblingBlock::TreeSeblingBlock(const BlockType* zType, const ItemType* zTool, Framework::Vec3<int> pos, const BlockType* wood, const BlockType* leaves)
  8. : Block(zType, zTool, pos, 0),
  9. wood(wood),
  10. leaves(leaves)
  11. {
  12. tickSource = 1;
  13. }
  14. bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  15. {
  16. seblingTicks += 1;
  17. return 0;
  18. }
  19. void TreeSeblingBlock::onPostTick()
  20. {
  21. if (seblingTicks >= seblingTicksMax)
  22. {
  23. Game::INSTANCE->doLater([wood = wood, leaves = leaves, pos = getPos(), dim = getDimensionId()]()
  24. {
  25. // the tree sebling object will be deleted during this operation
  26. RandNoise noise((int)time(0));
  27. if (!Game::INSTANCE->zGenerator()->spawnStructure(pos, dim, [wood = wood, leaves = leaves](GenerationTemplate* tmpl)
  28. {
  29. TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
  30. return tree && tree->getWoodType() == wood && tree->getLeavesType() == leaves;
  31. }))
  32. {
  33. Game::INSTANCE->zDimension(dim)->placeBlock(pos, AirBlockBlockType::ID);
  34. }
  35. });
  36. }
  37. }
  38. TreeSeblingBlockType::TreeSeblingBlockType(int typeId, int itemTypeId, ModelInfo model)
  39. : BlockType(typeId, 0, model, 1, 10, 0),
  40. itemType(itemTypeId),
  41. transparent(true),
  42. passable(true),
  43. hardness(0.1f),
  44. zTool(0),
  45. speedModifier(0.5f),
  46. interactable(1)
  47. {}
  48. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  49. {
  50. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  51. if (!block)
  52. throw "TreeSeblingBlockType::createSuperBlock was called with a block witch is not an instance of TreeSeblingBlock";
  53. block->transparent = transparent;
  54. block->passable = passable;
  55. block->hp = (float)getInitialMaxHP();
  56. block->maxHP = (float)getInitialMaxHP();
  57. block->hardness = hardness;
  58. block->zTool = zTool;
  59. block->speedModifier = speedModifier;
  60. block->interactable = interactable;
  61. BlockType::createSuperBlock(zBlock, zItem);
  62. }
  63. void TreeSeblingBlockType::loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  64. {
  65. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  66. if (!block)
  67. throw "TreeSeblingBlockType::loadSuperBlock was called with a block witch is not an instance of TreeSeblingBlock";
  68. zReader->lese((char*)&block->seblingTicks, 4);
  69. zReader->lese((char*)&block->seblingTicksMax, 4);
  70. int id;
  71. zReader->lese((char*)&id, 4);
  72. block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  73. zReader->lese((char*)&id, 4);
  74. block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  75. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  76. }
  77. void TreeSeblingBlockType::saveSuperBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  78. {
  79. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  80. if (!block)
  81. throw "TreeSeblingBlockType::saveSuperBlock was called with a block witch is not an instance of TreeSeblingBlock";
  82. zWriter->schreibe((char*)&block->seblingTicks, 4);
  83. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  84. int id = block->wood->getId();
  85. zWriter->schreibe((char*)&id, 4);
  86. int id = block->leaves->getId();
  87. zWriter->schreibe((char*)&id, 4);
  88. BlockType::saveSuperBlock(zBlock, zWriter);
  89. }
  90. Item* TreeSeblingBlockType::createItem() const
  91. {
  92. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  93. }
  94. OakSeblingBlockType::OakSeblingBlockType()
  95. : TreeSeblingBlockType(ID, OakSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  96. {
  97. hardness = 0.1f;
  98. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  99. }
  100. Block* OakSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  101. {
  102. return new TreeSeblingBlock(this, zTool, position, OakBlockType::INSTANCE, OakLeavesBlockType::INSTANCE);
  103. }
  104. BirchSeblingBlockType::BirchSeblingBlockType()
  105. : TreeSeblingBlockType(ID, BirchSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  106. {
  107. hardness = 0.1f;
  108. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  109. }
  110. Block* BirchSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  111. {
  112. return new TreeSeblingBlock(this, zTool, position, BirchBlockType::INSTANCE, BirchLeavesBlockType::INSTANCE);
  113. }
  114. BeechSeblingBlockType::BeechSeblingBlockType()
  115. : TreeSeblingBlockType(ID, BeechSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  116. {
  117. hardness = 0.1f;
  118. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  119. }
  120. Block* BeechSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  121. {
  122. return new TreeSeblingBlock(this, zTool, position, BeechBlockType::INSTANCE, BeechLeavesBlockType::INSTANCE);
  123. }
  124. PineSeblingBlockType::PineSeblingBlockType()
  125. : TreeSeblingBlockType(ID, PineSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  126. {
  127. hardness = 0.1f;
  128. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  129. }
  130. Block* PineSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  131. {
  132. return new TreeSeblingBlock(this, zTool, position, PineBlockType::INSTANCE, PineLeavesBlockType::INSTANCE);
  133. }