FluidBlock.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "FluidBlock.h"
  2. #include "FluidContainer.h"
  3. #include "Game.h"
  4. FluidBlock::FluidBlock(int typeId,
  5. Framework::Vec3<int> pos,
  6. int dimensionId,
  7. Vec3<float> lightWeights)
  8. : Block(typeId, 0, pos, dimensionId, 0),
  9. lightWeights(lightWeights),
  10. neighborChanged(1),
  11. nextFlow(0),
  12. maxFlowDistance(8)
  13. {
  14. transparent = 1;
  15. passable = 1;
  16. hp = 1;
  17. maxHP = 1;
  18. hardness = -1.f;
  19. speedModifier = 0.5f;
  20. tickSource = 1;
  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. if (neighborChanged)
  29. {
  30. nextFlow -= numTicks;
  31. if (nextFlow <= 0)
  32. {
  33. const FluidBlockType* zType
  34. = dynamic_cast<const FluidBlockType*>(zBlockType());
  35. if (zType)
  36. {
  37. nextFlow = zType->getTicktsToFlow();
  38. }
  39. else
  40. {
  41. nextFlow = 0;
  42. }
  43. neighborChanged = 0;
  44. doFlow();
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. void FluidBlock::onPostTick() {}
  51. void FluidBlock::setNeighbourType(Direction dir, int type)
  52. {
  53. Block::setNeighbourType(dir, type);
  54. neighborChanged = 1;
  55. }
  56. void FluidBlock::sendModelInfo(NetworkMessage* zMessage)
  57. {
  58. zMessage->addressBlock(this);
  59. char* msg = new char[3];
  60. msg[0] = 2; // fluid amount change
  61. *(msg + 1) = flowOptions;
  62. *(msg + 2) = distanceToSource;
  63. zMessage->setMessage(msg, 3);
  64. }
  65. void FluidBlock::doFlow()
  66. {
  67. bool doesFlowSidewards = 1;
  68. if (((zNeighbours[getDirectionIndex(Direction::BOTTOM)]
  69. && zNeighbours[getDirectionIndex(Direction::BOTTOM)]
  70. ->zBlockType()
  71. == zBlockType()
  72. || neighbourTypes[getDirectionIndex(Direction::BOTTOM)]
  73. == BlockTypeEnum::AIR)
  74. && distanceToSource)
  75. || distanceToSource >= maxFlowDistance)
  76. {
  77. doesFlowSidewards = 0;
  78. }
  79. bool changed = false;
  80. int minNeighborDistance = maxFlowDistance;
  81. for (int i = 0; i < 6; i++)
  82. {
  83. Direction dir = getDirectionFromIndex(i);
  84. if (dir & Direction::TOP)
  85. {
  86. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  87. {
  88. FluidBlock* neighbour
  89. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  90. minNeighborDistance = 0;
  91. distanceToSource = neighbour->distanceToSource + 1;
  92. flowOptions = (char)getOppositeDirection(dir);
  93. changed = true;
  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. std::cout
  122. << "ERROR: created flow fuild block is not an "
  123. "instance of FluidBlock\n";
  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 (distanceToSource > neighbour->distanceToSource + 1)
  137. {
  138. distanceToSource = neighbour->distanceToSource + 1;
  139. flowOptions = (char)getOppositeDirection(dir);
  140. changed = true;
  141. }
  142. else if (distanceToSource == neighbour->distanceToSource + 1)
  143. {
  144. char tmp = flowOptions;
  145. flowOptions |= (char)getOppositeDirection(dir);
  146. changed |= tmp != flowOptions;
  147. }
  148. if (neighbour->distanceToSource < minNeighborDistance)
  149. {
  150. minNeighborDistance = neighbour->distanceToSource;
  151. }
  152. }
  153. }
  154. }
  155. if (distanceToSource)
  156. {
  157. if (minNeighborDistance + 1 > distanceToSource)
  158. {
  159. distanceToSource = minNeighborDistance + 1;
  160. flowOptions = 0; // reavaluated next tick
  161. changed = true;
  162. }
  163. }
  164. if (distanceToSource > maxFlowDistance)
  165. {
  166. distanceToSource = maxFlowDistance;
  167. Game::INSTANCE->doLater([this]() { setHP(0.f); });
  168. }
  169. if (changed)
  170. {
  171. broadcastModelInfoChange();
  172. neighborChanged = 1;
  173. for (int i = 0; i < 6; i++)
  174. {
  175. Direction dir = getDirectionFromIndex(i);
  176. if (dir & (Direction::TOP | Direction::BOTTOM)) continue;
  177. if (zNeighbours[i] && zNeighbours[i]->zBlockType() == zBlockType())
  178. {
  179. FluidBlock* neighbour
  180. = dynamic_cast<FluidBlock*>(zNeighbours[i]);
  181. if (neighbour)
  182. {
  183. neighbour->neighborChanged = 1;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. bool FluidBlock::isInteractable(const Item* zItem) const
  190. {
  191. return distanceToSource == 0
  192. && dynamic_cast<const FluidContainerItem*>(zItem);
  193. }
  194. void FluidBlock::filterPassingLight(unsigned char rgb[3]) const
  195. {
  196. rgb[0] = (unsigned char)(rgb[0] * lightWeights.x);
  197. rgb[1] = (unsigned char)(rgb[1] * lightWeights.y);
  198. rgb[2] = (unsigned char)(rgb[2] * lightWeights.z);
  199. }
  200. char FluidBlock::getDistanceToSource() const
  201. {
  202. return distanceToSource;
  203. }
  204. char FluidBlock::getFlowOptions() const
  205. {
  206. return flowOptions;
  207. }
  208. FluidBlockType::FluidBlockType(int id,
  209. ModelInfo model,
  210. const char* name,
  211. int mapColor,
  212. Vec3<float> lightWeights,
  213. int ticktsToFlow,
  214. char flowDistance)
  215. : BlockType(id, 0, model, 1, 10, 0, name, true, mapColor),
  216. lightWeights(lightWeights),
  217. ticktsToFlow(ticktsToFlow),
  218. flowDistance(flowDistance)
  219. {}
  220. void FluidBlockType::loadSuperBlock(
  221. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  222. {
  223. FluidBlock* block = dynamic_cast<FluidBlock*>(zBlock);
  224. zReader->lese(&block->flowOptions, 1);
  225. zReader->lese(&block->distanceToSource, 1);
  226. block->nextFlow = ticktsToFlow;
  227. block->maxFlowDistance = flowDistance;
  228. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  229. }
  230. void FluidBlockType::saveSuperBlock(
  231. Block* zBlock, Framework::StreamWriter* zWriter) const
  232. {
  233. FluidBlock* block = dynamic_cast<FluidBlock*>(zBlock);
  234. zWriter->schreibe(&block->flowOptions, 1);
  235. zWriter->schreibe(&block->distanceToSource, 1);
  236. BlockType::saveSuperBlock(zBlock, zWriter);
  237. }
  238. Item* FluidBlockType::createItem() const
  239. {
  240. return 0;
  241. }
  242. Block* FluidBlockType::createBlock(
  243. Framework::Vec3<int> position, int dimensionId) const
  244. {
  245. FluidBlock* result
  246. = new FluidBlock(getId(), position, dimensionId, lightWeights);
  247. result->nextFlow = ticktsToFlow;
  248. result->maxFlowDistance = flowDistance;
  249. return result;
  250. }
  251. bool FluidBlockType::isFluid() const
  252. {
  253. return true;
  254. }
  255. int FluidBlockType::getTicktsToFlow() const
  256. {
  257. return ticktsToFlow;
  258. }
  259. char FluidBlockType::getFlowDistance() const
  260. {
  261. return flowDistance;
  262. }
  263. std::function<bool(FluidContainerItem*, Entity*)>
  264. FluidBlockType::getFoodEffect() const
  265. {
  266. return foodEffect;
  267. }
  268. FluidBlockType* FluidBlockType::setFoodEffect(
  269. std::function<bool(FluidContainerItem*, Entity*)> foodEffect)
  270. {
  271. this->foodEffect = foodEffect;
  272. return this;
  273. }