rustorio_engine/
lib.rs

1#![feature(generic_const_exprs)]
2#![allow(incomplete_features)] // silence the “still incomplete” lint
3#![warn(missing_docs)]
4//! The core engine for Rustorio.
5//! Only relevant if you are writing a mod for Rustorio.
6//! A save file depending on this crate has access to some APIs that make it trivial to cheat, which can be great for testing and debugging, but removes the challenge.
7//! To play the game, depend on the [`rustorio`](https://crates.io/crates/rustorio) crate instead.
8//!
9//! For more information, see the [repo](https://github.com/albertsgarde/rustorio).
10
11pub mod gamemodes;
12pub mod machine;
13pub mod recipe;
14pub mod research;
15pub mod resources;
16mod tick;
17
18use std::sync::Once;
19
20pub use crate::resources::{ResourceType, bundle, resource};
21use crate::{
22    gamemodes::{GameMode, StartingResources},
23    tick::Tick,
24};
25
26static ONCE: Once = Once::new();
27
28/// Runs your play. If it is run multiple times, it will panic. This is to prevent using multiple threads to cheat.
29pub fn play<G: GameMode>(main: fn(Tick, G::StartingResources) -> (Tick, G::VictoryResources)) -> ! {
30    let mut call_once_ran = false;
31    ONCE.call_once(|| call_once_ran = true);
32    if !call_once_ran {
33        panic!(
34            "play() can only be called once per program execution to prevent cheating via multithreading."
35        );
36    }
37    let tick = Tick::start();
38    let start_resources = G::StartingResources::init(&tick);
39    let (tick, _points) = main(tick, start_resources);
40    println!("You won in {} ticks!", tick.cur());
41    std::process::exit(0);
42}
43
44/// A trait to prevent players from implementing certain traits.
45/// Should not be reexported in mods.
46pub trait Sealed {}
47
48/// These are the items that a player should have direct access to.
49/// Should be glob reexported at the top level of mods.
50///
51/// ```rust
52/// pub use rustorio_engine::mod_reexports::*;
53/// ```
54pub mod mod_reexports {
55    pub use crate::{
56        gamemodes::GameMode,
57        play,
58        recipe::{HandRecipe, Recipe},
59        research::{ResearchPoint, Technology},
60        resources::{Bundle, InsufficientResourceError, Resource, ResourceType},
61        tick::Tick,
62    };
63}