Launching Godot with --automation=<dir> activates an AutomationHarness node that polls <dir>/inbox/ for JSON command files, executes them via a thin facade over existing public surfaces (GameSim, InputMapper, EventAnimator, ControlBar, PieceStockPanel), and writes results plus screenshots back to disk. The black-box simulation boundary is not crossed — every command routes through the same signals/methods a real player would trigger. A stdlib-only Python helper (tools/automation/harness.py) wraps the protocol for test scripts and interactive REPLs. Smoke test passes end-to-end: load mission, place a piece, step 10 turns, capture 14 1280x720 PNGs, handle rejections, quit cleanly. Existing 102 engine unit tests still green.
207 lines
6 KiB
C#
207 lines
6 KiB
C#
using Godot;
|
|
using System;
|
|
using Chessistics.Engine.Model;
|
|
using Chessistics.Engine.Rules;
|
|
using Chessistics.Scripts.Board;
|
|
|
|
namespace Chessistics.Scripts.Input;
|
|
|
|
public partial class InputMapper : Node
|
|
{
|
|
[Signal]
|
|
public delegate void PlacementRequestedEventHandler(int kindIndex, int startCol, int startRow, int endCol, int endRow);
|
|
[Signal]
|
|
public delegate void RemovalRequestedEventHandler(int pieceId);
|
|
[Signal]
|
|
public delegate void CellClickedEventHandler(int col, int row);
|
|
[Signal]
|
|
public delegate void CancelledEventHandler();
|
|
|
|
public enum PlacementPhase { None, SelectingStart, SelectingEnd }
|
|
|
|
private BoardView _boardView = null!;
|
|
private PieceKind? _selectedKind;
|
|
private Coords? _selectedStart;
|
|
private PlacementPhase _phase = PlacementPhase.None;
|
|
private BoardSnapshot? _snapshot;
|
|
private Coords? _hoverCoords;
|
|
|
|
public PlacementPhase CurrentPhase => _phase;
|
|
|
|
public void Initialize(BoardView boardView)
|
|
{
|
|
_boardView = boardView;
|
|
}
|
|
|
|
public void SetSnapshot(BoardSnapshot snapshot)
|
|
{
|
|
GD.Print($"[InputMapper] SetSnapshot called — null? {snapshot == null}");
|
|
_snapshot = snapshot;
|
|
}
|
|
|
|
public void SelectPieceKind(PieceKind kind)
|
|
{
|
|
GD.Print($"[InputMapper] SelectPieceKind: {kind}, phase → SelectingStart");
|
|
_selectedKind = kind;
|
|
_selectedStart = null;
|
|
_phase = PlacementPhase.SelectingStart;
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
_selectedKind = null;
|
|
_selectedStart = null;
|
|
_phase = PlacementPhase.None;
|
|
_boardView.ClearHighlights();
|
|
EmitSignal(SignalName.Cancelled);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_boardView == null || !_boardView.Visible) return;
|
|
|
|
var localPos = _boardView.GetLocalMousePosition();
|
|
var coords = _boardView.PixelToCoords(localPos);
|
|
|
|
if (coords != _hoverCoords)
|
|
{
|
|
_hoverCoords = coords;
|
|
_boardView.SetHoverCell(coords);
|
|
}
|
|
}
|
|
|
|
public override void _UnhandledInput(InputEvent @event)
|
|
{
|
|
if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed)
|
|
{
|
|
if (mouseEvent.ButtonIndex == MouseButton.Left)
|
|
{
|
|
var localPos = _boardView.GetLocalMousePosition();
|
|
GD.Print($"[InputMapper] LEFT CLICK — localPos={localPos}, phase={_phase}");
|
|
HandleLeftClick();
|
|
}
|
|
}
|
|
|
|
if (@event is InputEventKey keyEvent && keyEvent.Pressed && keyEvent.Keycode == Key.Escape)
|
|
{
|
|
Cancel();
|
|
}
|
|
}
|
|
|
|
private void HandleLeftClick()
|
|
{
|
|
var localPos = _boardView.GetLocalMousePosition();
|
|
var coords = _boardView.PixelToCoords(localPos);
|
|
|
|
GD.Print($"[InputMapper] HandleLeftClick — localPos={localPos}, coords={coords}");
|
|
|
|
if (coords == null)
|
|
{
|
|
GD.Print("[InputMapper] coords is null — click outside board");
|
|
return;
|
|
}
|
|
|
|
HandleClickAt(coords.Value);
|
|
}
|
|
|
|
private void HandleClickAt(Coords coords)
|
|
{
|
|
switch (_phase)
|
|
{
|
|
case PlacementPhase.SelectingStart:
|
|
OnStartSelected(coords);
|
|
break;
|
|
|
|
case PlacementPhase.SelectingEnd:
|
|
OnEndSelected(coords);
|
|
break;
|
|
|
|
default:
|
|
EmitSignal(SignalName.CellClicked, coords.Col, coords.Row);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Same effect as a left/right click on a board cell, for automation.
|
|
/// Runs the exact branch HandleLeftClick runs (no InputEvent synthesis).
|
|
/// </summary>
|
|
public void SimulateClick(Coords coords, MouseButton button)
|
|
{
|
|
if (button == MouseButton.Right)
|
|
{
|
|
Cancel();
|
|
return;
|
|
}
|
|
HandleClickAt(coords);
|
|
}
|
|
|
|
private void OnStartSelected(Coords start)
|
|
{
|
|
if (_selectedKind == null || _snapshot == null)
|
|
{
|
|
GD.Print($"[InputMapper] OnStartSelected ABORT — kind={_selectedKind}, snapshot={(_snapshot != null ? "ok" : "null")}");
|
|
return;
|
|
}
|
|
|
|
var boardState = GetBoardStateForValidation();
|
|
if (boardState == null)
|
|
{
|
|
GD.Print("[InputMapper] OnStartSelected ABORT — boardState is null");
|
|
return;
|
|
}
|
|
|
|
var legalEnds = MoveValidator.GetLegalEndCells(_selectedKind.Value, start, boardState);
|
|
GD.Print($"[InputMapper] OnStartSelected({start}) — {legalEnds.Count} legal end cells");
|
|
|
|
if (legalEnds.Count == 0) return;
|
|
|
|
_selectedStart = start;
|
|
_phase = PlacementPhase.SelectingEnd;
|
|
|
|
_boardView.ClearHighlights();
|
|
_boardView.HighlightCells(legalEnds, new Color("#4488FF88"));
|
|
var startCell = _boardView.GetCellView(start);
|
|
startCell?.SetHighlightColor(new Color("#44FFFF44"));
|
|
}
|
|
|
|
private void OnEndSelected(Coords end)
|
|
{
|
|
if (_selectedKind == null || _selectedStart == null) return;
|
|
|
|
var start = _selectedStart.Value;
|
|
var kind = _selectedKind.Value;
|
|
|
|
GD.Print($"[InputMapper] OnEndSelected — placing {kind} from {start} to {end}");
|
|
EmitSignal(SignalName.PlacementRequested, (int)kind, start.Col, start.Row, end.Col, end.Row);
|
|
|
|
// Reset placement state
|
|
_phase = PlacementPhase.None;
|
|
_selectedKind = null;
|
|
_selectedStart = null;
|
|
_boardView.ClearHighlights();
|
|
}
|
|
|
|
private BoardState? GetBoardStateForValidation()
|
|
{
|
|
if (_snapshot == null) return null;
|
|
|
|
var level = new LevelDef
|
|
{
|
|
Width = _snapshot.Width,
|
|
Height = _snapshot.Height,
|
|
Productions = [],
|
|
Demands = [],
|
|
Walls = [],
|
|
Stock = []
|
|
};
|
|
|
|
var state = BoardState.FromLevel(level);
|
|
|
|
for (int c = 0; c < _snapshot.Width; c++)
|
|
for (int r = 0; r < _snapshot.Height; r++)
|
|
state.Grid[c, r] = _snapshot.Grid[c, r];
|
|
|
|
return state;
|
|
}
|
|
}
|