From 16ddb6b84f5b43ce95e6f51605dcc956070d210e Mon Sep 17 00:00:00 2001 From: stfwi Date: Wed, 1 May 2019 00:54:11 +0200 Subject: [PATCH] Experimental passive fluid accumulator implemented. --- 1.12/meta/update.json | 2 +- 1.12/readme.md | 2 +- .../BlockDecorPassiveFluidAccumulator.java | 181 +++++++++++++----- .../pipe/passive_fluid_accumulator_model.json | 34 ++-- ...passive_fluid_accumulator_back_texture.png | Bin 0 -> 691 bytes ...assive_fluid_accumulator_front_texture.png | Bin 0 -> 693 bytes ...passive_fluid_accumulator_side_texture.png | Bin 0 -> 682 bytes meta/update.json | 4 +- 8 files changed, 154 insertions(+), 69 deletions(-) create mode 100644 1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_back_texture.png create mode 100644 1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_front_texture.png create mode 100644 1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_side_texture.png diff --git a/1.12/meta/update.json b/1.12/meta/update.json index 714af8a..cae1048 100644 --- a/1.12/meta/update.json +++ b/1.12/meta/update.json @@ -1,7 +1,7 @@ { "homepage": "https://www.curseforge.com/minecraft/mc-mods/engineers-decor/", "1.12.2": { - "1.0.4-b8": "[F] Fixed stairs rendering without smooth light (thanks rastot9).\n[A] Added passive fluid accumulator.", + "1.0.4-b8": "[F] Fixed stairs rendering without smooth light (thanks rastot9).\n[E] Added passive fluid accumulator (experimental feature, see config).", "1.0.4-b7": "[F] Fixed recipe loading issue if IE is not installed.\n[M] Valves support IE pressurized fluid transfer.", "1.0.4-b6": "[A] Added redstone controlled fluid valve.\n[A] Added redstone controlled analog fluid valve.\n[M] Check valve recipe adapted (thanks majijn).", "1.0.4-b5": "[A] Horizontal steel double-T support beam with pole connections.\n[A] Added fluid pipe check valve (straight, conducts only one way).\n[M] Internal registration block/te handling changed.", diff --git a/1.12/readme.md b/1.12/readme.md index ae4b005..f4f93bd 100644 --- a/1.12/readme.md +++ b/1.12/readme.md @@ -11,7 +11,7 @@ Mod sources for Minecraft version 1.12.2. ## Revision history - v1.0.4-b8 [F] Fixed stairs rendering without smooth light (thanks rastot9). - [A] Added passive fluid accumulator. + [E] Added passive fluid accumulator (experimental feature, see config). - v1.0.4-b7 [F] Fixed recipe loading issue if IE is not installed. [M] Valves support IE pressurized fluid transfer. diff --git a/1.12/src/main/java/wile/engineersdecor/blocks/BlockDecorPassiveFluidAccumulator.java b/1.12/src/main/java/wile/engineersdecor/blocks/BlockDecorPassiveFluidAccumulator.java index 0f6358d..2cccd40 100644 --- a/1.12/src/main/java/wile/engineersdecor/blocks/BlockDecorPassiveFluidAccumulator.java +++ b/1.12/src/main/java/wile/engineersdecor/blocks/BlockDecorPassiveFluidAccumulator.java @@ -9,8 +9,8 @@ */ package wile.engineersdecor.blocks; -import wile.engineersdecor.ModEngineersDecor; import wile.engineersdecor.detail.ModAuxiliaries; +import net.minecraft.block.Block; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; @@ -18,6 +18,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; +import net.minecraft.util.ITickable; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; @@ -45,31 +46,85 @@ public class BlockDecorPassiveFluidAccumulator extends BlockDecorDirected implem public TileEntity createTileEntity(World world, IBlockState state) { return new BlockDecorPassiveFluidAccumulator.BTileEntity(); } + public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos fromPos) + { TileEntity te = world.getTileEntity(pos); if(te instanceof BlockDecorPipeValve.BTileEntity) ((BTileEntity)te).block_changed(); } + //-------------------------------------------------------------------------------------------------------------------- // Tile entity //-------------------------------------------------------------------------------------------------------------------- - public static class BTileEntity extends TileEntity implements IFluidHandler, IFluidTankProperties, ICapabilityProvider -, ModAuxiliaries.IExperimentalFeature + public static class BTileEntity extends TileEntity implements IFluidHandler, IFluidTankProperties, ICapabilityProvider, ITickable, ModAuxiliaries.IExperimentalFeature { - protected static int tank_fill_rate_mb = 1000; - protected static int tank_capacity_mb = 2000; + protected static int tick_idle_interval = 20; // ca 1000ms, simulates suction delay and saves CPU when not drained. + protected static int max_flowrate = 1000; + protected static int tank_capacity_mb = max_flowrate * 2; private final IFluidTankProperties[] fluid_props_ = {this}; private final InputFillHandler fill_handler_ = new InputFillHandler(this); private EnumFacing block_facing_ = EnumFacing.NORTH; private FluidStack tank_ = null; - private FluidStack last_filled_ = null; private FluidStack last_drain_request_fluid_ = null; private int last_drain_request_amount_ = 0; + private int vacuum_ = 0; + private int tick_timer_ = 0; + private int round_robin_ = 0; + private boolean initialized_ = false; + + public void block_changed() + { initialized_ = false; tick_timer_ = MathHelper.clamp(tick_timer_ , 0, tick_idle_interval); } + + // Output flow handler --------------------------------------------------------------------- + + private static class InputFillHandler implements IFluidHandler, IFluidTankProperties + { + private final BTileEntity parent_; + private final IFluidTankProperties[] props_ = {this}; + InputFillHandler(BTileEntity parent) { parent_ = parent; } + @Override @Nullable public FluidStack drain(FluidStack resource, boolean doDrain) { return null; } + @Override @Nullable public FluidStack drain(int maxDrain, boolean doDrain) { return null; } + @Override public IFluidTankProperties[] getTankProperties() { return props_; } + @Override public int getCapacity() { return tank_capacity_mb; } + @Override public boolean canFill() { return true; } + @Override public boolean canDrain() { return false; } + @Override public boolean canFillFluidType(FluidStack fluidStack) { return true; } + @Override public boolean canDrainFluidType(FluidStack fluidStack) { return false; } + + @Nullable + @Override public FluidStack getContents() + { + if(parent_.tank_==null) return null; + FluidStack res = parent_.tank_.copy(); + if(res.amount > max_flowrate) res.amount = max_flowrate; + return res; + } + + @Override public int fill(FluidStack resource, boolean doFill) + { + if(!parent_.initialized_) return 0; + FluidStack res = resource.copy(); + if(parent_.tank_ == null) { + res.amount = MathHelper.clamp(res.amount, 0, max_flowrate*2); + if(doFill) parent_.tank_ = res; + return res.amount; + } else { + res.amount = MathHelper.clamp(res.amount, 0, Math.min(max_flowrate*2, tank_capacity_mb-parent_.tank_.amount)); + if((res.amount <= 0) || (!parent_.tank_.isFluidEqual(resource))) return 0; + if(doFill) parent_.tank_.amount += res.amount; + return res.amount; + } + } + } + + // TileEntity ------------------------------------------------------------------------------ public BTileEntity() {} - // TileEntity ------------------------------------------------------------------------------ - @Override public boolean shouldRefresh(World world, BlockPos pos, IBlockState os, IBlockState ns) - { return (os.getBlock() != ns.getBlock()) || (!(ns.getBlock() instanceof BlockDecorPipeValve)); } + { + block_changed(); + return (os.getBlock() != ns.getBlock()) || (!(ns.getBlock() instanceof BlockDecorPipeValve)); + } @Override public void readFromNBT(NBTTagCompound nbt) @@ -90,27 +145,32 @@ public class BlockDecorPassiveFluidAccumulator extends BlockDecorDirected implem @Override public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) - { return ((capability==CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)) || super.hasCapability(capability, facing); } + { return (initialized_ && (capability==CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)) || super.hasCapability(capability, facing); } @Override @Nullable @SuppressWarnings("unchecked") public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) - { return (capability!=CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) ? (super.getCapability(capability, facing)) : (facing==block_facing_) ? (((T)this)) : ((T)fill_handler_); } + { + if(capability != CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) return super.getCapability(capability, facing); + return (facing==block_facing_) ? (((T)this)) : ((T)fill_handler_); + } - // IFluidHandler/IFluidTankProperties of the output port ----------------------------------- + // IFluidHandler of the output port -------------------------------------------------------- - @Override public IFluidTankProperties[] getTankProperties() + @Override + public IFluidTankProperties[] getTankProperties() { return fluid_props_; } - @Override public int fill(FluidStack resource, boolean doFill) + @Override + public int fill(FluidStack resource, boolean doFill) { return 0; } @Override @Nullable public FluidStack drain(FluidStack resource, boolean doDrain) { - last_drain_request_fluid_ = resource.copy(); + if(doDrain) last_drain_request_fluid_ = resource.copy(); return ((tank_==null) || (!tank_.isFluidEqual(resource))) ? (null) : drain(resource.amount, doDrain); } @@ -118,58 +178,83 @@ public class BlockDecorPassiveFluidAccumulator extends BlockDecorDirected implem @Nullable public FluidStack drain(int maxDrain, boolean doDrain) { - last_drain_request_amount_ = maxDrain; + if(!initialized_) return null; + if(doDrain) last_drain_request_amount_ = maxDrain; if(tank_==null) return null; + FluidStack res; if(maxDrain >= tank_.amount) { if(!doDrain) return tank_.copy(); - FluidStack res = tank_; + res = tank_; tank_ = null; - return res; } else { - FluidStack res = tank_.copy(); + res = tank_.copy(); res.amount = maxDrain; if(doDrain) tank_.amount -= maxDrain; - return res; } + return res; } - // IFluidTankProperties -- + // IFluidTankProperties -------------------------------------------------------------------- + @Override @Nullable public FluidStack getContents() { return (tank_==null) ? (null) : (tank_.copy()); } - @Override public int getCapacity() { return tank_capacity_mb; } + @Override public int getCapacity() { return max_flowrate; } @Override public boolean canFill() { return false; } @Override public boolean canDrain() { return true; } @Override public boolean canFillFluidType(FluidStack fluidStack) { return false; } @Override public boolean canDrainFluidType(FluidStack fluidStack) { return true; } - // Output flow handler -- - private static class InputFillHandler implements IFluidHandler, IFluidTankProperties - { - private final BTileEntity parent_; - private final IFluidTankProperties[] props_ = {this}; - InputFillHandler(BTileEntity parent) { parent_ = parent; } - @Override @Nullable public FluidStack drain(FluidStack resource, boolean doDrain) { return null; } - @Override @Nullable public FluidStack drain(int maxDrain, boolean doDrain) { return null; } - @Override @Nullable public FluidStack getContents() { return (parent_.tank_==null) ? (null) : (parent_.tank_.copy()); } - @Override public IFluidTankProperties[] getTankProperties() { return props_; } - @Override public int getCapacity() { return tank_capacity_mb; } - @Override public boolean canFill() { return true; } - @Override public boolean canDrain() { return false; } - @Override public boolean canFillFluidType(FluidStack fluidStack) { return true; } - @Override public boolean canDrainFluidType(FluidStack fluidStack) { return false; } + // ITickable-------------------------------------------------------------------------------- - @Override public int fill(FluidStack resource, boolean doFill) - { - FluidStack res = resource.copy(); - if(parent_.tank_ == null) { - res.amount = MathHelper.clamp(res.amount, 0, tank_fill_rate_mb); - if(doFill) parent_.tank_ = res; - return res.amount; + public void update() + { + if((world.isRemote) || (--tick_timer_ > 0)) return; + tick_timer_ = tick_idle_interval; + if(!initialized_) { + initialized_ = true; + IBlockState state = world.getBlockState(pos); + if((state==null) || (!(state.getBlock() instanceof BlockDecorPassiveFluidAccumulator))) return; + block_facing_ = state.getValue(FACING); + } + int n_requested = last_drain_request_amount_; + final FluidStack req_res = last_drain_request_fluid_; + last_drain_request_amount_ = 0; + last_drain_request_fluid_ = null; + if(tank_!=null) { + if((n_requested > 0) && ((req_res == null) || (tank_.isFluidEqual(req_res)))) { vacuum_ += 2; } else { --vacuum_; } + vacuum_ = MathHelper.clamp(vacuum_, 0, 5); + if(vacuum_ <= 0) return; // nothing to do, noone's draining or can't because the fuild does not match. + } else { + n_requested = 10; // drip in + } + int tank_level = MathHelper.clamp( (tank_==null) ? 0 : tank_.amount, 0, tank_capacity_mb); + if(tank_level >= Math.min(tank_capacity_mb, n_requested * 4)) return; // enough reserve + tick_timer_ = 0; // pull next tick + FluidStack match = (tank_==null) ? (req_res) : (tank_); + for(int i=0; i<6; ++i) { + if(++round_robin_ > 5) round_robin_ = 0; + final EnumFacing f = EnumFacing.byIndex(round_robin_); + if(f == block_facing_) continue; + final TileEntity te = world.getTileEntity(pos.offset(f)); + if((te==null) || (!te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite()))) continue; + final IFluidHandler fh = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, f.getOpposite()); + if(fh==null) continue; + int refill = Math.min(Math.max(n_requested, 100), tank_capacity_mb-tank_level); + if(refill <= 0) break; + FluidStack res; + if(match==null) { + res = fh.drain(refill, true); + if(res != null) match = res.copy(); } else { - res.amount = MathHelper.clamp(res.amount, 0, Math.min(tank_fill_rate_mb, tank_capacity_mb-parent_.tank_.amount)); - if((res.amount <= 0) || (!parent_.tank_.isFluidEqual(resource))) return 0; - if(doFill) parent_.tank_.amount += res.amount; - return res.amount; + match.amount = refill; + res = fh.drain(match.copy(), true); } + if(res == null) continue; + if(tank_==null) { + tank_ = res; + } else { + tank_.amount += res.amount; + } + if(tank_.amount >= tank_capacity_mb) break; } } } diff --git a/1.12/src/main/resources/assets/engineersdecor/models/block/pipe/passive_fluid_accumulator_model.json b/1.12/src/main/resources/assets/engineersdecor/models/block/pipe/passive_fluid_accumulator_model.json index 3da6cda..7f9c16e 100644 --- a/1.12/src/main/resources/assets/engineersdecor/models/block/pipe/passive_fluid_accumulator_model.json +++ b/1.12/src/main/resources/assets/engineersdecor/models/block/pipe/passive_fluid_accumulator_model.json @@ -1,32 +1,32 @@ { "textures": { - "end": "engineersdecor:blocks/pipe/straight_pipe_valve_end_texture", - "particle": "engineersdecor:blocks/pipe/straight_pipe_valve_end_texture", - "side": "engineersdecor:blocks/pipe/straight_pipe_valve_side_texture" + "end": "engineersdecor:blocks/pipe/passive_fluid_accumulator_front_texture", + "particle": "engineersdecor:blocks/pipe/passive_fluid_accumulator_front_texture", + "side": "engineersdecor:blocks/pipe/passive_fluid_accumulator_side_texture", + "back": "engineersdecor:blocks/pipe/passive_fluid_accumulator_back_texture" }, "elements": [ { - "from": [2, 2, 0], - "to": [14, 14, 1], + "from": [3, 3, 0], + "to": [13, 13, 1], "faces": { - "north": {"uv": [2, 2, 14, 14], "texture": "#end"}, - "east": {"uv": [2, 0, 14, 1], "rotation": 90, "texture": "#side"}, - "south": {"uv": [2, 2, 14, 14], "texture": "#end"}, - "west": {"uv": [2, 0, 14, 1], "rotation": 270, "texture": "#side"}, - "up": {"uv": [2, 0, 14, 1], "texture": "#side"}, - "down": {"uv": [2, 15, 14, 16], "texture": "#side"} + "north": {"uv": [3, 3, 13, 13], "texture": "#end"}, + "east": {"uv": [15, 4, 16, 16], "texture": "#side"}, + "west": {"uv": [0, 4, 1, 16], "texture": "#side"}, + "up": {"uv": [4, 0, 16, 1], "texture": "#side"}, + "down": {"uv": [0, 15, 12, 16], "texture": "#side"} } }, { "from": [0, 0, 1], "to": [16, 16, 16], "faces": { - "north": {"uv": [4, 4, 12, 12], "texture": "#end"}, - "east": {"uv": [3, 5, 13, 11], "rotation": 90, "texture": "#side"}, - "south": {"uv": [4, 4, 12, 12], "texture": "#end"}, - "west": {"uv": [3, 5, 13, 11], "rotation": 270, "texture": "#side"}, - "up": {"uv": [3, 5, 13, 11], "texture": "#side"}, - "down": {"uv": [3, 5, 13, 11], "rotation": 180, "texture": "#side"} + "north": {"uv": [0, 0, 16, 16], "texture": "#end"}, + "east": {"uv": [0, 1, 16, 16], "rotation": 90, "texture": "#side"}, + "south": {"uv": [0, 0, 16, 16], "texture": "#back"}, + "west": {"uv": [0, 1, 16, 16], "rotation": 270, "texture": "#side"}, + "up": {"uv": [0, 1, 16, 16], "texture": "#side"}, + "down": {"uv": [0, 1, 16, 16], "rotation": 180, "texture": "#side"} } } ], diff --git a/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_back_texture.png b/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_back_texture.png new file mode 100644 index 0000000000000000000000000000000000000000..90dda9b05fdcdbcdc5f823bf681ea1086c105117 GIT binary patch literal 691 zcmV;k0!;mhP)VGd00L7@QO zXsyXPQ%b=YgSD2nZMnR>L@9+*ij)#U2to*i5YWRgFbo3#N-2a8lv239zUJ%25f4`v zoNR76K0ZcDNzNJP9Nv58c}9BgS*=!-Qb;MGwMI$_z{AxAH{ZXpf29<1&S=@L zAp{2p2RP@J=6kzc*P-{&0II5DnkMe=?|JwBJ)o4MGUC)`SpHN@1&AVM>WyG62?Ey!S{cF~*QmVi<-U0{|fe z#u%J))OEvpy{4%vrs)rrwY2RTYwfa1r4%tnGk)wKPq`fXP20B1M)r-p_shKi)OAhMG-x3N-g{zP5?rCfrPb0(#<3*b18 z^nK5Iy>0aK?t!Zh%pjE Z;4l8QddowV$uIx_002ovPDHLkV1n>LHM0N! literal 0 HcmV?d00001 diff --git a/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_front_texture.png b/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_front_texture.png new file mode 100644 index 0000000000000000000000000000000000000000..d40c59527988326e2247352dbd18230ab06f62ff GIT binary patch literal 693 zcmV;m0!safP)VGd00LD>L_t(I%Y~E8N+d}Xg}?a8NKDj< zgk(Svx}G5y?#fNQ#=tzmyo0-b9GBy!1W}F7fZfF82E-rA+5o09Ah;xpstE=M3IfpR@?>*8v zR~WQy%Povnuhs&jG|{&0Z+Vx?CAYV?yuH2s z_I7=J&CbpaP1B%-5P0tiA@K6@f>Mf<5+MZ6&d$neODTyla(;eJRaL|ozYcJ{USq9g zZ*LDF1jZOb2&9w{LQqvziI`F<*BWE|V({_t!TtR`#+dRk##l(_oY7j7b4Cb(5TZDo briuRm&=698{R;+D00000NkvXXu0mjfTlhQQ literal 0 HcmV?d00001 diff --git a/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_side_texture.png b/1.12/src/main/resources/assets/engineersdecor/textures/blocks/pipe/passive_fluid_accumulator_side_texture.png new file mode 100644 index 0000000000000000000000000000000000000000..11e46c961b4328f619a129acfcd8e7e196ba4ede GIT binary patch literal 682 zcmV;b0#*HqP)VGd00K%$L_t(I%T<%jP82~Dg}?fttGlNs z)004qXu^tjaO-<$z=Rl|gIB;{d=KBn6&pcK7=h3*cGtg)NmpX6YEij$&v(ud=jZ2n zZ*LE+HBw4)&g7gCLJ&d#KnQ`95~UO=B>AGx@^q+N8R7%^4F$@_O492}J2XHIgOCcO8= z7_rt8W90J7S03&k`2FiAo6R4_apL0QGqYT4Emc+RT(H&_2F4hsY2tbNgj5X2Aq1q9r2{D?VvH!I(8Dm$_dUzy zvVaUBkWykU+AJ}p#2maB