GrowingPlant.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "GrowingPlant.h"
  2. #include "Game.h"
  3. GrowthState::GrowthState(float percentage, ModelInfo* model)
  4. : ReferenceCounter(),
  5. percentage(percentage),
  6. model(model)
  7. {}
  8. GrowthState::~GrowthState()
  9. {
  10. model->release();
  11. }
  12. float GrowthState::getPercentage() const
  13. {
  14. return percentage;
  15. }
  16. ModelInfo* GrowthState::zModel() const
  17. {
  18. return model;
  19. }
  20. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  21. Framework::Vec3<int> pos,
  22. int dimensionId,
  23. int maxTicks,
  24. Framework::Text name,
  25. int blockTypeAfterGrowth)
  26. : Block(typeId, pos, dimensionId, 0),
  27. seblingTicks(0),
  28. seblingTicksMax(maxTicks),
  29. name(name),
  30. states(),
  31. blockTypeAfterGrowth(blockTypeAfterGrowth),
  32. plantSpawned(0),
  33. lastSendState(-1)
  34. {
  35. tickSource = 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. 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. Framework::Text GrowingPlantBlock::getTargetUIML()
  100. {
  101. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  102. + "\n" + "Growth: "
  103. + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  104. + "%</text></targetInfo>";
  105. }
  106. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState* state)
  107. {
  108. int index = 0;
  109. for (GrowthState* s : states)
  110. {
  111. if (s->getPercentage() > state->getPercentage())
  112. {
  113. states.add(state, index);
  114. return this;
  115. }
  116. index++;
  117. }
  118. states.add(state);
  119. return this;
  120. }
  121. GrowingPlantBlockType::GrowingPlantBlockType(ModelInfo* model,
  122. Framework::Text name,
  123. Framework::Text blockTypeNameAfterGrowth,
  124. const char* readableName,
  125. int ticksNeeded,
  126. int mapColor,
  127. float hardness,
  128. Framework::RCArray<Framework::Text> groupNames)
  129. : BlockType(
  130. 0, model, true, 10, false, name, true, mapColor, groupNames, hardness),
  131. transparent(1),
  132. passable(1),
  133. speedModifier(0.3f),
  134. interactable(1),
  135. states(),
  136. blockTypeNameAfterGrowth(blockTypeNameAfterGrowth),
  137. readableName(readableName),
  138. ticksNeeded(ticksNeeded)
  139. {}
  140. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  141. {
  142. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  143. block->transparent = transparent;
  144. block->passable = passable;
  145. block->hardness = getHardness();
  146. block->speedModifier = speedModifier;
  147. block->interactable = interactable;
  148. BlockType::createSuperBlock(zBlock, zItem);
  149. }
  150. void GrowingPlantBlockType::loadSuperBlock(
  151. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  152. {
  153. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  154. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  155. zReader->lese((char*)&block->seblingTicks, 4);
  156. }
  157. void GrowingPlantBlockType::saveSuperBlock(
  158. Block* zBlock, Framework::StreamWriter* zWriter) const
  159. {
  160. BlockType::saveSuperBlock(zBlock, zWriter);
  161. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  162. zWriter->schreibe((char*)&block->seblingTicks, 4);
  163. }
  164. Item* GrowingPlantBlockType::createItem() const
  165. {
  166. return 0;
  167. }
  168. Block* GrowingPlantBlockType::createBlock(
  169. Framework::Vec3<int> position, int dimensionId) const
  170. {
  171. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  172. position,
  173. dimensionId,
  174. ticksNeeded,
  175. readableName,
  176. blockTypeIdAfterGrowth);
  177. for (GrowthState* state : states)
  178. {
  179. block->addGrowthState(dynamic_cast<GrowthState*>(state->getThis()));
  180. }
  181. return block;
  182. }
  183. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  184. float growthPercentage, ModelInfo* model)
  185. {
  186. states.add(new GrowthState(growthPercentage, model));
  187. return this;
  188. }
  189. Framework::Text GrowingPlantBlockType::getBlockTypeNameAfterGrowth() const
  190. {
  191. return blockTypeNameAfterGrowth;
  192. }
  193. const char* GrowingPlantBlockType::getReadableName() const
  194. {
  195. return readableName;
  196. }
  197. int GrowingPlantBlockType::getTicksNeeded() const
  198. {
  199. return ticksNeeded;
  200. }
  201. const Framework::RCArray<GrowthState>& GrowingPlantBlockType::getStates() const
  202. {
  203. return states;
  204. }
  205. ItemType* GrowingPlantBlockType::createItemType() const
  206. {
  207. return 0;
  208. }
  209. GrowingPlantBlockTypeFactory::GrowingPlantBlockTypeFactory()
  210. : SubTypeFactory()
  211. {}
  212. GrowingPlantBlockType* GrowingPlantBlockTypeFactory::fromJson(
  213. Framework::JSON::JSONObject* zJson) const
  214. {
  215. Framework::RCArray<Framework::Text> groupNames;
  216. for (Framework::JSON::JSONValue* value :
  217. *zJson->zValue("groupNames")->asArray())
  218. {
  219. groupNames.add(new Framework::Text(value->asString()->getString()));
  220. }
  221. GrowingPlantBlockType* result = new GrowingPlantBlockType(
  222. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  223. zJson->zValue("model")),
  224. zJson->zValue("name")->asString()->getString(),
  225. zJson->zValue("blockTypeAfterGrowth")->asString()->getString(),
  226. zJson->zValue("readableName")->asString()->getString(),
  227. (int)zJson->zValue("ticksNeeded")->asNumber()->getNumber(),
  228. (int)zJson->zValue("mapColor")->asString()->getString(),
  229. (float)zJson->zValue("hardness")->asNumber()->getNumber(),
  230. groupNames);
  231. for (Framework::JSON::JSONValue* state :
  232. *zJson->zValue("states")->asArray())
  233. {
  234. result->addGrowthState((float)state->asObject()
  235. ->zValue("percentage")
  236. ->asNumber()
  237. ->getNumber(),
  238. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  239. state->asObject()->zValue("model")));
  240. }
  241. return result;
  242. }
  243. Framework::JSON::JSONObject* GrowingPlantBlockTypeFactory::toJson(
  244. GrowingPlantBlockType* zObject) const
  245. {
  246. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  247. result->addValue("readableName",
  248. new Framework::JSON::JSONString(zObject->getReadableName()));
  249. result->addValue(
  250. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  251. result->addValue(
  252. "name", new Framework::JSON::JSONString(zObject->getName()));
  253. result->addValue(
  254. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  255. result->addValue(
  256. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  257. result->addValue("blockTypeAfterGrowth",
  258. new Framework::JSON::JSONString(
  259. zObject->getBlockTypeNameAfterGrowth()));
  260. result->addValue("ticksNeeded",
  261. new Framework::JSON::JSONNumber((double)zObject->getTicksNeeded()));
  262. Framework::JSON::JSONArray* states = new Framework::JSON::JSONArray();
  263. for (GrowthState* state : zObject->getStates())
  264. {
  265. Framework::JSON::JSONObject* stateObj
  266. = new Framework::JSON::JSONObject();
  267. stateObj->addValue(
  268. "model", Game::INSTANCE->zTypeRegistry()->toJson(state->zModel()));
  269. stateObj->addValue("percentage",
  270. new Framework::JSON::JSONNumber(state->getPercentage()));
  271. states->addValue(stateObj);
  272. }
  273. result->addValue("states", states);
  274. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  275. for (Framework::Text* groupName : zObject->getGroupNames())
  276. {
  277. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  278. }
  279. result->addValue("groupNames", groupNames);
  280. return result;
  281. }
  282. Framework::JSON::Validator::JSONValidator*
  283. GrowingPlantBlockTypeFactory::getValidator(
  284. Framework::JSON::Validator::ObjectValidationBuilder<
  285. Framework::JSON::Validator::JSONValidator>* builder) const
  286. {
  287. return builder
  288. ->withRequiredAttribute(
  289. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  290. ->withRequiredString("name")
  291. ->finishString()
  292. ->withRequiredNumber("hardness")
  293. ->withDefault(1.0)
  294. ->finishNumber()
  295. ->withRequiredString("mapColor")
  296. ->finishString()
  297. ->withRequiredString("readableName")
  298. ->finishString()
  299. ->withRequiredNumber("ticksNeeded")
  300. ->finishNumber()
  301. ->withRequiredString("blockTypeAfterGrowth")
  302. ->finishString()
  303. ->withRequiredArray("states")
  304. ->addAcceptedObjectInArray()
  305. ->withRequiredNumber("percentage")
  306. ->whichIsGreaterOrEqual(0.0)
  307. ->whichIsLessOrEqual(1.0)
  308. ->finishNumber()
  309. ->withRequiredAttribute(
  310. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  311. ->finishObject()
  312. ->finishArray()
  313. ->withRequiredArray("groupNames")
  314. ->withDefault(new Framework::JSON::JSONArray())
  315. ->addAcceptedStringInArray()
  316. ->finishString()
  317. ->finishArray()
  318. ->finishObject();
  319. }
  320. Framework::Text GrowingPlantBlockTypeFactory::getTypeToken() const
  321. {
  322. return "growingPlant";
  323. }