FluidBlock.cpp 13 KB

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