-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add settings to rotate/mirror schematics #4452
Merged
leijurv
merged 2 commits into
cabaletta:1.19.4
from
ZacSharp:pr/1.19.4/builder/rotateAndMirrorSchematics
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/api/java/baritone/api/schematic/MirroredSchematic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* This file is part of Baritone. | ||
* | ||
* Baritone is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Baritone is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package baritone.api.schematic; | ||
|
||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.Mirror; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class MirroredSchematic implements ISchematic { | ||
|
||
private final ISchematic schematic; | ||
private final Mirror mirror; | ||
|
||
public MirroredSchematic(ISchematic schematic, Mirror mirror) { | ||
this.schematic = schematic; | ||
this.mirror = mirror; | ||
} | ||
|
||
@Override | ||
public boolean inSchematic(int x, int y, int z, BlockState currentState) { | ||
return schematic.inSchematic( | ||
mirrorX(x, widthX(), mirror), | ||
y, | ||
mirrorZ(z, lengthZ(), mirror), | ||
mirror(currentState, mirror) | ||
); | ||
} | ||
|
||
@Override | ||
public BlockState desiredState(int x, int y, int z, BlockState current, List<BlockState> approxPlaceable) { | ||
return mirror(schematic.desiredState( | ||
mirrorX(x, widthX(), mirror), | ||
y, | ||
mirrorZ(z, lengthZ(), mirror), | ||
mirror(current, mirror), | ||
mirror(approxPlaceable, mirror) | ||
), mirror); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
schematic.reset(); | ||
} | ||
|
||
@Override | ||
public int widthX() { | ||
return schematic.widthX(); | ||
} | ||
|
||
@Override | ||
public int heightY() { | ||
return schematic.heightY(); | ||
} | ||
|
||
@Override | ||
public int lengthZ() { | ||
return schematic.lengthZ(); | ||
} | ||
|
||
private static int mirrorX(int x, int sizeX, Mirror mirror) { | ||
switch (mirror) { | ||
case NONE: | ||
case LEFT_RIGHT: | ||
return x; | ||
case FRONT_BACK: | ||
return sizeX - x - 1; | ||
} | ||
throw new IllegalArgumentException("Unknown mirror"); | ||
} | ||
|
||
private static int mirrorZ(int z, int sizeZ, Mirror mirror) { | ||
switch (mirror) { | ||
case NONE: | ||
case FRONT_BACK: | ||
return z; | ||
case LEFT_RIGHT: | ||
return sizeZ - z - 1; | ||
} | ||
throw new IllegalArgumentException("Unknown mirror"); | ||
} | ||
|
||
private static BlockState mirror(BlockState state, Mirror mirror) { | ||
if (state == null) { | ||
return null; | ||
} | ||
return state.mirror(mirror); | ||
} | ||
|
||
private static List<BlockState> mirror(List<BlockState> states, Mirror mirror) { | ||
if (states == null) { | ||
return null; | ||
} | ||
return states.stream() | ||
.map(s -> mirror(s, mirror)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
src/api/java/baritone/api/schematic/RotatedSchematic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* This file is part of Baritone. | ||
* | ||
* Baritone is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Baritone is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package baritone.api.schematic; | ||
|
||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.Rotation; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class RotatedSchematic implements ISchematic { | ||
|
||
private final ISchematic schematic; | ||
private final Rotation rotation; | ||
private final Rotation inverseRotation; | ||
|
||
public RotatedSchematic(ISchematic schematic, Rotation rotation) { | ||
this.schematic = schematic; | ||
this.rotation = rotation; | ||
// I don't think a 14 line switch would improve readability | ||
this.inverseRotation = rotation.getRotated(rotation).getRotated(rotation); | ||
} | ||
|
||
@Override | ||
public boolean inSchematic(int x, int y, int z, BlockState currentState) { | ||
return schematic.inSchematic( | ||
rotateX(x, z, widthX(), lengthZ(), inverseRotation), | ||
y, | ||
rotateZ(x, z, widthX(), lengthZ(), inverseRotation), | ||
rotate(currentState, inverseRotation) | ||
); | ||
} | ||
|
||
@Override | ||
public BlockState desiredState(int x, int y, int z, BlockState current, List<BlockState> approxPlaceable) { | ||
return rotate(schematic.desiredState( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
rotateX(x, z, widthX(), lengthZ(), inverseRotation), | ||
y, | ||
rotateZ(x, z, widthX(), lengthZ(), inverseRotation), | ||
rotate(current, inverseRotation), | ||
rotate(approxPlaceable, inverseRotation) | ||
), rotation); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
schematic.reset(); | ||
} | ||
|
||
@Override | ||
public int widthX() { | ||
return flipsCoordinates(rotation) ? schematic.lengthZ() : schematic.widthX(); | ||
} | ||
|
||
@Override | ||
public int heightY() { | ||
return schematic.heightY(); | ||
} | ||
|
||
@Override | ||
public int lengthZ() { | ||
return flipsCoordinates(rotation) ? schematic.widthX() : schematic.lengthZ(); | ||
} | ||
|
||
/** | ||
* Wether {@code rotation} swaps the x and z components | ||
*/ | ||
private static boolean flipsCoordinates(Rotation rotation) { | ||
return rotation == Rotation.CLOCKWISE_90 || rotation == Rotation.COUNTERCLOCKWISE_90; | ||
} | ||
|
||
/** | ||
* The x component of x,y after applying the rotation | ||
*/ | ||
private static int rotateX(int x, int z, int sizeX, int sizeZ, Rotation rotation) { | ||
switch (rotation) { | ||
case NONE: | ||
return x; | ||
case CLOCKWISE_90: | ||
return sizeZ - z - 1; | ||
case CLOCKWISE_180: | ||
return sizeX - x - 1; | ||
case COUNTERCLOCKWISE_90: | ||
return z; | ||
} | ||
throw new IllegalArgumentException("Unknown rotation"); | ||
} | ||
|
||
/** | ||
* The z component of x,y after applying the rotation | ||
*/ | ||
private static int rotateZ(int x, int z, int sizeX, int sizeZ, Rotation rotation) { | ||
switch (rotation) { | ||
case NONE: | ||
return z; | ||
case CLOCKWISE_90: | ||
return x; | ||
case CLOCKWISE_180: | ||
return sizeZ - z - 1; | ||
case COUNTERCLOCKWISE_90: | ||
return sizeX - x - 1; | ||
} | ||
throw new IllegalArgumentException("Unknown rotation"); | ||
} | ||
|
||
private static BlockState rotate(BlockState state, Rotation rotation) { | ||
if (state == null) { | ||
return null; | ||
} | ||
return state.rotate(rotation); | ||
} | ||
|
||
private static List<BlockState> rotate(List<BlockState> states, Rotation rotation) { | ||
if (states == null) { | ||
return null; | ||
} | ||
return states.stream() | ||
.map(s -> rotate(s, rotation)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this flips the schematic in memory each time you call desiredState, I feel as though this could get really laggy on large schematics. Can we instead mirror it once in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the underlying schematic might have changed since the last call. We can only do this for static schematics, which most of our schematics are not (at least not reliably), and even then we might end up wasting memory by copying a sparse schematic into a dense array.
Also I think baking things in the wrapper schematic itself is actually a bad idea in most cases, because it forces the result to be static while we actually want it to be static iff it wraps a static schematic. Baking should be done separately from constructing the transformed schematic (i.e.
schematic = new BakedSchematic(schematic)
).IIRC mirroring block states is fairly fast (cache lookup) so the potentially laggy part here is transforming up to 36 states from
approxPlaceable
. Given that we know howBuilderProcess
usesdesiredState
we could commit a caching crime here (reuse last transformed if last untransformed == new untransformed, assuming the input wasn't mutated).