1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2025-11-29 15:56:53 +00:00

STC command

This commit is contained in:
dawnDus 2022-01-21 16:40:37 -05:00 committed by alula
parent af1f9f5d89
commit eadeedae6b
2 changed files with 60 additions and 13 deletions

View file

@ -1,4 +1,4 @@
use byteorder::{ReadBytesExt, LE}; use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use crate::common::Rect; use crate::common::Rect;
use crate::components::draw_common::{draw_number, draw_number_zeros, Alignment}; use crate::components::draw_common::{draw_number, draw_number_zeros, Alignment};
@ -7,7 +7,9 @@ use crate::frame::Frame;
use crate::framework::context::Context; use crate::framework::context::Context;
use crate::framework::error::GameResult; use crate::framework::error::GameResult;
use crate::framework::filesystem; use crate::framework::filesystem;
use crate::framework::vfs::OpenOptions;
use crate::player::Player; use crate::player::Player;
use crate::rng::RNG;
use crate::shared_game_state::{SharedGameState, TimingMode}; use crate::shared_game_state::{SharedGameState, TimingMode};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -21,9 +23,9 @@ impl NikumaruCounter {
NikumaruCounter { tick: 0, shown: false } NikumaruCounter { tick: 0, shown: false }
} }
fn load_saved_time(&mut self, ctx: &mut Context) -> GameResult<u32> { fn load_time(&mut self, ctx: &mut Context) -> GameResult<u32> {
if let Ok(mut data) = filesystem::user_open(ctx, "/290.rec") { if let Ok(mut data) = filesystem::user_open(ctx, "/290.rec") {
let mut ticks: [u32; 4] = [0, 0, 0, 0]; let mut ticks: [u32; 4] = [0; 4];
for iter in 0..=3 { for iter in 0..=3 {
ticks[iter] = data.read_u32::<LE>()?; ticks[iter] = data.read_u32::<LE>()?;
@ -34,10 +36,10 @@ impl NikumaruCounter {
for iter in 0..=3 { for iter in 0..=3 {
ticks[iter] = u32::from_le_bytes([ ticks[iter] = u32::from_le_bytes([
ticks[iter].to_le_bytes()[0] - random_list[iter], ticks[iter].to_le_bytes()[0].wrapping_sub(random_list[iter]),
ticks[iter].to_le_bytes()[1] - random_list[iter], ticks[iter].to_le_bytes()[1].wrapping_sub(random_list[iter]),
ticks[iter].to_le_bytes()[2] - random_list[iter], ticks[iter].to_le_bytes()[2].wrapping_sub(random_list[iter]),
ticks[iter].to_le_bytes()[3] - random_list[iter] / 2, ticks[iter].to_le_bytes()[3].wrapping_sub(random_list[iter] / 2),
]); ]);
} }
@ -45,18 +47,51 @@ impl NikumaruCounter {
return Ok(ticks[0]); return Ok(ticks[0]);
} }
} else { } else {
log::warn!("Cannot open 290.rec."); log::warn!("Failed to open 290 record.");
} }
Ok(0) Ok(0)
} }
fn save_time(&mut self, new_time: u32, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
if let Ok(mut data) = filesystem::open_options(ctx, "/290.rec", OpenOptions::new().write(true).create(true)) {
let mut ticks: [u32; 4] = [new_time; 4];
let mut random_list: [u8; 4] = [0; 4];
for iter in 0..=3 {
random_list[iter] = state.game_rng.range(0..250) as u8 + iter as u8;
ticks[iter] = u32::from_le_bytes([
ticks[iter].to_le_bytes()[0].wrapping_add(random_list[iter]),
ticks[iter].to_le_bytes()[1].wrapping_add(random_list[iter]),
ticks[iter].to_le_bytes()[2].wrapping_add(random_list[iter]),
ticks[iter].to_le_bytes()[3].wrapping_add(random_list[iter] / 2),
]);
data.write_u32::<LE>(ticks[iter])?;
}
data.write_u32::<LE>(u32::from_le_bytes(random_list))?;
} else {
log::warn!("Failed to write 290 record.");
}
Ok(())
}
pub fn load_counter(&mut self, ctx: &mut Context) -> GameResult { pub fn load_counter(&mut self, ctx: &mut Context) -> GameResult {
self.tick = self.load_saved_time(ctx)? as usize; self.tick = self.load_time(ctx)? as usize;
if self.tick > 0 { if self.tick > 0 {
self.shown = true; self.shown = true;
} }
Ok(()) Ok(())
} }
pub fn save_counter(&mut self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
let old_record = self.load_time(ctx)? as usize;
if self.tick < old_record || old_record == 0 {
self.save_time(self.tick as u32, state, ctx)?;
}
Ok(())
}
} }
impl GameEntity<&Player> for NikumaruCounter { impl GameEntity<&Player> for NikumaruCounter {

View file

@ -11,8 +11,8 @@ use std::rc::Rc;
use num_traits::{clamp, FromPrimitive}; use num_traits::{clamp, FromPrimitive};
use crate::bitfield; use crate::bitfield;
use crate::common::{Direction, FadeDirection, FadeState, Rect};
use crate::common::Direction::{Left, Right}; use crate::common::Direction::{Left, Right};
use crate::common::{Direction, FadeDirection, FadeState, Rect};
use crate::engine_constants::EngineConstants; use crate::engine_constants::EngineConstants;
use crate::entity::GameEntity; use crate::entity::GameEntity;
use crate::frame::UpdateTarget; use crate::frame::UpdateTarget;
@ -396,7 +396,13 @@ impl TextScriptVM {
state.textscript_vm.state = if !new_line { state.textscript_vm.state = if !new_line {
TextScriptExecutionState::Msg(event, cursor.position() as u32, remaining - 1, ticks) TextScriptExecutionState::Msg(event, cursor.position() as u32, remaining - 1, ticks)
} else { } else {
TextScriptExecutionState::MsgNewLine(event, cursor.position() as u32, remaining - 1, ticks, 4) TextScriptExecutionState::MsgNewLine(
event,
cursor.position() as u32,
remaining - 1,
ticks,
4,
)
}; };
} else { } else {
state.textscript_vm.state = state.textscript_vm.state =
@ -419,7 +425,8 @@ impl TextScriptVM {
state.textscript_vm.line_2.append(&mut state.textscript_vm.line_3); state.textscript_vm.line_2.append(&mut state.textscript_vm.line_3);
state.textscript_vm.state = TextScriptExecutionState::Msg(event, ip, remaining, ticks); state.textscript_vm.state = TextScriptExecutionState::Msg(event, ip, remaining, ticks);
} else { } else {
state.textscript_vm.state = TextScriptExecutionState::MsgNewLine(event, ip, remaining, ticks, counter); state.textscript_vm.state =
TextScriptExecutionState::MsgNewLine(event, ip, remaining, ticks, counter);
} }
break; break;
} }
@ -1650,9 +1657,14 @@ impl TextScriptVM {
mode != 0, mode != 0,
); );
} }
TSCOpCode::STC => {
game_scene.nikumaru.save_counter(state, ctx)?;
exec_state = TextScriptExecutionState::Running(event, cursor.position() as u32);
}
// unimplemented opcodes // unimplemented opcodes
// Zero operands // Zero operands
TSCOpCode::KE2 | TSCOpCode::MLP | TSCOpCode::FR2 | TSCOpCode::STC | TSCOpCode::HM2 => { TSCOpCode::KE2 | TSCOpCode::MLP | TSCOpCode::FR2 | TSCOpCode::HM2 => {
log::warn!("unimplemented opcode: {:?}", op); log::warn!("unimplemented opcode: {:?}", op);
exec_state = TextScriptExecutionState::Running(event, cursor.position() as u32); exec_state = TextScriptExecutionState::Running(event, cursor.position() as u32);