GrowingPlant.cpp 11 KB

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