Skip to content

Commit 8e96ea6

Browse files
committed
fix(gameplay): address Codex PR review — snapshot scoring inputs, fix undo net restoration
- StageManager: capture totalCost/boardArea/traceCount before async simulation to prevent race condition with live board edits during simulation - RouteTraceCommand: fix Undo for probe nets that have no traces — restore previous net connection even when RemoveTrace auto-deletes the net
1 parent 85a39b5 commit 8e96ea6

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

Assets/10_Scripts/10_Runtime/40_Managers/StageManager.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ private async UniTaskVoid RunSimulationAndEvaluateAsync(CancellationToken cancel
203203
}
204204
}
205205

206+
// Snapshot scoring inputs before awaiting simulation to avoid race with live board edits.
207+
float totalCost = 0f;
208+
foreach (var component in boardState.Components)
209+
{
210+
if (component.IsFixed) continue;
211+
var def = _simulationManager.GetComponentDefinition(component.ComponentDefinitionId);
212+
if (def != null)
213+
{
214+
totalCost += def.BaseCost;
215+
}
216+
}
217+
218+
var contentBounds = boardState.ComputeContentBounds();
219+
int boardArea = Math.Max(1, contentBounds.Width * contentBounds.Height);
220+
int traceCount = boardState.Traces.Count;
221+
206222
cancellationToken.ThrowIfCancellationRequested();
207223
await _simulationManager.RunSimulationAsync(boardState, probes, true, true, cancellationToken);
208224
cancellationToken.ThrowIfCancellationRequested();
@@ -226,22 +242,7 @@ private async UniTaskVoid RunSimulationAndEvaluateAsync(CancellationToken cancel
226242
evalResult = _objectiveEvaluator.Evaluate(simResult, testCaseInputs.ToArray());
227243
}
228244

229-
// Calculate total component cost by summing BaseCost of each placed component
230-
float totalCost = 0f;
231-
foreach (var component in boardState.Components)
232-
{
233-
// Exclude fixed (pre-placed) components from budget calculation
234-
if (component.IsFixed) continue;
235-
var def = _simulationManager.GetComponentDefinition(component.ComponentDefinitionId);
236-
if (def != null)
237-
{
238-
totalCost += def.BaseCost;
239-
}
240-
}
241-
242245
// Build scoring input from evaluation + board size + stage target.
243-
var contentBounds = _gameManager.BoardState.ComputeContentBounds();
244-
int boardArea = Math.Max(1, contentBounds.Width * contentBounds.Height);
245246
int targetArea = _currentStage.TargetArea;
246247

247248
var scoringInput = new ScoringInput(
@@ -250,7 +251,7 @@ private async UniTaskVoid RunSimulationAndEvaluateAsync(CancellationToken cancel
250251
budgetLimit: _currentStage.BudgetLimit,
251252
boardArea: boardArea,
252253
targetArea: targetArea,
253-
traceCount: boardState.Traces.Count
254+
traceCount: traceCount
254255
);
255256

256257
// Calculate final score breakdown

Assets/10_Scripts/10_Runtime/85_Commands/RouteTraceCommand.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class RouteTraceCommand : ICommand
2222

2323
// State captured during Execute for Undo
2424
private int _netId;
25+
private string _netName;
26+
private int? _restoredRouteNetId;
2527
private readonly List<int> _addedSegmentIds = new List<int>();
2628
// ReSharper disable once NotAccessedField.Local
2729
private bool _createdNewNet;
@@ -64,9 +66,11 @@ public void Execute()
6466

6567
_addedSegmentIds.Clear();
6668
_didMerge = false;
69+
_restoredRouteNetId = null;
6770

6871
// Resolve which net to use (may create a new one)
6972
_netId = ResolveNetId();
73+
_netName = _boardState.GetNet(_netId)?.NetName;
7074

7175
// Connect pins to the net
7276
_boardState.ConnectPinToNet(_netId, _startPin);
@@ -281,15 +285,27 @@ private void DisconnectPinFromNet(PinReference pinRef, Net net)
281285

282286
private void RestorePreviousPinConnection(PinReference pinRef, int? previousNetId)
283287
{
284-
if (!previousNetId.HasValue || previousNetId.Value == _netId)
288+
if (!previousNetId.HasValue)
285289
return;
286290

287-
// Only restore if the previous net still exists
288-
var previousNet = _boardState.GetNet(previousNetId.Value);
291+
int targetNetId = previousNetId.Value;
292+
if (_restoredRouteNetId.HasValue && previousNetId.Value == _netId)
293+
{
294+
targetNetId = _restoredRouteNetId.Value;
295+
}
296+
297+
var previousNet = _boardState.GetNet(targetNetId);
289298
if (previousNet == null)
290-
return;
299+
{
300+
if (previousNetId.Value != _netId || string.IsNullOrEmpty(_netName))
301+
return;
302+
303+
previousNet = _boardState.CreateNet(_netName);
304+
_restoredRouteNetId = previousNet.NetId;
305+
targetNetId = previousNet.NetId;
306+
}
291307

292-
_boardState.ConnectPinToNet(previousNetId.Value, pinRef);
308+
_boardState.ConnectPinToNet(targetNetId, pinRef);
293309
}
294310
}
295311
}

0 commit comments

Comments
 (0)