mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-11 17:53:32 +08:00
Work in progress for LogisticMiner
* Optimize fuel burning code * Sprayed fuel generates more energy as normal
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using BepInEx;
|
using BepInEx;
|
||||||
|
using BepInEx.Configuration;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Random = UnityEngine.Random;
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
@@ -17,6 +18,8 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
private static long _waterEnergyConsume = 20000000;
|
private static long _waterEnergyConsume = 20000000;
|
||||||
private static int _waterSpeed = 100;
|
private static int _waterSpeed = 100;
|
||||||
private static int _miningScale;
|
private static int _miningScale;
|
||||||
|
private static int _fuelIlsSlot = 3;
|
||||||
|
private static int _fuelPlsSlot = 2;
|
||||||
|
|
||||||
private static float _frame;
|
private static float _frame;
|
||||||
private static float _miningCostRateByTech;
|
private static float _miningCostRateByTech;
|
||||||
@@ -29,6 +32,7 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
|
|
||||||
private static uint _seed = (uint)Random.Range(int.MinValue, int.MaxValue);
|
private static uint _seed = (uint)Random.Range(int.MinValue, int.MaxValue);
|
||||||
private static readonly Dictionary<int, VeinCacheData> PlanetVeinCacheData = new();
|
private static readonly Dictionary<int, VeinCacheData> PlanetVeinCacheData = new();
|
||||||
|
private static readonly Dictionary<int, (long, bool)> Fuels = new();
|
||||||
|
|
||||||
private bool _cfgEnabled = true;
|
private bool _cfgEnabled = true;
|
||||||
|
|
||||||
@@ -46,15 +50,34 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
_miningScale = Config.Bind("General", "MiningScale", _miningScale,
|
_miningScale = Config.Bind("General", "MiningScale", _miningScale,
|
||||||
"0 for Auto(which means having researched makes mining scale 300, otherwise 100). Mining scale(in percents) for slots below half of slot limits, and the scale reduces to 100% smoothly till reach full. Please note that the power consumption increases by the square of the scale which is the same as Advanced Mining Machine")
|
"0 for Auto(which means having researched makes mining scale 300, otherwise 100). Mining scale(in percents) for slots below half of slot limits, and the scale reduces to 100% smoothly till reach full. Please note that the power consumption increases by the square of the scale which is the same as Advanced Mining Machine")
|
||||||
.Value;
|
.Value;
|
||||||
|
_fuelIlsSlot = Config.Bind("General", "ILSFuelSlot", _fuelIlsSlot + 1,
|
||||||
|
new ConfigDescription("Fuel slot for ILS, set to 0 to disable",
|
||||||
|
new AcceptableValueRange<int>(0, 5), Array.Empty<object>()))
|
||||||
|
.Value - 1;
|
||||||
|
_fuelPlsSlot = Config.Bind("General", "PLSFuelSlot", _fuelPlsSlot + 1,
|
||||||
|
new ConfigDescription("Fuel slot for PLS, set to 0 to disable",
|
||||||
|
new AcceptableValueRange<int>(0, 3), Array.Empty<object>()))
|
||||||
|
.Value - 1;
|
||||||
|
if (!_cfgEnabled) return;
|
||||||
|
|
||||||
if (_miningScale < 100)
|
if (_miningScale < 100)
|
||||||
{
|
{
|
||||||
_miningScale = 100;
|
_miningScale = 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_cfgEnabled) return;
|
|
||||||
Harmony.CreateAndPatchAll(typeof(LogisticMiner));
|
Harmony.CreateAndPatchAll(typeof(LogisticMiner));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int SplitIncLevel(ref int n, ref int m, int p)
|
||||||
|
{
|
||||||
|
var level = m / n;
|
||||||
|
var left = m - level * n;
|
||||||
|
n -= p;
|
||||||
|
left -= n;
|
||||||
|
m -= left > 0 ? level * p + left : level * p;
|
||||||
|
return level;
|
||||||
|
}
|
||||||
|
|
||||||
private static void CheckRecipes()
|
private static void CheckRecipes()
|
||||||
{
|
{
|
||||||
_advancedMiningMachineUnlocked = GameMain.history.recipeUnlocked.Contains(119);
|
_advancedMiningMachineUnlocked = GameMain.history.recipeUnlocked.Contains(119);
|
||||||
@@ -66,7 +89,7 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
_miningCostBarrier = (uint)(int)Math.Ceiling(2147483646.0 * _miningCostRateByTech);
|
_miningCostBarrier = (uint)(int)Math.Ceiling(2147483646.0 * _miningCostRateByTech);
|
||||||
_miningCostBarrierOil =
|
_miningCostBarrierOil =
|
||||||
(uint)(int)Math.Ceiling(2147483646.0 * _miningCostRateByTech * 0.401116669f /
|
(uint)(int)Math.Ceiling(2147483646.0 * _miningCostRateByTech * 0.401116669f /
|
||||||
GameMain.gameScenario.gameData.gameDesc.resourceMultiplier);
|
DSPGame.GameDesc.resourceMultiplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void UpdateSpeedScale()
|
private static void UpdateSpeedScale()
|
||||||
@@ -83,6 +106,14 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
{
|
{
|
||||||
Logger.LogInfo("Game Start");
|
Logger.LogInfo("Game Start");
|
||||||
PlanetVeinCacheData.Clear();
|
PlanetVeinCacheData.Clear();
|
||||||
|
Fuels.Clear();
|
||||||
|
foreach (var data in LDB.items.dataArray)
|
||||||
|
{
|
||||||
|
if (data.HeatValue > 0)
|
||||||
|
{
|
||||||
|
Fuels.Add(data.ID, (data.HeatValue, data.Productive));
|
||||||
|
}
|
||||||
|
}
|
||||||
/* Thinking: storage max may affect mining scale?
|
/* Thinking: storage max may affect mining scale?
|
||||||
_localStationMax = LDB.items.Select(2103).prefabDesc.stationMaxItemCount;
|
_localStationMax = LDB.items.Select(2103).prefabDesc.stationMaxItemCount;
|
||||||
_remoteStationMax = LDB.items.Select(2104).prefabDesc.stationMaxItemCount;
|
_remoteStationMax = LDB.items.Select(2104).prefabDesc.stationMaxItemCount;
|
||||||
@@ -157,6 +188,8 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
private static void RecalcVeins(PlanetFactory factory)
|
private static void RecalcVeins(PlanetFactory factory)
|
||||||
{
|
{
|
||||||
var planetId = factory.planetId;
|
var planetId = factory.planetId;
|
||||||
|
lock (PlanetVeinCacheData)
|
||||||
|
{
|
||||||
/* remove planet veins from dict */
|
/* remove planet veins from dict */
|
||||||
if (PlanetVeinCacheData.TryGetValue(planetId, out var vcd))
|
if (PlanetVeinCacheData.TryGetValue(planetId, out var vcd))
|
||||||
{
|
{
|
||||||
@@ -170,15 +203,18 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
PlanetVeinCacheData.Add(planetId, vcd);
|
PlanetVeinCacheData.Add(planetId, vcd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
[HarmonyPatch(typeof(FactorySystem), "CheckBeforeGameTick")]
|
[HarmonyPatch(typeof(FactorySystem), "CheckBeforeGameTick")]
|
||||||
private static void Miner(FactorySystem __instance)
|
private static void FactorySystemLogisticMiner(FactorySystem __instance)
|
||||||
{
|
{
|
||||||
if (_miningSpeedScaleLong <= 0)
|
if (_miningSpeedScaleLong <= 0)
|
||||||
return;
|
return;
|
||||||
var factory = __instance.factory;
|
var factory = __instance.factory;
|
||||||
var planetId = factory.planetId;
|
var planetId = factory.planetId;
|
||||||
|
lock (PlanetVeinCacheData)
|
||||||
|
{
|
||||||
if (PlanetVeinCacheData.TryGetValue(planetId, out var vcd))
|
if (PlanetVeinCacheData.TryGetValue(planetId, out var vcd))
|
||||||
{
|
{
|
||||||
if (vcd.FrameNext > _frame)
|
if (vcd.FrameNext > _frame)
|
||||||
@@ -227,6 +263,7 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
{
|
{
|
||||||
miningScale = _advancedMiningMachineUnlocked ? 300 : 100;
|
miningScale = _advancedMiningMachineUnlocked ? 300 : 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (miningScale > 100 && stationStore.count * 2 > stationStore.max)
|
if (miningScale > 100 && stationStore.count * 2 > stationStore.max)
|
||||||
{
|
{
|
||||||
miningScale = 100 +
|
miningScale = 100 +
|
||||||
@@ -236,7 +273,8 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
|
|
||||||
if (isVein)
|
if (isVein)
|
||||||
{
|
{
|
||||||
(amount, energyConsume) = vcd.Mine(factory, stationStore.itemId, miningScale, _miningSpeedScaleLong,
|
(amount, energyConsume) = vcd.Mine(factory, stationStore.itemId, miningScale,
|
||||||
|
_miningSpeedScaleLong,
|
||||||
stationComponent.energy);
|
stationComponent.energy);
|
||||||
if (amount < 0)
|
if (amount < 0)
|
||||||
{
|
{
|
||||||
@@ -265,17 +303,34 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isCollecting || stationComponent.energy * 2 >= stationComponent.energyMax) continue;
|
if (!isCollecting || stationComponent.energy * 2 >= stationComponent.energyMax) continue;
|
||||||
var index = stationComponent.isStellar ? storage.Length - 2 : storage.Length - 1;
|
var index = stationComponent.isStellar ? _fuelIlsSlot : _fuelPlsSlot;
|
||||||
|
if (index < 0 || index >= storage.Length)
|
||||||
|
continue;
|
||||||
var fuelCount = storage[index].count;
|
var fuelCount = storage[index].count;
|
||||||
if (fuelCount == 0) continue;
|
if (fuelCount == 0) continue;
|
||||||
var heatValue = LDB.items.Select(storage[index].itemId).HeatValue;
|
if (!Fuels.TryGetValue(storage[index].itemId, out var val) || val.Item1 <= 0)
|
||||||
if (heatValue <= 0) continue;
|
continue;
|
||||||
var count = (int)((stationComponent.energyMax - stationComponent.energy) /
|
/* Sprayed fuels */
|
||||||
heatValue);
|
int pretendIncLevel;
|
||||||
|
if (val.Item2 && (pretendIncLevel = storage[index].inc / storage[index].count) > 0)
|
||||||
|
{
|
||||||
|
var count = (int)((stationComponent.energyMax - stationComponent.energy) * 1000L /
|
||||||
|
Cargo.incTable[pretendIncLevel] / 7L);
|
||||||
if (count > fuelCount)
|
if (count > fuelCount)
|
||||||
count = fuelCount;
|
count = fuelCount;
|
||||||
storage[index].count -= count;
|
var incLevel = SplitIncLevel(ref storage[index].count, ref storage[index].inc, count);
|
||||||
stationComponent.energy += count * heatValue;
|
if (incLevel > 10)
|
||||||
|
incLevel = 10;
|
||||||
|
stationComponent.energy += val.Item1 * count * (1000L + Cargo.incTable[incLevel]) / 1000L;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var count = (int)((stationComponent.energyMax - stationComponent.energy) / val.Item1);
|
||||||
|
if (count > fuelCount)
|
||||||
|
count = fuelCount;
|
||||||
|
SplitIncLevel(ref storage[index].count, ref storage[index].inc, count);
|
||||||
|
stationComponent.energy += val.Item1 * count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vcd.FrameNext += _miningFrames;
|
vcd.FrameNext += _miningFrames;
|
||||||
@@ -283,10 +338,12 @@ public class LogisticMiner : BaseUnityPlugin
|
|||||||
|
|
||||||
PerformanceMonitor.EndSample(ECpuWorkEntry.Miner);
|
PerformanceMonitor.EndSample(ECpuWorkEntry.Miner);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class VeinCacheData
|
private class VeinCacheData
|
||||||
{
|
{
|
||||||
public float FrameNext;
|
public float FrameNext;
|
||||||
|
|
||||||
/* stores list of indices to veinData, with an extra INT which indicates cout of veinGroups at last */
|
/* stores list of indices to veinData, with an extra INT which indicates cout of veinGroups at last */
|
||||||
private Dictionary<int, List<int>> _veins = new();
|
private Dictionary<int, List<int>> _veins = new();
|
||||||
private int _mineIndex = -1;
|
private int _mineIndex = -1;
|
||||||
|
|||||||
32
LogisticMiner/package/README.md
Normal file
32
LogisticMiner/package/README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
## LogisticMiner
|
||||||
|
|
||||||
|
### Logistic Storages can mine all ores/water on current planet
|
||||||
|
|
||||||
|
* Inspired
|
||||||
|
by [PlanetMiner](https://dsp.thunderstore.io/package/blacksnipebiu/PlanetMiner)([github](https://github.com/blacksnipebiu/PlanetMiner))
|
||||||
|
.
|
||||||
|
|
||||||
|
But it is heavily optimized to resolve performance, accuracy and other issues in PlanetMiner.
|
||||||
|
* Only recalculate count of veins when vein chunks are changed (added/removed by foundations/Sandbox-Mode, or
|
||||||
|
exhausted), this removes Dictionary allocation on each planet for every frame which may impact performance.
|
||||||
|
* More accurate frame counting by use float number.
|
||||||
|
* Does not increase power consumptions on `Veins Utilization` upgrades.
|
||||||
|
* Separate power consumptions for veins, oil seeps and water.
|
||||||
|
* Power consumptions are counted by groups of veins and count of oil seeps, which is more sensible.
|
||||||
|
* Can burn fuels in certain slot when energy below half of max.
|
||||||
|
* Sprayed fuels generates extra energy as normal.
|
||||||
|
* All used parameters are configurable:
|
||||||
|
* ILS has the same speed as normal Mining Machine for normal ores by default.
|
||||||
|
|
||||||
|
But you can set mining scale in configuration, which makes ILS working like Advance Mining Machines: power
|
||||||
|
consumption increases by the square of the scale, and gradually decrease mining speed over half of the maximum
|
||||||
|
count.
|
||||||
|
|
||||||
|
This applies to all of veins, oils and water.
|
||||||
|
|
||||||
|
Mining scale can be set to 0(by default), which means it is automatically set by tech unlocks, set to 300 when you
|
||||||
|
reaches Advanced Mining Machine, otherwise 100.
|
||||||
|
* 100/s for water by default.
|
||||||
|
* Energy costs: 1MW/vein-group & 10MW/water-slot & 1.8MW/oil-seep(configurable), `Veins Utilization` upgrades
|
||||||
|
does not increase power consumption(unlike PlanetMiner).
|
||||||
|
* Fuels burning slot. Default: 4th for ILS, 3rd for PLS. Set to 0 to disable it.
|
||||||
@@ -34,13 +34,16 @@
|
|||||||
* Inspired
|
* Inspired
|
||||||
by [PlanetMiner](https://dsp.thunderstore.io/package/blacksnipebiu/PlanetMiner)([github](https://github.com/blacksnipebiu/PlanetMiner))
|
by [PlanetMiner](https://dsp.thunderstore.io/package/blacksnipebiu/PlanetMiner)([github](https://github.com/blacksnipebiu/PlanetMiner))
|
||||||
.
|
.
|
||||||
But it is heavily optimized to resolve performance, accuracy and other issues in PlanetMiner:
|
|
||||||
|
But it is heavily optimized to resolve performance, accuracy and other issues in PlanetMiner.
|
||||||
* Only recalculate count of veins when vein chunks are changed (added/removed by foundations/Sandbox-Mode, or
|
* Only recalculate count of veins when vein chunks are changed (added/removed by foundations/Sandbox-Mode, or
|
||||||
exhausted), so this removes Dictionary allocation on each planet for every frame.
|
exhausted), so this removes Dictionary allocation on each planet for every frame.
|
||||||
* More accurate frame counting by use float number.
|
* More accurate frame counting by use float number.
|
||||||
* Does not increase power consumptions on `Veins Utilization` upgrades.
|
* Does not increase power consumptions on `Veins Utilization` upgrades.
|
||||||
* Separate power consumptions for veins, oil seeps and water.
|
* Separate power consumptions for veins, oil seeps and water.
|
||||||
* Power consumptions are counted by groups of veins and count of oil seeps, which is more sensible.
|
* Power consumptions are counted by groups of veins and count of oil seeps, which is more sensible.
|
||||||
|
* Can burn fuels in certain slot when energy below half of max.
|
||||||
|
* Sprayed fuels generates extra energy as normal.
|
||||||
* All used parameters are configurable:
|
* All used parameters are configurable:
|
||||||
* ILS has the same speed as normal Mining Machine for normal ores by default.
|
* ILS has the same speed as normal Mining Machine for normal ores by default.
|
||||||
|
|
||||||
@@ -55,6 +58,7 @@
|
|||||||
* 100/s for water by default.
|
* 100/s for water by default.
|
||||||
* Energy costs: 1MW/vein-group & 10MW/water-slot & 1.8MW/oil-seep(configurable), `Veins Utilization` upgrades
|
* Energy costs: 1MW/vein-group & 10MW/water-slot & 1.8MW/oil-seep(configurable), `Veins Utilization` upgrades
|
||||||
does not increase power consumption(unlike PlanetMiner).
|
does not increase power consumption(unlike PlanetMiner).
|
||||||
|
* Fuels burning slot. Default: 4th for ILS, 3rd for PLS. Set to 0 to disable it.
|
||||||
|
|
||||||
## [HideTips](HideTips)
|
## [HideTips](HideTips)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user