Skip to main content

factorio_api/
prototypes.rs

1//! Hand-written prototype stubs for the data stage (`data.extend`).
2//!
3//! These are not generated from `prototype-api.json` yet. They exist so mods can
4//! register common prototypes with typed struct literals; the codegen injects
5//! Factorio's `type = "..."` discriminant from the Rust struct name.
6
7/// Minimal [`ItemPrototype`](https://lua-api.factorio.com/latest/prototypes/ItemPrototype.html)
8/// for `data.extend`.
9///
10/// Required Factorio fields: `name`, `icon` (or `icons`), `stack_size`.
11/// `type = "item"` is injected by the Lua generator.
12///
13/// Optional fields omit as Lua `nil` when `None`. Prefer
14/// `..Default::default()` for fields you do not set (same sparse-table pattern
15/// as other API structs).
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
17pub struct Item {
18    /// Internal prototype name (e.g. `"my-mod-widget"`).
19    pub name: &'static str,
20    /// Packaged icon path (e.g. `"__my_mod__/graphics/icon.png"`).
21    pub icon: &'static str,
22    /// Max items per inventory slot.
23    pub stack_size: i64,
24    /// Icon pixel size. Factorio defaults to `64` when omitted.
25    pub icon_size: Option<i64>,
26    /// Item subgroup id (e.g. `"intermediate-product"`).
27    pub subgroup: Option<&'static str>,
28    /// Sort order within the subgroup.
29    pub order: Option<&'static str>,
30}
31
32/// Item ingredient for a [`Recipe`] (`type = "item"` injected).
33///
34/// Factorio 2.0 requires the full `{type, name, amount}` table form.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
36pub struct RecipeIngredient {
37    /// Ingredient item prototype name.
38    pub name: &'static str,
39    /// Item count consumed.
40    pub amount: i64,
41}
42
43/// Item product for a [`Recipe`] (`type = "item"` injected).
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
45pub struct RecipeProduct {
46    /// Result item prototype name.
47    pub name: &'static str,
48    /// Item count produced.
49    pub amount: i64,
50}
51
52/// Minimal [`RecipePrototype`](https://lua-api.factorio.com/latest/prototypes/RecipePrototype.html)
53/// for `data.extend`.
54///
55/// `type = "recipe"` is injected by the Lua generator. Ingredients and results
56/// use [`RecipeIngredient`] / [`RecipeProduct`] (each injects `type = "item"`).
57#[derive(Debug, Clone, Copy, PartialEq, Default)]
58pub struct Recipe {
59    /// Internal prototype name (e.g. `"my-mod-widget"`).
60    pub name: &'static str,
61    /// Crafting ingredients.
62    pub ingredients: &'static [RecipeIngredient],
63    /// Crafting products.
64    pub results: &'static [RecipeProduct],
65    /// Crafting energy in seconds. Factorio defaults to `0.5` when omitted.
66    pub energy_required: Option<f64>,
67    /// Recipe category (e.g. `"crafting"`).
68    pub category: Option<&'static str>,
69    /// Whether the recipe is unlocked at start. Defaults to `true` in Factorio when omitted.
70    pub enabled: Option<bool>,
71    /// Item subgroup id.
72    pub subgroup: Option<&'static str>,
73    /// Sort order within the subgroup.
74    pub order: Option<&'static str>,
75}
76
77/// Science-pack entry for a [`TechnologyUnit`].
78///
79/// Emitted as a Factorio research ingredient tuple `{ "pack-name", amount }`.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
81pub struct TechnologyUnitIngredient {
82    /// Tool / science-pack item name (e.g. `"automation-science-pack"`).
83    pub name: &'static str,
84    /// Count consumed per research unit.
85    pub amount: i64,
86}
87
88/// Research cost block for a [`Technology`] (`unit = { count, time, ingredients }`).
89#[derive(Debug, Clone, Copy, PartialEq, Default)]
90pub struct TechnologyUnit {
91    /// How many lab cycles are required.
92    pub count: i64,
93    /// Seconds per cycle.
94    pub time: f64,
95    /// Science packs per cycle.
96    pub ingredients: &'static [TechnologyUnitIngredient],
97}
98
99/// Unlock-recipe modifier (`type = "unlock-recipe"` injected).
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
101pub struct UnlockRecipeEffect {
102    /// Recipe prototype name unlocked when the technology is researched.
103    pub recipe: &'static str,
104}
105
106/// Minimal [`TechnologyPrototype`](https://lua-api.factorio.com/latest/prototypes/TechnologyPrototype.html)
107/// for `data.extend`.
108///
109/// `type = "technology"` is injected by the Lua generator.
110#[derive(Debug, Clone, Copy, PartialEq, Default)]
111pub struct Technology {
112    /// Internal prototype name (e.g. `"my-mod-widget"`).
113    pub name: &'static str,
114    /// Packaged tech icon path (e.g. `"__my_mod__/graphics/technology.png"`).
115    pub icon: &'static str,
116    /// Icon pixel size. Technology icons are often `256`.
117    pub icon_size: Option<i64>,
118    /// Prerequisite technology ids.
119    pub prerequisites: &'static [&'static str],
120    /// Effects applied on research (typically unlock-recipe).
121    pub effects: &'static [UnlockRecipeEffect],
122    /// Lab cost.
123    pub unit: TechnologyUnit,
124    /// Sort order string.
125    pub order: Option<&'static str>,
126}