diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 2fed41a7..06298af1 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,38 +1,29 @@ # A Linux container carrying every toolchain this repo's dev loop needs: the -# pinned Rust toolchain, Node and python3, plus (until the C# is removed) the -# pinned .NET SDK with wasm-tools, PowerShell and Lua 5.2. +# pinned Rust toolchain, Node and python3. # CLAUDE.md's "Devcontainer" section says what it covers and what still needs -# the host (benchmarks and the Factorio oracle capture). +# the host (the Factorio oracle capture). # -# Pinned to the exact SDK in global.json, by digest. That is the whole point of -# the container: `rollForward: latestMajor` only rolls FORWARD, so a host whose -# package manager trails the pin cannot build at all - measured 2026-09-21, when -# global.json moved to 10.0.401 and Homebrew still shipped 10.0.400. -# -# WHEN global.json MOVES, THIS LINE MUST MOVE WITH IT, and nothing enforces that. -# The Devcontainer CI job builds this image and asserts `dotnet --version` -# resolves inside the repo, which is what turns a forgotten bump into a red -# check rather than a container that silently disagrees with the repo. +# Plain Ubuntu 24.04 (noble), pinned by digest so every rebuild starts from +# the same bytes. It was the .NET SDK image until the C# was removed; that +# image is itself noble, so the apt packages below did not change. # Re-pin with: -# docker buildx imagetools inspect mcr.microsoft.com/dotnet/sdk: -FROM mcr.microsoft.com/dotnet/sdk:10.0.402@sha256:0d99ebf1ccad15037b5c98c9fe8979d6b8dbac23812feae2277a0b277b5c5ffa +# docker buildx imagetools inspect ubuntu:24.04 +FROM ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac -# The base image is Ubuntu 24.04 (noble) and already has a `ubuntu` user at uid -# 1000, so there is no user to create - only sudo to add, for the postCreate -# chown of the named volumes. +# The base image already has a `ubuntu` user at uid 1000, so there is no user +# to create - only sudo to add, for the postCreate chown of the named volumes. # -# lua5.2 is the package CI installs, and on noble it is 5.2.4 - the exact -# version the README's Lua performance log was measured against, and what -# nickblah/lua:5.2-alpine provides in tools/check-lua.sh. It ships the binaries -# as `lua5.2` and `luac5.2`, which is what the transpile-lua CI job calls them; -# tools/check-lua.sh prefers them over Docker when they are on PATH. +# git is for vite.config.ts, which stamps the build with `git describe`. # -# python3 is only for trim-factorio-oracle.py. The capture itself cannot run in -# here - it needs a real Factorio install - but the trim half is worth having. +# python3 is for trim-factorio-oracle.py, and for the Devcontainer CI job's +# toolchain check, which reads rust/rust-toolchain.toml with tomllib. The +# capture itself cannot run in here - it needs a real Factorio install - but +# the trim half is worth having. # # gcc and libc6-dev are for Rust: cargo links native code (build scripts, proc # macros, the test binaries) with `cc`. Nothing in rust/Cargo.lock compiles C, -# so the rest of build-essential (g++, make) is not needed. +# so the rest of build-essential (g++, make) is not needed. The wasm32 build +# links with rust-lld, which ships with the toolchain. RUN apt-get update \ && apt-get install -y --no-install-recommends \ ca-certificates \ @@ -40,7 +31,6 @@ RUN apt-get update \ gcc \ git \ libc6-dev \ - lua5.2 \ python3 \ sudo \ xz-utils \ @@ -74,32 +64,6 @@ RUN set -eux; \ node --version; \ npm --version -# PowerShell, because src/lua/Invoke-LuaBuild.ps1 is the only way to regenerate -# src/lua and the transpile-lua CI job runs it with `pwsh -File`. -# -# Installed as a dotnet tool rather than from Microsoft's apt repo: the tool -# feed carries arm64 and amd64 under one command, so this stays one code path -# for both architectures. The script itself sets DOTNET_ROLL_FORWARD=Major so -# the net7.0 CSharp.lua launcher runs on this .NET 10 runtime - no .NET 7 -# runtime is needed here, despite what the launcher's target framework implies. -ARG POWERSHELL_VERSION=7.6.5 -ENV DOTNET_TOOL_PATH=/usr/local/dotnet-tools -ENV PATH=${DOTNET_TOOL_PATH}:${PATH} -RUN dotnet tool install PowerShell \ - --version "${POWERSHELL_VERSION}" \ - --tool-path "${DOTNET_TOOL_PATH}" \ - && pwsh --version - -# Needed by BrowserWasm and BlazorWebApp, so a full-solution `dotnet build` -# works in here. Installed at image build so it is not a surprise later: -# docker-build.sh has to tell you to prepend `dotnet workload restore`, and the -# point of this image is that you do not have to. -RUN dotnet workload install wasm-tools --skip-sign-check \ - && dotnet workload list - -ENV DOTNET_CLI_TELEMETRY_OPTOUT=1 \ - DOTNET_NOLOGO=1 - USER ubuntu # Rust, for rust/ and for `npm run build-wasm` in src/vue, which builds the diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index a3f3ea4f..d70d8af9 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,25 +1,15 @@ // A Linux container for the whole dev loop: the Rust planner (cargo build and -// test in rust/, and the WASM build the site loads), the Vue front-end, and, -// until the C# is removed, dotnet build/test and the Lua transpile with its 5.2 -// checks. CLAUDE.md's "Devcontainer" section says what it covers and what still -// needs the host - benchmarks and the Factorio oracle capture. +// test in rust/, and the WASM build the site loads) and the Vue front-end. +// CLAUDE.md's "Devcontainer" section says what it covers and what still needs +// the host - the Factorio oracle capture. // -// The Dockerfile pins the SDK from global.json by digest. That is the reason -// this exists: `rollForward: latestMajor` only rolls forward, so a host whose -// package manager trails the pin cannot build the repo at all. It also carries -// the Rust toolchain rust/rust-toolchain.toml pins, already installed. +// The Dockerfile carries the Rust toolchain rust/rust-toolchain.toml pins, +// already installed, and an exact, checksummed Node. { "name": "FactorioTools", "build": { "dockerfile": "Dockerfile" }, // uid 1000 in the base image, so bind-mounted files are not root-owned. "remoteUser": "ubuntu", - "containerEnv": { - // Outside the bind mount, so a Linux restore never fights the host's - // own NuGet cache. docker-build.sh puts this on the host at - // ~/.cache/factorio-nuget; here it is a named volume instead, so it - // survives a rebuild without being shared with the host's SDK. - "NUGET_PACKAGES": "/home/ubuntu/.cache/nuget" - }, "mounts": [ // src/vue/node_modules holds platform-native binaries - esbuild, // @parcel/watcher, fsevents on a Mac. Sharing the host's copy would @@ -30,11 +20,6 @@ "target": "${containerWorkspaceFolder}/src/vue/node_modules", "type": "volume" }, - { - "source": "factoriotools-nuget-${devcontainerId}", - "target": "/home/ubuntu/.cache/nuget", - "type": "volume" - }, // rust/target is the same problem as node_modules: Linux build output // in the bind mount would sit beside, and invalidate, a Mac host's own // cargo builds. A volume keeps them apart and survives a rebuild. @@ -66,9 +51,7 @@ "portsAttributes": { "5173": { "label": "Vite (needs --host)" } }, - // A new volume is root-owned, hence the chown. Submodules are required to - // build the C# - FluteSharp and delaunator-sharp are referenced by the core - // library, CSharp.lua by the transpile - and a fresh clone has none of them. + // A new volume is root-owned, hence the chown. // The safe.directory line is a no-op under OrbStack, where the mount already // appears owned by uid 1000; it is insurance on a Linux host whose own uid // is not 1000, where git otherwise refuses with "dubious ownership" and @@ -77,16 +60,10 @@ // planner it produces, and fail without it. It is a release build of the // planner and its dependencies, so it is the slow step here; the // Devcontainer section in CLAUDE.md has the measured time. - "postCreateCommand": "sudo chown ubuntu:ubuntu src/vue/node_modules /home/ubuntu/.cache/nuget rust/target /home/ubuntu/.cargo/registry /home/ubuntu/.cargo/git && git config --global --add safe.directory \"${containerWorkspaceFolder}\" && git submodule update --init --recursive && npm --prefix src/vue ci && npm --prefix src/vue run build-wasm", + "postCreateCommand": "sudo chown ubuntu:ubuntu src/vue/node_modules rust/target /home/ubuntu/.cargo/registry /home/ubuntu/.cargo/git && git config --global --add safe.directory \"${containerWorkspaceFolder}\" && npm --prefix src/vue ci && npm --prefix src/vue run build-wasm", "customizations": { "vscode": { - "extensions": [ - "rust-lang.rust-analyzer", - "Vue.volar", - "ms-dotnettools.csdevkit", - "ms-vscode.powershell", - "sumneko.lua" - ], + "extensions": ["rust-lang.rust-analyzer", "Vue.volar"], "settings": { // The Cargo workspace is in rust/, not at the repository root. "rust-analyzer.linkedProjects": ["rust/Cargo.toml"] diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 2a33e0df..00000000 --- a/.dockerignore +++ /dev/null @@ -1,9 +0,0 @@ -**/.* -**/bin -**/obj -**/node_modules - -# Keep the dotnet tool manifest (excluded by **/.* above) so the WebApp -# Dockerfile's PostBuild `dotnet tool restore` can install the swagger CLI. -!**/.config -!**/.config/** diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 993de3ab..00000000 --- a/.editorconfig +++ /dev/null @@ -1,89 +0,0 @@ - -[*.{cs,vb}] -#### Naming styles #### - -# Naming rules - -dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion -dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface -dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i - -dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.types_should_be_pascal_case.symbols = types -dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case - -dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members -dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case - -# Symbol specifications - -dotnet_naming_symbols.interface.applicable_kinds = interface -dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected -dotnet_naming_symbols.interface.required_modifiers = - -dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum -dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected -dotnet_naming_symbols.types.required_modifiers = - -dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method -dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected -dotnet_naming_symbols.non_field_members.required_modifiers = - -# Naming styles - -dotnet_naming_style.begins_with_i.required_prefix = I -dotnet_naming_style.begins_with_i.required_suffix = -dotnet_naming_style.begins_with_i.word_separator = -dotnet_naming_style.begins_with_i.capitalization = pascal_case - -dotnet_naming_style.pascal_case.required_prefix = -dotnet_naming_style.pascal_case.required_suffix = -dotnet_naming_style.pascal_case.word_separator = -dotnet_naming_style.pascal_case.capitalization = pascal_case - -dotnet_naming_style.pascal_case.required_prefix = -dotnet_naming_style.pascal_case.required_suffix = -dotnet_naming_style.pascal_case.word_separator = -dotnet_naming_style.pascal_case.capitalization = pascal_case -dotnet_style_operator_placement_when_wrapping = beginning_of_line -tab_width = 4 -indent_size = 4 -dotnet_style_coalesce_expression = true:suggestion -dotnet_style_null_propagation = true:suggestion -dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion -dotnet_style_prefer_auto_properties = true:silent -dotnet_style_object_initializer = true:suggestion -dotnet_style_collection_initializer = true:suggestion -dotnet_style_prefer_simplified_boolean_expressions = true:suggestion -dotnet_style_prefer_conditional_expression_over_assignment = true:silent -dotnet_style_prefer_conditional_expression_over_return = true:silent -dotnet_style_explicit_tuple_names = true:suggestion -dotnet_style_prefer_inferred_tuple_names = true:suggestion -dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion -dotnet_style_prefer_compound_assignment = true:suggestion -dotnet_style_prefer_simplified_interpolation = true:suggestion -dotnet_style_namespace_match_folder = true:suggestion - -[*.cs] -csharp_indent_labels = one_less_than_current -csharp_using_directive_placement = outside_namespace:silent -csharp_prefer_simple_using_statement = true:suggestion -csharp_prefer_braces = true:silent -csharp_style_namespace_declarations = block_scoped:silent -csharp_style_prefer_method_group_conversion = true:silent -csharp_style_prefer_top_level_statements = true:silent -csharp_style_expression_bodied_methods = false:silent -csharp_style_expression_bodied_constructors = false:silent -csharp_style_expression_bodied_operators = false:silent -csharp_style_expression_bodied_properties = true:silent -csharp_style_expression_bodied_indexers = true:silent -csharp_style_expression_bodied_accessors = true:silent -csharp_style_expression_bodied_lambdas = when_on_single_line:silent -csharp_style_expression_bodied_local_functions = when_on_single_line:silent -csharp_style_throw_expression = true:suggestion -csharp_prefer_braces = true:suggestion - -[*.{csproj,props,targets}] -indent_style = space -indent_size = 2 diff --git a/.gitattributes b/.gitattributes index fa5627f0..1c5aad8b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,13 +1,4 @@ -# src/lua is generated and CI checks it against a fresh transpile. The manifest -# is written with [Environment]::NewLine, so a Windows regen would otherwise -# produce CRLF and fail that check for no real reason. -*.lua text eol=lf - -# Same deal for swagger.json (Swashbuckle CLI), another committed generated file -# CI checks against a fresh regeneration. It is LF today; pinning it keeps a regen -# on a Windows checkout from failing that check on line endings alone. -src/WebApp/swagger.json text eol=lf - +# The snapshot tests compare these byte for byte, so pin LF on every checkout. *.verified.txt text eol=lf working-tree-encoding=UTF-8 *.verified.xml text eol=lf working-tree-encoding=UTF-8 *.verified.json text eol=lf working-tree-encoding=UTF-8 diff --git a/.github/renovate.json5 b/.github/renovate.json5 index ab3a3b9f..b73c8a20 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -1,16 +1,15 @@ // Dependency automation for this repo. Renovate, not Dependabot: this project has -// seven deliberate HOLDS whose reasoning lives in prose - in .csproj comments and -// CLAUDE.md - and Dependabot's `ignore` entries cannot express any of them. The +// deliberate HOLDS whose reasoning lives in prose - here and in CLAUDE.md - and +// Dependabot's `ignore` entries cannot express any of them. The // point of everything below is to stop a bot cheerfully re-proposing, every week, // the exact bump this project has already considered and rejected; and, where a // bump IS wanted, to make the reason travel attached to the proposal. // -// THIS FILE IS INERT UNTIL THE RENOVATE GITHUB APP IS INSTALLED. Adding it starts -// no automation by itself (see https://github.com/apps/renovate). As of this -// writing the app has never run here: no bot PRs, no bot commits, and no -// Dependency Dashboard issue despite `dependencyDashboard: true` below. +// THIS FILE IS INERT WITHOUT THE RENOVATE GITHUB APP. The file starts no automation +// by itself (see https://github.com/apps/renovate). The app is installed here now: +// it opens the bot PRs and keeps the dependency dashboard, issue #41. // -// The app is intended to be installed with "Automated PRs", "Require config file" +// The app is installed with "Automated PRs", "Require config file" // and "Create onboarding PRs". Two consequences worth knowing: // - "Require config file" means Renovate does NOTHING, SILENTLY, if this file is // absent or unparseable ON THE DEFAULT BRANCH. That is indistinguishable from @@ -33,7 +32,7 @@ // The org preset supplies what used to be spelled out here: config:recommended, // helpers:pinGitHubActionDigests, the America/Los_Angeles timezone, the - // "before 6am on monday" batch, automerge:false, prConcurrentLimit/prHourlyLimit, + // weekly Monday batch, automerge:false, prConcurrentLimit/prHourlyLimit, // dependencyDashboard, osvVulnerabilityAlerts, the rule that security fixes skip // the weekly window, and the github-actions grouping. // Source: https://github.com/FactoryGameFan/.github/blob/main/renovate-config.json @@ -44,9 +43,10 @@ // error - if PRs simply stop arriving, check that before anything else. // // WORTH KNOWING ABOUT THE ACTION SHA PINNING THE PRESET NOW SUPPLIES, because the - // knowledge is repo-specific even though the setting is not. All four actions here - // - actions/checkout, actions/setup-node, actions/setup-dotnet and - // cloudflare/wrangler-action - were converted from moving tags to SHAs by #61. + // knowledge is repo-specific even though the setting is not. The actions here - + // actions/checkout, actions/setup-node and cloudflare/wrangler-action, plus + // actions/setup-dotnet until the .NET code was removed - were converted from + // moving tags to SHAs by #61. // A digest update carries no release timestamp, so `minimumReleaseAge` below has // nothing to measure and `renovate/stability-days` sits PENDING rather than ever // going green. Waiting does not clear it. #61 was merged with that check @@ -60,7 +60,7 @@ // // This value is a JUDGEMENT CALL, not a constraint inherited from a package // manager. This repo is npm (src/vue/package-lock.json, lockfileVersion 3) plus - // NuGet; there is no pnpm anywhere, so none of pnpm 11's `minimum-release-age` + // Cargo (rust/Cargo.lock); there is no pnpm anywhere, so none of pnpm 11's `minimum-release-age` // cooldown behaviour - and its habit of writing a `minimumReleaseAgeExclude:` // bypass when asked to install something younger - is in play here. If this repo // ever moves to pnpm, revisit this line and the `vulnerabilityAlerts` one below @@ -78,24 +78,26 @@ // default in a way that passes a green suite while silently breaking a // byte-exactness invariant, and the resulting wrong belief survived about a year. // The same shape of failure is available here - the planner's correctness lives - // in Verify snapshots, and a dependency that changed planner output subtly would - // show up as a snapshot diff to be accepted rather than as an obvious failure. + // in the committed snapshots under testdata/oil-field, and a dependency that + // changed planner output subtly would show up as a snapshot diff to be accepted + // rather than as an obvious failure. // // IF THIS IS EVER REVISITED, automerge has two prerequisites that are easy to // miss and neither is satisfied today: - // 1. Branch protection on `main` with a CI check REQUIRED, `bypass_actors` - // empty, and `strict_required_status_checks_policy: true`. Without all - // three a PR can merge red, or merge having been tested against a different - // `main` than the one it lands on. Note this repo's CI is FOUR separate - // checks ("Build front-end", "Transpile to Lua", "Test .NET with - // Lua-compatible settings", "Test .NET with default settings"), so all four - // have to be required, not just one. + // 1. A rule on `main` with CI checks REQUIRED, `bypass_actors` empty, and + // `strict_required_status_checks_policy: true`. Without all three a PR can + // merge red, or merge having been tested against a different `main` than + // the one it lands on. Today nothing is required: the only ruleset, + // ProtectMain, blocks deletion and force pushes and has no status-check + // rule (checked 2026-10-21). This repo's CI is TWO separate checks from + // ci.yml ("Build front-end" and "Rust port"), so both have to be required, + // not just one. // - // There is also a fifth, "Build and deploy" from deploy-cloudflare.yml, - // which runs on pull requests behind a `paths` filter. Do NOT make that one - // required: a paths-filtered check does not report at all on a PR it does - // not match, which leaves a required check permanently pending and blocks - // the merge outright. + // There is also "Build and deploy" from deploy-cloudflare.yml, and + // "smoke" from devcontainer.yml. Both run on pull requests behind a + // `paths` filter. Do NOT make either one required: a paths-filtered check + // does not report at all on a PR it does not match, which leaves a + // required check permanently pending and blocks the merge outright. // 2. Repo-level "Allow auto-merge" enabled (`allow_auto_merge` in the repo // settings API). Renovate's `platformAutomerge` cannot do anything without // it, and when it is off Renovate silently falls back to polling and merging @@ -155,13 +157,6 @@ ], }, - // The submodules (FluteSharp, delaunator-sharp, CSharp.lua) are pinned to specific - // commits and are upstream repos, not ours to bump. Renovate disables this manager - // by default; stated explicitly so nobody "fixes" it later. - "git-submodules": { - enabled: false, - }, - packageRules: [ // --------------------------------------------------------------------------- // Grouping @@ -191,7 +186,7 @@ }, // --------------------------------------------------------------------------- - // Documented holds. Each mirrors a comment in the code; keep the two in sync. + // Documented holds. Each carries its reason and when to revisit it. // `allowedVersions` is used rather than `enabled: false` so patches within the // held line still flow - the hold is on the major, not on maintenance. // @@ -205,23 +200,18 @@ // Migration-shaped - there is real work that would lift the hold, so the // plan lives in an issue and the rule points at it: // typescript ............... #49 (upstream-gated: vue-tsc vs TS 7) - // WebApp OpenAPI stack ..... #47 (OpenApi 10.x + Swashbuckle 10.x) - // xunit-dotnet monorepo .... #111 (migrate to the new `dotnet test`) - // Verify 33 ................ #111 (Verify.XunitV3 33 needs xunit.v3 4) // - // The Verify family was listed here against #44 until the move to xunit v3 - // + Verify.XunitV3 landed. It is held again since 2026-10-20, for a - // different reason: every 33.x release requires xunit.v3 4.x (#155). + // The NuGet holds that used to be listed here went with the .NET code in + // stage 13b of the Rust port (#138). // // Conditional - nothing to plan until an external circumstance changes, so // there is deliberately NO issue and the revisit condition is stated in the // rule itself. Filing issues for these would just create things to close: // @types/node .............. tracks CI's Node, not the registry - // ApplicationInsights ...... only matters if WebApp is hosted again // // The live inventory of everything held back is dependency dashboard #41, // which is a better place to look than reading manifests by hand - it is how - // the missing swashbuckle.aspnetcore.cli hold was found (#43). + // a missing hold was found once before (#43). // --------------------------------------------------------------------------- { // Tracking issue: #49. Upstream-gated - nothing here unblocks it. Expected to @@ -246,183 +236,25 @@ ], }, { - // Tracking issue: #47 - the OpenApi 10.x + Swashbuckle 10.x migration that - // lifts this whole group. Read it before touching any ceiling here; the two - // halves have different ones and the reasons are not interchangeable. - // - // THE CLI IS PART OF THIS STACK AND WAS MISSING FROM THE FIRST VERSION OF - // THIS RULE. `swashbuckle.aspnetcore.cli` lives in - // src/WebApp/.config/dotnet-tools.json (currently 6.9.0), not in any .csproj, - // so reading the project files alone does not reveal it - and because it is a - // separate package name, matching "Swashbuckle.AspNetCore" never covered it. - // Renovate promptly queued a jump straight to v10 for it. That CLI is what - // WebApp.csproj's PostBuild target runs to generate the committed - // swagger.json, which in turn generates the Vue client, so a v10 generator - // against a 6.x library is exactly the piecemeal move the csproj comment - // forbids. It is held here with the rest of the stack. + // The only Dockerfile is .devcontainer/Dockerfile (src/WebApp/Dockerfile + // went with the .NET code). Its FROM line is an Ubuntu 24.04 image pinned + // by digest, so Renovate re-pins the digest as the image picks up OS + // security fixes. Not narrowed with matchFileNames: there is exactly one + // such file, and a path guess that stops matching after a move would fail + // silently. // - // CEILING IS <7.0.0, not the <9.0.0 originally guessed. Installed today are - // Swashbuckle.AspNetCore 6.9.0 and swashbuckle.aspnetcore.cli 6.9.0, and - // nothing in the csproj comment establishes that 7.x or 8.x is safe against - // Microsoft.AspNetCore.OpenApi 8.0.x - it reasons only about 6.x (current) - // and 10.x (the compatible target). <9.0.0 would have waved through a - // Swashbuckle v8 major on that unexamined basis. Patches within 6.x still - // flow; the hold is on the major. - description: - "HOLD the WebApp OpenAPI stack at its current majors, and keep all of it in one PR - these move together or not at all. Microsoft.AspNetCore.OpenApi 9.0+ pulls Microsoft.OpenApi 2.x, whose schema model Swashbuckle 6.x cannot consume; Swashbuckle 10.x adopts 2.x as well, so OpenApi 10.x + Swashbuckle 10.x + CLI 10.x is the compatible set. Getting there is a real migration (rewrite of OilFieldPlanRequestDefaultsSchemaFilter.cs, edits to RequireNonNullablePropertiesSchemaFilter.cs, regenerated swagger.json and Vue client), deferred because WebApp is not deployed. See the comment in src/WebApp/WebApp.csproj.", - matchManagers: ["nuget"], - matchPackageNames: [ - "Microsoft.AspNetCore.OpenApi", - "Swashbuckle.AspNetCore", - "swashbuckle.aspnetcore.cli", - ], - groupName: "WebApp OpenAPI stack", - allowedVersions: "<9.0.0", - }, - { - // Split out because the two halves of the stack have different ceilings: - // Microsoft.AspNetCore.OpenApi is held below 9.0.0 (that is where the - // Microsoft.OpenApi 2.x break lands), while the Swashbuckle packages are held - // below 7.0.0 for the reason above. `allowedVersions` is last-match-wins, so - // this narrows the two Swashbuckle entries without disturbing the grouping or - // the OpenApi ceiling set by the rule above. + // Majors used to be skipped here, because they only reached an undeployed + // WebApp and an SDK image that moved with global.json. Neither is true now. + // A major here is the next Ubuntu LTS, and it comes through as its own PR + // like any other update, with the note below. Nothing automerges, so it + // still waits for a read. description: - "HOLD both Swashbuckle packages on 6.x specifically. Installed: Swashbuckle.AspNetCore 6.9.0, swashbuckle.aspnetcore.cli 6.9.0 - the two were out of step until #63 brought the CLI up to match, which is what this rule existing at all is meant to keep true. The csproj rationale covers 6.x and 10.x only - 7.x/8.x are simply unexamined against Microsoft.AspNetCore.OpenApi 8.0.x, so they are not a safe default. Raise this only as part of the OpenApi 10.x migration, which moves the whole stack at once - tracked in #47.", - matchManagers: ["nuget"], - matchPackageNames: ["Swashbuckle.AspNetCore", "swashbuckle.aspnetcore.cli"], - groupName: "WebApp OpenAPI stack", - allowedVersions: "<7.0.0", - }, - { - // The version ceiling that used to sit here came off with #44: the test - // project moved to xunit v3 + Verify.XunitV3, so Verify.DiffPlex is no longer - // pinned to 3.1.x and the family tracks current again. - // - // The grouping stays, because the coupling that made the old hold necessary - // is structural, not version-specific. Verify.DiffPlex and Verify.XunitV3 - // both bind to Verify core, so bumping one alone can pull core past what the - // other supports. That mismatch is the dangerous kind - it compiles with zero - // warnings and dies at runtime. On the old stack it took out 2748 of 4261 - // tests with MethodAccessException on Guards.AgainstBadSourceFile. - description: - "Keep the whole Verify family in one PR. Verify.DiffPlex and Verify.XunitV3 both bind to the Verify core package, so bumping one alone can pull core past what the other supports - which builds cleanly and then fails at runtime with MethodAccessException on Guards.AgainstBadSourceFile. These move together, and 33.x is held by the rule below. See the comment in test/FactorioTools.Test/FactorioTools.Test.csproj.", - matchManagers: ["nuget"], - matchPackageNames: ["Verify.XunitV3", "Verify.DiffPlex", "Verify"], - groupName: "Verify test snapshot stack", - prBodyNotes: [ - "This family's failure mode BUILDS CLEANLY and dies at runtime, so a compiling branch is not evidence. Run the full `dotnet test` before believing any bump here.", - ], - }, - { - // Tracking issue: #111. Every Verify.XunitV3 33.x release depends on - // xunit.v3.extensibility.core 4.x, which the xunit rule below holds back. - // Renovate proposed 33.3.4 in #155. The build failed with SponsorCheck - // SC021 first, and with that answered, the test binary died at startup: - // - // System.TypeLoadException: Method 'get_AssemblyUniqueID' in type - // 'Xunit.Runner.Common.ErrorMessage' from assembly - // 'xunit.v3.runner.common, Version=3.2.2.0' does not have an - // implementation. - // - // Zero tests ran. Verify.DiffPlex 3.4.0 needs Verify core 33.2.0, so it is - // held at the same line (#137 was closed for that). - // - // Verify 33 also needs a license property, or the build fails with SC021. - // When #111 lands, add the OpenSource exemption to the test project with an - // end month at most 12 months out (2026-10-20 check: it builds with - // SC031 "Exemption claimed"): - // OpenSource - // yyyy-MM - description: - "HOLD Verify and Verify.XunitV3 below 33, and Verify.DiffPlex below 3.4.0. Verify.XunitV3 33.x requires xunit.v3 4.x, which is held for the dotnet test migration in #111. Lift this as part of #111, together with the Verify SponsorCheck exemption described in the comment here.", - matchManagers: ["nuget"], - matchPackageNames: ["Verify.XunitV3", "Verify"], - allowedVersions: "<33.0.0", - }, - { - // Same hold as the rule above, at the DiffPlex release that moves to core 33. - description: - "HOLD Verify.DiffPlex below 3.4.0, which needs Verify core 33. See the Verify 33 rule above and #111.", - matchManagers: ["nuget"], - matchPackageNames: ["Verify.DiffPlex"], - allowedVersions: "<3.4.0", - }, - { - // Tracking issue: #111. Migration-shaped, and unlike the Verify rule above - // this one fails LOUDLY at build time, which is the easier kind. Renovate - // proposed v4 in #108 and both .NET test jobs failed identically on CI and - // on a local Mac: - // - // Microsoft.Testing.Platform.MSBuild.targets(320,5): error : Testing with - // VSTest target is no longer supported by Microsoft.Testing.Platform on - // .NET 10 SDK and later. If you use dotnet test, you should opt-in to the - // new dotnet test experience. - // - // The chain is xunit.v3 4.0.0 -> xunit.v3.mtp-v2 [4.0.0, 4.0.0] -> - // Microsoft.Testing.Platform 2.x, which drops the VSTest bridge on the .NET - // 10 SDK. So this is not a bump, it is a test-host migration: the project - // has to opt into the new `dotnet test`, and ci.yml's two .NET jobs both - // pass `--logger "console;verbosity=normal"`, which is VSTest syntax that - // MTP does not accept. #111 has the full checklist. - // - // Ceiling is <4.0.0 rather than `enabled: false` so patches inside 3.x keep - // flowing while the migration waits. Raise it as part of #111, not before. - description: - "HOLD the xunit-dotnet monorepo on 3.x. xunit.v3 4.0.0 pulls xunit.v3.mtp-v2 and Microsoft.Testing.Platform 2.x, which no longer supports the VSTest target on the .NET 10 SDK, so `dotnet test` fails at build time. Lifting this means migrating the test project to the new dotnet test experience and replacing the VSTest-only arguments in ci.yml - tracked in #111. Patches within 3.x still flow; the hold is on the major.", - matchManagers: ["nuget"], - matchPackageNames: ["xunit.v3", "xunit.runner.visualstudio"], - groupName: "xunit-dotnet monorepo", - allowedVersions: "<4.0.0", - }, - { - // No tracking issue by design - this is a "not worth the review" call rather - // than a deferred upgrade, and it reverses on its own if WebApp is hosted - // again. The Dockerfile rule below is the same call for the same reason; - // keep the two consistent. - description: - "SKIP major bumps of Microsoft.ApplicationInsights.AspNetCore. WebApp is no longer deployed, so telemetry majors are not worth the review. Patches still flow. Re-enable if the API is ever hosted again.", - matchManagers: ["nuget"], - matchPackageNames: ["Microsoft.ApplicationInsights.AspNetCore"], - matchUpdateTypes: ["major"], - enabled: false, - }, - { - // Resolves #45. Same reasoning as the ApplicationInsights rule above, applied - // to the only Dockerfile in the repo (src/WebApp/Dockerfile - confirmed from - // the Renovate job log: "Matched 1 file(s) for manager dockerfile"). Not - // narrowed with matchFileNames, because there is exactly one such file and a - // path guess that stops matching after a move would fail silently. - // - // Majors off, patches ON, which is the deliberate half of this. A .NET major - // on an undeployed service is not worth a review, but base images accumulate - // OS-level CVEs, and `10.0` / `10.0-alpine` are FLOATING minor tags - the - // digest behind them moves on its own regardless of what Renovate proposes. - // Keeping patches live is what lets a rebuild pick those up. - // - // WORTH KNOWING: global.json now pins the SDK to 10.0.302 while this - // Dockerfile tracks whatever `10.0` currently resolves to. Those two can - // drift apart silently, and Renovate will not report it because a floating - // tag is never "out of date". If the container build ever behaves differently - // from CI, check that first. - description: - "SKIP major bumps of the src/WebApp/Dockerfile base images (mcr.microsoft.com/dotnet/sdk, runtime-deps). WebApp is no longer deployed, so a .NET major here is not worth the review - same call as Microsoft.ApplicationInsights.AspNetCore above. Patch/minor updates still flow, because base images carry OS-level security fixes even when the service is unhosted. Re-enable if the API is ever hosted again.", + "Devcontainer base image majors (the next Ubuntu LTS) come as their own PR. The Devcontainer CI job rebuilds the image and runs the Rust tests and the site build inside it.", matchManagers: ["dockerfile"], matchUpdateTypes: ["major"], - enabled: false, - }, - { - // .devcontainer/Dockerfile pins the exact SDK from global.json by digest, and - // the Devcontainer CI job fails when the two disagree. Renovate saw both - // updates for 10.0.402 but put only global.json in the dotnet monorepo PR - // (#148), so the smoke job went red until the FROM line was re-pinned by - // hand. Sharing the group name puts both in the same PR. The rule above - // predates this Dockerfile, so it now covers this image's majors too. - description: - "Move the devcontainer SDK image with global.json, in the dotnet monorepo PR. The Devcontainer CI job fails when they disagree.", - matchManagers: ["dockerfile"], - matchFileNames: [".devcontainer/Dockerfile"], - matchPackageNames: ["mcr.microsoft.com/dotnet/sdk"], - groupName: "dotnet monorepo", + prBodyNotes: [ + "This moves the dev container to a new Ubuntu release. Check that the Devcontainer CI job (`smoke`) ran and passed, and that the Node tarball and rustup steps in .devcontainer/Dockerfile still work on it.", + ], }, ], } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c14bc4c1..e9f07736 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,6 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: - submodules: recursive fetch-depth: 0 - name: Set up Node @@ -59,256 +58,6 @@ jobs: run: npm run test working-directory: ${{ env.BUILD_PATH }} - # Transpiles the core library with CSharp.lua, the way src/lua is actually - # produced. This is the job that catches a C# construct Lua cannot take - - # test-lua-settings below only flips MSBuild #if flags and never runs the - # transpiler. LINQ is the case worth knowing: CSharp.lua transpiles it - # happily, but Collections.Linq is not in the CoreSystem load list, so the - # emitted Lua would call a module that was never shipped and fail at runtime - # inside Factorio rather than at build time. - transpile-lua: - name: Transpile to Lua - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - submodules: recursive - - - name: Setup .NET - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6 - with: - global-json-file: global.json - - # No "dotnet workload restore" here: the script builds only FactorioTools - # and the two submodule libraries, so the wasm-tools workload is not needed. - - name: Transpile to Lua - run: pwsh -File src/lua/Invoke-LuaBuild.ps1 - - # src/lua is generated output that happens to be committed, so it can go - # stale without anything failing - it already had, for the pole wire reach. - - name: Check the committed Lua is up to date - run: | - if [ -n "$(git status --porcelain -- src/lua)" ]; then - echo "::error::src/lua does not match what the C# transpiles to. Run src/lua/Invoke-LuaBuild.ps1 and commit the result." - git status --porcelain -- src/lua - git diff -- src/lua - exit 1 - fi - echo "src/lua matches the transpiled output." - - # Factorio runs a modified Lua 5.2, and this package is Lua 5.2.4 - the - # same version the README's Lua performance log was measured against. - - name: Install Lua 5.2 - run: sudo apt-get update && sudo apt-get install -y lua5.2 - - - name: Syntax-check the generated Lua - run: find src/lua -name '*.lua' -print0 | xargs -0 -n1 luac5.2 -p - - # The step that catches LINQ, and the reason a syntax check is not enough. - # LINQ transpiles cleanly and parses cleanly; it emits - # "local Linq = System.Linq.Enumerable", which is nil because - # Collections.Linq is not in the CoreSystem load list. That only shows up - # when the module is loaded, so the check has to actually run the planner: - # ./FactorioTools/InitializeContext.lua:3: attempt to index field 'Linq' - # Takes well under a second, and proves the transpiled planner runs at all - # rather than merely parsing. - - name: Run the planner under Lua 5.2 - run: lua5.2 sample.lua - working-directory: src/lua - - test-lua-settings: - name: Test .NET with Lua-compatible settings - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - submodules: recursive - - - name: Setup .NET - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6 - with: - global-json-file: global.json - - - name: Restore workloads - run: dotnet workload restore - - - name: Restore dependencies - run: dotnet restore - - - name: Build debug, with Lua settings - run: dotnet build --configuration Debug /p:UseLuaSettings=true - - - name: Build release, with Lua settings - run: dotnet build --configuration Release /p:UseLuaSettings=true - - - name: Test with Lua settings - run: dotnet test --no-build --configuration Release --logger "console;verbosity=normal" - - - name: Check no Verify snapshots drifted - run: | - if [ -n "$(git status --porcelain -- '*.verified.*')" ]; then - echo "::error::A Verify snapshot changed during the test run. The committed snapshot does not match current behavior - regenerate locally and commit, or fix the regression." - git status --porcelain -- '*.verified.*' - git diff -- '*.verified.*' - exit 1 - fi - echo "No Verify snapshots drifted." - - # The browser bundle ships with LocationAsStruct=false (see build-wasm in - # src/vue/package.json and issue #139), so that combination must be tested on - # its own, not only built. UseLuaSettings also turns it off, but changes five - # other flags with it. - test-location-as-class: - name: Test .NET with Location as a class (the shipped WASM setting) - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - submodules: recursive - - - name: Setup .NET - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6 - with: - global-json-file: global.json - - - name: Restore workloads - run: dotnet workload restore - - - name: Restore dependencies - run: dotnet restore - - - name: Build release, Location as a class - run: dotnet build --configuration Release /p:LocationAsStruct=false - - - name: Test with Location as a class - run: dotnet test --no-build --configuration Release --logger "console;verbosity=normal" - - - name: Check no Verify snapshots drifted - run: | - if [ -n "$(git status --porcelain -- '*.verified.*')" ]; then - echo "::error::A Verify snapshot changed during the test run. The committed snapshot does not match current behavior - regenerate locally and commit, or fix the regression." - git status --porcelain -- '*.verified.*' - git diff -- '*.verified.*' - exit 1 - fi - echo "No Verify snapshots drifted." - - test-default: - name: Test .NET with default settings - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - submodules: recursive - - - name: Setup .NET - uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6 - with: - global-json-file: global.json - - - name: Restore workloads - run: dotnet workload restore - - - name: Restore dependencies - run: dotnet restore - - - name: Build debug, no hash sets - run: dotnet build --configuration Debug /p:UseHashSets=false - - - name: Build debug, no bit array - run: dotnet build --configuration Debug /p:UseBitArray=false - - - name: Build debug, location as class - run: dotnet build --configuration Debug /p:LocationAsStruct=false - - - name: Build debug, no shared instances - run: dotnet build --configuration Debug /p:UseSharedInstances=false - - - name: Build debug, no vectors - run: dotnet build --configuration Debug /p:UseVectors=false - - - name: Build debug, no stackalloc - run: dotnet build --configuration Debug /p:UseStackalloc=false - - - name: Build debug, rent neighbors - run: dotnet build --configuration Debug /p:RentNeighbors=true - - - name: Build debug, allow dynamic FLUTE degree - run: dotnet build --configuration Debug /p:AllowDynamicFluteDegree=true - - - name: Build debug, no grid ToString - run: dotnet build --configuration Debug /p:EnableGridToString=false - - - name: Build release with default settings - run: dotnet build --configuration Release - - # swagger.json is committed generated output: WebApp.csproj's PostBuild - # target reruns the Swashbuckle CLI on every build, including every build - # above, so by now CI has already regenerated it several times. Nothing - # compared it to the committed copy, so a contract change could sit stale - # indefinitely while CI stayed green. Note the invocation lives in MSBuild, - # not in this file - grepping the workflows for "swagger" finds nothing, - # which makes this easy to misdiagnose as "the tool never runs in CI". - - name: Check the committed swagger.json is up to date - run: | - if [ -n "$(git status --porcelain -- src/WebApp/swagger.json)" ]; then - echo "::error::src/WebApp/swagger.json does not match what the WebApp generates. Run 'dotnet build src/WebApp/WebApp.csproj' and commit the result (then 'npm run swagger-gen' in src/vue to refresh the client)." - git diff -- src/WebApp/swagger.json - exit 1 - fi - echo "src/WebApp/swagger.json matches the generated document." - - - name: Test with default settings - run: dotnet test --no-build --configuration Release --logger "console;verbosity=normal" - - # Backstop, not the primary guard. The primary guard works: SetVerifySettings - # calls AutoVerify(includeBuildServer: false), DiffEngine correctly detects this - # runner as a build server, and a stale snapshot fails the test above with - # VerifyException - confirmed by committing a corrupted snapshot and watching - # CI go red at "Test with Lua settings". - # - # This step exists because that guard is only as good as DiffEngine's detection, - # which is version-dependent and has changed before. If it ever regresses, a - # rewritten snapshot leaves the file dirty and this catches it. Costs nothing. - # - # Worth knowing if you ever debug this: setting CI=1 / GITHUB_ACTIONS=true - # locally does NOT reproduce the detection, so a local run auto-accepts and - # looks like the guard is broken. It is not - test it on a real runner. - - name: Check no Verify snapshots drifted - run: | - if [ -n "$(git status --porcelain -- '*.verified.*')" ]; then - echo "::error::A Verify snapshot changed during the test run. The committed snapshot does not match current behavior - regenerate locally and commit, or fix the regression." - git status --porcelain -- '*.verified.*' - git diff -- '*.verified.*' - exit 1 - fi - echo "No Verify snapshots drifted." - - # The Rust port (#138) is checked against fixtures recorded from .NET: the - # BCL's tie-breaking (dotnet-compat), DelaunatorSharp, FluteSharp and the C# - # planner itself. They are committed so the Rust job needs no .NET. This - # reruns every recorder on the pinned SDK and fails if any committed fixture - # changes - an SDK bump that changes .NET's behavior, or a C# planner change - # that has not been mirrored in the Rust port (see CLAUDE.md, "Rust port"). - - name: Check the Rust port's fixtures still match .NET - run: | - for crate in dotnet-compat delaunator-net flute planner contract; do - dotnet run --project "rust/$crate/oracle" --configuration Release -- "rust/$crate/fixtures" - done - if [ -n "$(git status --porcelain -- 'rust/*/fixtures/*')" ]; then - echo "::error::Recorded fixtures under rust/ no longer match what .NET produces. Rerun the recorder named in that crate's FIXTURES.md, commit the result, and make the Rust tests pass against it." - git status --porcelain -- 'rust/*/fixtures/*' - exit 1 - fi - echo "All Rust port fixtures match .NET." - - - name: Publish - run: dotnet publish src/WebApp --configuration Release --output src/WebApp/bin/publish --runtime linux-x64 --no-self-contained /p:PublishReadyToRun=true - rust: name: Rust port runs-on: ubuntu-latest @@ -338,12 +87,13 @@ jobs: - name: Build for wasm32 run: cargo build --workspace --target wasm32-unknown-unknown - # The port's end-to-end gate: the Rust planner must reproduce every committed - # Verify snapshot (2,747 planner files) and the Score scoreboard byte for byte, - # with no .NET involved. The contract replay (rust/contract) runs here too: the - # JSON the site sends and gets back, against responses recorded from the C#. - # In release this takes about a minute; both are #[ignore]d in debug because - # they are slow there. + # The end-to-end gate: the Rust planner must reproduce every committed + # snapshot under testdata/oil-field (2,747 planner files) and the Score + # scoreboard byte for byte. The contract replay (rust/contract) runs here too: + # the JSON the site sends and gets back, against the recorded responses. Both + # sets are frozen inputs, recorded from the C# planner at commit 0532d923 + # before it was removed. In release this takes about a minute; both are + # #[ignore]d in debug because they are slow there. - name: Check every planner snapshot (release) run: cargo test --release -p snapshots -p contract -- --include-ignored diff --git a/.github/workflows/deploy-cloudflare.yml b/.github/workflows/deploy-cloudflare.yml index 699d7f3d..37346103 100644 --- a/.github/workflows/deploy-cloudflare.yml +++ b/.github/workflows/deploy-cloudflare.yml @@ -36,7 +36,6 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: - submodules: recursive fetch-depth: 0 - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 @@ -63,8 +62,10 @@ jobs: run: npm run build working-directory: ${{ env.BUILD_PATH }} # Guard against shipping a site that cannot plan: the planner's .wasm and its - # worker must be in dist/assets with a content hash, and nothing may still - # point at the old .NET bundle under /framework/. + # worker must be in dist/assets with a content hash. The second half checks + # nothing still ships or points at the old .NET bundle under /framework/. That + # bundle and its src/vue/public/framework/ ignore rule are gone, so a stale + # local copy left in public/ would now show up as a file to commit. - name: Check the planner landed in the bundle run: | dist="$BUILD_PATH/dist" diff --git a/.github/workflows/devcontainer.yml b/.github/workflows/devcontainer.yml index d600ee2a..63f4caf4 100644 --- a/.github/workflows/devcontainer.yml +++ b/.github/workflows/devcontainer.yml @@ -5,7 +5,6 @@ on: paths: - '.devcontainer/**' - '.github/workflows/devcontainer.yml' - - 'global.json' # Not all of rust/: ci.yml's Rust job already builds and tests every Rust # change. What this job adds is the container, and the only Rust file the # container has to agree with is the toolchain pin. @@ -14,9 +13,8 @@ on: # .devcontainer/ changing, so the paths filter above cannot catch them: # - the Node tarball and the rustup-init binaries, and their checksums, if # a release is ever re-cut - # - the SDK digest and RUST_VERSION, once global.json or - # rust/rust-toolchain.toml moves (both ARE covered by the paths filter - # now, but only if the bump goes through a pull request) + # - RUST_VERSION, once rust/rust-toolchain.toml moves (it IS covered by the + # paths filter, but only if the bump goes through a pull request) # A rotted pin fails the build for everyone who opens the container, so it is # better found here. schedule: @@ -33,7 +31,6 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: - submodules: recursive # vite.config.ts stamps the build with `git describe`, so the Vue # build needs tags and real history, not a shallow checkout. fetch-depth: 0 @@ -41,39 +38,22 @@ jobs: - name: Create the development container run: npx --yes @devcontainers/cli@0.89.0 up --workspace-folder "$GITHUB_WORKSPACE" - - name: Every toolchain resolves, and the SDK and Rust match their pins + - name: Every toolchain resolves, and Rust matches its pin run: | npx --yes @devcontainers/cli@0.89.0 exec --workspace-folder "$GITHUB_WORKSPACE" bash -ec ' - # The check that matters: global.json pins an exact SDK and - # rollForward only rolls forward, so a Dockerfile left behind after - # a global.json bump fails right here rather than confusing someone - # later. Compare what dotnet resolves against what the file asks for. - pinned=$(python3 -c "import json;print(json.load(open(\"global.json\"))[\"sdk\"][\"version\"])") - actual=$(dotnet --version) - echo "global.json pins $pinned, container resolves $actual" - [ "$pinned" = "$actual" ] || { - echo "::error::The devcontainer SDK ($actual) does not match global.json ($pinned). Re-pin .devcontainer/Dockerfile." - exit 1 - } node --version npm --version - pwsh --version python3 --version - # CI installs the apt package "lua5.2" and calls these two names; - # the container has to agree, or tools/check-lua.sh and the - # transpile-lua job diverge. - luac5.2 -v - lua5.2 -v - dotnet workload list | grep -q wasm-tools || { - echo "::error::wasm-tools workload missing - BrowserWasm cannot publish." - exit 1 - } + git --version + # cargo links build scripts, proc macros and the test binaries + # with cc. + cc --version | head -n 1 - # The same idea for Rust, with one difference. A stale RUST_VERSION - # in the Dockerfile does not fail anything: rustup reads - # rust/rust-toolchain.toml and downloads the pin on first use. So - # checking what rustc resolves to proves nothing on its own; compare - # what the image pre-installed against what the file asks for. + # A stale RUST_VERSION in the Dockerfile does not fail anything: + # rustup reads rust/rust-toolchain.toml and downloads the pin on + # first use. So checking what rustc resolves to proves nothing on + # its own; compare what the image pre-installed against what the + # file asks for. rust_pinned=$(python3 -c "import tomllib;print(tomllib.load(open(\"rust/rust-toolchain.toml\",\"rb\"))[\"toolchain\"][\"channel\"])") echo "rust-toolchain.toml pins $rust_pinned, the image pre-installs $RUST_VERSION" [ "$rust_pinned" = "$RUST_VERSION" ] || { @@ -104,25 +84,6 @@ jobs: } ' - - name: The core dev loop runs - run: | - npx --yes @devcontainers/cli@0.89.0 exec --workspace-folder "$GITHUB_WORKSPACE" \ - dotnet test test/FactorioTools.Test/FactorioTools.Test.csproj -c Release - - - name: The Lua transpile reproduces the committed output - run: | - npx --yes @devcontainers/cli@0.89.0 exec --workspace-folder "$GITHUB_WORKSPACE" bash -ec ' - pwsh -File src/lua/Invoke-LuaBuild.ps1 - if [ -n "$(git status --porcelain -- src/lua)" ]; then - echo "::error::src/lua drifted when transpiled inside the container." - git status --porcelain -- src/lua - exit 1 - fi - find src/lua -name "*.lua" -print0 | xargs -0 -n1 luac5.2 -p - cd src/lua && lua5.2 sample.lua > /dev/null - echo "Transpile reproduced the committed output, and it parses and runs under 5.2." - ' - # Debug only. The release gates (every planner snapshot, the contract # replay) stay in ci.yml's Rust job: they test the planner, not the # container, and would add their release build to this job's time. diff --git a/.gitignore b/.gitignore index db6d581d..859c3c19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,416 +1,29 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore - -# User-specific files -*.rsuser -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Mono auto generated files -mono_crash.* - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -[Ww][Ii][Nn]32/ -[Aa][Rr][Mm]/ -[Aa][Rr][Mm]64/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ -[Ll]ogs/ - -# Visual Studio 2015/2017 cache/options directory +# Editor and OS files .vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUnit -*.VisualState.xml -TestResult.xml -nunit-*.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ - -# ASP.NET Scaffolding -ScaffoldingReadMe.txt - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*_wpftmp.csproj -*.log -*.tlog -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Coverlet is a free, cross platform Code Coverage Tool -coverage*.json -coverage*.xml -coverage*.info - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# NuGet Symbol Packages -*.snupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx -*.appxbundle -*.appxupload - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!?*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser -*- [Bb]ackup.rdl -*- [Bb]ackup ([0-9]).rdl -*- [Bb]ackup ([0-9][0-9]).rdl - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio 6 auto-generated project file (contains which files were open etc.) -*.vbp - -# Visual Studio 6 workspace and project file (working project files containing files to include in project) -*.dsw -*.dsp - -# Visual Studio 6 technical files -*.ncb -*.aps - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# CodeRush personal settings -.cr/personal - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -# Local History for Visual Studio -.localhistory/ - -# Visual Studio History (VSHistory) files -.vshistory/ - -# BeatPulse healthcheck temp database -healthchecksdb - -# Backup folder for Package Reference Convert tool in Visual Studio 2017 -MigrationBackup/ - -# Ionide (cross platform F# VS Code tools) working folder -.ionide/ - -# Fody - auto-generated XML schema -FodyWeavers.xsd - -# VS Code files for those working on multiple tools -.vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json +.idea/ *.code-workspace - -# Local History for Visual Studio Code .history/ +.DS_Store -# Windows Installer files from build outputs -*.cab -*.msi -*.msix -*.msm -*.msp - -# JetBrains Rider -*.sln.iml - -# Custom +# Snapshot files a failing test leaves beside its *.verified.txt *.received.* -.DS_Store -/Factorio API DocsVersion 2.0.76 files -# Generated WASM assets copied into the Vue public dir for local dev / build -# (the bundle, incl. dotnet.js, ships inside public/framework) -src/vue/public/framework/ +# Local Factorio downloads (the API docs and the pump mod) kept in the checkout +/Factorio API DocsVersion 2.0.76 files pump_2.2.0.zip /pump_2.2.0 -# Wrangler local dev state (Cloudflare Pages/Workers CLI cache, incl. miniflare) +# Wrangler local dev state (Cloudflare Workers CLI cache, incl. miniflare) .wrangler/ # Subagent-driven-development scratch: per-plan ledgers, task briefs, review # packages. Working state for an in-flight plan, not a record - git history is. .superpowers/ + +# Local secrets: Wrangler's .dev.vars, env files, and the certificate and +# publish-profile files the old Visual Studio template ignored +.dev.vars* +.env +.env.* +*.pfx +*.publishsettings diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4e344f85..00000000 --- a/.gitmodules +++ /dev/null @@ -1,12 +0,0 @@ -[submodule "submodules/FluteSharp"] - path = submodules/FluteSharp - url = https://github.com/joelverhagen/FluteSharp.git - branch = main -[submodule "submodules/delaunator-sharp"] - path = submodules/delaunator-sharp - url = https://github.com/joelverhagen/delaunator-sharp.git - branch = lua-compat -[submodule "submodules/CSharp.lua"] - path = submodules/CSharp.lua - url = https://github.com/joelverhagen/CSharp.lua.git - branch = lua-compat diff --git a/.vscode/settings.json b/.vscode/settings.json index 696489d2..ed19a922 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "yaml.schemas": { - "https://json.schemastore.org/github-workflow.json": ".github/workflows/deploy.yml" + "https://json.schemastore.org/github-workflow.json": ".github/workflows/*.yml" } } \ No newline at end of file diff --git a/CLAUDE.md b/CLAUDE.md index 87674c63..0b4fb2a8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,77 +4,58 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## What this is -FactorioTools is a Factorio oil-field (outpost) blueprint planner. Given a blueprint with pumpjacks, it outputs a new blueprint wiring them with pipes, beacons, and electric poles, choosing pumpjack orientations and running several competing planning algorithms to return the best result. Core planning logic is in `src/FactorioTools/OilField`. +FactorioTools is a Factorio oil-field (outpost) blueprint planner. Given a blueprint with pumpjacks, it outputs a new blueprint wiring them with pipes, beacons, and electric poles, choosing pumpjack orientations and running several competing planning algorithms to return the best result. -## Prerequisites - -- **.NET SDK 10.0.402** (pinned in `global.json`, `rollForward: latestMajor`). This is a floor, not an exact pin - `latestMajor` means any newer 10.x SDK works, but an *older* one now fails, so raising this number raises the bar for every contributor. Keep it in sync with `global.json`; CI installs exactly this version via `setup-dotnet`'s `global-json-file`. +The planner is written in Rust, in `rust/` (core logic in `rust/planner`). The site, in `src/vue`, runs it in the browser as WebAssembly. The planner began as C# (.NET) and was ported to Rust one stage at a time in issue #138; stage 13b removed the C#, the .NET apps and the Lua build. See "History" under Architecture. - **Renovate bumps `global.json` and cannot see this line**, so the two drift apart silently every time it does. #103 raised the pin 10.0.400 -> 10.0.401 and left this paragraph reading 10.0.400. After any SDK bump, `grep -rn` the old version string and fix whatever the bot could not reach. +## Prerequisites - **Expect Homebrew to trail the pin.** Measured 2026-09-21, right after #103: `brew info dotnet` was still 10.0.400 while the pin had moved to 10.0.401, and `dotnet --version` inside the checkout failed outright with "A compatible .NET SDK was not found" - `rollForward` only ever rolls *forward*. That is not a broken checkout, it is the bar being raised ahead of the package manager. Until Homebrew catches up, build through `./docker-build.sh` (its `mcr.microsoft.com/dotnet/sdk:10.0` tag floats and already carries the newer SDK), or install the exact SDK from Microsoft. -- **Git submodules are required** (`FluteSharp`, `delaunator-sharp`, `CSharp.lua`). Clone/update with `git submodule update --init --recursive`. CI checks out with `submodules: recursive`. -- Node 24 (Active LTS) for the Vue front-end. -- The browser-WASM project needs `dotnet workload restore`. +- **Rust, through rustup.** `rust/rust-toolchain.toml` pins the toolchain (1.98.1, with rustfmt, clippy and the `wasm32-unknown-unknown` target). Inside `rust/`, rustup reads that file and installs the pin on first use, so there is nothing to choose. The pin is deliberate: the planner's whole job is exact, repeatable output, and a compiler change is a codegen change. +- **Node 24 (Active LTS) and npm** for the front-end in `src/vue`. It uses npm, not pnpm: `package-lock.json` is the lockfile, and CI deploys with the `wrangler` pinned there. +- That is all for building and testing. There are no submodules and no .NET. `python3` and a real Factorio install are needed only to capture the Factorio oracle (see below). ## Common commands ```bash -# Build / test the .NET solution -dotnet build -dotnet test # all tests (xUnit + Verify snapshots) -dotnet test --filter "FullyQualifiedName~PlannerTest.ExecuteSample" # single test - -# Run the planner sample from the CLI (prints the grid) -dotnet run --project src/FactorioTools.Cli -- oil-field sample -# Re-normalize the test blueprint lists: now a Rust bin, see "Adding a test blueprint" under Testing notes - -# Benchmarks -dotnet run --project src/Benchmark -c Release +# Rust planner (from rust/) +cargo fmt --all +cargo clippy --all-targets -- -D warnings +cargo test --all-targets # quick tests; the slow snapshot sets are #[ignore]d here +cargo test -p planner --test factorio_oracle # one test file +cargo test --release -p snapshots -p contract -- --include-ignored # release gate, about 1.5 min +cargo build --workspace --target wasm32-unknown-unknown +cargo run --release -p snapshots --bin normalize-lists # re-normalize the test blueprint lists # Vue front-end (from src/vue) -npm install +npm ci +npm run build-wasm # build the Rust planner (rust/planner-wasm) and copy it into src/lib/planner-wasm npm run dev # vite dev server npm run build # type-checks (vue-tsc), builds -npm run build-wasm # build the Rust planner (rust/planner-wasm) and copy it into src/lib/planner-wasm +npm test # vitest ``` +CI (`.github/workflows/ci.yml`) runs all five Rust commands above except `normalize-lists`, plus a check that the WebAssembly build gives the same bytes as the native build (below), and builds and tests the front-end. + - The Vue app plans in-browser with the **Rust planner** (`rust/planner-wasm`), compiled to WebAssembly. `npm run build-wasm` in `src/vue` builds it (`cargo build --release -p planner-wasm --target wasm32-unknown-unknown`, run inside `rust/` so the pinned toolchain applies) and copies `planner.js` and `planner_wasm.wasm` into `src/vue/src/lib/planner-wasm/`. That folder is gitignored except for `planner.d.ts`, which types the wrapper, so run `build-wasm` before `npm run dev` or `npm run build`: the Vite build imports those files and - fails without them. Vite gives the `.wasm` a content-hashed name under `/assets/`. Needs a - Rust toolchain (rustup); no .NET. + fails without them. Vite gives the `.wasm` a content-hashed name under `/assets/`. - **Planning runs in a Web Worker** (`src/lib/plannerWorker.ts`), started by `src/lib/wasmPlanner.ts`, so the page stays responsive while it plans. If the worker cannot start (it fails to construct, reports an error, or does not answer within 15 s), the planner falls back for good to the main thread and logs one warning. Add `?planner=main-thread` to - the page URL to force the fallback. The .NET build could never run in a worker (its runtime - deadlocked in `create()`); the Rust module has no runtime, so it can. + the page URL to force the fallback. The old .NET build could never run in a worker (its + runtime deadlocked in `create()`); the Rust module has no runtime, so it can. `deploy-cloudflare.yml` runs `build-wasm` and checks the planner landed in `dist/assets`, on pull requests as well as on deploys. - -### Building without a local .NET 10 SDK (Docker / OrbStack) - -If the machine only has a different SDK than the `global.json` pin, run the build inside the pinned SDK image instead of installing .NET 10. `docker-build.sh` wraps this (works with OrbStack or Docker Desktop) - it mounts the checkout, caches NuGet on the host, and runs as the current user so artifacts are not root-owned: - -```bash -./docker-build.sh # default: test the core dev loop -./docker-build.sh build src/FactorioTools/FactorioTools.csproj -c Release /p:UseLuaSettings=true -./docker-build.sh run --project src/FactorioTools.Cli -- oil-field sample -``` - -A full-solution `dotnet build` also builds `BrowserWasm`/`BlazorWebApp`, which need the wasm-tools workload - prepend a workload restore for that: `./docker-build.sh bash -c "dotnet workload restore && dotnet build -c Release"`. +- **The WebAssembly build is checked against the native one.** The `rust` job in `ci.yml` replays every request in `rust/contract/fixtures/contract.jsonl` through the native contract (`cargo run --release -p contract --example replay`) and through the `.wasm` in Node (`rust/planner-wasm/tests/node-replay.mjs`), and fails on the first byte that differs. ### Devcontainer -`.devcontainer/` builds a Linux container carrying every toolchain this repo needs, which is more than the SDK: **Rust 1.98.1 (rustup 1.29.1, with rustfmt, clippy, rust-src and the wasm32 target), Node 24.21.0 and python3, plus dotnet 10.0.402 with wasm-tools, PowerShell 7.6.5 and Lua 5.2.4** for the C#, until it is removed. Before it, a full local loop meant separate host installs of each, one of which (`tools/check-lua.sh`) started a second container of its own. - -It is not a replacement for `docker-build.sh`. That script is the quick way to run one `dotnet` command in the pinned SDK; this is the whole environment, with an editor attached. - -**The Dockerfile pins the SDK from `global.json` by digest**, which is the point. `rollForward: latestMajor` only rolls forward, so a host whose package manager trails the pin cannot build the repo at all - that is what happened on 2026-09-21 when the pin moved to 10.0.401 and Homebrew still shipped 10.0.400. **When `global.json` moves, the `FROM` line must move with it.** The Devcontainer CI job compares `dotnet --version` against `global.json` and fails if they disagree, so a forgotten re-pin is a red check rather than a container that quietly contradicts the repo. +`.devcontainer/` builds a Linux container carrying every toolchain this repo needs: **Rust 1.98.1 through rustup (with rustfmt, clippy, rust-src and the wasm32 target), Node 24.21.0, python3, git and gcc** (cargo's linker), on plain `ubuntu:24.04` pinned by digest. Node is pinned exactly and checksummed. The base already has user `ubuntu` at uid 1000, which is the container's `remoteUser`. Before stage 13b the image was built on the .NET SDK and also carried dotnet, PowerShell and Lua 5.2; those are gone with the C#. **Rust is pre-installed, and `rust/rust-toolchain.toml` still decides.** The image installs the toolchain named by `RUST_VERSION` in the Dockerfile, so creating the container downloads no compiler. Inside `rust/`, rustup reads the toolchain file as it does anywhere else. A stale `RUST_VERSION` therefore breaks nothing - rustup just installs the new pin on first use (about 10 s, measured) - which is exactly why nobody would notice. So the Devcontainer CI job compares `RUST_VERSION` with the toolchain file, checks that `rustc` and `cargo` in `rust/` report that version, and checks that the wasm32 target is installed. **When `rust/rust-toolchain.toml` moves, move `RUST_VERSION` with it.** The toolchain file is in that job's paths filter for this reason. @@ -82,94 +63,72 @@ The container keeps its own `rust/target`, cargo registry and cargo git cache in Do not try to change the pre-installed toolchain's components from inside the container. `rustup target remove` failed there with `Invalid cross-device link (os error 18)`, because the files sit in a read-only image layer. Change the Dockerfile and rebuild instead. Installing another toolchain works. -Measured 2026-10-21 on tealc (Linux x86-64, 8 cores, Docker Desktop), with `@devcontainers/cli@0.89.0` running the Devcontainer job's own steps against a fresh clone with tags. Two full runs; where they differ, both times are shown: +Measured 2026-10-21 on tealc (Linux x86-64, 8 cores, Docker Desktop), on the stage 13b image, with `@devcontainers/cli@0.89.0` running the Devcontainer job's own steps against a fresh clone with tags: | step | result | | --- | --- | -| image build with `--no-cache`, base image already pulled | 2 min 58 s; the Rust layer took 17 s of it | -| `devcontainer up` | 45 s with the image cached, 88 s when the Rust layer had to be rebuilt. `postCreateCommand` took 34 s and 52 s, of which `build-wasm` took 17 s and 33 s and `npm ci` 10 s | -| toolchain check | Rust 1.98.1 in the image and in the toolchain file; `rustc` and `cargo` 1.98.1 in `rust/`; wasm32 target present | -| `cargo test --all-targets` in `rust/` | 175 passed, 7 ignored; 152 s and 188 s, of which about 11 s is compiling | -| `npm run build-wasm && npm run build && npm run test` in `src/vue` | build OK, 51 tests passed; 12.5 s and 14 s (`build-wasm` already up to date) | -| `dotnet test` on the test project | 4302 passed; 307 s and 241 s | -| Lua transpile step | reproduced the committed `src/lua`, parses and runs under 5.2; 41 s and 29 s | +| image build | 118 s with the base image already pulled. 1.93 GB on disk, down from 4.97 GB on the old .NET SDK base | +| `devcontainer up` | 36 s with the image cached, of which `build-wasm` took 17 s and `npm ci` 7 s | +| toolchain check | Rust 1.98.1 in the image and in the toolchain file; `rustc` and `cargo` 1.98.1 in `rust/`; wasm32 target present; `cc` 13.3.0 | +| `cargo test --all-targets` in `rust/` | 190 passed, 7 ignored, in 175 s | +| `npm run build-wasm && npm run build && npm run test` in `src/vue` | build OK, 55 tests passed, 10.4 s (`build-wasm` already up to date) | The release gates (`cargo test --release -p snapshots -p contract -- --include-ignored`) also pass in the container, in 96 s. The Devcontainer job leaves them to `ci.yml`'s Rust job: they test the planner, not the container. -Measured 2026-09-21 in the built image on macOS arm64 (OrbStack), against a real clone with tags: - -| step | result | -| --- | --- | -| `dotnet --version` inside the repo | 10.0.401, matching `global.json` | -| `dotnet test` on the test project | 4302 passed | -| `pwsh -File src/lua/Invoke-LuaBuild.ps1` | 88 files, reproduced the committed `src/lua` byte-for-byte | -| `luac5.2 -p` over the generated Lua | all 127 files parse | -| `lua5.2 sample.lua` | runs, prints the grid | -| `npm ci && npm run build && npm test` in `src/vue` | build OK, 51 tests passed | -| `npm run build-wasm` | Release publish OK, bundle lands at `public/framework/dotnet.js` (the .NET planner; `build-wasm` has built the Rust planner since stage 12b) | - -Two notes on why specific things are the way they are: - -- **Lua comes from apt, not source.** Ubuntu 24.04's `lua5.2` package is 5.2.4 - the exact version the README's Lua performance log was measured against, and the same package `ci.yml` installs. It provides `lua5.2` and `luac5.2`, the names the `transpile-lua` job uses. -- **No .NET 7 runtime is needed**, despite the CSharp.lua launcher targeting net7.0. `Invoke-LuaBuild.ps1` sets `DOTNET_ROLL_FORWARD=Major` so it runs on the .NET 10 runtime. - **`npm run dev` needs `--host`.** Vite binds `localhost` only, so a port forwarder dialling `127.0.0.1` gets a connection reset. Measured in the container: bare `npm run dev` printed `Network: use --host to expose` and the host got `HTTP 000` (connection reset by peer); with `npm run dev -- --host` the same request returned `HTTP 200`. So: ```bash cd src/vue && npm run dev -- --host ``` -**It does not do two things**, both of which need the host: - -- **Capture the Factorio oracle.** `tools/capture-factorio-oracle.sh` reads a real Factorio install - `--dump-data` from the game binary, the bundled `migrations/*.json`, and `doc-html/runtime-api.json`. There is no Factorio in the container, and the installed binary is the authority on which version gets captured. The `python3` in the image covers the trim half only. -- **Run benchmarks.** `src/Benchmark` is BenchmarkDotNet, and container timings are too noisy to compare against the README's performance log. Run those on the host. +**It cannot capture the Factorio oracle.** `tools/capture-factorio-oracle.sh` reads a real Factorio install - `--dump-data` from the game binary, the bundled `migrations/*.json`, and `doc-html/runtime-api.json`. There is no Factorio in the container, and the installed binary is the authority on which version gets captured. The `python3` in the image covers the trim half only. Run the capture on the host. ## Architecture -### Core library: `src/FactorioTools` (`Knapcode.FactorioTools`) -Pure planning logic with **no JSON/serialization dependencies** - this is intentional so the project can be transpiled to Lua (see Lua section). Key areas under `OilField/`: -- `Planner.cs` - entry point. `Planner.Execute(options, blueprint)` runs the full pipeline; `ExecuteSample()` builds a fixed 4-pumpjack blueprint for demos/tests. -- `Steps/` - the pipeline, roughly in numbered order: `InitializeContext`, `AddPipes.*` (pipe strategies), `PlanBeacons.*` (beacon strategies), `AddElectricPoles`, `PlanUndergroundPipes`, `RotateOptimize`, `AddPipeEntities`, `Validate`, `CleanBlueprint`. -- The planner tries multiple **pipe strategies** (`FbeOriginal`, `Fbe`, `ConnectedCentersDelaunay`, `ConnectedCentersDelaunayMst`, `ConnectedCentersFlute`) and **beacon strategies** (`FbeOriginal`, `Fbe`, `Snug`) - see `Models/PipeStrategy.cs` / `BeaconStrategy.cs` - then selects the best plan (most beacon effects, then fewest beacons, then fewest pipes). -- `Algorithms/` - graph/geometry primitives (A*, Dijkstra, Prim's, BFS, Bresenham's line). Delaunay triangulation and the FLUTE rectilinear-Steiner-tree algorithm come from the submodules. -- `Grid/` - the `SquareGrid` and entity types (`PumpjackCenter`, `Pipe`, `BeaconCenter`, `ElectricPoleCenter`, `Terminal`, etc.); `Location.cs` is a hot type. -- `Containers/` - hand-rolled set/dictionary implementations keyed by `Location` (e.g. `LocationBitSet`, `LocationIntSet`, `LocationHashSet`). Which one is used is controlled by build symbols below; these exist for performance and Lua compatibility. +### The Rust workspace: `rust/` + +Eight crates, in `rust/Cargo.toml`. All of them build for `wasm32-unknown-unknown` as well as natively. + +| crate | what it is | +| --- | --- | +| `planner` | The planner itself, with no JSON or serde. `planner::execute(options, blueprint)` runs the full pipeline; `execute_sample()` plans a fixed 4-pumpjack blueprint for tests. | +| `dotnet-compat` | Rust copies of the .NET collection, sort and rounding behavior the planner's output depends on (see "Ordering rules" below). | +| `delaunator-net` | Delaunay triangulation, a line-for-line port of DelaunatorSharp (MIT, `rust/delaunator-net/LICENSE`). | +| `flute` | FLUTE, the rectilinear Steiner minimal tree algorithm, ported from FluteSharp. Under FLUTE's Attribution Assurance License (`rust/flute/LICENSE-FLUTE`) and FluteSharp's MIT license (`rust/flute/LICENSE-FluteSharp`). Its lookup tables are vendored in `rust/flute/data`. | +| `blueprint` | Blueprint strings: reading them into the planner's types, and writing a planned grid back out, with .NET's JSON text formats so the output matches the snapshots. | +| `contract` | The JSON the site sends and gets back: `plan_json` and `normalize_json`, plus the error envelope. | +| `planner-wasm` | `contract` as a WebAssembly module with plain-number exports, so no binding generator is needed. `planner-wasm/js/planner.js` wraps it in `plan(json)` and `normalize(json)`. This is what the site loads. | +| `snapshots` | The end-to-end gate: plans every test case and compares the output with the committed snapshots in `testdata/oil-field/`, byte for byte. Also holds the `normalize-lists` binary. | -### Serialization: `src/FactorioTools.Serialization` -Blueprint string parsing and emitting live here, separate from the core lib: `ParseBlueprint`, `GridToBlueprintString`, `NormalizeBlueprints`, plus the `System.Text.Json` source-gen context. Front-ends and the CLI reference this project, not just the core. +Inside `rust/planner/src`: -### Front-ends and hosts -- `src/WebApp` - ASP.NET Core API (`OilFieldController`, routes under `api/v1/oil-field`: `normalize`, `plan`; the actions delegate to `PlanOrchestrator`). Produces `swagger.json`, which the front-end no longer uses (its API types are hand-maintained since stage 13a). No longer deployed (the Azure target was retired when the front-end moved to in-browser WASM); kept for local API use, swagger generation, and the `Dockerfile` if self-hosting is wanted. -- `src/vue` - the primary front-end (Vue 3 + Vite + Pinia, persisted settings). This is what's deployed, as a Cloudflare Worker with static assets (see "Deployment" below); it plans in-browser via the WASM bundle and no longer calls a hosted API. Planner constants (pole presets, geometry defaults, strategy defaults, quality levels) are not retyped in TypeScript - they come from `src/vue/src/lib/plannerDefaults.verified.json`, which the Rust test `rust/contract/tests/planner_defaults.rs` generates from `OilFieldOptions` and `Quality`, byte for byte the same as the C# `PlannerDefaultsTest` (both exist until stage 13b removes the C#). The test fails when the file is stale. After changing a default, run `UPDATE_PLANNER_DEFAULTS=1 cargo test -p contract --test planner_defaults` from `rust/` and commit the rewritten file. Adding a field to `OilFieldOptions` without emitting it fails to compile. +- `planner.rs` - the entry point and the loop over strategies. +- `steps/` - the pipeline: `initialize_context`, `add_pipes*` (pipe strategies), `plan_beacons*` (beacon strategies), `add_electric_poles`, `plan_underground_pipes`, `rotate_optimize`, `add_pipe_entities`, `validate`, `clean_blueprint`, plus heat pipes and FLUTE setup. +- The planner tries several **pipe strategies** (`FbeOriginal`, `Fbe`, `ConnectedCentersDelaunay`, `ConnectedCentersDelaunayMst`, `ConnectedCentersFlute`) and **beacon strategies** (`FbeOriginal`, `Fbe`, `Snug`) - see `PipeStrategy` and `BeaconStrategy` in `models.rs` - then picks the best plan (most beacon effects, then fewest beacons, then fewest pipes). +- `algorithms/` - A*, Dijkstra, Prim's, breadth-first search and Bresenham's line. Delaunay triangulation and FLUTE come from the `delaunator-net` and `flute` crates. +- `grid/` - the `SquareGrid` and its entity kinds (pumpjacks, pipes, beacons, poles, terminals); `location.rs` is a hot type. +- `containers/` - hand-rolled sets, dictionaries and bit arrays keyed by `Location`, and the sorted batches that pole placement uses. +- `data.rs` - entity names, item names and sizes, which the Factorio oracle checks (see below). - `src/vue/src/lib/FactorioToolsApi.ts` holds the request and response types for the planner. It began as a client generated from `WebApp/swagger.json` and is hand-maintained since stage 13a. `FactorioToolsApi.test.ts` checks it against the plan and normalize responses in `rust/contract/fixtures/contract.jsonl`: key shape, value types, and every strategy and quality name. When the contract changes, update the types and the test's samples together. +### Ordering rules - **Keep the FLUTE credit in the footer of `src/vue/src/App.vue`.** FLUTE (the `ConnectedCentersFlute` strategy, via the FluteSharp submodule) is under Chris Chu's Attribution Assurance License, `submodules/FluteSharp/flute-c/license.txt`. It requires a prominent display of "Dr. Chris C. N. Chu", "Iowa State University" and `http://home.engineering.iastate.edu/~cnchu/` every time the program launches. The footer shows on every page, which is what meets that. A Rust port of FLUTE would be under the same terms (#138). -- `src/BrowserWasm` - the planner as .NET WASM (trimmed). **The site no longer loads it**: it loads `rust/planner-wasm` instead (see the Rust port section). It stays until stage 13 of #138 removes the .NET projects, and the notes below describe how it was built. +- **Planner output must not change by accident**, including how ties are broken. The committed snapshots were recorded from the C# planner, and the Rust port reproduces them byte for byte. That depends on .NET details: `HashSet`/`Dictionary` enumeration order (insertion order, with freed slots reused last-in first-out), the 4-ary `PriorityQueue`, the unstable introsort behind `List.Sort`, and `Math.Round` rounding half to even. `rust/dotnet-compat` holds Rust copies of these. Use them, not std's `HashMap`, `BinaryHeap` or `sort_by`, wherever the result depends on order. +- **Do not swap in the `delaunator` crate** for `delaunator-net`. It is a newer algorithm whose tie-breaking differs, which changes the planner's pipes. `rust/flute` likewise matches FluteSharp tree for tree. +- **The fixtures are frozen.** `rust/*/fixtures` hold inputs and outputs recorded from the C# code (the planner, DelaunatorSharp, FluteSharp and .NET itself) at commit `0532d923`, and each crate's tests check the Rust against them. Each crate's `FIXTURES.md` gives the format. The recorders that wrote them were C# programs, deleted in stage 13b, so the fixtures cannot be re-recorded. A deliberate change that one of them pins means editing that fixture by hand and saying why in the commit. - **It shipped as AOT with `Location` as a class, and the two flags only work together.** `BrowserWasm.csproj` sets `RunAOTCompilation=true`, and `build-wasm` passes `--property:LocationAsStruct=false`. That flag must stay on the command line: it is a global property that has to reach the referenced projects, and setting it in the `.csproj` does not. Measured 2026-10-06 in issue #139, against native .NET on the same machine: +### Front-end: `src/vue` - | Build | Planning speed | Time to first plan | Download (brotli) | - | --- | --- | --- | --- | - | Interpreter (shipped until #139) | 17x slower | 1.22 s | 1.35 MiB | - | AOT, `Location` a struct | 14x slower | 0.78 s | 2.6 MiB | - | **AOT, `Location` a class (shipped)** | **4.3x slower** | **0.54 s** | **2.5 MiB** | +- The site (Vue 3 + Vite + Pinia, persisted settings). This is what's deployed, as a Cloudflare Worker with static assets (see "Deployment" below). It plans in-browser with `rust/planner-wasm`, through `src/vue/src/lib/wasmPlanner.ts` and a Web Worker, and calls no hosted API. +- Planner constants (pole presets, geometry defaults, strategy defaults, quality levels) are not retyped in TypeScript. They come from `src/vue/src/lib/plannerDefaults.verified.json`, which the Rust test `rust/contract/tests/planner_defaults.rs` generates from `OilFieldOptions` and `Quality`. The test fails when the file is stale. After changing a default, run `UPDATE_PLANNER_DEFAULTS=1 cargo test -p contract --test planner_defaults` from `rust/` and commit the rewritten file. Adding a field to `OilFieldOptions` without emitting it fails to compile. +- `src/vue/src/lib/FactorioToolsApi.ts` holds the request and response types for the Rust planner, hand-maintained since stage 13a (it began as a swagger-generated client). `FactorioToolsApi.test.ts` checks it against the plan and normalize responses in `rust/contract/fixtures/contract.jsonl`: key shape, value types, and every strategy and quality name. When the contract changes, update the types and the test's samples together. +- **Keep the FLUTE credit in the footer of `src/vue/src/App.vue`.** FLUTE (the `ConnectedCentersFlute` strategy, in `rust/flute`) is under Chris Chu's Attribution Assurance License, `rust/flute/LICENSE-FLUTE`. It requires a prominent display of "Dr. Chris C. N. Chu", "Iowa State University" and `http://home.engineering.iastate.edu/~cnchu/` every time the program launches. The footer shows on every page, which is what meets that. +- The site's "Add sample" blueprints are read from `testdata/oil-field/small-list.txt` at build time (`src/vue/vite.config.ts`). - AOT alone barely helps because Mono's AOT falls back to the interpreter for generic code over value types, and `Location` is a struct by default. A `dotnet publish` of `BrowserWasm` without the property quietly gives you that middle row. The interpreter was first chosen in `docs/superpowers/specs/2026-06-18-in-browser-wasm-planner-design.md` (decision 2) for a smaller download and faster CI; the AOT publish takes about 64 s against 23 s. The `test-location-as-class` CI job tests the shipped setting on its own. Read #139 before changing either flag, and measure again after. -- `src/BlazorWebApp` - alternate Blazor host. -- `src/FactorioTools.Cli` (`System.CommandLine`) - `oil-field` subcommands `sample`, `normalize`, `sandbox`. Output assembly is `Knapcode.FactorioTools.Sandbox`. -- `src/Benchmark` - BenchmarkDotNet harness. +### History -### Rust port (in progress): `rust/` -The planner is being ported to Rust/WASM, one stage per PR, following the plan on issue #138. The C# stays the live planner until the Rust version matches every Verify snapshot. Until then: +The planner was first written in C#, with a CLI, an ASP.NET API, a Blazor host, a BenchmarkDotNet harness, a Lua build for running inside Factorio, and a .NET WebAssembly build that the site loaded. Issue #138 ported it to Rust stage by stage. Stage 12b (#174) switched the site to the Rust build, and stage 13b deleted the C# and everything built from it. To read the old code, check out a commit from before that stage, such as `ee83ffcc` (the stage 13a merge). -- **The port must reproduce the C# output exactly**, including how ties are broken. That depends on .NET details: `HashSet`/`Dictionary` enumeration order (insertion order, with freed slots reused last-in first-out), the 4-ary `PriorityQueue`, the unstable introsort behind `List.Sort`, and `Math.Round` rounding half to even. `rust/dotnet-compat` holds Rust copies of these. Use them, not std's `HashMap`, `BinaryHeap` or `sort_by`, wherever the C# result depends on order. -- **Those copies are tested against fixtures recorded from real .NET** (`rust/dotnet-compat/fixtures`, format in `rust/dotnet-compat/FIXTURES.md`). The `test-default` CI job reruns the recorder on the pinned SDK and fails if the fixtures drift, so an SDK bump that changes .NET's behavior shows up as a red check. -- **`rust/delaunator-net` and `rust/flute` are ports of the two submodules**, DelaunatorSharp and FluteSharp, kept byte-for-byte compatible with them. Do not swap in the `delaunator` crate: it is a newer algorithm whose tie-breaking differs, which changes the planner's pipes. `rust/flute` is under FLUTE's Attribution Assurance License (`rust/flute/LICENSE-FLUTE`), the same terms as the C#, so the site's footer credit covers it too. -- **`rust/snapshots` is the end-to-end gate.** It plans every case the C# Verify tests plan, from the same `small-list.txt` and `big-list.txt`, and compares the output with the committed `*.verified.txt` files byte for byte, plus `Score.HasExpectedScore`. It also fails if a snapshot file exists that no Rust case checks. Run it in release: `cargo test --release -p snapshots -- --include-ignored` (about a minute). `rust/blueprint` parses blueprint strings for it. Since stage 13a it can also write the snapshots: see "Changing planner output on purpose" under Testing notes. -- **`rust/contract` and `rust/planner-wasm` are what the site loads.** `contract` is the JSON the site sends and gets back (`PlanOrchestrator`'s `Plan` and `Normalize`, the error envelope), checked against responses recorded from the C# (`rust/contract/fixtures`). `planner-wasm` builds it to WebAssembly with no extra toolchain, and `planner-wasm/js/planner.js` exposes `plan(json)` and `normalize(json)` with the same signatures as `src/vue/src/lib/wasmPlanner.ts`. CI checks the WebAssembly build's output against the native contract byte for byte. **The site loads this module** (stage 12b of #138), through `src/vue/src/lib/wasmPlanner.ts` and a Web Worker. -- **Commands**, from `rust/`: `cargo fmt --all`, `cargo clippy --all-targets -- -D warnings`, `cargo test --all-targets`, and `cargo build --workspace --target wasm32-unknown-unknown`. The toolchain is pinned in `rust/rust-toolchain.toml`; rustup installs it on first use. -- **A change to the C# planner during the port** has to land in the Rust version too, or wait for it. `rust/planner/fixtures` are recorded from the C# planner (format and the command to regenerate them in `rust/planner/FIXTURES.md`), and the `test-default` CI job reruns every recorder under `rust/`. So a C# change that alters planner output turns CI red until the fixtures are regenerated and the Rust port matches them. +The dated plans and specs in `docs/superpowers/` are kept as history and are not updated. Most describe the C# code and name files that no longer exist. For example, `docs/superpowers/specs/2026-06-18-in-browser-wasm-planner-design.md` is the design of the .NET WebAssembly planner that the site ran before the Rust one. ## Deployment @@ -197,43 +156,12 @@ npx wrangler deploy --dry-run # checks the config and reads the assets, uploa `wrangler dev` writes a `.wrangler/` state folder, which is gitignored. -## Performance build flags (and Lua compatibility) - -The core library is heavily perf-tuned through conditional compilation, configured in `Directory.Build.props`. Many features can be toggled per-build with MSBuild properties, e.g.: - -```bash -dotnet build /p:UseHashSets=false -dotnet build /p:LocationAsStruct=false -dotnet build /p:UseLuaSettings=true # turns OFF the perf features that Lua can't use -``` - -Flags include `UseHashSets` (`USE_HASHSETS`), `UseBitArray` (`USE_BITARRAY`), `LocationAsStruct` (`LOCATION_AS_STRUCT`), `UseSharedInstances`, `UseVectors`, `UseStackalloc`, `RentNeighbors`, `AllowDynamicFluteDegree`, `EnableVisualizer`, `EnableGridToString`. `UseLuaSettings=true` sets the Lua-safe combination. **CI builds the solution under many of these combinations** (see `.github/workflows/ci.yml`) - if you touch core data structures, build/test under both default and `UseLuaSettings=true` before assuming it's green. - -## Lua transpilation - -The core lib is transpiled to Lua via the `CSharp.lua` submodule so the planner can run inside Factorio/Lua. `FactorioTools.Serialization` is **not** transpiled - `Invoke-LuaBuild.ps1` never passes it, which is why the core must stay free of JSON/serialization dependencies (see the core library section above). Output lives in `src/lua`; rebuild with `src/lua/Invoke-LuaBuild.ps1` (PowerShell). Target is **Lua 5.2** - Factorio mods run on a modified Lua 5.2 environment, and the transpiled output is exercised with Lua 5.2.4 (see the "Lua performance log" in `README.md`). - -The `transpile-lua` CI job runs that script, fails if the committed `src/lua` no longer matches what the C# transpiles to, and syntax-checks the result with `luac5.2`. So if you change the core, regenerate `src/lua` and commit it in the same change. - -- Avoid C# constructs the existing code avoids in hot paths under Lua settings: `yield return`, LINQ, named tuples, try/catch, and struct dictionary keys have all been removed for Lua performance. LINQ is worse than a perf problem: `CoreSystem.lua` does not load `Collections.Linq`, so `Invoke-LuaBuild.ps1` never copies `Linq.lua` and LINQ transpiles into calls on a module that was never shipped - a runtime failure inside Factorio, not a build error. -- Keep control flow deterministic: Factorio modifies `pairs()` and `math.random()` for determinism, so prefer simple, stable iteration and avoid order-dependent assumptions. -### Checking the generated Lua locally - -Run `tools/check-lua.sh`. It does exactly what the `transpile-lua` CI job does, against the same Lua version, in Docker: - -1. syntax-checks every generated file with `luac` 5.2.4 -2. runs `sample.lua`, which is the step that actually matters - -Those two are not redundant. LINQ transpiles cleanly *and* parses cleanly - it emits `local Linq = System.Linq.Enumerable`, which is nil because `Collections.Linq` is not in the CoreSystem load list. It only fails when the module loads (`attempt to index field 'Linq'`), so the check has to run the planner, not just parse it. That takes well under a second. - -Use the script rather than your own `luac`. Homebrew no longer ships Lua 5.2, so a Mac checkout typically has 5.4 or newer, and parsing 5.2-targeted code with a 5.5 parser proves very little. The image (`nickblah/lua:5.2-alpine`, ~11MB) is Lua 5.2.4 - the exact version the performance log above was measured against. Needs Docker (OrbStack or Docker Desktop); regenerate first with `pwsh src/lua/Invoke-LuaBuild.ps1` if you changed the core. - -### Factorio reference +## Factorio reference - API docs root: - Runtime API: - Libraries/functions Factorio adds or modifies (incl. `require()` restrictions): -- Lua 5.2 manual: +- Lua 5.2 manual, for writing probe mods: - Friday Facts blog (Wube's own, so it counts as official): . Best source for *why* a behavior changed. See the oracle section below for how to use it and its limits. - Prefer official Factorio docs over forum/blog/wiki advice when changing runtime behavior. The Friday Facts blog is written by Wube, so it is on the official side of that line. @@ -262,7 +190,7 @@ Two more sources are *reference* rather than authoritative. They say **why** som Worked example: the pumpjack change behind issue #81 shows up in a dump as `output_fluid_box` pipe connections going from `[[1,-1],[1,-1],[-1,1],[-1,1]]` to `[[1,-1],[1,1],[-1,1],[-1,-1]]`. The dump does not say why. FFF #442, "Flip, Flow, and Fresh Paint", does, and the same release added `use_mirroring` and `migrate_horizontal_mirroring` to that prototype - which is how you learn the change is about entity mirroring rather than a typo fix. -`tools/capture-factorio-oracle.sh` pulls those three into `test/FactorioTools.Test/OilField/factorio-oracle.json`. Two tests read it: the C# `FactorioOracleTest`, and its Rust port, `rust/planner/tests/factorio_oracle.rs`, plus an entity-size check in the tests module of `rust/blueprint/src/write.rs`. Rust has no reflection, so `entity_names::vanilla::ALL` and `item_names::vanilla::ALL` in `rust/planner/src/data.rs` list the names by hand, and `data::tests::all_arrays_match_their_constants` fails if a name constant is added without adding it to `ALL`. +`tools/capture-factorio-oracle.sh` pulls those three into `testdata/oil-field/factorio-oracle.json`. The oracle checks are `rust/planner/tests/factorio_oracle.rs`, plus an entity-size check in the tests module of `rust/blueprint/src/write.rs`. Rust has no reflection, so `entity_names::vanilla::ALL` and `item_names::vanilla::ALL` in `rust/planner/src/data.rs` list the names by hand, and `data::tests::all_arrays_match_their_constants` fails if a name constant is added without adding it to `ALL`. ```bash tools/capture-factorio-oracle.sh # auto-detects a Steam or /Applications install @@ -273,7 +201,7 @@ tools/capture-factorio-oracle.sh --factorio /path/to/factorio.app Notes on using it: - **The committed fixture targets the experimental branch**, currently 2.1.14, not stable. That is deliberate: the bug reports come from 2.1 and that is where the game is heading. `captureInfo.factorioVersion` in the fixture records which build it came from. -- **Stable and experimental are not identical.** Comparing 2.0.77 stable against 2.1.14 experimental, everything the planner reads is byte-identical except one thing: the pumpjack's output fluid box went from 2 distinct corners to 4, one per rotation (FFF #442). That difference did mean the hardcoded terminal offsets in `Helpers.cs` were wrong for 2.1 on east and west, which is issue #81, now fixed. A probe mod settled it by reading `PipeConnection.target_position` out of both running games rather than deriving it; `FactorioOracleTest.TerminalOffsetsMatchTheFactorioOutputCorners` (and its Rust port, `terminal_offsets_match_the_factorio_output_corners` in `rust/planner/tests/factorio_oracle.rs`) now pins the offsets to the fixture so the next corner move fails a test. Capture any second version with `--factorio ` and `--out ` to compare. +- **Stable and experimental are not identical.** Comparing 2.0.77 stable against 2.1.14 experimental, everything the planner reads is byte-identical except one thing: the pumpjack's output fluid box went from 2 distinct corners to 4, one per rotation (FFF #442). That difference did mean the hardcoded terminal offsets (then in the C# `Helpers.cs`, now `TERMINAL_OFFSETS` in `rust/planner/src/helpers.rs`) were wrong for 2.1 on east and west, which is issue #81, now fixed. A probe mod settled it by reading `PipeConnection.target_position` out of both running games rather than deriving it; `terminal_offsets_match_the_factorio_output_corners` in `rust/planner/tests/factorio_oracle.rs` now pins the offsets to the fixture so the next corner move fails a test. Capture any second version with `--factorio ` and `--out ` to compare. - **Re-capture after every Factorio update and commit the diff.** A changed fixture is the signal that a hardcoded constant needs review. `--check` answers "has the game moved past what we committed?" without dirtying the tree. - **The installed binary is the authority** on which version gets captured. Steam updates it without asking, so it decides and everything else follows. Same convention as `scripts/sync-factorio-refs.sh` in FactorioMapWebUI. - **It runs with user mods disabled** (`--mod-directory` pointed at an empty directory). Mods rewrite prototypes freely, so a capture that loads them describes one person's modded game rather than Factorio. The script prints which mods loaded; expect only `core base elevated-rails quality recycler space-age`. @@ -282,7 +210,7 @@ Notes on using it: - **`defines` values in the fixture are inferred, not read.** `runtime-api.json` has no value field at all: across all 1,554 entries the only keys are `name`, `order` and `description`, and `trim-factorio-oracle.py:150` uses `order`. That is right today only because Factorio declares directions clockwise from `north = 0` with no gaps, and a dense index cannot express a gap, a duplicate, or a non-zero start. Issue #83. Reading the real value needs a probe mod; `factorio-oracle` does that now, and confirmed the two agree on 2.1.14. - **CI never runs the capture** and needs no Factorio install - it reads the committed fixture. That is why the fixture is committed rather than generated on demand. - Capture needs `python3` (for JSON trimming) and a Factorio install. Neither is needed to build or test. -- `EntityNames.AaiIndustry` names come from a mod, so they are deliberately absent from a vanilla capture. That is expected, not drift. +- The `entity_names::aai_industry` names in `data.rs` come from a mod, so they are deliberately absent from a vanilla capture. That is expected, not drift. - Output is deterministic: two captures of the same install are byte-identical. **This script now has a replacement, and it is proven equivalent.** @@ -294,7 +222,7 @@ reproduces the committed `factorio-oracle.json` **byte for byte** from a real ```bash factorio-oracle run --probe dump-data.json --work-dir /tmp/w > /tmp/run.json factorio-oracle trim --run /tmp/run.json --spec trim-spec.json \ - --out test/FactorioTools.Test/OilField/factorio-oracle.json [--check] + --out testdata/oil-field/factorio-oracle.json [--check] ``` The allowlists that live in `trim-factorio-oracle.py` move into that `trim-spec.json` @@ -309,9 +237,10 @@ Two related sources, for when the game binary is not the easiest thing to reach: ## Testing notes -- Tests use **xUnit v3 + Verify** (`xunit.v3` + `Verify.XunitV3`). Many tests assert against committed `*.verified.txt` snapshots under `test/FactorioTools.Test/OilField`. When behavior legitimately changes, update snapshots via Verify's accept workflow (received vs verified) rather than editing expected files by hand. A stale committed snapshot **does** fail on CI: `AutoVerify` is off there, so the test itself throws `VerifyException` with the diff (confirmed on a real runner, not just inferred - note that setting `CI`/`GITHUB_ACTIONS` locally does *not* reproduce the build-server detection, so local simulation of this is misleading). A "Check no Verify snapshots drifted" step backs that up by failing if `dotnet test` leaves any `*.verified.*` file dirty, in case the detection ever regresses. Commit regenerated snapshots with your change. +- **The planner's tests are Rust** (`cargo test`, from `rust/`). The main ones compare planner output with the committed `*.verified.txt` snapshots under `testdata/oil-field/`, byte for byte: `rust/snapshots` plans every case and also fails if a snapshot file exists that no case checks. The full sets are slow, so they are `#[ignore]`d in debug builds; run them in release with `--include-ignored`, as CI does. The front-end has its own vitest suite (`npm test` in `src/vue`). +- **The snapshots are still in the Verify library's file format**, because the C# tests that first wrote them used Verify: a UTF-8 byte order mark, then the text, with LF line ends. `.gitattributes` pins `*.verified.txt` (and `.xml`, `.json`) to `eol=lf` because the comparison is byte for byte. Never edit a snapshot by hand; regenerate it (see "Changing planner output on purpose"). - `Score.HasExpectedScore.verified.txt` is the planner-quality scoreboard across the 61 blueprints in `small-list.txt` (the file also holds 11 `#` comment lines, so do not read its line count as the blueprint count). `small-list.txt` / `big-list.txt` hold the blueprint corpus; `big-list.txt` is 1147 blueprints and is not scored. -- **Adding a test blueprint.** Paste the blueprint string as a new line at the end of `small-list.txt` (scored, may hold `#` comments), or in `big-list.txt` (not scored, blueprints only). Then, from `rust/`: +- **Adding a test blueprint.** Paste the blueprint string as a new line at the end of `testdata/oil-field/small-list.txt` (scored, may hold `#` comments), or in `big-list.txt` (not scored, blueprints only). Then, from `rust/`: ```bash cargo run --release -p snapshots --bin normalize-lists @@ -319,17 +248,17 @@ Two related sources, for when the game binary is not the easiest thing to reach: cargo test --release -p snapshots -p contract -- --include-ignored ``` - The first command cleans each blueprint down to its pumpjacks, drops repeats, and moves a blueprint found in both lists to the small list only. It compares blueprints by their decoded JSON, not the compressed string, and leaves a line that is already normalized exactly as written, so the diff shows only real changes. A line that does not parse stops it with the file and line number; fix or remove the line, or pass `--keep-invalid`. (It replaces the C# `oil-field normalize`, which on .NET 10 would rewrite all 1,208 lines because the committed strings came from an older zlib.) The second command writes the new snapshots, and the third checks everything in compare mode. A new small-list blueprint at index N creates 26 files: 24 `PlannerWithAllOptions/Theory.E__{_b}{_o}{_u}.verified.txt`, plus `AllStrategiesOnSmallList/Theory.E_.verified.txt` and `NonStandardBeacon/Theory.E_.verified.txt`. It also changes `Score.HasExpectedScore.verified.txt`. If the blueprint came out of `big-list.txt` at index M, every `AllStrategiesOnBigList/Theory.E_` from M on shifts down one and the last is deleted. No test has a count to edit. Commit the lists and every new, changed and deleted snapshot together. -- **Changing planner output on purpose.** From `rust/`, `UPDATE_SNAPSHOTS=1 cargo test --release -p snapshots -- --include-ignored` rewrites every snapshot and the scoreboard from the Rust output: it creates missing files, overwrites changed ones, and deletes any `*.verified.txt` no case produces. Only the value `1` turns it on, and CI never sets it, so a stale snapshot fails there. Run it in release and with `--include-ignored`, or only the quick `PlannerTest` snapshots are written. The bytes are what Verify writes (UTF-8 BOM, LF); stage 13a checked that Rust regenerates all 2,748 files byte for byte and that Verify accepts files Rust writes. Read the diff before committing: it is the only record of what the change did to real plans. + The first command cleans each blueprint down to its pumpjacks, drops repeats, and moves a blueprint found in both lists to the small list only. It compares blueprints by their decoded JSON, not the compressed string, and leaves a line that is already normalized exactly as written, so the diff shows only real changes. A line that does not parse stops it with the file and line number; fix or remove the line, or pass `--keep-invalid`. (It replaced the C# CLI's `oil-field normalize`, which on .NET 10 would have rewritten all 1,208 lines because the committed strings came from an older zlib.) The second command writes the new snapshots, and the third checks everything in compare mode. A new small-list blueprint at index N creates 26 files: 24 `PlannerWithAllOptions/Theory.E__{_b}{_o}{_u}.verified.txt`, plus `AllStrategiesOnSmallList/Theory.E_.verified.txt` and `NonStandardBeacon/Theory.E_.verified.txt`. It also changes `Score.HasExpectedScore.verified.txt`. If the blueprint came out of `big-list.txt` at index M, every `AllStrategiesOnBigList/Theory.E_` from M on shifts down one and the last is deleted. No test has a count to edit. Commit the lists and every new, changed and deleted snapshot together. +- **Changing planner output on purpose.** From `rust/`, `UPDATE_SNAPSHOTS=1 cargo test --release -p snapshots -- --include-ignored` rewrites every snapshot and the scoreboard from the Rust output: it creates missing files, overwrites changed ones, and deletes any `*.verified.txt` no case produces. Only the value `1` turns it on, and CI never sets it, so a stale snapshot fails there. Run it in release and with `--include-ignored`, or only the quick `PlannerTest` snapshots are written. Stage 13a checked that Rust regenerates all 2,748 files byte for byte and that Verify accepted files Rust wrote. Read the diff before committing: it is the only record of what the change did to real plans. A change like this may also break a frozen fixture under `rust/*/fixtures` (see "Ordering rules"). ## Dependency automation Dependency updates are handled by **Renovate**, configured in `.github/renovate.json5`. It is JSON5 so the reasoning lives in comments beside each rule - the comments are load-bearing, not decoration. - **Updates arrive as one weekly batch**, Monday morning `America/Los_Angeles`. Security fixes deliberately skip that window. To pull a run forward, tick the "trigger a request for Renovate to run again" checkbox on the dependency dashboard. -- **Nothing automerges.** `automerge: false` is global with no exceptions. A green CI run proves the repo is *consistent*, not that a bump is *correct* - and planner correctness lives in Verify snapshots, where a subtle output change surfaces as a snapshot diff to accept rather than an obvious failure. -- **There are deliberate holds.** Before "fixing" a version that looks stale, read the rule that holds it. Each carries its reason and the condition under which to revisit. Several also mirror a comment in the corresponding `.csproj`; keep the two in sync if you change either. -- **The dependency dashboard issue is the live inventory** of everything held back, everything queued, and everything detected. It is a better place to look than reading manifests by hand - it is how a missing hold on `swashbuckle.aspnetcore.cli` (which lives in `src/WebApp/.config/dotnet-tools.json` - a tool manifest under the project, not at the repo root, and not a `.csproj`) was found. +- **Nothing automerges.** `automerge: false` is global with no exceptions. A green CI run proves the repo is *consistent*, not that a bump is *correct* - and planner correctness lives in the snapshots, where a subtle output change surfaces as a snapshot diff to review rather than an obvious failure. +- **There are deliberate holds.** Before "fixing" a version that looks stale, read the rule that holds it. Each carries its reason and the condition under which to revisit. +- **The dependency dashboard issue is the live inventory** of everything held back, everything queued, and everything detected. It is a better place to look than reading manifests by hand. - Holds that a migration would lift have a tracking issue named in the rule. Holds waiting on an external circumstance say explicitly that they have no issue *by design*, so an absent issue reads as a decision rather than an oversight. ### Editing the config diff --git a/Directory.Build.props b/Directory.Build.props deleted file mode 100644 index f8a1d433..00000000 --- a/Directory.Build.props +++ /dev/null @@ -1,39 +0,0 @@ - - - - false - - - - false - false - false - false - false - false - true - false - true - false - - - - true - $(DefineConstants);USE_HASHSETS - - - - true - $(DefineConstants);USE_BITARRAY - - - - true - $(DefineConstants);LOCATION_AS_STRUCT - - - - $(NoWarn);NETSDK1138 - - - \ No newline at end of file diff --git a/FactorioTools.sln b/FactorioTools.sln deleted file mode 100644 index 0eac3b3f..00000000 --- a/FactorioTools.sln +++ /dev/null @@ -1,260 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.5.33209.295 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactorioTools", "src\FactorioTools\FactorioTools.csproj", "{1736C18D-D61E-45C1-BADB-290D72AC2899}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2237B275-4DF9-492E-A555-E38CE0DC0E12}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3CE8C202-6915-434A-A6D5-31776270EE21}" - ProjectSection(SolutionItems) = preProject - .gitattributes = .gitattributes - .gitignore = .gitignore - Directory.Build.props = Directory.Build.props - global.json = global.json - LICENSE = LICENSE - README.md = README.md - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorWebApp", "src\BlazorWebApp\BlazorWebApp.csproj", "{A6F55C6B-E834-43A3-A308-BFE109079F2A}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmark", "src\Benchmark\Benchmark.csproj", "{207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{789C06F6-F79B-4A6A-AC72-13672271A808}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactorioTools.Test", "test\FactorioTools.Test\FactorioTools.Test.csproj", "{6627AC05-E61C-4DC8-814B-C75366288DA4}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrowserWasm", "src\BrowserWasm\BrowserWasm.csproj", "{6AE486CE-8803-4259-B8C3-33600FEFD95C}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "src\WebApp\WebApp.csproj", "{67D1B7E1-FF93-474B-8964-10381D1E5B7A}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FluteSharp", "FluteSharp", "{EEA7B6C1-01A0-465D-8806-5E48680F8E70}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluteSharp", "submodules\FluteSharp\src\FluteSharp\FluteSharp.csproj", "{F59924C0-67E3-451B-B4CF-F82073CD8EE8}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "submodules", "submodules", "{C941BE21-DBF2-46EA-879E-892758A3AE53}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "delaunator-sharp", "delaunator-sharp", "{ADC9955B-7C4B-4657-B6F8-DDD569315D2A}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DelaunatorSharp", "submodules\delaunator-sharp\DelaunatorSharp\DelaunatorSharp.csproj", "{85D6968F-F3B7-40FC-8491-524D77FD858E}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "submodules\FluteSharp\src\SourceGenerator\SourceGenerator.csproj", "{CA3BE482-7936-432C-878F-F856719E326E}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactorioTools.Serialization", "src\FactorioTools.Serialization\FactorioTools.Serialization.csproj", "{AA3FA811-5810-40D4-ADB4-C9688EA4FA79}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CSharp.lua", "CSharp.lua", "{1C96F1C2-23C7-40C7-AA33-BB16311DFDDE}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharp.lua", "submodules\CSharp.lua\CSharp.lua\CSharp.lua.csproj", "{742421E3-AE7F-46DB-B569-5F3C5E261940}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharp.lua.Launcher", "submodules\CSharp.lua\CSharp.lua.Launcher\CSharp.lua.Launcher.csproj", "{5A546BA8-1514-47DD-A850-0F8B9C2B8F79}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactorioTools.Cli", "src\FactorioTools.Cli\FactorioTools.Cli.csproj", "{5262CC9E-421C-4A37-AE9D-F4408CA9A78C}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactorioTools.Contract", "src\FactorioTools.Contract\FactorioTools.Contract.csproj", "{58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|x64.ActiveCfg = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|x64.Build.0 = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|x86.ActiveCfg = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Debug|x86.Build.0 = Debug|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|Any CPU.Build.0 = Release|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|x64.ActiveCfg = Release|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|x64.Build.0 = Release|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|x86.ActiveCfg = Release|Any CPU - {1736C18D-D61E-45C1-BADB-290D72AC2899}.Release|x86.Build.0 = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|x64.ActiveCfg = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|x64.Build.0 = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|x86.ActiveCfg = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Debug|x86.Build.0 = Debug|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|Any CPU.Build.0 = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|x64.ActiveCfg = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|x64.Build.0 = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|x86.ActiveCfg = Release|Any CPU - {A6F55C6B-E834-43A3-A308-BFE109079F2A}.Release|x86.Build.0 = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|x64.ActiveCfg = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|x64.Build.0 = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|x86.ActiveCfg = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Debug|x86.Build.0 = Debug|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|Any CPU.Build.0 = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|x64.ActiveCfg = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|x64.Build.0 = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|x86.ActiveCfg = Release|Any CPU - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B}.Release|x86.Build.0 = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|x64.ActiveCfg = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|x64.Build.0 = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|x86.ActiveCfg = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Debug|x86.Build.0 = Debug|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|Any CPU.Build.0 = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|x64.ActiveCfg = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|x64.Build.0 = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|x86.ActiveCfg = Release|Any CPU - {6627AC05-E61C-4DC8-814B-C75366288DA4}.Release|x86.Build.0 = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|x64.ActiveCfg = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|x64.Build.0 = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|x86.ActiveCfg = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Debug|x86.Build.0 = Debug|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|Any CPU.Build.0 = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|x64.ActiveCfg = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|x64.Build.0 = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|x86.ActiveCfg = Release|Any CPU - {6AE486CE-8803-4259-B8C3-33600FEFD95C}.Release|x86.Build.0 = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|x64.ActiveCfg = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|x64.Build.0 = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|x86.ActiveCfg = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Debug|x86.Build.0 = Debug|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|Any CPU.Build.0 = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|x64.ActiveCfg = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|x64.Build.0 = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|x86.ActiveCfg = Release|Any CPU - {67D1B7E1-FF93-474B-8964-10381D1E5B7A}.Release|x86.Build.0 = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|x64.ActiveCfg = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|x64.Build.0 = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|x86.ActiveCfg = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Debug|x86.Build.0 = Debug|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|Any CPU.Build.0 = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|x64.ActiveCfg = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|x64.Build.0 = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|x86.ActiveCfg = Release|Any CPU - {F59924C0-67E3-451B-B4CF-F82073CD8EE8}.Release|x86.Build.0 = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|x64.ActiveCfg = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|x64.Build.0 = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|x86.ActiveCfg = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Debug|x86.Build.0 = Debug|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|Any CPU.Build.0 = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|x64.ActiveCfg = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|x64.Build.0 = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|x86.ActiveCfg = Release|Any CPU - {85D6968F-F3B7-40FC-8491-524D77FD858E}.Release|x86.Build.0 = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|x64.ActiveCfg = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|x64.Build.0 = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|x86.ActiveCfg = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Debug|x86.Build.0 = Debug|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|Any CPU.Build.0 = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|x64.ActiveCfg = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|x64.Build.0 = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|x86.ActiveCfg = Release|Any CPU - {CA3BE482-7936-432C-878F-F856719E326E}.Release|x86.Build.0 = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|x64.ActiveCfg = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|x64.Build.0 = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|x86.ActiveCfg = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Debug|x86.Build.0 = Debug|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|Any CPU.Build.0 = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|x64.ActiveCfg = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|x64.Build.0 = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|x86.ActiveCfg = Release|Any CPU - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79}.Release|x86.Build.0 = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|Any CPU.Build.0 = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|x64.ActiveCfg = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|x64.Build.0 = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|x86.ActiveCfg = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Debug|x86.Build.0 = Debug|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|Any CPU.ActiveCfg = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|Any CPU.Build.0 = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|x64.ActiveCfg = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|x64.Build.0 = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|x86.ActiveCfg = Release|Any CPU - {742421E3-AE7F-46DB-B569-5F3C5E261940}.Release|x86.Build.0 = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|x64.ActiveCfg = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|x64.Build.0 = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|x86.ActiveCfg = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Debug|x86.Build.0 = Debug|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|Any CPU.Build.0 = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|x64.ActiveCfg = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|x64.Build.0 = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|x86.ActiveCfg = Release|Any CPU - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79}.Release|x86.Build.0 = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|x64.ActiveCfg = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|x64.Build.0 = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|x86.ActiveCfg = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Debug|x86.Build.0 = Debug|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|Any CPU.Build.0 = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|x64.ActiveCfg = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|x64.Build.0 = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|x86.ActiveCfg = Release|Any CPU - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C}.Release|x86.Build.0 = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|x64.ActiveCfg = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|x64.Build.0 = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|x86.ActiveCfg = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Debug|x86.Build.0 = Debug|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|Any CPU.Build.0 = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|x64.ActiveCfg = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|x64.Build.0 = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|x86.ActiveCfg = Release|Any CPU - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4}.Release|x86.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {1736C18D-D61E-45C1-BADB-290D72AC2899} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {A6F55C6B-E834-43A3-A308-BFE109079F2A} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {207D0EE9-2E8A-4E1A-8AF9-6841AB2B553B} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {6627AC05-E61C-4DC8-814B-C75366288DA4} = {789C06F6-F79B-4A6A-AC72-13672271A808} - {6AE486CE-8803-4259-B8C3-33600FEFD95C} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {67D1B7E1-FF93-474B-8964-10381D1E5B7A} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {EEA7B6C1-01A0-465D-8806-5E48680F8E70} = {C941BE21-DBF2-46EA-879E-892758A3AE53} - {F59924C0-67E3-451B-B4CF-F82073CD8EE8} = {EEA7B6C1-01A0-465D-8806-5E48680F8E70} - {ADC9955B-7C4B-4657-B6F8-DDD569315D2A} = {C941BE21-DBF2-46EA-879E-892758A3AE53} - {85D6968F-F3B7-40FC-8491-524D77FD858E} = {ADC9955B-7C4B-4657-B6F8-DDD569315D2A} - {CA3BE482-7936-432C-878F-F856719E326E} = {EEA7B6C1-01A0-465D-8806-5E48680F8E70} - {AA3FA811-5810-40D4-ADB4-C9688EA4FA79} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {1C96F1C2-23C7-40C7-AA33-BB16311DFDDE} = {C941BE21-DBF2-46EA-879E-892758A3AE53} - {742421E3-AE7F-46DB-B569-5F3C5E261940} = {1C96F1C2-23C7-40C7-AA33-BB16311DFDDE} - {5A546BA8-1514-47DD-A850-0F8B9C2B8F79} = {1C96F1C2-23C7-40C7-AA33-BB16311DFDDE} - {5262CC9E-421C-4A37-AE9D-F4408CA9A78C} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - {58E9B8A2-9106-4F63-A7B9-CC74F907FAA4} = {2237B275-4DF9-492E-A555-E38CE0DC0E12} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {8E6A453D-7E2E-4504-B5F6-474D9AEA13B8} - EndGlobalSection -EndGlobal diff --git a/README.md b/README.md index 415055a5..2cab2a20 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Why did I build my own? Well, I didn't know of these tools before I started and ### Check it out -The tool is available here: https://oilfieldplanner.factorygamefan.com/oil-field. The planner runs entirely in your browser via WebAssembly (a Rust port of the C# planner, in a background worker), so no blueprint data leaves your machine. There is an "Add sample" button (which drops one of my test blueprints in) and "View in FBE" buttons to view the input and output blueprints. Give it a try and let me know what you think! +The tool is available here: https://oilfieldplanner.factorygamefan.com/oil-field. The planner is written in Rust and runs entirely in your browser as WebAssembly, in a background worker, so no blueprint data leaves your machine. There is an "Add sample" button (which drops one of my test blueprints in) and "View in FBE" buttons to view the input and output blueprints. Give it a try and let me know what you think! I've also attached a video of the thing in action. @@ -38,17 +38,17 @@ https://user-images.githubusercontent.com/94054/224560733-35ca401f-ae51-46a9-951 ### Methodology -I captured a set of test blueprints from my own saves and used them for a scoring data set (61 of them today, in [`small-list.txt`](test/FactorioTools.Test/OilField/small-list.txt)). If the beacon or pipe planning algorithms I tried produced better results on this data set, I considered it a better algorithm and moved forward with the idea. This iterative process allowed me to come up with several algorithm variants that are all used. +I captured a set of test blueprints from my own saves and used them for a scoring data set (61 of them today, in [`small-list.txt`](testdata/oil-field/small-list.txt)). If the beacon or pipe planning algorithms I tried produced better results on this data set, I considered it a better algorithm and moved forward with the idea. This iterative process allowed me to come up with several algorithm variants that are all used. I re-implemented FBE's pipe planner and beacon planner (with some tweaks) and wrote my own algorithms for pipe placement, pipe straitening, beacon placement, and electric pole placement. I did not try FBE's electric pole algorithm since it's not that important to have the fewest electric poles. My pipe planning algorithm is called "connected centers". It first tries to find pumpjacks that should be directly connected with lines of pipes ("trunks") and then connects these groups incrementally. My best performing "connected centers" variant uses Dr. Chris C. N. Chu's FLUTE algorithm, which is used for rectilinear Steiner minimal tree generation (RSMT) problem. RSMT is an NP-hard problem so getting a truly optimal solution isn't very feasible especially when considering multiple parameters like pipes, beacons, and poles. -FLUTE is used under Dr. Chu's Attribution Assurance License ([`submodules/FluteSharp/flute-c/license.txt`](https://github.com/joelverhagen/FluteSharp/blob/main/flute-c/license.txt)). The license asks that every launch of a program built from it shows his name, Iowa State University and his web page, so the site's footer carries that credit. Anyone planning commercial use must tell the author first. +FLUTE is used under Dr. Chu's Attribution Assurance License ([`rust/flute/LICENSE-FLUTE`](rust/flute/LICENSE-FLUTE)). The license asks that every launch of a program built from it shows his name, Iowa State University and his web page, so the site's footer carries that credit. Anyone planning commercial use must tell the author first. The planner's copy of FLUTE, in [`rust/flute/`](rust/flute/), is a Rust port of [FluteSharp](https://github.com/joelverhagen/FluteSharp), my C# version (MIT, [`LICENSE-FluteSharp`](rust/flute/LICENSE-FluteSharp)). My beacon planning algorithm is called "snug" and it tries to place beacons as close to each other as possible after preferring starting locations that cover the most pumpjacks. -The tool tries up to 16 different planning routines and returns the best one to the user. Even for larger oil fields (20+ pumps) it generally completes in less than 100ms. The tool shows the quality of the other plans so you can see how each algorithm did. +The tool tries up to 16 different planning routines and returns the best one to the user. On the live site, the four test fields with 20 or more pumpjacks (20 to 58 of them) plan in 131 to 402 ms. The tool shows the quality of the other plans so you can see how each algorithm did. ![image](https://user-images.githubusercontent.com/94054/224569867-936af0b4-28e1-4c44-9ad5-af33e89f8236.png) @@ -80,36 +80,16 @@ I want to thank [teoxoy](https://github.com/teoxoy), author of the awesome Facto ### Planner quality -See [`Score.HasExpectedScore.verified.txt`](test/FactorioTools.Test/OilField/Score.HasExpectedScore.verified.txt) for results. There is a table at the bottom. +See [`Score.HasExpectedScore.verified.txt`](testdata/oil-field/Score.HasExpectedScore.verified.txt) for results. There is a table at the bottom. ### Runtime performance -``` ini -BenchmarkDotNet v0.13.11, Windows 11 (10.0.22621.2861/22H2/2022Update/SunValley2) -AMD Ryzen 9 3950X, 1 CPU, 32 logical and 16 physical cores -.NET SDK 8.0.100 - [Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2 - DefaultJob : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2 -``` - -| Method | Mean | Error | StdDev | -| ----------------------------------------- | -------: | -------: | -------: | -| MediumElectricPole_NoBeacon_NoUnderground | 17.43 ms | 0.090 ms | 0.085 ms | -| SmallElectricPole_Beacon_Underground | 31.82 ms | 0.122 ms | 0.114 ms | -| MediumElectricPole_Beacon_Underground | 31.78 ms | 0.292 ms | 0.273 ms | -| BigElectricPole_Beacon_Underground | 69.82 ms | 1.009 ms | 0.944 ms | -| Substation_Beacon_Underground | 33.92 ms | 0.199 ms | 0.186 ms | - -### Lua performance log - -This is the time to run the planner sample with Lua 5.2.4 on my desktop machine. System specs are the same as the .NET performance benchmark above. - -Reproduced using commit `3822074caaff18d11fa56961707d41a7ebca68b3` of [CSharp.lua](https://github.com/yanghuan/CSharp.lua). - -- `b984d3c050b8f16135197e457bc461b662ac04e3`: 7.08 seconds (original working implementation) -- `6c1537e175b3b45879fe02d4b37ccce7ff349188`: 5.20 seconds (int based Location hash set, struct hash set is slow) -- `8964b81944476f3f76a55186fdadabbf4a357230`: 2.20 seconds (int based Location dictionary, struct dictionary key is slow) -- `fd55e01fd688e6bee033f96cbca7e24d3570de09`: 0.82 seconds (class instead struct for Location, struct does lots of copies) -- `362caba6117438e27693289e76baeaea3ac9bb5f`: 0.74 seconds (don't use yield return, reduce Lua coroutines) -- `596eb1bd1c808a48693d3239b412d05c8bff3cb9`: 0.71 seconds (don't use LINQ, reduce Lua coroutines) -- `5bd8888af55dc201984de52ccae3ecbb5c646b89`: 0.69 seconds (eliminate named tuples, try/catch) +The planner was first written in C#, and the site ran it in the browser on .NET compiled to WebAssembly. It is now written in Rust. These numbers compare the two in headless Chromium on a Linux desktop, with the site's default options: + +| What | .NET (before) | Rust (now) | +| --- | ---: | ---: | +| Planner download (brotli) | 3.15 MiB | 244 KB | +| First plan after the page loads | 1,146 ms | 312 ms | +| Largest field in [`big-list.txt`](testdata/oil-field/big-list.txt) | 26.7 s | 7.6 s | + +The .NET planner ran on the page itself, so the page froze until the plan was done. The Rust planner runs in a Web Worker (a background thread), so the page keeps responding while it works. The first two rows come from the live site, and the largest field was timed on a local build of the same code. More details are in the [write-up of the port](https://claude.ai/artifact/1514Dr6iCGjyVXgENCxEuX). diff --git a/docker-build.sh b/docker-build.sh deleted file mode 100755 index 91ee0780..00000000 --- a/docker-build.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash -# -# Run dotnet inside the pinned .NET 10 SDK container, so you can build and test -# without installing the .NET 10 SDK locally (handy on machines that only have a -# different SDK than the global.json pin). Works with OrbStack or Docker Desktop. -# -# Usage: ./docker-build.sh [dotnet args...] -# ./docker-build.sh # default: test the core dev loop -# ./docker-build.sh build # build the solution (needs wasm-tools, see note) -# ./docker-build.sh build src/FactorioTools/FactorioTools.csproj -c Release /p:UseLuaSettings=true -# ./docker-build.sh run --project src/FactorioTools.Cli -- oil-field sample -# -# Notes: -# - Git submodules are read from your checkout; run `git submodule update --init -# --recursive` first if they are missing. -# - A full-solution `build` pulls in BrowserWasm/BlazorWebApp, which need the -# wasm-tools workload. Prepend a workload restore for that, e.g.: -# ./docker-build.sh bash -c "dotnet workload restore && dotnet build -c Release" -# - NuGet packages are cached on the host (override with FACTORIO_NUGET_CACHE). -# - Runs as the current user so build artifacts in the tree are not root-owned. -set -euo pipefail - -REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -NUGET_CACHE="${FACTORIO_NUGET_CACHE:-$HOME/.cache/factorio-nuget}" -SDK_IMAGE="${FACTORIO_SDK_IMAGE:-mcr.microsoft.com/dotnet/sdk:10.0}" - -mkdir -p "$NUGET_CACHE" - -# Default to the core dev loop (core lib + serialization + Verify snapshots). -if [ "$#" -eq 0 ]; then - set -- test test/FactorioTools.Test/FactorioTools.Test.csproj -c Release -fi - -# If the first arg is a literal program (e.g. `bash`), run it as-is; otherwise -# treat the args as a `dotnet` subcommand. -case "${1:-}" in - bash|sh) ;; - *) set -- dotnet "$@" ;; -esac - -exec docker run --rm \ - --user "$(id -u):$(id -g)" \ - -e HOME=/tmp -e NUGET_PACKAGES=/nuget \ - -e DOTNET_CLI_TELEMETRY_OPTOUT=1 -e DOTNET_NOLOGO=1 \ - -v "$REPO_ROOT":/src -w /src \ - -v "$NUGET_CACHE":/nuget \ - "$SDK_IMAGE" \ - "$@" diff --git a/global.json b/global.json deleted file mode 100644 index 9b4b49b4..00000000 --- a/global.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "sdk": { - "version": "10.0.402", - "rollForward": "latestMajor", - "allowPrerelease": false - } -} \ No newline at end of file diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c1debe95..ecebe353 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,5 +1,5 @@ -# Rust port of the oil-field planner (#138). It grows stage by stage beside the C#, -# which stays the live planner until the port matches every snapshot. +# The oil-field planner, ported from C# to Rust (#138). It is the only planner now: the +# site runs its WASM build, and stage 13b removed the C#. [workspace] resolver = "3" members = ["dotnet-compat", "planner", "delaunator-net", "flute", "blueprint", "snapshots", "contract", "planner-wasm"] diff --git a/rust/blueprint/src/write.rs b/rust/blueprint/src/write.rs index 851b047d..7e42fbe5 100644 --- a/rust/blueprint/src/write.rs +++ b/rust/blueprint/src/write.rs @@ -438,7 +438,7 @@ mod tests { /// `tests/factorio_oracle.rs` checks the rest of it. fn factorio_oracle_path() -> std::path::PathBuf { std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../test/FactorioTools.Test/OilField/factorio-oracle.json") + .join("../../testdata/oil-field/factorio-oracle.json") } /// `FactorioOracleTest.EntityNameToSizeMatchesTheDerivedCollisionBoxFootprint`: each diff --git a/rust/blueprint/tests/parse_blueprint.rs b/rust/blueprint/tests/parse_blueprint.rs index a8e2d9fe..f5d76c7a 100644 --- a/rust/blueprint/tests/parse_blueprint.rs +++ b/rust/blueprint/tests/parse_blueprint.rs @@ -124,8 +124,7 @@ fn reads_blueprint_files_like_read_all_lines() { /// Every corpus blueprint parses. #[test] fn parses_the_whole_corpus() { - let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../../test/FactorioTools.Test/OilField"); + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../testdata/oil-field"); for list in ["small-list.txt", "big-list.txt"] { let lines = blueprint::read_blueprint_file(root.join(list)).unwrap(); assert!(!lines.is_empty()); diff --git a/rust/blueprint/tests/write_blueprint.rs b/rust/blueprint/tests/write_blueprint.rs index 11ec3b2a..b348078f 100644 --- a/rust/blueprint/tests/write_blueprint.rs +++ b/rust/blueprint/tests/write_blueprint.rs @@ -10,8 +10,8 @@ use planner::options::OilFieldOptions; use planner::quality::Quality; fn first_small_list_blueprint() -> planner::data::Blueprint { - let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../../test/FactorioTools.Test/OilField/small-list.txt"); + let path = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../testdata/oil-field/small-list.txt"); let lines = blueprint::read_blueprint_file(path).unwrap(); parse_blueprint(&lines[0]).unwrap() } diff --git a/rust/contract/FIXTURES.md b/rust/contract/FIXTURES.md index e44e2346..210f795f 100644 --- a/rust/contract/FIXTURES.md +++ b/rust/contract/FIXTURES.md @@ -1,16 +1,11 @@ # contract fixtures -Recorded from the C# contract by `oracle/`, so the Rust tests can check `rust/contract` (and -through it `rust/blueprint`'s writer) without .NET (#138). CI reruns the recorder on the -pinned SDK and fails if the committed file drifts. +Recorded from the C# contract by a recorder in `oracle/`, so the Rust tests can check +`rust/contract` (and through it `rust/blueprint`'s writer) without .NET (#138). -**Regenerate** (from the repo root, with submodules checked out): - -```bash -./docker-build.sh run --project rust/contract/oracle -c Release -- rust/contract/fixtures -``` - -The output is deterministic. +**These fixtures are frozen inputs.** They were recorded from the C# planner at commit +`0532d923`. Stage 13b (#138) deleted the C# and the recorder with it, so nothing can +regenerate them now. To see the recorder, check out that commit. ## `contract.jsonl` diff --git a/rust/contract/oracle/ContractOracle.csproj b/rust/contract/oracle/ContractOracle.csproj deleted file mode 100644 index c7a6fc0a..00000000 --- a/rust/contract/oracle/ContractOracle.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - - Exe - net10.0 - enable - enable - true - Knapcode.FactorioTools.ContractOracle - - - - - - - diff --git a/rust/contract/oracle/Program.cs b/rust/contract/oracle/Program.cs deleted file mode 100644 index be89c651..00000000 --- a/rust/contract/oracle/Program.cs +++ /dev/null @@ -1,275 +0,0 @@ -// Records the C# JSON contract for the Rust port (#138). Writes contract.jsonl: one line per -// call of PlanOrchestrator.PlanJson or NormalizeJson with the exact request text and the -// exact response text (or the exception the call threw), plus a few direct -// GridToBlueprintString calls with avoid entities, which only the CLI reaches. -// Usage: ContractOracle - -using System.IO.Compression; -using System.Text; -using System.Text.Json; -using System.Text.Json.Nodes; -using Knapcode.FactorioTools.Contract; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -var outputDir = Path.GetFullPath(args.Length > 0 ? args[0] : "."); -var repoRoot = FindRepoRoot(outputDir) ?? FindRepoRoot(Directory.GetCurrentDirectory()) - ?? throw new InvalidOperationException("Could not find the repository root (FactorioTools.sln)."); -var oilFieldTestDir = Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField"); -var smallList = ParseBlueprint.ReadBlueprintFile(Path.Combine(oilFieldTestDir, "small-list.txt")); -var bigList = ParseBlueprint.ReadBlueprintFile(Path.Combine(oilFieldTestDir, "big-list.txt")); - -Directory.CreateDirectory(outputDir); -var path = Path.Combine(outputDir, "contract.jsonl"); -var temp = path + ".tmp"; -var counts = new SortedDictionary(StringComparer.Ordinal); - -using (var writer = new StreamWriter(temp, append: false, new UTF8Encoding(false)) { NewLine = "\n" }) -{ - void Plan(string name, string request) => Write(writer, counts, name, "plan", request, () => PlanOrchestrator.PlanJson(request)); - void Normalize(string name, string request) => Write(writer, counts, name, "normalize", request, () => PlanOrchestrator.NormalizeJson(request)); - - string Serialize(OilFieldPlanRequest request) => JsonSerializer.Serialize(request, ContractJson.Options); - - // 1. The smallest request: only a blueprint, so every option takes its default. - for (var i = 0; i < smallList.Count; i++) - { - Plan($"small-{i:D4}_default", new JsonObject { ["blueprint"] = smallList[i] }.ToJsonString()); - } - - // 2. Full option objects, the way the site sends them. - var variants = new (string Name, int Step, int Offset, Action Apply)[] - { - ("small-pole", 3, 0, r => CopyOptions(OilFieldOptions.ForSmallElectricPole, r)), - ("big-pole", 3, 1, r => CopyOptions(OilFieldOptions.ForBigElectricPole, r)), - ("substation", 3, 2, r => CopyOptions(OilFieldOptions.ForSubstation, r)), - ("no-beacons-no-underground", 4, 1, r => { r.AddBeacons = false; r.UseUndergroundPipes = false; }), - ("no-overlap", 5, 2, r => r.OverlapBeacons = false), - ("heat", 6, 0, r => { r.AddHeatPipes = true; r.AddBeacons = false; }), - ("heat-beacons", 6, 3, r => r.AddHeatPipes = true), - ("quality", 7, 1, r => - { - r.PumpjackQuality = Quality.Legendary; - r.BeaconQuality = Quality.Rare; - r.ElectricPoleQuality = Quality.Uncommon; - r.PumpjackModuleQuality = Quality.Epic; - r.BeaconModuleQuality = Quality.Legendary; - }), - ("fbe-offset", 8, 2, r => r.AddFbeOffset = true), - ("validate", 9, 4, r => r.ValidateSolution = true), - ("modules", 10, 5, r => - { - r.PumpjackModules = new Dictionary { ["speed-module-3"] = 1, ["efficiency-module-3"] = 1 }; - r.BeaconModules = new Dictionary(); - }), - ("all-strategies", 11, 6, r => - { - r.PipeStrategies = OilFieldOptions.AllPipeStrategies.ToList(); - r.BeaconStrategies = OilFieldOptions.AllBeaconStrategies.ToList(); - }), - ("no-poles-no-optimize", 12, 7, r => { r.AddElectricPoles = false; r.OptimizePipes = false; }), - }; - - foreach (var (variantName, step, offset, apply) in variants) - { - for (var i = offset; i < smallList.Count; i += step) - { - var request = new OilFieldPlanRequest { Blueprint = smallList[i] }; - apply(request); - Plan($"small-{i:D4}_{variantName}", Serialize(request)); - } - } - - // 3. A big-list sample with defaults. - for (var i = 0; i < bigList.Count; i += 50) - { - Plan($"big-{i:D4}_default", new JsonObject { ["blueprint"] = bigList[i] }.ToJsonString()); - } - - // 4. How requests are read: case-insensitive names, numbers in strings, enum spellings, - // unknown and repeated properties. - var bp = JsonSerializer.Serialize(smallList[1]); - Plan("read_pascal-case-names", $"{{\"Blueprint\":{bp},\"AddBeacons\":false,\"ElectricPoleEntityName\":\"small-electric-pole\",\"ElectricPoleSupplyWidth\":5,\"ElectricPoleSupplyHeight\":5,\"ElectricPoleWireReach\":7.5}}"); - Plan("read_upper-case-names", $"{{\"BLUEPRINT\":{bp},\"USEUNDERGROUNDPIPES\":false}}"); - Plan("read_numbers-in-strings", $"{{\"blueprint\":{bp},\"electricPoleSupplyWidth\":\"5\",\"electricPoleSupplyHeight\":\"5\",\"electricPoleWireReach\":\"7.5\",\"beaconWidth\":\"3\"}}"); - Plan("read_enum-lowercase", $"{{\"blueprint\":{bp},\"pipeStrategies\":[\"fbe\",\"connectedcentersflute\"],\"beaconStrategies\":[\"snug\"]}}"); - Plan("read_enum-integers", $"{{\"blueprint\":{bp},\"pipeStrategies\":[1,4],\"beaconStrategies\":[2],\"pumpjackQuality\":5}}"); - Plan("read_quality-undefined-integer", $"{{\"blueprint\":{bp},\"pumpjackQuality\":42,\"beaconQuality\":\"Rare\"}}"); - Plan("read_unknown-property", $"{{\"blueprint\":{bp},\"somethingElse\":{{\"a\":[1,2,3]}},\"another\":null}}"); - Plan("read_repeated-property", $"{{\"blueprint\":{bp},\"addBeacons\":true,\"addBeacons\":false}}"); - Plan("read_wire-reach-integer", $"{{\"blueprint\":{bp},\"electricPoleWireReach\":10}}"); - Plan("read_wire-reach-fraction", $"{{\"blueprint\":{bp},\"electricPoleWireReach\":10.25}}"); - Plan("read_whitespace", $" {{ \"blueprint\" : {bp} ,\n\t\"addBeacons\" : false }} "); - Plan("read_escaped-name", $"{{\"blueprint\":{bp},\"heatPipeEntityName\":\"heat\\u002Dpipe <&'+>\\u00e9\",\"addHeatPipes\":true,\"addBeacons\":false}}"); - Plan("read_empty-strategy-lists", $"{{\"blueprint\":{bp},\"beaconStrategies\":[]}}"); - Plan("read_module-dictionary-order", $"{{\"blueprint\":{bp},\"beaconModules\":{{\"speed-module-3\":1,\"productivity-module-3\":1,\"speed-module-3\":3}}}}"); - - // 5. Normalize. - for (var i = 0; i < smallList.Count; i++) - { - Normalize($"small-{i:D4}", new JsonObject { ["blueprint"] = smallList[i] }.ToJsonString()); - } - - for (var i = 0; i < bigList.Count; i += 25) - { - Normalize($"big-{i:D4}", new JsonObject { ["blueprint"] = bigList[i] }.ToJsonString()); - } - - // 6. Bad input. Most become an error envelope; some throw out of the call. - var book = Encode("{\"blueprint_book\":{\"blueprints\":[],\"item\":\"blueprint-book\",\"label\":\"x\",\"active_index\":0,\"version\":0}}"); - var noPumpjacks = Encode("{\"blueprint\":{\"icons\":[],\"entities\":[{\"entity_number\":1,\"name\":\"pipe\",\"position\":{\"x\":0.5,\"y\":0.5}}],\"item\":\"blueprint\",\"version\":0}}"); - var diagonal = Encode("{\"blueprint\":{\"icons\":[],\"entities\":[{\"entity_number\":1,\"name\":\"pumpjack\",\"position\":{\"x\":0.5,\"y\":0.5},\"direction\":2}],\"item\":\"blueprint\",\"version\":562949955518464}}"); - var notJson = Encode("this is not json"); - var nullRoot = Encode("null"); - var noBlueprint = Encode("{}"); - var badBlueprints = new (string Name, string Value)[] - { - ("empty", ""), - ("not-a-blueprint", "not-a-blueprint"), - ("wrong-version-byte", "1eJyMj70OwjAMhN"), - ("bad-base64", "0!!!!"), - ("not-zlib", "0" + Convert.ToBase64String(Encoding.UTF8.GetBytes("hello world"))), - ("not-json", notJson), - ("null-root", nullRoot), - ("no-blueprint", noBlueprint), - ("book", book), - ("no-pumpjacks", noPumpjacks), - ("diagonal-pumpjack", diagonal), - }; - - foreach (var (name, value) in badBlueprints) - { - var quoted = JsonSerializer.Serialize(value); - Plan($"bad_{name}", $"{{\"blueprint\":{quoted}}}"); - Normalize($"bad_{name}", $"{{\"blueprint\":{quoted}}}"); - } - - var badRequests = new (string Name, string Value)[] - { - ("invalid-json", "{"), - ("json-null", "null"), - ("json-array", "[]"), - ("missing-blueprint", "{}"), - ("null-blueprint", "{\"blueprint\":null}"), - ("bool-in-string", $"{{\"blueprint\":{bp},\"addBeacons\":\"false\"}}"), - ("unknown-quality", $"{{\"blueprint\":{bp},\"pumpjackQuality\":\"Mythic\"}}"), - ("fraction-for-int", $"{{\"blueprint\":{bp},\"beaconWidth\":3.5}}"), - ("trailing-comma", $"{{\"blueprint\":{bp},}}"), - ("empty-pipe-strategies", $"{{\"blueprint\":{bp},\"pipeStrategies\":[]}}"), - }; - - foreach (var (name, value) in badRequests) - { - Plan($"bad-request_{name}", value); - Normalize($"bad-request_{name}", value); - } - - // 7. GridToBlueprintString with avoid entities, which the contract never asks for. - for (var i = 0; i < 6; i++) - { - var blueprint = ParseBlueprint.Execute(smallList[i]); - var pumpjack = blueprint.Entities.First(e => e.Name == EntityNames.Vanilla.Pumpjack); - var avoid = new List - { - new(pumpjack.Position.X + 5, pumpjack.Position.Y + 5), - new(pumpjack.Position.X - 6, pumpjack.Position.Y + 1), - }; - var options = OilFieldOptions.ForMediumElectricPole; - var record = new JsonObject - { - ["name"] = $"grid_small-{i:D4}_avoid", - ["op"] = "grid_to_blueprint_string", - ["blueprint"] = smallList[i], - ["avoid"] = new JsonArray(avoid.Select(a => (JsonNode)new JsonArray(a.X, a.Y)).ToArray()), - ["add_fbe_offset"] = i % 2 == 1, - }; - try - { - var result = Planner.Execute(options, blueprint, avoid); - var text = GridToBlueprintString.Execute(result.Context, addFbeOffset: i % 2 == 1, addAvoidEntities: true); - record["outcome"] = "ok"; - record["json"] = Decode(text); - } - catch (Exception ex) - { - record["outcome"] = "threw"; - record["exception"] = ex.GetType().Name; - record["message"] = ex.Message; - } - - writer.WriteLine(record.ToJsonString()); - counts["grid_to_blueprint_string"] = (counts.TryGetValue("grid_to_blueprint_string", out var gridCount) ? gridCount : 0) + 1; - } -} - -File.Move(temp, path, overwrite: true); -Console.WriteLine($"contract.jsonl: {string.Join(", ", counts.Select(x => $"{x.Key}={x.Value}"))}, {new FileInfo(path).Length} bytes"); -return 0; - -static void Write(StreamWriter writer, SortedDictionary counts, string name, string op, string request, Func call) -{ - var record = new JsonObject - { - ["name"] = name, - ["op"] = op, - ["request"] = request, - }; - try - { - record["outcome"] = "ok"; - record["response"] = call(); - } - catch (Exception ex) - { - record["outcome"] = "threw"; - record["exception"] = ex.GetType().Name; - record["message"] = ex.Message; - } - - writer.WriteLine(record.ToJsonString()); - var key = $"{op}:{record["outcome"]}"; - counts[key] = (counts.TryGetValue(key, out var count) ? count : 0) + 1; -} - -static void CopyOptions(OilFieldOptions source, OilFieldPlanRequest target) -{ - target.ElectricPoleEntityName = source.ElectricPoleEntityName; - target.ElectricPoleSupplyWidth = source.ElectricPoleSupplyWidth; - target.ElectricPoleSupplyHeight = source.ElectricPoleSupplyHeight; - target.ElectricPoleWireReach = source.ElectricPoleWireReach; - target.ElectricPoleWidth = source.ElectricPoleWidth; - target.ElectricPoleHeight = source.ElectricPoleHeight; -} - -static string Encode(string json) -{ - using var output = new MemoryStream(); - using (var zlib = new ZLibStream(output, CompressionLevel.Optimal)) - { - var bytes = Encoding.UTF8.GetBytes(json); - zlib.Write(bytes, 0, bytes.Length); - } - - return "0" + Convert.ToBase64String(output.ToArray()); -} - -static string Decode(string blueprintString) -{ - var bytes = Convert.FromBase64String(blueprintString.Substring(1)); - using var input = new ZLibStream(new MemoryStream(bytes), CompressionMode.Decompress); - using var reader = new StreamReader(input, Encoding.UTF8); - return reader.ReadToEnd(); -} - -static string? FindRepoRoot(string start) -{ - for (var dir = new DirectoryInfo(start); dir is not null; dir = dir.Parent) - { - if (File.Exists(Path.Combine(dir.FullName, "FactorioTools.sln"))) - { - return dir.FullName; - } - } - - return null; -} diff --git a/rust/contract/tests/csharp_tests.rs b/rust/contract/tests/csharp_tests.rs index 5f866ea2..0ef45669 100644 --- a/rust/contract/tests/csharp_tests.rs +++ b/rust/contract/tests/csharp_tests.rs @@ -63,7 +63,7 @@ fn plan_request_round_trips_with_string_enums() { } fn oil_field_test_dir() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../test/FactorioTools.Test/OilField") + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../testdata/oil-field") } /// `CleanBlueprintTest.VerifySameBlueprint`: the corpus is already normalized, so cleaning a diff --git a/rust/delaunator-net/FIXTURES.md b/rust/delaunator-net/FIXTURES.md index 5e173b4f..c6023bfc 100644 --- a/rust/delaunator-net/FIXTURES.md +++ b/rust/delaunator-net/FIXTURES.md @@ -1,16 +1,12 @@ # delaunator-net fixtures -`fixtures/delaunator.jsonl` holds triangulations recorded from DelaunatorSharp by `oracle/`, -so `tests/fixtures.rs` can check the Rust port without .NET (#138). +`fixtures/delaunator.jsonl` holds triangulations recorded from DelaunatorSharp by a recorder +in `oracle/`, so `tests/fixtures.rs` can check the Rust port without .NET (#138). The random +cases used `new Random(1)`. -**Regenerate**, from the repo root with submodules checked out: - -```bash -./docker-build.sh run --project rust/delaunator-net/oracle -c Release -- rust/delaunator-net/fixtures -``` - -The output is deterministic (the random cases use `new Random(1)`), so a rerun on the same -SDK gives the same bytes. +**These fixtures are frozen inputs.** They were recorded from the C# planner's copy of +DelaunatorSharp at commit `0532d923`. Stage 13b (#138) deleted the C# and the recorder with +it, so nothing can regenerate them now. To see the recorder, check out that commit. ## Format diff --git a/rust/delaunator-net/README.md b/rust/delaunator-net/README.md index a270b88e..d83b1a57 100644 --- a/rust/delaunator-net/README.md +++ b/rust/delaunator-net/README.md @@ -9,4 +9,5 @@ It copies the C# line for line rather than using the `delaunator` crate. The pla lie on a grid, so many of them sit on the same circle, and the triangle order then depends on details such as the seed choice, the custom quicksort and the non-robust predicates. Those details decide which pipes the planner draws (#138). `tests/fixtures.rs` checks the port -against triangulations recorded from the C# (see `FIXTURES.md`). +against frozen triangulations recorded from the C# before stage 13b removed it (see +`FIXTURES.md`). diff --git a/rust/delaunator-net/oracle/DelaunatorOracle.csproj b/rust/delaunator-net/oracle/DelaunatorOracle.csproj deleted file mode 100644 index 00fb9a5d..00000000 --- a/rust/delaunator-net/oracle/DelaunatorOracle.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - - Exe - net10.0 - enable - enable - true - - - - - - - diff --git a/rust/delaunator-net/oracle/Program.cs b/rust/delaunator-net/oracle/Program.cs deleted file mode 100644 index 25781563..00000000 --- a/rust/delaunator-net/oracle/Program.cs +++ /dev/null @@ -1,213 +0,0 @@ -// Records what DelaunatorSharp returns for the point sets the planner hands it, plus -// synthetic sets chosen to hit ties, so the Rust port in rust/delaunator-net can be -// checked without .NET. See ../FIXTURES.md. - -using System.Text; -using System.Text.Json; -using DelaunatorSharp; -using Knapcode.FactorioTools.OilField; - -if (args.Length != 1) -{ - Console.Error.WriteLine("usage: DelaunatorOracle "); - return 2; -} - -var outputDir = Path.GetFullPath(args[0]); -Directory.CreateDirectory(outputDir); -var repoRoot = FindRepoRoot(outputDir) ?? FindRepoRoot(Directory.GetCurrentDirectory()) - ?? throw new InvalidOperationException("Could not find the repository root."); -var listDir = Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField"); - -var cases = new List<(string Name, int[][] Points)>(); - -// 1. The pumpjack centers as the planner computes them, in context.Centers order. The -// connected-centers strategies (AddPipes.3.ConnectedCenters.DT.cs) and FBE -// (PointsToLines(context.Centers, sort: false)) both triangulate this array as is. -// 2. The same centers sorted by X then Y, as PointsToLines(..., sort: true) does, when -// PointsToLines would actually reach Delaunator (more than 2 points, not collinear). -foreach (var (prefix, file) in new[] { ("small", "small-list.txt"), ("big", "big-list.txt") }) -{ - var blueprints = ParseBlueprint.ReadBlueprintFile(Path.Combine(listDir, file)); - for (var i = 0; i < blueprints.Count; i++) - { - List centers; - try - { - var blueprint = ParseBlueprint.Execute(blueprints[i]); - var context = InitializeContext.Execute(OilFieldOptions.ForMediumElectricPole, blueprint, Array.Empty()); - centers = context.Centers; - } - catch (Exception ex) - { - Console.Error.WriteLine($"{prefix}-{i:D4}: skipped, {ex.GetType().Name}: {ex.Message}"); - continue; - } - - cases.Add(($"{prefix}-{i:D4}-centers", ToArray(centers))); - - var sorted = centers.ToList(); - sorted.Sort((a, b) => - { - var c = a.X.CompareTo(b.X); - return c != 0 ? c : a.Y.CompareTo(b.Y); - }); - if (sorted.Count > 2 && !Helpers.AreLocationsCollinear(sorted)) - { - cases.Add(($"{prefix}-{i:D4}-sorted", ToArray(sorted))); - } - } -} - -// 3. Synthetic sets. -cases.Add(("too-few-0", Array.Empty())); -cases.Add(("too-few-1", new[] { new[] { 3, 4 } })); -cases.Add(("too-few-2", new[] { new[] { 0, 0 }, new[] { 5, 5 } })); -cases.Add(("collinear-horizontal-3", Line(3, 1, 0))); -cases.Add(("collinear-vertical-5", Line(5, 0, 1))); -cases.Add(("collinear-diagonal-30", Line(30, 1, 1))); -cases.Add(("collinear-spaced-12", Line(12, 3, 0))); -cases.Add(("identical-5", Enumerable.Repeat(new[] { 7, 7 }, 5).ToArray())); -cases.Add(("duplicates-square", new[] { new[] { 0, 0 }, new[] { 4, 0 }, new[] { 0, 0 }, new[] { 4, 4 }, new[] { 0, 4 }, new[] { 4, 4 }, new[] { 2, 2 } })); -cases.Add(("right-triangle-circumcenter-is-a-point", new[] { new[] { 0, 0 }, new[] { 2, 0 }, new[] { 0, 2 }, new[] { 1, 1 }, new[] { 2, 2 } })); -cases.Add(("circle-r5", CirclePoints(5))); -cases.Add(("circle-r25", CirclePoints(25))); -cases.Add(("circle-r65", CirclePoints(65))); - -for (var k = 2; k <= 12; k++) -{ - cases.Add(($"lattice-{k}x{k}", Lattice(k, k, 1))); - cases.Add(($"lattice-{k}x{k}-spacing3", Lattice(k, k, 3))); -} - -cases.Add(("lattice-3x9", Lattice(3, 9, 1))); -cases.Add(("lattice-9x3", Lattice(9, 3, 1))); -cases.Add(("lattice-2x20", Lattice(2, 20, 1))); - -var random = new Random(1); -for (var k = 3; k <= 10; k++) -{ - for (var r = 0; r < 5; r++) - { - var points = Lattice(k, k, 3); - random.Shuffle(points); - cases.Add(($"lattice-{k}x{k}-shuffled-{r}", points)); - } -} - -foreach (var range in new[] { 6, 12, 40, 100 }) -{ - for (var r = 0; r < 100; r++) - { - var n = random.Next(3, 61); - var points = new int[n][]; - for (var p = 0; p < n; p++) - { - points[p] = new[] { random.Next(range), random.Next(range) }; - } - - cases.Add(($"random-range{range}-{r:D3}-n{n}", points)); - } -} - -var path = Path.Combine(outputDir, "delaunator.jsonl"); -using (var stream = File.Create(path)) -{ - var newline = Encoding.UTF8.GetBytes("\n"); - foreach (var (name, points) in cases) - { - using (var writer = new Utf8JsonWriter(stream)) - { - writer.WriteStartObject(); - writer.WriteString("name", name); - writer.WriteStartArray("points"); - foreach (var p in points) - { - writer.WriteStartArray(); - writer.WriteNumberValue(p[0]); - writer.WriteNumberValue(p[1]); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - try - { - var delaunator = new Delaunator(points.Select(p => (IPoint)new DelaunatorSharp.Point(p[0], p[1])).ToArray()); - WriteInts(writer, "triangles", delaunator.Triangles); - WriteInts(writer, "halfedges", delaunator.Halfedges); - } - catch (Exception ex) - { - writer.WriteString("error", ex.GetType().Name); - } - - writer.WriteEndObject(); - } - - stream.Write(newline); - } -} - -Console.WriteLine($"delaunator.jsonl: {cases.Count} cases, {new FileInfo(path).Length} bytes"); -return 0; - -static void WriteInts(Utf8JsonWriter writer, string name, int[] values) -{ - writer.WriteStartArray(name); - foreach (var v in values) - { - writer.WriteNumberValue(v); - } - - writer.WriteEndArray(); -} - -static int[][] ToArray(IReadOnlyList locations) => locations.Select(l => new[] { l.X, l.Y }).ToArray(); - -static int[][] Line(int n, int dx, int dy) => Enumerable.Range(0, n).Select(i => new[] { i * dx, i * dy }).ToArray(); - -static int[][] Lattice(int w, int h, int spacing) -{ - var points = new List(); - for (var y = 0; y < h; y++) - { - for (var x = 0; x < w; x++) - { - points.Add(new[] { x * spacing, y * spacing }); - } - } - - return points.ToArray(); -} - -// Every integer point on the circle x^2 + y^2 = r^2, shifted to be non-negative. -static int[][] CirclePoints(int r) -{ - var points = new List(); - for (var x = -r; x <= r; x++) - { - for (var y = -r; y <= r; y++) - { - if (x * x + y * y == r * r) - { - points.Add(new[] { x + r, y + r }); - } - } - } - - return points.ToArray(); -} - -static string? FindRepoRoot(string start) -{ - for (var dir = new DirectoryInfo(start); dir is not null; dir = dir.Parent) - { - if (File.Exists(Path.Combine(dir.FullName, "FactorioTools.sln"))) - { - return dir.FullName; - } - } - - return null; -} diff --git a/rust/delaunator-net/src/lib.rs b/rust/delaunator-net/src/lib.rs index 4f5d5b96..9c135ad7 100644 --- a/rust/delaunator-net/src/lib.rs +++ b/rust/delaunator-net/src/lib.rs @@ -1,6 +1,6 @@ -//! A line-for-line port of `submodules/delaunator-sharp/DelaunatorSharp/Delaunator.cs`, -//! limited to what the oil-field planner uses: the constructor, `Triangles`, `Halfedges` -//! and `Hull`. +//! A line-for-line port of DelaunatorSharp's `DelaunatorSharp/Delaunator.cs` (a submodule +//! here until stage 13b), limited to what the oil-field planner uses: the constructor, +//! `Triangles`, `Halfedges` and `Hull`. //! //! Keep the arithmetic exactly as the C# writes it. The planner feeds in grid points, so //! many lie on one circle and many distances tie. Which triangles come out then depends diff --git a/rust/delaunator-net/tests/fixtures.rs b/rust/delaunator-net/tests/fixtures.rs index 7fea6dd8..9bfa65f5 100644 --- a/rust/delaunator-net/tests/fixtures.rs +++ b/rust/delaunator-net/tests/fixtures.rs @@ -1,5 +1,5 @@ -//! Replays `fixtures/delaunator.jsonl`, recorded from DelaunatorSharp by `oracle/`, and -//! checks every triangulation matches exactly. +//! Replays `fixtures/delaunator.jsonl`, recorded from DelaunatorSharp by `oracle/` (removed +//! in stage 13b, so the file is frozen), and checks every triangulation matches exactly. use delaunator_net::{Delaunator, DelaunatorError}; use serde_json::Value; diff --git a/rust/dotnet-compat/FIXTURES.md b/rust/dotnet-compat/FIXTURES.md index 66f8b51e..99b5873f 100644 --- a/rust/dotnet-compat/FIXTURES.md +++ b/rust/dotnet-compat/FIXTURES.md @@ -2,16 +2,12 @@ The planner's output depends on how .NET orders sets, dictionaries, priority queues and sorts when there are ties (#138). This crate copies that behavior. The fixtures here are -recorded from real .NET by `oracle/`, so the Rust tests can check the copy without .NET. +recorded from real .NET by a recorder in `oracle/`, so the Rust tests can check the copy +without .NET. Every scenario came from a fixed seed. -**Regenerate** (needs the .NET SDK, or `./docker-build.sh` from the repo root): - -```bash -./docker-build.sh run --project rust/dotnet-compat/oracle -c Release -- rust/dotnet-compat/fixtures -``` - -The output is deterministic: every scenario comes from a fixed seed, so a rerun on the -same SDK gives the same bytes. CI reruns it and fails if the committed files drift. +**These fixtures are frozen inputs.** They were recorded with the .NET SDK the C# planner +pinned at commit `0532d923`. Stage 13b (#138) deleted the C# and the recorder with it, so +nothing can regenerate them now. To see the recorder, check out that commit. ## Format diff --git a/rust/dotnet-compat/oracle/DotnetCompatOracle.csproj b/rust/dotnet-compat/oracle/DotnetCompatOracle.csproj deleted file mode 100644 index 3f39f1d6..00000000 --- a/rust/dotnet-compat/oracle/DotnetCompatOracle.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - - Exe - net10.0 - enable - enable - true - - - diff --git a/rust/dotnet-compat/oracle/Program.cs b/rust/dotnet-compat/oracle/Program.cs deleted file mode 100644 index 7d482c61..00000000 --- a/rust/dotnet-compat/oracle/Program.cs +++ /dev/null @@ -1,1077 +0,0 @@ -using System.Globalization; -using System.Text; - -namespace DotnetCompatOracle; - -// Writes the fixtures described in ../FIXTURES.md. Every file uses its own fixed seed, -// so a rerun on the same SDK gives the same bytes. -public static class Program -{ - public static int Main(string[] args) - { - if (args.Length != 1) - { - Console.Error.WriteLine("usage: DotnetCompatOracle "); - return 2; - } - - var outDir = args[0]; - Directory.CreateDirectory(outDir); - - Write(outDir, "hashset.jsonl", HashSetScenarios.Generate(new Random(1))); - Write(outDir, "dictionary.jsonl", DictionaryScenarios.Generate(new Random(2))); - Write(outDir, "priority_queue.jsonl", PriorityQueueScenarios.Generate(new Random(3))); - Write(outDir, "sort.jsonl", SortScenarios.Generate(new Random(4))); - Write(outDir, "math.jsonl", MathScenarios.Generate(new Random(5))); - return 0; - } - - private static void Write(string outDir, string fileName, IEnumerable scenarios) - { - var sb = new StringBuilder(); - var count = 0; - foreach (var s in scenarios) - { - sb.Append(s.ToJson()).Append('\n'); - count++; - } - - var path = Path.Combine(outDir, fileName); - File.WriteAllText(path, sb.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); - Console.WriteLine($"{fileName}: {count} scenarios, {new FileInfo(path).Length} bytes"); - } -} - -/// One fixture line: a name and a list of ops, each a JSON array. -public sealed class Scenario -{ - private readonly List _ops = new(); - - public Scenario(string name) - { - Name = name; - } - - public string Name { get; } - - public void Op(params object[] items) - { - var sb = new StringBuilder("["); - for (var i = 0; i < items.Length; i++) - { - if (i > 0) - { - sb.Append(','); - } - - Json.Append(sb, items[i]); - } - - sb.Append(']'); - _ops.Add(sb.ToString()); - } - - public string ToJson() - { - var sb = new StringBuilder(); - sb.Append("{\"name\":"); - Json.Append(sb, Name); - sb.Append(",\"ops\":["); - sb.AppendJoin(',', _ops); - sb.Append("]}"); - return sb.ToString(); - } -} - -public static class Json -{ - public static void Append(StringBuilder sb, object? value) - { - switch (value) - { - case null: - sb.Append("null"); - break; - case string s: - sb.Append('"'); - foreach (var c in s) - { - if (c == '"' || c == '\\') - { - sb.Append('\\'); - } - - sb.Append(c); - } - - sb.Append('"'); - break; - case bool b: - sb.Append(b ? "true" : "false"); - break; - case int i: - sb.Append(i.ToString(CultureInfo.InvariantCulture)); - break; - case double d: - sb.Append(Double(d)); - break; - case System.Collections.IEnumerable list: - sb.Append('['); - var first = true; - foreach (var item in list) - { - if (!first) - { - sb.Append(','); - } - - first = false; - Append(sb, item); - } - - sb.Append(']'); - break; - default: - throw new ArgumentException($"Unsupported JSON value {value.GetType()}"); - } - } - - /// "R" round-trip text. Always has a '.' or exponent so JSON readers keep it a float. - public static string Double(double d) - { - if (double.IsNaN(d) || double.IsInfinity(d)) - { - throw new ArgumentException("Non-finite doubles are not written."); - } - - var s = d.ToString("R", CultureInfo.InvariantCulture); - if (!s.Contains('.') && !s.Contains('E')) - { - s += ".0"; - } - - return s; - } -} - -/// The capacities .NET's HashHelpers steps through, plus their neighbors. -public static class Sizes -{ - public static readonly int[] AroundPrimes = - { - 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 16, 17, 18, 22, 23, 24, 29, 30, 36, 37, 38, - 46, 47, 48, 58, 59, 60, 70, 71, 72, 88, 89, 90, 106, 107, 108, 130, 131, 132, - }; - - public static readonly int[] Capacities = { -1, 0, 1, 2, 3, 5, 7, 8, 11, 17, 30, 37, 100 }; -} - -public static class HashSetScenarios -{ - public static IEnumerable Generate(Random rng) - { - foreach (var s in EdgeCases()) - { - yield return s; - } - - // Fill, remove some, copy, then add: the copy constructor keeps free slots only - // when capacities line up, so walk the sizes around each prime step. - foreach (var n in Sizes.AroundPrimes) - { - foreach (var removeKind in new[] { "one", "quarter", "half", "all_but_one", "all" }) - { - foreach (var capacity in new[] { -1, n * 2 + 1 }) - { - yield return FillRemoveCopy(rng, n, removeKind, capacity, copyKind: "copy"); - } - } - - yield return FillRemoveCopy(rng, n, "half", -1, copyKind: "copy_list"); - } - - // Random operation mixes. - var keyRanges = new[] { 4, 8, 16, 32, 64, 200 }; - var opCounts = new[] { 10, 30, 80, 200, 300 }; - var index = 0; - foreach (var keyRange in keyRanges) - { - foreach (var opCount in opCounts) - { - for (var rep = 0; rep < (opCount >= 200 ? 2 : 4); rep++) - { - var capacity = Sizes.Capacities[rng.Next(Sizes.Capacities.Length)]; - yield return RandomMix(rng, $"random_{index++}_k{keyRange}_n{opCount}", keyRange, opCount, capacity); - } - } - } - } - - private static HashSet New(int capacity) => capacity < 0 ? new HashSet() : new HashSet(capacity); - - private static IEnumerable EdgeCases() - { - var s = new Scenario("empty"); - var set = New(-1); - s.Op("new", -1); - Order(s, set); - s.Op("copy"); - set = new HashSet(set); - Order(s, set); - yield return s; - - s = new Scenario("single"); - set = New(-1); - s.Op("new", -1); - s.Op("add", 5, set.Add(5)); - s.Op("add", 5, set.Add(5)); - s.Op("contains", 5, set.Contains(5)); - s.Op("contains", 6, set.Contains(6)); - Order(s, set); - s.Op("remove", 5, set.Remove(5)); - s.Op("remove", 5, set.Remove(5)); - Order(s, set); - yield return s; - - s = new Scenario("remove_all_then_add"); - set = New(-1); - s.Op("new", -1); - for (var i = 0; i < 10; i++) - { - s.Op("add", i, set.Add(i)); - } - - for (var i = 0; i < 10; i++) - { - s.Op("remove", i, set.Remove(i)); - } - - Order(s, set); - for (var i = 20; i < 25; i++) - { - s.Op("add", i, set.Add(i)); - } - - Order(s, set); - yield return s; - - s = new Scenario("clear_then_add"); - set = New(4); - s.Op("new", 4); - for (var i = 0; i < 12; i++) - { - s.Op("add", i * 3, set.Add(i * 3)); - } - - s.Op("remove", 6, set.Remove(6)); - s.Op("clear"); - set.Clear(); - Order(s, set); - for (var i = 0; i < 5; i++) - { - s.Op("add", 100 - i, set.Add(100 - i)); - } - - Order(s, set); - yield return s; - - s = new Scenario("copy_with_holes_then_add"); - set = New(-1); - s.Op("new", -1); - for (var i = 0; i < 7; i++) - { - s.Op("add", i, set.Add(i)); - } - - s.Op("remove", 1, set.Remove(1)); - s.Op("remove", 4, set.Remove(4)); - Order(s, set); - s.Op("copy"); - set = new HashSet(set); - Order(s, set); - s.Op("add", 10, set.Add(10)); - s.Op("add", 11, set.Add(11)); - s.Op("add", 12, set.Add(12)); - Order(s, set); - yield return s; - - s = new Scenario("union_with_self_and_except_with_self"); - set = New(-1); - s.Op("new", -1); - foreach (var k in new[] { 3, 1, 4, 1, 5, 9, 2, 6 }) - { - s.Op("add", k, set.Add(k)); - } - - s.Op("union", set.ToList()); - set.UnionWith(set.ToList()); - Order(s, set); - s.Op("except", set.ToList()); - set.ExceptWith(set.ToList()); - Order(s, set); - yield return s; - } - - private static Scenario FillRemoveCopy(Random rng, int n, string removeKind, int capacity, string copyKind) - { - var s = new Scenario($"fill_remove_copy_n{n}_{removeKind}_cap{capacity}_{copyKind}"); - var set = New(capacity); - s.Op("new", capacity); - var keys = Enumerable.Range(0, n).Select(i => i * 7 + rng.Next(3)).Distinct().ToList(); - foreach (var k in keys) - { - s.Op("add", k, set.Add(k)); - } - - var removeCount = removeKind switch - { - "one" => 1, - "quarter" => Math.Max(1, keys.Count / 4), - "half" => Math.Max(1, keys.Count / 2), - "all_but_one" => Math.Max(0, keys.Count - 1), - "all" => keys.Count, - _ => throw new ArgumentException(removeKind), - }; - - var shuffled = keys.OrderBy(_ => rng.Next()).ToList(); - for (var i = 0; i < removeCount && i < shuffled.Count; i++) - { - s.Op("remove", shuffled[i], set.Remove(shuffled[i])); - } - - Order(s, set); - s.Op(copyKind); - set = copyKind == "copy" ? new HashSet(set) : new HashSet(set.ToList()); - Order(s, set); - - var addCount = n / 2 + 2; - for (var i = 0; i < addCount; i++) - { - var k = 10_000 + i * 13; - s.Op("add", k, set.Add(k)); - } - - Order(s, set); - return s; - } - - private static Scenario RandomMix(Random rng, string name, int keyRange, int opCount, int capacity) - { - var s = new Scenario(name); - var set = New(capacity); - s.Op("new", capacity); - for (var i = 0; i < opCount; i++) - { - var roll = rng.Next(100); - if (roll < 35) - { - var k = rng.Next(keyRange); - s.Op("add", k, set.Add(k)); - } - else if (roll < 55) - { - var k = rng.Next(keyRange); - s.Op("remove", k, set.Remove(k)); - } - else if (roll < 62) - { - var k = rng.Next(keyRange); - s.Op("contains", k, set.Contains(k)); - } - else if (roll < 66) - { - var ks = RandomKeys(rng, keyRange); - s.Op("union", ks); - set.UnionWith(ks); - } - else if (roll < 69) - { - var ks = RandomKeys(rng, keyRange); - s.Op("union_set", ks); - set.UnionWith(new HashSet(ks)); - } - else if (roll < 73) - { - var ks = RandomKeys(rng, keyRange); - s.Op("except", ks); - set.ExceptWith(ks); - } - else if (roll < 75) - { - var ks = RandomKeys(rng, keyRange); - s.Op("intersect", ks); - set.IntersectWith(ks); - } - else if (roll < 77) - { - var ks = RandomKeys(rng, keyRange); - s.Op("intersect_set", ks); - set.IntersectWith(new HashSet(ks)); - } - else if (roll < 80) - { - var ks = RandomKeys(rng, keyRange); - s.Op("overlaps", ks, set.Overlaps(ks)); - } - else if (roll < 82) - { - var ks = rng.Next(2) == 0 ? set.ToList() : RandomKeys(rng, keyRange); - s.Op("set_equals", ks, set.SetEquals(ks)); - } - else if (roll < 88) - { - s.Op("copy"); - set = new HashSet(set); - } - else if (roll < 90) - { - s.Op("copy_list"); - set = new HashSet(set.ToList()); - } - else if (roll < 91) - { - s.Op("clear"); - set.Clear(); - } - else if (roll < 93) - { - var n = rng.Next(keyRange * 2); - s.Op("ensure_capacity", n, set.EnsureCapacity(n)); - } - else if (roll < 94) - { - s.Op("trim"); - set.TrimExcess(); - } - else - { - Order(s, set); - } - } - - Order(s, set); - return s; - } - - private static List RandomKeys(Random rng, int keyRange) - { - var n = rng.Next(0, Math.Min(keyRange, 12) + 1); - var list = new List(n); - for (var i = 0; i < n; i++) - { - list.Add(rng.Next(keyRange)); - } - - return list; - } - - private static void Order(Scenario s, HashSet set) - { - s.Op("order", set.ToList()); - } -} - -public static class DictionaryScenarios -{ - public static IEnumerable Generate(Random rng) - { - foreach (var s in EdgeCases()) - { - yield return s; - } - - foreach (var n in Sizes.AroundPrimes) - { - foreach (var removeKind in new[] { "one", "half", "all_but_one", "all" }) - { - foreach (var capacity in new[] { -1, n }) - { - yield return FillRemoveCopy(rng, n, removeKind, capacity); - } - } - } - - var keyRanges = new[] { 4, 8, 16, 32, 64, 200 }; - var opCounts = new[] { 10, 30, 80, 200, 300 }; - var index = 0; - foreach (var keyRange in keyRanges) - { - foreach (var opCount in opCounts) - { - for (var rep = 0; rep < (opCount >= 200 ? 2 : 4); rep++) - { - var capacity = Sizes.Capacities[rng.Next(Sizes.Capacities.Length)]; - yield return RandomMix(rng, $"random_{index++}_k{keyRange}_n{opCount}", keyRange, opCount, capacity); - } - } - } - } - - private static Dictionary New(int capacity) => capacity < 0 ? new Dictionary() : new Dictionary(capacity); - - private static IEnumerable EdgeCases() - { - var s = new Scenario("empty"); - var d = New(-1); - s.Op("new", -1); - Pairs(s, d); - s.Op("copy"); - d = new Dictionary(d); - Pairs(s, d); - yield return s; - - s = new Scenario("overwrite_keeps_position"); - d = New(-1); - s.Op("new", -1); - for (var i = 0; i < 6; i++) - { - s.Op("set", i, i * 10); - d[i] = i * 10; - } - - s.Op("set", 2, 99); - d[2] = 99; - s.Op("try_add", 3, 77, d.TryAdd(3, 77)); - s.Op("try_add", 30, 77, d.TryAdd(30, 77)); - Pairs(s, d); - yield return s; - - s = new Scenario("copy_with_holes_then_add"); - d = New(-1); - s.Op("new", -1); - for (var i = 0; i < 7; i++) - { - s.Op("add", i, -i); - d.Add(i, -i); - } - - s.Op("remove", 1, d.Remove(1)); - s.Op("remove", 4, d.Remove(4)); - Order(s, d); - s.Op("copy"); - d = new Dictionary(d); - Order(s, d); - s.Op("add", 10, 1); - d.Add(10, 1); - s.Op("add", 11, 2); - d.Add(11, 2); - s.Op("add", 12, 3); - d.Add(12, 3); - Pairs(s, d); - yield return s; - - s = new Scenario("clear_then_add"); - d = New(3); - s.Op("new", 3); - for (var i = 0; i < 9; i++) - { - s.Op("set", i * 5, i); - d[i * 5] = i; - } - - s.Op("remove", 10, d.Remove(10)); - s.Op("clear"); - d.Clear(); - for (var i = 0; i < 4; i++) - { - s.Op("set", 50 - i, i); - d[50 - i] = i; - } - - Pairs(s, d); - yield return s; - } - - private static Scenario FillRemoveCopy(Random rng, int n, string removeKind, int capacity) - { - var s = new Scenario($"fill_remove_copy_n{n}_{removeKind}_cap{capacity}"); - var d = New(capacity); - s.Op("new", capacity); - var keys = Enumerable.Range(0, n).Select(i => i * 5 + rng.Next(2)).Distinct().ToList(); - foreach (var k in keys) - { - var v = rng.Next(1000); - s.Op("add", k, v); - d.Add(k, v); - } - - var removeCount = removeKind switch - { - "one" => 1, - "half" => Math.Max(1, keys.Count / 2), - "all_but_one" => Math.Max(0, keys.Count - 1), - "all" => keys.Count, - _ => throw new ArgumentException(removeKind), - }; - - var shuffled = keys.OrderBy(_ => rng.Next()).ToList(); - for (var i = 0; i < removeCount && i < shuffled.Count; i++) - { - s.Op("remove", shuffled[i], d.Remove(shuffled[i])); - } - - Order(s, d); - s.Op("copy"); - d = new Dictionary(d); - Order(s, d); - for (var i = 0; i < n / 2 + 2; i++) - { - var k = 10_000 + i * 11; - s.Op("set", k, i); - d[k] = i; - } - - Pairs(s, d); - return s; - } - - private static Scenario RandomMix(Random rng, string name, int keyRange, int opCount, int capacity) - { - var s = new Scenario(name); - var d = New(capacity); - s.Op("new", capacity); - for (var i = 0; i < opCount; i++) - { - var roll = rng.Next(100); - var k = rng.Next(keyRange); - if (roll < 25) - { - var v = rng.Next(1000); - s.Op("set", k, v); - d[k] = v; - } - else if (roll < 35) - { - if (!d.ContainsKey(k)) - { - var v = rng.Next(1000); - s.Op("add", k, v); - d.Add(k, v); - } - } - else if (roll < 45) - { - var v = rng.Next(1000); - s.Op("try_add", k, v, d.TryAdd(k, v)); - } - else if (roll < 62) - { - s.Op("remove", k, d.Remove(k)); - } - else if (roll < 67) - { - s.Op("contains", k, d.ContainsKey(k)); - } - else if (roll < 75) - { - var found = d.TryGetValue(k, out var v); - s.Op("get", k, found, v); - } - else if (roll < 81) - { - s.Op("copy"); - d = new Dictionary(d); - } - else if (roll < 82) - { - s.Op("clear"); - d.Clear(); - } - else if (roll < 84) - { - var n = rng.Next(keyRange * 2); - s.Op("ensure_capacity", n, d.EnsureCapacity(n)); - } - else if (roll < 85) - { - s.Op("trim"); - d.TrimExcess(); - } - else if (roll < 92) - { - Order(s, d); - } - else - { - Pairs(s, d); - } - } - - Pairs(s, d); - return s; - } - - private static void Order(Scenario s, Dictionary d) - { - s.Op("order", d.Keys.ToList()); - } - - private static void Pairs(Scenario s, Dictionary d) - { - s.Op("pairs", d.Select(p => new[] { p.Key, p.Value }).ToList()); - } -} - -public static class PriorityQueueScenarios -{ - private static readonly double[] DoublePriorities = { -1, 0, 0.5, 1, 1.0001, 2, 2.0001, 3, 3.0001, 4, 7.5 }; - - public static IEnumerable Generate(Random rng) - { - // A*-like: costs grow by 1 with a heuristic and an occasional 0.0001 turn penalty. - for (var rep = 0; rep < 20; rep++) - { - yield return AStarLike(rng, rep); - } - - var opCounts = new[] { 5, 20, 60, 150, 300 }; - var index = 0; - foreach (var kind in new[] { "double", "int" }) - { - foreach (var opCount in opCounts) - { - for (var rep = 0; rep < 12; rep++) - { - yield return RandomMix(rng, $"random_{index++}_{kind}_n{opCount}", kind, opCount); - } - } - } - } - - private static Scenario AStarLike(Random rng, int rep) - { - var s = new Scenario($"astar_like_{rep}"); - var q = new PriorityQueue(); - s.Op("new", "double"); - var next = 0; - q.Enqueue(next, 0); - s.Op("enqueue", next++, 0.0); - var steps = 30 + rng.Next(120); - for (var i = 0; i < steps && q.Count > 0; i++) - { - q.TryPeek(out _, out var cost); - var e = q.Dequeue(); - s.Op("dequeue", e); - var neighbors = rng.Next(5); - for (var j = 0; j < neighbors; j++) - { - var p = Math.Floor(cost) + 1 + rng.Next(3) + (rng.Next(3) == 0 ? 0.0001 : 0); - q.Enqueue(next, p); - s.Op("enqueue", next++, p); - } - } - - Drain(s, q); - return s; - } - - private static Scenario RandomMix(Random rng, string name, string kind, int opCount) - { - var s = new Scenario(name); - s.Op("new", kind); - if (kind == "double") - { - var q = new PriorityQueue(); - Run(rng, s, opCount, q, () => DoublePriorities[rng.Next(DoublePriorities.Length)], p => p); - Drain(s, q); - } - else - { - var q = new PriorityQueue(); - Run(rng, s, opCount, q, () => rng.Next(6), p => p); - Drain(s, q); - } - - return s; - } - - private static void Run(Random rng, Scenario s, int opCount, PriorityQueue q, Func nextPriority, Func box) - { - for (var i = 0; i < opCount; i++) - { - var roll = rng.Next(100); - if (roll < 50 || q.Count == 0 && roll < 80) - { - var e = rng.Next(40); - var p = nextPriority(); - q.Enqueue(e, p); - s.Op("enqueue", e, box(p)); - } - else if (roll < 70) - { - if (q.Count > 0) - { - s.Op("dequeue", q.Dequeue()); - } - } - else if (roll < 78) - { - if (q.Count > 0) - { - s.Op("peek", q.Peek()); - } - } - else if (roll < 83) - { - s.Op("count", q.Count); - } - else if (roll < 85) - { - s.Op("clear"); - q.Clear(); - } - else - { - var e = rng.Next(40); - var found = q.Remove(e, out var removedElement, out var removedPriority); - s.Op("remove", e, found, found ? removedElement : 0, box(removedPriority!)); - } - } - } - - private static void Drain(Scenario s, PriorityQueue q) - { - var items = new List(); - while (q.TryDequeue(out var e, out var p)) - { - items.Add(new object[] { e, p! }); - } - - s.Op("drain", items); - } -} - -public static class SortScenarios -{ - private readonly record struct Item(int Key, int Id); - - private sealed class KeyComparer : IComparer - { - public int Compare(Item a, Item b) => a.Key.CompareTo(b.Key); - } - - private static readonly Comparison ByKey = (a, b) => a.Key.CompareTo(b.Key); - - public static IEnumerable Generate(Random rng) - { - var lengths = Enumerable.Range(0, 41).Concat(new[] { 47, 48, 50, 63, 64, 65, 100, 127, 128, 129, 200, 255, 256, 300 }).ToList(); - var patterns = new[] { "random_r1", "random_r2", "random_r3", "random_r5", "random_r10", "random_rn", "sorted", "reversed", "equal", "organ_pipe", "sawtooth" }; - var index = 0; - foreach (var n in lengths) - { - foreach (var pattern in patterns) - { - var keys = Keys(rng, n, pattern); - var s = new Scenario($"sort_{index++}_n{n}_{pattern}"); - if (n <= 64) - { - // Short inputs: record every API, in case they ever diverge. - ListSort(s, keys); - ArraySort(s, keys); - SpanSort(s, keys); - ListSortRange(rng, s, keys); - } - else - { - switch (index % 4) - { - case 0: ListSort(s, keys); break; - case 1: ArraySort(s, keys); break; - case 2: SpanSort(s, keys); break; - default: ListSortRange(rng, s, keys); break; - } - } - - yield return s; - } - } - - // McIlroy's adversary drives introsort past its depth limit into heapsort. - foreach (var n in new[] { 20, 50, 100, 200, 300 }) - { - var keys = Adversary(n); - var s = new Scenario($"sort_adversary_n{n}"); - ListSort(s, keys); - ArraySort(s, keys); - SpanSort(s, keys); - ListSortRange(rng, s, keys); - yield return s; - - // The same input with ties: collapse keys into a few buckets. - var tied = keys.Select(k => k / 7).ToArray(); - s = new Scenario($"sort_adversary_tied_n{n}"); - ListSort(s, tied); - ArraySort(s, tied); - yield return s; - } - } - - private static int[] Keys(Random rng, int n, string pattern) - { - var keys = new int[n]; - for (var i = 0; i < n; i++) - { - keys[i] = pattern switch - { - "random_r1" => 0, - "random_r2" => rng.Next(2), - "random_r3" => rng.Next(3), - "random_r5" => rng.Next(5), - "random_r10" => rng.Next(10), - "random_rn" => rng.Next(Math.Max(1, n)), - "sorted" => i / 2, - "reversed" => (n - i) / 2, - "equal" => 7, - "organ_pipe" => Math.Min(i, n - i), - "sawtooth" => i % 5, - _ => throw new ArgumentException(pattern), - }; - } - - return keys; - } - - private static int[] Adversary(int n) - { - var gas = n; - var val = Enumerable.Repeat(gas, n).ToArray(); - var nsolid = 0; - var candidate = 0; - var ids = Enumerable.Range(0, n).ToArray(); - Array.Sort(ids, (x, y) => - { - if (val[x] == gas && val[y] == gas) - { - if (x == candidate) - { - val[x] = nsolid++; - } - else - { - val[y] = nsolid++; - } - } - - if (val[x] == gas) - { - candidate = x; - } - else if (val[y] == gas) - { - candidate = y; - } - - return val[x].CompareTo(val[y]); - }); - return val; - } - - private static List Items(int[] keys) => keys.Select((k, i) => new Item(k, i)).ToList(); - - private static List Pairs(List items) => items.Select(x => new[] { x.Key, x.Id }).ToList(); - - private static void ListSort(Scenario s, int[] keys) - { - var items = Items(keys); - var input = Pairs(items); - items.Sort(ByKey); - s.Op("list_sort", input, items.Select(x => x.Id).ToList()); - } - - private static void ArraySort(Scenario s, int[] keys) - { - var items = Items(keys).ToArray(); - var input = Pairs(items.ToList()); - Array.Sort(items, ByKey); - s.Op("array_sort", input, items.Select(x => x.Id).ToList()); - } - - private static void SpanSort(Scenario s, int[] keys) - { - var items = Items(keys).ToArray(); - var input = Pairs(items.ToList()); - MemoryExtensions.Sort(items.AsSpan(), ByKey); - s.Op("span_sort", input, items.Select(x => x.Id).ToList()); - } - - private static void ListSortRange(Random rng, Scenario s, int[] keys) - { - var items = Items(keys); - var input = Pairs(items); - var index = items.Count == 0 ? 0 : rng.Next(items.Count); - var count = items.Count - index == 0 ? 0 : rng.Next(items.Count - index + 1); - items.Sort(index, count, new KeyComparer()); - s.Op("list_sort_range", input, index, count, items.Select(x => x.Id).ToList()); - } -} - -public static class MathScenarios -{ - public static IEnumerable Generate(Random rng) - { - var values = new List(); - for (var k = -20; k <= 20; k++) - { - values.Add(k + 0.5); - values.Add(k); - values.Add(k + 0.25); - values.Add(k + 0.75); - values.Add(BitDecrement(k + 0.5)); - values.Add(BitIncrement(k + 0.5)); - } - - // Centroid-style averages, the case AddPipes.1.FBE.cs:801 rounds. - for (var count = 1; count <= 12; count++) - { - for (var sum = -30; sum <= 60; sum++) - { - values.Add((double)sum / count); - } - } - - for (var i = 0; i < 300; i++) - { - values.Add((rng.NextDouble() - 0.5) * 2000); - } - - values.Add(0.0); - values.Add(-0.0); - values.Add(1e9 + 0.5); - values.Add(-1e9 - 0.5); - values.Add(2147483646.5); - values.Add(-2147483647.5); - - var round = new Scenario("round"); - var roundInt = new Scenario("round_int"); - var toInt = new Scenario("to_int"); - var ceiling = new Scenario("ceiling"); - var floor = new Scenario("floor"); - foreach (var x in values) - { - var r = Math.Round(x, 0); - round.Op("round", x, r); - if (r >= int.MinValue && r <= int.MaxValue) - { - roundInt.Op("round_int", x, (int)Math.Round(x, 0)); - } - - if (x > int.MinValue - 1.0 && x < int.MaxValue + 1.0) - { - toInt.Op("to_int", x, (int)x); - } - - ceiling.Op("ceiling", x, Math.Ceiling(x)); - floor.Op("floor", x, Math.Floor(x)); - } - - yield return round; - yield return roundInt; - yield return toInt; - yield return ceiling; - yield return floor; - } - - private static double BitDecrement(double x) => Math.BitDecrement(x); - - private static double BitIncrement(double x) => Math.BitIncrement(x); -} diff --git a/rust/flute/FIXTURES.md b/rust/flute/FIXTURES.md index 5f3348f1..93dbdf8a 100644 --- a/rust/flute/FIXTURES.md +++ b/rust/flute/FIXTURES.md @@ -1,14 +1,8 @@ # flute fixtures -`fixtures/flute.jsonl` holds what FluteSharp computes, so the Rust port can be checked without .NET (#138). CI reruns the recorder on the pinned SDK and fails if the committed file drifts. +`fixtures/flute.jsonl` holds what FluteSharp computes, recorded by a recorder in `oracle/`, so the Rust port can be checked without .NET (#138). -**Regenerate** (from the repo root, with submodules checked out): - -```bash -./docker-build.sh run --project rust/flute/oracle -c Release -- rust/flute/fixtures -``` - -The output is deterministic: two runs give the same bytes. +**These fixtures are frozen inputs.** They were recorded from the C# planner's copy of FluteSharp at commit `0532d923`. Stage 13b (#138) deleted the C# and the recorder with it, so nothing can regenerate them now. To see the recorder, check out that commit. ## Format @@ -24,7 +18,7 @@ JSON Lines, one case per line: | Cases | Input | |---|---| -| `small-NNNN` | All 61 blueprints in `test/FactorioTools.Test/OilField/small-list.txt` | +| `small-NNNN` | All 61 blueprints in `testdata/oil-field/small-list.txt` | | `big-NNNN` | Every second blueprint in `big-list.txt` (574 of 1,147) | | `tiny-*`, `small-random-*` | 1 to 10 random points, coordinates 0 to 4 (many duplicates) and 0 to 20 | | `medium-random-*`, `crowded-*` | 11 to 60 random points, coordinates -10 to 40, and 0 to 6 (crowded) | diff --git a/rust/flute/README.md b/rust/flute/README.md index 30bc4fae..d3d09f15 100644 --- a/rust/flute/README.md +++ b/rust/flute/README.md @@ -22,4 +22,4 @@ Only what the planner uses: the degree 6 lookup table and the default accuracy o ## How it is checked -`oracle/` records FluteSharp's trees for 1,346 inputs, and `tests/fixtures.rs` checks this port gives the same tree, branch for branch, for every one. See `FIXTURES.md`. +`fixtures/flute.jsonl` holds FluteSharp's trees for 1,346 inputs, and `tests/fixtures.rs` checks this port gives the same tree, branch for branch, for every one. The trees were recorded from the C# before stage 13b removed it, so the file is frozen. See `FIXTURES.md`. diff --git a/rust/flute/oracle/FluteOracle.csproj b/rust/flute/oracle/FluteOracle.csproj deleted file mode 100644 index e9cccfcf..00000000 --- a/rust/flute/oracle/FluteOracle.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - Exe - net10.0 - enable - enable - true - - - - - diff --git a/rust/flute/oracle/Program.cs b/rust/flute/oracle/Program.cs deleted file mode 100644 index ed5d7e0c..00000000 --- a/rust/flute/oracle/Program.cs +++ /dev/null @@ -1,164 +0,0 @@ -// Records FluteSharp's output for the Rust port in rust/flute (#138). See ../FIXTURES.md. -// -// Usage: dotnet run --project rust/flute/oracle -c Release -- -// Run from anywhere inside the repo; the blueprint lists are found from the repo root. - -using System.Text; -using System.Text.Json; -using Knapcode.FactorioTools.OilField; -using Knapcode.FluteSharp; - -var outputDirectory = Path.GetFullPath(args.Length > 0 ? args[0] : "rust/flute/fixtures"); -var repoRoot = FindRepoRoot(Directory.GetCurrentDirectory()); -var listDirectory = Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField"); - -var flute = new FLUTE(LookUpTable.Degree6); -var cases = new List<(string Name, List Points)>(); - -// (a) The terminal points the planner hands FLUTE, built the way -// AddPipesConnectedCentersFLUTE.GetFluteTree builds them: every terminal of every center, -// in CenterToTerminals order, straight after InitializeContext. -AddBlueprintCases("small", Path.Combine(listDirectory, "small-list.txt"), stride: 1); -AddBlueprintCases("big", Path.Combine(listDirectory, "big-list.txt"), stride: 2); - -// (b) Synthetic inputs, from a fixed seed. -var random = new Random(42); -cases.Add(("empty", new List())); -for (var n = 1; n <= 10; n++) -{ - for (var i = 0; i < 30; i++) - { - cases.Add(($"tiny-{n}-{i}", RandomPoints(n, 0, 4))); - } - - for (var i = 0; i < 15; i++) - { - cases.Add(($"small-random-{n}-{i}", RandomPoints(n, 0, 20))); - } -} - -for (var n = 11; n <= 60; n++) -{ - for (var i = 0; i < 3; i++) - { - cases.Add(($"medium-random-{n}-{i}", RandomPoints(n, -10, 40))); - } - - cases.Add(($"crowded-{n}", RandomPoints(n, 0, 6))); -} - -for (var n = 3; n <= 14; n++) -{ - cases.Add(($"horizontal-{n}", Enumerable.Range(0, n).Select(i => new Point(i * 2, 5)).ToList())); - cases.Add(($"vertical-{n}", Enumerable.Range(0, n).Select(i => new Point(3, i)).ToList())); - cases.Add(($"diagonal-{n}", Enumerable.Range(0, n).Select(i => new Point(i, i)).ToList())); - cases.Add(($"same-point-{n}", Enumerable.Range(0, n).Select(_ => new Point(7, 7)).ToList())); - cases.Add(($"grid-{n}", Enumerable.Range(0, n).Select(i => new Point(i % 3 * 3, i / 3 * 3)).ToList())); -} - -Directory.CreateDirectory(outputDirectory); -var outputPath = Path.Combine(outputDirectory, "flute.jsonl"); -var tempPath = outputPath + ".tmp"; -var errors = 0; -using (var stream = File.Create(tempPath)) -{ - foreach (var (name, points) in cases) - { - var buffer = new MemoryStream(); - using (var writer = new Utf8JsonWriter(buffer)) - { - writer.WriteStartObject(); - writer.WriteString("name", name); - writer.WriteStartArray("points"); - foreach (var p in points) - { - writer.WriteStartArray(); - writer.WriteNumberValue(p.X); - writer.WriteNumberValue(p.Y); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - try - { - var tree = flute.Execute(points); - writer.WriteNumber("deg", tree.Deg); - writer.WriteNumber("length", tree.Length); - writer.WriteStartArray("branches"); - foreach (var b in tree.Branch) - { - writer.WriteStartArray(); - writer.WriteNumberValue(b.X); - writer.WriteNumberValue(b.Y); - writer.WriteNumberValue(b.N); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - catch (Exception ex) - { - writer.WriteString("error", ex.GetType().Name); - errors++; - } - - writer.WriteEndObject(); - } - - buffer.WriteByte((byte)'\n'); - buffer.Position = 0; - buffer.CopyTo(stream); - } -} - -File.Move(tempPath, outputPath, overwrite: true); -Console.WriteLine($"flute.jsonl: {cases.Count} cases ({errors} errors), {new FileInfo(outputPath).Length} bytes"); -return 0; - -void AddBlueprintCases(string prefix, string path, int stride) -{ - var blueprints = ParseBlueprint.ReadBlueprintFile(path); - for (var i = 0; i < blueprints.Count; i += stride) - { - var blueprint = ParseBlueprint.Execute(blueprints[i]); - var context = InitializeContext.Execute(OilFieldOptions.ForMediumElectricPole, blueprint, Array.Empty()); - var points = new List(); - foreach (var terminals in context.CenterToTerminals.Values) - { - for (var j = 0; j < terminals.Count; j++) - { - points.Add(new Point(terminals[j].Terminal.X, terminals[j].Terminal.Y)); - } - } - - cases.Add(($"{prefix}-{i:D4}", points)); - } -} - -List RandomPoints(int n, int min, int max) -{ - var points = new List(n); - for (var i = 0; i < n; i++) - { - points.Add(new Point(random.Next(min, max + 1), random.Next(min, max + 1))); - } - - return points; -} - -static string FindRepoRoot(string start) -{ - var directory = new DirectoryInfo(start); - while (directory is not null) - { - if (File.Exists(Path.Combine(directory.FullName, "FactorioTools.sln"))) - { - return directory.FullName; - } - - directory = directory.Parent; - } - - throw new InvalidOperationException($"No FactorioTools.sln above {start}."); -} diff --git a/rust/planner-wasm/src/lib.rs b/rust/planner-wasm/src/lib.rs index 031ed4da..62657207 100644 --- a/rust/planner-wasm/src/lib.rs +++ b/rust/planner-wasm/src/lib.rs @@ -1,5 +1,5 @@ -//! The planner as a WebAssembly module (#138): the Rust counterpart of `src/BrowserWasm`'s -//! two `[JSExport]` methods, `Plan` and `Normalize`. +//! The planner as a WebAssembly module (#138). It replaced the .NET build in `src/BrowserWasm` +//! (removed in stage 13b) and its two `[JSExport]` methods, `Plan` and `Normalize`. //! //! The exports use plain numbers, so no binding generator is needed. `js/planner.js` wraps //! them in the same `plan(requestJson)` / `normalize(requestJson)` functions as diff --git a/rust/planner/FIXTURES.md b/rust/planner/FIXTURES.md index 9654261c..b2da8925 100644 --- a/rust/planner/FIXTURES.md +++ b/rust/planner/FIXTURES.md @@ -1,16 +1,14 @@ # planner fixtures -Recorded from the C# planner by `oracle/`, so the Rust tests can check the port without .NET. -Each stage of the port (#138) adds files here. CI reruns the recorder on the pinned SDK and -fails if the committed files drift. +Recorded from the C# planner by the recorders in `oracle/`, so the Rust tests can check the +port without .NET. Each stage of the port (#138) added files here. -**Regenerate** (from the repo root, with submodules checked out): +**These fixtures are frozen inputs.** They were recorded from the C# planner at commit +`0532d923`. Stage 13b (#138) deleted the C# and the recorders with it, so nothing can +regenerate them now. To see a recorder, check out that commit. The `oracle/...` file names +below are paths at that commit. -```bash -./docker-build.sh run --project rust/planner/oracle -c Release -- rust/planner/fixtures -``` - -All files are JSON Lines, one case per line. The output is deterministic. One run writes every file. +All files are JSON Lines, one case per line. ## `grids.jsonl` (stage 1) @@ -71,9 +69,9 @@ The Rust test rebuilds each grid by adding the entities in this order, then chec `EntityLocations` enumerates in the same order and that its `to_string` equals `text` byte for byte. -**Checking the recorder itself:** add `--check-snapshots` to the command above. The recorder -then also compares each case's `text` with the grid half of its committed Verify snapshot (the -part after the first blank line) and exits 1 on any difference. On 2026-10-21 all 126 +**Checking the recorder itself:** the recorder had a `--check-snapshots` flag that also +compared each case's `text` with the grid half of its committed Verify snapshot (the part +after the first blank line) and exited 1 on any difference. On 2026-10-21 all 126 snapshot-backed cases matched. ## `algorithms.jsonl` (stage 2) diff --git a/rust/planner/oracle/Algorithms.cs b/rust/planner/oracle/Algorithms.cs deleted file mode 100644 index e1bb41cd..00000000 --- a/rust/planner/oracle/Algorithms.cs +++ /dev/null @@ -1,575 +0,0 @@ -// Records calls to the planner's graph and line algorithms (stage 2 of #138): -// A*, Dijkstra, breadth-first search, Prim's MST and Bresenham's line. -// Format: rust/planner/FIXTURES.md, "algorithms.jsonl". - -using System.Text.Encodings.Web; -using System.Text.Json; -using DelaunatorSharp; -using Knapcode.FactorioTools.OilField; - -static class AlgorithmCases -{ - /// Every Nth big-list blueprint is recorded too, to keep the file small. - private const int BigListStride = 20; - - /// Full LocationToPrevious contents are written for this many Dijkstra calls per - /// blueprint; the rest carry only a count and a hash of the same contents. - private const int FullPreviousPerBlueprint = 1; - - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly (bool PreferNoTurns, int XWeight, int YWeight)[] Weights = - { - (true, 1, 1), - (false, 1, 1), - (true, 2, 1), - (true, 1, 2), - }; - - public static void Write(string outputDir, IReadOnlyList smallList, IReadOnlyList bigList) - { - var path = Path.Combine(outputDir, "algorithms.jsonl"); - var counts = new SortedDictionary(StringComparer.Ordinal); - using (var stream = File.Create(path)) - { - for (var i = 0; i < smallList.Count; i++) - { - WriteBlueprint(stream, $"small-{i:D4}", smallList[i], seed: i, withPlan: true, counts); - } - - for (var i = 0; i < bigList.Count; i += BigListStride) - { - WriteBlueprint(stream, $"big-{i:D4}", bigList[i], seed: 100_000 + i, withPlan: false, counts); - } - - var bresenham = new Random(424242); - for (var i = 0; i < 400; i++) - { - var a = new Location(bresenham.Next(-5, 60), bresenham.Next(-5, 60)); - var b = new Location(bresenham.Next(-5, 60), bresenham.Next(-5, 60)); - WriteLine(stream, w => - { - w.WriteString("kind", "bresenham"); - WritePoint(w, "a", a); - WritePoint(w, "b", b); - WritePoints(w, "path", BresenhamsLine.GetPath(a, b)); - }); - Count(counts, "bresenham"); - } - } - - Console.WriteLine($"algorithms.jsonl: {new FileInfo(path).Length} bytes; calls: " + string.Join(", ", counts.Select(x => $"{x.Key}={x.Value}"))); - } - - private static void WriteBlueprint(Stream stream, string id, string blueprintString, int seed, bool withPlan, IDictionary counts) - { - var random = new Random(seed); - var blueprint = ParseBlueprint.Execute(blueprintString); - var context = InitializeContext.Execute(OilFieldOptions.ForMediumElectricPole, blueprint, Array.Empty()); - - WriteLine(stream, w => - { - w.WriteString("kind", "grid"); - w.WriteString("id", id); - w.WriteNumber("width", context.Grid.Width); - w.WriteNumber("height", context.Grid.Height); - WritePoints(w, "occupied", context.Grid.EntityLocations.EnumerateItems()); - }); - - var terminals = context.CenterToTerminals.Values - .SelectMany(list => list.Select(t => t.Terminal)) - .Distinct() - .ToList(); - var occupied = context.Centers[0]; - var previousWritten = 0; - - // A* on the initial grid: varied weights, one goal set per call. - for (var k = 0; k < 8; k++) - { - var start = terminals[random.Next(terminals.Count)]; - var goals = RandomGoals(context, random, terminals, start, maxGoals: 6); - var (preferNoTurns, xWeight, yWeight) = Weights[k % Weights.Length]; - WriteAStar(stream, context, context.Grid, "grid", id, start, goals, "hash", preferNoTurns, xWeight, yWeight, prefix: null); - Count(counts, "astar"); - } - - // A* edge cases: the start is a goal, a single-location goal set, an unreachable goal - // (a pumpjack center, which is never empty), and a caller's list to append to. - { - var start = terminals[0]; - var goals = context.GetLocationSet(allowEnumerate: true); - goals.Add(start); - goals.Add(terminals[^1]); - WriteAStar(stream, context, context.Grid, "grid", id, start, goals, "hash", true, 1, 1, prefix: null); - - var single = context.GetSingleLocationSet(terminals[^1]); - WriteAStar(stream, context, context.Grid, "grid", id, start, single, "single", true, 1, 1, prefix: null); - - var unreachable = context.GetLocationSet(allowEnumerate: true); - unreachable.Add(occupied); - WriteAStar(stream, context, context.Grid, "grid", id, start, unreachable, "hash", true, 1, 1, prefix: new List { new(-5, -5) }); - - var withPrefix = RandomGoals(context, random, terminals, start, maxGoals: 3); - WriteAStar(stream, context, context.Grid, "grid", id, start, withPrefix, "hash", true, 2, 1, prefix: new List { new(-5, -5), new(-6, -6) }); - Count(counts, "astar", 4); - } - - // Dijkstra on the initial grid. - foreach (var (stopOnFirstGoal, allowGoalEnumerate) in new[] { (false, true), (true, true), (false, false) }) - { - var start = terminals[random.Next(terminals.Count)]; - var goals = RandomGoals(context, random, terminals, start, maxGoals: 6); - WriteDijkstra(stream, context, context.Grid, "grid", id, start, goals, stopOnFirstGoal, allowGoalEnumerate, previousWritten++ < FullPreviousPerBlueprint); - Count(counts, "dijkstra"); - } - - // Breadth-first search, which always walks context.Grid. - for (var k = 0; k < 4; k++) - { - var start = terminals[random.Next(terminals.Count)]; - var goal = terminals[random.Next(terminals.Count)]; - WriteBfs(stream, context, id, start, goal); - } - - WriteBfs(stream, context, id, terminals[0], terminals[0]); - WriteBfs(stream, context, id, terminals[0], occupied); - Count(counts, "bfs", 6); - - // Prim's MST on the graph AddPipesConnectedCentersDT.ExecuteWithDelaunayMst builds, and - // on a complete graph over a few centers with small costs, so ties are common. - var delaunayGraph = BuildDelaunayGraph(context); - if (delaunayGraph is not null) - { - var first = FirstMinByDistanceToMiddle(context); - var mst = WritePrims(stream, context, delaunayGraph, first, digraph: false); - WritePrims(stream, context, delaunayGraph, first, digraph: true); - CheckMatchesPlanner(context, mst, id); - Count(counts, "prims", 2); - } - - var someCenters = context.Centers.Take(8).ToList(); - if (someCenters.Count >= 2) - { - var complete = context.GetLocationDictionary>(someCenters.Count); - foreach (var center in someCenters) - { - complete.Add(center, context.GetLocationDictionary()); - } - - for (var a = 0; a < someCenters.Count; a++) - { - for (var b = a + 1; b < someCenters.Count; b++) - { - var cost = random.Next(1, 4); - complete[someCenters[a]][someCenters[b]] = cost; - complete[someCenters[b]][someCenters[a]] = cost; - } - } - - WritePrims(stream, context, complete, someCenters[random.Next(someCenters.Count)], digraph: false); - WritePrims(stream, context, complete, someCenters[0], digraph: true); - Count(counts, "prims", 2); - } - - if (!withPlan) - { - return; - } - - // The finished plan's pipes, walked through an ExistingPipeGrid as RotateOptimize and - // Validate do. - var plan = Planner.Execute(OilFieldOptions.ForMediumElectricPole, ParseBlueprint.Execute(blueprintString)); - var planContext = plan.Context; - var pipes = planContext.GetLocationSet(allowEnumerate: true); - var planTerminals = new List(); - foreach (var location in planContext.Grid.EntityLocations.EnumerateItems()) - { - var entity = planContext.Grid[location]; - if (entity is Pipe) - { - pipes.Add(location); - } - - if (entity is Terminal) - { - planTerminals.Add(location); - } - } - - var pipesId = id + "-pipes"; - WriteLine(stream, w => - { - w.WriteString("kind", "pipes"); - w.WriteString("id", pipesId); - w.WriteNumber("width", planContext.Grid.Width); - w.WriteNumber("height", planContext.Grid.Height); - WritePoints(w, "pipes", pipes.EnumerateItems()); - }); - - var existing = new ExistingPipeGrid(planContext.Grid, pipes); - var pipeList = pipes.EnumerateItems().ToList(); - for (var k = 0; k < 4; k++) - { - var start = pipeList[random.Next(pipeList.Count)]; - var goals = RandomGoals(planContext, random, planTerminals, start, maxGoals: 4); - var (preferNoTurns, xWeight, yWeight) = Weights[k % Weights.Length]; - WriteAStar(stream, planContext, existing, "pipes", pipesId, start, goals, "hash", preferNoTurns, xWeight, yWeight, prefix: null); - Count(counts, "astar_existing"); - } - - foreach (var stopOnFirstGoal in new[] { false, true }) - { - var goals = planContext.GetLocationSet(allowEnumerate: true); - foreach (var terminal in planTerminals) - { - goals.Add(terminal); - } - - var start = goals.EnumerateItems().First(); - goals.Remove(start); - WriteDijkstra(stream, planContext, existing, "pipes", pipesId, start, goals, stopOnFirstGoal, allowGoalEnumerate: true, writeFullPrevious: false); - Count(counts, "dijkstra_existing"); - } - } - - private static ILocationSet RandomGoals(Context context, Random random, List candidates, Location start, int maxGoals) - { - var goals = context.GetLocationSet(allowEnumerate: true); - var count = 1 + random.Next(maxGoals); - for (var i = 0; i < count; i++) - { - var goal = candidates[random.Next(candidates.Count)]; - if (goal != start) - { - goals.Add(goal); - } - } - - if (goals.Count == 0) - { - // Every pick was the start (a one-pumpjack field has few candidates). Fall back to - // any other candidate, or to the start itself, which A* answers immediately. - goals.Add(candidates.FirstOrDefault(c => c != start, start)); - } - - return goals; - } - - private static void WriteAStar(Stream stream, Context context, SquareGrid grid, string gridKind, string gridId, Location start, ILocationSet goals, string goalsKind, bool preferNoTurns, int xWeight, int yWeight, List? prefix) - { - var goalItems = goals.EnumerateItems().ToList(); - var outputList = prefix is null ? null : new List(prefix); - var result = AStar.GetShortestPath(context, grid, start, goals, preferNoTurns, xWeight, yWeight, outputList); - WriteLine(stream, w => - { - w.WriteString("kind", "astar"); - w.WriteString(gridKind, gridId); - WritePoint(w, "start", start); - WritePoints(w, "goals", goalItems); - w.WriteString("goalsKind", goalsKind); - w.WriteBoolean("preferNoTurns", preferNoTurns); - w.WriteNumber("xWeight", xWeight); - w.WriteNumber("yWeight", yWeight); - if (prefix is null) - { - w.WriteNull("prefix"); - } - else - { - WritePoints(w, "prefix", prefix); - } - - w.WriteBoolean("success", result.Success); - WritePoint(w, "reached", result.ReachedGoal); - if (result.Success) - { - WritePoints(w, "path", result.Path); - } - else - { - w.WriteNull("path"); - } - }); - } - - private static void WriteDijkstra(Stream stream, Context context, SquareGrid grid, string gridKind, string gridId, Location start, ILocationSet goals, bool stopOnFirstGoal, bool allowGoalEnumerate, bool writeFullPrevious) - { - var goalItems = goals.EnumerateItems().ToList(); - var result = Dijkstras.GetShortestPaths(context, grid, start, goals, stopOnFirstGoal, allowGoalEnumerate); - - // LocationToPrevious values are LocationBitSets, which cannot be enumerated. Every - // previous cell is a grid neighbor, so probe the four in-bounds neighbors instead and - // check that accounts for the whole count. - var previous = new List(); - foreach (var location in result.LocationToPrevious.Keys) - { - var set = result.LocationToPrevious[location]; - var mask = 0; - var neighbors = new[] - { - location.Translate(1, 0), - location.Translate(0, -1), - location.Translate(-1, 0), - location.Translate(0, 1), - }; - for (var i = 0; i < neighbors.Length; i++) - { - var n = neighbors[i]; - if (n.X >= 0 && n.Y >= 0 && n.X < context.Grid.Width && n.Y < context.Grid.Height && set.Contains(n)) - { - mask |= 1 << i; - } - } - - if (System.Numerics.BitOperations.PopCount((uint)mask) != set.Count) - { - throw new InvalidOperationException($"A LocationToPrevious set at {location} in {gridId} holds a cell that is not a neighbor."); - } - - previous.Add(location.X); - previous.Add(location.Y); - previous.Add(mask); - } - - WriteLine(stream, w => - { - w.WriteString("kind", "dijkstra"); - w.WriteString(gridKind, gridId); - WritePoint(w, "start", start); - WritePoints(w, "goals", goalItems); - w.WriteBoolean("stopOnFirstGoal", stopOnFirstGoal); - w.WriteBoolean("allowGoalEnumerate", allowGoalEnumerate); - w.WriteNumber("reachedCount", result.ReachedGoals.Count); - if (allowGoalEnumerate) - { - WritePoints(w, "reached", result.ReachedGoals.EnumerateItems()); - } - else - { - w.WriteStartArray("reachedContains"); - foreach (var goal in goalItems) - { - w.WriteBooleanValue(result.ReachedGoals.Contains(goal)); - } - - w.WriteEndArray(); - } - - w.WriteNumber("previousCount", result.LocationToPrevious.Count); - w.WriteNumber("previousHash", Fnv(previous)); - if (writeFullPrevious) - { - w.WriteStartArray("previous"); - foreach (var value in previous) - { - w.WriteNumberValue(value); - } - - w.WriteEndArray(); - } - }); - } - - private static void WriteBfs(Stream stream, Context context, string gridId, Location start, Location goal) - { - var path = BreadthFirstFinder.GetShortestPath(context, start, goal); - WriteLine(stream, w => - { - w.WriteString("kind", "bfs"); - w.WriteString("grid", gridId); - WritePoint(w, "start", start); - WritePoint(w, "goal", goal); - if (path is null) - { - w.WriteNull("path"); - } - else - { - WritePoints(w, "path", path); - } - }); - } - - private static ILocationDictionary WritePrims(Stream stream, Context context, ILocationDictionary> graph, Location first, bool digraph) - { - var mst = Prims.GetMinimumSpanningTree(context, graph, first, digraph); - WriteLine(stream, w => - { - w.WriteString("kind", "prims"); - w.WriteStartArray("graph"); - foreach (var pair in graph.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(pair.Key.X); - w.WriteNumberValue(pair.Key.Y); - w.WriteStartArray(); - foreach (var edge in pair.Value.EnumeratePairs()) - { - w.WriteNumberValue(edge.Key.X); - w.WriteNumberValue(edge.Key.Y); - w.WriteNumberValue(edge.Value); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - WritePoint(w, "first", first); - w.WriteBoolean("digraph", digraph); - w.WriteStartArray("mst"); - foreach (var pair in mst.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(pair.Key.X); - w.WriteNumberValue(pair.Key.Y); - w.WriteStartArray(); - foreach (var node in pair.Value.EnumerateItems()) - { - w.WriteNumberValue(node.X); - w.WriteNumberValue(node.Y); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - }); - - return mst; - } - - /// - /// The graph ExecuteWithDelaunayMst builds, copied step for step (CollectionExtensions is - /// internal). Null when Delaunator cannot triangulate the centers. - /// - private static ILocationDictionary>? BuildDelaunayGraph(Context context) - { - var centers = context.Centers; - var points = new IPoint[centers.Count]; - for (var i = 0; i < centers.Count; i++) - { - points[i] = new Point(centers[i].X, centers[i].Y); - } - - Delaunator delaunator; - try - { - delaunator = new Delaunator(points); - } - catch (Exception) - { - return null; - } - - var graph = context.GetLocationDictionary>(centers.Count); - foreach (var center in centers) - { - graph.Add(center, context.GetLocationDictionary()); - } - - for (var e = 0; e < delaunator.Triangles.Length; e++) - { - if (e > delaunator.Halfedges[e]) - { - var p = centers[delaunator.Triangles[e]]; - var q = centers[delaunator.Triangles[e % 3 == 2 ? e - 2 : e + 1]]; - var cost = p.GetEuclideanDistanceSquared(q); - graph[p][q] = cost; - graph[q][p] = cost; - } - } - - return graph; - } - - /// CollectionExtensions.MinBy: the first of equal minimums wins. - private static Location FirstMinByDistanceToMiddle(Context context) - { - var middle = context.Grid.Middle; - var best = context.Centers[0]; - var bestKey = middle.GetEuclideanDistanceSquared(best); - foreach (var center in context.Centers.Skip(1)) - { - var key = middle.GetEuclideanDistanceSquared(center); - if (key < bestKey) - { - best = center; - bestKey = key; - } - } - - return best; - } - - /// Guards the copied graph-building above against drifting from the planner's. - private static void CheckMatchesPlanner(Context context, ILocationDictionary mst, string id) - { - var planner = AddPipesConnectedCentersDT.ExecuteWithDelaunayMst(context, context.Centers); - var a = string.Join(";", mst.EnumeratePairs().Select(p => $"{p.Key.X},{p.Key.Y}:" + string.Join(" ", p.Value.EnumerateItems().Select(l => $"{l.X},{l.Y}")))); - var b = string.Join(";", planner.EnumeratePairs().Select(p => $"{p.Key.X},{p.Key.Y}:" + string.Join(" ", p.Value.EnumerateItems().Select(l => $"{l.X},{l.Y}")))); - if (a != b) - { - throw new InvalidOperationException($"The recorded Delaunay MST for {id} does not match AddPipesConnectedCentersDT.ExecuteWithDelaunayMst."); - } - } - - /// FNV-1a over the little-endian bytes of each int. - private static ulong Fnv(List values) - { - var hash = 14695981039346656037UL; - foreach (var value in values) - { - var v = unchecked((uint)value); - for (var b = 0; b < 4; b++) - { - hash ^= (v >> (8 * b)) & 0xFF; - hash *= 1099511628211UL; - } - } - - return hash; - } - - private static void Count(IDictionary counts, string kind, int n = 1) - { - counts[kind] = counts.TryGetValue(kind, out var c) ? c + n : n; - } - - private static void WriteLine(Stream stream, Action body) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - body(writer); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - private static void WritePoint(Utf8JsonWriter w, string name, Location location) - { - w.WriteStartArray(name); - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - w.WriteEndArray(); - } - - /// Points as one flat array: [x0, y0, x1, y1, ...]. - private static void WritePoints(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var location in locations) - { - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - } - - w.WriteEndArray(); - } -} diff --git a/rust/planner/oracle/BeaconsRecorder.cs b/rust/planner/oracle/BeaconsRecorder.cs deleted file mode 100644 index 3b7eaf46..00000000 --- a/rust/planner/oracle/BeaconsRecorder.cs +++ /dev/null @@ -1,591 +0,0 @@ -// Records PlanBeacons for the Rust port (#138), stage 9. -// Writes beacons.jsonl. The format is in rust/planner/FIXTURES.md. -// -// Each case is a real PlanBeacons.Execute call. AddPipes.GetSolution calls it with the pipe set -// PlanUndergroundPipes has already thinned, which is exactly the Pipe, Terminal and -// UndergroundPipe cells a finished plan's grid ends up with, on the planner's grid of -// pumpjacks (and avoid cells, and on the poles-first path, poles). So the recorder plans a -// blueprint, keeps those entities as the case's grid and the pipe cells as its pipe set, and -// calls PlanBeacons.Execute on a context rebuilt from that snapshot - the same rebuild the Rust -// test does, so both start from identical container state. On Aquilo the heat pipes passed in -// come from AddHeatPipes.Route on its own rebuild of the same snapshot, as GetSolution routes -// them before planning beacons. -// -// A built-in check confirms the inputs are the real ones: for every case without heat pipes, -// a selected plan's beacon strategy must give that plan's beacon and effect counts, and every -// beacon on the finished grid must be one of its beacons. Cases that fail it are named -// "_input-unverified" (see the comment in RecordPlan). - -using System.Reflection; -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class BeaconsRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly FieldInfo NextIdField = typeof(SquareGrid).GetField("_nextId", BindingFlags.NonPublic | BindingFlags.Instance) - ?? throw new InvalidOperationException("SquareGrid._nextId not found."); - - private sealed record Ent(int X, int Y, string Kind, int Id, int Extra, int[]? Neighbors); - - private sealed class Snapshot - { - public required OilFieldOptions Options; - public required int PoleSupplyWidth, PoleSupplyHeight; - public required int Width, Height, NextId; - public required List Entities; - public required List Centers; - public required int CenterToTerminalsCount; - } - - private static int _caseCount; - private static int _checked; - private static int _checkMismatches; - private static int _polesFirst; - - public static void Record(string outputDir, IReadOnlyList smallList, IReadOnlyList bigList) - { - var path = Path.Combine(outputDir, "beacons.jsonl"); - _caseCount = 0; - _checked = 0; - _checkMismatches = 0; - _polesFirst = 0; - - var all = OilFieldOptions.AllBeaconStrategies.ToList(); - var sets = new (string Name, Func Options, int Stride, int Offset, bool KeepPoles)[] - { - ("medium_b_o_u", () => Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: false, all), 1, 0, false), - ("big_b", () => Opts(OilFieldOptions.ForBigElectricPole, overlap: false, underground: false, heat: false, all), 2, 1, false), - ("substation_b_o", () => Opts(OilFieldOptions.ForSubstation, overlap: true, underground: false, heat: false, all), 3, 2, false), - ("small_b_u", () => Opts(OilFieldOptions.ForSmallElectricPole, overlap: false, underground: true, heat: false, all), 3, 0, false), - ("nonstandard_b_o_u", () => NonStandard(overlap: true), 2, 0, false), - ("nonstandard_b_u", () => NonStandard(overlap: false), 3, 1, false), - ("medium_heat_b_o", () => Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: true, all), 3, 0, false), - ("medium_heat_b", () => Opts(OilFieldOptions.ForMediumElectricPole, overlap: false, underground: true, heat: true, all), 4, 2, false), - ("medium_b_o_u_poles", () => Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: false, all), 4, 3, true), - ("big_b_poles", () => Opts(OilFieldOptions.ForBigElectricPole, overlap: false, underground: false, heat: false, all), 6, 5, true), - }; - - using (var stream = File.Create(path)) - { - foreach (var set in sets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - RecordPlan(stream, $"small-{i:D4}_{set.Name}", set.Options(), ParseBlueprint.Execute(smallList[i]), Array.Empty(), set.KeepPoles, strategies: null); - } - } - - for (var i = 0; i < bigList.Count; i += 50) - { - RecordPlan(stream, $"big-{i:D4}_medium_b_o_u", Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: false, all), ParseBlueprint.Execute(bigList[i]), Array.Empty(), keepPoles: false, strategies: null); - } - - // PlannerTest.AllowsLocationsToBeAvoided, for AvoidEntity cells. - var avoidOptions = Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: false, all); - RecordPlan(stream, "PlannerTest.AllowsLocationsToBeAvoided", avoidOptions, AvoidBlueprint(), Enumerable.Range(-7, 16).Select(x => new AvoidLocation(x, 0)).ToArray(), keepPoles: false, strategies: null); - - // The strategy list: order, repeats (each runs once), the default list, and none. - var lists = new (string Name, List Strategies)[] - { - ("order", new List { BeaconStrategy.Snug, BeaconStrategy.Fbe, BeaconStrategy.Snug, BeaconStrategy.FbeOriginal, BeaconStrategy.Fbe }), - ("default", OilFieldOptions.DefaultBeaconStrategies.ToList()), - ("none", new List()), - }; - foreach (var (name, strategies) in lists) - { - foreach (var i in new[] { 4, 17, 33 }) - { - RecordPlan(stream, $"small-{i:D4}_medium_b_o_u_strategies-{name}", Opts(OilFieldOptions.ForMediumElectricPole, overlap: true, underground: true, heat: false, all), ParseBlueprint.Execute(smallList[i]), Array.Empty(), keepPoles: false, strategies); - } - } - } - - Console.WriteLine($"beacons.jsonl: {_caseCount} cases, {new FileInfo(path).Length} bytes; selected-plan check: {_checked} checked, {_checkMismatches} differ, {_polesFirst} poles-first"); - } - - private static OilFieldOptions Opts(OilFieldOptions options, bool overlap, bool underground, bool heat, List strategies) - { - options.AddBeacons = true; - options.OverlapBeacons = overlap; - options.UseUndergroundPipes = underground; - options.AddHeatPipes = heat; - options.ValidateSolution = true; - options.BeaconStrategies = strategies.ToList(); - return options; - } - - /// NonStandardBeacon/Theory.cs: a 2x4 beacon with a 4x6 supply area. - private static OilFieldOptions NonStandard(bool overlap) - { - var options = Opts(OilFieldOptions.ForMediumElectricPole, overlap, underground: true, heat: false, OilFieldOptions.AllBeaconStrategies.ToList()); - options.BeaconWidth = 2; - options.BeaconHeight = 4; - options.BeaconSupplyWidth = 4; - options.BeaconSupplyHeight = 6; - return options; - } - - private static Blueprint AvoidBlueprint() => new() - { - Entities = new[] - { - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -3, Y = -5 } }, - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 4, Y = 5 } }, - }, - Icons = new[] - { - new Icon - { - Index = 1, - Signal = new SignalID { Name = EntityNames.Vanilla.Pumpjack, Type = SignalTypes.Vanilla.Item }, - }, - }, - Item = ItemNames.Vanilla.Blueprint, - Version = 0, - }; - - private static void RecordPlan(Stream stream, string name, OilFieldOptions options, Blueprint blueprint, AvoidLocation[] avoid, bool keepPoles, List? strategies) - { - var result = Planner.Execute(options, blueprint, avoid); - var grid = result.Context.Grid; - - var pipes = new List(); - var beacons = new HashSet(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - switch (grid[location]) - { - case Pipe: - pipes.Add(location); - break; - case BeaconCenter: - beacons.Add(location); - break; - } - } - - // A plan whose poles went on first (the planner's poles-first retry) planned its beacons - // with the poles already on the grid. Its beacons only come back with the poles kept, so - // a mismatch is retried that way and the case recorded with its poles. - var checkable = !options.AddHeatPipes && strategies is null - && result.Summary.SelectedPlans.Count > 0 && result.Summary.SelectedPlans[0].BeaconStrategy is not null; - var check = checkable && !keepPoles; - var (snapshot, heatPipes, context, solutions, error) = Run(result, options, keepPoles, strategies); - if (check && solutions is not null && !Matches(result, beacons, solutions)) - { - var retry = Run(result, options, keepPoles: true, strategies); - if (retry.Solutions is not null && Matches(result, beacons, retry.Solutions)) - { - (snapshot, heatPipes, context, solutions, error) = retry; - name += "_poles-first"; - keepPoles = true; - _polesFirst++; - } - } - - if (snapshot is null) - { - // GetSolution does not plan beacons for a layout it cannot heat. - return; - } - - // The inputs are the planner's own only if they give back the plan's beacons. A plan that - // went through the poles-first retry with beacons on placed its poles before planning - // beacons and then ran pole placement again, which can move poles; no snapshot of the - // finished plan has the poles its beacon pass saw. Such a case is still a valid input - // for comparing .NET and Rust, so it is kept and named as unverified. - if (check) - { - _checked++; - if (solutions is null || !Matches(result, beacons, solutions)) - { - _checkMismatches++; - name += "_input-unverified"; - Console.Error.WriteLine($"beacons differ from the plan's: {name}"); - } - } - - _caseCount++; - WriteLine(stream, w => - { - w.WriteString("name", name); - WriteSnapshot(w, snapshot); - WriteLocations(w, "pipes", pipes); - if (heatPipes is null) - { - w.WriteNull("heat_pipes"); - } - else - { - WriteLocations(w, "heat_pipes", heatPipes); - } - - if (error is not null) - { - w.WriteString("error", error); - } - else - { - w.WriteStartArray("solutions"); - foreach (var s in solutions!) - { - w.WriteStartObject(); - w.WriteNumber("strategy", (int)s.Strategy); - WriteLocations(w, "beacons", s.Beacons); - w.WriteNumber("effects", s.Effects); - w.WriteStartArray("counts"); - foreach (var c in s.EffectsGivenCounts) - { - w.WriteNumberValue(c); - } - - w.WriteEndArray(); - w.WriteEndObject(); - } - - w.WriteEndArray(); - } - - WriteLocations(w, "after_entity_locations", context.Grid.EntityLocations.EnumerateItems()); - w.WriteNumber("after_next_id", (int)NextIdField.GetValue(context.Grid)!); - }); - } - - /// Whether the solutions reproduce the plan: some selected plan's beacon strategy - /// gives exactly that plan's beacon and effect counts, and every beacon left on the finished - /// grid is one of that solution's. (A later step can still drop a beacon from the grid, such - /// as one no pole can power, so the grid holds a subset.) - private static bool Matches(PlannerResult result, HashSet beacons, List solutions) - { - foreach (var plan in result.Summary.SelectedPlans) - { - var solution = solutions.SingleOrDefault(s => s.Strategy == plan.BeaconStrategy); - if (solution is not null - && solution.Beacons.Count == plan.BeaconCount - && solution.Effects == plan.BeaconEffectCount - && beacons.IsSubsetOf(solution.Beacons)) - { - return true; - } - } - - return false; - } - - private static (Snapshot? Snapshot, List? HeatPipes, Context? Context, List? Solutions, string? Error) Run( - PlannerResult result, OilFieldOptions options, bool keepPoles, List? strategies) - { - var pipes = Pipes(result.Context.Grid); - var snapshot = Capture(result.Context, keepPoles); - if (strategies is not null) - { - snapshot.Options.BeaconStrategies = strategies.ToList(); - } - - List? heatPipes = null; - if (options.AddHeatPipes) - { - var routeContext = Rebuild(snapshot); - var routed = AddHeatPipes.Route(routeContext, ToSet(routeContext, pipes), out var uncoveredPipes, out var uncoveredCenters); - if (uncoveredPipes.Count > 0 || uncoveredCenters.Count > 0) - { - return (null, null, null, null, null); - } - - heatPipes = routed.EnumerateItems().ToList(); - } - - var context = Rebuild(snapshot); - try - { - var solutions = PlanBeacons.Execute(context, ToSet(context, pipes), heatPipes is null ? null : ToSet(context, heatPipes)); - return (snapshot, heatPipes, context, solutions, null); - } - catch (FactorioToolsException ex) - { - return (snapshot, heatPipes, context, null, ex.Message); - } - } - - /// The Pipe, Terminal and UndergroundPipe cells, in EntityLocations order. - private static List Pipes(SquareGrid grid) - { - var pipes = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - if (grid[location] is Pipe) - { - pipes.Add(location); - } - } - - return pipes; - } - - private static ILocationSet ToSet(Context context, IEnumerable locations) - { - var set = context.GetLocationSet(allowEnumerate: true); - foreach (var location in locations) - { - set.Add(location); - } - - return set; - } - - // --------------------------------------------------------------------------------------- - // Snapshots - - /// The finished grid without pipes, heat pipes, beacons and (unless kept) poles: the - /// grid PlanBeacons sees, minus the pipes it adds itself. - private static Snapshot Capture(Context context, bool keepPoles) - { - var grid = context.Grid; - var entities = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var entity = grid[location]!; - var keep = entity switch - { - Pipe or HeatPipe or BeaconCenter or BeaconSide => false, - ElectricPoleCenter or ElectricPoleSide => keepPoles, - _ => true, - }; - if (keep) - { - entities.Add(ToEnt(location, entity)); - } - } - - var o = context.Options; - var options = new OilFieldOptions - { - BeaconWidth = o.BeaconWidth, - BeaconHeight = o.BeaconHeight, - BeaconSupplyWidth = o.BeaconSupplyWidth, - BeaconSupplyHeight = o.BeaconSupplyHeight, - ElectricPoleWidth = o.ElectricPoleWidth, - ElectricPoleHeight = o.ElectricPoleHeight, - OverlapBeacons = o.OverlapBeacons, - ValidateSolution = o.ValidateSolution, - AddHeatPipes = o.AddHeatPipes, - AddBeacons = o.AddBeacons, - BeaconStrategies = o.BeaconStrategies.ToList(), - }; - - return new Snapshot - { - Options = options, - PoleSupplyWidth = context.ElectricPoleSupplyWidthWithQuality, - PoleSupplyHeight = context.ElectricPoleSupplyHeightWithQuality, - Width = grid.Width, - Height = grid.Height, - NextId = (int)NextIdField.GetValue(grid)!, - Entities = entities, - Centers = context.Centers.ToList(), - CenterToTerminalsCount = context.CenterToTerminals.Count, - }; - } - - private static Ent ToEnt(Location location, GridEntity entity) - { - return entity switch - { - TemporaryEntity => new Ent(location.X, location.Y, "TemporaryEntity", entity.Id, 0, null), - AvoidEntity => new Ent(location.X, location.Y, "AvoidEntity", entity.Id, 0, null), - PumpjackCenter p => new Ent(location.X, location.Y, "PumpjackCenter", entity.Id, (int)p.Direction, null), - PumpjackSide s => new Ent(location.X, location.Y, "PumpjackSide", entity.Id, s.Center.Id, null), - ElectricPoleSide s => new Ent(location.X, location.Y, "ElectricPoleSide", entity.Id, s.Center.Id, null), - ElectricPoleCenter p => new Ent(location.X, location.Y, "ElectricPoleCenter", entity.Id, 0, p.Neighbors.ToArray()), - _ => throw new NotSupportedException(entity.GetType().Name), - }; - } - - /// A fresh context holding the snapshot, built the way the Rust test builds it. - /// Only what PlanBeacons and AddHeatPipes.Route read is filled in. - private static Context Rebuild(Snapshot s) - { - var grid = new PipeGrid(s.Width, s.Height); - var byId = new Dictionary(); - foreach (var e in s.Entities) - { - GridEntity? entity = e.Kind switch - { - "PumpjackCenter" => new PumpjackCenter(e.Id) { Direction = (Direction)e.Extra }, - "ElectricPoleCenter" => new ElectricPoleCenter(e.Id), - _ => null, - }; - if (entity is not null) - { - byId.Add(e.Id, entity); - } - } - - foreach (var e in s.Entities) - { - GridEntity entity = e.Kind switch - { - "PumpjackCenter" or "ElectricPoleCenter" => byId[e.Id], - "PumpjackSide" => new PumpjackSide(e.Id, (PumpjackCenter)byId[e.Extra]), - "ElectricPoleSide" => new ElectricPoleSide(e.Id, (ElectricPoleCenter)byId[e.Extra]), - "TemporaryEntity" => new TemporaryEntity(e.Id), - "AvoidEntity" => new AvoidEntity(e.Id), - _ => throw new NotSupportedException(e.Kind), - }; - grid.AddEntity(new Location(e.X, e.Y), entity); - } - - foreach (var e in s.Entities) - { - if (e.Neighbors is not null) - { - var pole = (ElectricPoleCenter)byId[e.Id]; - foreach (var id in e.Neighbors) - { - pole.Neighbors.Add(id); - } - } - } - - NextIdField.SetValue(grid, s.NextId); - - var o = s.Options; - var options = new OilFieldOptions - { - BeaconWidth = o.BeaconWidth, - BeaconHeight = o.BeaconHeight, - BeaconSupplyWidth = o.BeaconSupplyWidth, - BeaconSupplyHeight = o.BeaconSupplyHeight, - ElectricPoleWidth = o.ElectricPoleWidth, - ElectricPoleHeight = o.ElectricPoleHeight, - OverlapBeacons = o.OverlapBeacons, - ValidateSolution = o.ValidateSolution, - AddHeatPipes = o.AddHeatPipes, - AddBeacons = o.AddBeacons, - BeaconStrategies = o.BeaconStrategies.ToList(), - }; - - // Snug sizes a list by CenterToTerminals.Count; nothing else reads it here. - var centerToTerminals = new LocationHashDictionary>(); - foreach (var center in s.Centers.Take(s.CenterToTerminalsCount)) - { - centerToTerminals.Add(center, new List()); - } - - return new Context - { - Options = options, - InputBlueprint = new Blueprint { Entities = Array.Empty(), Icons = Array.Empty(), Item = ItemNames.Vanilla.Blueprint }, - DeltaX = 0, - DeltaY = 0, - Grid = grid, - Centers = s.Centers.ToList(), - CenterToTerminals = centerToTerminals, - CenterToOriginalDirection = new LocationHashDictionary(), - LocationToTerminals = new LocationHashDictionary>(), - LocationToAdjacentCount = new int[s.Width * s.Height], - SharedInstances = new SharedInstances(grid), - ElectricPoleSupplyWidthWithQuality = s.PoleSupplyWidth, - ElectricPoleSupplyHeightWithQuality = s.PoleSupplyHeight, - }; - } - - private static void WriteSnapshot(Utf8JsonWriter w, Snapshot s) - { - var o = s.Options; - w.WriteStartObject("options"); - w.WriteNumber("beacon_width", o.BeaconWidth); - w.WriteNumber("beacon_height", o.BeaconHeight); - w.WriteNumber("beacon_supply_width", o.BeaconSupplyWidth); - w.WriteNumber("beacon_supply_height", o.BeaconSupplyHeight); - w.WriteNumber("electric_pole_width", o.ElectricPoleWidth); - w.WriteNumber("electric_pole_height", o.ElectricPoleHeight); - w.WriteBoolean("overlap_beacons", o.OverlapBeacons); - w.WriteBoolean("validate_solution", o.ValidateSolution); - w.WriteBoolean("add_heat_pipes", o.AddHeatPipes); - w.WriteBoolean("add_beacons", o.AddBeacons); - w.WriteStartArray("beacon_strategies"); - foreach (var strategy in o.BeaconStrategies) - { - w.WriteNumberValue((int)strategy); - } - - w.WriteEndArray(); - w.WriteEndObject(); - w.WriteStartArray("pole_supply"); - w.WriteNumberValue(s.PoleSupplyWidth); - w.WriteNumberValue(s.PoleSupplyHeight); - w.WriteEndArray(); - w.WriteNumber("width", s.Width); - w.WriteNumber("height", s.Height); - w.WriteNumber("next_id", s.NextId); - w.WriteStartArray("entities"); - foreach (var e in s.Entities) - { - w.WriteStartArray(); - w.WriteNumberValue(e.X); - w.WriteNumberValue(e.Y); - w.WriteStringValue(e.Kind); - w.WriteNumberValue(e.Id); - switch (e.Kind) - { - case "PumpjackCenter": - case "PumpjackSide": - case "ElectricPoleSide": - w.WriteNumberValue(e.Extra); - break; - case "ElectricPoleCenter": - w.WriteStartArray(); - foreach (var n in e.Neighbors!) - { - w.WriteNumberValue(n); - } - - w.WriteEndArray(); - break; - } - - w.WriteEndArray(); - } - - w.WriteEndArray(); - WriteLocations(w, "centers", s.Centers); - w.WriteNumber("center_to_terminals_count", s.CenterToTerminalsCount); - } - - // --------------------------------------------------------------------------------------- - // Writing helpers - - private static void WriteLine(Stream stream, Action body) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - body(writer); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - /// A flat [x0, y0, x1, y1, ...] array. - private static void WriteLocations(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var l in locations) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - } -} diff --git a/rust/planner/oracle/ConnectedCentersRecorder.cs b/rust/planner/oracle/ConnectedCentersRecorder.cs deleted file mode 100644 index 80417dad..00000000 --- a/rust/planner/oracle/ConnectedCentersRecorder.cs +++ /dev/null @@ -1,435 +0,0 @@ -// Records the connected-centers pipe strategies for the Rust port (#138), stage 7. -// Writes connected_centers.jsonl. The format is in rust/planner/FIXTURES.md. -// -// For each case it builds the planner's starting Context (InitializeContext.Execute), then -// for each connected-centers strategy does what AddPipes.GetSolutionGroups does: give the -// context fresh copies of its terminal maps, call AddPipesConnectedCenters.GetConnectedPumpjacks, -// then AddPipesConnectedCenters.FindTrunksAndConnect, and record the graph, the pipes (or the -// error) and the terminal maps those calls leave behind. No C# under src/ is changed. - -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class ConnectedCentersRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly PipeStrategy[] Strategies = - { - PipeStrategy.ConnectedCentersDelaunay, - PipeStrategy.ConnectedCentersDelaunayMst, - PipeStrategy.ConnectedCentersFlute, - }; - - private sealed record Case(string Name, string Preset, bool ValidateSolution, Blueprint Blueprint, AvoidLocation[] Avoid); - - public static void Record(string outputDir, IReadOnlyList smallList, IReadOnlyList bigList) - { - var cases = BuildCases(smallList, bigList); - var path = Path.Combine(outputDir, "connected_centers.jsonl"); - var written = 0; - var skipped = 0; - var runs = 0; - var returnedErrors = 0; - var thrownErrors = 0; - - using (var stream = File.Create(path)) - { - foreach (var c in cases) - { - Context context; - try - { - context = InitializeContext.Execute(Options(c), c.Blueprint, c.Avoid); - } - catch (FactorioToolsException) - { - // Bad inputs (e.g. an avoided cell under a pumpjack) are stage 3's to check. - skipped++; - continue; - } - - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - writer.WriteString("name", c.Name); - writer.WriteString("preset", c.Preset); - writer.WriteBoolean("validate_solution", c.ValidateSolution); - WriteBlueprint(writer, c.Blueprint); - writer.WriteStartArray("avoid"); - foreach (var a in c.Avoid) - { - writer.WriteStartArray(); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(a.X)); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(a.Y)); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - // The starting state, so the Rust test can tell an InitializeContext - // difference apart from a connected-centers one. - WriteLocations(writer, "centers", context.Centers); - WriteTerminalDictionary(writer, "center_to_terminals", context.CenterToTerminals); - WriteTerminalDictionary(writer, "location_to_terminals", context.LocationToTerminals); - - var originalCenterToTerminals = context.CenterToTerminals; - var originalLocationToTerminals = context.LocationToTerminals; - - writer.WriteStartArray("runs"); - foreach (var strategy in Strategies) - { - runs++; - - // AddPipes.GetSolutionGroups: fresh dictionaries and lists, the same - // TerminalLocation objects, in the original enumeration order. - context.CenterToTerminals = CopyTerminals(context, originalCenterToTerminals); - context.LocationToTerminals = CopyTerminals(context, originalLocationToTerminals); - - writer.WriteStartObject(); - writer.WriteNumber("strategy", (int)strategy); - - ILocationDictionary? graph = null; - try - { - graph = AddPipesConnectedCenters.GetConnectedPumpjacks(context, strategy); - } - catch (Exception ex) - { - thrownErrors++; - WriteError(writer, "graph_error", ex); - } - - if (graph is not null) - { - writer.WriteStartArray("graph"); - foreach (var pair in graph.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - WriteFlat(writer, pair.Value.EnumerateItems()); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - Result? result = null; - try - { - result = AddPipesConnectedCenters.FindTrunksAndConnect(context, graph); - } - catch (Exception ex) - { - thrownErrors++; - WriteError(writer, "thrown_error", ex); - } - - if (result is not null) - { - if (result.Exception is not null) - { - returnedErrors++; - WriteError(writer, "returned_error", result.Exception); - } - else - { - writer.WritePropertyName("pipes"); - WriteFlat(writer, result.Data!.EnumerateItems()); - } - } - - WriteTerminalDictionary(writer, "center_to_terminals_after", context.CenterToTerminals); - WriteTerminalDictionary(writer, "location_to_terminals_after", context.LocationToTerminals); - } - - writer.WriteEndObject(); - } - - writer.WriteEndArray(); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - written++; - } - } - - Console.WriteLine($"connected_centers.jsonl: {written} cases ({skipped} skipped as bad input), {runs} strategy runs, {returnedErrors} returned and {thrownErrors} thrown errors, {new FileInfo(path).Length} bytes"); - } - - private static List BuildCases(IReadOnlyList smallList, IReadOnlyList bigList) - { - var cases = new List(); - var none = Array.Empty(); - - for (var i = 0; i < smallList.Count; i++) - { - var blueprint = ParseBlueprint.Execute(smallList[i]); - - // The PlannerOnAllBlueprintsAndOptions theory validates every solution, which also - // runs the bidirectional-graph check in GetConnectedPumpjacks. - cases.Add(new Case($"small-{i:D4}_medium", "ForMediumElectricPole", true, blueprint, none)); - - // Pole presets change the grid's padding, so the same field plans on another grid. - var other = (i % 3) switch - { - 0 => "ForSmallElectricPole", - 1 => "ForBigElectricPole", - _ => "ForSubstation", - }; - cases.Add(new Case($"small-{i:D4}_{other}", other, false, blueprint, none)); - - // Avoided cells near the pumpjacks bend the A* paths and block trunks. - if (i % 3 == 0) - { - cases.Add(new Case($"small-{i:D4}_medium_avoid", "ForMediumElectricPole", true, blueprint, AvoidNearPumpjacks(blueprint, seed: i))); - } - - // A ring of avoided cells around one pumpjack cuts it off, so A* finds no path to - // it. Which pumpjack decides whether the error is thrown or returned in a Result. - if (i % 2 == 0 && Ring(blueprint, pumpjackIndex: i / 2) is { } ring) - { - cases.Add(new Case($"small-{i:D4}_medium_ring", "ForMediumElectricPole", true, blueprint, ring)); - } - } - - for (var i = 0; i < bigList.Count; i += 8) - { - var blueprint = ParseBlueprint.Execute(bigList[i]); - cases.Add(new Case($"big-{i:D4}_medium", "ForMediumElectricPole", true, blueprint, none)); - } - - // PlannerTest.AllowsLocationsToBeAvoided: a wall of avoided cells between the two - // pumpjacks. - var wallBlueprint = new Blueprint - { - Entities = new[] - { - new Entity { EntityNumber = 1, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -3, Y = -5 } }, - new Entity { EntityNumber = 2, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 4, Y = 5 } }, - }, - }; - var wall = Enumerable.Range(-7, 16).Select(x => new AvoidLocation(x, 0)).ToArray(); - cases.Add(new Case("PlannerTest.AllowsLocationsToBeAvoided", "ForMediumElectricPole", true, wallBlueprint, wall)); - - cases.Add(new Case("ring-two-pumpjacks", "ForMediumElectricPole", true, wallBlueprint, Ring(wallBlueprint, pumpjackIndex: 0)!)); - - return cases; - } - - private static OilFieldOptions Options(Case c) - { - var options = c.Preset switch - { - "ForSmallElectricPole" => OilFieldOptions.ForSmallElectricPole, - "ForMediumElectricPole" => OilFieldOptions.ForMediumElectricPole, - "ForBigElectricPole" => OilFieldOptions.ForBigElectricPole, - "ForSubstation" => OilFieldOptions.ForSubstation, - _ => throw new NotSupportedException(c.Preset), - }; - options.ValidateSolution = c.ValidateSolution; - return options; - } - - /// - /// Up to 8 cells two or three tiles from a pumpjack's center, picked with a fixed seed, - /// skipping any cell inside a pumpjack. Offsets are whole numbers, so each lands on a cell. - /// - private static AvoidLocation[] AvoidNearPumpjacks(Blueprint blueprint, int seed) - { - var pumpjacks = blueprint.Entities.Where(e => e.Name == EntityNames.Vanilla.Pumpjack).Select(e => e.Position).ToList(); - var candidates = new List<(float X, float Y)>(); - foreach (var p in pumpjacks) - { - for (var dx = -3; dx <= 3; dx++) - { - for (var dy = -3; dy <= 3; dy++) - { - if (Math.Max(Math.Abs(dx), Math.Abs(dy)) < 2) - { - continue; - } - - var x = p.X + dx; - var y = p.Y + dy; - if (pumpjacks.Any(q => Math.Abs(q.X - x) <= 1 && Math.Abs(q.Y - y) <= 1)) - { - continue; - } - - if (!candidates.Contains((x, y))) - { - candidates.Add((x, y)); - } - } - } - } - - var random = new Random(seed); - var picked = new List(); - while (picked.Count < 8 && candidates.Count > 0) - { - var index = random.Next(candidates.Count); - picked.Add(new AvoidLocation(candidates[index].X, candidates[index].Y)); - candidates.RemoveAt(index); - } - - return picked.ToArray(); - } - - /// - /// The cells exactly three tiles (Chebyshev) from one pumpjack's center, which encloses - /// its terminals. Null when the ring would cross another pumpjack. - /// - private static AvoidLocation[]? Ring(Blueprint blueprint, int pumpjackIndex) - { - var pumpjacks = blueprint.Entities.Where(e => e.Name == EntityNames.Vanilla.Pumpjack).Select(e => e.Position).ToList(); - if (pumpjacks.Count < 2) - { - return null; - } - - var center = pumpjacks[pumpjackIndex % pumpjacks.Count]; - var ring = new List(); - for (var dx = -3; dx <= 3; dx++) - { - for (var dy = -3; dy <= 3; dy++) - { - if (Math.Max(Math.Abs(dx), Math.Abs(dy)) != 3) - { - continue; - } - - var x = center.X + dx; - var y = center.Y + dy; - if (pumpjacks.Any(q => Math.Abs(q.X - x) <= 1 && Math.Abs(q.Y - y) <= 1)) - { - return null; - } - - ring.Add(new AvoidLocation(x, y)); - } - } - - return ring.ToArray(); - } - - private static ILocationDictionary> CopyTerminals(Context context, ILocationDictionary> original) - { - var copy = context.GetLocationDictionary>(original.Count); - foreach (var pair in original.EnumeratePairs()) - { - copy.Add(pair.Key, new List(pair.Value)); - } - - return copy; - } - - private static void WriteError(Utf8JsonWriter writer, string name, Exception ex) - { - writer.WriteStartObject(name); - writer.WriteString("type", ex.GetType().Name); - writer.WriteString("message", ex.Message); - if (ex is NoPathBetweenTerminalsException noPath) - { - writer.WriteStartArray("a"); - writer.WriteNumberValue(noPath.LocationA.X); - writer.WriteNumberValue(noPath.LocationA.Y); - writer.WriteEndArray(); - writer.WriteStartArray("b"); - writer.WriteNumberValue(noPath.LocationB.X); - writer.WriteNumberValue(noPath.LocationB.Y); - writer.WriteEndArray(); - } - - writer.WriteEndObject(); - } - - private static void WriteFlat(Utf8JsonWriter writer, IEnumerable locations) - { - writer.WriteStartArray(); - foreach (var l in locations) - { - writer.WriteNumberValue(l.X); - writer.WriteNumberValue(l.Y); - } - - writer.WriteEndArray(); - } - - private static void WriteLocations(Utf8JsonWriter writer, string name, IEnumerable locations) - { - writer.WritePropertyName(name); - WriteFlat(writer, locations); - } - - /// [entity_number, name, x_bits, y_bits, direction or null, mirror or null], as in initialize_context.jsonl. - private static void WriteBlueprint(Utf8JsonWriter writer, Blueprint blueprint) - { - writer.WriteStartArray("entities"); - foreach (var e in blueprint.Entities) - { - writer.WriteStartArray(); - writer.WriteNumberValue(e.EntityNumber); - writer.WriteStringValue(e.Name); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(e.Position.X)); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(e.Position.Y)); - if (e.Direction.HasValue) - { - writer.WriteNumberValue((int)e.Direction.Value); - } - else - { - writer.WriteNullValue(); - } - - if (e.Mirror.HasValue) - { - writer.WriteBooleanValue(e.Mirror.Value); - } - else - { - writer.WriteNullValue(); - } - - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - - /// [[key_x, key_y, [[center_x, center_y, terminal_x, terminal_y, direction], ...]], ...]. - private static void WriteTerminalDictionary(Utf8JsonWriter writer, string name, ILocationDictionary> dictionary) - { - writer.WriteStartArray(name); - foreach (var pair in dictionary.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - writer.WriteStartArray(); - foreach (var t in pair.Value) - { - writer.WriteStartArray(); - writer.WriteNumberValue(t.Center.X); - writer.WriteNumberValue(t.Center.Y); - writer.WriteNumberValue(t.Terminal.X); - writer.WriteNumberValue(t.Terminal.Y); - writer.WriteNumberValue((int)t.Direction); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } -} diff --git a/rust/planner/oracle/FbePipesRecorder.cs b/rust/planner/oracle/FbePipesRecorder.cs deleted file mode 100644 index 3b05652b..00000000 --- a/rust/planner/oracle/FbePipesRecorder.cs +++ /dev/null @@ -1,327 +0,0 @@ -// Records AddPipesFbe.Execute for the Rust port (#138), stage 6. -// Writes fbe_pipes.jsonl. The format is in rust/planner/FIXTURES.md. - -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class FbePipesRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private sealed record Case(string Name, OilFieldOptions Options, Blueprint Blueprint, AvoidLocation[] Avoid, bool PolesFirst, bool Walled = false); - - public static void Record(string outputDir, IReadOnlyList smallList, IReadOnlyList bigList) - { - var cases = new List(); - for (var i = 0; i < smallList.Count; i++) - { - var blueprint = ParseBlueprint.Execute(smallList[i]); - - // The grid's size depends on AddBeacons and the pole's footprint, and the middle - // (which FBE measures distances from) moves with it, so cover each shape. - cases.Add(new Case($"small-{i:D4}_medium_b", Opts(OilFieldOptions.ForMediumElectricPole, beacons: true), blueprint, [], false)); - cases.Add(new Case($"small-{i:D4}_medium", Opts(OilFieldOptions.ForMediumElectricPole, beacons: false), blueprint, [], false)); - if (i % 2 == 0) - { - cases.Add(new Case($"small-{i:D4}_substation_b", Opts(OilFieldOptions.ForSubstation, beacons: true), blueprint, [], false)); - } - else - { - cases.Add(new Case($"small-{i:D4}_big", Opts(OilFieldOptions.ForBigElectricPole, beacons: false), blueprint, [], false)); - } - - // Poles placed before pipes (the planner's retry mode): the poles block cells and - // remove the terminals they cover. - if (i % 3 == 0) - { - cases.Add(new Case($"small-{i:D4}_medium_poles-first", Opts(OilFieldOptions.ForMediumElectricPole, beacons: false), blueprint, [], true)); - } - - // Not a real planner input: the last pumpjack's terminals are walled in, so FBE - // cannot reach them. This drives the failure paths (no path between terminals, - // the FbeOriginal fallback, the thrown "should have" checks), which real inputs - // rarely reach. - if (i % 4 == 0) - { - cases.Add(new Case($"small-{i:D4}_medium_walled", Opts(OilFieldOptions.ForMediumElectricPole, beacons: false), blueprint, [], false, Walled: true)); - } - } - - for (var i = 0; i < bigList.Count; i += 20) - { - cases.Add(new Case($"big-{i:D4}_medium_b", Opts(OilFieldOptions.ForMediumElectricPole, beacons: true), ParseBlueprint.Execute(bigList[i]), [], false)); - } - - cases.Add(AvoidCase()); - - var path = Path.Combine(outputDir, "fbe_pipes.jsonl"); - var count = 0; - var errors = 0; - var fallbacks = 0; - using (var stream = File.Create(path)) - { - foreach (var c in cases) - { - foreach (var strategy in new[] { PipeStrategy.FbeOriginal, PipeStrategy.Fbe }) - { - var context = Prepare(c); - if (context is null) - { - continue; - } - - var outcome = Write(stream, $"{c.Name}_{strategy}", strategy, context); - count++; - if (outcome == Outcome.Error) - { - errors++; - } - else if (outcome == Outcome.Fallback) - { - fallbacks++; - } - } - } - } - - Console.WriteLine($"fbe_pipes.jsonl: {count} cases ({errors} errors, {fallbacks} FbeOriginal fallbacks to Fbe), {new FileInfo(path).Length} bytes"); - } - - private static OilFieldOptions Opts(OilFieldOptions options, bool beacons) - { - options.AddBeacons = beacons; - options.ValidateSolution = true; - return options; - } - - private static Case AvoidCase() - { - // From PlannerTest.AllowsLocationsToBeAvoided. - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - options.AddBeacons = true; - var blueprint = new Blueprint - { - Entities = new[] - { - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -3, Y = -5 } }, - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 4, Y = 5 } }, - }, - Icons = new[] - { - new Icon - { - Index = 1, - Signal = new SignalID - { - Name = EntityNames.Vanilla.Pumpjack, - Type = SignalTypes.Vanilla.Item, - } - } - }, - Item = ItemNames.Vanilla.Blueprint, - Version = 0, - }; - var avoid = Enumerable.Range(-7, 16).Select(x => new AvoidLocation(x, 0)).ToArray(); - return new Case("PlannerTest.AllowsLocationsToBeAvoided", options, blueprint, avoid, false); - } - - /// - /// The context AddPipes.GetSolutionGroups hands AddPipesFbe.Execute: the starting context - /// with the terminal maps cloned (AddPipes.0.cs ~:443). Null when the planner would not call - /// FBE (one pumpjack is handled without a strategy) or the poles-first placement fails. - /// - private static Context? Prepare(Case c) - { - var context = InitializeContext.Execute(c.Options, c.Blueprint, c.Avoid); - if (c.PolesFirst) - { - var poles = AddElectricPoles.Execute(context, EmptyLocationSet.Instance, allowRetries: false); - if (poles is null) - { - return null; - } - - // Planner.cs: remove terminals overlapping with the added poles. - foreach (var terminals in context.CenterToTerminals.Values) - { - for (int i = 0; i < terminals.Count; i++) - { - var terminal = terminals[i]; - if (context.Grid.IsEmpty(terminal.Terminal)) - { - continue; - } - - terminals.RemoveAt(i); - context.LocationToTerminals[terminal.Terminal].Remove(terminal); - i--; - } - } - } - - if (context.CenterToTerminals.Count <= 1) - { - return null; - } - - if (c.Walled) - { - var center = context.Centers[context.Centers.Count - 1]; - foreach (var terminal in context.CenterToTerminals[center]) - { - foreach (var (dx, dy) in new[] { (1, 0), (0, -1), (-1, 0), (0, 1) }) - { - var cell = terminal.Terminal.Translate(dx, dy); - if (context.Grid.IsInBounds(cell) - && context.Grid.IsEmpty(cell) - && !context.LocationToTerminals.ContainsKey(cell)) - { - context.Grid.AddEntity(cell, new TemporaryEntity(context.Grid.GetId())); - } - } - } - } - - context.CenterToTerminals = Clone(context, context.CenterToTerminals); - context.LocationToTerminals = Clone(context, context.LocationToTerminals); - return context; - } - - /// CollectionExtensions.ToDictionary (internal), as GetSolutionGroups uses it. - private static ILocationDictionary> Clone(Context context, ILocationDictionary> original) - { - var pairs = original.EnumeratePairs(); - var dictionary = context.GetLocationDictionary>(pairs.Count); - foreach (var pair in pairs) - { - dictionary.Add(pair.Key, pair.Value.ToList()); - } - - return dictionary; - } - - private enum Outcome { Data, Fallback, Error } - - private static Outcome Write(Stream stream, string name, PipeStrategy strategy, Context context) - { - using var w = new Utf8JsonWriter(stream, WriterOptions); - w.WriteStartObject(); - w.WriteString("name", name); - w.WriteNumber("strategy", (int)strategy); - w.WriteNumber("width", context.Grid.Width); - w.WriteNumber("height", context.Grid.Height); - WritePoint(w, "middle", context.Grid.Middle); - WriteLocations(w, "occupied", context.Grid.EntityLocations.EnumerateItems()); - WriteLocations(w, "centers", context.Centers); - WriteTerminals(w, "center_to_terminals", context.CenterToTerminals); - WriteTerminals(w, "location_to_terminals", context.LocationToTerminals); - - Outcome outcome; - Result? result = null; - Exception? thrown = null; - try - { - result = AddPipesFbe.Execute(context, strategy); - } - catch (Exception ex) - { - thrown = ex; - } - - var exception = thrown ?? result!.Exception; - if (exception is not null) - { - outcome = Outcome.Error; - w.WriteStartObject("error"); - w.WriteString("type", exception.GetType().Name); - w.WriteString("message", exception.Message); - w.WriteBoolean("thrown", thrown is not null); - if (exception is FactorioToolsException fte) - { - w.WriteBoolean("bad_input", fte.BadInput); - } - - if (exception is NoPathBetweenTerminalsException noPath) - { - w.WriteStartArray("no_path"); - w.WriteNumberValue(noPath.LocationA.X); - w.WriteNumberValue(noPath.LocationA.Y); - w.WriteNumberValue(noPath.LocationB.X); - w.WriteNumberValue(noPath.LocationB.Y); - w.WriteEndArray(); - } - - w.WriteEndObject(); - } - else - { - var (pipes, finalStrategy) = result!.Data!; - outcome = finalStrategy != strategy ? Outcome.Fallback : Outcome.Data; - w.WriteNumber("final_strategy", (int)finalStrategy); - WriteLocations(w, "pipes", pipes.EnumerateItems()); - } - - // The terminal maps afterwards: EliminateOtherTerminals on success, untouched on failure. - WriteTerminals(w, "center_to_terminals_after", context.CenterToTerminals); - WriteTerminals(w, "location_to_terminals_after", context.LocationToTerminals); - - w.WriteEndObject(); - w.Flush(); - stream.WriteByte((byte)'\n'); - return outcome; - } - - private static void WritePoint(Utf8JsonWriter w, string name, Location l) - { - w.WriteStartArray(name); - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - w.WriteEndArray(); - } - - /// A flat [x0, y0, x1, y1, ...] array, in the given order. - private static void WriteLocations(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var l in locations) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - } - - /// [[key x, key y, [center x, center y, terminal x, terminal y, direction, ...]], ...] in enumeration order. - private static void WriteTerminals(Utf8JsonWriter w, string name, ILocationDictionary> dictionary) - { - w.WriteStartArray(name); - foreach (var pair in dictionary.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(pair.Key.X); - w.WriteNumberValue(pair.Key.Y); - w.WriteStartArray(); - foreach (var t in pair.Value) - { - w.WriteNumberValue(t.Center.X); - w.WriteNumberValue(t.Center.Y); - w.WriteNumberValue(t.Terminal.X); - w.WriteNumberValue(t.Terminal.Y); - w.WriteNumberValue((int)t.Direction); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } -} diff --git a/rust/planner/oracle/HelpersRecorder.cs b/rust/planner/oracle/HelpersRecorder.cs deleted file mode 100644 index 600c21b8..00000000 --- a/rust/planner/oracle/HelpersRecorder.cs +++ /dev/null @@ -1,1927 +0,0 @@ -// Records Helpers and Validate for the Rust port (#138), stage 4. -// Writes helpers.jsonl. The format is in rust/planner/FIXTURES.md. -// -// Every case runs on a context the recorder rebuilds from a recorded snapshot, so .NET and -// the Rust test start from the same container state: entities added in the recorded order -// into fresh, compact sets and dictionaries. A real plan's grid can have freed slots that a -// rebuild does not, and the helpers that change the grid would then order things differently. - -using System.Reflection; -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; -using static Knapcode.FactorioTools.OilField.Helpers; -// The core's own alias (CollectionExtensions.cs) is global only inside that project. The -// recorder builds with the default USE_BITARRAY, so it is the wrapper type here too. -using CountedBitArray = Knapcode.FactorioTools.OilField.WrapperCountedBitArray; - -static class HelpersRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly FieldInfo NextIdField = typeof(SquareGrid).GetField("_nextId", BindingFlags.NonPublic | BindingFlags.Instance) - ?? throw new InvalidOperationException("SquareGrid._nextId not found."); - - /// How many contexts keep the full candidate list, for reading a mismatch. - private const int FullListCases = 4; - - /// The most provider placements a simulated beacon or pole loop records. - private const int MaxSteps = 60; - - private sealed record Ent(int X, int Y, string Kind, int Id, int Extra, int[]? Neighbors); - - private sealed record Term(int Cx, int Cy, int Tx, int Ty, int Dir); - - private sealed class Snapshot - { - public required string Name; - public required int BeaconWidth, BeaconHeight, BeaconSupplyWidth, BeaconSupplyHeight; - public required int PoleWidth, PoleHeight, PoleSupplyWidth, PoleSupplyHeight; - public required bool OverlapBeacons, ValidateSolution, AddHeatPipes, AddBeacons; - public required int Width, Height, NextId; - public required List Entities; - public required List Centers; - public required List<(Location Key, List Terminals)> CenterToTerminals; - public required List<(Location Key, List Terminals)> LocationToTerminals; - public required List? HeatPipes; - } - - private sealed class Fnv - { - private ulong _hash = 1469598103934665603UL; - - public Fnv Mix(int value) - { - var v = unchecked((uint)value); - for (var b = 0; b < 4; b++) - { - _hash ^= (v >> (8 * b)) & 0xFF; - _hash *= 1099511628211UL; - } - - return this; - } - - public Fnv Mix(double value) - { - var bits = BitConverter.DoubleToInt64Bits(value); - Mix(unchecked((int)(bits & 0xFFFFFFFF))); - return Mix(unchecked((int)(bits >> 32))); - } - - public Fnv Mix(Location location) => Mix(location.X).Mix(location.Y); - - public string Hex => _hash.ToString("x16"); - } - - private static int _contextCount; - private static int _caseCount; - - public static void Record(string outputDir, IReadOnlyList smallList) - { - var path = Path.Combine(outputDir, "helpers.jsonl"); - _contextCount = 0; - _caseCount = 0; - using (var stream = File.Create(path)) - { - var fullLists = 0; - - // Finished plans, and the same grids before poles and before beacons and poles, the - // states the pole and beacon passes start from. - var planSets = new (string Name, Func Options, int Stride, int Offset, bool Derive)[] - { - ("medium_b_o_u", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: false), 4, 0, true), - ("substation_b", () => Opts(OilFieldOptions.ForSubstation, beacons: true, overlap: false, heat: false), 8, 1, true), - ("small_u", () => Opts(OilFieldOptions.ForSmallElectricPole, beacons: false, overlap: false, heat: false), 8, 2, true), - ("big_b", () => Opts(OilFieldOptions.ForBigElectricPole, beacons: true, overlap: false, heat: false), 8, 5, true), - ("medium_heat_b", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: true), 8, 3, false), - ("medium_heat", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: false, overlap: false, heat: true), 8, 6, false), - }; - - foreach (var set in planSets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - var name = $"small-{i:D4}_{set.Name}"; - var result = Planner.Execute(set.Options(), ParseBlueprint.Execute(smallList[i])); - var final = Capture(name, result.Context, _ => false); - var finalId = WriteSnapshot(stream, final); - var full = fullLists++ < FullListCases; - - RecordFinal(stream, finalId, final, full); - - if (set.Derive) - { - var noPoles = Capture(name + "_no-poles", result.Context, e => e is ElectricPoleCenter or ElectricPoleSide); - var noPolesId = WriteSnapshot(stream, noPoles); - RecordNoPoles(stream, noPolesId, noPoles, full); - - if (final.AddBeacons) - { - var bare = Capture(name + "_no-beacons", result.Context, e => e is ElectricPoleCenter or ElectricPoleSide or BeaconCenter or BeaconSide); - var bareId = WriteSnapshot(stream, bare); - RecordNoBeacons(stream, bareId, bare, full); - } - } - } - } - - // Starting contexts, before any pipe, for the terminal helpers. - for (var i = 0; i < smallList.Count; i += 4) - { - var name = $"small-{i:D4}_initial"; - var context = InitializeContext.Execute(OilFieldOptions.ForMediumElectricPole, ParseBlueprint.Execute(smallList[i]), Array.Empty()); - var snapshot = Capture(name, context, _ => false); - var id = WriteSnapshot(stream, snapshot); - RecordInitial(stream, id, snapshot); - } - - RecordGeometry(stream); - } - - Console.WriteLine($"helpers.jsonl: {_contextCount} contexts, {_caseCount} cases, {new FileInfo(path).Length} bytes"); - } - - private static OilFieldOptions Opts(OilFieldOptions options, bool beacons, bool overlap, bool heat) - { - options.AddBeacons = beacons; - options.OverlapBeacons = overlap; - options.AddHeatPipes = heat; - options.ValidateSolution = true; - return options; - } - - // --------------------------------------------------------------------------------------- - // Snapshots - - private static Snapshot Capture(string name, Context context, Func exclude) - { - var grid = context.Grid; - var entities = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var entity = grid[location]!; - if (exclude(entity)) - { - continue; - } - - entities.Add(ToEnt(location, entity)); - } - - // Excluded poles are gone, so drop them from the remaining poles' neighbor lists. Only - // poles are ever excluded together with all other poles, so this leaves no pole. - return new Snapshot - { - Name = name, - BeaconWidth = context.Options.BeaconWidth, - BeaconHeight = context.Options.BeaconHeight, - BeaconSupplyWidth = context.Options.BeaconSupplyWidth, - BeaconSupplyHeight = context.Options.BeaconSupplyHeight, - PoleWidth = context.Options.ElectricPoleWidth, - PoleHeight = context.Options.ElectricPoleHeight, - PoleSupplyWidth = context.ElectricPoleSupplyWidthWithQuality, - PoleSupplyHeight = context.ElectricPoleSupplyHeightWithQuality, - OverlapBeacons = context.Options.OverlapBeacons, - ValidateSolution = context.Options.ValidateSolution, - AddHeatPipes = context.Options.AddHeatPipes, - AddBeacons = context.Options.AddBeacons, - Width = grid.Width, - Height = grid.Height, - NextId = (int)NextIdField.GetValue(grid)!, - Entities = entities, - Centers = context.Centers.ToList(), - CenterToTerminals = context.CenterToTerminals.EnumeratePairs().Select(p => (p.Key, p.Value.Select(ToTerm).ToList())).ToList(), - LocationToTerminals = context.LocationToTerminals.EnumeratePairs().Select(p => (p.Key, p.Value.Select(ToTerm).ToList())).ToList(), - HeatPipes = context.HeatPipes?.EnumerateItems().ToList(), - }; - } - - private static Term ToTerm(TerminalLocation t) => new(t.Center.X, t.Center.Y, t.Terminal.X, t.Terminal.Y, (int)t.Direction); - - private static Ent ToEnt(Location location, GridEntity entity) - { - return entity switch - { - UndergroundPipe u => new Ent(location.X, location.Y, "UndergroundPipe", entity.Id, (int)u.Direction, null), - Terminal => new Ent(location.X, location.Y, "Terminal", entity.Id, 0, null), - Pipe => new Ent(location.X, location.Y, "Pipe", entity.Id, 0, null), - HeatPipe => new Ent(location.X, location.Y, "HeatPipe", entity.Id, 0, null), - BeaconCenter => new Ent(location.X, location.Y, "BeaconCenter", entity.Id, 0, null), - TemporaryEntity => new Ent(location.X, location.Y, "TemporaryEntity", entity.Id, 0, null), - AvoidEntity => new Ent(location.X, location.Y, "AvoidEntity", entity.Id, 0, null), - PumpjackCenter p => new Ent(location.X, location.Y, "PumpjackCenter", entity.Id, (int)p.Direction, null), - PumpjackSide s => new Ent(location.X, location.Y, "PumpjackSide", entity.Id, s.Center.Id, null), - BeaconSide s => new Ent(location.X, location.Y, "BeaconSide", entity.Id, s.Center.Id, null), - ElectricPoleSide s => new Ent(location.X, location.Y, "ElectricPoleSide", entity.Id, s.Center.Id, null), - ElectricPoleCenter p => new Ent(location.X, location.Y, "ElectricPoleCenter", entity.Id, 0, p.Neighbors.ToArray()), - _ => throw new NotSupportedException(entity.GetType().Name), - }; - } - - /// A fresh context holding the snapshot, built the way the Rust test builds it. - private static Context Rebuild(Snapshot s) - { - var options = new OilFieldOptions - { - BeaconWidth = s.BeaconWidth, - BeaconHeight = s.BeaconHeight, - BeaconSupplyWidth = s.BeaconSupplyWidth, - BeaconSupplyHeight = s.BeaconSupplyHeight, - ElectricPoleWidth = s.PoleWidth, - ElectricPoleHeight = s.PoleHeight, - OverlapBeacons = s.OverlapBeacons, - ValidateSolution = s.ValidateSolution, - AddHeatPipes = s.AddHeatPipes, - AddBeacons = s.AddBeacons, - }; - - var grid = new PipeGrid(s.Width, s.Height); - var byId = new Dictionary(); - foreach (var e in s.Entities) - { - GridEntity? entity = e.Kind switch - { - "PumpjackCenter" => new PumpjackCenter(e.Id) { Direction = (Direction)e.Extra }, - "BeaconCenter" => new BeaconCenter(e.Id), - "ElectricPoleCenter" => new ElectricPoleCenter(e.Id), - _ => null, - }; - if (entity is not null) - { - byId.Add(e.Id, entity); - } - } - - foreach (var e in s.Entities) - { - GridEntity entity = e.Kind switch - { - "PumpjackCenter" or "BeaconCenter" or "ElectricPoleCenter" => byId[e.Id], - "PumpjackSide" => new PumpjackSide(e.Id, (PumpjackCenter)byId[e.Extra]), - "BeaconSide" => new BeaconSide(e.Id, (BeaconCenter)byId[e.Extra]), - "ElectricPoleSide" => new ElectricPoleSide(e.Id, (ElectricPoleCenter)byId[e.Extra]), - "Pipe" => new Pipe(e.Id), - "Terminal" => new Terminal(e.Id), - "UndergroundPipe" => new UndergroundPipe(e.Id, (Direction)e.Extra), - "HeatPipe" => new HeatPipe(e.Id), - "TemporaryEntity" => new TemporaryEntity(e.Id), - "AvoidEntity" => new AvoidEntity(e.Id), - _ => throw new NotSupportedException(e.Kind), - }; - grid.AddEntity(new Location(e.X, e.Y), entity); - } - - foreach (var e in s.Entities) - { - if (e.Neighbors is not null) - { - var pole = (ElectricPoleCenter)byId[e.Id]; - foreach (var id in e.Neighbors) - { - pole.Neighbors.Add(id); - } - } - } - - NextIdField.SetValue(grid, s.NextId); - - var context = new Context - { - Options = options, - InputBlueprint = new Blueprint { Entities = Array.Empty(), Icons = Array.Empty(), Item = ItemNames.Vanilla.Blueprint }, - DeltaX = 0, - DeltaY = 0, - Grid = grid, - Centers = s.Centers.ToList(), - CenterToTerminals = new LocationHashDictionary>(), - CenterToOriginalDirection = new LocationHashDictionary(), - LocationToTerminals = new LocationHashDictionary>(), - LocationToAdjacentCount = new int[s.Width * s.Height], - SharedInstances = new SharedInstances(grid), - ElectricPoleSupplyWidthWithQuality = s.PoleSupplyWidth, - ElectricPoleSupplyHeightWithQuality = s.PoleSupplyHeight, - }; - - // The two terminal dictionaries share TerminalLocation objects, as the planner's do. - var terminals = new Dictionary(); - TerminalLocation Get(Term t) - { - if (!terminals.TryGetValue(t, out var terminal)) - { - terminal = new TerminalLocation(new Location(t.Cx, t.Cy), new Location(t.Tx, t.Ty), (Direction)t.Dir); - terminals.Add(t, terminal); - } - - return terminal; - } - - foreach (var (key, list) in s.CenterToTerminals) - { - context.CenterToTerminals.Add(key, list.Select(Get).ToList()); - } - - foreach (var (key, list) in s.LocationToTerminals) - { - context.LocationToTerminals.Add(key, list.Select(Get).ToList()); - } - - if (s.HeatPipes is not null) - { - var heatPipes = context.GetLocationSet(allowEnumerate: true); - foreach (var location in s.HeatPipes) - { - heatPipes.Add(location); - } - - context.HeatPipes = heatPipes; - } - - return context; - } - - private static int WriteSnapshot(Stream stream, Snapshot s) - { - var id = _contextCount++; - WriteLine(stream, w => - { - w.WriteString("kind", "context"); - w.WriteNumber("id", id); - w.WriteString("name", s.Name); - w.WriteStartObject("options"); - w.WriteNumber("beacon_width", s.BeaconWidth); - w.WriteNumber("beacon_height", s.BeaconHeight); - w.WriteNumber("beacon_supply_width", s.BeaconSupplyWidth); - w.WriteNumber("beacon_supply_height", s.BeaconSupplyHeight); - w.WriteNumber("electric_pole_width", s.PoleWidth); - w.WriteNumber("electric_pole_height", s.PoleHeight); - w.WriteBoolean("overlap_beacons", s.OverlapBeacons); - w.WriteBoolean("validate_solution", s.ValidateSolution); - w.WriteBoolean("add_heat_pipes", s.AddHeatPipes); - w.WriteBoolean("add_beacons", s.AddBeacons); - w.WriteEndObject(); - w.WriteStartArray("pole_supply"); - w.WriteNumberValue(s.PoleSupplyWidth); - w.WriteNumberValue(s.PoleSupplyHeight); - w.WriteEndArray(); - w.WriteNumber("width", s.Width); - w.WriteNumber("height", s.Height); - w.WriteNumber("next_id", s.NextId); - w.WriteStartArray("entities"); - foreach (var e in s.Entities) - { - w.WriteStartArray(); - w.WriteNumberValue(e.X); - w.WriteNumberValue(e.Y); - w.WriteStringValue(e.Kind); - w.WriteNumberValue(e.Id); - switch (e.Kind) - { - case "PumpjackCenter": - case "PumpjackSide": - case "BeaconSide": - case "ElectricPoleSide": - case "UndergroundPipe": - w.WriteNumberValue(e.Extra); - break; - case "ElectricPoleCenter": - w.WriteStartArray(); - foreach (var n in e.Neighbors!) - { - w.WriteNumberValue(n); - } - - w.WriteEndArray(); - break; - } - - w.WriteEndArray(); - } - - w.WriteEndArray(); - WriteLocations(w, "centers", s.Centers); - WriteTerminals(w, "center_to_terminals", s.CenterToTerminals); - WriteTerminals(w, "location_to_terminals", s.LocationToTerminals); - if (s.HeatPipes is null) - { - w.WriteNull("heat_pipes"); - } - else - { - WriteLocations(w, "heat_pipes", s.HeatPipes); - } - }); - - return id; - } - - private static void WriteTerminals(Utf8JsonWriter w, string name, List<(Location Key, List Terminals)> dictionary) - { - w.WriteStartArray(name); - foreach (var (key, list) in dictionary) - { - w.WriteStartArray(); - w.WriteNumberValue(key.X); - w.WriteNumberValue(key.Y); - w.WriteStartArray(); - foreach (var t in list) - { - w.WriteNumberValue(t.Cx); - w.WriteNumberValue(t.Cy); - w.WriteNumberValue(t.Tx); - w.WriteNumberValue(t.Ty); - w.WriteNumberValue(t.Dir); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } - - // --------------------------------------------------------------------------------------- - // Writing helpers - - private static void WriteLine(Stream stream, Action body) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - body(writer); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - private static void Case(Stream stream, string op, int? ctx, Action inputs, Action outputs) - { - _caseCount++; - WriteLine(stream, w => - { - w.WriteString("kind", "case"); - w.WriteString("op", op); - if (ctx is not null) - { - w.WriteNumber("ctx", ctx.Value); - } - - inputs(w); - w.WriteStartObject("out"); - outputs(w); - w.WriteEndObject(); - }); - } - - private static void WriteLocations(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var l in locations) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - } - - private static void WritePoint(Utf8JsonWriter w, string name, Location l) - { - w.WriteStartArray(name); - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - w.WriteEndArray(); - } - - private static string Bits(CountedBitArray bits) - { - var chars = new char[bits.Count]; - for (var i = 0; i < bits.Count; i++) - { - chars[i] = bits[i] ? '1' : '0'; - } - - return new string(chars); - } - - private static void WriteSetDictionary(Utf8JsonWriter w, string name, ILocationDictionary dictionary) - { - w.WriteStartArray(name); - foreach (var (key, set) in dictionary.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(key.X); - w.WriteNumberValue(key.Y); - w.WriteStartArray(); - foreach (var l in set.EnumerateItems()) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } - - // --------------------------------------------------------------------------------------- - // Hashes, defined the same way in rust/planner/tests/helpers.rs - - private static int KindCode(GridEntity e) => e switch - { - PumpjackCenter => 0, - PumpjackSide => 1, - BeaconCenter => 2, - BeaconSide => 3, - ElectricPoleCenter => 4, - ElectricPoleSide => 5, - UndergroundPipe => 8, - Terminal => 7, - Pipe => 6, - HeatPipe => 9, - TemporaryEntity => 10, - AvoidEntity => 11, - _ => throw new NotSupportedException(e.GetType().Name), - }; - - /// Every entity in enumeration order, with its id and per-kind state, then the - /// next id. Reading the next id takes it, so this is the last thing done to a grid. - private static string HashGrid(SquareGrid grid) - { - var h = new Fnv(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var e = grid[location]!; - h.Mix(location).Mix(KindCode(e)).Mix(e.Id); - switch (e) - { - case PumpjackCenter p: h.Mix((int)p.Direction); break; - case PumpjackSide s: h.Mix(s.Center.Id); break; - case BeaconSide s: h.Mix(s.Center.Id); break; - case ElectricPoleSide s: h.Mix(s.Center.Id); break; - case UndergroundPipe u: h.Mix((int)u.Direction); break; - case ElectricPoleCenter pole: - h.Mix(pole.Neighbors.Count); - foreach (var n in pole.Neighbors) - { - h.Mix(n); - } - - break; - } - } - - h.Mix(grid.GetId()); - return h.Hex; - } - - private static string HashCandidates(ILocationDictionary dictionary) where TInfo : CandidateInfo - { - var h = new Fnv().Mix(dictionary.Count); - foreach (var (location, info) in dictionary.EnumeratePairs()) - { - h.Mix(location).Mix(info.Covered.Count).Mix(info.Covered.TrueCount); - for (var i = 0; i < info.Covered.Count; i++) - { - h.Mix(info.Covered[i] ? 1 : 0); - } - - h.Mix(info.EntityDistance); - } - - return h.Hex; - } - - private static string HashKeys(ILocationDictionary dictionary) - { - var h = new Fnv().Mix(dictionary.Count); - foreach (var location in dictionary.Keys) - { - h.Mix(location); - } - - return h.Hex; - } - - private static string HashCoveredToCandidates(Dictionary> coveredToCandidates) - { - var h = new Fnv().Mix(coveredToCandidates.Count); - for (var i = 0; i < coveredToCandidates.Count; i++) - { - var d = coveredToCandidates[i]; - h.Mix(i).Mix(d.Count); - foreach (var location in d.Keys) - { - h.Mix(location); - } - } - - return h.Hex; - } - - private static string HashBatches(SortedBatches batches) - { - var h = new Fnv().Mix(batches.Queue.Count).Mix(batches.Lookup.Count); - foreach (var (key, d) in batches.Lookup) - { - h.Mix(key).Mix(d.Count); - foreach (var location in d.Keys) - { - h.Mix(location); - } - } - - return h.Hex; - } - - private static string HashTerminals(ILocationDictionary> dictionary) - { - var h = new Fnv().Mix(dictionary.Count); - foreach (var (key, list) in dictionary.EnumeratePairs()) - { - h.Mix(key).Mix(list.Count); - foreach (var t in list) - { - h.Mix(t.Center).Mix(t.Terminal).Mix((int)t.Direction); - } - } - - return h.Hex; - } - - // --------------------------------------------------------------------------------------- - // Mutations: changes applied to a rebuilt context before an operation, recorded under - // "mut" so the Rust test applies the same ones. - - private sealed class Mutation - { - public List RemoveCells = new(); - public List AddPoles = new(); - public List RemoveHeatPipes = new(); - public bool? OverlapBeacons; - - public bool IsEmpty => RemoveCells.Count == 0 && AddPoles.Count == 0 && RemoveHeatPipes.Count == 0 && OverlapBeacons is null; - - public void Apply(Context context) - { - if (OverlapBeacons is not null) - { - context.Options.OverlapBeacons = OverlapBeacons.Value; - } - - foreach (var l in RemoveCells) - { - context.Grid.RemoveEntity(l); - } - - foreach (var l in AddPoles) - { - AddProviderToGrid( - context.Grid, - l, - new ElectricPoleCenter(context.Grid.GetId()), - c => new ElectricPoleSide(context.Grid.GetId(), c), - context.Options.ElectricPoleWidth, - context.Options.ElectricPoleHeight); - } - - foreach (var l in RemoveHeatPipes) - { - context.HeatPipes!.Remove(l); - } - } - - public void Write(Utf8JsonWriter w) - { - if (IsEmpty) - { - return; - } - - w.WriteStartObject("mut"); - WriteLocations(w, "remove_cells", RemoveCells); - WriteLocations(w, "add_poles", AddPoles); - WriteLocations(w, "remove_heat_pipes", RemoveHeatPipes); - if (OverlapBeacons is not null) - { - w.WriteBoolean("overlap_beacons", OverlapBeacons.Value); - } - - w.WriteEndObject(); - } - } - - private static readonly Mutation None = new(); - - private static void WriteResult(Utf8JsonWriter w, Action action) - { - try - { - action(); - w.WriteBoolean("ok", true); - } - catch (FactorioToolsException ex) - { - w.WriteString("error", ex.Message); - } - } - - private static List CellsOf(SquareGrid grid, Func match) - { - var cells = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - if (match(grid[location]!)) - { - cells.Add(location); - } - } - - return cells; - } - - /// The first spot, scanning from the bottom right, where a pole fits fully inside - /// the grid. Used to add a pole that is likely to cover nothing. - private static Location? SpareSpot(Context context, int width, int height) - { - for (var y = context.Grid.Height - 2; y >= 1; y--) - { - for (var x = context.Grid.Width - 2; x >= 1; x--) - { - var l = new Location(x, y); - if (IsProviderInBounds(context.Grid, width, height, l) && DoesProviderFit(context.Grid, width, height, l)) - { - return l; - } - } - } - - return null; - } - - // --------------------------------------------------------------------------------------- - // Cases on a finished plan - - private static void RecordFinal(Stream stream, int id, Snapshot s, bool full) - { - var context = Rebuild(s); - var grid = context.Grid; - var poles = CellsOf(grid, e => e is ElectricPoleCenter); - var beacons = CellsOf(grid, e => e is BeaconCenter); - var pipes = CellsOf(grid, e => e is Pipe); - var undergrounds = CellsOf(grid, e => e is UndergroundPipe); - var heatPipes = CellsOf(grid, e => e is HeatPipe); - - Case(stream, "powered", id, _ => { }, w => - { - var (powered, hasBeacons) = GetPoweredEntities(Rebuild(s)); - WriteRecipients(w, powered); - w.WriteBoolean("has_beacons", hasBeacons); - }); - - RecordCoverage(stream, id, s, None); - if (poles.Count > 0) - { - // Without one pole, coverage may fail. - var m = new Mutation(); - m.RemoveCells.AddRange(ProviderCells(poles[poles.Count / 2], s.PoleWidth, s.PoleHeight)); - RecordCoverage(stream, id, s, m); - } - - RecordBeaconCoverage(stream, id, s, None); - - // Existing poles as providers: kept when used, and a spare pole that is likely unused. - RecordCandidates(stream, id, s, None, "pole", "pole", removeUnused: true, full, simulate: false); - var spare = SpareSpot(context, s.PoleWidth, s.PoleHeight); - if (spare is not null) - { - var m = new Mutation(); - m.AddPoles.Add(spare.Value); - RecordCandidates(stream, id, s, m, "pole", "pole", removeUnused: true, full, simulate: false); - } - - if (s.AddBeacons) - { - RecordCandidates(stream, id, s, None, "beacon", "plain", removeUnused: true, full, simulate: false); - } - - // Grid changes. - if (beacons.Count > 0) - { - RecordMutateGrid(stream, id, s, "remove_entity", beacons[0], s.BeaconWidth, s.BeaconHeight, null); - } - - if (poles.Count > 0) - { - RecordMutateGrid(stream, id, s, "remove_entity", poles[0], s.PoleWidth, s.PoleHeight, null); - } - - RecordMutateGrid(stream, id, s, "remove_pumpjack", s.Centers[0], 0, 0, null); - if (spare is not null) - { - RecordMutateGrid(stream, id, s, "add_pole", spare.Value, 0, 0, null); - RecordMutateGrid(stream, id, s, "add_beacons", spare.Value, 0, 0, new List { spare.Value }); - } - - // A beacon on a pipe fails part way, after placing what fit before it. Its cells must - // stay on the grid, or the C# throws IndexOutOfRangeException instead. - var blocked = pipes.FirstOrDefault(p => p.X - 1 - ((s.BeaconWidth - 1) / 2) >= 0 - && p.X - 1 + (s.BeaconWidth / 2) < s.Width - && p.Y - ((s.BeaconHeight - 1) / 2) >= 0 - && p.Y + (s.BeaconHeight / 2) < s.Height); - if (pipes.Count > 0 && blocked != default) - { - RecordMutateGrid(stream, id, s, "add_beacons", blocked, 0, 0, new List { new Location(blocked.X - 1, blocked.Y) }); - } - - // Every cell of the grid, so only on every other finished plan to keep the file small. - if (id % 2 == 0) - { - RecordFit(stream, id, s); - } - - // Validate. - RecordValidate(stream, id, s, None, "pipes_connected", w => w.WriteNumber("drop", -1)); - if (pipes.Count > 2) - { - RecordValidate(stream, id, s, None, "pipes_connected", w => w.WriteNumber("drop", pipes.Count / 2)); - } - - RecordValidate(stream, id, s, None, "underground_are_pipes", w => w.WriteNumber("drop", -1)); - if (undergrounds.Count > 0) - { - RecordValidate(stream, id, s, None, "underground_are_pipes", w => w.WriteNumber("drop", 0)); - } - - foreach (var variant in new[] { "same", "minus_one", "bit_same" }) - { - RecordValidate(stream, id, s, None, "pipes_do_not_match", w => w.WriteString("variant", variant)); - } - - if (s.AddBeacons) - { - RecordValidate(stream, id, s, None, "beacons_do_not_overlap", _ => { }); - var m = new Mutation { OverlapBeacons = false }; - RecordValidate(stream, id, s, m, "beacons_do_not_overlap", _ => { }); - } - - RecordValidate(stream, id, s, None, "no_existing_beacons", w => w.WriteString("variant", "empty")); - RecordValidate(stream, id, s, None, "no_existing_beacons", w => w.WriteString("variant", "grid")); - - RecordValidate(stream, id, s, None, "unheated", _ => { }); - RecordValidate(stream, id, s, None, "no_unheated_beacons", _ => { }); - RecordValidate(stream, id, s, None, "heat_connected", _ => { }); - if (heatPipes.Count > 0) - { - var m = new Mutation(); - m.RemoveCells.Add(heatPipes[0]); - RecordValidate(stream, id, s, m, "unheated", _ => { }); - - var all = new Mutation(); - all.RemoveCells.AddRange(heatPipes); - RecordValidate(stream, id, s, all, "no_unheated_beacons", _ => { }); - - var gap = new Mutation(); - gap.RemoveHeatPipes.Add(s.HeatPipes![s.HeatPipes.Count / 2]); - RecordValidate(stream, id, s, gap, "heat_connected", _ => { }); - } - - RecordValidate(stream, id, s, None, "all_power", _ => { }); - if (poles.Count > 0) - { - var m = new Mutation(); - m.RemoveCells.AddRange(ProviderCells(poles[0], s.PoleWidth, s.PoleHeight)); - RecordValidate(stream, id, s, m, "all_power", _ => { }); - } - - RecordGetPath(stream, id, s); - RecordLinesOnEmpty(stream, id, s, pipes); - - // Pole centers as AddElectricPoles gives them to PointsToLines. - if (poles.Count > 0) - { - RecordLines(stream, poles, sort: true); - } - - RecordTurns(stream, pipes); - } - - private static IEnumerable ProviderCells(Location center, int width, int height) - { - for (var x = center.X - ((width - 1) / 2); x <= center.X + (width / 2); x++) - { - for (var y = center.Y - ((height - 1) / 2); y <= center.Y + (height / 2); y++) - { - yield return new Location(x, y); - } - } - } - - private static void WriteRecipients(Utf8JsonWriter w, List recipients) - { - w.WriteStartArray("recipients"); - foreach (var r in recipients) - { - w.WriteNumberValue(r.Center.X); - w.WriteNumberValue(r.Center.Y); - w.WriteNumberValue(r.Width); - w.WriteNumberValue(r.Height); - } - - w.WriteEndArray(); - } - - private static void RecordCoverage(Stream stream, int id, Snapshot s, Mutation m) - { - Case(stream, "pole_coverage", id, m.Write, w => - { - var context = Rebuild(s); - m.Apply(context); - var (powered, _) = GetPoweredEntities(context); - var poles = CellsOf(context.Grid, e => e is ElectricPoleCenter); - try - { - var (p2c, c2p) = GetElectricPoleCoverage(context, powered, poles); - WriteSetDictionary(w, "p2c", p2c); - WriteSetDictionary(w, "c2p", c2p); - } - catch (FactorioToolsException ex) - { - w.WriteString("error", ex.Message); - } - }); - } - - private static void RecordBeaconCoverage(Stream stream, int id, Snapshot s, Mutation m) - { - Case(stream, "beacon_coverage", id, m.Write, w => - { - var context = Rebuild(s); - m.Apply(context); - var beacons = CellsOf(context.Grid, e => e is BeaconCenter); - var p2c = GetProviderCenterToCoveredCenters( - context, - context.Options.BeaconWidth, - context.Options.BeaconHeight, - context.Options.BeaconSupplyWidth, - context.Options.BeaconSupplyHeight, - beacons, - includePumpjacks: true, - includeBeacons: false); - WriteSetDictionary(w, "p2c", p2c); - WriteSetDictionary(w, "c2p", GetCoveredCenterToProviderCenters(context, p2c)); - }); - } - - private sealed class PlainFactory : ICandidateFactory - { - public CandidateInfo Create(CountedBitArray covered) => new(covered); - } - - private sealed class PoleFactory : ICandidateFactory - { - public ElectricPoleCandidateInfo Create(CountedBitArray covered) => new(covered); - } - - private static List Recipients(Context context, string provider) - { - // The pole pass powers pumpjacks and beacons (GetPoweredEntities); the beacon pass - // affects pumpjacks, in Centers order (PlanBeaconsSnug). - return provider == "pole" - ? GetPoweredEntities(context).PoweredEntities - : context.Centers.Select(c => new ProviderRecipient(c, PumpjackWidth, PumpjackHeight)).ToList(); - } - - private static void RecordCandidates(Stream stream, int id, Snapshot s, Mutation m, string provider, string info, bool removeUnused, bool full, bool simulate) - { - Case( - stream, - "candidates", - id, - w => - { - m.Write(w); - w.WriteString("provider", provider); - w.WriteString("info", info); - w.WriteBoolean("remove_unused", removeUnused); - w.WriteBoolean("simulate", simulate); - }, - w => - { - if (info == "pole") - { - CandidatesOut(w, s, m, provider, new PoleFactory(), removeUnused, full, simulate); - } - else - { - CandidatesOut(w, s, m, provider, new PlainFactory(), removeUnused, full, simulate); - } - }); - } - - private static (Context Context, List Recipients, ILocationDictionary CandidateToInfo, CountedBitArray Covered, object Providers) RunCandidates( - Snapshot s, Mutation m, string provider, ICandidateFactory factory, bool removeUnused) - where TInfo : CandidateInfo - { - var context = Rebuild(s); - m.Apply(context); - var recipients = Recipients(context, provider); - if (provider == "pole") - { - var (c, covered, providers) = GetElectricPoleCandidateToCovered(context, recipients, factory, removeUnused); - return (context, recipients, c, covered, providers); - } - else - { - var (c, covered, providers) = GetBeaconCandidateToCovered(context, recipients, factory, removeUnused); - return (context, recipients, c, covered, providers); - } - } - - private static void CandidatesOut(Utf8JsonWriter w, Snapshot s, Mutation m, string provider, ICandidateFactory factory, bool removeUnused, bool full, bool simulate) - where TInfo : CandidateInfo - { - var (context, recipients, candidateToInfo, covered, providersObject) = RunCandidates(s, m, provider, factory, removeUnused); - - WriteRecipients(w, recipients); - w.WriteNumber("count", candidateToInfo.Count); - w.WriteString("hash", HashCandidates(candidateToInfo)); - w.WriteString("covered", Bits(covered)); - w.WriteStartArray("providers"); - foreach (var (location, entity) in ProviderPairs(providersObject)) - { - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - w.WriteNumberValue(entity.Id); - } - - w.WriteEndArray(); - w.WriteString("grid", HashGrid(context.Grid)); - - if (full) - { - w.WriteStartArray("list"); - foreach (var (location, i) in candidateToInfo.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - w.WriteStringValue(Bits(i.Covered)); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } - - // GetEntityDistance for every candidate, then GetCoveredToCandidates. - var distances = new Fnv(); - foreach (var (location, i) in candidateToInfo.EnumeratePairs()) - { - distances.Mix(GetEntityDistance(recipients, location, i.Covered)); - } - - w.WriteString("entity_distance", distances.Hex); - w.WriteString("covered_to_candidates", HashCoveredToCandidates(GetCoveredToCandidates(context, candidateToInfo, covered))); - - if (!simulate) - { - return; - } - - if (provider == "pole" && candidateToInfo is ILocationDictionary poleCandidates) - { - // CandidateCoversMoreEntities on the first candidate: nothing covered yet (passes), - // then everything it covers already covered (throws). - var (first, firstInfo) = poleCandidates.EnumeratePairs().FirstOrDefault(); - if (firstInfo is not null) - { - w.WriteStartArray("covers_more"); - foreach (var coveredSoFar in new[] { new CountedBitArray(recipients.Count), new CountedBitArray(firstInfo.Covered) }) - { - try - { - Validate.CandidateCoversMoreEntities(context, recipients, coveredSoFar, first, firstInfo); - w.WriteStringValue("ok"); - } - catch (FactorioToolsException ex) - { - w.WriteStringValue(ex.Message); - } - } - - w.WriteEndArray(); - } - - SimulateAllow(w, "allow", s, m, separate: false); - SimulateAllow(w, "allow_separate", s, m, separate: true); - } - else if (provider == "beacon") - { - SimulatePrevent(w, s, m); - SimulateOverlap(w, s, m); - } - } - - private static IEnumerable<(Location, GridEntity)> ProviderPairs(object providers) - { - return providers switch - { - ILocationDictionary p => p.EnumeratePairs().Select(x => (x.Key, (GridEntity)x.Value)), - ILocationDictionary p => p.EnumeratePairs().Select(x => (x.Key, (GridEntity)x.Value)), - _ => throw new NotSupportedException(), - }; - } - - /// - /// The pole pass's loop (AddElectricPoles.AddElectricPolesAroundEntities) without its - /// priority subsets and candidate ranking: candidates batched by covered count, the first - /// candidate of the top batch placed each time with AddProviderAndAllowMultipleProviders. - /// With , the scoped dictionary is a copy of every candidate - /// instead of a batch, as the priority subsets are in the real pass. - /// - private static void SimulateAllow(Utf8JsonWriter w, string name, Snapshot s, Mutation m, bool separate) - { - var (context, recipients, all, covered, _) = RunCandidates(s, m, "pole", new PoleFactory(), removeUnused: true); - foreach (var (location, info) in all.EnumeratePairs()) - { - info.EntityDistance = GetEntityDistance(recipients, location, info.Covered); - } - - var coveredToCandidates = GetCoveredToCandidates(context, all, covered); - var coveredToLocationToInfo = new Dictionary>(); - foreach (var (location, info) in all.EnumeratePairs()) - { - if (!coveredToLocationToInfo.TryGetValue(info.Covered.TrueCount, out var locationToInfo)) - { - locationToInfo = context.GetLocationDictionary(); - coveredToLocationToInfo.Add(info.Covered.TrueCount, locationToInfo); - } - - locationToInfo.Add(location, info); - } - - var batches = new SortedBatches(coveredToLocationToInfo, ascending: false); - - ILocationDictionary? scoped = null; - if (separate) - { - scoped = context.GetLocationDictionary(); - foreach (var (location, info) in all.EnumeratePairs()) - { - scoped.Add(location, info); - } - } - - w.WriteStartArray(name); - var steps = 0; - while (covered.Any(false) && steps < MaxSteps) - { - if (scoped is null) - { - if (separate || batches.Queue.Count == 0) - { - break; - } - - scoped = batches.Queue.Peek(); - if (scoped.Count == 0) - { - scoped = null; - batches.Queue.Dequeue(); - continue; - } - } - - var (candidate, info) = scoped.EnumeratePairs().First(); - if (!all.ContainsKey(candidate)) - { - scoped.Remove(candidate); - if (scoped.Count == 0) - { - scoped = null; - } - - continue; - } - - string? error = null; - try - { - AddProviderAndAllowMultipleProviders( - context, - candidate, - info, - context.Options.ElectricPoleWidth, - context.Options.ElectricPoleHeight, - recipients, - covered, - coveredToCandidates, - all, - scoped, - batches); - } - catch (FactorioToolsException ex) - { - error = ex.Message; - } - - steps++; - w.WriteStartArray(); - w.WriteNumberValue(candidate.X); - w.WriteNumberValue(candidate.Y); - w.WriteStringValue(error ?? Bits(covered)); - w.WriteStringValue(HashCandidates(all)); - w.WriteStringValue(HashCoveredToCandidates(coveredToCandidates)); - w.WriteStringValue(HashBatches(batches)); - w.WriteNumberValue(scoped.Count); - w.WriteEndArray(); - - if (error is not null) - { - break; - } - - if (scoped.Count == 0) - { - scoped = null; - } - } - - w.WriteEndArray(); - } - - /// - /// The beacon pass without overlap (PlanBeaconsSnug with OverlapBeacons off), simplified: - /// the first candidate that still covers something new is placed each time with - /// AddProviderAndPreventMultipleProviders. Then one candidate that covers nothing new, - /// which throws. - /// - private static void SimulatePrevent(Utf8JsonWriter w, Snapshot s, Mutation m) - { - var (context, recipients, candidateToInfo, covered, _) = RunCandidates(s, m, "beacon", new PlainFactory(), removeUnused: false); - var coveredToCandidates = GetCoveredToCandidates(context, candidateToInfo, covered); - - w.WriteStartArray("prevent"); - var steps = 0; - while (steps < MaxSteps) - { - Location? pick = null; - CandidateInfo? pickInfo = null; - foreach (var (location, info) in candidateToInfo.EnumeratePairs()) - { - var newly = new CountedBitArray(covered); - newly.Not(); - newly.And(info.Covered); - if (newly.TrueCount > 0) - { - pick = location; - pickInfo = info; - break; - } - } - - if (pick is null) - { - break; - } - - AddProviderAndPreventMultipleProviders( - context, - pick.Value, - pickInfo!, - context.Options.BeaconWidth, - context.Options.BeaconHeight, - recipients, - covered, - coveredToCandidates, - candidateToInfo); - steps++; - - w.WriteStartArray(); - w.WriteNumberValue(pick.Value.X); - w.WriteNumberValue(pick.Value.Y); - w.WriteStringValue(Bits(covered)); - w.WriteStringValue(HashKeys(candidateToInfo)); - w.WriteStringValue(HashCoveredToCandidates(coveredToCandidates)); - w.WriteEndArray(); - } - - w.WriteEndArray(); - - var (rest, restInfo) = candidateToInfo.EnumeratePairs().FirstOrDefault(); - if (restInfo is not null) - { - try - { - AddProviderAndPreventMultipleProviders( - context, - rest, - restInfo, - context.Options.BeaconWidth, - context.Options.BeaconHeight, - recipients, - covered, - coveredToCandidates, - candidateToInfo); - w.WriteString("prevent_last", "ok"); - } - catch (FactorioToolsException ex) - { - w.WriteString("prevent_last", ex.Message); - } - } - } - - /// - /// The three RemoveOverlappingCandidates overloads, each on its own fresh copy of the beacon - /// candidates, at the first few candidate spots in turn. - /// - private static void SimulateOverlap(Utf8JsonWriter w, Snapshot s, Mutation m) - { - var (context, _, candidateToInfo, covered, _) = RunCandidates(s, m, "beacon", new PlainFactory(), removeUnused: false); - var centers = candidateToInfo.Keys.Take(4).ToList(); - var width = context.Options.BeaconWidth; - var height = context.Options.BeaconHeight; - - ILocationDictionary Copy() - { - var copy = context.GetLocationDictionary(); - foreach (var (location, info) in candidateToInfo.EnumeratePairs()) - { - copy.Add(location, info); - } - - return copy; - } - - w.WriteStartArray("overlap"); - - var one = Copy(); - var two = Copy(); - var twoCovered = GetCoveredToCandidates(context, two, covered); - var three = Copy(); - var threeScoped = Copy(); - var four = Copy(); - var fourScoped = Copy(); - var fourCovered = GetCoveredToCandidates(context, four, covered); - foreach (var center in centers) - { - RemoveOverlappingCandidates(context.Grid, center, width, height, one); - RemoveOverlappingCandidates(context.Grid, center, width, height, two, twoCovered); - RemoveOverlappingCandidates(context.Grid, center, width, height, three, threeScoped, null!); - RemoveOverlappingCandidates(context.Grid, center, width, height, four, fourScoped, fourCovered); - - w.WriteStartArray(); - w.WriteNumberValue(center.X); - w.WriteNumberValue(center.Y); - w.WriteStringValue(HashKeys(one)); - w.WriteStringValue(HashKeys(two)); - w.WriteStringValue(HashCoveredToCandidates(twoCovered)); - w.WriteStringValue(HashKeys(three)); - w.WriteStringValue(HashKeys(threeScoped)); - w.WriteStringValue(HashKeys(four)); - w.WriteStringValue(HashKeys(fourScoped)); - w.WriteStringValue(HashCoveredToCandidates(fourCovered)); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } - - private static void RecordMutateGrid(Stream stream, int id, Snapshot s, string action, Location center, int width, int height, List? centers) - { - Case( - stream, - "mutate_grid", - id, - w => - { - w.WriteString("action", action); - WritePoint(w, "center", center); - w.WriteStartArray("size"); - w.WriteNumberValue(width); - w.WriteNumberValue(height); - w.WriteEndArray(); - if (centers is not null) - { - WriteLocations(w, "centers", centers); - } - }, - w => - { - var context = Rebuild(s); - var grid = context.Grid; - try - { - switch (action) - { - case "remove_entity": - RemoveEntity(grid, center, width, height); - break; - case "remove_pumpjack": - RemovePumpjack(grid, center); - break; - case "add_pole": - AddProviderToGrid( - grid, - center, - new ElectricPoleCenter(grid.GetId()), - c => new ElectricPoleSide(grid.GetId(), c), - context.Options.ElectricPoleWidth, - context.Options.ElectricPoleHeight); - break; - case "add_beacons": - AddBeaconsToGrid(grid, context.Options, centers!); - break; - } - } - catch (FactorioToolsException ex) - { - w.WriteString("error", ex.Message); - } - - w.WriteNumber("count", grid.EntityLocations.Count); - w.WriteString("grid", HashGrid(grid)); - }); - } - - private static void RecordFit(Stream stream, int id, Snapshot s) - { - foreach (var (width, height) in new[] { (s.PoleWidth, s.PoleHeight), (s.BeaconWidth, s.BeaconHeight) }) - { - Case( - stream, - "fit", - id, - w => - { - w.WriteStartArray("size"); - w.WriteNumberValue(width); - w.WriteNumberValue(height); - w.WriteEndArray(); - }, - w => - { - var context = Rebuild(s); - var grid = context.Grid; - var fit = new System.Text.StringBuilder(); - var bounds = new System.Text.StringBuilder(); - for (var y = 0; y < grid.Height; y++) - { - for (var x = 0; x < grid.Width; x++) - { - var l = new Location(x, y); - var inside = x - ((width - 1) / 2) >= 0 && y - ((height - 1) / 2) >= 0 && x + (width / 2) < grid.Width && y + (height / 2) < grid.Height; - fit.Append(!inside ? '-' : DoesProviderFit(grid, width, height, l) ? '1' : '0'); - bounds.Append(IsProviderInBounds(grid, width, height, l) ? '1' : '0'); - } - } - - w.WriteString("fit", fit.ToString()); - w.WriteString("bounds", bounds.ToString()); - }); - } - } - - private static void RecordValidate(Stream stream, int id, Snapshot s, Mutation m, string check, Action inputs) - { - // The inputs are read back from the JSON the Rust test sees, so the same values drive - // both sides. - var inputBuffer = new MemoryStream(); - using (var iw = new Utf8JsonWriter(inputBuffer)) - { - iw.WriteStartObject(); - inputs(iw); - iw.WriteEndObject(); - } - - var input = JsonDocument.Parse(inputBuffer.ToArray()).RootElement; - - Case( - stream, - "validate", - id, - w => - { - m.Write(w); - w.WriteString("check", check); - inputs(w); - }, - w => - { - var context = Rebuild(s); - m.Apply(context); - var grid = context.Grid; - var pipes = CellsOf(grid, e => e is Pipe); - switch (check) - { - case "pipes_connected": - { - var drop = input.GetProperty("drop").GetInt32(); - var set = context.GetLocationSet(allowEnumerate: true); - for (var i = 0; i < pipes.Count; i++) - { - if (i != drop) - { - set.Add(pipes[i]); - } - } - - WriteResult(w, () => Validate.PipesAreConnected(context, set)); - break; - } - case "underground_are_pipes": - { - var drop = input.GetProperty("drop").GetInt32(); - var undergrounds = CellsOf(grid, e => e is UndergroundPipe); - var directions = context.GetLocationDictionary(); - foreach (var u in undergrounds) - { - directions.Add(u, ((UndergroundPipe)grid[u]!).Direction); - } - - var set = context.GetLocationSet(allowEnumerate: true); - foreach (var p in pipes) - { - if (drop < 0 || p != undergrounds[drop]) - { - set.Add(p); - } - } - - WriteResult(w, () => Validate.UndergroundPipesArePipes(context, set, directions)); - break; - } - case "pipes_do_not_match": - { - var variant = input.GetProperty("variant").GetString(); - var bits = variant == "bit_same"; - var one = context.GetLocationSet(allowEnumerate: !bits); - var two = context.GetLocationSet(allowEnumerate: !bits); - foreach (var p in pipes) - { - one.Add(p); - two.Add(p); - } - - if (variant == "minus_one" && pipes.Count > 0) - { - two.Remove(pipes[0]); - } - - WriteResult(w, () => Validate.PipesDoNotMatch(context, one, two)); - break; - } - case "beacons_do_not_overlap": - { - var beacons = CellsOf(grid, e => e is BeaconCenter); - var solutions = new List { new(BeaconStrategy.Snug, beacons, 0, new List()) }; - WriteResult(w, () => Validate.BeaconsDoNotOverlap(context, solutions)); - break; - } - case "no_existing_beacons": - { - var existing = context.GetLocationDictionary(); - if (input.GetProperty("variant").GetString() == "grid") - { - foreach (var b in CellsOf(grid, e => e is BeaconCenter)) - { - existing.Add(b, (BeaconCenter)grid[b]!); - } - } - - WriteResult(w, () => Validate.NoExistingBeacons(context, existing)); - break; - } - case "unheated": - { - Validate.CountUnheatedTargets(context, out var pumpjacks, out var unheatedPipes); - w.WriteStartArray("counts"); - w.WriteNumberValue(pumpjacks); - w.WriteNumberValue(unheatedPipes); - w.WriteEndArray(); - break; - } - case "no_unheated_beacons": - WriteResult(w, () => Validate.NoUnheatedBeacons(context)); - break; - case "heat_connected": - WriteResult(w, () => Validate.HeatPipesAreConnected(context)); - break; - case "all_power": - WriteResult(w, () => Validate.AllEntitiesHavePower(context)); - break; - default: - throw new NotSupportedException(check); - } - }); - } - - /// A breadth-first tree over empty cells, as A* and Dijkstra record it, and the - /// path GetPath reads back from its last cell. - private static void RecordGetPath(Stream stream, int id, Snapshot s) - { - var context = Rebuild(s); - var grid = context.Grid; - Location? start = null; - for (var y = 0; y < grid.Height && start is null; y++) - { - for (var x = 0; x < grid.Width && start is null; x++) - { - if (grid.IsEmpty(new Location(x, y))) - { - start = new Location(x, y); - } - } - } - - if (start is null) - { - return; - } - - var cameFrom = context.GetLocationDictionary(); - cameFrom.Add(start.Value, start.Value); - var queue = new Queue(); - queue.Enqueue(start.Value); - var last = start.Value; - Span adjacent = new Location[4]; - while (queue.Count > 0 && cameFrom.Count < 150) - { - var current = queue.Dequeue(); - last = current; - grid.GetAdjacent(adjacent, current); - foreach (var next in adjacent) - { - if (next.IsValid && grid.IsEmpty(next) && !cameFrom.ContainsKey(next)) - { - cameFrom.Add(next, current); - queue.Enqueue(next); - } - } - } - - Case( - stream, - "path", - null, - w => - { - w.WriteStartArray("came_from"); - foreach (var (location, from) in cameFrom.EnumeratePairs()) - { - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - w.WriteNumberValue(from.X); - w.WriteNumberValue(from.Y); - } - - w.WriteEndArray(); - WritePoint(w, "start", start.Value); - WritePoint(w, "goal", last); - }, - w => WriteLocations(w, "path", GetPath(cameFrom, start.Value, last))); - } - - private static void RecordLinesOnEmpty(Stream stream, int id, Snapshot s, List pipes) - { - var context = Rebuild(s); - var grid = context.Grid; - var pairs = new List<(Location, Location)>(); - - // Straight runs between terminal spots, which may cross entities, and runs from each - // pipe to the grid edge. - var terminals = s.LocationToTerminals.Select(p => p.Key).ToList(); - for (var i = 0; i < terminals.Count && pairs.Count < 12; i++) - { - for (var j = i + 1; j < terminals.Count && pairs.Count < 12; j++) - { - if (terminals[i].X == terminals[j].X || terminals[i].Y == terminals[j].Y) - { - pairs.Add((terminals[i], terminals[j])); - } - } - } - - foreach (var p in pipes.Take(4)) - { - pairs.Add((new Location(p.X, 0), new Location(p.X, grid.Height - 1))); - pairs.Add((new Location(0, p.Y), p)); - } - - if (terminals.Count >= 2) - { - pairs.Add((new Location(0, 0), new Location(1, 1))); - } - - foreach (var (a, b) in pairs) - { - Case( - stream, - "line_on_empty", - id, - w => - { - WritePoint(w, "a", a); - WritePoint(w, "b", b); - }, - w => - { - try - { - var line = MakeStraightLineOnEmpty(Rebuild(s).Grid, a, b); - if (line is null) - { - w.WriteBoolean("none", true); - } - else - { - WriteLocations(w, "points", line); - } - } - catch (ArgumentException ex) - { - w.WriteString("error", $"ArgumentException: {ex.Message}"); - } - }); - } - } - - private static void RecordLines(Stream stream, IReadOnlyList points, bool sort) - { - Case( - stream, - "lines", - null, - w => - { - WriteLocations(w, "points", points); - w.WriteBoolean("sort", sort); - }, - w => - { - try - { - var lines = PointsToLines(points, sort); - w.WriteStartArray("lines"); - foreach (var line in lines) - { - w.WriteNumberValue(line.A.X); - w.WriteNumberValue(line.A.Y); - w.WriteNumberValue(line.B.X); - w.WriteNumberValue(line.B.Y); - } - - w.WriteEndArray(); - } - catch (Exception ex) when (ex is not FactorioToolsException) - { - w.WriteString("error_type", ex.GetType().Name); - w.WriteString("error", ex.Message); - } - }); - } - - private static void RecordTurns(Stream stream, IReadOnlyList points) - { - Case(stream, "turns", null, w => WriteLocations(w, "points", points), w => w.WriteNumber("result", CountTurns(points.ToList()))); - Case(stream, "collinear", null, w => WriteLocations(w, "points", points), w => w.WriteBoolean("result", AreLocationsCollinear(points))); - } - - // --------------------------------------------------------------------------------------- - // Cases on the states before poles and before beacons - - private static void RecordNoPoles(Stream stream, int id, Snapshot s, bool full) - { - RecordCandidates(stream, id, s, None, "pole", "pole", removeUnused: true, full, simulate: true); - RecordCandidates(stream, id, s, None, "pole", "plain", removeUnused: false, full, simulate: false); - } - - private static void RecordNoBeacons(Stream stream, int id, Snapshot s, bool full) - { - RecordCandidates(stream, id, s, None, "beacon", "plain", removeUnused: false, full, simulate: true); - RecordCandidates(stream, id, s, None, "pole", "pole", removeUnused: true, full, simulate: false); - RecordBeaconCoverage(stream, id, s, None); - } - - // --------------------------------------------------------------------------------------- - // Cases on a starting context - - private static void RecordInitial(Stream stream, int id, Snapshot s) - { - Case(stream, "terminals", id, _ => { }, w => - { - var context = Rebuild(s); - var c2t = GetCenterToTerminals(context, context.Grid, context.Centers); - var l2t = GetLocationToTerminals(context, c2t); - w.WriteString("c2t", HashTerminals(c2t)); - w.WriteString("l2t", HashTerminals(l2t)); - - // EliminateOtherTerminals, choosing each pumpjack's last option in turn on a fresh - // context. - w.WriteStartArray("eliminate"); - foreach (var center in s.Centers.Take(6)) - { - var fresh = Rebuild(s); - var options = fresh.CenterToTerminals[center]; - var selected = options[options.Count - 1]; - EliminateOtherTerminals(fresh, selected); - w.WriteStartArray(); - w.WriteNumberValue(center.X); - w.WriteNumberValue(center.Y); - w.WriteStringValue(HashTerminals(fresh.CenterToTerminals)); - w.WriteStringValue(HashTerminals(fresh.LocationToTerminals)); - w.WriteEndArray(); - } - - w.WriteEndArray(); - }); - - RecordLines(stream, s.Centers, sort: true); - RecordLines(stream, s.Centers, sort: false); - for (var n = 0; n <= Math.Min(4, s.Centers.Count); n++) - { - RecordLines(stream, s.Centers.Take(n).ToList(), sort: true); - } - - RecordTurns(stream, s.Centers); - RecordTurns(stream, s.Centers.Take(3).ToList()); - foreach (var t in s.LocationToTerminals.Take(6)) - { - var a = t.Key; - RecordStraightLine(stream, a, new Location(a.X, a.Y + 5)); - RecordStraightLine(stream, a, new Location(a.X - 3, a.Y)); - RecordStraightLine(stream, a, new Location(a.X + 1, a.Y + 1)); - } - } - - private static void RecordStraightLine(Stream stream, Location a, Location b) - { - Case( - stream, - "line", - null, - w => - { - WritePoint(w, "a", a); - WritePoint(w, "b", b); - }, - w => - { - try - { - WriteLocations(w, "points", MakeStraightLine(a, b)); - } - catch (ArgumentException ex) - { - w.WriteString("error", $"ArgumentException: {ex.Message}"); - } - }); - } - - // --------------------------------------------------------------------------------------- - // Synthetic geometry - - private static void RecordGeometry(Stream stream) - { - var random = new Random(4); - var sets = new List> - { - new(), - new() { new Location(5, 5) }, - new() { new Location(5, 5), new Location(5, 5) }, - new() { new Location(5, 5), new Location(5, 5), new Location(5, 5) }, - new() { new Location(0, 0), new Location(1, 1), new Location(2, 2), new Location(3, 3) }, - new() { new Location(0, 0), new Location(0, 4), new Location(0, 2) }, - new() { new Location(2, 0), new Location(0, 0), new Location(1, 0), new Location(1, 1) }, - new() { new Location(0, 0), new Location(2, 1), new Location(4, 2), new Location(6, 4) }, - }; - - for (var i = 0; i < 40; i++) - { - var count = random.Next(3, 14); - var set = new List(); - for (var j = 0; j < count; j++) - { - set.Add(new Location(random.Next(0, 12), random.Next(0, 12))); - } - - sets.Add(set); - } - - foreach (var set in sets) - { - RecordLines(stream, set, sort: true); - RecordLines(stream, set, sort: false); - RecordTurns(stream, set); - } - } -} diff --git a/rust/planner/oracle/InitializeContextRecorder.cs b/rust/planner/oracle/InitializeContextRecorder.cs deleted file mode 100644 index b168ca2e..00000000 --- a/rust/planner/oracle/InitializeContextRecorder.cs +++ /dev/null @@ -1,485 +0,0 @@ -// Records InitializeContext.Execute for the Rust port (#138), stage 3. -// Writes initialize_context.jsonl. The format is in rust/planner/FIXTURES.md. - -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class InitializeContextRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private sealed record Case( - string Name, - OilFieldOptions Options, - Blueprint Blueprint, - AvoidLocation[] Avoid, - int MinWidth, - int MinHeight); - - /// The presets and defaults, so the Rust test checks its own against them. - private static readonly (string Name, Func Options)[] Presets = - { - ("Default", () => new OilFieldOptions()), - ("ForSmallIronElectricPole", () => OilFieldOptions.ForSmallIronElectricPole), - ("ForSmallElectricPole", () => OilFieldOptions.ForSmallElectricPole), - ("ForMediumElectricPole", () => OilFieldOptions.ForMediumElectricPole), - ("ForBigElectricPole", () => OilFieldOptions.ForBigElectricPole), - ("ForSubstation", () => OilFieldOptions.ForSubstation), - }; - - public static void Record(string outputDir, string repoRoot, IReadOnlyList smallList) - { - var bigList = ParseBlueprint.ReadBlueprintFile(Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField", "big-list.txt")); - var cases = BuildCases(smallList, bigList); - - var path = Path.Combine(outputDir, "initialize_context.jsonl"); - var errors = 0; - using (var stream = File.Create(path)) - { - foreach (var (name, options) in Presets) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - writer.WriteString("kind", "preset"); - writer.WriteString("name", name); - writer.WritePropertyName("options"); - WriteOptions(writer, options()); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - foreach (var c in cases) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - writer.WriteString("kind", "case"); - writer.WriteString("name", c.Name); - writer.WritePropertyName("options"); - WriteOptions(writer, c.Options); - WriteBlueprint(writer, c.Blueprint); - writer.WriteStartArray("avoid"); - foreach (var a in c.Avoid) - { - writer.WriteStartArray(); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(a.X)); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(a.Y)); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - writer.WriteStartArray("min"); - writer.WriteNumberValue(c.MinWidth); - writer.WriteNumberValue(c.MinHeight); - writer.WriteEndArray(); - - Context? context = null; - FactorioToolsException? error = null; - try - { - context = InitializeContext.Execute(c.Options, c.Blueprint, c.Avoid, c.MinWidth, c.MinHeight); - } - catch (FactorioToolsException ex) - { - error = ex; - } - - if (error is not null) - { - errors++; - writer.WriteStartObject("error"); - writer.WriteString("message", error.Message); - writer.WriteBoolean("bad_input", error.BadInput); - writer.WriteEndObject(); - } - else - { - WriteContext(writer, c.Name, context!); - } - - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - } - - Console.WriteLine($"initialize_context.jsonl: {Presets.Length} presets, {cases.Count} cases ({errors} errors), {new FileInfo(path).Length} bytes"); - } - - private static List BuildCases(IReadOnlyList smallList, IReadOnlyList bigList) - { - var cases = new List(); - var none = Array.Empty(); - - // Every small-list blueprint with the medium pole; the other option sets on every - // third blueprint, offset so each blueprint also meets other presets. - var sets = new (string Name, Func Options, int Stride, int Offset)[] - { - ("medium", () => OilFieldOptions.ForMediumElectricPole, 1, 0), - ("small", () => OilFieldOptions.ForSmallElectricPole, 3, 0), - ("big", () => OilFieldOptions.ForBigElectricPole, 3, 1), - ("substation", () => OilFieldOptions.ForSubstation, 3, 2), - ("medium-no-beacons", () => { var o = OilFieldOptions.ForMediumElectricPole; o.AddBeacons = false; return o; }, 3, 1), - ("medium-non-standard-beacon", () => - { - var o = OilFieldOptions.ForMediumElectricPole; - o.BeaconWidth = 2; - o.BeaconHeight = 4; - o.BeaconSupplyWidth = 4; - o.BeaconSupplyHeight = 6; - return o; - }, 3, 2), - ("medium-legendary-pole", () => { var o = OilFieldOptions.ForMediumElectricPole; o.ElectricPoleQuality = Quality.Legendary; return o; }, 6, 0), - ("big-rare-everything", () => - { - var o = OilFieldOptions.ForBigElectricPole; - o.ElectricPoleQuality = Quality.Rare; - o.PumpjackQuality = Quality.Epic; - o.BeaconQuality = Quality.Uncommon; - o.PumpjackModuleQuality = Quality.Legendary; - o.BeaconModuleQuality = Quality.Rare; - return o; - }, 6, 3), - }; - - foreach (var set in sets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - cases.Add(new Case($"small-{i:D4}_{set.Name}", set.Options(), ParseBlueprint.Execute(smallList[i]), none, 0, 0)); - } - } - - // A sample of the big list with the medium pole. - for (var i = 0; i < bigList.Count; i += 20) - { - cases.Add(new Case($"big-{i:D4}_medium", OilFieldOptions.ForMediumElectricPole, ParseBlueprint.Execute(bigList[i]), none, 0, 0)); - } - - // InitializeContextTest's inputs. - var one = Centers((25.5f, -14.5f)); - var two = Centers((25.5f, -14.5f), (33.5f, -12.5f)); - var allSides = new[] - { - new AvoidLocation(29.5f, -24.5f), - new AvoidLocation(43.5f, -13.5f), - new AvoidLocation(29.5f, -2.5f), - new AvoidLocation(15.5f, -13.5f), - }; - foreach (var (w, h) in new[] { (0, 0), (0, 1), (1, 0), (1, 1), (5, 3) }) - { - cases.Add(new Case($"test-empty-{w}x{h}", NoBeacons(OilFieldOptions.ForMediumElectricPole), EmptyBlueprint(), none, w, h)); - } - - cases.Add(new Case("test-one-substation-beacons", OilFieldOptions.ForSubstation, one, none, 0, 0)); - cases.Add(new Case("test-two-medium-beacons", OilFieldOptions.ForMediumElectricPole, two, none, 0, 0)); - cases.Add(new Case("test-avoid-less-than-min-y", OilFieldOptions.ForSubstation, two, new[] { new AvoidLocation(29.5f, -21.5f) }, 0, 0)); - cases.Add(new Case("test-avoid-all-sides", OilFieldOptions.ForSubstation, two, allSides, 0, 0)); - cases.Add(new Case("test-larger-min-no-avoid", OilFieldOptions.ForSubstation, two, none, 45, 0)); - cases.Add(new Case("test-larger-min-with-avoid", OilFieldOptions.ForSubstation, two, allSides, 50, 40)); - cases.Add(new Case("test-odd-min-padding", OilFieldOptions.ForMediumElectricPole, two, none, 30, 24)); - cases.Add(new Case("avoid-only", OilFieldOptions.ForMediumElectricPole, Centers(), new[] { new AvoidLocation(3, 4), new AvoidLocation(-2, 7) }, 0, 0)); - - // PlannerTest.AllowsLocationsToBeAvoided. - var avoided = new Blueprint - { - Entities = new[] - { - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -3, Y = -5 } }, - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 4, Y = 5 } }, - }, - }; - cases.Add(new Case("planner-test-avoid", OilFieldOptions.ForMediumElectricPole, avoided, Enumerable.Range(-7, 16).Select(x => new AvoidLocation(x, 0)).ToArray(), 0, 0)); - - // Directions, a missing direction, and entities that are not pumpjacks. - cases.Add(new Case("mixed-entities-and-directions", OilFieldOptions.ForMediumElectricPole, new Blueprint - { - Entities = new[] - { - new Entity { EntityNumber = 1, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 0.5f, Y = 0.5f }, Direction = Direction.Right }, - new Entity { EntityNumber = 2, Name = EntityNames.Vanilla.Pipe, Position = new Position { X = 4.5f, Y = 0.5f } }, - new Entity { EntityNumber = 3, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 8.5f, Y = -3.5f }, Direction = Direction.Left, Mirror = true }, - new Entity { EntityNumber = 4, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 0.5f, Y = 9.5f } }, - new Entity { EntityNumber = 5, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -6.5f, Y = 4.5f }, Direction = Direction.Down }, - new Entity { EntityNumber = 6, Name = EntityNames.Vanilla.Beacon, Position = new Position { X = 20.5f, Y = 20.5f } }, - }, - }, none, 0, 0)); - - // Errors. - cases.Add(new Case("error-too-many-pumpjacks", OilFieldOptions.ForMediumElectricPole, - Centers(Enumerable.Range(0, 151).Select(i => ((float)(i % 15) * 4, (float)(i / 15) * 4)).ToArray()), none, 0, 0)); - cases.Add(new Case("error-non-integer-width", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f), (5.25f, 0.5f)), none, 0, 0)); - cases.Add(new Case("error-non-integer-height", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f), (5.5f, 4.75f)), none, 0, 0)); - // The outermost pumpjacks line up, so the grid size is whole, but a middle one does not. - cases.Add(new Case("error-non-integer-entity-x", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f), (4.25f, 8.5f), (9.5f, 0.5f)), none, 0, 0)); - cases.Add(new Case("error-non-integer-entity-y", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f), (4.5f, 4.75f), (9.5f, 9.5f)), none, 0, 0)); - cases.Add(new Case("error-non-integer-avoid-x", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f)), new[] { new AvoidLocation(2.25f, 1) }, 0, 0)); - cases.Add(new Case("error-non-integer-avoid-y", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f)), new[] { new AvoidLocation(2.5f, 1.5f), new AvoidLocation(1.5f, 2.75f) }, 0, 0)); - cases.Add(new Case("error-grid-too-wide", OilFieldOptions.ForMediumElectricPole, Centers((0, 0), (1200, 0)), none, 0, 0)); - cases.Add(new Case("error-grid-area-too-large", OilFieldOptions.ForMediumElectricPole, Centers((0, 0), (600, 600)), none, 0, 0)); - cases.Add(new Case("error-avoid-on-pumpjack", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f)), new[] { new AvoidLocation(0.5f, 0.5f) }, 0, 0)); - cases.Add(new Case("error-avoid-twice", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f)), new[] { new AvoidLocation(4.5f, 0.5f), new AvoidLocation(4.5f, 0.5f) }, 0, 0)); - cases.Add(new Case("error-overlapping-pumpjacks", OilFieldOptions.ForMediumElectricPole, Centers((0.5f, 0.5f), (2.5f, 1.5f)), none, 0, 0)); - cases.Add(new Case("error-no-room-for-terminals", OilFieldOptions.ForMediumElectricPole, - Centers((0.5f, 0.5f), (1.5f, -2.5f), (3.5f, 1.5f), (-0.5f, 3.5f), (-2.5f, -0.5f)), none, 0, 0)); - - return cases; - } - - private static OilFieldOptions NoBeacons(OilFieldOptions options) - { - options.AddBeacons = false; - return options; - } - - private static Blueprint EmptyBlueprint() - { - return new Blueprint - { - Entities = Array.Empty(), - Icons = new[] { new Icon { Index = 1, Signal = new SignalID { Name = EntityNames.Vanilla.Pumpjack, Type = SignalTypes.Vanilla.Item } } }, - Item = ItemNames.Vanilla.Blueprint, - }; - } - - private static Blueprint Centers(params (float X, float Y)[] centers) - { - return new Blueprint - { - Entities = centers - .Select((p, i) => new Entity { EntityNumber = i + 1, Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = p.X, Y = p.Y } }) - .ToArray(), - }; - } - - private static void WriteOptions(Utf8JsonWriter writer, OilFieldOptions o) - { - writer.WriteStartObject(); - writer.WriteBoolean("use_underground_pipes", o.UseUndergroundPipes); - writer.WriteBoolean("add_beacons", o.AddBeacons); - writer.WriteBoolean("optimize_pipes", o.OptimizePipes); - writer.WriteBoolean("overlap_beacons", o.OverlapBeacons); - writer.WriteBoolean("add_electric_poles", o.AddElectricPoles); - writer.WriteBoolean("add_heat_pipes", o.AddHeatPipes); - writer.WriteString("heat_pipe_entity_name", o.HeatPipeEntityName); - writer.WriteStartArray("pipe_strategies"); - foreach (var s in o.PipeStrategies) - { - writer.WriteNumberValue((int)s); - } - - writer.WriteEndArray(); - writer.WriteStartArray("beacon_strategies"); - foreach (var s in o.BeaconStrategies) - { - writer.WriteNumberValue((int)s); - } - - writer.WriteEndArray(); - writer.WriteString("electric_pole_entity_name", o.ElectricPoleEntityName); - writer.WriteNumber("electric_pole_supply_width", o.ElectricPoleSupplyWidth); - writer.WriteNumber("electric_pole_supply_height", o.ElectricPoleSupplyHeight); - writer.WriteNumber("electric_pole_wire_reach_bits", BitConverter.DoubleToInt64Bits(o.ElectricPoleWireReach)); - writer.WriteNumber("electric_pole_width", o.ElectricPoleWidth); - writer.WriteNumber("electric_pole_height", o.ElectricPoleHeight); - writer.WriteString("beacon_entity_name", o.BeaconEntityName); - writer.WriteNumber("beacon_supply_width", o.BeaconSupplyWidth); - writer.WriteNumber("beacon_supply_height", o.BeaconSupplyHeight); - writer.WriteNumber("beacon_width", o.BeaconWidth); - writer.WriteNumber("beacon_height", o.BeaconHeight); - writer.WriteBoolean("validate_solution", o.ValidateSolution); - WriteModules(writer, "pumpjack_modules", o.PumpjackModules); - WriteModules(writer, "beacon_modules", o.BeaconModules); - writer.WriteNumber("pumpjack_quality", (int)o.PumpjackQuality); - writer.WriteNumber("beacon_quality", (int)o.BeaconQuality); - writer.WriteNumber("electric_pole_quality", (int)o.ElectricPoleQuality); - writer.WriteNumber("pumpjack_module_quality", (int)o.PumpjackModuleQuality); - writer.WriteNumber("beacon_module_quality", (int)o.BeaconModuleQuality); - writer.WriteEndObject(); - } - - private static void WriteModules(Utf8JsonWriter writer, string name, Dictionary modules) - { - writer.WriteStartArray(name); - foreach (var (item, count) in modules) - { - writer.WriteStartArray(); - writer.WriteStringValue(item); - writer.WriteNumberValue(count); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - - /// The fields InitializeContext reads, plus mirror. Positions are float bits. - private static void WriteBlueprint(Utf8JsonWriter writer, Blueprint blueprint) - { - writer.WriteStartArray("entities"); - foreach (var e in blueprint.Entities) - { - writer.WriteStartArray(); - writer.WriteNumberValue(e.EntityNumber); - writer.WriteStringValue(e.Name); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(e.Position.X)); - writer.WriteNumberValue(BitConverter.SingleToInt32Bits(e.Position.Y)); - if (e.Direction.HasValue) - { - writer.WriteNumberValue((int)e.Direction.Value); - } - else - { - writer.WriteNullValue(); - } - - if (e.Mirror.HasValue) - { - writer.WriteBooleanValue(e.Mirror.Value); - } - else - { - writer.WriteNullValue(); - } - - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - - private static void WriteTerminal(Utf8JsonWriter writer, TerminalLocation t) - { - writer.WriteStartArray(); - writer.WriteNumberValue(t.Center.X); - writer.WriteNumberValue(t.Center.Y); - writer.WriteNumberValue(t.Terminal.X); - writer.WriteNumberValue(t.Terminal.Y); - writer.WriteNumberValue((int)t.Direction); - writer.WriteEndArray(); - } - - private static void WriteTerminalDictionary(Utf8JsonWriter writer, string name, ILocationDictionary> dictionary) - { - writer.WriteStartArray(name); - foreach (var pair in dictionary.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - writer.WriteStartArray(); - foreach (var t in pair.Value) - { - WriteTerminal(writer, t); - } - - writer.WriteEndArray(); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - - private static void WriteContext(Utf8JsonWriter writer, string name, Context context) - { - var grid = context.Grid; - writer.WriteStartObject("context"); - writer.WriteNumber("delta_x_bits", BitConverter.SingleToInt32Bits(context.DeltaX)); - writer.WriteNumber("delta_y_bits", BitConverter.SingleToInt32Bits(context.DeltaY)); - writer.WriteNumber("width", grid.Width); - writer.WriteNumber("height", grid.Height); - writer.WriteStartArray("middle"); - writer.WriteNumberValue(grid.Middle.X); - writer.WriteNumberValue(grid.Middle.Y); - writer.WriteEndArray(); - - writer.WriteStartArray("entities"); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var entity = grid[location] ?? throw new InvalidOperationException($"No entity at {location} in {name}."); - writer.WriteStartArray(); - writer.WriteNumberValue(location.X); - writer.WriteNumberValue(location.Y); - writer.WriteStringValue(entity.GetType().Name); - writer.WriteNumberValue(entity.Id); - switch (entity) - { - case PumpjackCenter pumpjack: - writer.WriteNumberValue((int)pumpjack.Direction); - break; - case PumpjackSide side: - writer.WriteNumberValue(side.Center.Id); - break; - case AvoidEntity: - break; - default: - throw new NotSupportedException($"InitializeContext placed an unexpected {entity.GetType().Name} in {name}."); - } - - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - // The id the next GetId() call returns. Taking it changes nothing recorded above. - writer.WriteNumber("next_id", grid.GetId()); - - writer.WriteStartArray("centers"); - foreach (var center in context.Centers) - { - writer.WriteStartArray(); - writer.WriteNumberValue(center.X); - writer.WriteNumberValue(center.Y); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - WriteTerminalDictionary(writer, "center_to_terminals", context.CenterToTerminals); - - writer.WriteStartArray("center_to_original_direction"); - foreach (var pair in context.CenterToOriginalDirection.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - writer.WriteNumberValue((int)pair.Value); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - - WriteTerminalDictionary(writer, "location_to_terminals", context.LocationToTerminals); - - // Sparse: most cells border no pumpjack side. - writer.WriteNumber("adjacent_count_length", context.LocationToAdjacentCount.Length); - writer.WriteStartArray("adjacent_counts"); - for (var i = 0; i < context.LocationToAdjacentCount.Length; i++) - { - if (context.LocationToAdjacentCount[i] != 0) - { - writer.WriteStartArray(); - writer.WriteNumberValue(i); - writer.WriteNumberValue(context.LocationToAdjacentCount[i]); - writer.WriteEndArray(); - } - } - - writer.WriteEndArray(); - - writer.WriteNumber("electric_pole_supply_width_with_quality", context.ElectricPoleSupplyWidthWithQuality); - writer.WriteNumber("electric_pole_supply_height_with_quality", context.ElectricPoleSupplyHeightWithQuality); - writer.WriteNumber("electric_pole_wire_reach_with_quality_bits", BitConverter.DoubleToInt64Bits(context.ElectricPoleWireReachWithQuality)); - writer.WriteNumber("electric_pole_wire_reach_squared_with_quality_bits", BitConverter.DoubleToInt64Bits(context.ElectricPoleWireReachSquaredWithQuality)); - writer.WriteEndObject(); - } -} diff --git a/rust/planner/oracle/PipesRecorder.cs b/rust/planner/oracle/PipesRecorder.cs deleted file mode 100644 index 99d661de..00000000 --- a/rust/planner/oracle/PipesRecorder.cs +++ /dev/null @@ -1,432 +0,0 @@ -// Records PlanUndergroundPipes.Execute and AddPipeEntities.Execute for the Rust port (#138), -// stage 5. Writes pipes.jsonl. The format is in rust/planner/FIXTURES.md. -// -// The planner calls both inside AddPipes with the pipe set of the plan it picked, which no -// public API hands out. So each case rebuilds that set from a finished plan: every Pipe, -// Terminal and UndergroundPipe cell of the final grid, plus the cells each underground pair -// skips over. The case then runs both steps on a fresh starting Context that carries the -// plan's selected terminals, the way AddPipes runs them, and records the inputs and the -// results. The recorder also checks that the rebuilt run lands the same pipes on the grid as -// the real plan did, so the inputs are known to be real ones. - -using System.Reflection; -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class PipesRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly FieldInfo NextIdField = typeof(SquareGrid).GetField("_nextId", BindingFlags.NonPublic | BindingFlags.Instance) - ?? throw new InvalidOperationException("SquareGrid._nextId not found."); - - /// How a case runs the two steps. - private enum Mode - { - /// As AddPipes does: the plan's selected terminals, onto the context's grid. - Selected, - - /// Every candidate terminal, onto a clone, with allowMultipleTerminals. - MultipleOnClone, - - /// Every candidate terminal without allowMultipleTerminals, which throws. - MultipleError, - } - - private sealed record Case(string Name, OilFieldOptions Options, string Blueprint, bool PlanUnderground, Mode Mode); - - public static void Record(string outputDir, string repoRoot, IReadOnlyList smallList) - { - var bigList = ParseBlueprint.ReadBlueprintFile(Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField", "big-list.txt")); - var cases = BuildCases(smallList, bigList); - - var path = Path.Combine(outputDir, "pipes.jsonl"); - var written = 0; - var skipped = 0; - var rebuiltMismatches = 0; - var errors = 0; - using (var stream = File.Create(path)) - { - foreach (var c in cases) - { - var plan = Planner.Execute(c.Options, ParseBlueprint.Execute(c.Blueprint)); - var context = InitializeContext.Execute(c.Options, ParseBlueprint.Execute(c.Blueprint), Array.Empty()); - if (context.Grid.Width != plan.Context.Grid.Width || context.Grid.Height != plan.Context.Grid.Height) - { - // The planner re-ran InitializeContext with other inputs (the poles-first - // retry), so the plan's cells do not line up with a fresh context. - skipped++; - continue; - } - - if (c.Mode == Mode.Selected) - { - context.CenterToTerminals = Copy(context, plan.Context.CenterToTerminals); - context.LocationToTerminals = Copy(context, plan.Context.LocationToTerminals); - } - - var pipes = RebuildPipeSet(context, plan.Context.Grid); - - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - writer.WriteString("name", c.Name); - writer.WriteBoolean("validate", c.Options.ValidateSolution); - writer.WriteNumber("width", context.Grid.Width); - writer.WriteNumber("height", context.Grid.Height); - writer.WriteNumber("next_id", (int)NextIdField.GetValue(context.Grid)!); - WriteEntities(writer, "grid", context.Grid, context.Grid.EntityLocations.EnumerateItems()); - WriteTerminalDictionary(writer, "center_to_terminals", context.CenterToTerminals); - WriteTerminalDictionary(writer, "location_to_terminals", context.LocationToTerminals); - WriteLocations(writer, "pipes", pipes.EnumerateItems()); - writer.WriteBoolean("plan_underground", c.PlanUnderground); - writer.WriteString("mode", c.Mode switch - { - Mode.Selected => "selected", - Mode.MultipleOnClone => "multiple_on_clone", - _ => "multiple_error", - }); - - ILocationDictionary? underground = null; - if (c.PlanUnderground) - { - underground = PlanUndergroundPipes.Execute(context, pipes); - writer.WriteStartArray("underground"); - foreach (var pair in underground.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - writer.WriteNumberValue((int)pair.Value); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - WriteLocations(writer, "pipes_after", pipes.EnumerateItems()); - } - - if (c.Mode == Mode.Selected) - { - WriteNoOverlapChecks(writer, context, plan.Context.Grid, pipes, underground); - } - - var grid = c.Mode == Mode.MultipleOnClone ? new PipeGrid(context.Grid) : context.Grid; - var before = grid.EntityLocations.EnumerateItems().ToList(); - try - { - AddPipeEntities.Execute(context, grid, pipes, underground, allowMultipleTerminals: c.Mode == Mode.MultipleOnClone); - - // The grid's entity set only grows here, so the starting entities keep - // their order and the new ones follow. Record just the new ones. - var after = grid.EntityLocations.EnumerateItems().ToList(); - if (!after.Take(before.Count).SequenceEqual(before)) - { - throw new InvalidOperationException($"{c.Name}: the starting entities changed order."); - } - - WriteEntities(writer, "added", grid, after.Skip(before.Count)); - writer.WriteNumber("next_id_after", (int)NextIdField.GetValue(grid)!); - - if (c.Mode == Mode.Selected && c.PlanUnderground == c.Options.UseUndergroundPipes && !SamePipes(grid, plan.Context.Grid)) - { - rebuiltMismatches++; - Console.Error.WriteLine($"pipes.jsonl: {c.Name}: the rebuilt run placed different pipes than the plan."); - } - } - catch (FactorioToolsException ex) - { - errors++; - writer.WriteString("error", ex.Message); - } - - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - written++; - } - } - - Console.WriteLine($"pipes.jsonl: {written} cases ({errors} errors, {skipped} skipped, {rebuiltMismatches} rebuilt-plan mismatches), {new FileInfo(path).Length} bytes"); - if (rebuiltMismatches > 0) - { - throw new InvalidOperationException("Some rebuilt pipe sets do not reproduce their plan; see above."); - } - } - - /// - /// Validate.NoOverlappingEntities on this case's pipes: with no beacons, with the plan's own - /// beacons (both pass), and three broken inputs that must fail - a beacon on a pipe, the same - /// beacon twice, and an extra pipe on a pumpjack center. Each result is null or the message. - /// The check clones the grid, so the context is unchanged afterwards. - /// - private static void WriteNoOverlapChecks(Utf8JsonWriter writer, Context context, SquareGrid planGrid, ILocationSet pipes, ILocationDictionary? underground) - { - var planBeacons = planGrid.EntityLocations.EnumerateItems() - .Where(l => planGrid[l] is BeaconCenter) - .ToList(); - // A pipe whose whole beacon footprint is inside the grid: AddProviderToGrid places cells - // from the top-left with no bounds check, and an edge pipe would index off the grid. - var firstPipe = pipes.EnumerateItems().First(l => - l.X - ((context.Options.BeaconWidth - 1) / 2) >= 0 - && l.Y - ((context.Options.BeaconHeight - 1) / 2) >= 0 - && l.X + (context.Options.BeaconWidth / 2) < context.Grid.Width - && l.Y + (context.Options.BeaconHeight / 2) < context.Grid.Height); - var pipesOnPumpjack = context.GetLocationSet(pipes); - pipesOnPumpjack.Add(context.Centers[0]); - - var checks = new List<(string Label, ILocationSet Pipes, List? Beacons)> - { - ("no_beacons", pipes, null), - ("plan_beacons", pipes, planBeacons), - ("beacon_on_pipe", pipes, new List { firstPipe }), - ("duplicate_beacon", pipes, planBeacons.Count > 0 ? new List { planBeacons[0], planBeacons[0] } : null), - ("pipe_on_pumpjack", pipesOnPumpjack, null), - }; - - writer.WriteStartArray("no_overlap"); - foreach (var check in checks) - { - if (check.Label == "duplicate_beacon" && check.Beacons is null) - { - continue; - } - - writer.WriteStartObject(); - writer.WriteString("label", check.Label); - WriteLocations(writer, "pipes", check.Pipes.EnumerateItems()); - if (check.Beacons is not null) - { - WriteLocations(writer, "beacons", check.Beacons); - } - - List? solutions = check.Beacons is null - ? null - : new List { new BeaconSolution(BeaconStrategy.Snug, check.Beacons, 0, new List()) }; - try - { - Validate.NoOverlappingEntities(context, check.Pipes, underground, solutions); - writer.WriteNull("error"); - } - catch (FactorioToolsException ex) - { - writer.WriteString("error", ex.Message); - } - - writer.WriteEndObject(); - } - - writer.WriteEndArray(); - } - - private static List BuildCases(IReadOnlyList smallList, IReadOnlyList bigList) - { - var cases = new List(); - - // The PlannerOnAllBlueprintsAndOptions option sets that grids.jsonl uses: the medium - // pole with undergrounds on every blueprint, the others on every third. - var sets = new (string Name, Func Options, bool Beacons, bool Overlap, bool Underground, int Stride, int Offset)[] - { - ("medium-electric-pole_b_o_u", () => OilFieldOptions.ForMediumElectricPole, true, true, true, 1, 0), - ("small-electric-pole_u", () => OilFieldOptions.ForSmallElectricPole, false, false, true, 3, 0), - ("big-electric-pole_b", () => OilFieldOptions.ForBigElectricPole, true, false, false, 3, 1), - ("substation_b_o", () => OilFieldOptions.ForSubstation, true, true, false, 3, 2), - }; - - foreach (var set in sets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - var options = set.Options(); - options.AddBeacons = set.Beacons; - options.OverlapBeacons = set.Overlap; - options.UseUndergroundPipes = set.Underground; - options.ValidateSolution = true; - cases.Add(new Case($"small-{i:D4}_{set.Name}", options, smallList[i], set.Underground, Mode.Selected)); - - // Plans made without undergrounds still give real pipe sets to plan them on. - if (!set.Underground) - { - cases.Add(new Case($"small-{i:D4}_{set.Name}_forced-u", options, smallList[i], true, Mode.Selected)); - } - } - } - - // Every candidate terminal at once, as RotateOptimize and the connected-centers - // strategy pass them, onto a clone. Plus the error AddPipes guards against. - for (var i = 0; i < smallList.Count; i += 2) - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - cases.Add(new Case($"small-{i:D4}_medium_multiple-terminals", options, smallList[i], true, Mode.MultipleOnClone)); - } - - for (var i = 1; i < smallList.Count; i += 12) - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = false; - cases.Add(new Case($"small-{i:D4}_medium_multiple-terminals-error", options, smallList[i], false, Mode.MultipleError)); - } - - // A sample of the big list, without validation, as the site plans. - for (var i = 0; i < bigList.Count; i += 40) - { - cases.Add(new Case($"big-{i:D4}_medium", OilFieldOptions.ForMediumElectricPole, bigList[i], true, Mode.Selected)); - } - - return cases; - } - - /// - /// The plan's pipe set before PlanUndergroundPipes: every pipe cell of the final grid, in - /// entity order, with each underground pair's skipped cells added right after its - /// left or upper end. - /// - private static ILocationSet RebuildPipeSet(Context context, SquareGrid grid) - { - var pipes = context.GetLocationSet(allowEnumerate: true); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - if (grid[location] is not Pipe) - { - continue; - } - - pipes.Add(location); - if (grid[location] is UndergroundPipe { Direction: Direction.Left or Direction.Up } start) - { - var step = start.Direction == Direction.Left ? new Location(1, 0) : new Location(0, 1); - var end = start.Direction == Direction.Left ? Direction.Right : Direction.Down; - var current = location.Translate(step); - while (grid[current] is not UndergroundPipe u || u.Direction != end) - { - pipes.Add(current); - current = current.Translate(step); - } - } - } - - return pipes; - } - - /// Whether two grids hold the same pipe entities at the same cells. - private static bool SamePipes(SquareGrid a, SquareGrid b) - { - static Dictionary Pipes(SquareGrid grid) - { - var result = new Dictionary(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - switch (grid[location]) - { - case UndergroundPipe u: - result.Add(location, $"UndergroundPipe {u.Direction}"); - break; - case Pipe p: - result.Add(location, p.GetType().Name); - break; - } - } - - return result; - } - - var pa = Pipes(a); - var pb = Pipes(b); - return pa.Count == pb.Count && pa.All(x => pb.TryGetValue(x.Key, out var v) && v == x.Value); - } - - private static ILocationDictionary> Copy(Context context, ILocationDictionary> source) - { - var copy = context.GetLocationDictionary>(); - foreach (var pair in source.EnumeratePairs()) - { - copy.Add(pair.Key, pair.Value.ToList()); - } - - return copy; - } - - private static void WriteLocations(Utf8JsonWriter writer, string name, IEnumerable locations) - { - writer.WriteStartArray(name); - foreach (var location in locations) - { - writer.WriteNumberValue(location.X); - writer.WriteNumberValue(location.Y); - } - - writer.WriteEndArray(); - } - - private static void WriteTerminalDictionary(Utf8JsonWriter writer, string name, ILocationDictionary> dictionary) - { - writer.WriteStartArray(name); - foreach (var pair in dictionary.EnumeratePairs()) - { - writer.WriteStartArray(); - writer.WriteNumberValue(pair.Key.X); - writer.WriteNumberValue(pair.Key.Y); - writer.WriteStartArray(); - foreach (var t in pair.Value) - { - writer.WriteStartArray(); - writer.WriteNumberValue(t.Center.X); - writer.WriteNumberValue(t.Center.Y); - writer.WriteNumberValue(t.Terminal.X); - writer.WriteNumberValue(t.Terminal.Y); - writer.WriteNumberValue((int)t.Direction); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } - - /// Entities as `[x, y, kind, id, ...extra]`, the grids.jsonl encoding. - private static void WriteEntities(Utf8JsonWriter writer, string name, SquareGrid grid, IEnumerable locations) - { - writer.WriteStartArray(name); - foreach (var location in locations) - { - var entity = grid[location] ?? throw new InvalidOperationException($"No entity at {location}."); - writer.WriteStartArray(); - writer.WriteNumberValue(location.X); - writer.WriteNumberValue(location.Y); - writer.WriteStringValue(entity.GetType().Name); - writer.WriteNumberValue(entity.Id); - switch (entity) - { - case UndergroundPipe underground: - writer.WriteNumberValue((int)underground.Direction); - break; - case Terminal: - case Pipe: - case AvoidEntity: - case TemporaryEntity: - break; - case PumpjackCenter pumpjack: - writer.WriteNumberValue((int)pumpjack.Direction); - break; - case PumpjackSide side: - writer.WriteNumberValue(side.Center.Id); - break; - default: - throw new NotSupportedException($"pipes.jsonl has no encoding for {entity.GetType().Name}."); - } - - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - } -} diff --git a/rust/planner/oracle/PlannerOracle.csproj b/rust/planner/oracle/PlannerOracle.csproj deleted file mode 100644 index c4fd536c..00000000 --- a/rust/planner/oracle/PlannerOracle.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - - Exe - net10.0 - enable - enable - true - Knapcode.FactorioTools.PlannerOracle - - - - - - - diff --git a/rust/planner/oracle/PolesAndHeatRecorder.cs b/rust/planner/oracle/PolesAndHeatRecorder.cs deleted file mode 100644 index 907abbc3..00000000 --- a/rust/planner/oracle/PolesAndHeatRecorder.cs +++ /dev/null @@ -1,836 +0,0 @@ -// Records AddElectricPoles and AddHeatPipes for the Rust port (#138), stage 10. -// Writes poles_and_heat.jsonl. The format is in rust/planner/FIXTURES.md. -// -// Like HelpersRecorder, every case runs on a context the recorder rebuilds from a recorded -// snapshot, so .NET and the Rust test start from the same container state: entities added in -// the recorded order, into fresh sets and dictionaries, with the recorded next entity id. -// The snapshots are the states the planner hands these steps, taken from finished plans: -// -// - before poles: the finished grid without its poles (AddElectricPoles runs last, and with -// AddLast it only adds poles), checked by replanning the poles and comparing with the plan; -// - poles first: only the pumpjacks, with every terminal avoided, as Planner.cs does on its -// retry path; -// - before heat: the grid without poles, beacons and heat pipes (AddHeatPipes.Execute), or -// with only the pumpjacks and the plan's pipe tiles passed in (AddHeatPipes.Route); -// - a routed heat network plus the plan's beacons (AddHeatPipes.ExtendToBeacons). - -using System.Reflection; -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; -using static Knapcode.FactorioTools.OilField.Helpers; - -static class PolesAndHeatRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static readonly FieldInfo NextIdField = typeof(SquareGrid).GetField("_nextId", BindingFlags.NonPublic | BindingFlags.Instance) - ?? throw new InvalidOperationException("SquareGrid._nextId not found."); - - /// An entity: location, kind code (see KindCode), id, and per-kind state. - private sealed record Ent(int X, int Y, int Kind, int Id, int Extra, int[]? Neighbors); - - private sealed class Snapshot - { - public required string Name; - public required int BeaconWidth, BeaconHeight, PoleWidth, PoleHeight; - public required int PoleSupplyWidth, PoleSupplyHeight; - public required double WireReach, WireReachSquared; - public required bool ValidateSolution, AddHeatPipes; - public required int Width, Height, NextId; - public required List Entities; - public required List Centers; - public required List? HeatPipes; - } - - private sealed class Fnv - { - private ulong _hash = 1469598103934665603UL; - - public Fnv Mix(int value) - { - var v = unchecked((uint)value); - for (var b = 0; b < 4; b++) - { - _hash ^= (v >> (8 * b)) & 0xFF; - _hash *= 1099511628211UL; - } - - return this; - } - - public Fnv Mix(Location location) => Mix(location.X).Mix(location.Y); - - public string Hex => _hash.ToString("x16"); - } - - private static int _contextCount; - private static int _caseCount; - private static int _planPolesMatched; - private static int _planPolesCompared; - - public static void Record(string outputDir, IReadOnlyList smallList) - { - var path = Path.Combine(outputDir, "poles_and_heat.jsonl"); - _contextCount = 0; - _caseCount = 0; - _planPolesMatched = 0; - _planPolesCompared = 0; - - using (var stream = File.Create(path)) - { - RecordPoles(stream, smallList); - RecordHeat(stream, smallList); - } - - Console.WriteLine($"poles_and_heat.jsonl: {_contextCount} contexts, {_caseCount} cases, {new FileInfo(path).Length} bytes, replanned poles match the plan in {_planPolesMatched} of {_planPolesCompared}"); - } - - private static OilFieldOptions Opts(OilFieldOptions options, bool beacons, bool overlap, bool heat, Quality poleQuality = Quality.Normal) - { - options.AddBeacons = beacons; - options.OverlapBeacons = overlap; - options.AddHeatPipes = heat; - options.ElectricPoleQuality = poleQuality; - options.ValidateSolution = true; - return options; - } - - // --------------------------------------------------------------------------------------- - // AddElectricPoles - - private static void RecordPoles(Stream stream, IReadOnlyList smallList) - { - var sets = new (string Name, Func Options, int Stride, int Offset)[] - { - ("medium_b_o_u", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: false), 2, 0), - ("small_u", () => Opts(OilFieldOptions.ForSmallElectricPole, beacons: false, overlap: false, heat: false), 4, 1), - ("big_b", () => Opts(OilFieldOptions.ForBigElectricPole, beacons: true, overlap: false, heat: false), 4, 2), - ("substation_b_o", () => Opts(OilFieldOptions.ForSubstation, beacons: true, overlap: true, heat: false), 4, 3), - ("medium_rare_b", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: false, heat: false, Quality.Rare), 6, 1), - ("small_legendary", () => Opts(OilFieldOptions.ForSmallElectricPole, beacons: false, overlap: false, heat: false, Quality.Legendary), 6, 4), - ("medium_heat_b", () => Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: true), 6, 5), - }; - - foreach (var set in sets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - var name = $"small-{i:D4}_{set.Name}"; - var options = set.Options(); - var blueprint = ParseBlueprint.Execute(smallList[i]); - var result = Planner.Execute(options, blueprint); - var context = result.Context; - var planPoles = CellsOf(context.Grid, e => e is ElectricPoleCenter); - - // Before poles: the plan without them, as AddLast hands it to AddElectricPoles. - var noPoles = Capture(name + "_no-poles", context, e => e is ElectricPoleCenter or ElectricPoleSide); - var noPolesId = WriteSnapshot(stream, noPoles); - var replanned = RecordPoleCase(stream, noPolesId, noPoles, avoid: new List(), allowRetries: false, planPoles); - _planPolesCompared++; - if (replanned is not null && replanned.SequenceEqual(planPoles)) - { - _planPolesMatched++; - } - - // Poles first, avoiding every terminal: only the pumpjacks (and avoided cells). - if (i % 3 == 0) - { - var initial = InitializeContext.Execute(options, blueprint, Array.Empty()); - var terminals = new List(); - foreach (var list in initial.CenterToTerminals.Values) - { - foreach (var terminal in list) - { - terminals.Add(terminal.Terminal); - } - } - - var bare = Capture(name + "_pumpjacks", context, e => e is not (PumpjackCenter or PumpjackSide or AvoidEntity)); - var bareId = WriteSnapshot(stream, bare); - RecordPoleCase(stream, bareId, bare, terminals, allowRetries: false, planPoles: null); - RecordPoleCase(stream, bareId, bare, terminals, allowRetries: true, planPoles: null); - } - - // Crowded grids, so the cover fails and the retry strategies run. - if (i % 4 == 1) - { - foreach (var fraction in new[] { 0.5, 0.8 }) - { - var random = new Random(1000 + i * 10 + (int)(fraction * 10)); - var empty = new List(); - for (var y = 0; y < noPoles.Height; y++) - { - for (var x = 0; x < noPoles.Width; x++) - { - var location = new Location(x, y); - if (context.Grid.IsEmpty(location) || context.Grid[location] is ElectricPoleCenter or ElectricPoleSide) - { - empty.Add(location); - } - } - } - - var avoid = empty.Where(_ => random.NextDouble() < fraction).ToList(); - RecordPoleCase(stream, noPolesId, noPoles, avoid, allowRetries: true, planPoles: null); - RecordPoleCase(stream, noPolesId, noPoles, avoid, allowRetries: false, planPoles: null); - } - } - } - } - - // Blocked entities: avoid every free cell around one pumpjack, one beacon, or both, so - // no pole can power it. Without retries that fails; with them it walks the retry - // strategies down to removing the unpowered entities. - for (var i = 0; i < smallList.Count; i += 5) - { - var options = Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: false); - var result = Planner.Execute(options, ParseBlueprint.Execute(smallList[i])); - var context = result.Context; - var noPoles = Capture($"small-{i:D4}_medium_b_o_blocked", context, e => e is ElectricPoleCenter or ElectricPoleSide); - var id = WriteSnapshot(stream, noPoles); - var pumpjack = context.Centers[i % context.Centers.Count]; - var beaconCells = CellsOf(context.Grid, e => e is BeaconCenter); - var targets = new List<(string Name, List Centers)> { ("pumpjack", new List { pumpjack }) }; - if (beaconCells.Count > 0) - { - var beacon = beaconCells[i % beaconCells.Count]; - targets.Add(("beacon", new List { beacon })); - targets.Add(("both", new List { pumpjack, beacon })); - } - - foreach (var (_, centers) in targets) - { - var radius = (noPoles.PoleSupplyWidth / 2) + 3; - var avoid = new List(); - for (var y = 0; y < noPoles.Height; y++) - { - for (var x = 0; x < noPoles.Width; x++) - { - var location = new Location(x, y); - var free = context.Grid.IsEmpty(location) || context.Grid[location] is ElectricPoleCenter or ElectricPoleSide; - if (free && centers.Any(c => Math.Abs(c.X - x) <= radius && Math.Abs(c.Y - y) <= radius)) - { - avoid.Add(location); - } - } - } - - RecordPoleCase(stream, id, noPoles, avoid, allowRetries: false, planPoles: null); - RecordPoleCase(stream, id, noPoles, avoid, allowRetries: true, planPoles: null); - } - } - - // The sample, with and without beacons. - foreach (var beacons in new[] { false, true }) - { - var options = Opts(OilFieldOptions.ForMediumElectricPole, beacons, overlap: true, heat: false); - var result = Planner.Execute(options, Planner.ExecuteSample().Context.InputBlueprint); - var planPoles = CellsOf(result.Context.Grid, e => e is ElectricPoleCenter); - var noPoles = Capture($"sample_{(beacons ? "b" : "plain")}_no-poles", result.Context, e => e is ElectricPoleCenter or ElectricPoleSide); - var id = WriteSnapshot(stream, noPoles); - RecordPoleCase(stream, id, noPoles, new List(), allowRetries: false, planPoles); - } - } - - /// Runs AddElectricPoles on a rebuilt context. Returns the resulting pole centers in - /// grid order, or null when it returned null or threw. - private static List? RecordPoleCase(Stream stream, int ctx, Snapshot s, List avoid, bool allowRetries, List? planPoles) - { - var context = Rebuild(s); - ILocationSet avoidSet; - if (avoid.Count == 0) - { - avoidSet = EmptyLocationSet.Instance; - } - else - { - avoidSet = context.GetLocationSet(allowEnumerate: true); - foreach (var location in avoid) - { - avoidSet.Add(location); - } - } - - string? error = null; - string? exception = null; - ILocationSet? returned = null; - try - { - returned = AddElectricPoles.Execute(context, avoidSet, allowRetries); - } - catch (FactorioToolsException e) - { - error = e.Message; - } - catch (Exception e) when (e is not OutOfMemoryException) - { - exception = e.GetType().Name; - } - - var poles = CellsOf(context.Grid, e => e is ElectricPoleCenter); - Case(stream, "poles", ctx, w => - { - WriteLocations(w, "avoid", avoid); - w.WriteBoolean("allow_retries", allowRetries); - if (planPoles is not null) - { - WriteLocations(w, "plan_poles", planPoles); - } - }, w => - { - WriteError(w, error, exception); - if (returned is null) - { - w.WriteNull("returned"); - } - else - { - w.WriteStartObject("returned"); - w.WriteNumber("count", returned.Count); - WriteLocations(w, "members", poles.Where(returned.Contains)); - w.WriteEndObject(); - } - - WritePoles(w, context.Grid); - w.WriteString("grid", HashGrid(context.Grid)); - }); - - return error is null && exception is null && returned is not null ? poles : null; - } - - // --------------------------------------------------------------------------------------- - // AddHeatPipes - - private static void RecordHeat(Stream stream, IReadOnlyList smallList) - { - var runs = new List<(string Name, Func Run)>(); - for (var i = 0; i < smallList.Count; i += 2) - { - var blueprintString = smallList[i]; - runs.Add(($"small-{i:D4}_medium_heat", () => Planner.Execute(Opts(OilFieldOptions.ForMediumElectricPole, beacons: false, overlap: false, heat: true), ParseBlueprint.Execute(blueprintString)))); - } - - for (var i = 1; i < smallList.Count; i += 3) - { - var blueprintString = smallList[i]; - runs.Add(($"small-{i:D4}_medium_heat_b_o", () => Planner.Execute(Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: true), ParseBlueprint.Execute(blueprintString)))); - } - - for (var i = 2; i < smallList.Count; i += 6) - { - var blueprintString = smallList[i]; - runs.Add(($"small-{i:D4}_big_heat_b", () => Planner.Execute(Opts(OilFieldOptions.ForBigElectricPole, beacons: true, overlap: false, heat: true), ParseBlueprint.Execute(blueprintString)))); - } - - // PlannerTest's two heat snapshots. - runs.Add(("PlannerTest.AddsHeatPipesForAquilo", () => Planner.Execute(Opts(OilFieldOptions.ForMediumElectricPole, beacons: false, overlap: true, heat: true), ParseBlueprint.Execute(smallList[0])))); - runs.Add(("PlannerTest.AddsHeatPipesAndBeaconsTogetherForAquilo", () => Planner.Execute(Opts(OilFieldOptions.ForMediumElectricPole, beacons: true, overlap: true, heat: true), ParseBlueprint.Execute(smallList[0])))); - - foreach (var (name, run) in runs) - { - var result = run(); - var context = result.Context; - - // Execute: the pipes are on the grid, no heat yet. - var beforeHeat = Capture(name + "_pipes", context, e => e is ElectricPoleCenter or ElectricPoleSide or BeaconCenter or BeaconSide or HeatPipe, keepHeatPipes: false); - var beforeHeatId = WriteSnapshot(stream, beforeHeat); - { - var rebuilt = Rebuild(beforeHeat); - string? error = null, exception = null; - try - { - AddHeatPipes.Execute(rebuilt); - } - catch (FactorioToolsException e) - { - error = e.Message; - } - catch (Exception e) when (e is not OutOfMemoryException) - { - exception = e.GetType().Name; - } - - Case(stream, "heat_execute", beforeHeatId, _ => { }, w => - { - WriteError(w, error, exception); - if (rebuilt.HeatPipes is null) - { - w.WriteNull("heat_pipes"); - } - else - { - WriteLocations(w, "heat_pipes", rebuilt.HeatPipes.EnumerateItems()); - } - - w.WriteString("grid", HashGrid(rebuilt.Grid)); - }); - } - - // Route: only the pumpjacks on the grid, the plan's pipe tiles passed in. - var pipeTiles = CellsOf(context.Grid, e => e is Pipe); - var pumpjacks = Capture(name + "_pumpjacks", context, e => e is not (PumpjackCenter or PumpjackSide or AvoidEntity), keepHeatPipes: false); - var pumpjacksId = WriteSnapshot(stream, pumpjacks); - { - var rebuilt = Rebuild(pumpjacks); - var tiles = rebuilt.GetLocationSet(allowEnumerate: true); - foreach (var location in pipeTiles) - { - tiles.Add(location); - } - - string? error = null, exception = null; - ILocationSet? chosen = null, uncoveredPipes = null, uncoveredCenters = null; - try - { - chosen = AddHeatPipes.Route(rebuilt, tiles, out uncoveredPipes, out uncoveredCenters); - } - catch (FactorioToolsException e) - { - error = e.Message; - } - catch (Exception e) when (e is not OutOfMemoryException) - { - exception = e.GetType().Name; - } - - Case(stream, "heat_route", pumpjacksId, w => WriteLocations(w, "pipe_tiles", pipeTiles), w => - { - WriteError(w, error, exception); - WriteLocations(w, "chosen", chosen?.EnumerateItems() ?? Array.Empty()); - WriteLocations(w, "uncovered_pipes", uncoveredPipes?.EnumerateItems() ?? Array.Empty()); - WriteLocations(w, "uncovered_centers", uncoveredCenters?.EnumerateItems() ?? Array.Empty()); - w.WriteString("grid", HashGrid(rebuilt.Grid)); - }); - } - - // ExtendToBeacons: route and place the heat network, add the plan's beacons where - // they still fit, and extend the network to them. - var beacons = CellsOf(context.Grid, e => e is BeaconCenter); - if (beacons.Count > 0) - { - var staged = Rebuild(beforeHeat); - staged.Options.AddHeatPipes = true; - AddHeatPipes.Execute(staged); - var placed = new List(); - foreach (var center in beacons) - { - if (DoesProviderFit(staged.Grid, staged.Options.BeaconWidth, staged.Options.BeaconHeight, center)) - { - AddBeaconsToGrid(staged.Grid, staged.Options, new[] { center }); - placed.Add(center); - } - } - - var withBeacons = Capture(name + "_beacons", staged, _ => false, keepHeatPipes: true); - var withBeaconsId = WriteSnapshot(stream, withBeacons); - var rebuilt = Rebuild(withBeacons); - - string? error = null, exception = null; - ILocationSet? heated = null; - try - { - heated = AddHeatPipes.ExtendToBeacons(rebuilt, placed); - } - catch (FactorioToolsException e) - { - error = e.Message; - } - catch (Exception e) when (e is not OutOfMemoryException) - { - exception = e.GetType().Name; - } - - Case(stream, "heat_extend", withBeaconsId, w => WriteLocations(w, "beacons", placed), w => - { - WriteError(w, error, exception); - WriteLocations(w, "heated", heated?.EnumerateItems() ?? Array.Empty()); - WriteLocations(w, "heat_pipes", rebuilt.HeatPipes?.EnumerateItems() ?? Array.Empty()); - w.WriteString("grid", HashGrid(rebuilt.Grid)); - }); - } - } - } - - // --------------------------------------------------------------------------------------- - // Snapshots - - private static Snapshot Capture(string name, Context context, Func exclude, bool keepHeatPipes = false) - { - var grid = context.Grid; - var entities = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var entity = grid[location]!; - if (exclude(entity)) - { - continue; - } - - entities.Add(ToEnt(location, entity)); - } - - // Excluded poles are always excluded together with every other pole, so no recorded - // pole is left pointing at one that is gone. - return new Snapshot - { - Name = name, - BeaconWidth = context.Options.BeaconWidth, - BeaconHeight = context.Options.BeaconHeight, - PoleWidth = context.Options.ElectricPoleWidth, - PoleHeight = context.Options.ElectricPoleHeight, - PoleSupplyWidth = context.ElectricPoleSupplyWidthWithQuality, - PoleSupplyHeight = context.ElectricPoleSupplyHeightWithQuality, - WireReach = context.ElectricPoleWireReachWithQuality, - WireReachSquared = context.ElectricPoleWireReachSquaredWithQuality, - ValidateSolution = context.Options.ValidateSolution, - AddHeatPipes = context.Options.AddHeatPipes, - Width = grid.Width, - Height = grid.Height, - NextId = (int)NextIdField.GetValue(grid)!, - Entities = entities, - Centers = context.Centers.ToList(), - HeatPipes = keepHeatPipes ? context.HeatPipes?.EnumerateItems().ToList() : null, - }; - } - - private static Ent ToEnt(Location location, GridEntity entity) - { - var code = KindCode(entity); - return entity switch - { - UndergroundPipe u => new Ent(location.X, location.Y, code, entity.Id, (int)u.Direction, null), - PumpjackCenter p => new Ent(location.X, location.Y, code, entity.Id, (int)p.Direction, null), - PumpjackSide s => new Ent(location.X, location.Y, code, entity.Id, s.Center.Id, null), - BeaconSide s => new Ent(location.X, location.Y, code, entity.Id, s.Center.Id, null), - ElectricPoleSide s => new Ent(location.X, location.Y, code, entity.Id, s.Center.Id, null), - ElectricPoleCenter p => new Ent(location.X, location.Y, code, entity.Id, 0, p.Neighbors.ToArray()), - _ => new Ent(location.X, location.Y, code, entity.Id, 0, null), - }; - } - - /// A fresh context holding the snapshot, built the way the Rust test builds it. - private static Context Rebuild(Snapshot s) - { - var options = new OilFieldOptions - { - BeaconWidth = s.BeaconWidth, - BeaconHeight = s.BeaconHeight, - ElectricPoleWidth = s.PoleWidth, - ElectricPoleHeight = s.PoleHeight, - ValidateSolution = s.ValidateSolution, - AddHeatPipes = s.AddHeatPipes, - }; - - var grid = new PipeGrid(s.Width, s.Height); - var byId = new Dictionary(); - foreach (var e in s.Entities) - { - GridEntity? entity = e.Kind switch - { - 0 => new PumpjackCenter(e.Id) { Direction = (Direction)e.Extra }, - 2 => new BeaconCenter(e.Id), - 4 => new ElectricPoleCenter(e.Id), - _ => null, - }; - if (entity is not null) - { - byId.Add(e.Id, entity); - } - } - - foreach (var e in s.Entities) - { - GridEntity entity = e.Kind switch - { - 0 or 2 or 4 => byId[e.Id], - 1 => new PumpjackSide(e.Id, (PumpjackCenter)byId[e.Extra]), - 3 => new BeaconSide(e.Id, (BeaconCenter)byId[e.Extra]), - 5 => new ElectricPoleSide(e.Id, (ElectricPoleCenter)byId[e.Extra]), - 6 => new Pipe(e.Id), - 7 => new Terminal(e.Id), - 8 => new UndergroundPipe(e.Id, (Direction)e.Extra), - 9 => new HeatPipe(e.Id), - 10 => new TemporaryEntity(e.Id), - 11 => new AvoidEntity(e.Id), - _ => throw new NotSupportedException(e.Kind.ToString()), - }; - grid.AddEntity(new Location(e.X, e.Y), entity); - } - - foreach (var e in s.Entities) - { - if (e.Neighbors is not null) - { - var pole = (ElectricPoleCenter)byId[e.Id]; - foreach (var id in e.Neighbors) - { - pole.Neighbors.Add(id); - } - } - } - - NextIdField.SetValue(grid, s.NextId); - - var context = new Context - { - Options = options, - InputBlueprint = new Blueprint { Entities = Array.Empty(), Icons = Array.Empty(), Item = ItemNames.Vanilla.Blueprint }, - DeltaX = 0, - DeltaY = 0, - Grid = grid, - Centers = s.Centers.ToList(), - CenterToTerminals = new LocationHashDictionary>(), - CenterToOriginalDirection = new LocationHashDictionary(), - LocationToTerminals = new LocationHashDictionary>(), - LocationToAdjacentCount = new int[s.Width * s.Height], - SharedInstances = new SharedInstances(grid), - ElectricPoleSupplyWidthWithQuality = s.PoleSupplyWidth, - ElectricPoleSupplyHeightWithQuality = s.PoleSupplyHeight, - ElectricPoleWireReachWithQuality = s.WireReach, - ElectricPoleWireReachSquaredWithQuality = s.WireReachSquared, - }; - - if (s.HeatPipes is not null) - { - var heatPipes = context.GetLocationSet(allowEnumerate: true); - foreach (var location in s.HeatPipes) - { - heatPipes.Add(location); - } - - context.HeatPipes = heatPipes; - } - - return context; - } - - private static int WriteSnapshot(Stream stream, Snapshot s) - { - var id = _contextCount++; - WriteLine(stream, w => - { - w.WriteString("kind", "context"); - w.WriteNumber("id", id); - w.WriteString("name", s.Name); - w.WriteStartArray("sizes"); - w.WriteNumberValue(s.BeaconWidth); - w.WriteNumberValue(s.BeaconHeight); - w.WriteNumberValue(s.PoleWidth); - w.WriteNumberValue(s.PoleHeight); - w.WriteNumberValue(s.PoleSupplyWidth); - w.WriteNumberValue(s.PoleSupplyHeight); - w.WriteEndArray(); - w.WriteStartArray("wire_reach"); - w.WriteNumberValue(s.WireReach); - w.WriteNumberValue(s.WireReachSquared); - w.WriteEndArray(); - w.WriteBoolean("validate_solution", s.ValidateSolution); - w.WriteBoolean("add_heat_pipes", s.AddHeatPipes); - w.WriteNumber("width", s.Width); - w.WriteNumber("height", s.Height); - w.WriteNumber("next_id", s.NextId); - w.WriteStartArray("entities"); - foreach (var e in s.Entities) - { - w.WriteStartArray(); - w.WriteNumberValue(e.X); - w.WriteNumberValue(e.Y); - w.WriteNumberValue(e.Kind); - w.WriteNumberValue(e.Id); - switch (e.Kind) - { - case 0: - case 1: - case 3: - case 5: - case 8: - w.WriteNumberValue(e.Extra); - break; - case 4: - w.WriteStartArray(); - foreach (var n in e.Neighbors!) - { - w.WriteNumberValue(n); - } - - w.WriteEndArray(); - break; - } - - w.WriteEndArray(); - } - - w.WriteEndArray(); - WriteLocations(w, "centers", s.Centers); - if (s.HeatPipes is null) - { - w.WriteNull("heat_pipes"); - } - else - { - WriteLocations(w, "heat_pipes", s.HeatPipes); - } - }); - - return id; - } - - // --------------------------------------------------------------------------------------- - // Writing helpers - - private static void WriteLine(Stream stream, Action body) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - body(writer); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - private static void Case(Stream stream, string op, int ctx, Action inputs, Action outputs) - { - _caseCount++; - WriteLine(stream, w => - { - w.WriteString("kind", "case"); - w.WriteString("op", op); - w.WriteNumber("ctx", ctx); - inputs(w); - w.WriteStartObject("out"); - outputs(w); - w.WriteEndObject(); - }); - } - - private static void WriteError(Utf8JsonWriter w, string? error, string? exception) - { - if (error is null) - { - w.WriteNull("error"); - } - else - { - w.WriteString("error", error); - } - - if (exception is not null) - { - w.WriteString("exception", exception); - } - } - - /// A flat [x0, y0, x1, y1, ...] array. - private static void WriteLocations(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var l in locations) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - } - - /// Every pole, in grid order, as [x, y, id, [neighbor ids in order]]: what the - /// grid hash covers, spelled out so a mismatch is readable. - private static void WritePoles(Utf8JsonWriter w, SquareGrid grid) - { - w.WriteStartArray("poles"); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - if (grid[location] is ElectricPoleCenter pole) - { - w.WriteStartArray(); - w.WriteNumberValue(location.X); - w.WriteNumberValue(location.Y); - w.WriteNumberValue(pole.Id); - w.WriteStartArray(); - foreach (var n in pole.Neighbors) - { - w.WriteNumberValue(n); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - } - - w.WriteEndArray(); - } - - private static List CellsOf(SquareGrid grid, Func match) - { - var cells = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - if (match(grid[location]!)) - { - cells.Add(location); - } - } - - return cells; - } - - // --------------------------------------------------------------------------------------- - // The grid hash, defined the same way in rust/planner/tests/helpers.rs and - // rust/planner/tests/poles_and_heat.rs - - private static int KindCode(GridEntity e) => e switch - { - PumpjackCenter => 0, - PumpjackSide => 1, - BeaconCenter => 2, - BeaconSide => 3, - ElectricPoleCenter => 4, - ElectricPoleSide => 5, - UndergroundPipe => 8, - Terminal => 7, - Pipe => 6, - HeatPipe => 9, - TemporaryEntity => 10, - AvoidEntity => 11, - _ => throw new NotSupportedException(e.GetType().Name), - }; - - /// Every entity in enumeration order, with its id and per-kind state, then the - /// next id. Reading the next id takes it, so this is the last thing done to a grid. - private static string HashGrid(SquareGrid grid) - { - var h = new Fnv(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var e = grid[location]!; - h.Mix(location).Mix(KindCode(e)).Mix(e.Id); - switch (e) - { - case PumpjackCenter p: h.Mix((int)p.Direction); break; - case PumpjackSide s: h.Mix(s.Center.Id); break; - case BeaconSide s: h.Mix(s.Center.Id); break; - case ElectricPoleSide s: h.Mix(s.Center.Id); break; - case UndergroundPipe u: h.Mix((int)u.Direction); break; - case ElectricPoleCenter pole: - h.Mix(pole.Neighbors.Count); - foreach (var n in pole.Neighbors) - { - h.Mix(n); - } - - break; - } - } - - h.Mix(grid.GetId()); - return h.Hex; - } -} diff --git a/rust/planner/oracle/Program.cs b/rust/planner/oracle/Program.cs deleted file mode 100644 index 67f8f25d..00000000 --- a/rust/planner/oracle/Program.cs +++ /dev/null @@ -1,323 +0,0 @@ -// Records the C# planner's behavior as fixtures for the Rust port (#138). -// Usage: PlannerOracle [--check-snapshots] -// Writes grids.jsonl (stage 1) and algorithms.jsonl (stage 2, see Algorithms.cs). -// Format and how to regenerate: rust/planner/FIXTURES.md. - -using System.Text; -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -if (args.Length < 1) -{ - Console.Error.WriteLine("usage: PlannerOracle [--check-snapshots]"); - return 2; -} - -var outputDir = Path.GetFullPath(args[0]); -var checkSnapshots = args.Contains("--check-snapshots"); -var repoRoot = FindRepoRoot(outputDir) ?? FindRepoRoot(Directory.GetCurrentDirectory()) - ?? throw new InvalidOperationException("Could not find the repo root (FactorioTools.sln) above the output directory or the current directory."); -var oilFieldTestDir = Path.Combine(repoRoot, "test", "FactorioTools.Test", "OilField"); -var smallList = ParseBlueprint.ReadBlueprintFile(Path.Combine(oilFieldTestDir, "small-list.txt")); - -Directory.CreateDirectory(outputDir); - -var cases = GridCases.Build(smallList); -var gridsPath = Path.Combine(outputDir, "grids.jsonl"); -var snapshotMismatches = 0; -var snapshotMatches = 0; -var kindCounts = new SortedDictionary(StringComparer.Ordinal); - -using (var stream = File.Create(gridsPath)) -{ - foreach (var gridCase in cases) - { - var result = gridCase.Run(); - var grid = result.Context.Grid; - var builder = new StringBuilder(); - grid.ToString(builder, spacing: 1); - - // StringBuilder.AppendLine writes Environment.NewLine. Record LF everywhere so the - // fixture does not depend on the OS the recorder ran on (the Verify snapshots are - // LF too, via .gitattributes). - var text = builder.ToString().Replace("\r\n", "\n"); - - GridCases.Write(stream, gridCase.Name, grid, text, kindCounts); - - if (checkSnapshots && gridCase.SnapshotFile is not null) - { - var snapshotPath = Path.Combine(oilFieldTestDir, gridCase.SnapshotFile); - var snapshot = File.ReadAllText(snapshotPath).Replace("\r\n", "\n"); // strips the BOM - var gridStart = snapshot.IndexOf("\n\n", StringComparison.Ordinal); - var snapshotGrid = gridStart < 0 ? null : snapshot.Substring(gridStart + 2); - if (snapshotGrid == text) - { - snapshotMatches++; - } - else - { - snapshotMismatches++; - Console.Error.WriteLine($"grid differs from snapshot: {gridCase.Name} vs {gridCase.SnapshotFile}"); - } - } - } -} - -Console.WriteLine($"grids.jsonl: {cases.Count} cases, {new FileInfo(gridsPath).Length} bytes"); - -var bigList = ParseBlueprint.ReadBlueprintFile(Path.Combine(oilFieldTestDir, "big-list.txt")); -AlgorithmCases.Write(outputDir, smallList, bigList); -Console.WriteLine("entity kinds: " + string.Join(", ", kindCounts.Select(x => $"{x.Key}={x.Value}"))); -InitializeContextRecorder.Record(outputDir, repoRoot, smallList); -HelpersRecorder.Record(outputDir, smallList); -PipesRecorder.Record(outputDir, repoRoot, smallList); -FbePipesRecorder.Record(outputDir, smallList, bigList); -ConnectedCentersRecorder.Record(outputDir, smallList, bigList); -RotateOptimizeRecorder.Record(outputDir, smallList, bigList); -BeaconsRecorder.Record(outputDir, smallList, bigList); -PolesAndHeatRecorder.Record(outputDir, smallList); -if (checkSnapshots) -{ - Console.WriteLine($"snapshots: {snapshotMatches} match, {snapshotMismatches} differ"); - if (snapshotMismatches > 0) - { - return 1; - } -} - -return 0; - -static string? FindRepoRoot(string start) -{ - for (var dir = new DirectoryInfo(start); dir is not null; dir = dir.Parent) - { - if (File.Exists(Path.Combine(dir.FullName, "FactorioTools.sln"))) - { - return dir.FullName; - } - } - - return null; -} - -/// One planner run whose final grid is recorded. -/// The committed Verify snapshot with the same inputs, relative to -/// test/FactorioTools.Test/OilField, if there is one. Used only by --check-snapshots. -sealed record GridCase(string Name, Func Run, string? SnapshotFile); - -static class GridCases -{ - /// - /// The option sets, each copied from a PlannerOnAllBlueprintsAndOptions theory so every - /// case has a committed snapshot to check against. Between them they place every pole - /// preset, beacons with and without overlap, and pipes with and without undergrounds. - /// The first set runs on every blueprint. To keep the file small, each of the others runs - /// on every third blueprint (Stride 3, starting at Offset), so every blueprint is also - /// planned with exactly one other pole preset. - /// - private static readonly (string Pole, Func Options, bool Beacons, bool Overlap, bool Underground, int Stride, int Offset)[] OptionSets = - { - ("medium-electric-pole", () => OilFieldOptions.ForMediumElectricPole, true, true, true, 1, 0), - ("small-electric-pole", () => OilFieldOptions.ForSmallElectricPole, false, false, true, 3, 0), - ("big-electric-pole", () => OilFieldOptions.ForBigElectricPole, true, false, false, 3, 1), - ("substation", () => OilFieldOptions.ForSubstation, true, true, false, 3, 2), - }; - - /// Heat pipes run on every fourth blueprint, for the same reason. - private const int HeatStride = 4; - - public static List Build(IReadOnlyList smallList) - { - var cases = new List(); - - foreach (var set in OptionSets) - { - for (var i = set.Offset; i < smallList.Count; i += set.Stride) - { - var blueprintString = smallList[i]; - var suffix = $"{set.Pole}{(set.Beacons ? "_b" : "")}{(set.Overlap ? "_o" : "")}{(set.Underground ? "_u" : "")}"; - cases.Add(new GridCase( - $"small-{i:D4}_{suffix}", - () => - { - var options = set.Options(); - options.AddBeacons = set.Beacons; - options.OverlapBeacons = set.Overlap; - options.UseUndergroundPipes = set.Underground; - options.ValidateSolution = true; - return Planner.Execute(options, ParseBlueprint.Execute(blueprintString)); - }, - $"PlannerWithAllOptions/Theory.E_{i:D4}_{suffix}.verified.txt")); - } - } - - // Heat pipes (Aquilo), with the options PlannerTest's small-list heat theory uses. No - // snapshot exists for these. - for (var i = 0; i < smallList.Count; i += HeatStride) - { - var blueprintString = smallList[i]; - cases.Add(new GridCase( - $"small-{i:D4}_medium-electric-pole_heat", - () => - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - options.AddHeatPipes = true; - options.AddBeacons = false; - return Planner.Execute(options, ParseBlueprint.Execute(blueprintString)); - }, - null)); - } - - // PlannerTest cases with snapshots, copied input for input. - cases.Add(new GridCase("PlannerTest.ExecuteSample", Planner.ExecuteSample, "PlannerTest.ExecuteSample.verified.txt")); - - cases.Add(new GridCase( - "PlannerTest.AddsHeatPipesForAquilo", - () => - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - options.AddHeatPipes = true; - options.AddBeacons = false; - return Planner.Execute(options, ParseBlueprint.Execute(smallList[0])); - }, - "PlannerTest.AddsHeatPipesForAquilo.verified.txt")); - - cases.Add(new GridCase( - "PlannerTest.AddsHeatPipesAndBeaconsTogetherForAquilo", - () => - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - options.AddHeatPipes = true; - options.AddBeacons = true; - return Planner.Execute(options, ParseBlueprint.Execute(smallList[0])); - }, - "PlannerTest.AddsHeatPipesAndBeaconsTogetherForAquilo.verified.txt")); - - cases.Add(new GridCase( - "PlannerTest.AllowsLocationsToBeAvoided", - () => - { - var options = OilFieldOptions.ForMediumElectricPole; - options.ValidateSolution = true; - options.AddBeacons = true; - var blueprint = new Blueprint - { - Entities = new[] - { - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = -3, Y = -5 } }, - new Entity { Name = EntityNames.Vanilla.Pumpjack, Position = new Position { X = 4, Y = 5 } }, - }, - Icons = new[] - { - new Icon - { - Index = 1, - Signal = new SignalID - { - Name = EntityNames.Vanilla.Pumpjack, - Type = SignalTypes.Vanilla.Item, - }, - }, - }, - Item = ItemNames.Vanilla.Blueprint, - Version = 0, - }; - var avoid = Enumerable.Range(-7, 16).Select(x => new AvoidLocation(x, 0)).ToArray(); - return Planner.Execute(options, blueprint, avoid); - }, - "PlannerTest.AllowsLocationsToBeAvoided.verified.txt")); - - return cases; - } - - private static readonly JsonWriterOptions WriterOptions = new() - { - // Keeps '+', '<' and '>' readable in the grid text. Control characters such as the - // line feeds are still escaped, so each case stays on one line. - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - public static void Write(Stream stream, string name, SquareGrid grid, string text, IDictionary kindCounts) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - writer.WriteString("name", name); - writer.WriteNumber("width", grid.Width); - writer.WriteNumber("height", grid.Height); - writer.WriteStartArray("middle"); - writer.WriteNumberValue(grid.Middle.X); - writer.WriteNumberValue(grid.Middle.Y); - writer.WriteEndArray(); - - writer.WriteStartArray("entities"); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - var entity = grid[location] ?? throw new InvalidOperationException($"No entity at {location} in {name}."); - var kind = entity.GetType().Name; - kindCounts[kind] = kindCounts.TryGetValue(kind, out var count) ? count + 1 : 1; - - writer.WriteStartArray(); - writer.WriteNumberValue(location.X); - writer.WriteNumberValue(location.Y); - writer.WriteStringValue(kind); - writer.WriteNumberValue(entity.Id); - WriteExtra(writer, entity); - writer.WriteEndArray(); - } - - writer.WriteEndArray(); - writer.WriteString("text", text); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } - - /// The per-kind fields listed in rust/planner/FIXTURES.md. - private static void WriteExtra(Utf8JsonWriter writer, GridEntity entity) - { - switch (entity) - { - // Check the subclasses of Pipe first: Terminal and UndergroundPipe are Pipes. - case UndergroundPipe underground: - writer.WriteNumberValue((int)underground.Direction); - break; - case Terminal: - case Pipe: - case HeatPipe: - case BeaconCenter: - case TemporaryEntity: - case AvoidEntity: - break; - case PumpjackCenter pumpjack: - writer.WriteNumberValue((int)pumpjack.Direction); - break; - case PumpjackSide side: - writer.WriteNumberValue(side.Center.Id); - break; - case BeaconSide side: - writer.WriteNumberValue(side.Center.Id); - break; - case ElectricPoleSide side: - writer.WriteNumberValue(side.Center.Id); - break; - case ElectricPoleCenter pole: - writer.WriteStartArray(); - foreach (var id in pole.Neighbors) - { - writer.WriteNumberValue(id); - } - - writer.WriteEndArray(); - break; - default: - throw new NotSupportedException($"No fixture format for grid entity {entity.GetType().Name}. Add it to rust/planner/FIXTURES.md first."); - } - } -} diff --git a/rust/planner/oracle/RotateOptimizeRecorder.cs b/rust/planner/oracle/RotateOptimizeRecorder.cs deleted file mode 100644 index 90febcbd..00000000 --- a/rust/planner/oracle/RotateOptimizeRecorder.cs +++ /dev/null @@ -1,431 +0,0 @@ -// Records RotateOptimize for the Rust port (#138), stage 8. -// Writes rotate_optimize.jsonl. The format is in rust/planner/FIXTURES.md. -// -// AddPipes.GetSolutionGroups runs each pipe strategy on fresh copies of the terminal maps, then -// OptimizeAndAddSolutions copies the maps again and calls RotateOptimize.Execute on a copy of -// the strategy's pipes. This does the same through the public API. The recorded input pipes are -// a compact rebuild (the strategy's pipes added in order to a fresh set): HashSet's copy -// constructor can keep freed slots, which a rebuild from a list cannot reproduce, and later -// adds would fill them in a different order. Each case also runs on the planner's real copy and -// records whether that gave the same result, so the fixture says how often the hidden state -// mattered. - -using System.Text.Encodings.Web; -using System.Text.Json; -using Knapcode.FactorioTools; -using Knapcode.FactorioTools.Data; -using Knapcode.FactorioTools.OilField; - -static class RotateOptimizeRecorder -{ - private static readonly JsonWriterOptions WriterOptions = new() - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - - private static int _caseCount; - private static int _errorCount; - private static int _changedCount; - private static int _realCopyDiffers; - private static int _strategyFailures; - - public static void Record(string outputDir, IReadOnlyList smallList, IReadOnlyList bigList) - { - _caseCount = 0; - _errorCount = 0; - _changedCount = 0; - _realCopyDiffers = 0; - _strategyFailures = 0; - - var path = Path.Combine(outputDir, "rotate_optimize.jsonl"); - var grids = new Dictionary(StringComparer.Ordinal); - using (var stream = File.Create(path)) - { - for (var i = 0; i < smallList.Count; i++) - { - var blueprint = ParseBlueprint.Execute(smallList[i]); - RecordBlueprint(stream, grids, $"small-{i:D4}", blueprint, OilFieldOptions.ForMediumElectricPole, polesFirst: false); - - // Planner.cs's poles-first path: poles go in before the pipes, and terminals - // under a pole are dropped. RotateOptimize then walks a grid that holds poles. - if (i % 3 == 0) - { - RecordBlueprint(stream, grids, $"small-{i:D4}-poles-first-small", blueprint, OilFieldOptions.ForSmallElectricPole, polesFirst: true); - } - - if (i % 3 == 1) - { - RecordBlueprint(stream, grids, $"small-{i:D4}-poles-first-big", blueprint, OilFieldOptions.ForBigElectricPole, polesFirst: true); - } - } - - for (var i = 0; i < bigList.Count; i += 20) - { - var blueprint = ParseBlueprint.Execute(bigList[i]); - RecordBlueprint(stream, grids, $"big-{i:D4}", blueprint, OilFieldOptions.ForMediumElectricPole, polesFirst: false); - } - - // Every one-pumpjack blueprint in the big list: AddPipes's single-terminal path, - // where RotateOptimize returns at once. - var singles = 0; - for (var i = 0; i < bigList.Count && singles < 3; i++) - { - var blueprint = ParseBlueprint.Execute(bigList[i]); - var options = OilFieldOptions.ForMediumElectricPole; - options.PipeStrategies = OilFieldOptions.AllPipeStrategies.ToList(); - var context = InitializeContext.Execute(options, blueprint, Array.Empty()); - if (context.CenterToTerminals.Count != 1) - { - continue; - } - - singles++; - var terminal = context.CenterToTerminals.EnumeratePairs().Single().Value[0]; - Helpers.EliminateOtherTerminals(context, terminal); - var pipes = context.GetSingleLocationSet(terminal.Terminal); - RecordCall(stream, grids, $"big-{i:D4}-single", "single", context, pipes); - } - } - - Console.WriteLine( - $"rotate_optimize.jsonl: {_caseCount} cases ({_changedCount} changed which cells are pipes, {_errorCount} errors, " + - $"{_realCopyDiffers} differ on the planner's real pipe copy, {_strategyFailures} strategy runs failed and were skipped), " + - $"{grids.Count} grids, {new FileInfo(path).Length} bytes"); - } - - private static void RecordBlueprint(Stream stream, Dictionary grids, string name, Blueprint blueprint, OilFieldOptions options, bool polesFirst) - { - options.PipeStrategies = OilFieldOptions.AllPipeStrategies.ToList(); - Context context; - try - { - context = InitializeContext.Execute(options, blueprint, Array.Empty()); - } - catch (FactorioToolsException) - { - return; - } - - if (context.CenterToTerminals.Count < 2) - { - return; - } - - if (polesFirst) - { - var poles = AddElectricPoles.Execute(context, EmptyLocationSet.Instance, allowRetries: false); - if (poles is null) - { - return; - } - - // Copied from Planner.Execute: remove terminals overlapping with the added poles. - foreach (var terminals in context.CenterToTerminals.Values) - { - for (int i = 0; i < terminals.Count; i++) - { - var terminal = terminals[i]; - if (context.Grid.IsEmpty(terminal.Terminal)) - { - continue; - } - - terminals.RemoveAt(i); - context.LocationToTerminals[terminal.Terminal].Remove(terminal); - i--; - } - } - } - - var originalCenterToTerminals = context.CenterToTerminals; - var originalLocationToTerminals = context.LocationToTerminals; - - foreach (var strategy in context.Options.PipeStrategies) - { - context.CenterToTerminals = Copy(context, originalCenterToTerminals); - context.LocationToTerminals = Copy(context, originalLocationToTerminals); - - ILocationSet pipes; - try - { - switch (strategy) - { - case PipeStrategy.FbeOriginal: - case PipeStrategy.Fbe: - { - var result = AddPipesFbe.Execute(context, strategy); - if (result.Exception is not null) - { - _strategyFailures++; - continue; - } - - pipes = result.Data!.Pipes; - } - break; - - default: - { - var centerToConnectedCenters = AddPipesConnectedCenters.GetConnectedPumpjacks(context, strategy); - var result = AddPipesConnectedCenters.FindTrunksAndConnect(context, centerToConnectedCenters); - if (result.Exception is not null) - { - _strategyFailures++; - continue; - } - - pipes = result.Data!; - } - break; - } - } - catch (FactorioToolsException) - { - _strategyFailures++; - continue; - } - - RecordCall(stream, grids, $"{name}-{strategy}", strategy.ToString(), context, pipes); - } - - context.CenterToTerminals = originalCenterToTerminals; - context.LocationToTerminals = originalLocationToTerminals; - } - - /// OptimizeAndAddSolutions's call, twice: on a compact rebuild of the pipes (recorded) - /// and on the planner's own copy (compared). - private static void RecordCall(Stream stream, Dictionary grids, string name, string strategy, Context context, ILocationSet pipes) - { - var strategyCenterToTerminals = context.CenterToTerminals; - var strategyLocationToTerminals = context.LocationToTerminals; - - // The compact rebuild the Rust test can reproduce. - var single = pipes is SingleLocationSet; - ILocationSet input; - if (single) - { - input = context.GetLocationSet(pipes); - } - else - { - input = context.GetLocationSet(pipes.Count, allowEnumerate: true); - foreach (var pipe in pipes.EnumerateItems()) - { - input.Add(pipe); - } - } - - var inputPipes = input.EnumerateItems().ToList(); - var inputCenterToTerminals = Copy(context, strategyCenterToTerminals); - var inputLocationToTerminals = Copy(context, strategyLocationToTerminals); - - var gridId = WriteGrid(stream, grids, context.Grid); - - context.CenterToTerminals = Copy(context, strategyCenterToTerminals); - context.LocationToTerminals = Copy(context, strategyLocationToTerminals); - var compact = Run(context, input); - - context.CenterToTerminals = Copy(context, strategyCenterToTerminals); - context.LocationToTerminals = Copy(context, strategyLocationToTerminals); - var real = Run(context, context.GetLocationSet(pipes)); - - var realMatches = compact.Equals(real); - if (!realMatches) - { - _realCopyDiffers++; - } - - if (compact.Error is not null) - { - _errorCount++; - } - else if (!new HashSet(compact.Pipes).SetEquals(inputPipes)) - { - _changedCount++; - } - - _caseCount++; - WriteLine(stream, w => - { - w.WriteString("kind", "case"); - w.WriteString("name", name); - w.WriteString("strategy", strategy); - w.WriteNumber("grid", gridId); - w.WriteString("pipes_type", single ? "single" : "hash"); - WriteLocations(w, "pipes", inputPipes); - WriteTerminals(w, "center_to_terminals", inputCenterToTerminals); - WriteTerminals(w, "location_to_terminals", inputLocationToTerminals); - w.WriteStartObject("out"); - if (compact.Error is not null) - { - w.WriteString("error", compact.Error); - } - else - { - w.WriteNull("error"); - } - - WriteLocations(w, "pipes", compact.Pipes); - w.WriteString("center_to_terminals", compact.CenterToTerminals); - w.WriteString("location_to_terminals", compact.LocationToTerminals); - w.WriteEndObject(); - w.WriteBoolean("real_copy_matches", realMatches); - }); - - context.CenterToTerminals = strategyCenterToTerminals; - context.LocationToTerminals = strategyLocationToTerminals; - } - - private record Outcome(string? Error, List Pipes, string CenterToTerminals, string LocationToTerminals) - { - public virtual bool Equals(Outcome? other) - { - return other is not null - && Error == other.Error - && Pipes.SequenceEqual(other.Pipes) - && CenterToTerminals == other.CenterToTerminals - && LocationToTerminals == other.LocationToTerminals; - } - - public override int GetHashCode() => 0; - } - - private static Outcome Run(Context context, ILocationSet pipes) - { - string? error = null; - try - { - RotateOptimize.Execute(context, pipes); - } - catch (Exception ex) when (ex is FactorioToolsException or InvalidOperationException or KeyNotFoundException) - { - error = $"{ex.GetType().Name}: {ex.Message}"; - } - - return new Outcome( - error, - pipes.EnumerateItems().ToList(), - TerminalsText(context.CenterToTerminals), - TerminalsText(context.LocationToTerminals)); - } - - /// ToDictionary(context, x => x.Key, x => x.Value.ToList()), as AddPipes copies the maps. - private static ILocationDictionary> Copy(Context context, ILocationDictionary> source) - { - var copy = context.GetLocationDictionary>(); - foreach (var pair in source.EnumeratePairs()) - { - copy.Add(pair.Key, pair.Value.ToList()); - } - - return copy; - } - - /// A terminal map in enumeration order, as text: "kx,ky:cx,cy,tx,ty,dir;...|". - /// The Rust test builds the same text. - private static string TerminalsText(ILocationDictionary> dictionary) - { - var builder = new System.Text.StringBuilder(); - foreach (var pair in dictionary.EnumeratePairs()) - { - builder.Append(pair.Key.X).Append(',').Append(pair.Key.Y).Append(':'); - foreach (var t in pair.Value) - { - builder.Append(t.Center.X).Append(',').Append(t.Center.Y).Append(',') - .Append(t.Terminal.X).Append(',').Append(t.Terminal.Y).Append(',') - .Append((int)t.Direction).Append(';'); - } - - builder.Append('|'); - } - - return builder.ToString(); - } - - /// The grid as RotateOptimize sees it: which cells hold an entity, and which of those - /// are pipes. Written once per distinct grid. - private static int WriteGrid(Stream stream, Dictionary grids, SquareGrid grid) - { - var cells = new List(); - foreach (var location in grid.EntityLocations.EnumerateItems()) - { - cells.Add(location.X); - cells.Add(location.Y); - cells.Add(grid[location] is Pipe ? 1 : 0); - } - - var key = $"{grid.Width}x{grid.Height}:{string.Join(",", cells)}"; - if (grids.TryGetValue(key, out var id)) - { - return id; - } - - id = grids.Count; - grids.Add(key, id); - WriteLine(stream, w => - { - w.WriteString("kind", "grid"); - w.WriteNumber("id", id); - w.WriteNumber("width", grid.Width); - w.WriteNumber("height", grid.Height); - w.WriteStartArray("cells"); - foreach (var value in cells) - { - w.WriteNumberValue(value); - } - - w.WriteEndArray(); - }); - - return id; - } - - private static void WriteTerminals(Utf8JsonWriter w, string name, ILocationDictionary> dictionary) - { - w.WriteStartArray(name); - foreach (var pair in dictionary.EnumeratePairs()) - { - w.WriteStartArray(); - w.WriteNumberValue(pair.Key.X); - w.WriteNumberValue(pair.Key.Y); - w.WriteStartArray(); - foreach (var t in pair.Value) - { - w.WriteNumberValue(t.Center.X); - w.WriteNumberValue(t.Center.Y); - w.WriteNumberValue(t.Terminal.X); - w.WriteNumberValue(t.Terminal.Y); - w.WriteNumberValue((int)t.Direction); - } - - w.WriteEndArray(); - w.WriteEndArray(); - } - - w.WriteEndArray(); - } - - private static void WriteLocations(Utf8JsonWriter w, string name, IEnumerable locations) - { - w.WriteStartArray(name); - foreach (var l in locations) - { - w.WriteNumberValue(l.X); - w.WriteNumberValue(l.Y); - } - - w.WriteEndArray(); - } - - private static void WriteLine(Stream stream, Action body) - { - using (var writer = new Utf8JsonWriter(stream, WriterOptions)) - { - writer.WriteStartObject(); - body(writer); - writer.WriteEndObject(); - } - - stream.WriteByte((byte)'\n'); - } -} diff --git a/rust/planner/src/collection_ext.rs b/rust/planner/src/collection_ext.rs index 0ef15d33..1ee9db29 100644 --- a/rust/planner/src/collection_ext.rs +++ b/rust/planner/src/collection_ext.rs @@ -1,6 +1,6 @@ //! `CollectionExtensions`, from `src/FactorioTools/CollectionExtensions.cs`. //! -//! The C# replaces LINQ with these so the core transpiles to Lua. Most map straight onto +//! The C# replaced LINQ with these so the core could transpile to Lua. Most map straight onto //! Rust iterators. The ones kept here are those whose behavior is easy to get subtly wrong: //! //! - `MaxBy`/`MinBy`/`Max`/`Min` compare with `Comparer.Default` and only replace on a diff --git a/rust/planner/src/lib.rs b/rust/planner/src/lib.rs index 6c0f011b..be506d8f 100644 --- a/rust/planner/src/lib.rs +++ b/rust/planner/src/lib.rs @@ -1,5 +1,5 @@ -//! Rust port of the FactorioTools oil-field planner (#138), built stage by stage beside -//! the C# in `src/FactorioTools`. Each module names the C# file it copies. Wherever the C# +//! Rust port of the FactorioTools oil-field planner (#138), from the C# that lived in +//! `src/FactorioTools` until stage 13b. Each module names the C# file it copies. Wherever the C# //! result depends on .NET's ordering or tie-breaking, the port uses `dotnet_compat`. pub mod algorithms; diff --git a/rust/planner/tests/beacons.rs b/rust/planner/tests/beacons.rs index 0247e783..34a90301 100644 --- a/rust/planner/tests/beacons.rs +++ b/rust/planner/tests/beacons.rs @@ -1,7 +1,8 @@ //! Replays `fixtures/beacons.jsonl` (recorded from the C# by `oracle/BeaconsRecorder.cs`, -//! format in `FIXTURES.md`): each `PlanBeacons.Execute` call, on a context rebuilt from its -//! recorded snapshot the same way the recorder rebuilt it, must give the recorded solutions -//! (or error), and leave the grid's entity order and next id where the C# left them. +//! removed in stage 13b; format in `FIXTURES.md`): each `PlanBeacons.Execute` call, on a +//! context rebuilt from its recorded snapshot the same way the recorder rebuilt it, must +//! give the recorded solutions (or error), and leave the grid's entity order and next id +//! where the C# left them. use std::path::PathBuf; diff --git a/rust/planner/tests/factorio_oracle.rs b/rust/planner/tests/factorio_oracle.rs index c572c6eb..92250907 100644 --- a/rust/planner/tests/factorio_oracle.rs +++ b/rust/planner/tests/factorio_oracle.rs @@ -23,8 +23,7 @@ use entity_names::vanilla; /// The one place this file names the fixture's location. fn factorio_oracle_path() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../../test/FactorioTools.Test/OilField/factorio-oracle.json") + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../testdata/oil-field/factorio-oracle.json") } const RE_CAPTURE_HINT: &str = "Two likely causes: either Factorio changed (re-run \ diff --git a/rust/planner/tests/fbe_pipes.rs b/rust/planner/tests/fbe_pipes.rs index b947dc13..c2521fb4 100644 --- a/rust/planner/tests/fbe_pipes.rs +++ b/rust/planner/tests/fbe_pipes.rs @@ -1,6 +1,7 @@ //! Replays `fixtures/fbe_pipes.jsonl` (recorded from the C# by `oracle/FbePipesRecorder.cs`, -//! format in `FIXTURES.md`): `AddPipesFbe.Execute` on each recorded context must give the -//! recorded pipes, final strategy or error, and leave the terminal maps as the C# did. +//! removed in stage 13b; format in `FIXTURES.md`): `AddPipesFbe.Execute` on each recorded +//! context must give the recorded pipes, final strategy or error, and leave the terminal +//! maps as the C# did. use std::path::PathBuf; diff --git a/rust/planner/tests/helpers.rs b/rust/planner/tests/helpers.rs index 3caf2ade..fd556e22 100644 --- a/rust/planner/tests/helpers.rs +++ b/rust/planner/tests/helpers.rs @@ -1,6 +1,7 @@ //! Replays `fixtures/helpers.jsonl` (recorded from the C# by `oracle/HelpersRecorder.cs`, -//! format in `FIXTURES.md`): every `Helpers` and `Validate` case, on a context rebuilt from -//! its recorded snapshot the same way the recorder rebuilt it, must give the recorded result. +//! removed in stage 13b; format in `FIXTURES.md`): every `Helpers` and `Validate` case, on +//! a context rebuilt from its recorded snapshot the same way the recorder rebuilt it, must +//! give the recorded result. use std::collections::HashMap; use std::panic::{AssertUnwindSafe, catch_unwind}; diff --git a/rust/planner/tests/poles_and_heat.rs b/rust/planner/tests/poles_and_heat.rs index 165859d6..90264b76 100644 --- a/rust/planner/tests/poles_and_heat.rs +++ b/rust/planner/tests/poles_and_heat.rs @@ -1,8 +1,8 @@ //! Replays `fixtures/poles_and_heat.jsonl` (recorded from the C# by -//! `oracle/PolesAndHeatRecorder.cs`, format in `FIXTURES.md`): every `AddElectricPoles` and -//! `AddHeatPipes` case, on a context rebuilt from its recorded snapshot the same way the -//! recorder rebuilt it, must give the recorded result and leave the grid in the recorded -//! state (entities, ids, pole wiring and the next id). +//! `oracle/PolesAndHeatRecorder.cs`, removed in stage 13b; format in `FIXTURES.md`): every +//! `AddElectricPoles` and `AddHeatPipes` case, on a context rebuilt from its recorded +//! snapshot the same way the recorder rebuilt it, must give the recorded result and leave +//! the grid in the recorded state (entities, ids, pole wiring and the next id). use std::collections::HashMap; use std::panic::{AssertUnwindSafe, catch_unwind}; diff --git a/rust/planner/tests/rotate_optimize.rs b/rust/planner/tests/rotate_optimize.rs index 47f416cd..c48f4f78 100644 --- a/rust/planner/tests/rotate_optimize.rs +++ b/rust/planner/tests/rotate_optimize.rs @@ -1,6 +1,7 @@ //! Replays `fixtures/rotate_optimize.jsonl` (recorded from the C# by -//! `oracle/RotateOptimizeRecorder.cs`, format in `FIXTURES.md`): every `RotateOptimize.Execute` -//! call must leave the pipes and both terminal maps exactly as the C# did, in .NET's order. +//! `oracle/RotateOptimizeRecorder.cs`, removed in stage 13b; format in `FIXTURES.md`): +//! every `RotateOptimize.Execute` call must leave the pipes and both terminal maps exactly +//! as the C# did, in .NET's order. use std::collections::HashMap; use std::fmt::Write as _; diff --git a/rust/snapshots/Cargo.toml b/rust/snapshots/Cargo.toml index 5ca1b19c..6f384d1e 100644 --- a/rust/snapshots/Cargo.toml +++ b/rust/snapshots/Cargo.toml @@ -10,11 +10,6 @@ repository.workspace = true [lints] workspace = true -# Not in src/bin, which the repository's .gitignore ignores as a .NET output folder. -[[bin]] -name = "normalize-lists" -path = "tools/normalize-lists.rs" - [dependencies] # normalize-lists reads a line's own compressed data strictly, beside blueprint's reader. base64 = "0.22" diff --git a/rust/snapshots/tools/normalize-lists.rs b/rust/snapshots/src/bin/normalize-lists.rs similarity index 92% rename from rust/snapshots/tools/normalize-lists.rs rename to rust/snapshots/src/bin/normalize-lists.rs index 34d3df85..191e2a34 100644 --- a/rust/snapshots/tools/normalize-lists.rs +++ b/rust/snapshots/src/bin/normalize-lists.rs @@ -1,8 +1,8 @@ -//! Rewrites `big-list.txt` and `small-list.txt` the way the C# CLI's `oil-field normalize` -//! does: `cargo run --release -p snapshots --bin normalize-lists`. See +//! Rewrites `big-list.txt` and `small-list.txt` the way the removed C# CLI's +//! `oil-field normalize` did: `cargo run --release -p snapshots --bin normalize-lists`. See //! [`snapshots::normalize`] for what it changes and how it differs from the C#. //! -//! `--keep-invalid` keeps a line that does not normalize, where the C# would ask. +//! `--keep-invalid` keeps a line that does not normalize, where the C# asked. //! `--dir ` works on the lists in another directory instead of the test directory. use std::path::PathBuf; diff --git a/rust/snapshots/src/issue_cases.rs b/rust/snapshots/src/issue_cases.rs index 09b6b592..341d5d89 100644 --- a/rust/snapshots/src/issue_cases.rs +++ b/rust/snapshots/src/issue_cases.rs @@ -1,12 +1,10 @@ -//! Inputs of the snapshot tests in `test/FactorioTools.Test/OilField/PlannerIssueTest.cs`, -//! extracted from that file by `rust/snapshots/tools/extract_issue_cases.py` so the numbers -//! cannot drift by hand. Regenerate rather than edit if the C# test changes, from the repo root: +//! Inputs of the planner issue snapshot tests (the `PlannerIssueTest.*` snapshots in +//! `testdata/oil-field`). //! -//! ```text -//! python3 -I rust/snapshots/tools/extract_issue_cases.py \ -//! test/FactorioTools.Test/OilField/PlannerIssueTest.cs rust/snapshots/src/issue_cases.rs -//! cd rust && cargo fmt --all -//! ``` +//! This file was first generated from the C# `PlannerIssueTest.cs` by a script, +//! `extract_issue_cases.py`. Stage 13b removed both, so this file is now the source of truth +//! and is edited by hand. To add a case, add an entry here, then write its snapshot with +//! [`crate::UPDATE_COMMAND`]. /// One test: its name, the options preset, whether beacons are added, the pumpjack /// positions and the avoided locations. diff --git a/rust/snapshots/src/lib.rs b/rust/snapshots/src/lib.rs index 8f29403b..c27f7583 100644 --- a/rust/snapshots/src/lib.rs +++ b/rust/snapshots/src/lib.rs @@ -1,12 +1,12 @@ -//! Runs the Rust planner on exactly the inputs of the C# Verify snapshot tests in -//! `test/FactorioTools.Test/OilField` and compares its output with the committed -//! `*.verified.txt` files, byte for byte, with no .NET involved (#138). +//! Runs the Rust planner on exactly the inputs of the old C# Verify snapshot tests and +//! compares its output with the committed `*.verified.txt` files in `testdata/oil-field`, +//! byte for byte (#138). The C# tests were removed in stage 13b; the snapshots stayed. //! //! It lives in its own crate because it needs both the planner and the blueprint parser, //! and because the full set is slow enough to want a release build of its own. //! //! [`normalize`] and the `normalize-lists` binary keep the two blueprint lists those tests -//! read normalized, in place of the C# CLI's `oil-field normalize`. +//! read normalized. They replaced the C# CLI's `oil-field normalize`. //! //! With `UPDATE_SNAPSHOTS=1` the tests write the snapshots instead of comparing them: they //! create missing files, rewrite changed ones and delete files no case produces any more. @@ -36,9 +36,10 @@ pub fn repo_root() -> PathBuf { .expect("the repository root exists") } -/// `test/FactorioTools.Test/OilField`. +/// `testdata/oil-field`: the test blueprint lists, the Factorio oracle fixture and the +/// committed snapshots. pub fn oil_field_test_dir() -> PathBuf { - repo_root().join("test/FactorioTools.Test/OilField") + repo_root().join("testdata/oil-field") } /// Set to `1` to write the snapshots rather than compare against them. @@ -126,7 +127,7 @@ pub enum Input { /// One snapshot test case: the inputs, and the snapshot file it must match. #[derive(Clone, Debug)] pub struct SnapshotCase { - /// The snapshot file, relative to `test/FactorioTools.Test/OilField`. + /// The snapshot file, relative to `testdata/oil-field`. pub snapshot: String, pub input: Input, pub options: OilFieldOptions, @@ -247,7 +248,7 @@ pub fn all_cases(big_list_step: usize) -> Vec { cases } -/// The snapshot methods of `PlannerTest.cs` and `PlannerIssueTest.cs`. +/// The snapshot methods of the C# `PlannerTest.cs` and `PlannerIssueTest.cs`, ported. pub fn planner_test_cases() -> Vec { let mut cases = Vec::new(); diff --git a/rust/snapshots/tools/extract_issue_cases.py b/rust/snapshots/tools/extract_issue_cases.py deleted file mode 100644 index 28586d22..00000000 --- a/rust/snapshots/tools/extract_issue_cases.py +++ /dev/null @@ -1,72 +0,0 @@ -"""Extract the inline inputs of the C# snapshot tests in PlannerIssueTest.cs into Rust data. - -Usage: python3 -I extract_issue_cases.py -""" - -import re -import sys - -METHODS = [ - "CanPlanCollinearConnectedCenters", - "AllowsManyTurnsAroundAvoidLocations", - "CanPlanSinglePumpjackSurrounded", - "CanPlanTwoPumpjacksWithLotsOfAvoids", -] - -NUM = r"(-?\d+(?:\.\d+)?)f?" - - -def method_body(text: str, name: str) -> str: - start = text.index(f"public async Task {name}()") - end = text.find("public ", start + 10) - return text[start:end if end != -1 else len(text)] - - -def rust_f32(value: str) -> str: - return value if "." in value else value + ".0" - - -def main() -> None: - text = open(sys.argv[1], encoding="utf-8-sig").read() - out = [ - "//! Inputs of the snapshot tests in `test/FactorioTools.Test/OilField/PlannerIssueTest.cs`,", - "//! extracted from that file by a script so the numbers cannot drift by hand. Regenerate", - "//! rather than edit if the C# test changes.", - "", - "/// One test: its name, the options preset, whether beacons are added, the pumpjack", - "/// positions and the avoided locations.", - "pub struct IssueCase {", - " pub name: &'static str,", - " pub preset: &'static str,", - " pub add_beacons: bool,", - " pub pumpjacks: &'static [(f32, f32)],", - " pub avoid: &'static [(f32, f32)],", - "}", - "", - "pub const ISSUE_CASES: &[IssueCase] = &[", - ] - for name in METHODS: - body = method_body(text, name) - preset = re.search(r"OilFieldOptions\.(For\w+)", body).group(1) - add_beacons = "options.AddBeacons = false" not in body - assert "options.ValidateSolution = true" in body, name - pumpjacks = re.findall(r"new Position \{ X = " + NUM + r", Y = " + NUM + r" \}", body) - avoid = re.findall(r"new AvoidLocation\(" + NUM + r", " + NUM + r"\)", body) - out.append(" IssueCase {") - out.append(f' name: "{name}",') - out.append(f' preset: "{preset}",') - out.append(f" add_beacons: {'true' if add_beacons else 'false'},") - out.append(" pumpjacks: &[") - out.extend(f" ({rust_f32(x)}, {rust_f32(y)})," for x, y in pumpjacks) - out.append(" ],") - out.append(" avoid: &[") - out.extend(f" ({rust_f32(x)}, {rust_f32(y)})," for x, y in avoid) - out.append(" ],") - out.append(" },") - print(f"{name}: {preset}, beacons={add_beacons}, {len(pumpjacks)} pumpjacks, {len(avoid)} avoid") - out.append("];") - open(sys.argv[2], "w").write("\n".join(out) + "\n") - - -if __name__ == "__main__": - main() diff --git a/src/Benchmark/Benchmark.csproj b/src/Benchmark/Benchmark.csproj deleted file mode 100644 index 20cfe9d5..00000000 --- a/src/Benchmark/Benchmark.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - Exe - net10.0 - enable - enable - - - - Knapcode.FactorioTools.Benchmark - - - - - - - - - - - diff --git a/src/Benchmark/Program.cs b/src/Benchmark/Program.cs deleted file mode 100644 index b4970eeb..00000000 --- a/src/Benchmark/Program.cs +++ /dev/null @@ -1,70 +0,0 @@ -using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Running; -using Knapcode.FactorioTools.OilField; -using Knapcode.FactorioTools.Data; - -namespace Knapcode.FactorioTools.Benchmark; - -public class Program -{ - private static void Main(string[] args) - { - var summary = BenchmarkRunner.Run(); - } -} - -public class Benchmark -{ - private static string BlueprintString = "0eJyM1MFqhDAQBuB3mXMOTkzcNa9SyuK6oaRdo2gsFfHdGxMPhS34n8QYPx0m/6x0f852GJ0PZFZybe8nMm8rTe7DN899zTedJUPD3A2fTftFgsIy7Csu2I42Qc4/7A8Z3t4FWR9ccDYb6Wa5+bm72zFuEP9YQz/FF3q/fyki5UXQEi+RfbjRtvlRsYkXTQKa5qSpc60ENKWSps81hVSasPoc0wimUK0CNM5N4OKcuyA/l7vAfM5dAU5WmQO6WiPF5kYw0FYukGo17iGJuGauAjgkEqrGPSQUUmYPOHqMxEJnTwKHhTVerwQmCiPZkDlpEqkXCYc8Bp4EPCQdR73lSzricE4D2/yZ+IK+7TgdG7ZfAAAA//8DABy5/JQ="; - private Blueprint Blueprint { get; } - - public Benchmark() - { - Blueprint = ParseBlueprint.Execute(BlueprintString); - } - - [Benchmark] - public void MediumElectricPole_NoBeacon_NoUnderground() - { - var options = OilFieldOptions.ForMediumElectricPole; - options.AddBeacons = false; - options.UseUndergroundPipes = false; - Planner.Execute(options, Blueprint); - } - - [Benchmark] - public void SmallElectricPole_Beacon_Underground() - { - var options = OilFieldOptions.ForSmallElectricPole; - options.AddBeacons = true; - options.UseUndergroundPipes = true; - Planner.Execute(options, Blueprint); - } - - [Benchmark] - public void MediumElectricPole_Beacon_Underground() - { - var options = OilFieldOptions.ForMediumElectricPole; - options.AddBeacons = true; - options.UseUndergroundPipes = true; - Planner.Execute(options, Blueprint); - } - - [Benchmark] - public void BigElectricPole_Beacon_Underground() - { - var options = OilFieldOptions.ForBigElectricPole; - options.AddBeacons = true; - options.UseUndergroundPipes = true; - Planner.Execute(options, Blueprint); - } - - [Benchmark] - public void Substation_Beacon_Underground() - { - var options = OilFieldOptions.ForSubstation; - options.AddBeacons = true; - options.UseUndergroundPipes = true; - Planner.Execute(options, Blueprint); - } -} \ No newline at end of file diff --git a/src/BlazorWebApp/App.razor b/src/BlazorWebApp/App.razor deleted file mode 100644 index 6fd3ed1b..00000000 --- a/src/BlazorWebApp/App.razor +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

-
-
-
diff --git a/src/BlazorWebApp/BlazorWebApp.csproj b/src/BlazorWebApp/BlazorWebApp.csproj deleted file mode 100644 index 31422ea3..00000000 --- a/src/BlazorWebApp/BlazorWebApp.csproj +++ /dev/null @@ -1,28 +0,0 @@ - - - - net10.0 - enable - enable - true - - - - - Knapcode.FactorioTools.BlazorWebApp - Knapcode.FactorioTools.BlazorWebApp - - - - - - - - - - - - diff --git a/src/BlazorWebApp/Pages/Index.razor b/src/BlazorWebApp/Pages/Index.razor deleted file mode 100644 index a6250fd8..00000000 --- a/src/BlazorWebApp/Pages/Index.razor +++ /dev/null @@ -1,55 +0,0 @@ -@page "/" -@using System.Diagnostics; -@using Knapcode.FactorioTools.OilField; - -Index - -
-