Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed config seed #516

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion feather/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn init_world_source(game: &mut Game, config: &Config) {
// world otherwise. This is a placeholder:
// we don't have proper world generation yet.

let seed = 42; // FIXME: load from the level file
let seed = worldgen::seed_from_string(&config.world.seed);

let generator: Arc<dyn WorldGenerator> = match &config.world.generator[..] {
"flat" => Arc::new(SuperflatWorldGenerator::new(
Expand Down
18 changes: 18 additions & 0 deletions feather/worldgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,18 @@ impl BiomeGenerator for StaticBiomeGenerator {
}
}

pub fn seed_from_string(s: &str) -> u64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should maybe be an i64, such that negative seeds also work as expected?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere in feather seeds are defined by u64

Copy link
Member

@Defman Defman Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it should be parsed as i64 and then cast to u64. I thin that's how vanilla Minecraft works.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be i64 everywhere and only be casted when passing it to RNGs, it would be more convenient for feather or plugin developers who expect -104019040 seed to be negative, and for end users that expect /seed (or seed value in logs / crash reports) to be the same as in config.toml.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the above comment, @ELginas do you want to make this change or should I merge this? We can then create a new issue to change the seed type from u64 to i64.

s.parse().unwrap_or_else(|_| {
if s.is_empty() {
rand::random()
} else {
s.chars()
.fold(0i32, |val, ch| val.wrapping_mul(31).wrapping_add(ch as i32))
as i64
}
}) as u64
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -468,4 +480,10 @@ mod tests {
assert_eq!(biomes.get_at_block(-1, 0, -1), Biome::Plains);
assert_eq!(biomes.get_at_block(-1, 0, 0), Biome::BirchForest);
}

#[test]
fn test_seed_from_config() {
assert_eq!(seed_from_string("42"), 42);
assert_eq!(seed_from_string("Hello"), 69609650);
}
}