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//! To play the game, depend on the `rustorio` crate instead.
7//!
8//! For more information, see the [repo](https://github.com/albertsgarde/rustorio)
9
10pub mod gamemodes;
11pub mod research;
12mod resources;
13mod tick;
14
15use std::sync::Once;
16
17pub use crate::resources::{ResourceType, bundle, resource};
18use crate::{
19    gamemodes::{GameMode, StartingResources},
20    tick::Tick,
21};
22
23static ONCE: Once = Once::new();
24
25/// Runs your play. If it is run multiple times, it will panic. This is to prevent using multiple threads to cheat.
26pub fn play<G: GameMode>(main: fn(Tick, G::StartingResources) -> (Tick, G::VictoryResources)) -> ! {
27    let mut call_once_ran = false;
28    ONCE.call_once(|| call_once_ran = true);
29    if !call_once_ran {
30        panic!("play() can only be called once per program execution to prevent cheating via multithreading.");
31    }
32    let tick = Tick::start();
33    let start_resources = G::StartingResources::init();
34    let (tick, _points) = main(tick, start_resources);
35    println!("You won in {} ticks!", tick.cur());
36    std::process::exit(0);
37}
38
39/// A trait to prevent players from implementing certain traits.
40/// Should not be reexported in mods.
41pub trait Sealed {}
42
43/// These are the items that a player should have direct access to.
44/// Should be glob reexported at the top level of mods.
45///
46/// ```rust
47/// pub use rustorio_engine::mod_reexports::*;
48/// ```
49pub mod mod_reexports {
50    pub use crate::{
51        gamemodes::GameMode,
52        play,
53        research::Technology,
54        resources::{Bundle, InsufficientResourceError, Resource},
55        tick::Tick,
56    };
57}