Skip to content

Commit

Permalink
Make energy usage based on machine type, add tooltip for energy usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Rearth committed Feb 23, 2024
1 parent 139f6c5 commit f6582c2
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public abstract class MachineBlockEntity extends BlockEntity

// crafting / processing
protected int progress;
protected int energyPerTick;
protected OritechRecipe currentRecipe = OritechRecipe.DUMMY;
protected InventoryInputMode inventoryInputMode = InventoryInputMode.FILL_LEFT_TO_RIGHT;

Expand All @@ -75,8 +76,9 @@ protected void onFinalCommit() {
}
};

public MachineBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
public MachineBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state, int energyPerTick) {
super(type, pos, state);
this.energyPerTick = energyPerTick;
}

@Override
Expand Down Expand Up @@ -119,16 +121,16 @@ public void tick(World world, BlockPos pos, BlockState state, MachineBlockEntity
}

private boolean hasEnoughEnergy(OritechRecipe recipe) {
return energyStorage.amount > calculateEnergyUsage(recipe);
return energyStorage.amount > calculateEnergyUsage();
}

@SuppressWarnings("lossy-conversions")
private void useEnergy(OritechRecipe recipe) {
energyStorage.amount -= calculateEnergyUsage(recipe);
energyStorage.amount -= calculateEnergyUsage();
}

private float calculateEnergyUsage(OritechRecipe recipe) {
return recipe.getEnergyPerTick() * getEfficiencyMultiplier() * (1 / getSpeedMultiplier());
private float calculateEnergyUsage() {
return energyPerTick * getEfficiencyMultiplier() * (1 / getSpeedMultiplier());
}

private void updateNetwork() {
Expand Down Expand Up @@ -323,7 +325,7 @@ private int slotRecipeSearch(ItemStack stack, List<ItemStack> inv) {

// find matching recipe
// check if currently already using a recipe, if so use this one. This means that all slots are used, and we can just top the slots up
if (currentRecipe.getEnergyPerTick() != -1) {
if (currentRecipe.getTime() != -1) {
return findLowestMatchingSlot(stack, inv, false);
}

Expand Down Expand Up @@ -566,4 +568,11 @@ public void setEnergyStored(long amount) {
energyStorage.amount = amount;
}

public int getBaseEnergyPerTick() {
return energyPerTick;
}

public float getEffectiveEnergyPerTick() {
return calculateEnergyUsage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,26 @@ public abstract class MultiblockMachineEntity extends UpgradableMachineBlockEnti

private float coreQuality = 1f;

public MultiblockMachineEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
public MultiblockMachineEntity(BlockEntityType<?> type, BlockPos pos, BlockState state, int energyPerTick) {
super(type, pos, state, energyPerTick);
}

public static Vec3i rotatePosition(Vec3i relativePos, Direction facing) {
return switch (facing) {
case NORTH -> new BlockPos(relativePos.getZ(), relativePos.getY(), relativePos.getX());
case WEST -> new BlockPos(-relativePos.getX(), relativePos.getY(), -relativePos.getZ());
case SOUTH -> new BlockPos(-relativePos.getZ(), relativePos.getY(), -relativePos.getX());
case EAST -> new BlockPos(relativePos.getX(), relativePos.getY(), relativePos.getZ());
default -> relativePos;
};
}

// this seems to work as expected for some reason?
public static Vec3i worldToRelativePos(Vec3i ownWorldPos, Vec3i worldPos, Direction ownFacing) {
var relativePos = worldPos.subtract(ownWorldPos);
return relativePos;
// var facingInverted = ownFacing.getOpposite();
// return rotatePosition(relativePos, facingInverted);
}

@Override
Expand Down Expand Up @@ -158,24 +176,6 @@ private void highlightBlock(BlockPos block) {
ParticleContent.HIGHLIGHT_BLOCK.spawn(world, Vec3d.of(block), null);
}

public static Vec3i rotatePosition(Vec3i relativePos, Direction facing) {
return switch (facing) {
case NORTH -> new BlockPos(relativePos.getZ(), relativePos.getY(), relativePos.getX());
case WEST -> new BlockPos(-relativePos.getX(), relativePos.getY(), -relativePos.getZ());
case SOUTH -> new BlockPos(-relativePos.getZ(), relativePos.getY(), -relativePos.getX());
case EAST -> new BlockPos(relativePos.getX(), relativePos.getY(), relativePos.getZ());
default -> relativePos;
};
}

// this seems to work as expected for some reason?
public static Vec3i worldToRelativePos(Vec3i ownWorldPos, Vec3i worldPos, Direction ownFacing) {
var relativePos = worldPos.subtract(ownWorldPos);
return relativePos;
// var facingInverted = ownFacing.getOpposite();
// return rotatePosition(relativePos, facingInverted);
}

@Override
public boolean isActive(BlockState state) {
return state.get(MultiblockMachine.ASSEMBLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public abstract class UpgradableMachineBlockEntity extends MachineBlockEntity {
private long combinedEnergyStorage = 0;
private long combinedEnergyInsert = 0;

public UpgradableMachineBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
public UpgradableMachineBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state, int energyPerTick) {
super(type, pos, state, energyPerTick);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import rearth.oritech.Oritech;
import rearth.oritech.block.entity.machines.MachineCoreEntity;
import rearth.oritech.block.entity.machines.addons.AddonBlockEntity;
import rearth.oritech.block.base.entity.UpgradableMachineBlockEntity;
import rearth.oritech.block.entity.machines.PulverizerBlockEntity;
Expand Down Expand Up @@ -73,7 +74,7 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return getBlockEntityType().getDeclaredConstructor(BlockPos.class, BlockState.class).newInstance(pos, state);
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
Oritech.LOGGER.error("Unable to create blockEntity for " + getBlockEntityType().getSimpleName() + " at " + this);
return new PulverizerBlockEntity(pos, state);
return new MachineCoreEntity(pos, state);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class AssemblerBlockEntity extends MultiblockMachineEntity {

public AssemblerBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.ASSEMBLER_ENTITY, pos, state);
super(BlockEntitiesContent.ASSEMBLER_ENTITY, pos, state, 30);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class GrinderBlockEntity extends MultiblockMachineEntity {

public GrinderBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.GRINDER_ENTITY, pos, state);
super(BlockEntitiesContent.GRINDER_ENTITY, pos, state, 50);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class PulverizerBlockEntity extends UpgradableMachineBlockEntity {

public PulverizerBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntitiesContent.PULVERIZER_ENTITY, pos, state);
super(BlockEntitiesContent.PULVERIZER_ENTITY, pos, state, 20);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import rearth.oritech.Oritech;
import rearth.oritech.block.base.entity.MachineBlockEntity;
import rearth.oritech.network.NetworkContent;

public class BasicMachineScreen<S extends BasicMachineScreenHandler> extends BaseOwoHandledScreen<FlowLayout, S> {
Expand Down Expand Up @@ -97,7 +98,8 @@ private void updateEnergyBar() {
public Text getEnergyTooltip(long amount, long max) {
var percentage = (float) amount / max;
var energyFill = String.format("%.1f", percentage * 100);
return Text.literal(amount + " / " + max + " RF\n" + energyFill + "% Charged");
var energyUsage = ((MachineBlockEntity) handler.blockEntity).getEffectiveEnergyPerTick();
return Text.literal(amount + " / " + max + " RF\n" + energyFill + "% Charged\n\nMaximum Usage: " + energyUsage + " RF/t");
}

public void updateSettingsButtons() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ public void fillDisplay(FlowLayout root, OritechDisplay display, ReiUIAdapter<Fl
);

// data
var energyUsage = display.getEntry().value().getEnergyPerTick();
var duration = String.format("%.0f", display.getEntry().value().getTime() / 20f);
root.child(
Components.label(Text.of(duration + "s, " + energyUsage + " RF/t")).lineHeight(7)
Components.label(Text.of(duration + "s, ")).lineHeight(7)
.positioning(Positioning.absolute(72, HEIGHT_BASE + 28))
);

Expand Down
11 changes: 2 additions & 9 deletions src/main/java/rearth/oritech/init/recipes/OritechRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ public class OritechRecipe implements Recipe<Inventory> {
private final OritechRecipeType type;
private final List<Ingredient> inputs;
private final List<ItemStack> results;
private final int energyPerTick;
private final int time;

public static final OritechRecipe DUMMY = new OritechRecipe(-1, 10, DefaultedList.ofSize(1, Ingredient.ofStacks(Items.IRON_INGOT.getDefaultStack())), DefaultedList.ofSize(1, Items.IRON_BLOCK.getDefaultStack()), RecipeContent.PULVERIZER);
public static final OritechRecipe DUMMY = new OritechRecipe(-1, DefaultedList.ofSize(1, Ingredient.ofStacks(Items.IRON_INGOT.getDefaultStack())), DefaultedList.ofSize(1, Items.IRON_BLOCK.getDefaultStack()), RecipeContent.PULVERIZER);

public OritechRecipe(int energy, int time, List<Ingredient> inputs, List<ItemStack> results, OritechRecipeType type) {
public OritechRecipe(int time, List<Ingredient> inputs, List<ItemStack> results, OritechRecipeType type) {
this.type = type;
this.results = results;
this.inputs = inputs;
this.energyPerTick = energy;
this.time = time;
}

Expand Down Expand Up @@ -85,15 +83,10 @@ public String toString() {
"type=" + type +
", inputs=" + inputs +
", results=" + results +
", energy=" + energyPerTick +
", time=" + time +
'}';
}

public int getEnergyPerTick() {
return energyPerTick;
}

public int getTime() {
return time;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
public class OritechRecipeType extends EndecRecipeSerializer<OritechRecipe> implements RecipeType<OritechRecipe> {

public static final Endec<OritechRecipe> ORI_RECIPE_ENDEC = StructEndecBuilder.of(
Endec.INT.fieldOf("energyPerTick", OritechRecipe::getEnergyPerTick),
Endec.INT.fieldOf("time", OritechRecipe::getTime),
Endec.ofCodec(Ingredient.DISALLOW_EMPTY_CODEC).listOf().fieldOf("ingredients", OritechRecipe::getInputs),
BuiltInEndecs.ITEM_STACK.listOf().fieldOf("results", OritechRecipe::getResults),
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/oritech/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
"block.oritech.machine_capacitor_addon": "Capacitor Addon",
"block.oritech.machine_acceptor_addon": "Energy Acceptor Addon",
"block.oritech.machine_inventory_proxy_addon": "Inventory Proxy Addon",
"block.oritech.machine_extender": "Machine Extender"
"block.oritech.machine_extender": "Machine Extender",
"block.oritech.machine_core_basic": "Basic Machine Core",
"block.oritech.machine_core_good": "Good Machine Core"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
"Count": 1
}
],
"energyPerTick": 15,
"time": 80
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
"Count": 2
}
],
"energyPerTick": 10,
"time": 40
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
"Count": 1
}
],
"energyPerTick": 100,
"time": 15
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@
"Count": 2
}
],
"energyPerTick": 25,
"time": 40
}

0 comments on commit f6582c2

Please sign in to comment.