Skip to content

Commit

Permalink
Merge pull request #49 from zzhgithub/feature-biome-support
Browse files Browse the repository at this point in the history
Feature biome support
  • Loading branch information
zzhgithub authored Sep 6, 2023
2 parents aa85075 + 4f463c0 commit 543bb1d
Show file tree
Hide file tree
Showing 21 changed files with 398 additions and 20 deletions.
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ egui_extras = "0.22.0"
huffman-compress = { git = "https://github.com/zzhgithub/rust-huffman-compress", branch = "features-serde", features = [
"serde",
] }

noise = { version = "0.8.2" }
# 解决冲突
lock_api = "0.4.10"
codespan-reporting = "0.11.1"
inotify = "0.9.6"

[profile.dev.package.bevy_rapier3d]
opt-level = 3
Expand All @@ -64,13 +68,8 @@ codegen-units = 1
[target.'cfg(target_arch = "x86_64")'.dependencies]
simdnoise = { version = "3.1.6" }

[target.'cfg(target_arch = "aarch64")'.dependencies]
noise = { version = "0.8.2" }
# 解决冲突
lock_api = "0.4.10"
codespan-reporting = "0.11.1"
inotify = "0.9.6"

# [target.'cfg(target_arch = "aarch64")'.dependencies]
# noise = { version = "0.8.2" }

[features]
default = ["server_ui"]
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Just Join (来噻)

![f](pic/f.png)

----

this rep is rebuild from https://github.com/zzhgithub/just_join. The goal is to make the original project support the online multiplayer game

> ~~Ka Le on windows i5 cpu. optimizing!~~
Expand All @@ -18,6 +22,14 @@ For Client
cargo run --release --bin client
```

# Src/lib.rs Const
```rust
// generate map every time when you restart game(use in test mode)
// Before game you should set to flase
pub const CLIENT_MAP_GEN: bool = true;
```


# Controller
- W - forward
- S - backward
Expand All @@ -26,8 +38,8 @@ cargo run --release --bin client
- Space - Jump
- ESC - toggle grab cursor
- T - toggle One/Thrid Person
- E Open composite rules list
- Q throw active toolbar object
- E - Open composite rules list
- Q - throw active toolbar object

# Feature List
- [x] Load unlimited maps
Expand Down Expand Up @@ -58,6 +70,8 @@ cargo run --release --bin client
- [ ] opmtimize UI(Show current toolbar and knapsack)
- [ ] support search
- [ ] add more formula
- [x] more biomes support
- [ ] decorate_terrain
- [ ] task system
- [ ] manor system
- [ ] wind zone
Expand Down
12 changes: 12 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Just Join (来噻)

![f](pic/f.png)

----

此仓库是 https://github.com/zzhgithub/just_join 的网络化重构施工中。敬请期待。


Expand Down Expand Up @@ -30,6 +34,12 @@ cargo run --release --bin client
- E 打开合成表
- Q 丢弃toolbar上激活中的物品

# LIB 中的配置常量
```rust
// 是否每次都重新生成地形
// 游戏前 请设置成false
pub const CLIENT_MAP_GEN: bool = true;
```

# 功能列表

Expand Down Expand Up @@ -61,6 +71,8 @@ cargo run --release --bin client
- [ ] 增强交互(显示当前背包)
- [ ] 支撑合成公式搜索
- [ ] 添加更多游戏内的公式
- [x] 更多群落支撑
- [ ] 装饰群落
- [ ] 任务系统
- [ ] 领地系统
- [ ] 风场
Expand Down
Binary file added assets/textures/干草侧面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/干草地.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/苍翠侧面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/苍翠地.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/f.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ pub const CHUNK_SIZE: i32 = 16;
pub const CHUNK_SIZE_U32: u32 = CHUNK_SIZE as u32;
pub const CHUNK_SIZE_ADD_2_U32: u32 = CHUNK_SIZE_U32 + 2;
// 贴图个数
pub const MAX_TEXTURE_COUNT: usize = 9;
pub const MAX_TEXTURE_COUNT: usize = 13;
// 物体选择半径
pub const TOUCH_RADIUS: f32 = 5.;
pub const CLIENT_DEBUG: bool = false;
pub const CLIENT_FPS: bool = false;
// 是否每次都重新生成地形
pub const CLIENT_MAP_GEN: bool = true;

// 最大物品堆放
pub const MAX_STAFF_FIXED: usize = 999;
Expand Down
49 changes: 49 additions & 0 deletions src/voxel_world/biomes/basic_land.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 基础大陆

use ndshape::ConstShape;

use crate::voxel_world::voxel::{Grass, Soli, Sown, Stone, Voxel, VoxelMaterial};

use super::{BiomesGenerator, SampleShape, MOUNTAIN_LEVEL, SEE_LEVEL, SNOW_LEVEL};

// 基础大陆
// 1. 雪顶
// 2. 石块
// 3. 草
// 4. 泥土 5
pub struct BasicLandBiomes;

impl BiomesGenerator for BasicLandBiomes {
fn gen_land_with_info(
&self,
_chunk_key: crate::voxel_world::chunk::ChunkKey,
voxels: &mut Vec<Voxel>,
chunk_index: u32,
_plane_index: u32,
height: f32,
xyz: [u32; 3],
) {
let [x, y, z] = xyz;
if height >= SNOW_LEVEL {
// 雪线之上
voxels[chunk_index as usize] = Sown::into_voxel();
if y > 0 {
// 雪之下雪乃
let under_sown = SampleShape::linearize([x, y - 1, z]);
voxels[under_sown as usize] = Sown::into_voxel();
}
} else if height >= MOUNTAIN_LEVEL {
voxels[chunk_index as usize] = Stone::into_voxel();
// 一层实体
} else if height >= SEE_LEVEL {
// 一层 草 5层的土
voxels[chunk_index as usize] = Grass::into_voxel();
for y_offset in 1..=5 {
if y > y_offset {
let under_grass = SampleShape::linearize([x, y - y_offset, z]);
voxels[under_grass as usize] = Soli::into_voxel();
}
}
}
}
}
44 changes: 44 additions & 0 deletions src/voxel_world/biomes/bule_land.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 苍翠大陆

use ndshape::ConstShape;

use crate::voxel_world::voxel::{BuleGrass, Soli, Sown, Stone, VoxelMaterial};

use super::{BiomesGenerator, SampleShape, MOUNTAIN_LEVEL, SEE_LEVEL, SNOW_LEVEL};

pub struct BuleLandBoimes;

impl BiomesGenerator for BuleLandBoimes {
fn gen_land_with_info(
&self,
_chunk_key: crate::voxel_world::chunk::ChunkKey,
voxels: &mut Vec<crate::voxel_world::voxel::Voxel>,
chunk_index: u32,
_plane_index: u32,
height: f32,
xyz: [u32; 3],
) {
let [x, y, z] = xyz;
if height >= SNOW_LEVEL {
// 雪线之上
voxels[chunk_index as usize] = Sown::into_voxel();
if y > 0 {
// 雪之下雪乃
let under_sown = SampleShape::linearize([x, y - 1, z]);
voxels[under_sown as usize] = Sown::into_voxel();
}
} else if height >= MOUNTAIN_LEVEL + 6.0 {
voxels[chunk_index as usize] = Stone::into_voxel();
// 一层实体
} else if height >= SEE_LEVEL {
// 一层 草 5层的土
voxels[chunk_index as usize] = BuleGrass::into_voxel();
for y_offset in 1..=5 {
if y > y_offset {
let under_grass = SampleShape::linearize([x, y - y_offset, z]);
voxels[under_grass as usize] = Soli::into_voxel();
}
}
}
}
}
44 changes: 44 additions & 0 deletions src/voxel_world/biomes/dry_land.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 干燥大陆

use ndshape::ConstShape;

use crate::voxel_world::voxel::{DryGrass, Soli, Sown, Stone, VoxelMaterial};

use super::{BiomesGenerator, SampleShape, MOUNTAIN_LEVEL, SEE_LEVEL, SNOW_LEVEL};

pub struct DryLandBiomes;

impl BiomesGenerator for DryLandBiomes {
fn gen_land_with_info(
&self,
_chunk_key: crate::voxel_world::chunk::ChunkKey,
voxels: &mut Vec<crate::voxel_world::voxel::Voxel>,
chunk_index: u32,
_plane_index: u32,
height: f32,
xyz: [u32; 3],
) {
let [x, y, z] = xyz;
if height >= SNOW_LEVEL {
// 雪线之上
voxels[chunk_index as usize] = Sown::into_voxel();
if y > 0 {
// 雪之下雪乃
let under_sown = SampleShape::linearize([x, y - 1, z]);
voxels[under_sown as usize] = Sown::into_voxel();
}
} else if height >= MOUNTAIN_LEVEL + 2.0 {
voxels[chunk_index as usize] = Stone::into_voxel();
// 一层实体
} else if height >= SEE_LEVEL {
// 一层 草 5层的土
voxels[chunk_index as usize] = DryGrass::into_voxel();
for y_offset in 1..=5 {
if y > y_offset {
let under_grass = SampleShape::linearize([x, y - y_offset, z]);
voxels[under_grass as usize] = Soli::into_voxel();
}
}
}
}
}
130 changes: 130 additions & 0 deletions src/voxel_world/biomes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use ndshape::{ConstShape, ConstShape2u32, ConstShape3u32};
use noise::{
core::worley::{distance_functions::euclidean, ReturnType},
utils::NoiseMapBuilder,
Worley,
};

use crate::{CHUNK_SIZE, CHUNK_SIZE_U32};

use self::{
basic_land::BasicLandBiomes, bule_land::BuleLandBoimes, dry_land::DryLandBiomes,
sand_land::SandLandBiomes, snow_land::SnowLandBiomes,
};

use super::{chunk::ChunkKey, voxel::Voxel};

pub mod basic_land;
pub mod bule_land;
pub mod dry_land;
pub mod sand_land;
pub mod snow_land;

pub type SampleShape = ConstShape3u32<CHUNK_SIZE_U32, CHUNK_SIZE_U32, CHUNK_SIZE_U32>;
pub type PanleShap = ConstShape2u32<CHUNK_SIZE_U32, CHUNK_SIZE_U32>;

// 处理 生物群落
pub fn biomes_generate(
chunk_key: ChunkKey,
seed: i32,
suface_index: Vec<u32>,
voxels: &mut Vec<Voxel>,
) {
if suface_index.len() == 0 {
return;
}
// 生成噪声
let noise = biomes_noise(chunk_key, seed);

for index in suface_index {
// 由噪声生产的特征值
let [x, _, z] = SampleShape::delinearize(index);
let index_2d = PanleShap::linearize([x, z]);
let atrr = noise[index_2d as usize];
let generator = get_generator_by_atrr(atrr);
generator.gen_land(chunk_key.clone(), voxels, index, index_2d);
// fixme: 这里要记录对于其他方块的影响
}
}

// 获取不同的生成器
fn get_generator_by_atrr(data: f32) -> Box<dyn BiomesGenerator> {
if data < 0.1 {
return BasicLandBiomes.into_boxed_generator();
} else if data < 0.4 {
return DryLandBiomes.into_boxed_generator();
} else if data < 0.6 {
return SnowLandBiomes.into_boxed_generator();
} else if data < 0.8 {
return SandLandBiomes.into_boxed_generator();
} else {
return BuleLandBoimes.into_boxed_generator();
}
}

pub fn biomes_noise(chunk_key: ChunkKey, seed: i32) -> Vec<f32> {
let noise = Worley::new(seed as u32)
.set_distance_function(euclidean)
.set_return_type(ReturnType::Value)
.set_frequency(0.008);

let x_offset = (chunk_key.0.x * CHUNK_SIZE) as f64;
let z_offset = (chunk_key.0.z * CHUNK_SIZE) as f64;

noise::utils::PlaneMapBuilder::<_, 2>::new(noise)
.set_size(CHUNK_SIZE as usize, CHUNK_SIZE as usize)
.set_x_bounds(x_offset, x_offset + CHUNK_SIZE as f64)
.set_y_bounds(z_offset, z_offset + CHUNK_SIZE as f64)
.build()
.into_iter()
.map(|x| x as f32)
.collect()
}

pub trait BiomesGenerator: 'static + Sync + Send {
fn gen_land_with_info(
&self,
chunk_key: ChunkKey,
voxels: &mut Vec<Voxel>,
chunk_index: u32,
plane_index: u32,
height: f32,
xyz: [u32; 3],
);

fn gen_land(
&self,
chunk_key: ChunkKey,
voxels: &mut Vec<Voxel>,
chunk_index: u32,
plane_index: u32,
) {
let base_y: f32 = (chunk_key.0.y * CHUNK_SIZE) as f32;
let [x, y, z] = SampleShape::delinearize(chunk_index);
let height = base_y + y as f32;
self.gen_land_with_info(
chunk_key,
voxels,
chunk_index,
plane_index,
height,
[x, y, z],
);
}
}

pub trait IntoBoxedTerrainGenerator: BiomesGenerator + Sized {
fn into_boxed_generator(self) -> Box<Self>;
}

impl<T: BiomesGenerator> IntoBoxedTerrainGenerator for T {
fn into_boxed_generator(self) -> Box<Self> {
Box::new(self)
}
}
// 海平面
pub const SEE_LEVEL: f32 = -60. + 76.;
// 山峰线
pub const MOUNTAIN_LEVEL: f32 = -60. + 100.;
// 雪线
pub const SNOW_LEVEL: f32 = -60. + 100.;
Loading

0 comments on commit 543bb1d

Please sign in to comment.