Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
make p1 and p2 configs separate
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusKelvin committed Nov 16, 2019
1 parent 2d0d71f commit d1123f2
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
13 changes: 7 additions & 6 deletions battle/src/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ pub struct Battle {

impl Battle {
pub fn new(
config: GameConfig,
p1_config: GameConfig, p2_config: GameConfig,
p1_seed: <Pcg64Mcg as SeedableRng>::Seed,
p2_seed: <Pcg64Mcg as SeedableRng>::Seed,
garbage_seed: <Pcg64Mcg as SeedableRng>::Seed
) -> Self {
let mut p1_rng = Pcg64Mcg::from_seed(p1_seed);
let mut p2_rng = Pcg64Mcg::from_seed(p2_seed);
let garbage_rng = Pcg64Mcg::from_seed(garbage_seed);
let player_1 = Game::new(config, &mut p1_rng);
let player_2 = Game::new(config, &mut p2_rng);
let player_1 = Game::new(p1_config, &mut p1_rng);
let player_2 = Game::new(p2_config, &mut p2_rng);
Battle {
replay: Replay {
p1_name: String::new(), p2_name: String::new(),
config, p1_seed, p2_seed, garbage_seed,
p1_config, p2_config, p1_seed, p2_seed, garbage_seed,
updates: VecDeque::new()
},
player_1, player_2,
p1_rng, p2_rng, garbage_rng,
time: 0,
margin_time: config.margin_time,
margin_time: p1_config.margin_time,
multiplier: 1.0,
}
}
Expand Down Expand Up @@ -102,6 +102,7 @@ pub struct Replay {
pub p1_seed: <Pcg64Mcg as SeedableRng>::Seed,
pub p2_seed: <Pcg64Mcg as SeedableRng>::Seed,
pub garbage_seed: <Pcg64Mcg as SeedableRng>::Seed,
pub config: GameConfig,
pub p1_config: GameConfig,
pub p2_config: GameConfig,
pub updates: VecDeque<(Controller, Controller)>
}
13 changes: 8 additions & 5 deletions battle/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl Game {

match self.state {
GameState::SpawnDelay(0) => {
let mut events = vec![];
if self.config.spawn_delay == 0 {
events.push(Event::FrameBeforePieceSpawns);
}
let next_piece = self.board.advance_queue().unwrap();
let new_piece = self.board.generate_next_piece(piece_rng);
self.board.add_next_piece(new_piece);
Expand All @@ -136,14 +140,13 @@ impl Game {
});
let mut ghost = spawned;
ghost.sonic_drop(&self.board);
vec![
Event::PieceSpawned { new_in_queue: new_piece },
Event::PieceFalling(spawned, ghost)
]
events.push(Event::PieceSpawned { new_in_queue: new_piece });
events.push(Event::PieceFalling(spawned, ghost));
} else {
self.state = GameState::GameOver;
vec![Event::GameOver]
events.push(Event::GameOver);
}
events
}
GameState::SpawnDelay(ref mut delay) => {
*delay -= 1;
Expand Down
2 changes: 1 addition & 1 deletion compare/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct BotInput<E: Evaluator> {
bot: bot::BotState<E>
}

const THINK_AMOUNT: Duration = Duration::from_millis(2);
const THINK_AMOUNT: Duration = Duration::from_millis(4);

impl<E: Evaluator> BotInput<E> {
pub fn new(board: Board, eval: E) -> Self {
Expand Down
7 changes: 4 additions & 3 deletions compare/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {

let (send, recv) = std::sync::mpsc::channel();

for _ in 0..2 {
for _ in 0..12 {
let p1_eval = p1_eval.clone();
let p2_eval = p2_eval.clone();
let send = send.clone();
Expand All @@ -32,7 +32,7 @@ fn main() {
let mut p1_wins = 0;
let mut p2_wins = 0;

let games = 3000;
let games = 5000;

while p1_wins + p2_wins < games {
match recv.recv() {
Expand All @@ -59,7 +59,8 @@ fn main() {

fn do_battle(p1: impl Evaluator, p2: impl Evaluator) -> (InfoReplay, bool) {
let mut battle = Battle::new(
Default::default(), thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
Default::default(), Default::default(),
thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
);

battle.replay.p1_name = format!("Cold Clear\n{}", p1.name());
Expand Down
11 changes: 6 additions & 5 deletions gui/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub struct LocalGame<'a> {
p2_info_updates: VecDeque<Option<bot::Info>>,
state: State,
resources: &'a mut Resources,
config: GameConfig,
p1_config: GameConfig,
p2_config: GameConfig,
gamepad: Option<GamepadId>
}

Expand All @@ -43,10 +44,10 @@ impl<'a> LocalGame<'a> {
resources: &'a mut Resources,
p1: Box<InputFactory>,
p2: Box<InputFactory>,
config: GameConfig
p1_config: GameConfig, p2_config: GameConfig
) -> Self {
let mut battle = Battle::new(
config, thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
p1_config, p2_config, thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
);
let (p1_input, p1_name) = p1(battle.player_1.board.to_compressed());
let (p2_input, p2_name) = p2(battle.player_2.board.to_compressed());
Expand All @@ -64,7 +65,7 @@ impl<'a> LocalGame<'a> {
p2_info_updates: VecDeque::new(),
state: State::Starting(180),
resources,
config,
p1_config, p2_config,
gamepad: None
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ impl EventHandler for LocalGame<'_> {
while timer::check_update_time(ctx, 60) {}

self.battle = Battle::new(
self.config,
self.p1_config, self.p2_config,
thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
);

Expand Down
7 changes: 4 additions & 3 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn main() {
use bot::evaluation::{ self, Evaluator };
use crate::input::BotInput;
let Options {
controls, bot_options, bot_config, config
controls, bot_options, bot_config, p1_config, p2_config
} = match read_options() {
Ok(options) => options,
Err(e) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ fn main() {
bot_config.clone()
))), name)
}),
config
p1_config, p2_config
);
event::run(&mut ctx, &mut events, &mut local_game).unwrap();
}
Expand All @@ -163,7 +163,8 @@ fn read_options() -> Result<Options, Box<dyn std::error::Error>> {
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
struct Options {
controls: UserInput,
config: GameConfig,
p1_config: GameConfig,
p2_config: GameConfig,
bot_config: bot::evaluation::Standard,
bot_options: bot::Options
}
8 changes: 6 additions & 2 deletions gui/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl<'a, P: AsRef<std::path::Path> + Clone> ReplayGame<'a, P> {
deflate::Decoder::new(std::fs::File::open(file.clone()).unwrap())
).unwrap();
let battle = Battle::new(
replay.config, replay.p1_seed, replay.p2_seed, replay.garbage_seed
replay.p1_config, replay.p2_config,
replay.p1_seed, replay.p2_seed,
replay.garbage_seed
);
ReplayGame {
gui: Gui::new(&battle, replay.p1_name, replay.p2_name),
Expand Down Expand Up @@ -75,7 +77,9 @@ impl<P: AsRef<std::path::Path> + Clone> EventHandler for ReplayGame<'_, P> {
}
let InfoReplay { replay, p1_info_updates, p2_info_updates } = replay;
let battle = Battle::new(
replay.config, replay.p1_seed, replay.p2_seed, replay.garbage_seed
replay.p1_config, replay.p2_config,
replay.p1_seed, replay.p2_seed,
replay.garbage_seed
);
self.gui = Gui::new(&battle, replay.p1_name, replay.p2_name);
self.battle = battle;
Expand Down
3 changes: 2 additions & 1 deletion optimizer/src/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ impl<E: Evaluator> BotInput<E> {

pub fn do_battle(p1: impl Evaluator, p2: impl Evaluator) -> Option<(InfoReplay, bool)> {
let mut battle = Battle::new(
Default::default(), thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
Default::default(), Default::default(),
thread_rng().gen(), thread_rng().gen(), thread_rng().gen()
);

battle.replay.p1_name = format!("Cold Clear\n{}", p1.name());
Expand Down

0 comments on commit d1123f2

Please sign in to comment.