FluidBlock.cpp 13 KB

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