sots-engine/src/game/ai/agent.cpp
alex 3ca010978c game/ai: the order block, the turn's phase spine, and what a command costs
Adds the half of the AI's turn that is arithmetic rather than judgement: what an
order looks like in the command block, which orders advance the save's
modification counter and by how much, and the phase/pass skeleton the decisions
hang in.

The counter's per-command cost turns out to have a sharp boundary. Applying an
element of command lists 1..16 advances it; applying an element of lists 17..27
does not, and one of the six flag-gated single commands is free as well. So a
uniform per-element cost model is wrong on any turn that touches the free half.

Two behaviours here are not conveniences and change the output:

  * every submitted block costs at least one, because the send-buffer build sets
    the research-rate gate unconditionally whatever the player did. On a quiet
    board that is the largest term in the turn's delta -- four of the ten command
    bumps on the reference turn are exactly this, and one of the four is the
    human's;
  * an AI fleet order costs three where the interface's costs two, because the
    AI's bridge issues the fleet-task command twice, mode 0 then mode 1, and the
    adder keys on (fleet, mode).

The phase spine records the one thing a literal port gets wrong: the turn submits
at phase 28 of 34, the submit latches the client closed before it builds the send
buffer, and every order the last five phases issue -- one of which is a colonize
order -- is refused. Tested through the client rather than by asserting a flag.

The task walk reproduces the two passes: rank once, walk twice, and refuse every
write in the first pass at the client rather than trusting the caller to check.

Nothing here decides anything. Which tasks exist and what each one wants are
questions about the board, and no part of this models the board; the module
supplies the order API, the pass gate and the cost function, and a caller
supplies the decisions.

game/ai tests 233 -> 423 checks; ctest 51/51 -> 53/53. Not linked into the
standalone driver, whose divergence on the reference pair is unchanged at 128.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ARBgSooAfokKUy6wKUKEyZ
2026-09-08 16:27:38 -04:00

76 lines
2.8 KiB
C++

#include "game/ai/agent.h"
namespace sots::ai {
namespace {
// Phases are numbered by their position in the original's body. Names are given only where a log
// string or an already-named callee pins one; the rest stay empty on purpose, because a plausible
// name for an unread phase is how a guess becomes a fact.
std::vector<ProcessTurnPhase> BuildPhases() {
std::vector<ProcessTurnPhase> p(kProcessTurnPhaseCount);
for (int i = 0; i < kProcessTurnPhaseCount; ++i) {
p[static_cast<std::size_t>(i)].index = i;
p[static_cast<std::size_t>(i)].dead = PhaseRunsAfterSubmit(i);
}
auto at = [&p](int i) -> ProcessTurnPhase& { return p[static_cast<std::size_t>(i)]; };
at(0).name = "log banner";
at(2).emits = PhaseEmission::Group5Gate;
at(2).speciesRestricted = true;
at(10).name = "survival outlook";
at(11).name = "survival outlook log";
at(12).name = "surrender roll"; // the AI's only chance draw in the turn body
at(14).emits = PhaseEmission::Group4Gate;
at(15).name = "set research rate";
at(15).emits = PhaseEmission::ResearchRate;
at(18).name = "set research rate and target";
at(18).emits = PhaseEmission::ResearchTarget;
at(kTaskListPhase).name = "task list";
at(kTaskListPhase).emits = PhaseEmission::TaskList;
at(21).name = "system rates";
at(21).emits = PhaseEmission::SystemRates;
at(23).emits = PhaseEmission::PopulationCmd;
at(24).emits = PhaseEmission::FleetLayout;
at(26).name = "new design";
at(26).emits = PhaseEmission::NewDesign;
at(kSubmitPhase).name = "end turn";
at(kSubmitPhase).emits = PhaseEmission::Submit;
at(32).name = "colonize";
at(32).emits = PhaseEmission::Colonize;
return p;
}
} // namespace
const std::vector<ProcessTurnPhase>& ProcessTurnPhases() {
static const std::vector<ProcessTurnPhase> kPhases = BuildPhases();
return kPhases;
}
bool PhaseCanEmit(const ProcessTurnPhase& phase, sim::Species species) {
if (phase.emits == PhaseEmission::None) return false;
if (phase.emits == PhaseEmission::Submit) return false;
if (phase.dead) return false;
if (phase.speciesRestricted && species != kGroup5GatePhaseSpecies) return false;
return true;
}
const std::vector<int>& TaskWalkPasses() {
static const std::vector<int> kPasses = {static_cast<int>(TaskPass::Reserve),
static_cast<int>(TaskPass::Fill)};
return kPasses;
}
void RunTaskList(std::vector<RankedTask>& tasks, const TaskPriorityPolicy& policy,
OrderClient& client, const TaskAction& action) {
Rank(tasks, policy);
for (int pass : TaskWalkPasses()) {
client.EnterTaskPass(pass);
for (const RankedTask& t : tasks) {
if (action) action(t, pass, client);
}
client.LeaveTaskPass();
}
}
} // namespace sots::ai