FluidBlock.cpp 13 KB

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