1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//! Game logic for Taurus.

// TODO: Get to the point where we can uncomment this.
// #![deny(missing_docs)]

// TODO: Get to the point where we can remove this without it causing an avalanche of warnings.
#![allow(dead_code, clippy::doc_markdown, unused_imports, unused_variables)]
// Flamer
#![cfg_attr(feature = "dev", feature(plugin, custom_attribute))]
#![cfg_attr(feature = "dev", plugin(flamer))]

#[cfg(feature = "dev")]
extern crate flame;

// Required dependencies
#[macro_use]
extern crate failure;
#[macro_use]
extern crate lazy_static;
extern crate num;
extern crate num_traits;
extern crate over;
extern crate rand;
extern crate tcod;

// These modules are public so that they can be used in external integration tests.

#[macro_use]
pub mod util;

pub mod actor;
pub mod console;
pub mod coord;
pub mod database;
pub mod defs;
pub mod dungeon;
pub mod error;
pub mod game_data;
pub mod generate;
pub mod item;
pub mod material;
pub mod name_gen;
pub mod object;
pub mod player;
pub mod tile;
pub mod ui;

mod constants;
#[cfg(test)]
mod tests;

use crate::console::DrawConsole;
use crate::database::{load_data, Database};
use crate::dungeon::{Dungeon, DungeonList};
use crate::error::GameError;
use crate::game_data::{GameData, GameLoopOutcome};
use std::sync::{Arc, Mutex, RwLock};

/// A generic result type used throughout the game.
pub type GameResult<T> = Result<T, failure::Error>;

lazy_static! {
    /// Global database reference;
    static ref DATABASE: RwLock<Database> = {
        RwLock::new(handle_error(dev_time!(load_data(), "Loading database...")))
    };

    /// Global game data struct.
    static ref GAMEDATA: RwLock<GameData> = {
        RwLock::new(handle_error(GameData::new()))
    };

    /// Drawing console.
    pub static ref CONSOLE: Mutex<DrawConsole> = {
        let game_data = GAMEDATA.read().unwrap();
        Mutex::new(dev_time!(DrawConsole::new(&game_data.console_settings),
                             "Initializing draw console..."))
    };
}

/// Runs the main game loop.
pub fn run_game() -> GameResult<()> {
    // Load database.
    lazy_static::initialize(&DATABASE);

    // Load global game data.
    lazy_static::initialize(&GAMEDATA);

    // Initialize the console.
    lazy_static::initialize(&CONSOLE);

    // Display random names. TODO: remove this
    #[cfg(feature = "dev")]
    for race in &["human", "elf", "dwarf", "dragon"] {
        let name_profile = DATABASE
            .read()
            .unwrap()
            .get_obj("name_profiles")?
            .get_obj(race)?;

        println!("{} names:", util::string::capitalize(race));
        for _ in 1..10 {
            println!("{}", name_gen::name_gen(&name_profile)?);
        }
        println!();
    }

    // Initialize a brand new game.
    let mut dungeon_list = init_new_game()?;

    loop {
        // Get the current dungeon from the list.
        let dungeon = dungeon_list.current_dungeon();
        debug_assert!(dungeon.num_actors() > 0);

        // Main game loop.
        match dungeon.run_loop() {
            GameLoopOutcome::DepthChanged => {
                unimplemented!(); // TODO
            }
            GameLoopOutcome::WindowClosed => {
                println!("\nWindow closed. Goodbye!");
                break;
            }
            GameLoopOutcome::PlayerDead => {
                unimplemented!(); // TODO
            }
            GameLoopOutcome::QuitGame => {
                println!("\nQuitting. Goodbye!");
                break;
            }
            GameLoopOutcome::NoActors => {
                unreachable!();
            }
            GameLoopOutcome::None => {
                unimplemented!(); // TODO
            }
        }
    }

    Ok(())
}

fn init_new_game() -> GameResult<DungeonList> {
    // Generate game.
    let dungeon_list = dev_time!(DungeonList::new()?, "Generating game world...");

    Ok(dungeon_list)
}

/// Process error if one is contained in `result`.
pub fn handle_error<T>(result: Result<T, failure::Error>) -> T {
    if let Err(error) = result {
        // Handle errors.
        // Just display them for now.
        println!("------");
        println!("Error:");
        let mut i = 1;
        for cause in error.causes() {
            println!("{}{}", "  ".repeat(i), cause);
            i += 1;
        }
        println!("------");

        ::std::process::exit(1);
    }

    result.unwrap()
}