mindustry logic execution, map- and schematic- parsing and rendering
add example
bendn 2023-11-05
parent 36d9397 · commit be11b4e
-rw-r--r--Cargo.toml2
-rw-r--r--mindus/src/lib.rs2
-rw-r--r--p2s/.gitignore1
-rw-r--r--p2s/Cargo.toml11
-rw-r--r--p2s/README.md4
-rw-r--r--p2s/src/main.rs101
6 files changed, 119 insertions, 2 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e311b50..69ed9e4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[workspace]
-members = ["mindus", "lemu"]
+members = ["mindus", "lemu", "p2s"]
resolver = "2"
[profile.release]
diff --git a/mindus/src/lib.rs b/mindus/src/lib.rs
index 1d3a310..09bdba7 100644
--- a/mindus/src/lib.rs
+++ b/mindus/src/lib.rs
@@ -10,7 +10,7 @@
clippy::perf
)]
pub mod block;
-mod content;
+pub mod content;
pub mod data;
pub mod fluid;
pub mod item;
diff --git a/p2s/.gitignore b/p2s/.gitignore
new file mode 100644
index 0000000..b68bac7
--- /dev/null
+++ b/p2s/.gitignore
@@ -0,0 +1 @@
+quant.png
diff --git a/p2s/Cargo.toml b/p2s/Cargo.toml
new file mode 100644
index 0000000..2411584
--- /dev/null
+++ b/p2s/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "p2s"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+clipp = "0.1.0"
+comat = "0.1.3"
+exoquant = "0.2.0"
+fimg = "0.4.23"
+mindus = { version = "5.0.9", path = "../mindus", default-features = false }
diff --git a/p2s/README.md b/p2s/README.md
new file mode 100644
index 0000000..f8f2d1f
--- /dev/null
+++ b/p2s/README.md
@@ -0,0 +1,4 @@
+# p2s
+
+small example showcasing schematic construction.
+this takes a png, and turns it into a sorter schematic. \ No newline at end of file
diff --git a/p2s/src/main.rs b/p2s/src/main.rs
new file mode 100644
index 0000000..7f894eb
--- /dev/null
+++ b/p2s/src/main.rs
@@ -0,0 +1,101 @@
+use exoquant::{ditherer, Color, Remapper, SimpleColorSpace};
+use fimg::{scale::Nearest, DynImage, Image};
+use mindus::{
+ block::{Rotation, SORTER},
+ data::dynamic::DynData,
+ item::Type::*,
+ Schematic,
+};
+use std::process::ExitCode;
+macro_rules! fail {
+ () => {
+ fail!("<in> (<width>x<height>)")
+ };
+ ($usage:literal) => {{
+ eprintln!(concat!("usage: p2s ", comat::comat!($usage)));
+ return ExitCode::FAILURE;
+ }};
+}
+const ITEMS: [mindus::item::Type; 22] = [
+ Copper,
+ Lead,
+ Metaglass,
+ Graphite,
+ Sand,
+ Coal,
+ Titanium,
+ Thorium,
+ Scrap,
+ Silicon,
+ Plastanium,
+ PhaseFabric,
+ SurgeAlloy,
+ SporePod,
+ BlastCompound,
+ Pyratite,
+ Beryllium,
+ Tungsten,
+ Oxide,
+ Carbide,
+ FissileMatter,
+ DormantCyst,
+];
+
+fn main() -> ExitCode {
+ let mut args = std::env::args().skip(1);
+ let Some(input) = args.next() else {
+ fail!("{bold_red}<input_file>{reset}");
+ };
+ let mut img = DynImage::open(input).to_rgb();
+ let palette = ITEMS
+ .map(|i| i.color())
+ .map(|(r, g, b)| Color::new(r, g, b, 255));
+
+ if let Some(size) = args.next() {
+ let Some((w, h)) = size.split_once('x') else {
+ fail!(".. <w>{bold_red}x{reset}<h>")
+ };
+ let Ok(w) = w.parse() else {
+ fail!(".. <width: valid number>x<h>")
+ };
+ let Ok(h) = h.parse() else {
+ fail!(".. <w>x<h: valid number>")
+ };
+ img = img.scale::<Nearest>(w, h);
+ }
+
+ let quant = Remapper::new(&palette, &SimpleColorSpace::default(), &ditherer::None).remap(
+ &img.chunked()
+ .map(|&[r, g, b]| Color::new(r, g, b, 255))
+ .collect::<Vec<_>>(),
+ img.width() as usize,
+ );
+ Image::<Box<[u8]>, 3>::build(img.width(), img.height())
+ .buf(
+ quant
+ .iter()
+ .map(|&i| ITEMS[i as usize].color())
+ .flat_map(|(r, g, b)| [r, g, b])
+ .collect(),
+ )
+ .scale::<Nearest>(img.width() * 4, img.height() * 4)
+ .save("quant.png");
+ let mut s = Schematic::new(img.width() as usize, img.height() as usize);
+ for x in 0..img.width() as usize {
+ for y in 0..img.height() as usize {
+ s.set(
+ x as usize,
+ y as usize,
+ &SORTER,
+ DynData::Content(
+ mindus::content::Type::Item,
+ quant[(img.height() as usize - y - 1) * img.width() as usize + x] as u16,
+ ),
+ Rotation::Up,
+ )
+ .unwrap();
+ }
+ }
+ clipp::copy(s.serialize_base64().unwrap());
+ ExitCode::SUCCESS
+}