craftbrain

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package craftbrain plans the full recursive work needed to build a target (item, ship, or facility): what to mine, buy, haul, and craft, and where.

The Engine reads every fact through a Source, so the arithmetic is testable without a database or a game connection. It is read-only: nothing here writes to the KB or talks to the server.

Index

Constants

View Source
const DefaultCraftBase = "any_docked_station"

DefaultCraftBase is where hand-crafts are sited. Any docked station will do, so the plan names the target's own hub rather than inventing a location. It is not a real base id: a Source that can pick a concrete stand-in (e.g. the operator's current system) should resolve it in SystemOf, otherwise every haul into a hand-craft site degrades to StatusUnknownRoute.

Variables

This section is empty.

Functions

func Format

func Format(p *Plan) string

Format renders a Plan for human review. The JSON DAG is the contract for Executor B; this is for the operator who has to approve it.

Types

type Coverage

type Coverage struct {
	Stations            int `json:"stations"`
	FacilityOnlyCovered int `json:"facility_only_covered"`
	FacilityOnlyTotal   int `json:"facility_only_total"`
}

Coverage reports how much of the facility catalog the plan could see, so a BLOCKED node reads as "unknown" rather than "impossible".

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine plans crafts from a Source.

func New

func New(src Source) *Engine

New constructs an Engine. src must be non-nil.

func (*Engine) Plan

func (e *Engine) Plan(ctx context.Context, target string, qty int, opts Options) (*Plan, error)

Plan computes the full recursive work to build qty of target.

Items are visited in topological order (consumers before producers), so an item's demand is final before its runs are rounded. This is why the expander is not a tree walk: a shared intermediate needed 4+4+4 must round once at 12, not three times at 4.

type Facility

type Facility struct {
	StationID       string
	FacilityID      string
	Level           int
	RentalFeePerRun int
	TicksPerRun     float64
	OutputPerRun    int
	BacklogTicks    float64
	LastSeenUTC     string
}

Facility is one public production line, as sited by the planner.

TicksPerRun is read per-instance and never derived: measured across the live catalog, crafting_time / ticks_per_run / 3^(level-1) takes at least five distinct values, so there is no formula.

func (*Facility) ParseProduction

func (f *Facility) ParseProduction(detailsJSON string) error

ParseProduction fills the production fields from a captured details_json payload. An empty or unparseable payload leaves zero values, except OutputPerRun which defaults to 1 so run arithmetic never divides by zero.

type Holding

type Holding struct {
	Holder     string // agent_id, or "" for faction storage
	BaseID     string
	Qty        int
	CapturedAt time.Time
}

Holding is stock the fleet already has, attributed to whoever holds it. Executor B needs Holder+BaseID to tell an agent what to withdraw and where.

type Kind

type Kind string

Kind is the action a plan Node represents.

const (
	KindMine    Kind = "mine"
	KindBuy     Kind = "buy"
	KindHaul    Kind = "haul"
	KindCraft   Kind = "craft"
	KindBlocked Kind = "blocked"
)

type MineOrBuy

type MineOrBuy struct {
	Mineable    bool    `json:"mineable"`
	MarketPrice float64 `json:"market_price,omitempty"`
	MarketQty   float64 `json:"market_qty,omitempty"`
	BestStation string  `json:"best_station,omitempty"`
}

MineOrBuy carries both options for a raw leaf. The operator decides at review; the planner never chooses.

type Node

type Node struct {
	ID         string   `json:"id"`
	Kind       Kind     `json:"kind"`
	ItemID     string   `json:"item_id"`
	Qty        int      `json:"qty"`
	Runs       int      `json:"runs,omitempty"`
	RecipeID   string   `json:"recipe_id,omitempty"`
	StationID  string   `json:"station_id,omitempty"`
	FacilityID string   `json:"facility_id,omitempty"`
	FeeTotal   int      `json:"fee_total,omitempty"`
	TicksEst   float64  `json:"ticks_est,omitempty"`
	Holder     string   `json:"holder,omitempty"`
	FromBase   string   `json:"from_base,omitempty"` // haul source
	ToBase     string   `json:"to_base,omitempty"`   // haul destination
	Jumps      int      `json:"jumps,omitempty"`
	Status     Status   `json:"status"`
	Reason     string   `json:"reason,omitempty"` // why blocked/slow/stale
	DependsOn  []string `json:"depends_on,omitempty"`
}

Node is one step in the plan DAG.

type Options

type Options struct {
	// MaxHandTicks is the running plan-time budget above which a craft
	// prefers a facility over hand-crafting. A tick is ~10s, so the default
	// 360 is about an hour.
	MaxHandTicks float64
	// MaxStockAge marks a holding stale (still subtracted, but flagged).
	MaxStockAge time.Duration
	// Now is injectable so tests are deterministic. Zero means time.Now().
	Now time.Time
}

Options tunes the planner. Zero value is not valid; use DefaultOptions.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the documented defaults.

type Plan

type Plan struct {
	Target         string               `json:"target"`
	Quantity       int                  `json:"quantity"`
	Nodes          []Node               `json:"nodes"`
	Leaves         map[string]MineOrBuy `json:"leaves,omitempty"`
	Surplus        map[string]int       `json:"surplus,omitempty"`
	Diagnostics    []string             `json:"diagnostics,omitempty"`
	TotalFee       int                  `json:"total_fee"`
	TotalTicks     float64              `json:"total_ticks"`
	TotalHaulJumps int                  `json:"total_haul_jumps"`
	Coverage       Coverage             `json:"coverage"`
}

Plan is the full step-DAG plus the honesty footers.

type Source

type Source interface {
	// Recipes returns the whole recipe graph keyed by recipe_id, with Inputs
	// and Outputs hydrated.
	Recipes(ctx context.Context) (map[string]knowledge.RecipeDef, error)

	// Facilities returns known public production sites for recipeID, with
	// production details already parsed. Empty means none are known — which
	// signifies "unknown", not "impossible".
	Facilities(ctx context.Context, recipeID string) ([]Facility, error)

	// OnHand returns stock of itemID the fleet holds, attributed by holder.
	OnHand(ctx context.Context, itemID string) ([]Holding, error)

	// Buyable returns market sellers of itemID with at least qty depth.
	Buyable(ctx context.Context, itemID string, qty int) ([]finditem.Result, error)

	// SystemOf resolves a station_id (a base_id) to its system_id. Returns
	// "" when unknown.
	SystemOf(ctx context.Context, stationID string) (string, error)

	// Jumps returns hop distance from fromSystem to each of toSystems.
	Jumps(ctx context.Context, fromSystem string, toSystems []string) (map[string]int, error)

	// Coverage reports catalog breadth for the plan footer.
	Coverage(ctx context.Context) (Coverage, error)
}

Source provides every fact the Engine needs. The real implementation lives in cmd/tools/play_as and wraps the knowledge DB + market collector; tests use an in-memory fake.

Every method is read-only. The Engine never mutates game or KB state.

type Status

type Status string

Status annotates a Node whose assumptions are weaker than they look.

const (
	// StatusOK is a node with fresh data and a resolved site.
	StatusOK Status = "ok"
	// StatusBlocked is facility_only with no known facility and no market seller.
	StatusBlocked Status = "blocked"
	// StatusStale leaned on a holding or facility older than Options.MaxStockAge.
	StatusStale Status = "stale"
	// StatusSlow blew the MaxHandTicks budget but had no facility to escape to.
	StatusSlow Status = "slow"
	// StatusUnknownRoute is a haul node drawn from a base whose system could
	// not be resolved (Source.SystemOf returned ""), so Source.Jumps came back
	// navigation.RouteInf. The stock is still real and still drawn — only the
	// distance is unknown — so this is a distinct status rather than
	// StatusBlocked (which means no supply was found at all) or StatusStale
	// (which means the data is old, not that the route is unmeasurable).
	StatusUnknownRoute Status = "unknown_route"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL