craftplan

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 10 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 craftplan computes what an agent can craft right now and what is missing to craft a target recipe. The Engine consumes a Source (live game client + crafting DB in production; fakes in tests) and returns plain-data results that callers render however they like.

Index

Constants

This section is empty.

Variables

View Source
var ErrBOMUnavailable = errors.New("BOM unavailable")

ErrBOMUnavailable wraps any error returned by Source.BOM when --reachable is requested. Callers (REPL) should check via errors.Is and surface the "install/update the crafting DB" hint.

Functions

func FormatCraftableCompact

func FormatCraftableCompact(rows []CraftableRow, opts FormatCraftableOpts) string

FormatCraftableCompact renders the compact table view of craftable rows. Matches the layout in the design spec.

func FormatCraftableDetail

func FormatCraftableDetail(rows []CraftableRow, opts FormatCraftableOpts) string

FormatCraftableDetail renders each row as its own per-recipe block instead of one table. Use when the operator wants depth over breadth.

func FormatPlan

func FormatPlan(res *PlanResult) string

FormatPlan renders a PlanResult to a string matching the design spec.

Types

type BOMRow

type BOMRow struct {
	BaseItemID string
	Quantity   int
	RecipePath []string
}

BOMRow is one base-material row from the crafting DB's bill_of_materials table for a single target recipe. RecipePath lists the recipe_ids that connect the target down to this base material (ordered output → leaf; length 1 means the target recipe itself consumes the base directly).

type CraftableOpts

type CraftableOpts struct {
	Reachable           bool
	IncludeFaction      bool
	IncludeFacilityOnly bool   // include recipes flagged facility_only (default: hide; their ∞ can_make floods the top)
	IncludeShipPassive  bool   // include "Ship Passive" recipes (default: hide; they run automatically on capable ships and can't be crafted manually)
	IncludeHidden       bool   // include recipes flagged hidden (default: hide)
	CategoryFilter      string // substring match, case-insensitive; empty = no filter
	SearchFilter        string // substring match against name + output item_ids, case-insensitive
	Refresh             bool   // bypass session recipe-catalog cache
	Max                 int    // hard cap on rows returned; 0 = engine default (100)
	OneRecipe           string // if non-empty, return only this recipe (still in CraftableRow form); used by --detail --recipe X
	SortBy              SortMode
}

CraftableOpts controls Engine.Craftable.

type CraftableRow

type CraftableRow struct {
	Recipe         serverapi.Recipe // pulled through so the renderer can reach inputs/outputs without re-lookup
	CanMake        int              // integer batches buildable from the current inventory; math.MaxInt for ∞
	OutputItemID   string           // primary output item_id (recipe.Outputs[0])
	OutputQuantity int              // primary output quantity per batch
	Depth          int              // 1 = direct, 2+ = via N-1 intermediate crafts (only meaningful in --reachable)
}

CraftableRow is one row in the craftable table.

type Engine

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

Engine computes craftable rows and plan results from a Source.

func New

func New(src Source) *Engine

New constructs an Engine. The Source must be non-nil; methods will panic otherwise.

func (*Engine) Craftable

func (e *Engine) Craftable(ctx context.Context, opts CraftableOpts) ([]CraftableRow, int, error)

Craftable returns the list of recipes the agent can build right now (or, with opts.Reachable, can build via intermediate crafts). Sort: can_make DESC, depth ASC, recipe_id ASC. The second return value is the pre-truncation total so callers can render "showing N / TOTAL" honestly.

func (*Engine) Plan

func (e *Engine) Plan(ctx context.Context, opts PlanOpts) (*PlanResult, error)

Plan computes the gap between the agent's current inventory and the inputs required to craft opts.ID at opts.Quantity. Reachable mode (in reachable.go) re-uses these gates and replaces the input walk with a BOM walk.

type FormatCraftableOpts

type FormatCraftableOpts struct {
	StationID string
	Reachable bool
	// Total is the pre-truncation count of matching recipes. If > len(rows)
	// the footer prints "showing N / TOTAL" so the operator knows to widen
	// --max. If 0, treated as equal to len(rows).
	Total int
	// SortBy is surfaced in the footer so the operator can see which sort
	// produced the row order (the engine applies the sort; the formatter
	// just labels it).
	SortBy SortMode
}

FormatCraftableOpts controls FormatCraftableCompact rendering.

type IntermediateCraft

type IntermediateCraft struct {
	RecipeID       string
	OutputItemID   string
	OutputQuantity int
	BatchesNeeded  int
}

IntermediateCraft describes one of the sub-crafts the operator would have to run to reach the target via --reachable mode.

type Inventory

type Inventory struct {
	Cargo   map[string]int // item_id → quantity in ship cargo
	Storage map[string]int // item_id → quantity in personal storage at current station
	Faction map[string]int // item_id → quantity in faction storage at current station (may be nil)
}

Inventory is the count of items the agent has access to, split by where they live. Plan/Craftable sum the buckets the user opted into; the split is preserved so the per-recipe display can show which bucket a material is currently sitting in.

type PlanInputRow

type PlanInputRow struct {
	ItemID      string
	Need        int
	HaveCargo   int
	HaveStorage int
	HaveFaction int
	Short       int // max(0, Need - (HaveCargo + HaveStorage [+ HaveFaction]))
}

PlanInputRow is one row in the plan output. For --reachable mode each row represents a base material; for direct mode each row is a direct ingredient.

type PlanOpts

type PlanOpts struct {
	ID             string // recipe_id OR item_id; recipe wins on tie
	Quantity       int    // number of OUTPUT UNITS to plan for; must be ≥ 1 (converted to runs via the recipe's output-per-run)
	Reachable      bool
	IncludeFaction bool
	Refresh        bool
}

PlanOpts controls Engine.Plan.

type PlanResult

type PlanResult struct {
	Recipe         serverapi.Recipe
	Quantity       int // requested output units (what the user asked for)
	Runs           int // crafting runs needed = ceil(Quantity / output-units-per-run)
	StationID      string
	Inputs         []PlanInputRow
	Intermediates  []IntermediateCraft // populated only in --reachable mode
	BlockedSkill   map[string]int      // skill_id → level shortfall; empty if no skill block
	BlockedIllegal bool                // true if recipe is illegal at this station
	BlockedPassive bool                // true if recipe is Ship Passive (runs automatically, can't be crafted manually)
	// FacilityOnlyNoAlt is true when the chosen recipe is facility_only AND no
	// other recipe producing the same primary output can be hand-crafted (every
	// recipe for that output is facility_only) — i.e. this item cannot be built
	// at a Station Workshop at all; a facility is required.
	FacilityOnlyNoAlt bool
	Ready             bool
}

PlanResult is the full output of Engine.Plan. Ready==true means every Inputs row has Short==0 and the agent meets the recipe's skill + legality gates.

type SortMode

type SortMode int

SortMode controls Craftable row ordering. Unknown / zero value = default (can_make desc, then depth asc, then recipe_id asc).

const (
	SortDefault    SortMode = iota // can_make desc, depth asc, recipe_id asc
	SortCanMakeAsc                 // can_make asc (lowest first — what's running out)
	SortName                       // recipe.Name asc, then can_make desc
	SortCategory                   // recipe.Category asc, then can_make desc
	SortRecipeID                   // recipe.ID asc
)

func ParseSortMode

func ParseSortMode(s string) SortMode

ParseSortMode maps a user-supplied --sort value to a SortMode. Empty or unknown values map to SortDefault.

func (SortMode) String

func (s SortMode) String() string

String renders a SortMode for the footer.

type Source

type Source interface {
	// Recipes returns the server's authoritative catalog. The implementation
	// is allowed to cache; refresh=true forces a re-fetch.
	Recipes(ctx context.Context, refresh bool) (map[string]serverapi.Recipe, error)

	// Inventory returns the agent's current cargo + station storage. If
	// includeFaction is true the Faction map is populated; otherwise the
	// implementation MAY leave it nil (engine doesn't read it in that case).
	Inventory(ctx context.Context, includeFaction bool) (Inventory, error)

	// Skills returns the agent's current skill levels keyed by skill_id.
	// Missing skills are treated as level 0 by callers.
	Skills(ctx context.Context) (map[string]int, error)

	// CurrentStationID returns the station_id the agent is docked at, used
	// for the illegal-recipes legality gate and the output header. Returns
	// "" if not docked.
	CurrentStationID(ctx context.Context) (string, error)

	// IllegalAt returns the set of recipe_ids that are illegal to craft at
	// the given stationID. Missing crafting DB returns an empty map and
	// nil error (no recipes are illegal if we can't tell — server enforces
	// at craft time anyway).
	IllegalAt(ctx context.Context, stationID string) (map[string]bool, error)

	// BOM returns flattened bill-of-materials rows for the given recipe IDs,
	// keyed by recipe_id. Only target_type='item' rows are returned. Missing
	// crafting DB returns nil + a wrapped error; the engine surfaces this
	// as "BOM unavailable" when --reachable is requested.
	BOM(ctx context.Context, recipeIDs []string) (map[string][]BOMRow, error)
}

Source provides every fact the Engine needs to plan crafts. The real implementation in cmd/tools/play_as wraps game.GameClient + *sql.DB; the tests in this package use fakeSource so the engine can be exercised without spinning up a client or opening a database.

Methods take a context so a slow view_storage call doesn't strand the REPL; the engine passes the same context through to every Source call for a single Craftable/Plan invocation.

Jump to

Keyboard shortcuts

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