GrowingPlant.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "GrowingPlant.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. GrowthState::GrowthState(float percentage, ModelInfo* model)
  5. : ReferenceCounter(),
  6. percentage(percentage),
  7. model(model)
  8. {}
  9. GrowthState::~GrowthState()
  10. {
  11. model->release();
  12. }
  13. float GrowthState::getPercentage() const
  14. {
  15. return percentage;
  16. }
  17. ModelInfo* GrowthState::zModel() const
  18. {
  19. return model;
  20. }
  21. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  22. Framework::Vec3<int> pos,
  23. int dimensionId,
  24. int maxTicks,
  25. Framework::Text name,
  26. int blockTypeAfterGrowth)
  27. : Block(typeId, pos, dimensionId, 0),
  28. seblingTicks(0),
  29. seblingTicksMax(maxTicks),
  30. name(name),
  31. states(),
  32. blockTypeAfterGrowth(blockTypeAfterGrowth),
  33. plantSpawned(0),
  34. lastSendState(-1)
  35. {
  36. tickSource = 1;
  37. }
  38. bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  39. {
  40. float beforePercentage = seblingTicks / (float)seblingTicksMax;
  41. seblingTicks += (float)numTicks;
  42. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f)
  43. != (int)(beforePercentage * 100.f))
  44. {
  45. Game::INSTANCE->blockTargetChanged(this);
  46. }
  47. int index = 0;
  48. int currentIndex = 0;
  49. for (GrowthState* state : states)
  50. {
  51. if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
  52. {
  53. currentIndex = index;
  54. }
  55. else
  56. {
  57. break;
  58. }
  59. index++;
  60. }
  61. if (lastSendState != currentIndex)
  62. {
  63. updateModel(states.z(currentIndex)->zModel());
  64. lastSendState = currentIndex;
  65. }
  66. return 1;
  67. }
  68. void GrowingPlantBlock::onPostTick()
  69. {
  70. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  71. {
  72. plantSpawned = 1;
  73. Game::INSTANCE->doLater([this]() {
  74. Game::INSTANCE->zDimension(getDimensionId())
  75. ->placeBlock(getPos(), blockTypeAfterGrowth);
  76. });
  77. }
  78. }
  79. void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage)
  80. {
  81. GrowthState* current = 0;
  82. for (GrowthState* state : states)
  83. {
  84. if (state->getPercentage() <= seblingTicks / (float)seblingTicksMax)
  85. {
  86. current = state;
  87. }
  88. }
  89. if (current)
  90. {
  91. zMessage->addressBlock(this);
  92. Framework::InMemoryBuffer buffer;
  93. current->zModel()->writeTo(&buffer);
  94. char* msg = new char[(int)buffer.getSize() + 1];
  95. msg[0] = 1; // hmodel change
  96. buffer.lese(msg + 1, (int)buffer.getSize());
  97. zMessage->setMessage(msg, (int)buffer.getSize() + 1);
  98. }
  99. }
  100. Framework::Text GrowingPlantBlock::getTargetUIML()
  101. {
  102. return Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  103. + name + "\n" + "Growth: "
  104. + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  105. + "%</text></targetInfo>";
  106. }
  107. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState* state)
  108. {
  109. int index = 0;
  110. for (GrowthState* s : states)
  111. {
  112. if (s->getPercentage() > state->getPercentage())
  113. {
  114. states.add(state, index);
  115. return this;
  116. }
  117. index++;
  118. }
  119. states.add(state);
  120. return this;
  121. }
  122. GrowingPlantBlockType::GrowingPlantBlockType()
  123. : BlockType(),
  124. transparent(1),
  125. passable(1),
  126. speedModifier(0.3f),
  127. interactable(1),
  128. ticksNeeded(0)
  129. {}
  130. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  131. {
  132. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  133. block->transparent = transparent;
  134. block->passable = passable;
  135. block->hardness = getHardness();
  136. block->speedModifier = speedModifier;
  137. block->interactable = interactable;
  138. BlockType::createSuperBlock(zBlock, zItem);
  139. }
  140. void GrowingPlantBlockType::loadSuperBlock(
  141. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  142. {
  143. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  144. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  145. zReader->lese((char*)&block->seblingTicks, 4);
  146. }
  147. void GrowingPlantBlockType::saveSuperBlock(
  148. Block* zBlock, Framework::StreamWriter* zWriter) const
  149. {
  150. BlockType::saveSuperBlock(zBlock, zWriter);
  151. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  152. zWriter->schreibe((char*)&block->seblingTicks, 4);
  153. }
  154. Item* GrowingPlantBlockType::createItem() const
  155. {
  156. return 0;
  157. }
  158. Block* GrowingPlantBlockType::createBlock(
  159. Framework::Vec3<int> position, int dimensionId) const
  160. {
  161. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  162. position,
  163. dimensionId,
  164. ticksNeeded,
  165. readableName,
  166. blockTypeIdAfterGrowth);
  167. for (GrowthState* state : states)
  168. {
  169. block->addGrowthState(dynamic_cast<GrowthState*>(state->getThis()));
  170. }
  171. return block;
  172. }
  173. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  174. float growthPercentage, ModelInfo* model)
  175. {
  176. states.add(new GrowthState(growthPercentage, model));
  177. return this;
  178. }
  179. Framework::Text GrowingPlantBlockType::getBlockTypeNameAfterGrowth() const
  180. {
  181. return blockTypeNameAfterGrowth;
  182. }
  183. void GrowingPlantBlockType::setReadableName(Framework::Text readableName)
  184. {
  185. this->readableName = readableName;
  186. }
  187. Framework::Text GrowingPlantBlockType::getReadableName() const
  188. {
  189. return readableName;
  190. }
  191. void GrowingPlantBlockType::setTicksNeeded(int ticksNeeded)
  192. {
  193. this->ticksNeeded = ticksNeeded;
  194. }
  195. int GrowingPlantBlockType::getTicksNeeded() const
  196. {
  197. return ticksNeeded;
  198. }
  199. void GrowingPlantBlockType::setTransparent(bool transparent)
  200. {
  201. this->transparent = transparent;
  202. }
  203. bool GrowingPlantBlockType::isTransparent() const
  204. {
  205. return transparent;
  206. }
  207. void GrowingPlantBlockType::setPassable(bool passable)
  208. {
  209. this->passable = passable;
  210. }
  211. bool GrowingPlantBlockType::isPassable() const
  212. {
  213. return passable;
  214. }
  215. void GrowingPlantBlockType::setSpeedModifier(float speedModifier)
  216. {
  217. this->speedModifier = speedModifier;
  218. }
  219. float GrowingPlantBlockType::getSpeedModifier() const
  220. {
  221. return speedModifier;
  222. }
  223. void GrowingPlantBlockType::setInteractable(bool interactable)
  224. {
  225. this->interactable = interactable;
  226. }
  227. bool GrowingPlantBlockType::isInteractable() const
  228. {
  229. return interactable;
  230. }
  231. const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
  232. {
  233. return states;
  234. }
  235. void GrowingPlantBlockType::setBlockTypeNameAfterGrowth(
  236. Framework::Text blockTypeIdAfterGrowth)
  237. {}
  238. ItemType* GrowingPlantBlockType::createItemType() const
  239. {
  240. return 0;
  241. }
  242. GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
  243. : BlockTypeFactoryBase()
  244. {}
  245. GrowingPlantBlockType* GrowingPlantBlockTypeFactory::createValue(
  246. Framework::JSON::JSONObject* zJson) const
  247. {
  248. return new GrowingPlantBlockType();
  249. }
  250. void GrowingPlantBlockTypeFactory::fromJson(
  251. GrowingPlantBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  252. {
  253. zResult->setBlockTypeNameAfterGrowth(
  254. zJson->zValue("blockTypeAfterGrowth")->asString()->getString());
  255. zResult->setReadableName(
  256. zJson->zValue("readableName")->asString()->getString());
  257. zResult->setTicksNeeded(
  258. (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber());
  259. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  260. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  261. zResult->setSpeedModifier(
  262. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  263. zResult->setInteractable(
  264. zJson->zValue("interactable")->asBool()->getBool());
  265. for (Framework::JSON::JSONValue* state :
  266. *zJson->zValue("states")->asArray())
  267. {
  268. zResult->addGrowthState((float)state->asObject()
  269. ->zValue("percentage")
  270. ->asNumber()
  271. ->getNumber(),
  272. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  273. state->asObject()->zValue("model")));
  274. }
  275. BlockTypeFactoryBase::fromJson(zResult, zJson);
  276. }
  277. void GrowingPlantBlockTypeFactory::toJson(
  278. GrowingPlantBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  279. {
  280. zResult->addValue("readableName",
  281. new Framework::JSON::JSONString(zObject->getReadableName()));
  282. zResult->addValue(
  283. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  284. zResult->addValue(
  285. "name", new Framework::JSON::JSONString(zObject->getName()));
  286. zResult->addValue(
  287. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  288. zResult->addValue(
  289. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  290. zResult->addValue("blockTypeAfterGrowth",
  291. new Framework::JSON::JSONString(
  292. zObject->getBlockTypeNameAfterGrowth()));
  293. zResult->addValue("ticksNeeded",
  294. new Framework::JSON::JSONNumber((double)zObject->getTicksNeeded()));
  295. Framework::JSON::JSONArray* states = new Framework::JSON::JSONArray();
  296. for (GrowthState* state : zObject->getStates())
  297. {
  298. Framework::JSON::JSONObject* stateObj
  299. = new Framework::JSON::JSONObject();
  300. stateObj->addValue(
  301. "model", Game::INSTANCE->zTypeRegistry()->toJson(state->zModel()));
  302. stateObj->addValue("percentage",
  303. new Framework::JSON::JSONNumber(state->getPercentage()));
  304. states->addValue(stateObj);
  305. }
  306. zResult->addValue("states", states);
  307. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  308. for (Framework::Text* groupName : zObject->getGroupNames())
  309. {
  310. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  311. }
  312. zResult->addValue("groupNames", groupNames);
  313. }
  314. JSONObjectValidationBuilder* GrowingPlantBlockTypeFactory::addToValidator(
  315. JSONObjectValidationBuilder* builder) const
  316. {
  317. return BlockTypeFactoryBase::addToValidator(
  318. builder->withRequiredString("readableName")
  319. ->finishString()
  320. ->withRequiredNumber("ticksNeeded")
  321. ->finishNumber()
  322. ->withRequiredString("blockTypeAfterGrowth")
  323. ->finishString()
  324. ->withRequiredArray("states")
  325. ->addAcceptedObjectInArray()
  326. ->withRequiredNumber("percentage")
  327. ->whichIsGreaterOrEqual(0.0)
  328. ->whichIsLessOrEqual(1.0)
  329. ->finishNumber()
  330. ->withRequiredAttribute("model",
  331. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  332. ->finishObject()
  333. ->finishArray()
  334. ->withRequiredBool("transparent")
  335. ->withDefault(true)
  336. ->finishBool()
  337. ->withRequiredBool("passable")
  338. ->withDefault(true)
  339. ->finishBool()
  340. ->withRequiredNumber("speedModifier")
  341. ->withDefault(0.5)
  342. ->finishNumber()
  343. ->withRequiredBool("interactable")
  344. ->withDefault(true)
  345. ->finishBool());
  346. }
  347. Framework::Text GrowingPlantBlockTypeFactory::getTypeToken() const
  348. {
  349. return "growingPlant";
  350. }