mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-11-19 17:05:20 +08:00
feat(uxassist): make quick miner restoration and burial optional
This commit is contained in:
1 parent
bc8a0b0189
commit
bffab3c7ca
9 files changed
+136
-54
No files matched your search
@@ -14,14 +14,14 @@ internal static class MinerPlacementDebug
|
||||
[Serializable]
|
||||
internal sealed class Snapshot
|
||||
{
|
||||
public int schema = 3;
|
||||
public int schema = 4;
|
||||
public string algorithm = "parallel-repack-v5-pose-exploration-min3-unlimited-stock";
|
||||
public int minimumCoverage = MinerPlacementSolver.MinimumCoverage;
|
||||
public string version, gameVersion, startedUtc, outcome, error;
|
||||
public int planetId, veinType, veinGroup, initialStock, placedCount, totalCoverage, buriedCount;
|
||||
public int logicalProcessors, workerCount;
|
||||
public float planetRadius, buildRange;
|
||||
public bool unlimitedRange;
|
||||
public bool unlimitedRange, allowVeinReform;
|
||||
public long elapsedMilliseconds;
|
||||
public float[] playerPosition, center, right, forward;
|
||||
public Node[] nodes;
|
||||
|
||||
@@ -27,14 +27,17 @@ internal sealed class MinerPlacementSolver
|
||||
|
||||
private readonly List<Candidate> _candidates;
|
||||
private readonly int _nodeCount;
|
||||
private readonly bool _allowVeinReform;
|
||||
private int _bestBuriedCount;
|
||||
public List<Candidate> Best { get; private set; } = [];
|
||||
public int BestScore { get; private set; }
|
||||
|
||||
public MinerPlacementSolver(List<Candidate> candidates, int nodeCount)
|
||||
// With reform disabled, the caller supplies only burial-free candidates.
|
||||
public MinerPlacementSolver(List<Candidate> candidates, int nodeCount, bool allowVeinReform = true)
|
||||
{
|
||||
_candidates = candidates;
|
||||
_nodeCount = nodeCount;
|
||||
_allowVeinReform = allowVeinReform;
|
||||
}
|
||||
|
||||
public IEnumerator Search(int passes) => Search(passes, 0, CancellationToken.None);
|
||||
@@ -109,7 +112,7 @@ internal sealed class MinerPlacementSolver
|
||||
private IEnumerator GreedyPass(int pass, uint[] keys, SeedCollisions seeds, int removed, SearchResult[] results, int resultIndex,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var layout = new Layout(_nodeCount);
|
||||
var layout = new Layout(_nodeCount, _allowVeinReform);
|
||||
// Selected miners already proven compatible with each candidate, or -1 once it cannot
|
||||
// join. Layouts and burials only grow during a pass, so every exclusion is permanent.
|
||||
var verified = new int[_candidates.Count];
|
||||
@@ -121,7 +124,7 @@ internal sealed class MinerPlacementSolver
|
||||
verified[i] = (removed >= 0 && ReferenceEquals(_candidates[i], seeds.Seeds[removed])) || !seeds.AllowsWithout(i, removed)
|
||||
? -1 : layout.Selected.Count;
|
||||
}
|
||||
else if (pass == 0)
|
||||
else if (_allowVeinReform && pass == 0)
|
||||
{
|
||||
for (var i = 0; i < verified.Length; i++)
|
||||
if (_candidates[i].Buried.Length != 0) verified[i] = -1;
|
||||
@@ -159,7 +162,8 @@ internal sealed class MinerPlacementSolver
|
||||
hash ^= hash >> 16;
|
||||
jitter = 0.65f + (hash & 65535) / 65535f * 0.7f;
|
||||
}
|
||||
var value = gain * jitter - candidate.Buried.Length * 0.0001f;
|
||||
var value = gain * jitter;
|
||||
if (_allowVeinReform) value -= candidate.Buried.Length * 0.0001f;
|
||||
if (value <= priority) continue;
|
||||
if (!layout.FitsFrom(candidate, verified[i]) || !layout.PreservesCoverage(candidate))
|
||||
{
|
||||
@@ -319,7 +323,7 @@ internal sealed class MinerPlacementSolver
|
||||
|
||||
private IEnumerator RepackRegion(SeedCollisions seeds, (int, int, int) region, SearchResult result, CancellationToken cancellationToken)
|
||||
{
|
||||
var layout = new Layout(_nodeCount);
|
||||
var layout = new Layout(_nodeCount, _allowVeinReform);
|
||||
var removed = new List<Candidate>();
|
||||
var centerX = 0f;
|
||||
var centerZ = 0f;
|
||||
@@ -423,15 +427,17 @@ internal sealed class MinerPlacementSolver
|
||||
|
||||
// A partial layout. Score counts each visible node once per covering miner; RawScore
|
||||
// ignores burials and is therefore an upper bound for pruning.
|
||||
private sealed class Layout(int nodeCount)
|
||||
private sealed class Layout(int nodeCount, bool allowVeinReform)
|
||||
{
|
||||
public readonly List<Candidate> Selected = [];
|
||||
private readonly int[] _buried = new int[nodeCount], _uses = new int[nodeCount];
|
||||
private readonly int[] _buried = allowVeinReform ? new int[nodeCount] : null,
|
||||
_uses = allowVeinReform ? new int[nodeCount] : null;
|
||||
public int Score, RawScore, BuriedCount;
|
||||
|
||||
public int Gain(Candidate candidate, out int coverage)
|
||||
{
|
||||
coverage = Coverage(candidate);
|
||||
if (_buried == null) return coverage;
|
||||
var gain = coverage;
|
||||
foreach (var index in candidate.Buried) if (_buried[index] == 0) gain -= _uses[index];
|
||||
return gain;
|
||||
@@ -439,6 +445,7 @@ internal sealed class MinerPlacementSolver
|
||||
|
||||
private int Coverage(Candidate candidate)
|
||||
{
|
||||
if (_buried == null) return candidate.Covered.Length;
|
||||
var coverage = 0;
|
||||
foreach (var index in candidate.Covered) if (_buried[index] == 0) coverage++;
|
||||
return coverage;
|
||||
@@ -457,6 +464,7 @@ internal sealed class MinerPlacementSolver
|
||||
// Every selected miner already keeps the minimum, so only newly buried used nodes matter.
|
||||
public bool PreservesCoverage(Candidate candidate)
|
||||
{
|
||||
if (_buried == null) return true;
|
||||
var affected = false;
|
||||
foreach (var index in candidate.Buried)
|
||||
if (_buried[index] == 0 && _uses[index] > 0) { affected = true; break; }
|
||||
@@ -474,6 +482,12 @@ internal sealed class MinerPlacementSolver
|
||||
public void Add(Candidate candidate)
|
||||
{
|
||||
RawScore += candidate.Covered.Length;
|
||||
if (_buried == null)
|
||||
{
|
||||
Score = RawScore;
|
||||
Selected.Add(candidate);
|
||||
return;
|
||||
}
|
||||
foreach (var index in candidate.Buried)
|
||||
if (_buried[index]++ == 0) { Score -= _uses[index]; BuriedCount++; }
|
||||
foreach (var index in candidate.Covered)
|
||||
@@ -489,6 +503,11 @@ internal sealed class MinerPlacementSolver
|
||||
var candidate = Selected[Selected.Count - 1];
|
||||
Selected.RemoveAt(Selected.Count - 1);
|
||||
RawScore -= candidate.Covered.Length;
|
||||
if (_buried == null)
|
||||
{
|
||||
Score = RawScore;
|
||||
return;
|
||||
}
|
||||
foreach (var index in candidate.Covered)
|
||||
{
|
||||
if (_buried[index] == 0) Score--;
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BepInEx;
|
||||
using BepInEx.Configuration;
|
||||
using UnityEngine;
|
||||
using UXAssist.Common;
|
||||
using UXAssist.Common.GameConstants;
|
||||
@@ -21,6 +22,8 @@ namespace UXAssist.Functions.Factory;
|
||||
[ModFeature("QuickMinerBuild")]
|
||||
public static class QuickMinerBuild
|
||||
{
|
||||
public static ConfigEntry<bool> AllowVeinReform;
|
||||
|
||||
private static Job _job;
|
||||
private static readonly Stopwatch FrameTimer = new();
|
||||
private static UIRealtimeTip _resultTip;
|
||||
@@ -55,7 +58,7 @@ public static class QuickMinerBuild
|
||||
}
|
||||
try
|
||||
{
|
||||
_job = new Job(player, planet);
|
||||
_job = new Job(player, planet, AllowVeinReform.Value);
|
||||
UIRealtimeTip.Popup(I18NKeys.QuickMinerSearching.Translate());
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -177,12 +180,13 @@ public static class QuickMinerBuild
|
||||
private readonly List<ColliderData> _obstacles = [], _nearObstacles = [];
|
||||
private readonly List<Vector3> _ejectors = [];
|
||||
private readonly List<Candidate> _candidates = [];
|
||||
private readonly List<int> _covered = [], _buried = [], _reachable = [], _touching = [];
|
||||
private readonly List<int> _covered = [], _reachable = [], _touching = [];
|
||||
private readonly List<int> _buried;
|
||||
private readonly Box[] _buildBoxes, _obstacleBoxes;
|
||||
private readonly ColliderData[] _nativeObstacles;
|
||||
private readonly ColliderData _body;
|
||||
private readonly float _footprintRadius, _halfDiagonal;
|
||||
private readonly bool _capsule;
|
||||
private readonly bool _capsule, _allowVeinReform;
|
||||
private readonly MinerPlacementDebug.Snapshot _debug = new();
|
||||
private readonly List<MinerPlacementDebug.Phase> _phases = [];
|
||||
private readonly List<MinerPlacementDebug.Placement> _placements = [];
|
||||
@@ -202,10 +206,12 @@ public static class QuickMinerBuild
|
||||
GameMain.localPlanet == _planet && _planet.factory == _factory && _planet.physics == _physics && _planet.factoryLoaded;
|
||||
public bool WaitingForWorkers => _search != null && !_search.IsCompleted;
|
||||
|
||||
public Job(Player player, PlanetData planet)
|
||||
public Job(Player player, PlanetData planet, bool allowVeinReform)
|
||||
{
|
||||
_player = player;
|
||||
_planet = planet;
|
||||
_allowVeinReform = allowVeinReform;
|
||||
_buried = allowVeinReform ? [] : null;
|
||||
_factory = planet.factory;
|
||||
_physics = planet.physics;
|
||||
_item = LDB.items.Select(ItemIds.MiningMachine);
|
||||
@@ -237,6 +243,7 @@ public static class QuickMinerBuild
|
||||
_debug.playerPosition = MinerPlacementDebug.Vector(player.position);
|
||||
_debug.buildRange = player.mecha.buildArea;
|
||||
_debug.unlimitedRange = FactoryPatch.RemoveBuildRangeLimitEnabled.Value;
|
||||
_debug.allowVeinReform = allowVeinReform;
|
||||
_debug.buildColliders = desc.buildColliders.Select(MinerPlacementDebug.Collider.Capture).ToArray();
|
||||
_debug.minerColliders = _nativeObstacles.Select(MinerPlacementDebug.Collider.Capture).ToArray();
|
||||
_debug.outcome = "cancelled";
|
||||
@@ -276,7 +283,7 @@ public static class QuickMinerBuild
|
||||
_result = I18NKeys.QuickMinerNoVeins.Translate();
|
||||
yield break;
|
||||
}
|
||||
RestoreTargetVeins();
|
||||
if (_allowVeinReform) RestoreTargetVeins();
|
||||
var reach = 9.5f * _planet.realRadius / 200f;
|
||||
var width = _maxX - _minX + reach * 2;
|
||||
var depth = _maxZ - _minZ + reach * 2;
|
||||
@@ -291,7 +298,7 @@ public static class QuickMinerBuild
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
var solver = new MinerPlacementSolver(_candidates, _nodes.Count);
|
||||
var solver = new MinerPlacementSolver(_candidates, _nodes.Count, _allowVeinReform);
|
||||
var work = Search(solver, "coarse", 10);
|
||||
while (work.MoveNext()) yield return null;
|
||||
work = Expand(solver.Best, 0.35f, 3f);
|
||||
@@ -371,6 +378,7 @@ public static class QuickMinerBuild
|
||||
{
|
||||
ref var vein = ref _factory.veinPool[i];
|
||||
if (vein.id != i || vein.amount <= 0 || vein.type is EVeinType.None or EVeinType.Oil) continue;
|
||||
if (!_allowVeinReform && vein.pos.magnitude < _planet.realRadius - 40f) continue;
|
||||
var distance = (vein.pos.normalized * _planet.realRadius - _player.position).sqrMagnitude;
|
||||
if (distance >= nearest) continue;
|
||||
nearest = distance;
|
||||
@@ -420,7 +428,7 @@ public static class QuickMinerBuild
|
||||
}
|
||||
}
|
||||
FindEjectors(radius);
|
||||
var reserved = ReservedNodes();
|
||||
var reserved = _allowVeinReform ? ReservedNodes() : null;
|
||||
for (var i = 1; i < _factory.veinCursor; i++)
|
||||
{
|
||||
ref var vein = ref _factory.veinPool[i];
|
||||
@@ -432,7 +440,7 @@ public static class QuickMinerBuild
|
||||
Id = i, Target = vein.groupIndex == group, Type = vein.type,
|
||||
Position = vein.pos, OriginalPosition = vein.pos, Collider = _physics.GetColliderData(vein.colliderId),
|
||||
Visible = vein.pos.magnitude >= _planet.realRadius - 40f,
|
||||
CanBury = vein.groupIndex == group && vein.minerCount == 0 && !reserved.Contains(i)
|
||||
CanBury = _allowVeinReform && vein.groupIndex == group && vein.minerCount == 0 && !reserved.Contains(i)
|
||||
});
|
||||
}
|
||||
var existing = new List<MinerPlacementDebug.ExistingMiner>();
|
||||
@@ -605,7 +613,7 @@ public static class QuickMinerBuild
|
||||
if (_covered.Count < MinerPlacementSolver.MinimumCoverage) { _debug.rejections.noCoverage++; return; }
|
||||
for (var i = 0; i < _buildBoxes.Length; i++)
|
||||
_buildBoxes[i] = new Box(_item.prefabDesc.buildColliders[i].BindToObject(0, 0, EObjectType.None, pos, rotation));
|
||||
_buried.Clear();
|
||||
_buried?.Clear();
|
||||
foreach (var i in _touching)
|
||||
{
|
||||
var node = _nodes[i];
|
||||
@@ -613,8 +621,11 @@ public static class QuickMinerBuild
|
||||
if (!node.CanBury) { _debug.rejections.veinCollision++; return; }
|
||||
_buried.Add(i);
|
||||
}
|
||||
foreach (var index in _buried) _covered.Remove(index);
|
||||
if (_covered.Count < MinerPlacementSolver.MinimumCoverage) { _debug.rejections.noCoverage++; return; }
|
||||
if (_allowVeinReform)
|
||||
{
|
||||
foreach (var index in _buried) _covered.Remove(index);
|
||||
if (_covered.Count < MinerPlacementSolver.MinimumCoverage) { _debug.rejections.noCoverage++; return; }
|
||||
}
|
||||
foreach (var obstacle in _nearObstacles)
|
||||
if (Hits(obstacle)) { _debug.rejections.buildingCollision++; return; }
|
||||
if (!HasGround(pos, rotation)) { _debug.rejections.ground++; return; }
|
||||
@@ -623,7 +634,7 @@ public static class QuickMinerBuild
|
||||
_candidates.Add(new Candidate
|
||||
{
|
||||
X = x, Z = z, Yaw = yaw, Position = pos, Rotation = rotation, Radius = _footprintRadius,
|
||||
Covered = _covered.ToArray(), Buried = _buried.ToArray(),
|
||||
Covered = _covered.ToArray(), Buried = _buried?.ToArray() ?? [],
|
||||
BuildBoxes = (Box[])_buildBoxes.Clone(), ObstacleBoxes = (Box[])_obstacleBoxes.Clone()
|
||||
});
|
||||
}
|
||||
@@ -715,8 +726,9 @@ public static class QuickMinerBuild
|
||||
return;
|
||||
}
|
||||
var placed = new List<(Candidate Candidate, int Prebuild, MinerPlacementDebug.Placement Debug)>();
|
||||
var plannedBurial = layout.SelectMany(candidate => candidate.Buried).Select(index => _nodes[index].Id).ToHashSet();
|
||||
var hidden = new Dictionary<int, Vector3>();
|
||||
var plannedBurial = _allowVeinReform
|
||||
? layout.SelectMany(candidate => candidate.Buried).Select(index => _nodes[index].Id).ToHashSet() : null;
|
||||
var hidden = _allowVeinReform ? new Dictionary<int, Vector3>() : null;
|
||||
var coverage = 0;
|
||||
var buriedCount = 0;
|
||||
var validator = new BuildTool_Click();
|
||||
@@ -738,13 +750,16 @@ public static class QuickMinerBuild
|
||||
};
|
||||
_placements.Add(debug);
|
||||
// No yield between burial, native validation, item consumption, and creation.
|
||||
foreach (var index in candidate.Buried)
|
||||
if (_allowVeinReform)
|
||||
{
|
||||
var node = _nodes[index];
|
||||
ref var vein = ref _factory.veinPool[node.Id];
|
||||
if (vein.id != node.Id || hidden.ContainsKey(node.Id)) continue;
|
||||
hidden.Add(node.Id, vein.pos);
|
||||
SetVeinPosition(node.Id, vein.pos.normalized * (_planet.realRadius - 50f));
|
||||
foreach (var index in candidate.Buried)
|
||||
{
|
||||
var node = _nodes[index];
|
||||
ref var vein = ref _factory.veinPool[node.Id];
|
||||
if (vein.id != node.Id || hidden.ContainsKey(node.Id)) continue;
|
||||
hidden.Add(node.Id, vein.pos);
|
||||
SetVeinPosition(node.Id, vein.pos.normalized * (_planet.realRadius - 50f));
|
||||
}
|
||||
}
|
||||
// Validation needs current Unity colliders; GPU buffers are synced once at the end.
|
||||
_physics.RefreshColliders();
|
||||
@@ -775,11 +790,15 @@ public static class QuickMinerBuild
|
||||
{
|
||||
validator._Free();
|
||||
// A failed/shortened layout must not leave nodes buried for unplaced miners.
|
||||
var needed = new HashSet<int>();
|
||||
foreach (var entry in placed)
|
||||
foreach (var index in entry.Candidate.Buried) needed.Add(_nodes[index].Id);
|
||||
foreach (var entry in hidden)
|
||||
if (!needed.Contains(entry.Key)) SetVeinPosition(entry.Key, entry.Value);
|
||||
if (_allowVeinReform)
|
||||
{
|
||||
var needed = new HashSet<int>();
|
||||
foreach (var entry in placed)
|
||||
foreach (var index in entry.Candidate.Buried) needed.Add(_nodes[index].Id);
|
||||
foreach (var entry in hidden)
|
||||
if (!needed.Contains(entry.Key)) SetVeinPosition(entry.Key, entry.Value);
|
||||
buriedCount = needed.Count;
|
||||
}
|
||||
foreach (var entry in placed)
|
||||
{
|
||||
var ids = new List<int>();
|
||||
@@ -796,8 +815,7 @@ public static class QuickMinerBuild
|
||||
entry.Debug.finalVeins = ids.ToArray();
|
||||
coverage += ids.Count;
|
||||
}
|
||||
buriedCount = needed.Count;
|
||||
RefreshVeins(buriedCount > 0 ? 1 : 0);
|
||||
if (_allowVeinReform) RefreshVeins(buriedCount > 0 ? 1 : 0);
|
||||
}
|
||||
_debug.placedCount = placed.Count;
|
||||
_debug.totalCoverage = coverage;
|
||||
@@ -816,6 +834,7 @@ public static class QuickMinerBuild
|
||||
if (vein.id != node.Id || vein.type != node.Type || vein.amount <= 0 || vein.colliderId <= 0 ||
|
||||
(vein.pos - node.Position).sqrMagnitude > 0.0001f) return false;
|
||||
}
|
||||
if (!_allowVeinReform) return true;
|
||||
var reserved = ReservedNodes();
|
||||
foreach (var candidate in layout)
|
||||
foreach (var index in candidate.Buried)
|
||||
@@ -870,7 +889,8 @@ public static class QuickMinerBuild
|
||||
if (!valid || preview.condition != EBuildCondition.Ok || preview.coverObjId != 0) return false;
|
||||
// Include every planned burial now, so a later miner cannot reduce this one
|
||||
// below the minimum after its item has already been consumed.
|
||||
debug.plannedVeins = debug.nativeVeins.Where(id => !plannedBurial.Contains(id)).ToArray();
|
||||
debug.plannedVeins = _allowVeinReform
|
||||
? debug.nativeVeins.Where(id => !plannedBurial.Contains(id)).ToArray() : debug.nativeVeins;
|
||||
if (debug.plannedVeins.Length < MinerPlacementSolver.MinimumCoverage)
|
||||
{ debug.outcome = "below-minimum-coverage"; return false; }
|
||||
for (var i = 0; i < preview.paramCount; i++)
|
||||
|
||||
Reference in new issue
Block a user