Skip to content

Commit

Permalink
Last minuite changes to fix stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexIIL committed Jul 5, 2017
1 parent 8bcea60 commit ed7b3ab
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 65 deletions.
1 change: 1 addition & 0 deletions common/buildcraft/energy/BCEnergy.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private static void registerMigrations() {
// Tiles
registerTag("tile.engine.stone").reg("engine.stone");
registerTag("tile.engine.iron").reg("engine.iron");
registerTag("tile.spring.oil").reg("spring.oil");

endBatch(TagManager.prependTags("buildcraftenergy:", EnumTagType.REGISTRY_NAME, EnumTagType.MODEL_LOCATION).andThen(TagManager.setTab("buildcraft.main")));
}
Expand Down
12 changes: 10 additions & 2 deletions common/buildcraft/energy/BCEnergyBlocks.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
/* Copyright (c) 2016 SpaceToad and the BuildCraft team
/*
* Copyright (c) 2016 SpaceToad and the BuildCraft team
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */
* distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package buildcraft.energy;

import buildcraft.api.enums.EnumEngineType;
import buildcraft.api.enums.EnumSpring;

import buildcraft.lib.tile.TileBC_Neptune;

import buildcraft.core.BCCoreBlocks;
import buildcraft.energy.tile.TileEngineIron_BC8;
import buildcraft.energy.tile.TileEngineStone_BC8;
import buildcraft.energy.tile.TileSpringOil;

public class BCEnergyBlocks {
public static void preInit() {
EnumSpring.OIL.liquidBlock = BCEnergyFluids.crudeOil[0].getBlock().getDefaultState();
EnumSpring.OIL.tileConstructor = TileSpringOil::new;
TileBC_Neptune.registerTile(TileSpringOil.class, "tile.spring.oil");

if (BCCoreBlocks.engine != null) {
TileBC_Neptune.registerTile(TileEngineStone_BC8.class, "tile.engine.stone");
BCCoreBlocks.engine.registerEngine(EnumEngineType.STONE, TileEngineStone_BC8::new);
Expand Down
5 changes: 0 additions & 5 deletions common/buildcraft/energy/generation/IHeightFunction.java

This file was deleted.

84 changes: 84 additions & 0 deletions common/buildcraft/energy/generation/OilGenStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

import java.util.function.Predicate;

import net.minecraft.block.BlockSponge;
import net.minecraft.block.state.IBlockState;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import buildcraft.api.core.BCLog;
import buildcraft.api.enums.EnumSpring;

import buildcraft.lib.misc.BlockUtil;
import buildcraft.lib.misc.VecUtil;
import buildcraft.lib.misc.data.Box;

import buildcraft.core.BCCoreBlocks;
import buildcraft.core.block.BlockSpring;
import buildcraft.energy.BCEnergyFluids;
import buildcraft.energy.tile.TileSpringOil;

public abstract class OilGenStructure {
public final Box box;
Expand All @@ -34,6 +40,10 @@ public final void generate(World world, Box within) {
/** Generates this structure in the world, but only between the given coordinates. */
protected abstract void generateWithin(World world, Box intersect);

/** @return The number of oil blocks that this structure will set. Note that this is called *after*
* {@link #generateWithin(World, Box)}, by the Spring type, so this can store the number set. */
protected abstract int countOilBlocks();

public void setOilIfCanReplace(World world, BlockPos pos) {
if (canReplaceForOil(world, pos)) {
setOil(world, pos);
Expand Down Expand Up @@ -80,6 +90,17 @@ protected void generateWithin(World world, Box intersect) {
}
}
}

@Override
protected int countOilBlocks() {
int count = 0;
for (BlockPos pos : BlockPos.getAllInBox(box.min(), box.max())) {
if (predicate.test(pos)) {
count++;
}
}
return count;
}
}

public static class FlatPattern extends OilGenStructure {
Expand Down Expand Up @@ -110,6 +131,19 @@ protected void generateWithin(World world, Box intersect) {
}
}
}

@Override
protected int countOilBlocks() {
int count = 0;
for (int x = 0; x < pattern.length; x++) {
for (int z = 0; z < pattern[x].length; z++) {
if (pattern[x][z]) {
count++;
}
}
}
return count * depth;
}
}

public static class Spout extends OilGenStructure {
Expand All @@ -119,6 +153,7 @@ public static class Spout extends OilGenStructure {
public final BlockPos start;
public final int radius;
public final int height;
private int count = 0;

public Spout(BlockPos start, ReplaceType replaceType, int radius, int height) {
super(createBox(start), replaceType);
Expand All @@ -135,6 +170,7 @@ private static Box createBox(BlockPos start) {

@Override
protected void generateWithin(World world, Box intersect) {
count = 0;
int segment = world.getChunkFromBlockCoords(start).getTopFilledSegment();
BlockPos worldTop = new BlockPos(start.getX(), segment + 16, start.getZ());
for (int y = segment; y >= start.getY(); y--) {
Expand All @@ -152,13 +188,61 @@ protected void generateWithin(World world, Box intersect) {
}
OilGenStructure tubeY = OilGenerator.createTubeY(start, worldTop.getY() - start.getY(), radius);
tubeY.generate(world, tubeY.box);
count += tubeY.countOilBlocks();
BlockPos base = worldTop;
for (int r = radius; r >= 0; r--) {
BCLog.logger.info(" - " + base + " = " + r);
OilGenStructure struct = OilGenerator.createTubeY(base, height, r);
struct.generate(world, struct.box);
base = base.add(0, height, 0);
count += struct.countOilBlocks();
}
}

@Override
protected int countOilBlocks() {
if (count == 0) {
throw new IllegalStateException("Called countOilBlocks before calling generateWithin!");
}
return count;
}
}

public static class Spring extends OilGenStructure {
public final BlockPos pos;

public Spring(BlockPos pos) {
super(new Box(pos, pos), ReplaceType.ALWAYS);
this.pos = pos;
}

@Override
protected void generateWithin(World world, Box intersect) {
// NO-OP (this one is called separately)
}

@Override
protected int countOilBlocks() {
return 0;
}

public void generate(World world, int count) {
IBlockState state = BCCoreBlocks.spring.getDefaultState();
state = state.withProperty(BlockSpring.SPRING_TYPE, EnumSpring.OIL);
world.setBlockState(pos, state);
TileEntity tile = world.getTileEntity(pos);
TileSpringOil spring;
if (tile instanceof TileSpringOil) {
spring = (TileSpringOil) tile;
} else {
BCLog.logger.warn("[energy.gen.oil] Setting the blockstate didn't also set the tile at " + pos);
spring = new TileSpringOil();
spring.setWorld(world);
spring.setPos(pos);
world.setTileEntity(pos, spring);
}
spring.totalSources = count;
BCLog.logger.info("[energy.gen.oil] Generated TileSpringOil as " + System.identityHashCode(tile)); // TODO: This might not work properly!
}
}
}
86 changes: 43 additions & 43 deletions common/buildcraft/energy/generation/OilGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import java.util.Random;
import java.util.function.Predicate;

import com.google.common.collect.ImmutableList;

import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.ChunkProviderServer;

import net.minecraftforge.fml.common.IWorldGenerator;

Expand All @@ -21,6 +22,7 @@

import buildcraft.energy.generation.OilGenStructure.GenByPredicate;
import buildcraft.energy.generation.OilGenStructure.ReplaceType;
import buildcraft.energy.generation.OilGenStructure.Spring;
import buildcraft.energy.generation.OilPopulate.GenType;

public enum OilGenerator implements IWorldGenerator {
Expand All @@ -42,25 +44,35 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
int z = chunkZ * 16 + 8;
BlockPos min = new BlockPos(x, 0, z);
Box box = new Box(min, min.add(15, world.getHeight(), 15));
List<OilGenStructure> structures = new ArrayList<>();

world.profiler.startSection("scan");
for (int cdx = -MAX_CHUNK_RADIUS; cdx <= MAX_CHUNK_RADIUS; cdx++) {
for (int cdz = -MAX_CHUNK_RADIUS; cdz <= MAX_CHUNK_RADIUS; cdz++) {
int cx = chunkX + cdx;
int cz = chunkZ + cdz;
addStructures(structures, world, cx, cz);
world.profiler.startSection("scan");
List<OilGenStructure> structures = getStructures(world, cx, cz);
OilGenStructure.Spring spring = null;
world.profiler.endStartSection("gen");
for (OilGenStructure struct : structures) {
struct.generate(world, box);
if (struct instanceof OilGenStructure.Spring) {
spring = (Spring) struct;
}
}
if (spring != null && box.contains(spring.pos)) {
int count = 0;
for (OilGenStructure struct : structures) {
count += struct.countOilBlocks();
}
spring.generate(world, count);
}
world.profiler.endSection();
}
}
world.profiler.endStartSection("gen");
for (OilGenStructure struct : structures) {
struct.generate(world, box);
}
world.profiler.endSection();
world.profiler.endSection();
}

public static void addStructures(List<OilGenStructure> structures, World world, int cx, int cz) {
public static List<OilGenStructure> getStructures(World world, int cx, int cz) {
Random rand = RandUtil.createRandomForChunk(world, cx, cz, MAGIC_GEN_NUMBER);

// shift to world coordinates
Expand All @@ -71,7 +83,7 @@ public static void addStructures(List<OilGenStructure> structures, World world,

// Do not generate oil in the End or Nether
if (OilPopulate.INSTANCE.excludedBiomeNames.contains(biome.getBiomeName())) {
return;
return ImmutableList.of();
}

boolean oilBiome = OilPopulate.INSTANCE.surfaceDepositBiomeNames.contains(biome.getBiomeName());
Expand All @@ -91,27 +103,23 @@ public static void addStructures(List<OilGenStructure> structures, World world,
// 2%
type = GenType.LAKE;
} else {
return;
return ImmutableList.of();
}

// FIXME: Half implemented!
int[][] heights;
IChunkProvider provider = world.getChunkProvider();
if (provider instanceof IHeightFunction) {
heights = ((IHeightFunction) provider).genHeights(cx, cz);
} else if (provider instanceof ChunkProviderServer) {
IChunkGenerator gen = ((ChunkProviderServer) provider).chunkGenerator;
if (gen instanceof IHeightFunction) {
heights = ((IHeightFunction) gen).genHeights(cx, cz);
} else {
heights = new int[16][16];
for (int hx = 0; hx < 16; hx++) {
for (int hz = 0; hz < 16; hz++) {
heights[hx][hz] = world.getSeaLevel();
}
}
}
List<OilGenStructure> structures = new ArrayList<>();
int lakeRadius;
int tendrilRadius;
if (type == GenType.LARGE) {
lakeRadius = 4;
tendrilRadius = 25 + rand.nextInt(20);
} else if (type == GenType.LAKE) {
lakeRadius = 6;
tendrilRadius = 25 + rand.nextInt(20);
} else {
lakeRadius = 2;
tendrilRadius = 5 + rand.nextInt(10);
}
structures.add(createTendril(new BlockPos(x, 62, z), lakeRadius, tendrilRadius, rand));

if (type != GenType.LAKE) {
// Generate a spherical cave deposit
Expand Down Expand Up @@ -141,22 +149,10 @@ public static void addStructures(List<OilGenStructure> structures, World world,
// Generate a spring at the very bottom
if (type == GenType.LARGE) {
structures.add(createTubeY(new BlockPos(x, 1, z), wellY, radius));
structures.add(createSpring(new BlockPos(x, 0, z)));
}
}

int lakeRadius;
int tendrilRadius;
if (type == GenType.LARGE) {
lakeRadius = 4;
tendrilRadius = 25 + rand.nextInt(20);
} else if (type == GenType.LAKE) {
lakeRadius = 6;
tendrilRadius = 25 + rand.nextInt(20);
} else {
lakeRadius = 2;
tendrilRadius = 5 + rand.nextInt(10);
}
structures.add(createTendril(new BlockPos(x, 62, z), lakeRadius, tendrilRadius, rand));
return structures;
}

private static OilGenStructure createSpout(BlockPos start, int height, int radius) {
Expand All @@ -175,6 +171,10 @@ public static OilGenStructure createTubeZ(BlockPos start, int length, int radius
return createTube(start, length, radius, Axis.Z);
}

public static OilGenStructure createSpring(BlockPos at) {
return new OilGenStructure.Spring(at);
}

private static OilGenStructure createTube(BlockPos center, int length, int radius, Axis axis) {
int valForAxis = VecUtil.getValue(center, axis);
BlockPos min = VecUtil.replaceValue(center.add(-radius, -radius, -radius), axis, valForAxis);
Expand Down
Loading

0 comments on commit ed7b3ab

Please sign in to comment.