FluidBlock.cpp 13 KB

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