GrowingPlant.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "GrowingPlant.h"
  2. #include "Game.h"
  3. GrowthState::GrowthState()
  4. : percentage(1),
  5. model("", "", 0)
  6. {}
  7. GrowthState::GrowthState(float percentage, ModelInfo model)
  8. : percentage(percentage),
  9. model(model)
  10. {}
  11. GrowthState::GrowthState(const GrowthState& right)
  12. : percentage(right.percentage),
  13. model(right.model)
  14. {}
  15. GrowthState& GrowthState::operator=(const GrowthState& right)
  16. {
  17. percentage = right.percentage;
  18. model = ModelInfo(right.model);
  19. return *this;
  20. }
  21. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  22. const ItemType* zTool,
  23. Framework::Vec3<int> pos,
  24. int dimensionId,
  25. int maxTicks,
  26. const char* name,
  27. int blockTypeAfterGrowth)
  28. : Block(typeId, zTool, pos, dimensionId, 0),
  29. seblingTicks(0),
  30. seblingTicksMax(maxTicks),
  31. name(name),
  32. states(),
  33. blockTypeAfterGrowth(blockTypeAfterGrowth),
  34. plantSpawned(0),
  35. lastSendState(-1)
  36. {
  37. tickSource = 1;
  38. }
  39. bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  40. {
  41. float beforePercentage = seblingTicks / (float)seblingTicksMax;
  42. seblingTicks += (float)numTicks;
  43. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f)
  44. != (int)(beforePercentage * 100.f))
  45. {
  46. Game::INSTANCE->blockTargetChanged(this);
  47. }
  48. int index = 0;
  49. int currentIndex = 0;
  50. for (const GrowthState& state : states)
  51. {
  52. if (state.percentage <= seblingTicks / (float)seblingTicksMax)
  53. {
  54. currentIndex = index;
  55. }
  56. else
  57. {
  58. break;
  59. }
  60. index++;
  61. }
  62. if (lastSendState != currentIndex)
  63. {
  64. updateModel(states.get(currentIndex).model);
  65. lastSendState = currentIndex;
  66. }
  67. return 1;
  68. }
  69. void GrowingPlantBlock::onPostTick()
  70. {
  71. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  72. {
  73. plantSpawned = 1;
  74. Game::INSTANCE->doLater([this]() {
  75. Game::INSTANCE->zDimension(getDimensionId())
  76. ->placeBlock(getPos(), blockTypeAfterGrowth);
  77. });
  78. }
  79. }
  80. void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage)
  81. {
  82. GrowthState current;
  83. bool found = 0;
  84. for (const GrowthState& state : states)
  85. {
  86. if (state.percentage <= seblingTicks / (float)seblingTicksMax)
  87. {
  88. found = 1;
  89. current = state;
  90. }
  91. }
  92. if (found)
  93. {
  94. zMessage->addressBlock(this);
  95. InMemoryBuffer buffer;
  96. current.model.writeTo(&buffer);
  97. char* msg = new char[(int)buffer.getSize() + 1];
  98. msg[0] = 1; // hmodel change
  99. buffer.lese(msg + 1, (int)buffer.getSize());
  100. zMessage->setMessage(msg, (int)buffer.getSize() + 1);
  101. }
  102. }
  103. Framework::Text GrowingPlantBlock::getTargetUIML()
  104. {
  105. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  106. + "\n" + "Growth: "
  107. + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  108. + "%</text></targetInfo>";
  109. }
  110. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState state)
  111. {
  112. int index = 0;
  113. for (const GrowthState& s : states)
  114. {
  115. if (s.percentage > state.percentage)
  116. {
  117. states.add(state, index);
  118. return this;
  119. }
  120. index++;
  121. }
  122. states.add(state);
  123. return this;
  124. }
  125. GrowingPlantBlockType::GrowingPlantBlockType(int typeId,
  126. ModelInfo model,
  127. const char* name,
  128. int blockTypeAfterGrowth,
  129. const char* readableName,
  130. int ticksNeeded,
  131. int mapColor)
  132. : BlockType(typeId, 0, model, 1, 10, 0, name, true, mapColor),
  133. transparent(1),
  134. passable(1),
  135. hardness(0.1f),
  136. zTool(0),
  137. speedModifier(0.3f),
  138. interactable(1),
  139. states(),
  140. blockTypeAfterGrowth(blockTypeAfterGrowth),
  141. readableName(readableName),
  142. ticksNeeded(ticksNeeded)
  143. {}
  144. GrowingPlantBlockType* GrowingPlantBlockType::setHardness(float hardness)
  145. {
  146. this->hardness = hardness;
  147. return this;
  148. }
  149. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  150. float growthPercentage, ModelInfo model)
  151. {
  152. states.add(GrowthState(growthPercentage, model));
  153. return this;
  154. }
  155. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  156. {
  157. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  158. block->transparent = transparent;
  159. block->passable = passable;
  160. block->hardness = hardness;
  161. block->zTool = zTool;
  162. block->speedModifier = speedModifier;
  163. block->interactable = interactable;
  164. BlockType::createSuperBlock(zBlock, zItem);
  165. }
  166. void GrowingPlantBlockType::loadSuperBlock(
  167. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  168. {
  169. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  170. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  171. zReader->lese((char*)&block->seblingTicks, 4);
  172. }
  173. void GrowingPlantBlockType::saveSuperBlock(
  174. Block* zBlock, Framework::StreamWriter* zWriter) const
  175. {
  176. BlockType::saveSuperBlock(zBlock, zWriter);
  177. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  178. zWriter->schreibe((char*)&block->seblingTicks, 4);
  179. }
  180. Item* GrowingPlantBlockType::createItem() const
  181. {
  182. return 0;
  183. }
  184. Block* GrowingPlantBlockType::createBlock(
  185. Framework::Vec3<int> position, int dimensionId) const
  186. {
  187. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  188. zTool,
  189. position,
  190. dimensionId,
  191. ticksNeeded,
  192. readableName,
  193. blockTypeAfterGrowth);
  194. for (const GrowthState& state : states)
  195. {
  196. block->addGrowthState(state);
  197. }
  198. return block;
  199. }