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
170
171
172
173
174
175
mod cli;
mod collections;
mod io;
mod random;
mod time;

pub use crate::cli::CmdArgs;
pub use crate::collections::{
    contains_duplicates, retain_btreemap, retain_btreeset, wraparound_get, Counter, MultiMap, Tags,
    VecMap,
};
pub use crate::io::{
    basename, deserialize_btreemap, deserialize_multimap, deserialize_usize, file_exists,
    find_next_file, find_prev_file, from_json, list_all_objects, list_dir, load_all_objects,
    maybe_read_binary, maybe_read_json, read_binary, read_json, serialize_btreemap,
    serialize_multimap, serialize_usize, serialized_size_bytes, slurp_file, to_json, write_binary,
    write_json, FileWithProgress,
};
pub use crate::random::{fork_rng, WeightedUsizeChoice};
pub use crate::time::{
    elapsed_seconds, prettyprint_usize, start_profiler, stop_profiler, MeasureMemory, Parallelism,
    Profiler, Timer, TimerSink,
};
use std::collections::BTreeSet;
use std::fmt::Write;

const PROGRESS_FREQUENCY_SECONDS: f64 = 0.2;

pub fn clamp(x: f64, min: f64, max: f64) -> f64 {
    if x < min {
        min
    } else if x > max {
        max
    } else {
        x
    }
}

pub fn plain_list_names(names: BTreeSet<String>) -> String {
    let mut s = String::new();
    let len = names.len();
    for (idx, n) in names.into_iter().enumerate() {
        if idx != 0 {
            if idx == len - 1 {
                if len == 2 {
                    write!(s, " and ").unwrap();
                } else {
                    write!(s, ", and ").unwrap();
                }
            } else {
                write!(s, ", ").unwrap();
            }
        }
        write!(s, "{}", n).unwrap();
    }
    s
}

lazy_static::lazy_static! {
    static ref ROOT_DIR: String = {
        // If you're packaging for a release and need the data directory to be in some fixed
        // location: ABST_DATA_DIR=/some/path cargo build ...
        if let Some(dir) = option_env!("ABST_DATA_DIR") {
            dir.trim_end_matches('/').to_string()
        } else if cfg!(target_arch = "wasm32") {
            "../data".to_string()
        } else if file_exists("data/".to_string()) {
            "data".to_string()
        } else if file_exists("../data/".to_string()) {
            "../data".to_string()
        } else {
            panic!("Can't find the data/ directory");
        }
    };

    static ref ROOT_PLAYER_DIR: String = {
        // If you're packaging for a release and want the player's local data directory to be
        // $HOME/.abstreet, set ABST_PLAYER_HOME_DIR=1
        if option_env!("ABST_PLAYER_HOME_DIR").is_some() {
            match std::env::var("HOME") {
                Ok(dir) => format!("{}/.abstreet", dir.trim_end_matches('/')),
                Err(err) => panic!("This build of A/B Street stores player data in $HOME/.abstreet, but $HOME isn't set: {}", err),
            }
        } else if cfg!(target_arch = "wasm32") {
            "../data".to_string()
        } else if file_exists("data/".to_string()) {
            "data".to_string()
        } else if file_exists("../data/".to_string()) {
            "../data".to_string()
        } else {
            panic!("Can't find the data/ directory");
        }
    };
}

pub fn path<I: Into<String>>(p: I) -> String {
    let p = p.into();
    if p.starts_with("player/") {
        format!("{}/{}", *ROOT_PLAYER_DIR, p)
    } else {
        format!("{}/{}", *ROOT_DIR, p)
    }
}

// System data (Players can't edit, needed at runtime)

pub fn path_map(map_name: &str) -> String {
    path(format!("system/maps/{}.bin", map_name))
}
pub fn path_all_maps() -> String {
    path("system/maps")
}

pub fn path_prebaked_results(map_name: &str, scenario_name: &str) -> String {
    path(format!(
        "system/prebaked_results/{}/{}.bin",
        map_name, scenario_name
    ))
}

pub fn path_scenario(map_name: &str, scenario_name: &str) -> String {
    path(format!(
        "system/scenarios/{}/{}.bin",
        map_name, scenario_name
    ))
}
pub fn path_all_scenarios(map_name: &str) -> String {
    path(format!("system/scenarios/{}", map_name))
}

pub fn path_synthetic_map(map_name: &str) -> String {
    path(format!("system/synthetic_maps/{}.json", map_name))
}
pub fn path_all_synthetic_maps() -> String {
    path("system/synthetic_maps")
}

// Player data (Players edit this)

pub fn path_camera_state(map_name: &str) -> String {
    path(format!("player/camera_state/{}.json", map_name))
}

pub fn path_edits(map_name: &str, edits_name: &str) -> String {
    path(format!("player/edits/{}/{}.json", map_name, edits_name))
}
pub fn path_all_edits(map_name: &str) -> String {
    path(format!("player/edits/{}", map_name))
}

pub fn path_save(map_name: &str, edits_name: &str, run_name: &str, time: String) -> String {
    path(format!(
        "player/saves/{}/{}_{}/{}.bin",
        map_name, edits_name, run_name, time
    ))
}
pub fn path_all_saves(map_name: &str, edits_name: &str, run_name: &str) -> String {
    path(format!(
        "player/saves/{}/{}_{}",
        map_name, edits_name, run_name
    ))
}

// Input data (For developers to build maps, not needed at runtime)

pub fn path_popdat() -> String {
    path("input/seattle/popdat.bin")
}

pub fn path_raw_map(map_name: &str) -> String {
    path(format!("input/raw_maps/{}.bin", map_name))
}
pub fn path_all_raw_maps() -> String {
    path("input/raw_maps")
}