FluidBlock.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "FluidBlock.h"
  2. #include "Dimension.h"
  3. #include "FluidContainer.h"
  4. #include "Game.h"
  5. FluidBlock::FluidBlock(int typeId,
  6. Framework::Vec3<int> pos,
  7. int dimensionId,
  8. Framework::Vec3<float> lightWeights)
  9. : Block(typeId, pos, dimensionId, 0),
  10. lightWeights(lightWeights),
  11. neighborChanged(1),
  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. tickSource = 1;
  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. for (int i = 0; i < 6; i++)
  83. {
  84. Direction dir = getDirectionFromIndex(i);
  85. if (dir & Direction::TOP)
  86. {
  87. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  88. {
  89. FluidBlock* neighbour
  90. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  91. minNeighborDistance = 0;
  92. distanceToSource = neighbour->distanceToSource + 1;
  93. flowOptions = (char)getOppositeDirection(dir);
  94. changed = true;
  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. std::cout
  123. << "ERROR: created flow fuild block is not an "
  124. "instance of FluidBlock\n";
  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 (distanceToSource > neighbour->distanceToSource + 1)
  138. {
  139. distanceToSource = neighbour->distanceToSource + 1;
  140. flowOptions = (char)getOppositeDirection(dir);
  141. changed = true;
  142. }
  143. else if (distanceToSource == neighbour->distanceToSource + 1)
  144. {
  145. char tmp = flowOptions;
  146. flowOptions |= (char)getOppositeDirection(dir);
  147. changed |= tmp != flowOptions;
  148. }
  149. if (neighbour->distanceToSource < minNeighborDistance)
  150. {
  151. minNeighborDistance = neighbour->distanceToSource;
  152. }
  153. }
  154. }
  155. }
  156. if (distanceToSource)
  157. {
  158. if (minNeighborDistance + 1 > distanceToSource)
  159. {
  160. distanceToSource = minNeighborDistance + 1;
  161. flowOptions = 0; // reavaluated next tick
  162. changed = true;
  163. }
  164. }
  165. if (distanceToSource > maxFlowDistance)
  166. {
  167. distanceToSource = maxFlowDistance;
  168. Game::INSTANCE->doLater([this]() { setHP(0.f); });
  169. }
  170. if (changed)
  171. {
  172. broadcastModelInfoChange();
  173. neighborChanged = 1;
  174. for (int i = 0; i < 6; i++)
  175. {
  176. Direction dir = getDirectionFromIndex(i);
  177. if (dir & (Direction::TOP | Direction::BOTTOM)) continue;
  178. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  179. {
  180. FluidBlock* neighbour
  181. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  182. if (neighbour)
  183. {
  184. neighbour->neighborChanged = 1;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. bool FluidBlock::isInteractable(const Item* zItem) const
  191. {
  192. return distanceToSource == 0
  193. && dynamic_cast<const FluidContainerItem*>(zItem);
  194. }
  195. void FluidBlock::filterPassingLight(unsigned char rgb[3]) const
  196. {
  197. rgb[0] = (unsigned char)(rgb[0] * lightWeights.x);
  198. rgb[1] = (unsigned char)(rgb[1] * lightWeights.y);
  199. rgb[2] = (unsigned char)(rgb[2] * lightWeights.z);
  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. }