Skip to content

Commit

Permalink
feat: the great refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bruberu committed Jul 21, 2024
1 parent 43767a6 commit 9abbc5c
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 169 deletions.
5 changes: 3 additions & 2 deletions src/main/java/gregtech/api/capability/ICoolantHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability;

import gregtech.api.capability.impl.LockableFluidTank;
import gregtech.api.nuclear.fission.ICoolantStats;
import gregtech.api.unification.material.properties.CoolantProperty;

import net.minecraft.util.EnumFacing;
Expand All @@ -12,9 +13,9 @@
public interface ICoolantHandler extends ILockableHandler<Fluid> {

@Nullable
CoolantProperty getCoolant();
ICoolantStats getCoolant();

void setCoolant(@Nullable CoolantProperty prop);
void setCoolant(@Nullable ICoolantStats prop);

@NotNull
LockableFluidTank getFluidTank();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/gregtech/api/capability/IFuelRodHandler.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package gregtech.api.capability;

import gregtech.api.items.itemhandlers.LockableItemStackHandler;
import gregtech.api.nuclear.fission.IFissionFuelStats;
import gregtech.api.nuclear.fission.components.FuelRod;
import gregtech.api.unification.material.properties.FissionFuelProperty;

import net.minecraft.item.ItemStack;

public interface IFuelRodHandler extends ILockableHandler<ItemStack> {

FissionFuelProperty getFuel();
IFissionFuelStats getFuel();

void setFuel(FissionFuelProperty prop);
void setFuel(IFissionFuelStats prop);

FissionFuelProperty getPartialFuel();
IFissionFuelStats getPartialFuel();

/**
* Set the fuel type that's currently being processed by this specific handler.
*
* @param prop The new fuel type.
* @return true if the partial fuel changed.
*/
boolean setPartialFuel(FissionFuelProperty prop);
boolean setPartialFuel(IFissionFuelStats prop);

void setInternalFuelRod(FuelRod rod);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gregtech/api/fluids/GTFluidRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.nuclear.fission.CoolantRegistry;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.CoolantProperty;
import gregtech.api.unification.material.properties.FluidProperty;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.common.blocks.MetaBlocks;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/gregtech/api/nuclear/fission/CoolantRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package gregtech.api.nuclear.fission;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

import net.minecraftforge.fluids.Fluid;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class CoolantRegistry {
private static final Map<Fluid, ICoolantStats> COOLANTS = new Object2ObjectOpenHashMap<>();
private static final Map<ICoolantStats, Fluid> COOLANTS_INVERSE = new Object2ObjectOpenHashMap<>();


public static void registerCoolant(@NotNull Fluid fluid, @NotNull ICoolantStats coolant) {
COOLANTS.put(fluid, coolant);
COOLANTS_INVERSE.put(coolant, fluid);
}

@Nullable
public static ICoolantStats getCoolant(Fluid fluid) {
return COOLANTS.get(fluid);
}

@Nullable
public static Fluid originalFluid(ICoolantStats stats) {
return COOLANTS_INVERSE.get(stats);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package gregtech.api.nuclear.fission;

import gregtech.api.util.ItemStackHashStrategy;

import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

import net.minecraft.item.ItemStack;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

public class FissionFuelRegistry {
private static final Map<Integer, IFissionFuelStats> HASHED_FUELS = new Int2ObjectArrayMap<>();
private static final Map<ItemStack, IFissionFuelStats> FUELS = new Object2ObjectOpenCustomHashMap<>(
ItemStackHashStrategy.comparingAllButCount());
private static final Map<IFissionFuelStats, ItemStack> DEPLETED_FUELS = new Object2ObjectOpenHashMap<>();


public static void registerFuel(@NotNull ItemStack item, @NotNull IFissionFuelStats fuel, @NotNull ItemStack depletedFuel) {
HASHED_FUELS.put(fuel.hashCode(), fuel);
FUELS.put(item, fuel);
DEPLETED_FUELS.put(fuel, depletedFuel);
}

@Nullable
public static IFissionFuelStats getFissionFuel(ItemStack stack) {
return FUELS.get(stack);
}

@Nullable
public static IFissionFuelStats getFissionFuel(int hash) {
return HASHED_FUELS.get(hash);
}

@Nullable
public static ItemStack getDepletedFuel(IFissionFuelStats stats) {
return DEPLETED_FUELS.get(stats);
}
}
36 changes: 18 additions & 18 deletions src/main/java/gregtech/api/nuclear/fission/FissionReactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import gregtech.api.nuclear.fission.components.CoolantChannel;
import gregtech.api.nuclear.fission.components.FuelRod;
import gregtech.api.nuclear.fission.components.ReactorComponent;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.CoolantProperty;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.common.ConfigHolder;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import java.util.ArrayList;
Expand Down Expand Up @@ -427,11 +425,14 @@ public void prepareInitialConditions() {
weightedGenerationTime /= fuelRods.size();

for (CoolantChannel channel : coolantChannels) {
CoolantProperty prop = channel.getCoolant().getProperty(PropertyKey.COOLANT);
ICoolantStats prop = channel.getCoolant();
Fluid fluid = CoolantRegistry.originalFluid(prop);

coolantBaseTemperature += channel.getCoolant().getFluid().getTemperature();
if (fluid != null) {
coolantBaseTemperature += fluid.getTemperature();
}
coolantBoilingPointStandardPressure += prop.getBoilingPoint();
coolantExitTemperature += prop.getHotHPCoolant().getFluid().getTemperature();
coolantExitTemperature += prop.getHotCoolant().getTemperature();
coolantHeatOfVaporization += prop.getHeatOfVaporization();
}

Expand Down Expand Up @@ -465,18 +466,17 @@ public double makeCoolantFlow(int flowRate) {
if (tryFluidDrain != null) {
int drained = tryFluidDrain.amount;

Material coolant = channel.getCoolant();

CoolantProperty prop = coolant.getProperty(PropertyKey.COOLANT);
ICoolantStats prop = channel.getCoolant();
int coolantTemp = CoolantRegistry.originalFluid(prop).getTemperature();

double cooledTemperature = prop.getHotHPCoolant().getFluid().getTemperature();
double cooledTemperature = prop.getHotCoolant().getTemperature();
if (cooledTemperature > this.temperature) {
continue;
}

double heatRemovedPerLiter = prop.getSpecificHeatCapacity() /
ConfigHolder.machines.nuclear.fissionCoolantDivisor *
(cooledTemperature - coolant.getFluid().getTemperature());
(cooledTemperature - coolantTemp);
// Explained by:
// https://physics.stackexchange.com/questions/153434/heat-transfer-between-the-bulk-of-the-fluid-inside-the-pipe-and-the-pipe-externa
double heatFluxPerAreaAndTemp = 1 /
Expand All @@ -500,13 +500,13 @@ public double makeCoolantFlow(int flowRate) {
channel.partialCoolant += cappedFluidUsed - actualFlowRate;

FluidStack HPCoolant = new FluidStack(
prop.getHotHPCoolant().getFluid(), actualFlowRate);
prop.getHotCoolant(), actualFlowRate);

channel.getInputHandler().getFluidTank().drain(actualFlowRate, true);
channel.getOutputHandler().getFluidTank().fill(HPCoolant, true);
if (prop.accumulatesHydrogen() &&
this.temperature > zircaloyHydrogenReactionTemperature) {
double boilingPoint = coolantBoilingPoint(coolant);
double boilingPoint = coolantBoilingPoint(prop);
if (this.temperature > boilingPoint) {
this.accumulatedHydrogen += (this.temperature - boilingPoint) / boilingPoint;
} else if (actualFlowRate < Math.min(remainingSpace, idealFluidUsed)) {
Expand All @@ -515,7 +515,7 @@ public double makeCoolantFlow(int flowRate) {
}
}

this.coolantMass += cappedFluidUsed * coolant.getMass();
this.coolantMass += cappedFluidUsed * prop.getMass();
heatRemoved += cappedFluidUsed * heatRemovedPerLiter;
}
}
Expand All @@ -533,13 +533,13 @@ protected double coolantBoilingPoint() {
R * Math.log(this.pressure / standardPressure) / this.coolantHeatOfVaporization);
}

protected double coolantBoilingPoint(Material coolant) {
if (coolant.getProperty(PropertyKey.COOLANT).getBoilingPoint() == 0) {
protected double coolantBoilingPoint(ICoolantStats coolant) {
if (coolant.getBoilingPoint() == 0) {
return coolantBoilingPoint();
}
return 1. / (1. / coolant.getProperty(PropertyKey.COOLANT).getBoilingPoint() -
return 1. / (1. / coolant.getBoilingPoint() -
R * Math.log(this.pressure / standardPressure) /
coolant.getProperty(PropertyKey.COOLANT).getHeatOfVaporization());
coolant.getHeatOfVaporization());
}

public void updateTemperature(int flowRate) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/gregtech/api/nuclear/fission/ICoolantStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
public interface ICoolantStats {

/**
* @return The fluid with these cooling properties.
* @return The heated coolant fluid.
*/
Fluid getFluid();
Fluid getHotCoolant();

/**
* @return The specific heat capacity of the fluid in J/(kg*K).
Expand All @@ -30,9 +30,19 @@ public interface ICoolantStats {
*/
double getBoilingPoint();

/**
* @return The heat of vaporization of the fluid in J/(kg*K).
*/
double getHeatOfVaporization();

/**
* @return If the coolant reacts with zirconium cladding at high temperatures. This is true for mostly water-based
* coolants.
*/
boolean accumulatesHydrogen();

/**
* @return The mass of the coolant per liter in kg
*/
double getMass();
}
37 changes: 32 additions & 5 deletions src/main/java/gregtech/api/nuclear/fission/IFissionFuelStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,37 @@
public interface IFissionFuelStats {

/**
* Used internally to get the depleted fuel the first time; do not call this yourself!
*
* @return The depleted fuel to be put into the fuel registry.
* @return The maximum temperature the fuel can handle before the reactor melts down.
*/
@NotNull
ItemStack getDepletedFuel();
int getMaxTemperature();

/**
* @return How long the fuel lasts in the reactor, in terms of the megajoules it makes.
*/
int getDuration();

/**
* @return The cross section of slow neutrons that can be captured by this fuel to breed it.
*/
double getSlowNeutronCaptureCrossSection();

/**
* @return The cross section of fast neutrons that can be captured by this fuel to breed it.
*/
double getFastNeutronCaptureCrossSection();

/**
* @return The cross section of slow neutrons that can cause fission in this fuel.
*/
double getSlowNeutronFissionCrossSection();

/**
* @return The cross section of fast neutrons that can cause fission in this fuel.
*/
double getFastNeutronFissionCrossSection();

/**
* @return The average time for a neutron to be emitted during a fission event. Do not make this accurate.
*/
double getNeutronGenerationTime();
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package gregtech.api.nuclear.fission.components;

import gregtech.api.capability.ICoolantHandler;
import gregtech.api.nuclear.fission.ICoolantStats;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.PropertyKey;

import java.util.List;

public class CoolantChannel extends ReactorComponent {

private final Material coolant;
private final ICoolantStats coolant;
private double weight;
private int relatedFuelRodPairs;

Expand All @@ -18,9 +19,9 @@ public class CoolantChannel extends ReactorComponent {
// Allows fission reactors to heat up less than a full liter of coolant.
public double partialCoolant;

public CoolantChannel(double maxTemperature, double thermalConductivity, Material coolant, double mass,
public CoolantChannel(double maxTemperature, double thermalConductivity, ICoolantStats coolant, double mass,
ICoolantHandler inputHandler, ICoolantHandler outputHandler) {
super(coolant.getProperty(PropertyKey.COOLANT).getModeratorFactor(), maxTemperature, thermalConductivity, mass,
super(coolant.getModeratorFactor(), maxTemperature, thermalConductivity, mass,
true);
this.coolant = coolant;
this.weight = 0;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void setWeight(double weight) {
this.weight = weight;
}

public Material getCoolant() {
public ICoolantStats getCoolant() {
return coolant;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package gregtech.api.nuclear.fission.components;

import gregtech.api.unification.material.properties.FissionFuelProperty;
import gregtech.api.nuclear.fission.IFissionFuelStats;

public class FuelRod extends ReactorComponent {

private FissionFuelProperty fuel;
private IFissionFuelStats fuel;

public FuelRod(double maxTemperature, double thermalConductivity, FissionFuelProperty fuel, double mass) {
public FuelRod(double maxTemperature, double thermalConductivity, IFissionFuelStats fuel, double mass) {
super(0, maxTemperature, thermalConductivity, mass, true);
this.fuel = fuel;
}
Expand Down Expand Up @@ -35,11 +35,11 @@ public double getNeutronGenerationTime() {
return fuel.getNeutronGenerationTime();
}

public FissionFuelProperty getFuel() {
public IFissionFuelStats getFuel() {
return fuel;
}

public void setFuel(FissionFuelProperty property) {
public void setFuel(IFissionFuelStats property) {
this.fuel = property;
this.maxTemperature = property.getMaxTemperature();
}
Expand Down
Loading

0 comments on commit 9abbc5c

Please sign in to comment.