GrowingPlant.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "GrowingPlant.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "Recipie.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 new Framework::XML::Element(
  106. Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  107. + name + "\n" + "Growth: "
  108. + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  109. + "%</text></targetInfo>");
  110. }
  111. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState* state)
  112. {
  113. int index = 0;
  114. for (GrowthState* s : states)
  115. {
  116. if (s->getPercentage() > state->getPercentage())
  117. {
  118. states.add(state, index);
  119. return this;
  120. }
  121. index++;
  122. }
  123. states.add(state);
  124. return this;
  125. }
  126. GrowingPlantBlockType::GrowingPlantBlockType()
  127. : BlockType(),
  128. transparent(1),
  129. passable(1),
  130. speedModifier(0.3f),
  131. interactable(1),
  132. ticksNeeded(0)
  133. {}
  134. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  135. {
  136. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  137. block->transparent = transparent;
  138. block->passable = passable;
  139. block->hardness = getHardness();
  140. block->speedModifier = speedModifier;
  141. block->interactable = interactable;
  142. BlockType::createSuperBlock(zBlock, zItem);
  143. }
  144. void GrowingPlantBlockType::loadSuperBlock(
  145. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  146. {
  147. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  148. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  149. zReader->lese((char*)&block->seblingTicks, 4);
  150. }
  151. void GrowingPlantBlockType::saveSuperBlock(
  152. Block* zBlock, Framework::StreamWriter* zWriter) const
  153. {
  154. BlockType::saveSuperBlock(zBlock, zWriter);
  155. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  156. zWriter->schreibe((char*)&block->seblingTicks, 4);
  157. }
  158. Item* GrowingPlantBlockType::createItem() const
  159. {
  160. return 0;
  161. }
  162. Block* GrowingPlantBlockType::createBlock(
  163. Framework::Vec3<int> position, int dimensionId) const
  164. {
  165. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  166. position,
  167. dimensionId,
  168. ticksNeeded,
  169. readableName,
  170. blockTypeIdAfterGrowth);
  171. for (GrowthState* state : states)
  172. {
  173. block->addGrowthState(dynamic_cast<GrowthState*>(state->getThis()));
  174. }
  175. return block;
  176. }
  177. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  178. float growthPercentage, ModelInfo* model)
  179. {
  180. states.add(new GrowthState(growthPercentage, model));
  181. return this;
  182. }
  183. Framework::Text GrowingPlantBlockType::getBlockTypeNameAfterGrowth() const
  184. {
  185. return blockTypeNameAfterGrowth;
  186. }
  187. void GrowingPlantBlockType::setReadableName(Framework::Text readableName)
  188. {
  189. this->readableName = readableName;
  190. }
  191. Framework::Text GrowingPlantBlockType::getReadableName() const
  192. {
  193. return readableName;
  194. }
  195. void GrowingPlantBlockType::setTicksNeeded(int ticksNeeded)
  196. {
  197. this->ticksNeeded = ticksNeeded;
  198. }
  199. int GrowingPlantBlockType::getTicksNeeded() const
  200. {
  201. return ticksNeeded;
  202. }
  203. void GrowingPlantBlockType::setTransparent(bool transparent)
  204. {
  205. this->transparent = transparent;
  206. }
  207. bool GrowingPlantBlockType::isTransparent() const
  208. {
  209. return transparent;
  210. }
  211. void GrowingPlantBlockType::setPassable(bool passable)
  212. {
  213. this->passable = passable;
  214. }
  215. bool GrowingPlantBlockType::isPassable() const
  216. {
  217. return passable;
  218. }
  219. void GrowingPlantBlockType::setSpeedModifier(float speedModifier)
  220. {
  221. this->speedModifier = speedModifier;
  222. }
  223. float GrowingPlantBlockType::getSpeedModifier() const
  224. {
  225. return speedModifier;
  226. }
  227. void GrowingPlantBlockType::setInteractable(bool interactable)
  228. {
  229. this->interactable = interactable;
  230. }
  231. bool GrowingPlantBlockType::isInteractable() const
  232. {
  233. return interactable;
  234. }
  235. const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
  236. {
  237. return states;
  238. }
  239. void GrowingPlantBlockType::setBlockTypeNameAfterGrowth(
  240. Framework::Text blockTypeNameAfterGrowth)
  241. {
  242. this->blockTypeNameAfterGrowth = blockTypeNameAfterGrowth;
  243. }
  244. bool GrowingPlantBlockType::initialize(Game* zGame)
  245. {
  246. blockTypeIdAfterGrowth = zGame->getBlockTypeId(blockTypeNameAfterGrowth);
  247. return blockTypeIdAfterGrowth >= 0 && BlockType::initialize(zGame);
  248. }
  249. ItemType* GrowingPlantBlockType::createItemType() const
  250. {
  251. return 0;
  252. }
  253. GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
  254. : BlockTypeFactoryBase()
  255. {}
  256. GrowingPlantBlockType* GrowingPlantBlockTypeFactory::createValue(
  257. Framework::JSON::JSONObject* zJson) const
  258. {
  259. return new GrowingPlantBlockType();
  260. }
  261. GrowingPlantBlockType* GrowingPlantBlockTypeFactory::fromJson(
  262. Framework::JSON::JSONObject* zJson) const
  263. {
  264. GrowingPlantBlockType* result = BlockTypeFactoryBase::fromJson(zJson);
  265. result->setBlockTypeNameAfterGrowth(
  266. zJson->zValue("blockTypeAfterGrowth")->asString()->getString());
  267. result->setReadableName(
  268. zJson->zValue("readableName")->asString()->getString());
  269. result->setTicksNeeded(
  270. (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber());
  271. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  272. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  273. result->setSpeedModifier(
  274. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  275. result->setInteractable(zJson->zValue("interactable")->asBool()->getBool());
  276. for (Framework::JSON::JSONValue* state :
  277. *zJson->zValue("states")->asArray())
  278. {
  279. result->addGrowthState((float)state->asObject()
  280. ->zValue("percentage")
  281. ->asNumber()
  282. ->getNumber(),
  283. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  284. state->asObject()->zValue("model")));
  285. }
  286. return result;
  287. }
  288. Framework::JSON::JSONObject* GrowingPlantBlockTypeFactory::toJsonObject(
  289. GrowingPlantBlockType* zObject) const
  290. {
  291. Framework::JSON::JSONObject* result
  292. = BlockTypeFactoryBase::toJsonObject(zObject);
  293. result->addValue("readableName",
  294. new Framework::JSON::JSONString(zObject->getReadableName()));
  295. result->addValue(
  296. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  297. result->addValue(
  298. "name", new Framework::JSON::JSONString(zObject->getName()));
  299. result->addValue(
  300. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  301. result->addValue(
  302. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  303. result->addValue("blockTypeAfterGrowth",
  304. new Framework::JSON::JSONString(
  305. zObject->getBlockTypeNameAfterGrowth()));
  306. result->addValue("ticksNeeded",
  307. new Framework::JSON::JSONNumber((double)zObject->getTicksNeeded()));
  308. Framework::JSON::JSONArray* states = new Framework::JSON::JSONArray();
  309. for (GrowthState* state : zObject->getStates())
  310. {
  311. Framework::JSON::JSONObject* stateObj
  312. = new Framework::JSON::JSONObject();
  313. stateObj->addValue(
  314. "model", Game::INSTANCE->zTypeRegistry()->toJson(state->zModel()));
  315. stateObj->addValue("percentage",
  316. new Framework::JSON::JSONNumber(state->getPercentage()));
  317. states->addValue(stateObj);
  318. }
  319. result->addValue("states", states);
  320. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  321. for (Framework::Text* groupName : zObject->getGroupNames())
  322. {
  323. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  324. }
  325. result->addValue("groupNames", groupNames);
  326. return result;
  327. }
  328. JSONObjectValidationBuilder* GrowingPlantBlockTypeFactory::addToValidator(
  329. JSONObjectValidationBuilder* builder) const
  330. {
  331. return BlockTypeFactoryBase::addToValidator(
  332. builder->withRequiredString("readableName")
  333. ->finishString()
  334. ->withRequiredNumber("ticksNeeded")
  335. ->finishNumber()
  336. ->withRequiredAttribute("blockTypeAfterGrowth",
  337. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  338. BlockTypeNameFactory::TYPE_ID))
  339. ->withRequiredArray("states")
  340. ->addAcceptedObjectInArray()
  341. ->withRequiredNumber("percentage")
  342. ->whichIsGreaterOrEqual(0.0)
  343. ->whichIsLessOrEqual(1.0)
  344. ->finishNumber()
  345. ->withRequiredAttribute("model",
  346. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  347. ->finishObject()
  348. ->finishArray()
  349. ->withRequiredBool("transparent")
  350. ->withDefault(true)
  351. ->finishBool()
  352. ->withRequiredBool("passable")
  353. ->withDefault(true)
  354. ->finishBool()
  355. ->withRequiredNumber("speedModifier")
  356. ->withDefault(0.5)
  357. ->finishNumber()
  358. ->withRequiredBool("interactable")
  359. ->withDefault(true)
  360. ->finishBool());
  361. }
  362. const char* GrowingPlantBlockTypeFactory::getTypeToken() const
  363. {
  364. return "growingPlant";
  365. }