Skip to content

Commit

Permalink
New filled template API
Browse files Browse the repository at this point in the history
  • Loading branch information
afdw committed Oct 31, 2017
1 parent 3613f6d commit 3ea28ff
Show file tree
Hide file tree
Showing 32 changed files with 545 additions and 548 deletions.
2 changes: 1 addition & 1 deletion buildcraft_resources/assets/buildcraft/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ item.package.name=Package
item.guide.name=Guide Book
item.guide_note.name=Guide Note
item.robot_goggles.name=Robot Goggles
item.filler_planner.name=Filler Planner
item.filling_planner.name=Filling Planner

itemGroup.buildcraft.boards=BuildCraft Robots
itemGroup.buildcraft.main=BuildCraft
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"size": [176,81],
"parent": "buildcraftbuilders:gui/filler_base",
"texture": "buildcraftbuilders:gui/filler_planner",
"texture": "buildcraftbuilders:gui/filling_planner",
"elements_below": {
"background": {
"type": "buildcraftlib:drawable",
Expand All @@ -11,7 +11,7 @@
"elements": {
"title": {
"type": "buildcraftlib:text",
"text": "item.filler_planner.name",
"text": "item.filling_planner.name",
"pos": [88,10],
"colour": "0x40_40_40",
"centered": true
Expand Down
1 change: 0 additions & 1 deletion common/buildcraft/builders/addon/AddonFillingPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void updateBuildingInfo() {
box.box.min(),
box.box.size(),
patternStatement,
this,
IntStream.range(0, patternStatement.maxParams)
.mapToObj(patternStatement::get)
.toArray(IStatementParameter[]::new),
Expand Down
46 changes: 27 additions & 19 deletions common/buildcraft/builders/addon/AddonRendererFillingPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.VertexBuffer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;

import net.minecraftforge.client.model.ModelLoader;

import buildcraft.api.filler.FilledTemplate.TemplateState;

import buildcraft.builders.snapshot.Template;
import buildcraft.core.marker.volume.IFastAddonRenderer;

public class AddonRendererFillingPlanner implements IFastAddonRenderer<AddonFillingPlanner> {
@Override
public void renderAddonFast(AddonFillingPlanner addon, EntityPlayer player, float partialTicks, VertexBuffer vb) {
Template.BuildingInfo buildingInfo = addon.buildingInfo;
if (buildingInfo == null) {
if (addon.buildingInfo == null) {
return;
}
Profiler prof = Minecraft.getMinecraft().mcProfiler;
prof.startSection("filler_planner");
prof.startSection("iter");
List<BlockPos> list = new ArrayList<>();
for (BlockPos p : BlockPos.getAllInBoxMutable(buildingInfo.box.min(), buildingInfo.box.max())) {
if (buildingInfo.getSnapshot().data.getOffset(buildingInfo.fromWorld(p)) == TemplateState.FILL && player.world.isAirBlock(p)) {
list.add(p.toImmutable());
}
}
Minecraft.getMinecraft().mcProfiler.startSection("filling_planner");

prof.endStartSection("sort");
Minecraft.getMinecraft().mcProfiler.startSection("iter");
List<BlockPos> list = StreamSupport.stream(
BlockPos.getAllInBoxMutable(addon.buildingInfo.box.min(), addon.buildingInfo.box.max()).spliterator(),
false
)
.filter(blockPos ->
addon.buildingInfo.getSnapshot().data.get(
addon.buildingInfo.getSnapshot().posToIndex(
addon.buildingInfo.fromWorld(blockPos)
)
)
)
.filter(player.world::isAirBlock)
.map(BlockPos.MutableBlockPos::toImmutable)
.collect(Collectors.toCollection(ArrayList::new));
Minecraft.getMinecraft().mcProfiler.endSection();

Minecraft.getMinecraft().mcProfiler.startSection("sort");
list.sort(Comparator.<BlockPos>comparingDouble(p -> player.getPositionVector().squareDistanceTo(new Vec3d(p))).reversed());
Minecraft.getMinecraft().mcProfiler.endSection();

prof.endStartSection("render");
Minecraft.getMinecraft().mcProfiler.startSection("render");
for (BlockPos p : list) {
AxisAlignedBB bb = new AxisAlignedBB(p, p.add(1, 1, 1)).expandXyz(-0.1);
TextureAtlasSprite s = ModelLoader.White.INSTANCE;
Expand Down Expand Up @@ -81,7 +88,8 @@ public void renderAddonFast(AddonFillingPlanner addon, EntityPlayer player, floa
vb.pos(bb.maxX, bb.maxY, bb.maxZ).color(153, 153, 153, 127).tex(s.getMaxU(), s.getMaxV()).lightmap(240, 0).endVertex();
vb.pos(bb.maxX, bb.minY, bb.maxZ).color(153, 153, 153, 127).tex(s.getMaxU(), s.getMinV()).lightmap(240, 0).endVertex();
}
prof.endSection();
prof.endSection();
Minecraft.getMinecraft().mcProfiler.endSection();

Minecraft.getMinecraft().mcProfiler.endSection();
}
}
28 changes: 13 additions & 15 deletions common/buildcraft/builders/filler/Filling.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,35 @@

package buildcraft.builders.filler;

import java.util.Optional;
import java.util.BitSet;

import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;

import buildcraft.api.filler.IFillerPattern;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.containers.IFillerStatementContainer;

import buildcraft.lib.statement.FullStatement;

import buildcraft.builders.snapshot.Snapshot;
import buildcraft.builders.snapshot.Template;

public class Filling {
public static Template.BuildingInfo createBuildingInfo(BlockPos basePos,
BlockPos size,
FullStatement<IFillerPattern> patternStatement,
IFillerStatementContainer filler,
IStatementParameter[] params,
boolean inverted) {
return Optional.ofNullable(patternStatement.get().createTemplate(filler, params))
.map(filledTemplate -> {
Template template = new Template();
template.size = size;
template.offset = BlockPos.ORIGIN;
template.data = filledTemplate;
if (inverted) {
template.data.invert();
}
return template.new BuildingInfo(basePos, Rotation.NONE);
})
.orElse(null);
Template template = new Template();
template.size = size;
template.offset = BlockPos.ORIGIN;
template.data = new BitSet(Snapshot.getDataSize(size));
if (!patternStatement.get().fillTemplate(template.getFilledTemplate(), params)) {
return null;
}
if (inverted) {
template.invert();
}
return template.new BuildingInfo(basePos, Rotation.NONE);
}
}
2 changes: 1 addition & 1 deletion common/buildcraft/builders/gui/GuiFillerPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import buildcraft.builders.filler.FillerStatementContext;

public class GuiFillerPlanner extends GuiJson<ContainerFillerPlanner> {
private static final ResourceLocation LOCATION = new ResourceLocation("buildcraftbuilders:gui/filler_planner.json");
private static final ResourceLocation LOCATION = new ResourceLocation("buildcraftbuilders:gui/filling_planner.json");
private static final SpriteDelegate SPRITE_PATTERN = new SpriteDelegate();

public GuiFillerPlanner(ContainerFillerPlanner container) {
Expand Down
10 changes: 5 additions & 5 deletions common/buildcraft/builders/snapshot/Blueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void deserializeNBT(NBTTagCompound nbt) throws InvalidInputDataException
// schematics through mod additions/deletions)
palette.add(SchematicBlockManager.readFromNBT(schematicBlockTag));
}
data = new int[size.getX() * size.getY() * size.getZ()];
data = new int[Snapshot.getDataSize(size)];
NBTTagList serializedDataList = nbt.hasKey("data", Constants.NBT.TAG_LIST)
? nbt.getTagList("data", Constants.NBT.TAG_INT)
: null;
Expand All @@ -99,11 +99,11 @@ public void deserializeNBT(NBTTagCompound nbt) throws InvalidInputDataException
int serializedDataLength = serializedDataList == null
? serializedDataIntArray.length
: serializedDataList.tagCount();
if (serializedDataLength != size.getX() * size.getY() * size.getZ()) {
if (serializedDataLength != getDataSize()) {
throw new InvalidInputDataException(
"Serialized data has length of " + serializedDataLength +
", but we expected " +
size.getX() * size.getY() * size.getZ() + " (" + size.toString() + ")"
getDataSize() + " (" + size.toString() + ")"
);
}
for (int z = 0; z < size.getZ(); z++) {
Expand Down Expand Up @@ -138,9 +138,9 @@ public class BuildingInfo extends Snapshot.BuildingInfo {
public BuildingInfo(BlockPos basePos, Rotation rotation) {
super(basePos, rotation);
// noinspection unchecked
toPlaceRequiredItems = (List<ItemStack>[]) new List<?>[size.getX() * size.getY() * size.getZ()];
toPlaceRequiredItems = (List<ItemStack>[]) new List<?>[getDataSize()];
// noinspection unchecked
toPlaceRequiredFluids = (List<FluidStack>[]) new List<?>[size.getX() * size.getY() * size.getZ()];
toPlaceRequiredFluids = (List<FluidStack>[]) new List<?>[getDataSize()];
rotatedPalette = ImmutableList.copyOf(
palette.stream()
.map(schematicBlock -> schematicBlock.getRotated(rotation))
Expand Down
12 changes: 8 additions & 4 deletions common/buildcraft/builders/snapshot/BlueprintBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public BlueprintBuilder(ITileForBlueprintBuilder tile) {
}

private ISchematicBlock getSchematicBlock(BlockPos blockPos) {
BlockPos snapshotPos = getBuildingInfo().fromWorld(blockPos);
return getBuildingInfo().box.contains(blockPos) ? getBuildingInfo().rotatedPalette.get(
getBuildingInfo().getSnapshot().data[getBuildingInfo().getSnapshot().posToIndex(snapshotPos)]
) : null;
return getBuildingInfo().box.contains(blockPos)
?
getBuildingInfo().rotatedPalette.get(
getBuildingInfo().getSnapshot().data[getBuildingInfo().getSnapshot().posToIndex(
getBuildingInfo().fromWorld(blockPos)
)]
)
: null;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/builders/snapshot/FakeWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void uploadSnapshot(Snapshot snapshot) {
}
}
if (snapshot instanceof Template) {
if (((Template) snapshot).data.shouldFill(x, y, z)) {
if (((Template) snapshot).data.get(snapshot.posToIndex(x, y, z))) {
setBlockState(pos, Blocks.QUARTZ_BLOCK.getDefaultState());
}
}
Expand Down
15 changes: 15 additions & 0 deletions common/buildcraft/builders/snapshot/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public BlockPos indexToPos(int i) {
return indexToPos(size, i);
}

@SuppressWarnings({"WeakerAccess", "unused"})
public static int getDataSize(int x, int y, int z) {
return x * y * z;
}

@SuppressWarnings({"WeakerAccess", "unused"})
public static int getDataSize(BlockPos size) {
return getDataSize(size.getX(), size.getY(), size.getZ());
}

@SuppressWarnings({"WeakerAccess", "unused"})
public int getDataSize() {
return getDataSize(size);
}

public static NBTTagCompound writeToNBT(Snapshot snapshot) {
NBTTagCompound nbt = snapshot.serializeNBT();
nbt.setTag("type", NBTUtilBC.writeEnum(snapshot.getType()));
Expand Down
Loading

0 comments on commit 3ea28ff

Please sign in to comment.