feat: 缓存加速倍率、能源卡数量
This commit is contained in:
parent
2ac589066b
commit
704f8ebd93
|
|
@ -64,6 +64,9 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTick
|
|||
|
||||
public EntitySpeedTickerMenu menu; // 当前打开的菜单实例
|
||||
private boolean networkEnergySufficient = true; // 网络能量是否充足
|
||||
private int cachedSpeed = -1; // 缓存的加速倍率
|
||||
private int cachedEnergyCardCount = -1; // 缓存的能量卡数量
|
||||
|
||||
|
||||
private static volatile Method cachedFEExtractMethod;
|
||||
private static volatile boolean FE_UNAVAILABLE;
|
||||
|
|
@ -190,6 +193,10 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTick
|
|||
*/
|
||||
@Override
|
||||
public void upgradesChanged() {
|
||||
// 更新缓存的升级卡数量和加速倍率
|
||||
this.cachedEnergyCardCount = getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD);
|
||||
this.cachedSpeed = calculateSpeed();
|
||||
|
||||
if (menu != null) {
|
||||
menu.broadcastChanges();
|
||||
}
|
||||
|
|
@ -233,17 +240,24 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTick
|
|||
return;
|
||||
}
|
||||
|
||||
int speed = calculateSpeed();
|
||||
if (speed <= 0) {
|
||||
// 如果缓存未初始化(第一次 tick),计算并更新缓存
|
||||
if (cachedEnergyCardCount == -1 || cachedSpeed == -1) {
|
||||
this.cachedEnergyCardCount = getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD);
|
||||
this.cachedSpeed = calculateSpeed();
|
||||
}
|
||||
|
||||
if (cachedSpeed <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double requiredPower = calculateRequiredPower(speed, blockId);
|
||||
// 使用 PowerUtils 的缓存获取能耗,并应用方块特定的倍率
|
||||
double requiredPower = PowerUtils.getCachedPower(cachedSpeed, cachedEnergyCardCount)
|
||||
* ConfigParsingUtils.getMultiplierForBlock(blockId, List.of(ModConfig.INSTANCE.entityTickerMultipliers));
|
||||
if (!extractPower(requiredPower)) {
|
||||
return;
|
||||
}
|
||||
|
||||
performTicks(blockEntity, ticker, speed);
|
||||
performTicks(blockEntity, ticker, cachedSpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -274,18 +288,6 @@ public class EntitySpeedTickerPart extends UpgradeablePart implements IGridTick
|
|||
return PowerUtils.computeProductWithCap(getUpgrades(), 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算所需能量。
|
||||
* @param speed 加速倍率
|
||||
* @param blockId 目标方块ID
|
||||
* @return 所需能量
|
||||
*/
|
||||
private double calculateRequiredPower(int speed, String blockId) {
|
||||
int energyCardCount = getUpgrades().getInstalledUpgrades(AEItems.ENERGY_CARD);
|
||||
double multiplier = ConfigParsingUtils.getMultiplierForBlock(blockId, List.of(ModConfig.INSTANCE.entityTickerMultipliers));
|
||||
return PowerUtils.computeFinalPowerForProduct(speed, energyCardCount) * multiplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取网络能量并更新状态,优先从 AE2 网络提取 AE 能量,不足时从磁盘提取 FE 能量。
|
||||
* @param requiredPower 所需能量(AE 单位)
|
||||
|
|
|
|||
|
|
@ -18,19 +18,18 @@ public final class PowerUtils {
|
|||
private static final int[] VALID_MULTIPLIERS = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
|
||||
private static final int[] VALID_ENERGY_CARD_COUNTS = {0, 1, 2, 3, 4, 5, 6, 7, 8};
|
||||
private static final AtomicInteger checkCounter = new AtomicInteger(0); // 原子计数器
|
||||
private static final int CHECK_COUNTER_THRESHOLD = 100; // 配置检查阈值
|
||||
private static final int CHECK_COUNTER_THRESHOLD = 100; // 配置检查阈值
|
||||
private static final int CHECK_COUNTER_RESET_THRESHOLD = 10000; // 计数器重置阈值
|
||||
private static volatile Map<Integer, Map<Integer, Double>> powerCache = new HashMap<>();
|
||||
private static volatile Map<Integer, Double> ratioCache = new HashMap<>();
|
||||
private static volatile int lastEntityTickerCost = -1; // 使用 int,与 entityTickerCost 类型一致
|
||||
private static volatile int lastEntityTickerCost = -1;
|
||||
|
||||
// 静态初始化块,预计算缓存
|
||||
static {
|
||||
initializeCaches();
|
||||
}
|
||||
|
||||
private PowerUtils() {
|
||||
}
|
||||
private PowerUtils() {}
|
||||
|
||||
/**
|
||||
* 初始化所有可能的缓存条目
|
||||
|
|
@ -124,13 +123,13 @@ public final class PowerUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* 计算最终能耗。
|
||||
* 直接从缓存获取能耗值
|
||||
*
|
||||
* @param product 加速卡乘积
|
||||
* @param energyCardCount 能量卡数量
|
||||
* @return 最终能耗值
|
||||
* @return 缓存的能耗值
|
||||
*/
|
||||
public static double computeFinalPowerForProduct(int product, int energyCardCount) {
|
||||
public static double getCachedPower(int product, int energyCardCount) {
|
||||
if (product <= 1) return 0.0;
|
||||
// 每 100 次调用检查一次配置
|
||||
if (checkCounter.getAndIncrement() % CHECK_COUNTER_THRESHOLD == 0) {
|
||||
|
|
@ -143,26 +142,10 @@ public final class PowerUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
// 查找缓存
|
||||
return getCachedPower(product, energyCardCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接从缓存获取能耗值(无配置检查)。
|
||||
*
|
||||
* @param product 加速卡乘积
|
||||
* @param energyCardCount 能量卡数量
|
||||
* @return 缓存的能耗值
|
||||
*/
|
||||
public static double getCachedPower(int product, int energyCardCount) {
|
||||
Map<Integer, Double> energyCardMap = powerCache.get(product);
|
||||
if (energyCardMap == null) {
|
||||
return 0.0;
|
||||
}
|
||||
if (energyCardMap == null) return 0.0;
|
||||
Double cachedPower = energyCardMap.get(energyCardCount);
|
||||
if (cachedPower == null) {
|
||||
return 0.0;
|
||||
}
|
||||
if (cachedPower == null) return 0.0;
|
||||
return cachedPower;
|
||||
}
|
||||
|
||||
|
|
@ -174,9 +157,7 @@ public final class PowerUtils {
|
|||
*/
|
||||
public static double getCachedRatio(int energyCardCount) {
|
||||
Double cachedRatio = ratioCache.get(energyCardCount);
|
||||
if (cachedRatio == null) {
|
||||
return 1.0;
|
||||
}
|
||||
if (cachedRatio == null) return 1.0;
|
||||
return cachedRatio;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user