Implement crafting solver and crafting table interaction by caelunshun · Pull Request #266 · feather-rs/feather · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
15 changes: 15 additions & 0 deletions core/crafting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "feather-crafting"
version = "0.1.0"
authors = ["caelunshun <caelunshun@gmail.com>"]
edition = "2018"

[dependencies]
feather-items = { path = "../items" }

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
smallvec = "1.4"
arrayvec = { version = "0.5", features = ["serde"] }
anyhow = "1.0"
ahash = "0.3"
19 changes: 19 additions & 0 deletions core/crafting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Loading of recipe files and crafting algorithms ("solving").

use feather_items::Item;

mod model;
mod recipe;
mod solver;

pub use recipe::convert;
pub use solver::{transpose, Solver};

pub const TABLE_WIDTH: usize = 3;
pub const TABLE_SIZE: usize = TABLE_WIDTH * TABLE_WIDTH;

/// A crafting grid. Origin is at the upper left corner.
/// Stored in column-major format. Indexing by [x][y]
/// will yield the item `x` slots from the left
/// and `y` slots from the top.
pub type Grid = [[Option<Item>; TABLE_WIDTH]; TABLE_WIDTH];
47 changes: 47 additions & 0 deletions core/crafting/src/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Serde data model for recipe files.
use crate::{TABLE_SIZE, TABLE_WIDTH};
use arrayvec::{ArrayString, ArrayVec};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

pub type TableEntry = ArrayString<[u8; TABLE_WIDTH]>;
pub type Table = ArrayVec<[TableEntry; TABLE_WIDTH]>;

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Recipe<'a> {
#[serde(rename = "minecraft:crafting_shaped")]
Shaped {
pattern: Table,
#[serde(borrow)]
key: BTreeMap<char, Key<'a>>,
#[serde(rename = "result")]
output: Output<'a>,
},
#[serde(rename = "minecraft:crafting_shapeless")]
Shapeless {
ingredients: ArrayVec<[Key<'a>; TABLE_SIZE]>,
#[serde(rename = "result")]
output: Output<'a>,
},
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
#[serde(rename_all = "lowercase")]
pub enum Key<'a> {
Item(&'a str),
Tag(&'a str),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Output<'a> {
/// Identifier of output item.
pub item: &'a str,
#[serde(default = "one")]
pub count: u8,
}

const fn one() -> u8 {
1
}
150 changes: 150 additions & 0 deletions core/crafting/src/recipe.rs
Loading