Skip to content

Commit

Permalink
feat: group strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Grizelj committed Nov 6, 2024
1 parent 9117d0c commit 194cf69
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
45 changes: 43 additions & 2 deletions src/gui/maps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dioxus::prelude::*;

use crate::gui::checkbox;
use crate::json::{Preset, ShopItems};
use crate::json::{GroupStrategy, Preset, ShopItems};

#[component]
pub fn maps() -> Element {
Expand All @@ -14,6 +14,9 @@ pub fn maps() -> Element {
let backgrounds = read_state.randomizer.maps.backgrounds;
let fight_backgrounds = read_state.randomizer.maps.fight_backgrounds;
let item_boxes = read_state.randomizer.maps.item_boxes;

let selected_group_strategy = read_state.randomizer.maps.group_strategy;

let selected = read_state.randomizer.maps.item_boxes_items_only.clone();

rsx! {
Expand Down Expand Up @@ -61,8 +64,8 @@ pub fn maps() -> Element {
}
},
div {
class: "left",
div {
class: "left",
checkbox::checkbox {
label: "Fight Backgrounds",
id: "maps.fight_backgrounds",
Expand All @@ -73,6 +76,44 @@ pub fn maps() -> Element {
state.write().randomizer.maps.fight_backgrounds = x;
}
},
},
div {
class: "tooltip",
span {
class: "tooltiptext",
style: "width: 200px",
"None => fully random",
br {},
"Map => group based on overworld map",
br {},
"Party => group based on party id"
},
label {
r#for: "maps.group_strategy",
"Group Strategy"
},
select {
id: "maps.group_strategy",
disabled: !enabled || !fight_backgrounds,
onchange: move |x: Event<FormData>| {
state.write().randomizer.maps.group_strategy = GroupStrategy::from(x.data.value().parse::<u8>().unwrap_or(0));
},
option {
value: "0",
selected: selected_group_strategy == GroupStrategy::None,
"None"
},
option {
value: "1",
selected: selected_group_strategy == GroupStrategy::Map,
"Map"
},
option {
value: "2",
selected: selected_group_strategy == GroupStrategy::Party,
"Party"
},
}
}
},
div {
Expand Down
22 changes: 22 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct Maps {
pub item_boxes_items_only: ShopItems,
#[serde(default = "default_bool_true")]
pub fight_backgrounds: bool,
#[serde(default = "GroupStrategy::default")]
pub group_strategy: GroupStrategy,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -339,6 +341,26 @@ impl From<u8> for TNTStrategy {
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
pub enum GroupStrategy {
None,
Map,
#[default]
Party,
}

impl From<u8> for GroupStrategy {
fn from(value: u8) -> Self {
match value {
0 => GroupStrategy::None,
1 => GroupStrategy::Map,
_ => GroupStrategy::Party,
}
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
#[derive(Default)]
Expand Down
53 changes: 49 additions & 4 deletions src/rand/maps.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{BTreeSet, HashSet};
use std::collections::{BTreeSet, HashMap};

use crate::{rand::Objects, util};
use crate::{json::GroupStrategy, rand::Objects, util};
use anyhow::Context;
use rand_xoshiro::rand_core::RngCore;
use rand_xoshiro::Xoshiro256StarStar;
Expand Down Expand Up @@ -58,13 +58,13 @@ pub fn patch(
}

if maps.fight_backgrounds {
random_fight_backgrounds(objects, rng);
random_fight_backgrounds(preset, objects, rng);
}

Ok(())
}

fn random_fight_backgrounds(objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
fn random_fight_backgrounds_ungrouped(objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
for map in &mut objects.map_objects {
if let Some(se_obj) = &mut map.stage_encounters {
for opt in &mut se_obj.stage_encounters {
Expand All @@ -78,6 +78,51 @@ fn random_fight_backgrounds(objects: &mut Objects, rng: &mut Xoshiro256StarStar)
}
}

fn random_fight_backgrounds_grouped(
preset: &Randomizer,
objects: &mut Objects,
rng: &mut Xoshiro256StarStar,
) {
let mut generated = HashMap::new();

for map in &mut objects.map_objects {
if let Some(se_obj) = &mut map.stage_encounters {
for opt in &mut se_obj.stage_encounters {
if let Some(encounters_obj) = opt {
for encounter in &mut encounters_obj.modified {
encounter.stage = match generated.get(&encounter.team_id) {
Some(x) => *x,
None => {
let nv = rng.next_u32() % 0x39;

generated.insert(encounter.team_id, rng.next_u32() % 0x39);

nv
}
};
}
}
}
}

if preset.maps.group_strategy == GroupStrategy::Map {
generated.clear();
}
}
}

fn random_fight_backgrounds(
preset: &Randomizer,
objects: &mut Objects,
rng: &mut Xoshiro256StarStar,
) {
if preset.maps.group_strategy == GroupStrategy::None {
random_fight_backgrounds_ungrouped(objects, rng);
} else {
random_fight_backgrounds_grouped(preset, objects, rng);
}
}

fn color(objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
for map in &mut objects.map_objects {
if let Some(color_object) = &mut map.map_color {
Expand Down

0 comments on commit 194cf69

Please sign in to comment.