Skip to content

Commit

Permalink
Fix wind using blocks not correctly checking if theyre outside. Made …
Browse files Browse the repository at this point in the history
…the outside check more strict to require cross breeze like conditions in closed areas.
  • Loading branch information
Corosauce committed Dec 3, 2023
1 parent 7b15ce9 commit 0b171cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void tick(Level level, BlockPos pos, BlockState state) {

} else {
if (level.getGameTime() % 40 == 0) {
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F));
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F), false, true);
}

if (isOutsideCached) {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/weather2/blockentity/WindTurbineBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class WindTurbineBlockEntity extends BlockEntity {

public boolean isOutsideCached = false;

private boolean needsInit = true;

//private final EnergyManager energyManager;
private LazyOptional<EnergyManager> energy;
private EnergyManager energyManager;
Expand Down Expand Up @@ -64,10 +66,13 @@ public void tick(Level level, BlockPos pos, BlockState state) {
this.energyManager.addEnergy((int) (maxNormalGenerated * lastWindSpeed));
outputEnergy();
} else {
if (level.getGameTime() % 40 == 0) {
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F));
if (needsInit) {
needsInit = false;
updateIsOutside();
}
if (level.getGameTime() % 100 == 0) {
updateIsOutside();
}

if (isOutsideCached) {
float windSpeed = WindReader.getWindSpeed(level);
float rotMax = 100F;
Expand All @@ -89,6 +94,10 @@ public void tick(Level level, BlockPos pos, BlockState state) {
}
}

public void updateIsOutside() {
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F), false, true);
}

public void outputEnergy() {
//System.out.println(this.energyManager.getEnergyStored());
if (this.energyManager.getEnergyStored() >= this.energyManager.getMaxExtract() && this.energyManager.canExtract()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void tick(Level level, BlockPos pos, BlockState state) {

} else {
if (level.getGameTime() % 40 == 0) {
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F));
isOutsideCached = WeatherUtilEntity.isPosOutside(level, new Vec3(getBlockPos().getX()+0.5F, getBlockPos().getY()+0.5F, getBlockPos().getZ()+0.5F), false, true);
}

if (isOutsideCached) {
Expand Down
54 changes: 40 additions & 14 deletions src/main/java/weather2/util/WeatherUtilEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,39 +196,65 @@ public static boolean isEntityOutside(Entity parEnt) {
}

public static boolean isEntityOutside(Entity parEnt, boolean cheapCheck) {
return isPosOutside(parEnt.level(), parEnt.position(), cheapCheck);
return isPosOutside(parEnt.level(), parEnt.position(), cheapCheck, false);
}

public static boolean isPosOutside(Level parWorld, Vec3 parPos) {
return isPosOutside(parWorld, parPos, false);
return isPosOutside(parWorld, parPos, false, false);
}

public static boolean isPosOutside(Level parWorld, Vec3 parPos, boolean cheapCheck) {
int rangeCheck = 5;
int yOffset = 1;
public static boolean isPosOutside(Level parWorld, Vec3 parPos, boolean cheapCheck, boolean eachSideClearCheck) {

if (WeatherUtilBlock.getPrecipitationHeightSafe(parWorld, new BlockPos(Mth.floor(parPos.x), 0, Mth.floor(parPos.z))).getY() < parPos.y+1) return true;

if (cheapCheck) return false;

int rangeCheck = 5;
int yOffset = 0;
//start 1 block away from start position to avoid colliding with self when used for a block
int xzInitialOffset = 1;

boolean nsCheck = false;

Vec3 vecTry = new Vec3(parPos.x + Direction.NORTH.getStepX()*rangeCheck, parPos.y+yOffset, parPos.z + Direction.NORTH.getStepZ()*rangeCheck);
if (checkVecOutside(parWorld, parPos, vecTry)) {
return true;
Vec3 parPosTry = new Vec3(parPos.x + Direction.NORTH.getStepX()*xzInitialOffset, parPos.y+yOffset, parPos.z + Direction.NORTH.getStepZ()*xzInitialOffset);
if (checkVecOutside(parWorld, parPosTry, vecTry)) {
if (eachSideClearCheck) {
nsCheck = true;
} else {
return true;
}
}

vecTry = new Vec3(parPos.x + Direction.SOUTH.getStepX()*rangeCheck, parPos.y+yOffset, parPos.z + Direction.SOUTH.getStepZ()*rangeCheck);
if (checkVecOutside(parWorld, parPos, vecTry)) {
return true;
parPosTry = new Vec3(parPos.x + Direction.SOUTH.getStepX()*xzInitialOffset, parPos.y+yOffset, parPos.z + Direction.SOUTH.getStepZ()*xzInitialOffset);
if (checkVecOutside(parWorld, parPosTry, vecTry)) {
if (eachSideClearCheck) {
return nsCheck;
} else {
return true;
}
}

nsCheck = false;

vecTry = new Vec3(parPos.x + Direction.EAST.getStepX()*rangeCheck, parPos.y+yOffset, parPos.z + Direction.EAST.getStepZ()*rangeCheck);
if (checkVecOutside(parWorld, parPos, vecTry)) {
return true;
parPosTry = new Vec3(parPos.x + Direction.EAST.getStepX()*xzInitialOffset, parPos.y+yOffset, parPos.z + Direction.EAST.getStepZ()*xzInitialOffset);
if (checkVecOutside(parWorld, parPosTry, vecTry)) {
if (eachSideClearCheck) {
nsCheck = true;
} else {
return true;
}
}

vecTry = new Vec3(parPos.x + Direction.WEST.getStepX()*rangeCheck, parPos.y+yOffset, parPos.z + Direction.WEST.getStepZ()*rangeCheck);
if (checkVecOutside(parWorld, parPos, vecTry)) {
return true;
parPosTry = new Vec3(parPos.x + Direction.WEST.getStepX()*xzInitialOffset, parPos.y+yOffset, parPos.z + Direction.WEST.getStepZ()*xzInitialOffset);
if (checkVecOutside(parWorld, parPosTry, vecTry)) {
if (eachSideClearCheck) {
return nsCheck;
} else {
return true;
}
}

return false;
Expand Down

0 comments on commit 0b171cd

Please sign in to comment.