FluidBlock.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "FluidBlock.h"
  2. #include "FluidContainer.h"
  3. #include "Game.h"
  4. FluidBlock::FluidBlock(int typeId,
  5. Framework::Vec3<int> pos,
  6. int dimensionId,
  7. Vec3<float> lightWeights)
  8. : Block(typeId, pos, dimensionId, 0),
  9. lightWeights(lightWeights),
  10. neighborChanged(1),
  11. nextFlow(0),
  12. maxFlowDistance(8)
  13. {
  14. transparent = 1;
  15. passable = 1;
  16. hp = 1;
  17. maxHP = 1;
  18. hardness = -1.f;
  19. speedModifier = 0.5f;
  20. tickSource = 1;
  21. interactable = 0;
  22. flowOptions = 0;
  23. distanceToSource = 0;
  24. }
  25. FluidBlock::~FluidBlock() {}
  26. bool FluidBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  27. {
  28. if (neighborChanged)
  29. {
  30. nextFlow -= numTicks;
  31. if (nextFlow <= 0)
  32. {
  33. const FluidBlockType* zType
  34. = dynamic_cast<const FluidBlockType*>(zBlockType());
  35. if (zType)
  36. {
  37. nextFlow = zType->getTicktsToFlow();
  38. }
  39. else
  40. {
  41. nextFlow = 0;
  42. }
  43. neighborChanged = 0;
  44. doFlow();
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. void FluidBlock::onPostTick() {}
  51. void FluidBlock::setNeighbourType(Direction dir, int type)
  52. {
  53. Block::setNeighbourType(dir, type);
  54. neighborChanged = 1;
  55. }
  56. void FluidBlock::sendModelInfo(NetworkMessage* zMessage)
  57. {
  58. zMessage->addressBlock(this);
  59. char* msg = new char[3];
  60. msg[0] = 2; // fluid amount change
  61. *(msg + 1) = flowOptions;
  62. *(msg + 2) = distanceToSource;
  63. zMessage->setMessage(msg, 3);
  64. }
  65. void FluidBlock::doFlow()
  66. {
  67. bool doesFlowSidewards = 1;
  68. if (((zNeighbours[getDirectionIndex(Direction::BOTTOM)]
  69. && zNeighbours[getDirectionIndex(Direction::BOTTOM)]
  70. ->zBlockType()
  71. == zBlockType()
  72. || neighbourTypes[getDirectionIndex(Direction::BOTTOM)]
  73. == BlockTypeEnum::AIR)
  74. && distanceToSource)
  75. || distanceToSource >= maxFlowDistance)
  76. {
  77. doesFlowSidewards = 0;
  78. }
  79. bool changed = false;
  80. int minNeighborDistance = maxFlowDistance;
  81. for (int i = 0; i < 6; i++)
  82. {
  83. Direction dir = getDirectionFromIndex(i);
  84. if (dir & Direction::TOP)
  85. {
  86. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  87. {
  88. FluidBlock* neighbour
  89. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  90. minNeighborDistance = 0;
  91. distanceToSource = neighbour->distanceToSource + 1;
  92. flowOptions = (char)getOppositeDirection(dir);
  93. changed = true;
  94. }
  95. continue;
  96. }
  97. if (neighbourTypes[i] == BlockTypeEnum::AIR)
  98. {
  99. if (dir & Direction::BOTTOM || doesFlowSidewards)
  100. {
  101. Game::INSTANCE->doLater([this, dir, i]() {
  102. if (neighbourTypes[i] == BlockTypeEnum::AIR)
  103. {
  104. Block* belowBlock = zBlockType()->createBlockAt(
  105. getPos() + getDirection(dir), getDimensionId(), 0);
  106. FluidBlock* fluidBlock
  107. = dynamic_cast<FluidBlock*>(belowBlock);
  108. if (fluidBlock)
  109. {
  110. fluidBlock->distanceToSource
  111. = dir & Direction::BOTTOM
  112. ? 1
  113. : distanceToSource + 1;
  114. fluidBlock->flowOptions = (char)dir;
  115. Game::INSTANCE->zDimension(getDimensionId())
  116. ->placeBlock(
  117. getPos() + getDirection(dir), belowBlock);
  118. }
  119. else
  120. {
  121. std::cout
  122. << "ERROR: created flow fuild block is not an "
  123. "instance of FluidBlock\n";
  124. belowBlock->release();
  125. }
  126. }
  127. });
  128. }
  129. }
  130. else if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  131. {
  132. if (dir & Direction::BOTTOM) continue;
  133. FluidBlock* neighbour = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  134. if (neighbour)
  135. {
  136. if (distanceToSource > neighbour->distanceToSource + 1)
  137. {
  138. distanceToSource = neighbour->distanceToSource + 1;
  139. flowOptions = (char)getOppositeDirection(dir);
  140. changed = true;
  141. }
  142. else if (distanceToSource == neighbour->distanceToSource + 1)
  143. {
  144. char tmp = flowOptions;
  145. flowOptions |= (char)getOppositeDirection(dir);
  146. changed |= tmp != flowOptions;
  147. }
  148. if (neighbour->distanceToSource < minNeighborDistance)
  149. {
  150. minNeighborDistance = neighbour->distanceToSource;
  151. }
  152. }
  153. }
  154. }
  155. if (distanceToSource)
  156. {
  157. if (minNeighborDistance + 1 > distanceToSource)
  158. {
  159. distanceToSource = minNeighborDistance + 1;
  160. flowOptions = 0; // reavaluated next tick
  161. changed = true;
  162. }
  163. }
  164. if (distanceToSource > maxFlowDistance)
  165. {
  166. distanceToSource = maxFlowDistance;
  167. Game::INSTANCE->doLater([this]() { setHP(0.f); });
  168. }
  169. if (changed)
  170. {
  171. broadcastModelInfoChange();
  172. neighborChanged = 1;
  173. for (int i = 0; i < 6; i++)
  174. {
  175. Direction dir = getDirectionFromIndex(i);
  176. if (dir & (Direction::TOP | Direction::BOTTOM)) continue;
  177. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  178. {
  179. FluidBlock* neighbour
  180. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  181. if (neighbour)
  182. {
  183. neighbour->neighborChanged = 1;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. bool FluidBlock::isInteractable(const Item* zItem) const
  190. {
  191. return distanceToSource == 0
  192. && dynamic_cast<const FluidContainerItem*>(zItem);
  193. }
  194. void FluidBlock::filterPassingLight(unsigned char rgb[3]) const
  195. {
  196. rgb[0] = (unsigned char)(rgb[0] * lightWeights.x);
  197. rgb[1] = (unsigned char)(rgb[1] * lightWeights.y);
  198. rgb[2] = (unsigned char)(rgb[2] * lightWeights.z);
  199. }
  200. char FluidBlock::getDistanceToSource() const
  201. {
  202. return distanceToSource;
  203. }
  204. char FluidBlock::getFlowOptions() const
  205. {
  206. return flowOptions;
  207. }
  208. FluidBlockType::FluidBlockType(ModelInfo* model,
  209. Framework::Text name,
  210. int mapColor,
  211. Vec3<float> lightWeights,
  212. int ticktsToFlow,
  213. unsigned char flowDistance,
  214. float hpRecoveryPerL,
  215. float thirstRecoveryPerL,
  216. float heat,
  217. Framework::RCArray<Framework::Text> groupNames)
  218. : BlockType(0, model, 1, 10, 0, name, true, mapColor, groupNames, 0.f),
  219. lightWeights(lightWeights),
  220. ticktsToFlow(ticktsToFlow),
  221. flowDistance(flowDistance),
  222. hungerRecoveryPerL(hungerRecoveryPerL),
  223. thirstRecoveryPerL(thirstRecoveryPerL),
  224. heat(heat)
  225. {}
  226. void FluidBlockType::loadSuperBlock(
  227. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  228. {
  229. FluidBlock* block = dynamic_cast<FluidBlock*>(zBlock);
  230. zReader->lese(&block->flowOptions, 1);
  231. zReader->lese(&block->distanceToSource, 1);
  232. block->nextFlow = ticktsToFlow;
  233. block->maxFlowDistance = flowDistance;
  234. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  235. }
  236. void FluidBlockType::saveSuperBlock(
  237. Block* zBlock, Framework::StreamWriter* zWriter) const
  238. {
  239. FluidBlock* block = dynamic_cast<FluidBlock*>(zBlock);
  240. zWriter->schreibe(&block->flowOptions, 1);
  241. zWriter->schreibe(&block->distanceToSource, 1);
  242. BlockType::saveSuperBlock(zBlock, zWriter);
  243. }
  244. Item* FluidBlockType::createItem() const
  245. {
  246. return 0;
  247. }
  248. Block* FluidBlockType::createBlock(
  249. Framework::Vec3<int> position, int dimensionId) const
  250. {
  251. FluidBlock* result
  252. = new FluidBlock(getId(), position, dimensionId, lightWeights);
  253. result->nextFlow = ticktsToFlow;
  254. result->maxFlowDistance = flowDistance;
  255. return result;
  256. }
  257. bool FluidBlockType::isFluid() const
  258. {
  259. return true;
  260. }
  261. int FluidBlockType::getTicktsToFlow() const
  262. {
  263. return ticktsToFlow;
  264. }
  265. unsigned char FluidBlockType::getFlowDistance() const
  266. {
  267. return flowDistance;
  268. }
  269. Framework::Vec3<float> FluidBlockType::getLightWeights() const
  270. {
  271. return lightWeights;
  272. }
  273. float FluidBlockType::getHungerRecoveryPerL() const
  274. {
  275. return hungerRecoveryPerL;
  276. }
  277. float FluidBlockType::getThirstRecoveryPerL() const
  278. {
  279. return thirstRecoveryPerL;
  280. }
  281. ItemType* FluidBlockType::createItemType() const
  282. {
  283. return 0;
  284. }
  285. float FluidBlockType::getHeat() const
  286. {
  287. return heat;
  288. }
  289. FluidBlockTypeFactory::FluidBlockTypeFactory()
  290. : SubTypeFactory()
  291. {}
  292. FluidBlockType* FluidBlockTypeFactory::fromJson(
  293. Framework::JSON::JSONObject* zJson) const
  294. {
  295. Framework::RCArray<Framework::Text> groupNames;
  296. for (Framework::JSON::JSONValue* value :
  297. *zJson->zValue("groupNames")->asArray())
  298. {
  299. groupNames.add(new Framework::Text(value->asString()->getString()));
  300. }
  301. return new FluidBlockType(
  302. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  303. zJson->zValue("model")),
  304. zJson->zValue("name")->asString()->getString(),
  305. (int)zJson->zValue("mapColor")->asString()->getString(),
  306. Framework::Vec3<float>((float)zJson->zValue("lightWeight")
  307. ->asObject()
  308. ->zValue("red")
  309. ->asNumber()
  310. ->getNumber(),
  311. (float)zJson->zValue("lightWeight")
  312. ->asObject()
  313. ->zValue("green")
  314. ->asNumber()
  315. ->getNumber(),
  316. (float)zJson->zValue("lightWeight")
  317. ->asObject()
  318. ->zValue("blue")
  319. ->asNumber()
  320. ->getNumber()),
  321. (int)zJson->zValue("ticksToFlow")->asNumber()->getNumber(),
  322. (char)zJson->zValue("flowDistance")->asNumber()->getNumber(),
  323. (float)zJson->zValue("hungerRecoveryPerL")->asNumber()->getNumber(),
  324. (float)zJson->zValue("thirstRecoveryPerL")->asNumber()->getNumber(),
  325. (float)zJson->zValue("heat")->asNumber()->getNumber(),
  326. groupNames);
  327. }
  328. Framework::JSON::JSONObject* FluidBlockTypeFactory::toJson(
  329. FluidBlockType* zObject) const
  330. {
  331. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  332. result->addValue(
  333. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  334. result->addValue(
  335. "name", new Framework::JSON::JSONString(zObject->getName()));
  336. result->addValue(
  337. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  338. Framework::JSON::JSONObject* lightWeight
  339. = new Framework::JSON::JSONObject();
  340. lightWeight->addValue(
  341. "red", new Framework::JSON::JSONNumber(zObject->getLightWeights().x));
  342. lightWeight->addValue(
  343. "green", new Framework::JSON::JSONNumber(zObject->getLightWeights().y));
  344. lightWeight->addValue(
  345. "blue", new Framework::JSON::JSONNumber(zObject->getLightWeights().z));
  346. result->addValue("lightWeight", lightWeight);
  347. result->addValue("ticksToFlow",
  348. new Framework::JSON::JSONNumber((double)zObject->getTicktsToFlow()));
  349. result->addValue("flowDistance",
  350. new Framework::JSON::JSONNumber((double)zObject->getFlowDistance()));
  351. result->addValue("hungerRecoveryPerL",
  352. new Framework::JSON::JSONNumber(
  353. (double)zObject->getHungerRecoveryPerL()));
  354. result->addValue("thirstRecoveryPerL",
  355. new Framework::JSON::JSONNumber(
  356. (double)zObject->getThirstRecoveryPerL()));
  357. result->addValue("heat",
  358. new Framework::JSON::JSONNumber(
  359. (double)zObject->getHeat()));
  360. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  361. for (Framework::Text* groupName : zObject->getGroupNames())
  362. {
  363. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  364. }
  365. result->addValue("groupNames", groupNames);
  366. return result;
  367. }
  368. Framework::JSON::Validator::JSONValidator* FluidBlockTypeFactory::getValidator(
  369. Framework::JSON::Validator::ObjectValidationBuilder<
  370. Framework::JSON::Validator::JSONValidator>* builder) const
  371. {
  372. return builder
  373. ->withRequiredAttribute(
  374. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  375. ->withRequiredString("name")
  376. ->finishString()
  377. ->withRequiredString("mapColor")
  378. ->finishString()
  379. ->withRequiredObject("lightWeight")
  380. ->withRequiredNumber("red")
  381. ->whichIsGreaterOrEqual(0.0)
  382. ->whichIsLessOrEqual(1.0)
  383. ->finishNumber()
  384. ->withRequiredNumber("green")
  385. ->whichIsGreaterOrEqual(0.0)
  386. ->whichIsLessOrEqual(1.0)
  387. ->finishNumber()
  388. ->withRequiredNumber("blue")
  389. ->whichIsGreaterOrEqual(0.0)
  390. ->whichIsLessOrEqual(1.0)
  391. ->finishNumber()
  392. ->finishObject()
  393. ->withRequiredNumber("ticksToFlow")
  394. ->whichIsGreaterOrEqual(1.0)
  395. ->finishNumber()
  396. ->withRequiredNumber("flowDistance")
  397. ->whichIsGreaterOrEqual(0.0)
  398. ->finishNumber()
  399. ->withRequiredNumber("hungerRecoveryPerL")
  400. ->whichIsGreaterOrEqual(0.0)
  401. ->withDefault(0.0)
  402. ->finishNumber()
  403. ->withRequiredNumber("thirstRecoveryPerL")
  404. ->whichIsGreaterOrEqual(0.0)
  405. ->withDefault(0.0)
  406. ->finishNumber()
  407. ->withRequiredNumber("heat")
  408. ->withDefault(10.0)
  409. ->finishNumber()
  410. ->withRequiredArray("groupNames")
  411. ->withDefault(new Framework::JSON::JSONArray())
  412. ->addAcceptedStringInArray()
  413. ->finishString()
  414. ->finishArray()
  415. ->finishObject();
  416. }
  417. Framework::Text FluidBlockTypeFactory::getTypeToken() const
  418. {
  419. return "fluid";
  420. }