outfly/src/var.rs

854 lines
30 KiB
Rust
Raw Normal View History

2024-04-21 16:23:40 +00:00
// ▄████████▄ + ███ + ▄█████████ ███ +
// ███▀ ▀███ + + ███ ███▀ + ███ + +
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
2024-04-21 17:34:00 +00:00
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
2024-04-21 16:23:40 +00:00
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
// + + + ███
// + ▀████████████████████████████████████████████████████▀
2024-04-23 15:33:36 +00:00
//
// This module manages variables, settings, as well as evaluating
// "if"-conditions in chats.
2024-04-21 16:23:40 +00:00
2024-05-12 23:42:14 +00:00
use crate::prelude::*;
2024-03-16 21:20:23 +00:00
use bevy::prelude::*;
2024-05-22 02:57:20 +00:00
use bevy::window::WindowMode;
2024-06-13 01:26:19 +00:00
use serde::{Deserialize, Serialize};
2024-05-22 02:57:20 +00:00
use std::collections::{HashMap, HashSet};
2024-04-30 20:26:54 +00:00
use std::env;
use std::fs;
2024-05-22 02:57:20 +00:00
use toml_edit::DocumentMut;
2024-03-16 21:20:23 +00:00
2024-04-14 14:20:51 +00:00
pub const SCOPE_SEPARATOR: &str = "$";
pub const TOKEN_EQUALS: &str = "==";
pub const TOKEN_EQUALS_NOT: &str = "!=";
pub const TOKEN_GREATER_THAN: &str = ">";
pub const TOKEN_LESS_THAN: &str = "<";
pub const TOKEN_GREATER_EQUALS: &str = ">=";
pub const TOKEN_LESS_EQUALS: &str = "<=";
pub const TOKEN_NEGATE: &str = "~";
2024-04-12 23:34:18 +00:00
pub const DEFAULT_CHAT_SPEED: f32 = 10.0;
pub const DEFAULT_CONFIG_TOML: &str = include_str!("data/outfly.toml");
2024-04-12 23:34:18 +00:00
2024-03-17 17:26:44 +00:00
#[derive(Resource)]
2024-03-16 21:20:23 +00:00
pub struct Settings {
2024-03-30 20:14:04 +00:00
pub dev_mode: bool,
2024-04-07 23:44:36 +00:00
pub god_mode: bool,
2024-04-18 01:56:32 +00:00
pub version: String,
2024-05-13 02:47:47 +00:00
pub alive: bool,
pub window_focused: bool,
2024-03-16 21:20:23 +00:00
pub mute_sfx: bool,
2024-06-13 02:06:15 +00:00
pub noise_cancellation_mode: usize,
2024-10-03 21:59:15 +00:00
pub noise_cancellation_modes: Vec<(String, f32)>,
2024-06-13 01:26:19 +00:00
pub radio_mode: usize,
2024-06-13 01:41:15 +00:00
pub radio_modes: Vec<String>, // see also: settings.is_radio_playing()
2024-10-03 21:59:15 +00:00
pub volume_sfx: f32,
pub volume_music: f32,
2024-03-30 15:27:56 +00:00
pub mouse_sensitivity: f32,
2024-04-08 01:15:45 +00:00
pub fov: f32,
pub fov_highspeed: f32,
pub zoom_fov: f32,
2024-04-05 21:38:20 +00:00
pub zoom_sensitivity_factor: f32,
2024-03-17 19:31:16 +00:00
pub font_size_hud: f32,
pub font_size_fps: f32,
2024-03-17 19:31:16 +00:00
pub font_size_conversations: f32,
2024-04-15 18:58:19 +00:00
pub font_size_choices: f32,
pub font_size_console: f32,
2024-04-28 04:29:01 +00:00
pub font_size_speedometer: f32,
2024-05-12 20:17:17 +00:00
pub font_size_deathtext: f32,
pub font_size_deathsubtext: f32,
2024-05-12 23:42:22 +00:00
pub font_size_deathpoem: f32,
2024-05-14 04:28:14 +00:00
pub font_size_death_achievements: f32,
2024-05-14 03:17:32 +00:00
pub font_size_achievement: f32,
pub font_size_achievement_header: f32,
pub font_size_keybindings: f32,
2024-05-14 17:08:18 +00:00
pub font_size_version: f32,
2024-04-15 18:58:19 +00:00
pub hud_color: Color,
pub hud_color_fps: Color,
2024-04-15 18:58:19 +00:00
pub hud_color_console: Color,
pub hud_color_console_warn: Color,
pub hud_color_console_system: Color,
pub hud_color_console_achievement: Color,
2024-04-15 18:58:19 +00:00
pub hud_color_alert: Color,
pub hud_color_subtitles: Color,
pub hud_color_choices: Color,
2024-04-28 04:29:01 +00:00
pub hud_color_speedometer: Color,
2024-05-12 23:42:22 +00:00
pub hud_color_deathpoem: Color,
2024-05-14 03:17:32 +00:00
pub hud_color_achievement: Color,
pub hud_color_achievement_header: Color,
pub hud_color_achievement_accomplished: Color,
2024-10-26 21:20:51 +00:00
pub hud_color_phonebook_locked: Color,
pub hud_color_phonebook_unlocked: Color,
2024-05-14 04:28:14 +00:00
pub hud_color_death: Color,
pub hud_color_death_achievements: Color,
pub hud_color_keybindings: Color,
2024-05-14 17:08:18 +00:00
pub hud_color_version: Color,
2024-04-12 23:34:18 +00:00
pub chat_speed: f32,
2024-05-23 03:02:59 +00:00
pub ar_avatar: usize,
pub flashlight_active: bool,
pub reactor_state: usize,
pub hud_active: bool,
2024-04-18 19:01:03 +00:00
pub map_active: bool,
2024-05-12 20:17:17 +00:00
pub deathscreen_active: bool,
2024-05-13 18:21:56 +00:00
pub menu_active: bool,
2024-05-12 20:17:17 +00:00
pub death_cause: String,
2024-04-05 21:38:20 +00:00
pub is_zooming: bool,
pub third_person: bool,
2024-04-11 19:06:21 +00:00
pub rotation_stabilizer_active: bool,
pub cruise_control_active: bool,
pub shadows_sun: bool,
pub shadows_pointlights: bool,
2024-04-24 17:59:14 +00:00
pub shadowmap_resolution: usize,
pub large_moons: bool,
pub key_selectobject: MouseButton,
2024-04-05 21:38:20 +00:00
pub key_zoom: MouseButton,
2024-04-18 19:01:03 +00:00
pub key_map: KeyCode,
pub key_map_zoom_out: KeyCode,
pub key_map_zoom_in: KeyCode,
//pub key_map_zoom_out_wheel: MouseButton,
//pub key_map_zoom_in_wheel: MouseButton,
2024-03-18 03:39:26 +00:00
pub key_togglehud: KeyCode,
2024-05-13 18:21:56 +00:00
pub key_menu: KeyCode,
2024-03-18 03:39:26 +00:00
pub key_fullscreen: KeyCode,
2024-04-15 21:17:44 +00:00
pub key_help: KeyCode,
2024-03-18 03:39:26 +00:00
pub key_forward: KeyCode,
pub key_back: KeyCode,
pub key_left: KeyCode,
pub key_right: KeyCode,
pub key_up: KeyCode,
pub key_down: KeyCode,
pub key_run: KeyCode,
pub key_stop: KeyCode,
pub key_interact: KeyCode,
pub key_vehicle: KeyCode,
pub key_camera: KeyCode,
pub key_flashlight: KeyCode,
2024-05-15 02:58:55 +00:00
pub key_cruise_control: KeyCode,
2024-03-30 18:14:59 +00:00
pub key_rotate: KeyCode,
2024-04-11 19:06:21 +00:00
pub key_rotation_stabilizer: KeyCode,
pub key_mouseup: KeyCode,
pub key_mousedown: KeyCode,
pub key_mouseleft: KeyCode,
pub key_mouseright: KeyCode,
pub key_rotateleft: KeyCode,
pub key_rotateright: KeyCode,
2024-03-20 01:03:42 +00:00
pub key_reply1: KeyCode,
pub key_reply2: KeyCode,
pub key_reply3: KeyCode,
pub key_reply4: KeyCode,
pub key_reply5: KeyCode,
pub key_reply6: KeyCode,
pub key_reply7: KeyCode,
pub key_reply8: KeyCode,
pub key_reply9: KeyCode,
pub key_reply10: KeyCode,
2024-04-07 23:44:36 +00:00
pub key_cheat_god_mode: KeyCode,
2024-04-01 03:25:35 +00:00
pub key_cheat_stop: KeyCode,
pub key_cheat_speed: KeyCode,
pub key_cheat_speed_backward: KeyCode,
2024-04-14 21:38:55 +00:00
pub key_cheat_teleport: KeyCode,
2024-04-01 03:25:35 +00:00
pub key_cheat_pizza: KeyCode,
pub key_cheat_farview1: KeyCode,
pub key_cheat_farview2: KeyCode,
pub key_cheat_adrenaline_zero: KeyCode,
pub key_cheat_adrenaline_mid: KeyCode,
pub key_cheat_adrenaline_max: KeyCode,
2024-04-05 00:58:02 +00:00
pub key_cheat_die: KeyCode,
2024-03-16 21:20:23 +00:00
}
impl Default for Settings {
fn default() -> Self {
let dev_mode = cfg!(feature = "dev_mode") && env::var("CARGO").is_ok();
2024-04-18 01:56:32 +00:00
let version = if let Some(version) = option_env!("CARGO_PKG_VERSION") {
version.to_string()
} else {
2024-05-14 17:08:18 +00:00
"".to_string()
2024-04-18 01:56:32 +00:00
};
2024-03-22 11:08:00 +00:00
2024-03-16 21:20:23 +00:00
Settings {
2024-04-14 02:56:34 +00:00
dev_mode,
2024-04-07 23:44:36 +00:00
god_mode: false,
2024-04-18 01:56:32 +00:00
version,
2024-05-13 02:47:47 +00:00
alive: true,
window_focused: true,
2024-06-13 02:06:15 +00:00
mute_sfx: false,
noise_cancellation_mode: 0,
2024-10-03 21:59:15 +00:00
noise_cancellation_modes: vec![
("Off".to_string(), 1.0),
("33%".to_string(), 0.66),
("66%".to_string(), 0.33),
("100%".to_string(), 0.0),
],
2024-06-13 01:26:19 +00:00
radio_mode: 1,
2024-06-13 02:06:15 +00:00
radio_modes: vec![
// see also: settings.is_radio_playing()
2024-06-13 01:26:19 +00:00
"Off".to_string(),
"Space Wave Radio".to_string(),
"Amplify outside recordings".to_string(),
2024-06-13 01:26:19 +00:00
],
2024-10-03 21:59:15 +00:00
volume_sfx: 1.0,
volume_music: 1.0,
mouse_sensitivity: 0.4,
2024-04-08 01:15:45 +00:00
fov: 50.0,
fov_highspeed: 25.0,
2024-04-08 02:16:01 +00:00
zoom_fov: 15.0,
2024-04-08 00:17:36 +00:00
zoom_sensitivity_factor: 0.25,
2024-04-15 19:29:06 +00:00
font_size_hud: 24.0,
font_size_fps: 14.0,
2024-03-17 19:31:16 +00:00
font_size_conversations: 32.0,
2024-04-15 18:58:19 +00:00
font_size_choices: 28.0,
font_size_console: 20.0,
2024-04-28 04:29:01 +00:00
font_size_speedometer: 34.0,
2024-05-12 20:17:17 +00:00
font_size_deathtext: 64.0,
font_size_deathsubtext: 32.0,
2024-05-12 23:42:22 +00:00
font_size_deathpoem: 18.0,
2024-05-14 04:28:14 +00:00
font_size_death_achievements: 24.0,
2024-05-14 03:17:32 +00:00
font_size_achievement: 24.0,
font_size_achievement_header: 32.0,
font_size_keybindings: 20.0,
2024-05-14 17:08:18 +00:00
font_size_version: 20.0,
2024-07-09 00:38:28 +00:00
hud_color: Srgba::hex(COLOR_PRIMARY).unwrap().into(),
hud_color_fps: Srgba::hex("#181818").unwrap().into(),
hud_color_console: Srgba::hex(COLOR_PRIMARY).unwrap().into(),
hud_color_console_achievement: Srgba::hex(COLOR_SUCCESS).unwrap().into(),
hud_color_console_warn: Srgba::hex(COLOR_WARNING).unwrap().into(),
hud_color_console_system: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
hud_color_alert: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
hud_color_subtitles: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
hud_color_choices: Srgba::hex(COLOR_BODY).unwrap().into(),
hud_color_speedometer: Srgba::hex(COLOR_PRIMARY).unwrap().into(),
hud_color_deathpoem: Srgba::hex("#CC2200").unwrap().into(),
hud_color_achievement: Srgba::hex(COLOR_DIM).unwrap().into(),
hud_color_achievement_accomplished: Srgba::hex(COLOR_SUCCESS).unwrap().into(),
hud_color_achievement_header: Srgba::hex(COLOR_PRIMARY).unwrap().into(),
2024-10-26 21:20:51 +00:00
hud_color_phonebook_locked: Srgba::hex(COLOR_DIM).unwrap().into(),
hud_color_phonebook_unlocked: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
2024-07-09 00:38:28 +00:00
hud_color_death: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
hud_color_death_achievements: Srgba::hex(COLOR_SECONDARY).unwrap().into(),
hud_color_keybindings: Srgba::hex(COLOR_DIM).unwrap().into(),
hud_color_version: Srgba::hex(COLOR_PRIMARY).unwrap().into(),
chat_speed: DEFAULT_CHAT_SPEED,
2024-05-23 03:02:59 +00:00
ar_avatar: 0,
flashlight_active: false,
reactor_state: 1,
hud_active: true,
2024-04-18 19:01:03 +00:00
map_active: false,
2024-05-12 20:17:17 +00:00
deathscreen_active: false,
2024-05-13 18:21:56 +00:00
menu_active: false,
2024-05-12 20:17:17 +00:00
death_cause: "Unknown".to_string(),
2024-04-05 21:38:20 +00:00
is_zooming: false,
third_person: true,
2024-04-11 19:06:21 +00:00
rotation_stabilizer_active: true,
2024-05-15 02:58:55 +00:00
cruise_control_active: false,
shadows_sun: true,
shadows_pointlights: false,
2024-04-24 17:59:14 +00:00
shadowmap_resolution: 2048,
large_moons: false,
key_selectobject: MouseButton::Left,
2024-04-05 21:38:20 +00:00
key_zoom: MouseButton::Right,
2024-04-20 02:48:17 +00:00
key_map: KeyCode::KeyM,
2024-04-18 19:01:03 +00:00
key_map_zoom_out: KeyCode::ShiftLeft,
key_map_zoom_in: KeyCode::ControlLeft,
//key_map_zoom_out_wheel: KeyCode::Shift,
//key_map_zoom_in_wheel: KeyCode::Shift,
2024-03-18 03:39:26 +00:00
key_togglehud: KeyCode::Tab,
2024-05-13 18:21:56 +00:00
key_menu: KeyCode::Escape,
2024-03-18 03:39:26 +00:00
key_fullscreen: KeyCode::F11,
2024-04-15 21:17:44 +00:00
key_help: KeyCode::F1,
2024-03-18 03:39:26 +00:00
key_forward: KeyCode::KeyW,
key_back: KeyCode::KeyS,
key_left: KeyCode::KeyA,
key_right: KeyCode::KeyD,
key_up: KeyCode::ShiftLeft,
key_down: KeyCode::ControlLeft,
key_run: KeyCode::KeyR,
key_stop: KeyCode::Space,
key_interact: KeyCode::KeyE,
key_vehicle: KeyCode::KeyQ,
2024-05-13 21:01:00 +00:00
key_camera: KeyCode::KeyC,
key_flashlight: KeyCode::KeyF,
2024-05-15 02:58:55 +00:00
key_cruise_control: KeyCode::KeyT,
2024-03-30 18:14:59 +00:00
key_rotate: KeyCode::KeyR,
2024-04-11 19:06:21 +00:00
key_rotation_stabilizer: KeyCode::KeyY,
key_mouseup: KeyCode::KeyI,
key_mousedown: KeyCode::KeyK,
key_mouseleft: KeyCode::KeyJ,
key_mouseright: KeyCode::KeyL,
key_rotateleft: KeyCode::KeyU,
key_rotateright: KeyCode::KeyO,
2024-03-20 01:03:42 +00:00
key_reply1: KeyCode::Digit1,
key_reply2: KeyCode::Digit2,
key_reply3: KeyCode::Digit3,
key_reply4: KeyCode::Digit4,
key_reply5: KeyCode::Digit5,
key_reply6: KeyCode::Digit6,
key_reply7: KeyCode::Digit7,
key_reply8: KeyCode::Digit8,
key_reply9: KeyCode::Digit9,
key_reply10: KeyCode::Digit0,
2024-04-07 23:44:36 +00:00
key_cheat_god_mode: KeyCode::KeyG,
2024-05-13 21:01:00 +00:00
key_cheat_stop: KeyCode::KeyZ,
2024-04-01 03:25:35 +00:00
key_cheat_speed: KeyCode::KeyV,
key_cheat_speed_backward: KeyCode::KeyB,
2024-04-14 21:38:55 +00:00
key_cheat_teleport: KeyCode::KeyX,
2024-04-11 18:47:11 +00:00
key_cheat_pizza: KeyCode::F9,
key_cheat_farview1: KeyCode::F10,
key_cheat_farview2: KeyCode::F12,
2024-04-01 03:25:35 +00:00
key_cheat_adrenaline_zero: KeyCode::F5,
key_cheat_adrenaline_mid: KeyCode::F6,
2024-04-11 18:47:11 +00:00
key_cheat_adrenaline_max: KeyCode::F8,
2024-05-13 21:01:00 +00:00
key_cheat_die: KeyCode::F4,
2024-03-16 21:20:23 +00:00
}
}
}
impl Settings {
2024-04-11 19:20:54 +00:00
#[allow(dead_code)]
2024-03-16 21:20:23 +00:00
pub fn reset(&mut self) {
println!("Resetting settings!");
*self = Self::default();
}
2024-03-20 01:03:42 +00:00
pub fn is_game_running(&self) -> bool {
!self.menu_active && self.window_focused
}
2024-04-11 19:20:54 +00:00
pub fn reset_player_settings(&mut self) {
println!("Resetting player settings!");
let default = Self::default();
self.rotation_stabilizer_active = default.rotation_stabilizer_active;
self.is_zooming = default.is_zooming;
2024-05-08 03:47:37 +00:00
self.flashlight_active = default.flashlight_active;
2024-05-15 03:28:41 +00:00
self.cruise_control_active = default.cruise_control_active;
2024-05-12 21:30:55 +00:00
self.map_active = default.map_active;
2024-04-11 19:20:54 +00:00
}
2024-03-20 01:03:42 +00:00
pub fn get_reply_keys(&self) -> [KeyCode; 10] {
return [
self.key_reply1,
self.key_reply2,
self.key_reply3,
self.key_reply4,
self.key_reply5,
self.key_reply6,
self.key_reply7,
self.key_reply8,
self.key_reply9,
self.key_reply10,
];
}
2024-05-13 18:21:56 +00:00
pub fn in_control(&self) -> bool {
return self.alive && !self.menu_active;
}
2024-06-13 01:41:15 +00:00
pub fn is_radio_playing(&self, sfx: audio::Sfx) -> Option<bool> {
2024-06-13 02:06:15 +00:00
let radio = self.radio_mode;
2024-06-13 01:41:15 +00:00
match sfx {
audio::Sfx::BGMTakeoff => Some(radio == 1),
audio::Sfx::BGMActualJupiterRecording => Some(radio == 2),
2024-06-13 01:41:15 +00:00
_ => None,
}
}
2024-06-13 02:06:15 +00:00
pub fn set_noise_cancellation_mode(&mut self, value: usize) {
let value = if value >= self.noise_cancellation_modes.len() {
warn!("Attempting to set too large noise cancellation mode: {value}");
0
} else {
value
};
self.noise_cancellation_mode = value;
2024-10-03 21:59:15 +00:00
self.volume_sfx = if let Some(noisecancel) = self
.noise_cancellation_modes
.get(self.noise_cancellation_mode)
{
noisecancel.1
} else {
self.noise_cancellation_modes[0].1
};
self.mute_sfx = value >= 3;
2024-06-13 02:06:15 +00:00
}
2024-03-16 21:20:23 +00:00
}
2024-04-14 13:37:36 +00:00
2024-05-13 23:24:57 +00:00
#[derive(Resource, Default, Debug)]
pub struct AchievementTracker {
pub repair_suit: bool,
pub drink_a_pizza: bool,
pub in_jupiters_shadow: bool,
2024-05-14 03:33:35 +00:00
pub find_earth: bool,
2024-05-13 23:24:57 +00:00
pub ride_every_vehicle: bool,
pub vehicles_ridden: HashSet<String>,
pub all_vehicles: HashSet<String>,
pub talk_to_everyone: bool,
pub people_talked_to: HashSet<String>,
pub all_people: HashSet<String>,
}
2024-05-14 03:17:32 +00:00
impl AchievementTracker {
pub fn to_bool_vec(&self) -> Vec<bool> {
vec![
self.repair_suit,
self.drink_a_pizza,
self.ride_every_vehicle,
self.talk_to_everyone,
2024-05-14 03:33:35 +00:00
self.find_earth,
2024-05-14 03:17:32 +00:00
self.in_jupiters_shadow,
]
}
2024-05-14 04:28:14 +00:00
pub fn achieve_all(&mut self) {
self.repair_suit = true;
self.drink_a_pizza = true;
self.ride_every_vehicle = true;
self.talk_to_everyone = true;
self.find_earth = true;
self.in_jupiters_shadow = true;
}
2024-05-14 03:17:32 +00:00
pub fn to_textsections(&self) -> Vec<String> {
2024-05-14 03:41:26 +00:00
fn collectible(current: usize, total: usize) -> String {
if current < total {
format!(" ({}/{})", current, total)
} else {
"".to_string()
}
2024-05-14 03:17:32 +00:00
}
2024-05-14 03:41:26 +00:00
let ride = collectible(self.vehicles_ridden.len(), self.all_vehicles.len());
let talk = collectible(self.people_talked_to.len(), self.all_people.len());
2024-05-14 03:17:32 +00:00
vec![
2024-05-14 03:41:26 +00:00
"Repair Your Suit\n".to_string(),
2024-05-14 16:45:35 +00:00
"Enjoy A Pizza\n".to_string(),
2024-05-14 03:41:26 +00:00
format!("Ride Every Vehicle{ride}\n"),
format!("Talk To Everyone{talk}\n"),
"Find Earth\n".to_string(),
"Enter Jupiter's Shadow\n".to_string(),
2024-05-14 03:17:32 +00:00
]
}
2024-05-14 04:28:14 +00:00
pub fn to_overview(&self) -> Vec<(bool, String)> {
vec![
(self.repair_suit, "repair your suit".into()),
2024-05-14 16:45:35 +00:00
(self.drink_a_pizza, "enjoy a pizza".into()),
2024-05-14 04:28:14 +00:00
(self.ride_every_vehicle, "ride every vehicle".into()),
(self.talk_to_everyone, "talk to everyone".into()),
(self.find_earth, "find Earth".into()),
2024-07-14 19:16:19 +00:00
(self.in_jupiters_shadow, "enter Jupiter's shadow".into()),
2024-05-14 04:28:14 +00:00
]
}
pub fn to_summary(&self) -> String {
let list = self.to_overview();
let count = list.iter().filter(|(achieved, _)| *achieved).count();
if count == 0 {
2024-05-22 02:57:20 +00:00
return "".to_string();
2024-05-14 04:28:14 +00:00
}
let mut summary = "\n\n\nYou managed to ".to_string();
for (i, (_, text)) in list.iter().filter(|(achieved, _)| *achieved).enumerate() {
summary += text.as_str();
if i + 2 == count {
summary += ", and ";
2024-05-22 02:57:20 +00:00
} else if i + 1 == count {
2024-05-14 04:28:14 +00:00
summary += " before you perished.";
if count == list.len() {
summary += "\nA truly astounding achievement, a glimmer in the void, before it all fades, into nothingness.";
}
2024-05-22 02:57:20 +00:00
} else {
2024-05-14 04:28:14 +00:00
summary += ", ";
}
}
summary
}
2024-05-14 03:17:32 +00:00
}
// Used for settings that are preserved across restarts
#[derive(Resource, Serialize, Deserialize, Debug, Default)]
#[serde(default)]
pub struct Preferences {
pub fullscreen_mode: String,
pub fullscreen_on: bool,
pub render_mode: String,
pub augmented_reality: bool,
2024-06-13 01:26:19 +00:00
pub radio_station: usize,
2024-06-13 02:06:15 +00:00
pub noise_cancellation_mode: usize,
pub third_person: bool,
pub shadows_sun: bool,
pub avatar: usize,
2024-10-12 03:16:04 +00:00
pub pointer: hud::Pointer,
2024-10-05 02:23:47 +00:00
#[serde(default = "Preferences::default_light_amp")]
2024-10-05 02:27:00 +00:00
pub light_amp: usize, // 0-3
2024-10-05 02:23:47 +00:00
#[serde(default = "Preferences::default_flashlight_power")]
2024-09-22 01:40:12 +00:00
pub flashlight_power: usize, // 0-2
2024-10-05 02:27:00 +00:00
pub thruster_boost: usize, // 0-2
2024-10-26 20:01:52 +00:00
pub contacts: Vec<String>,
pub luna_backup: bool,
#[serde(skip)]
pub source_file: Option<String>,
}
impl Preferences {
2024-10-05 02:23:47 +00:00
pub fn default_light_amp() -> usize {
1
}
pub fn default_flashlight_power() -> usize {
2
}
pub fn get_fullscreen_mode(&self) -> WindowMode {
match self.fullscreen_mode.as_str() {
"legacy" => WindowMode::Fullscreen,
"sized" => WindowMode::SizedFullscreen,
_ => WindowMode::BorderlessFullscreen,
}
}
pub fn get_window_mode(&self) -> WindowMode {
match self.fullscreen_on {
true => self.get_fullscreen_mode(),
false => WindowMode::Windowed,
}
}
pub fn render_mode_is_gl(&self) -> bool {
return self.render_mode == "gl";
}
pub fn save(&self) {
if let Some(path) = get_prefs_path() {
match toml_edit::ser::to_document::<Preferences>(self) {
2024-06-17 00:03:19 +00:00
Ok(doc) => match fs::write(path.clone(), doc.to_string()) {
Ok(_) => {
info!("Saved preferences to {path}.");
}
2024-06-17 00:03:19 +00:00
Err(error) => {
error!("Error while writing preferences: {:?}", error);
}
},
Err(error) => {
error!("Error while writing preferences: {:?}", error);
}
}
}
}
}
fn file_is_readable(file_path: &str) -> bool {
2024-05-22 02:57:20 +00:00
fs::metadata(file_path)
.map(|metadata| metadata.is_file())
.unwrap_or(false)
}
fn path_is_directory(file_path: &str) -> bool {
fs::metadata(file_path)
.map(|metadata| metadata.is_dir())
.unwrap_or(false)
}
fn get_prefs_path() -> Option<String> {
2024-05-12 23:42:14 +00:00
let test = CONF_FILE;
if file_is_readable(test) {
return Some(test.to_string());
}
if let Some(mut conf) = dirs::config_dir() {
conf.push("OutFly");
if !conf.exists() {
match fs::create_dir_all(&conf) {
Ok(_) => {}
Err(error) => {
eprintln!("Failed creating configuration directory: {error}");
}
}
}
if let Some(test) = conf.to_str() {
if !path_is_directory(test) {
eprintln!("Failed creating configuration directory");
return None;
}
}
conf.push(CONF_FILE);
if !conf.exists() {
match fs::write(&conf, DEFAULT_CONFIG_TOML.to_string()) {
Ok(_) => {}
Err(error) => {
eprintln!("Failed creating configuration file: {error}");
}
}
}
if let Some(test) = conf.to_str() {
if file_is_readable(test) {
return Some(test.to_string());
}
}
}
return None;
}
pub fn load_prefs() -> Preferences {
let (toml, path) = match get_prefs_path() {
Some(path) => {
let toml = fs::read_to_string(&path);
match toml {
Ok(toml) => (toml, Some(path)),
Err(error) => {
eprintln!("Error: Failed to open preferences file '{path}': {error}");
return Preferences::default();
}
}
}
None => {
println!("Found no preference file, using default preferences.");
(DEFAULT_CONFIG_TOML.to_string(), None)
}
};
match toml.parse::<DocumentMut>() {
2024-05-22 02:57:20 +00:00
Ok(doc) => match toml_edit::de::from_document::<Preferences>(doc) {
2024-09-22 01:40:12 +00:00
Ok(mut prefs) => {
2024-05-22 02:57:20 +00:00
if let Some(path) = &path {
println!("Loaded preference file from {path}");
2024-05-22 02:57:20 +00:00
} else {
println!("Loaded preferences from internal defaults");
}
2024-09-22 01:40:12 +00:00
prefs.source_file = path;
2024-09-22 04:39:21 +00:00
prefs.flashlight_power = prefs.flashlight_power.clamp(0, 3);
2024-09-22 01:40:12 +00:00
prefs.light_amp = prefs.light_amp.clamp(0, 3);
2024-09-22 02:34:36 +00:00
prefs.thruster_boost = prefs.thruster_boost.clamp(0, 2);
2024-09-22 01:40:12 +00:00
prefs
}
2024-05-22 02:57:20 +00:00
Err(error) => {
eprintln!("Error: Failed to read preference line: {error}");
2024-09-22 01:40:12 +00:00
Preferences::default()
2024-05-22 02:57:20 +00:00
}
},
Err(error) => {
eprintln!("Error: Failed to open preferences: {error}");
2024-09-22 01:40:12 +00:00
Preferences::default()
}
}
}
#[derive(Resource, Debug)]
2024-04-14 14:20:51 +00:00
pub struct GameVars {
2024-04-14 13:37:36 +00:00
pub db: HashMap<String, String>,
}
2024-04-14 14:20:51 +00:00
impl Default for GameVars {
fn default() -> Self {
2024-05-22 02:57:20 +00:00
Self { db: HashMap::new() }
2024-04-14 14:20:51 +00:00
}
}
2024-04-14 13:37:36 +00:00
impl GameVars {
pub fn reset(&mut self) {
self.db.clear();
}
2024-04-14 13:37:36 +00:00
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn get(&self, key: &str) -> Option<String> {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
return Some(value.clone());
}
return None;
}
#[allow(dead_code)]
2024-04-14 14:20:51 +00:00
pub fn getf(&self, key: &str) -> Option<f64> {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
if let Ok(float) = value.parse::<f64>() {
return Some(float);
}
}
return None;
}
2024-04-14 14:20:51 +00:00
pub fn getb(&self, key: &str) -> bool {
2024-04-14 13:37:36 +00:00
if let Some(value) = self.db.get(key) {
return Self::evaluate_str_as_bool(value);
2024-04-14 13:37:36 +00:00
}
return false;
}
pub fn evaluate_str_as_bool(string: &str) -> bool {
return string != "0";
2024-04-14 14:20:51 +00:00
}
// This method ensures that the variable name contains a scope separator,
// and if a scope is missing, it prefixes the fallback scope.
// Should NOT be used on non-variable values, like plain strings.
//
// See test_normalize_varname() for examples.
pub fn normalize_varname(fallback_scope: &str, key: &str) -> String {
let parts: Vec<&str> = key.split(SCOPE_SEPARATOR).collect();
let key: String = if parts.len() == 1 {
// we got a key like "foo", turn it into "<scope>$foo"
fallback_scope.to_string() + SCOPE_SEPARATOR + key
} else if parts.len() > 1 {
// we got a key with at least one "$"
// extract anything before the last "$":
let scope_part: String = parts[0..parts.len() - 1].join(SCOPE_SEPARATOR);
if scope_part.is_empty() {
// we got a key like "$foo", just prefix the fallback scope
fallback_scope.to_string() + key
2024-05-22 02:57:20 +00:00
} else {
// we got a key like "Ke$ha$foo" or "$$foo" (which is the convention for
// global variables), leave the scope intact
key.to_string()
}
2024-04-14 14:20:51 +00:00
} else {
// we got an empty string. this is bad, but handle gracefully
fallback_scope.to_string() + SCOPE_SEPARATOR
2024-04-14 14:20:51 +00:00
};
return key.to_lowercase();
}
pub fn set_in_scope(&mut self, fallback_scope: &str, key: &str, value: String) {
let key = Self::normalize_varname(fallback_scope, key);
2024-04-14 14:20:51 +00:00
self.db.insert(key, value);
2024-04-14 13:37:36 +00:00
}
pub fn evaluate_condition(&self, condition: &str, scope: &str) -> bool {
let parts: Vec<&str> = condition.split(" ").collect();
if parts.len() == 0 {
// Got an empty string, this is always false.
return false;
} else if parts.len() == 1 {
// Got something like "if $somevar:".
// Check whether the variable evaluates to true.
let part = parts[0];
let (part, negate) = if part.starts_with(TOKEN_NEGATE) {
(&part[1..], true)
} else {
(part, false)
};
if part.contains(SCOPE_SEPARATOR) {
let part = Self::normalize_varname(scope, part);
let value_bool = self.getb(part.as_str());
return value_bool ^ negate;
2024-05-22 02:57:20 +00:00
} else {
return Self::evaluate_str_as_bool(part) ^ negate;
}
} else if parts.len() == 2 {
// Got something like "if $something somethingelse"
// Check whether the two are identical.
let mut left: String = parts[0].to_string();
if left.contains(SCOPE_SEPARATOR) {
let key = Self::normalize_varname(scope, left.as_str());
let value = self.get(key.as_str());
left = if let Some(value) = value {
value
} else {
warn!("Couldn't find variable `{key}` on left hand side of a condition");
"".to_string()
};
}
let mut right: String = parts[1].to_string();
if right.contains(SCOPE_SEPARATOR) {
let key = Self::normalize_varname(scope, right.as_str());
let value = self.get(key.as_str());
right = if let Some(value) = value {
value
} else {
warn!("Couldn't find variable `{key}` on right hand side of a condition");
"".to_string()
};
}
return left == right;
} else {
// Got something like "if $something != somethingelse bla bla"
let mut left: String = parts[0].to_string();
if left.contains(SCOPE_SEPARATOR) {
let key = Self::normalize_varname(scope, left.as_str());
let value = self.get(key.as_str());
left = if let Some(value) = value {
value
} else {
warn!("Couldn't find variable `{key}` on left hand side of a condition");
"".to_string()
};
}
let mut right: String = parts[2..parts.len()].join(" ").to_string();
if right.contains(SCOPE_SEPARATOR) {
let key = Self::normalize_varname(scope, right.as_str());
let value = self.get(key.as_str());
right = if let Some(value) = value {
value
} else {
warn!("Couldn't find variable `{key}` on right hand side of a condition");
"".to_string()
};
}
let floats = (left.parse::<f64>(), right.parse::<f64>());
let operator: &str = parts[1];
match operator {
TOKEN_EQUALS => {
if let (Ok(left), Ok(right)) = floats {
return left == right;
}
return left == right;
}
TOKEN_EQUALS_NOT => {
if let (Ok(left), Ok(right)) = floats {
return left != right;
}
return left != right;
}
TOKEN_GREATER_THAN => {
if let (Ok(left), Ok(right)) = floats {
return left > right;
}
return false;
}
TOKEN_GREATER_EQUALS => {
if let (Ok(left), Ok(right)) = floats {
return left >= right;
}
return false;
}
TOKEN_LESS_THAN => {
if let (Ok(left), Ok(right)) = floats {
return left < right;
}
return false;
}
TOKEN_LESS_EQUALS => {
if let (Ok(left), Ok(right)) = floats {
return left <= right;
}
return false;
}
_ => {
error!("Unknown operator '{operator}' in if-condition!");
return false;
}
}
}
}
2024-04-14 13:37:36 +00:00
}
2024-05-13 18:21:56 +00:00
#[test]
fn test_normalize_varname() {
assert_eq!(GameVars::normalize_varname("Clippy", ""), "clippy$");
assert_eq!(GameVars::normalize_varname("Clippy", "foo"), "clippy$foo");
assert_eq!(GameVars::normalize_varname("Clippy", "FOO"), "clippy$foo");
assert_eq!(GameVars::normalize_varname("Clippy", "$foo"), "clippy$foo");
assert_eq!(GameVars::normalize_varname("Clippy", "$$foo"), "$$foo");
2024-06-12 22:39:05 +00:00
assert_eq!(
GameVars::normalize_varname("Clippy", "PizzaClippy$foo"),
"pizzaclippy$foo"
);
assert_eq!(
GameVars::normalize_varname("Clippy", "$foo$foo$foo$foo"),
"$foo$foo$foo$foo"
);
}
2024-05-13 18:21:56 +00:00
#[derive(Resource, Default)]
pub struct CommandLineOptions {
pub window_mode_fullscreen: WindowMode,
pub window_mode_initial: WindowMode,
pub use_gl: bool,
}