2020-09-20 15:27:31 +00:00
|
|
|
use std::ops::Div;
|
|
|
|
|
|
|
|
use bitvec::vec::BitVec;
|
2020-11-07 17:17:01 +00:00
|
|
|
use chrono::{Datelike, Local};
|
2020-09-20 15:27:31 +00:00
|
|
|
|
|
|
|
use crate::bmfont_renderer::BMFontRenderer;
|
|
|
|
use crate::caret::{Caret, CaretType};
|
2020-11-28 19:25:51 +00:00
|
|
|
use crate::common::{ControlFlags, Direction, FadeState};
|
2020-09-20 15:27:31 +00:00
|
|
|
use crate::engine_constants::EngineConstants;
|
2021-02-10 20:14:09 +00:00
|
|
|
use crate::framework::backend::BackendTexture;
|
2021-01-27 18:20:47 +00:00
|
|
|
use crate::framework::context::Context;
|
|
|
|
use crate::framework::error::GameResult;
|
2021-02-10 20:14:09 +00:00
|
|
|
use crate::framework::graphics::{create_texture_mutable, set_render_target};
|
|
|
|
use crate::framework::keyboard::ScanCode;
|
2021-01-27 18:20:47 +00:00
|
|
|
use crate::framework::vfs::OpenOptions;
|
2021-06-28 11:07:20 +00:00
|
|
|
use crate::framework::{filesystem, graphics};
|
2021-03-29 21:19:07 +00:00
|
|
|
#[cfg(feature = "hooks")]
|
|
|
|
use crate::hooks::init_hooks;
|
2020-11-28 19:25:51 +00:00
|
|
|
use crate::input::touch_controls::TouchControls;
|
2020-12-25 22:39:41 +00:00
|
|
|
use crate::npc::NPCTable;
|
2020-09-22 21:28:36 +00:00
|
|
|
use crate::profile::GameProfile;
|
2020-12-25 22:39:41 +00:00
|
|
|
use crate::rng::XorShift;
|
2020-09-22 21:28:36 +00:00
|
|
|
use crate::scene::game_scene::GameScene;
|
2021-06-28 11:07:20 +00:00
|
|
|
use crate::scene::title_scene::TitleScene;
|
2020-09-20 15:27:31 +00:00
|
|
|
use crate::scene::Scene;
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
|
|
|
use crate::scripting::lua::LuaScriptingState;
|
2021-10-16 02:37:42 +00:00
|
|
|
use crate::scripting::tsc::credit_script::CreditScriptVM;
|
2022-01-13 16:32:33 +00:00
|
|
|
use crate::scripting::tsc::text_script::{ScriptMode, TextScriptExecutionState, TextScriptVM};
|
2020-11-28 19:25:51 +00:00
|
|
|
use crate::settings::Settings;
|
2020-09-20 15:27:31 +00:00
|
|
|
use crate::sound::SoundManager;
|
|
|
|
use crate::stage::StageData;
|
2021-01-27 18:20:47 +00:00
|
|
|
use crate::texture_set::TextureSet;
|
2020-09-22 21:28:36 +00:00
|
|
|
|
2021-10-14 07:43:17 +00:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, serde::Serialize, serde::Deserialize)]
|
2020-09-22 21:28:36 +00:00
|
|
|
pub enum TimingMode {
|
|
|
|
_50Hz,
|
|
|
|
_60Hz,
|
|
|
|
FrameSynchronized,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimingMode {
|
|
|
|
pub fn get_delta(self) -> usize {
|
|
|
|
match self {
|
2021-02-10 20:14:09 +00:00
|
|
|
TimingMode::_50Hz => 1000000000 / 50,
|
|
|
|
TimingMode::_60Hz => 1000000000 / 60,
|
|
|
|
TimingMode::FrameSynchronized => 0,
|
2020-09-22 21:28:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 22:47:29 +00:00
|
|
|
|
|
|
|
pub fn get_delta_millis(self) -> f64 {
|
|
|
|
match self {
|
2021-02-10 20:14:09 +00:00
|
|
|
TimingMode::_50Hz => 1000.0 / 50.0,
|
|
|
|
TimingMode::_60Hz => 1000.0 / 60.0,
|
|
|
|
TimingMode::FrameSynchronized => 0.0,
|
2020-10-30 22:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-24 23:08:27 +00:00
|
|
|
|
|
|
|
pub fn get_tps(self) -> usize {
|
|
|
|
match self {
|
2021-02-10 20:14:09 +00:00
|
|
|
TimingMode::_50Hz => 50,
|
|
|
|
TimingMode::_60Hz => 60,
|
|
|
|
TimingMode::FrameSynchronized => 0,
|
2020-11-24 23:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 21:28:36 +00:00
|
|
|
}
|
2020-09-20 15:27:31 +00:00
|
|
|
|
2020-11-02 01:38:39 +00:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum Season {
|
|
|
|
None,
|
|
|
|
Halloween,
|
|
|
|
Christmas,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Season {
|
|
|
|
pub fn current() -> Season {
|
|
|
|
let now = Local::now();
|
|
|
|
|
|
|
|
if (now.month() == 10 && now.day() > 25) || (now.month() == 11 && now.day() < 3) {
|
|
|
|
Season::Halloween
|
|
|
|
} else if (now.month() == 12 && now.day() > 23) || (now.month() == 0 && now.day() < 7) {
|
|
|
|
Season::Christmas
|
|
|
|
} else {
|
|
|
|
Season::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 01:06:56 +00:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum TileSize {
|
|
|
|
Tile8x8,
|
|
|
|
Tile16x16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TileSize {
|
|
|
|
pub const fn as_float(self) -> f32 {
|
|
|
|
match self {
|
|
|
|
TileSize::Tile8x8 => 8.0,
|
|
|
|
TileSize::Tile16x16 => 16.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn as_int(self) -> i32 {
|
|
|
|
match self {
|
|
|
|
TileSize::Tile8x8 => 8,
|
|
|
|
TileSize::Tile16x16 => 16,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 15:27:31 +00:00
|
|
|
pub struct SharedGameState {
|
|
|
|
pub control_flags: ControlFlags,
|
|
|
|
pub game_flags: BitVec,
|
2021-05-02 00:09:54 +00:00
|
|
|
pub skip_flags: BitVec,
|
2021-05-02 02:04:52 +00:00
|
|
|
pub map_flags: BitVec,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub fade_state: FadeState,
|
2020-10-27 01:05:49 +00:00
|
|
|
/// RNG used by game state, using it for anything else might cause unintended side effects and break replays.
|
2020-12-25 22:39:41 +00:00
|
|
|
pub game_rng: XorShift,
|
2020-10-27 01:05:49 +00:00
|
|
|
/// RNG used by graphics effects that aren't dependent on game's state.
|
2020-12-25 22:39:41 +00:00
|
|
|
pub effect_rng: XorShift,
|
2021-06-27 01:06:56 +00:00
|
|
|
pub tile_size: TileSize,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub quake_counter: u16,
|
2022-01-13 16:32:33 +00:00
|
|
|
pub super_quake_counter: u16,
|
2020-09-29 20:19:47 +00:00
|
|
|
pub teleporter_slots: Vec<(u16, u16)>,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub carets: Vec<Caret>,
|
2020-10-20 20:45:56 +00:00
|
|
|
pub touch_controls: TouchControls,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub base_path: String,
|
|
|
|
pub npc_table: NPCTable,
|
2021-01-01 01:46:01 +00:00
|
|
|
pub npc_super_pos: (i32, i32),
|
2021-05-05 12:04:59 +00:00
|
|
|
pub npc_curly_target: (i32, i32),
|
|
|
|
pub npc_curly_counter: u16,
|
2021-10-08 02:41:31 +00:00
|
|
|
pub water_level: i32,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub stages: Vec<StageData>,
|
2020-10-30 22:47:29 +00:00
|
|
|
pub frame_time: f64,
|
2021-02-10 11:53:49 +00:00
|
|
|
pub debugger: bool,
|
2020-09-20 15:27:31 +00:00
|
|
|
pub scale: f32,
|
|
|
|
pub canvas_size: (f32, f32),
|
|
|
|
pub screen_size: (f32, f32),
|
2021-06-28 11:07:20 +00:00
|
|
|
pub preferred_viewport_size: (f32, f32),
|
2020-09-20 15:27:31 +00:00
|
|
|
pub next_scene: Option<Box<dyn Scene>>,
|
|
|
|
pub textscript_vm: TextScriptVM,
|
2021-10-16 02:37:42 +00:00
|
|
|
pub creditscript_vm: CreditScriptVM,
|
2021-02-05 09:47:30 +00:00
|
|
|
pub lightmap_canvas: Option<Box<dyn BackendTexture>>,
|
2020-11-02 01:38:39 +00:00
|
|
|
pub season: Season,
|
|
|
|
pub constants: EngineConstants,
|
|
|
|
pub font: BMFontRenderer,
|
|
|
|
pub texture_set: TextureSet,
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
2021-01-01 01:46:01 +00:00
|
|
|
pub lua: LuaScriptingState,
|
2020-11-02 01:38:39 +00:00
|
|
|
pub sound_manager: SoundManager,
|
|
|
|
pub settings: Settings,
|
2021-02-10 20:14:09 +00:00
|
|
|
pub shutdown: bool,
|
2020-09-20 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedGameState {
|
|
|
|
pub fn new(ctx: &mut Context) -> GameResult<SharedGameState> {
|
|
|
|
let mut constants = EngineConstants::defaults();
|
2021-06-20 22:42:10 +00:00
|
|
|
let sound_manager = SoundManager::new(ctx)?;
|
2020-09-20 15:27:31 +00:00
|
|
|
let mut base_path = "/";
|
2020-11-28 19:25:51 +00:00
|
|
|
let settings = Settings::load(ctx)?;
|
2020-09-20 15:27:31 +00:00
|
|
|
|
2022-01-22 01:19:18 +00:00
|
|
|
if filesystem::exists(ctx, "/base/lighting.tbl") {
|
2020-09-20 15:27:31 +00:00
|
|
|
info!("Cave Story+ (Switch) data files detected.");
|
2021-10-16 12:59:27 +00:00
|
|
|
ctx.size_hint = (854, 480);
|
2021-06-20 22:42:10 +00:00
|
|
|
constants.apply_csplus_patches(&sound_manager);
|
2020-09-20 15:27:31 +00:00
|
|
|
constants.apply_csplus_nx_patches();
|
|
|
|
base_path = "/base/";
|
2022-01-22 01:19:18 +00:00
|
|
|
} else if filesystem::exists(ctx, "/base/Nicalis.bmp") || filesystem::exists(ctx, "/base/Nicalis.png") {
|
|
|
|
info!("Cave Story+ (PC) data files detected.");
|
|
|
|
constants.apply_csplus_patches(&sound_manager);
|
|
|
|
base_path = "/base/";
|
2020-09-20 15:27:31 +00:00
|
|
|
} else if filesystem::exists(ctx, "/mrmap.bin") {
|
|
|
|
info!("CSE2E data files detected.");
|
|
|
|
} else if filesystem::exists(ctx, "/stage.dat") {
|
|
|
|
info!("NXEngine-evo data files detected.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let font = BMFontRenderer::load(base_path, &constants.font_path, ctx)
|
|
|
|
.or_else(|_| BMFontRenderer::load("/", "builtin/builtin_font.fnt", ctx))?;
|
2020-11-02 01:38:39 +00:00
|
|
|
let season = Season::current();
|
|
|
|
let mut texture_set = TextureSet::new(base_path);
|
|
|
|
|
|
|
|
if constants.is_cs_plus {
|
|
|
|
texture_set.apply_seasonal_content(season, &settings);
|
|
|
|
}
|
|
|
|
|
2021-06-20 22:42:10 +00:00
|
|
|
for i in 0..0xffu8 {
|
|
|
|
let path = format!("{}/pxt/fx{:02x}.pxt", base_path, i);
|
|
|
|
if let Ok(file) = filesystem::open(ctx, path) {
|
|
|
|
sound_manager.set_sample_params_from_file(i, file)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = format!("/pxt/fx{:02x}.pxt", i);
|
|
|
|
if let Ok(file) = filesystem::open(ctx, path) {
|
|
|
|
sound_manager.set_sample_params_from_file(i, file)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = format!("{}/PixTone/{:03}.pxt", base_path, i);
|
|
|
|
if let Ok(file) = filesystem::open(ctx, path) {
|
|
|
|
sound_manager.set_sample_params_from_file(i, file)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = format!("/PixTone/{:03}.pxt", i);
|
|
|
|
if let Ok(file) = filesystem::open(ctx, path) {
|
|
|
|
sound_manager.set_sample_params_from_file(i, file)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:19:07 +00:00
|
|
|
#[cfg(feature = "hooks")]
|
|
|
|
init_hooks();
|
|
|
|
|
2020-09-20 15:27:31 +00:00
|
|
|
Ok(SharedGameState {
|
|
|
|
control_flags: ControlFlags(0),
|
|
|
|
game_flags: bitvec::bitvec![0; 8000],
|
2021-05-02 00:09:54 +00:00
|
|
|
skip_flags: bitvec::bitvec![0; 64],
|
2021-05-02 02:04:52 +00:00
|
|
|
map_flags: bitvec::bitvec![0; 64],
|
2020-09-20 15:27:31 +00:00
|
|
|
fade_state: FadeState::Hidden,
|
2020-12-25 22:39:41 +00:00
|
|
|
game_rng: XorShift::new(0),
|
2021-01-27 18:20:47 +00:00
|
|
|
effect_rng: XorShift::new(123),
|
2021-06-27 01:06:56 +00:00
|
|
|
tile_size: TileSize::Tile16x16,
|
2020-09-20 15:27:31 +00:00
|
|
|
quake_counter: 0,
|
2022-01-13 16:32:33 +00:00
|
|
|
super_quake_counter: 0,
|
2020-09-29 20:19:47 +00:00
|
|
|
teleporter_slots: Vec::with_capacity(8),
|
2020-09-20 15:27:31 +00:00
|
|
|
carets: Vec::with_capacity(32),
|
2020-10-20 20:45:56 +00:00
|
|
|
touch_controls: TouchControls::new(),
|
2021-10-15 14:36:05 +00:00
|
|
|
base_path: base_path.to_owned(),
|
2020-09-20 15:27:31 +00:00
|
|
|
npc_table: NPCTable::new(),
|
2020-10-27 01:05:49 +00:00
|
|
|
npc_super_pos: (0, 0),
|
2021-05-05 12:04:59 +00:00
|
|
|
npc_curly_target: (0, 0),
|
|
|
|
npc_curly_counter: 0,
|
2021-10-08 02:41:31 +00:00
|
|
|
water_level: 0,
|
2020-09-20 15:27:31 +00:00
|
|
|
stages: Vec::with_capacity(96),
|
2020-10-30 22:47:29 +00:00
|
|
|
frame_time: 0.0,
|
2021-02-10 11:53:49 +00:00
|
|
|
debugger: false,
|
2021-01-28 22:33:43 +00:00
|
|
|
scale: 2.0,
|
|
|
|
screen_size: (640.0, 480.0),
|
|
|
|
canvas_size: (320.0, 240.0),
|
2021-06-28 11:07:20 +00:00
|
|
|
preferred_viewport_size: (320.0, 240.0),
|
2020-09-20 15:27:31 +00:00
|
|
|
next_scene: None,
|
|
|
|
textscript_vm: TextScriptVM::new(),
|
2021-10-16 02:37:42 +00:00
|
|
|
creditscript_vm: CreditScriptVM::new(),
|
2021-02-05 09:47:30 +00:00
|
|
|
lightmap_canvas: None,
|
2020-11-02 01:38:39 +00:00
|
|
|
season,
|
|
|
|
constants,
|
|
|
|
font,
|
|
|
|
texture_set,
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
2021-01-01 01:46:01 +00:00
|
|
|
lua: LuaScriptingState::new(),
|
2021-06-20 22:42:10 +00:00
|
|
|
sound_manager,
|
2020-11-02 01:38:39 +00:00
|
|
|
settings,
|
2020-09-20 15:27:31 +00:00
|
|
|
shutdown: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:53:49 +00:00
|
|
|
pub fn process_debug_keys(&mut self, key_code: ScanCode) {
|
|
|
|
match key_code {
|
2021-02-10 20:14:09 +00:00
|
|
|
ScanCode::F3 => self.settings.god_mode = !self.settings.god_mode,
|
|
|
|
ScanCode::F4 => self.settings.infinite_booster = !self.settings.infinite_booster,
|
|
|
|
ScanCode::F5 => self.settings.subpixel_coords = !self.settings.subpixel_coords,
|
2021-03-29 21:19:07 +00:00
|
|
|
ScanCode::F6 => self.settings.motion_interpolation = !self.settings.motion_interpolation,
|
2021-02-10 20:14:09 +00:00
|
|
|
ScanCode::F7 => self.set_speed(1.0),
|
2021-02-10 11:53:49 +00:00
|
|
|
ScanCode::F8 => {
|
|
|
|
if self.settings.speed > 0.2 {
|
|
|
|
self.set_speed(self.settings.speed - 0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ScanCode::F9 => {
|
|
|
|
if self.settings.speed < 3.0 {
|
|
|
|
self.set_speed(self.settings.speed + 0.1);
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 20:14:09 +00:00
|
|
|
ScanCode::F10 => self.settings.debug_outlines = !self.settings.debug_outlines,
|
2021-04-19 19:13:18 +00:00
|
|
|
ScanCode::F12 => self.debugger = !self.debugger,
|
2021-02-10 11:53:49 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 01:38:39 +00:00
|
|
|
pub fn reload_textures(&mut self) {
|
2022-01-17 22:29:30 +00:00
|
|
|
let mut texture_set = TextureSet::new(&self.base_path);
|
2020-11-02 01:38:39 +00:00
|
|
|
|
|
|
|
if self.constants.is_cs_plus {
|
|
|
|
texture_set.apply_seasonal_content(self.season, &self.settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.texture_set = texture_set;
|
|
|
|
}
|
|
|
|
|
2021-02-24 08:28:47 +00:00
|
|
|
pub fn graphics_reset(&mut self) {
|
|
|
|
self.reload_textures();
|
|
|
|
}
|
|
|
|
|
2020-09-22 00:01:55 +00:00
|
|
|
pub fn start_new_game(&mut self, ctx: &mut Context) -> GameResult {
|
2021-06-28 05:27:36 +00:00
|
|
|
self.reset();
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
2021-06-28 11:07:20 +00:00
|
|
|
self.lua.reload_scripts(ctx)?;
|
2021-06-27 21:14:36 +00:00
|
|
|
|
|
|
|
let mut next_scene = GameScene::new(self, ctx, self.constants.game.new_game_stage as usize)?;
|
2020-12-04 17:37:47 +00:00
|
|
|
next_scene.player1.cond.set_alive(true);
|
2021-06-28 11:07:20 +00:00
|
|
|
let (pos_x, pos_y) = self.constants.game.new_game_player_pos;
|
2021-06-27 21:14:36 +00:00
|
|
|
next_scene.player1.x = pos_x as i32 * next_scene.stage.map.tile_size.as_int() * 0x200;
|
|
|
|
next_scene.player1.y = pos_y as i32 * next_scene.stage.map.tile_size.as_int() * 0x200;
|
2021-05-02 02:04:52 +00:00
|
|
|
|
|
|
|
self.reset_map_flags();
|
2020-09-22 00:01:55 +00:00
|
|
|
self.fade_state = FadeState::Hidden;
|
2021-06-27 21:14:36 +00:00
|
|
|
self.textscript_vm.state = TextScriptExecutionState::Running(self.constants.game.new_game_event, 0);
|
2021-01-01 01:46:01 +00:00
|
|
|
|
2020-09-22 00:01:55 +00:00
|
|
|
self.next_scene = Some(Box::new(next_scene));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-30 08:49:30 +00:00
|
|
|
pub fn start_intro(&mut self, ctx: &mut Context) -> GameResult {
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
2021-06-28 11:07:20 +00:00
|
|
|
self.lua.reload_scripts(ctx)?;
|
2021-06-27 21:14:36 +00:00
|
|
|
|
|
|
|
let start_stage_id = self.constants.game.intro_stage as usize;
|
2021-06-20 19:41:09 +00:00
|
|
|
|
|
|
|
if self.stages.len() < start_stage_id {
|
|
|
|
log::warn!("Intro scene out of bounds in stage table, skipping to title...");
|
|
|
|
self.next_scene = Some(Box::new(TitleScene::new()));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:14:36 +00:00
|
|
|
let mut next_scene = GameScene::new(self, ctx, start_stage_id)?;
|
2020-11-28 19:25:51 +00:00
|
|
|
next_scene.player1.cond.set_hidden(true);
|
2021-06-28 11:07:20 +00:00
|
|
|
let (pos_x, pos_y) = self.constants.game.intro_player_pos;
|
2021-06-27 21:14:36 +00:00
|
|
|
next_scene.player1.x = pos_x as i32 * next_scene.stage.map.tile_size.as_int() * 0x200;
|
|
|
|
next_scene.player1.y = pos_y as i32 * next_scene.stage.map.tile_size.as_int() * 0x200;
|
2020-10-30 08:49:30 +00:00
|
|
|
next_scene.intro_mode = true;
|
2021-05-02 02:04:52 +00:00
|
|
|
|
|
|
|
self.reset_map_flags();
|
2020-10-30 08:49:30 +00:00
|
|
|
self.fade_state = FadeState::Hidden;
|
2021-06-27 21:14:36 +00:00
|
|
|
self.textscript_vm.state = TextScriptExecutionState::Running(self.constants.game.intro_event, 0);
|
2021-01-01 01:46:01 +00:00
|
|
|
|
2020-10-30 08:49:30 +00:00
|
|
|
self.next_scene = Some(Box::new(next_scene));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-30 01:29:53 +00:00
|
|
|
pub fn save_game(&mut self, game_scene: &mut GameScene, ctx: &mut Context) -> GameResult {
|
2021-03-29 21:19:07 +00:00
|
|
|
if let Ok(data) = filesystem::open_options(ctx, "/Profile.dat", OpenOptions::new().write(true).create(true)) {
|
2020-10-30 01:29:53 +00:00
|
|
|
let profile = GameProfile::dump(self, game_scene);
|
|
|
|
profile.write_save(data)?;
|
|
|
|
} else {
|
|
|
|
log::warn!("Cannot open save file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-09-22 00:01:55 +00:00
|
|
|
pub fn load_or_start_game(&mut self, ctx: &mut Context) -> GameResult {
|
2020-10-30 01:29:53 +00:00
|
|
|
if let Ok(data) = filesystem::user_open(ctx, "/Profile.dat") {
|
2020-09-22 00:01:55 +00:00
|
|
|
match GameProfile::load_from_save(data) {
|
|
|
|
Ok(profile) => {
|
|
|
|
self.reset();
|
|
|
|
let mut next_scene = GameScene::new(self, ctx, profile.current_map as usize)?;
|
|
|
|
|
|
|
|
profile.apply(self, &mut next_scene, ctx);
|
|
|
|
|
2021-10-15 14:36:05 +00:00
|
|
|
#[cfg(feature = "scripting-lua")]
|
2021-02-10 20:14:09 +00:00
|
|
|
self.lua.reload_scripts(ctx)?;
|
2021-01-01 01:46:01 +00:00
|
|
|
|
2020-09-22 00:01:55 +00:00
|
|
|
self.next_scene = Some(Box::new(next_scene));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("Failed to load save game, starting new one: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log::warn!("No save game found, starting new one...");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.start_new_game(ctx)
|
|
|
|
}
|
|
|
|
|
2020-09-20 15:27:31 +00:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.control_flags.0 = 0;
|
|
|
|
self.game_flags = bitvec::bitvec![0; 8000];
|
|
|
|
self.fade_state = FadeState::Hidden;
|
2020-12-25 22:39:41 +00:00
|
|
|
self.game_rng = XorShift::new(0);
|
2020-09-29 20:19:47 +00:00
|
|
|
self.teleporter_slots.clear();
|
2020-09-20 15:27:31 +00:00
|
|
|
self.quake_counter = 0;
|
|
|
|
self.carets.clear();
|
2020-09-29 20:19:47 +00:00
|
|
|
self.textscript_vm.set_mode(ScriptMode::Map);
|
2020-09-20 15:27:31 +00:00
|
|
|
self.textscript_vm.suspend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_resize(&mut self, ctx: &mut Context) -> GameResult {
|
2021-01-28 22:33:43 +00:00
|
|
|
self.screen_size = graphics::screen_size(ctx);
|
2021-06-28 11:07:20 +00:00
|
|
|
let scale_x = self.screen_size.1.div(self.preferred_viewport_size.1).floor().max(1.0);
|
|
|
|
let scale_y = self.screen_size.0.div(self.preferred_viewport_size.0).floor().max(1.0);
|
|
|
|
|
|
|
|
self.scale = f32::min(scale_x, scale_y);
|
2021-03-29 21:19:07 +00:00
|
|
|
self.canvas_size = (self.screen_size.0 / self.scale, self.screen_size.1 / self.scale);
|
2020-09-20 15:27:31 +00:00
|
|
|
|
2021-02-05 09:47:30 +00:00
|
|
|
let (width, height) = (self.screen_size.0 as u16, self.screen_size.1 as u16);
|
|
|
|
|
|
|
|
// ensure no texture is bound before destroying them.
|
|
|
|
set_render_target(ctx, None)?;
|
|
|
|
self.lightmap_canvas = Some(create_texture_mutable(ctx, width, height)?);
|
|
|
|
|
2020-09-20 15:27:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick_carets(&mut self) {
|
2022-01-17 22:29:30 +00:00
|
|
|
for caret in &mut self.carets {
|
2020-09-20 15:27:31 +00:00
|
|
|
caret.tick(&self.effect_rng, &self.constants);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.carets.retain(|c| !c.is_dead());
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:46:01 +00:00
|
|
|
pub fn create_caret(&mut self, x: i32, y: i32, ctype: CaretType, direct: Direction) {
|
2021-03-29 21:19:07 +00:00
|
|
|
self.carets.push(Caret::new(x, y, ctype, direct, &self.constants));
|
2020-09-20 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 12:22:56 +00:00
|
|
|
pub fn set_speed(&mut self, value: f64) {
|
2021-03-29 21:19:07 +00:00
|
|
|
self.settings.speed = value.clamp(0.1, 3.0);
|
2020-10-30 22:47:29 +00:00
|
|
|
self.frame_time = 0.0;
|
2020-09-20 15:27:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:08:27 +00:00
|
|
|
pub fn current_tps(&self) -> f64 {
|
2021-10-14 07:43:17 +00:00
|
|
|
self.settings.timing_mode.get_tps() as f64 * self.settings.speed
|
2020-11-24 23:08:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:27:31 +00:00
|
|
|
pub fn shutdown(&mut self) {
|
|
|
|
self.shutdown = true;
|
|
|
|
}
|
2021-02-10 20:14:09 +00:00
|
|
|
|
|
|
|
pub fn set_flag(&mut self, id: usize, value: bool) {
|
|
|
|
if id < self.game_flags.len() {
|
|
|
|
self.game_flags.set(id, value);
|
|
|
|
} else {
|
|
|
|
log::warn!("Attempted to set an out-of-bounds flag {}:", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_flag(&self, id: usize) -> bool {
|
|
|
|
if let Some(flag) = self.game_flags.get(id) {
|
|
|
|
*flag
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 00:09:54 +00:00
|
|
|
|
|
|
|
pub fn reset_skip_flags(&mut self) {
|
|
|
|
self.skip_flags = bitvec::bitvec![0; 64];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_skip_flag(&mut self, id: usize, value: bool) {
|
|
|
|
if id < self.skip_flags.len() {
|
|
|
|
self.skip_flags.set(id, value);
|
|
|
|
} else {
|
|
|
|
log::warn!("Attempted to set an out-of-bounds skip flag {}:", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_skip_flag(&self, id: usize) -> bool {
|
|
|
|
if let Some(flag) = self.skip_flags.get(id) {
|
|
|
|
*flag
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 02:04:52 +00:00
|
|
|
|
|
|
|
pub fn reset_map_flags(&mut self) {
|
|
|
|
self.map_flags = bitvec::bitvec![0; 128];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_map_flag(&mut self, id: usize, value: bool) {
|
|
|
|
if id < self.map_flags.len() {
|
|
|
|
self.map_flags.set(id, value);
|
|
|
|
} else {
|
|
|
|
log::warn!("Attempted to set an out-of-bounds map flag {}:", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_map_flag(&self, id: usize) -> bool {
|
|
|
|
if let Some(flag) = self.map_flags.get(id) {
|
|
|
|
*flag
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 15:27:31 +00:00
|
|
|
}
|