GrowingPlant.cpp 12 KB

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