Skip to content

Commit db78c9a

Browse files
[RS] formatted source code with 'cargo fmt'
1 parent 51a52a1 commit db78c9a

File tree

10 files changed

+275
-1199
lines changed

10 files changed

+275
-1199
lines changed

method-rs/coding_style.txt

Lines changed: 0 additions & 909 deletions
This file was deleted.

method-rs/src/chunk_io.rs

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ use std::fs::{self, File};
22
use std::io::{BufRead, BufReader, Write};
33
use std::path::Path;
44

5-
pub fn print_chunk(
6-
chunk: &Vec<Vec<Vec<u8>>>,
7-
width: usize,
8-
height: usize,
9-
depth: usize
10-
) {
5+
pub fn print_chunk(chunk: &Vec<Vec<Vec<u8>>>, width: usize, height: usize, depth: usize) {
116
for x in 0..width {
127
for y in (0..height).rev() {
138
for z in 0..depth {
@@ -18,11 +13,9 @@ pub fn print_chunk(
1813
}
1914
}
2015

21-
2216
pub fn fread_chunk(file_in: &str) -> (Vec<Vec<Vec<u8>>>, usize, usize, usize) {
2317
// Read the whole file as a string
24-
let input = fs::read_to_string(file_in)
25-
.expect("Failed to read params file");
18+
let input = fs::read_to_string(file_in).expect("Failed to read params file");
2619

2720
let mut lines = input.lines();
2821

@@ -77,53 +70,58 @@ pub fn fread_chunk(file_in: &str) -> (Vec<Vec<Vec<u8>>>, usize, usize, usize) {
7770
(chunk, width, height, depth)
7871
}
7972

80-
8173
pub fn fread_block_coordinates(file_params: &str) -> (isize, isize, isize, u8) {
8274
let file = File::open(file_params).unwrap();
8375
let mut reader = BufReader::new(file);
8476

8577
let mut line = String::new();
86-
reader.read_line(&mut line).unwrap();
78+
reader.read_line(&mut line).unwrap();
8779

8880
// Split line by whitespace
8981
let mut parts = line.trim().split_whitespace();
9082

91-
let x: isize = parts.next().expect("Missing x")
92-
.parse().expect("Invalid x");
93-
let y: isize = parts.next().expect("Missing y")
94-
.parse().expect("Invalid y");
95-
let z: isize = parts.next().expect("Missing z")
96-
.parse().expect("Invalid z");
83+
let x: isize = parts.next().expect("Missing x").parse().expect("Invalid x");
84+
let y: isize = parts.next().expect("Missing y").parse().expect("Invalid y");
85+
let z: isize = parts.next().expect("Missing z").parse().expect("Invalid z");
9786

98-
let block: u8 = parts.next().expect("Missing block")
99-
.parse().expect("Invalid block");
87+
let block: u8 = parts
88+
.next()
89+
.expect("Missing block")
90+
.parse()
91+
.expect("Invalid block");
10092

10193
(x, y, z, block)
10294
}
10395

104-
10596
pub fn fread_chunk_sizes(file_params: &str) -> (usize, usize, usize) {
10697
let file = File::open(file_params).unwrap();
10798
let mut reader = BufReader::new(file);
10899

109100
let mut line = String::new();
110-
reader.read_line(&mut line).unwrap();
101+
reader.read_line(&mut line).unwrap();
111102

112103
// Split line by whitespace
113104
let mut parts = line.trim().split_whitespace();
114105

115-
let width: usize = parts.next().expect("Missing width")
116-
.parse().expect("Invalid width");
117-
let height: usize = parts.next().expect("Missing height")
118-
.parse().expect("Invalid height");
119-
let depth: usize = parts.next().expect("Missing depth")
120-
.parse().expect("Invalid depth");
121-
106+
let width: usize = parts
107+
.next()
108+
.expect("Missing width")
109+
.parse()
110+
.expect("Invalid width");
111+
let height: usize = parts
112+
.next()
113+
.expect("Missing height")
114+
.parse()
115+
.expect("Invalid height");
116+
let depth: usize = parts
117+
.next()
118+
.expect("Missing depth")
119+
.parse()
120+
.expect("Invalid depth");
122121

123122
(width, height, depth)
124123
}
125124

126-
127125
fn create_file_out(file_out: &str) -> File {
128126
let path = Path::new(file_out);
129127

@@ -135,16 +133,15 @@ fn create_file_out(file_out: &str) -> File {
135133
));
136134
}
137135

138-
File::create(path)
139-
.expect(&format!("[ERROR] cannot create {:?} output file", file_out))
136+
File::create(path).expect(&format!("[ERROR] cannot create {:?} output file", file_out))
140137
}
141138

142139
pub fn fwrite_chunk(
143140
file_out: &str,
144141
chunk: &Vec<Vec<Vec<u8>>>,
145142
width: usize,
146143
height: usize,
147-
depth: usize
144+
depth: usize,
148145
) {
149146
let mut file: File = create_file_out(file_out);
150147
writeln!(file, "{}", width).expect("Failed to write width");
@@ -162,16 +159,12 @@ pub fn fwrite_chunk(
162159
}
163160
}
164161

165-
pub fn fwrite_encode(
166-
file_out: &str,
167-
code: &[u8]
168-
) {
162+
pub fn fwrite_encode(file_out: &str, code: &[u8]) {
169163
let mut file: File = create_file_out(file_out);
170-
file.write(&code).expect(&format!("Cannot write bytes in {}", file_out));
164+
file.write(&code)
165+
.expect(&format!("Cannot write bytes in {}", file_out));
171166
}
172167

173-
174168
pub fn fread_code(file_in: &str) -> Vec<u8> {
175-
return fs::read(file_in)
176-
.expect(&format!("[ERROR] cannot read {:?} file", file_in));
177-
}
169+
return fs::read(file_in).expect(&format!("[ERROR] cannot read {:?} file", file_in));
170+
}

method-rs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub mod libchunk {
22
pub mod chunk;
3+
pub mod chunk_compress;
34
pub mod chunk_gen;
45
pub mod chunk_process;
56
pub mod chunk_transform;
6-
pub mod chunk_compress;
77
}
88

99
pub mod chunk_io;

method-rs/src/libchunk/chunk_compress.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::libchunk::chunk::BLOCK_AIR;
22
use crate::libchunk::chunk::*;
33

4-
54
#[derive(Default)]
65
struct Run {
76
num_occurrences: usize,
8-
block: u8
7+
block: u8,
98
}
109

1110
const B0: usize = 0;
@@ -16,17 +15,9 @@ const B11: usize = 11;
1615
const IDX_BIT_B1: usize = 7;
1716
const IDX_BIT_B0: usize = 6;
1817

19-
2018
const MAX_NUM_OCCURRENCES: usize = 4095;
2119

22-
23-
24-
fn flatten(
25-
chunk: &Vec<Vec<Vec<u8>>>,
26-
width: usize,
27-
height: usize,
28-
depth: usize,
29-
) -> Vec<u8> {
20+
fn flatten(chunk: &Vec<Vec<Vec<u8>>>, width: usize, height: usize, depth: usize) -> Vec<u8> {
3021
let mut array: Vec<u8> = vec![0; width * height * depth];
3122

3223
for y in 0..height {
@@ -54,37 +45,45 @@ fn get_runs(flat_chunk: &[u8]) -> Vec<Run> {
5445

5546
while idx < flat_chunk.len() {
5647
if last_block != flat_chunk[idx] {
57-
runs.push(Run{num_occurrences: num_occurrences, block: last_block});
48+
runs.push(Run {
49+
num_occurrences: num_occurrences,
50+
block: last_block,
51+
});
5852
last_block = flat_chunk[idx];
5953
num_occurrences = 1;
6054
} else {
6155
num_occurrences += 1;
6256
}
6357

6458
if num_occurrences == MAX_NUM_OCCURRENCES {
65-
runs.push(Run{num_occurrences: MAX_NUM_OCCURRENCES, block: last_block});
59+
runs.push(Run {
60+
num_occurrences: MAX_NUM_OCCURRENCES,
61+
block: last_block,
62+
});
6663
num_occurrences = 0;
6764
}
6865

6966
idx += 1;
7067
}
7168

7269
if num_occurrences > 0 {
73-
runs.push(Run{num_occurrences: num_occurrences, block: last_block});
74-
}
70+
runs.push(Run {
71+
num_occurrences: num_occurrences,
72+
block: last_block,
73+
});
74+
}
7575

7676
runs
7777
}
7878

79-
8079
fn encode_run(bytes: &mut Vec<u8>, run: &Run) {
8180
let mut byte = 0u8;
8281

8382
match run.block {
8483
BLOCK_GRASS => byte |= 1 << IDX_BIT_B0,
8584
BLOCK_WOOD => byte |= 1 << IDX_BIT_B1,
8685
BLOCK_STONE => byte |= (1 << IDX_BIT_B0) | (1 << IDX_BIT_B1),
87-
_ => ()
86+
_ => (),
8887
}
8988

9089
if run.num_occurrences < (1 << B5) {
@@ -97,7 +96,7 @@ fn encode_run(bytes: &mut Vec<u8>, run: &Run) {
9796
bytes.push(byte);
9897
} else {
9998
// bb10nnnn nnnnnnnn
100-
99+
101100
byte |= 1 << B5;
102101

103102
// Primul octet:
@@ -119,7 +118,6 @@ fn encode_run(bytes: &mut Vec<u8>, run: &Run) {
119118
}
120119
}
121120

122-
123121
pub fn chunk_encode(
124122
chunk: &Vec<Vec<Vec<u8>>>,
125123
width: usize,
@@ -147,14 +145,16 @@ pub fn chunk_decode(
147145
let mut idx: usize = 0;
148146

149147
while idx < code.len() {
150-
let block: u8 = match (code[idx] & (1 << IDX_BIT_B1) > 0, code[idx] & (1 << IDX_BIT_B0) > 0) {
148+
let block: u8 = match (
149+
code[idx] & (1 << IDX_BIT_B1) > 0,
150+
code[idx] & (1 << IDX_BIT_B0) > 0,
151+
) {
151152
(true, true) => BLOCK_STONE, // b1b0 = 11
152153
(true, false) => BLOCK_WOOD, // b1b0 = 10
153154
(false, true) => BLOCK_GRASS, // b1b0 = 01
154-
_ => BLOCK_AIR // b1b0 = 00
155+
_ => BLOCK_AIR, // b1b0 = 00
155156
};
156157

157-
158158
let mut num_occurrences = 0usize;
159159

160160
if code[idx] & (1 << B5) == 0 {

method-rs/src/libchunk/chunk_gen.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
use std::cmp::{min, max};
2-
3-
pub fn is_inside(
4-
width: usize, height: usize, depth: usize,
5-
x: isize, y: isize, z: isize
6-
) -> bool {
7-
0 <= x && x < (width as isize)
8-
&& 0 <= y && y < (height as isize)
9-
&& 0 <= z && z < (depth as isize)
10-
1+
use std::cmp::{max, min};
2+
3+
pub fn is_inside(width: usize, height: usize, depth: usize, x: isize, y: isize, z: isize) -> bool {
4+
0 <= x
5+
&& x < (width as isize)
6+
&& 0 <= y
7+
&& y < (height as isize)
8+
&& 0 <= z
9+
&& z < (depth as isize)
1110
}
1211

13-
1412
pub fn chunk_place_block(
1513
chunk: &mut Vec<Vec<Vec<u8>>>,
16-
width: usize, height: usize, depth: usize,
17-
x: isize, y: isize, z: isize,
18-
block: u8
14+
width: usize,
15+
height: usize,
16+
depth: usize,
17+
x: isize,
18+
y: isize,
19+
z: isize,
20+
block: u8,
1921
) {
2022
if !is_inside(width, height, depth, x, y, z) {
2123
return;
2224
}
2325
chunk[x as usize][y as usize][z as usize] = block;
2426
}
2527

26-
27-
28-
2928
pub fn chunk_fill_cuboid(
3029
chunk: &mut Vec<Vec<Vec<u8>>>,
31-
width: usize, height: usize, depth: usize,
32-
x0: isize, y0: isize, z0: isize,
33-
x1: isize, y1: isize, z1: isize,
34-
block: u8
30+
width: usize,
31+
height: usize,
32+
depth: usize,
33+
x0: isize,
34+
y0: isize,
35+
z0: isize,
36+
x1: isize,
37+
y1: isize,
38+
z1: isize,
39+
block: u8,
3540
) {
3641
let min_x = max(min(x0, x1), 0isize);
3742
let min_y = max(min(y0, y1), 0isize);
@@ -44,31 +49,26 @@ pub fn chunk_fill_cuboid(
4449
for x in min_x..=max_x {
4550
for y in min_y..=max_y {
4651
for z in min_z..=max_z {
47-
chunk_place_block(
48-
chunk, width, height, depth,
49-
x, y, z, block);
52+
chunk_place_block(chunk, width, height, depth, x, y, z, block);
5053
}
51-
}
54+
}
5255
}
5356
}
5457

55-
56-
fn euclidian_dist(
57-
x0: isize, y0: isize, z0: isize,
58-
x1: isize, y1: isize, z1: isize
59-
) -> f32 {
60-
(
61-
(x0 - x1).pow(2) as f32
62-
+ (y0 - y1).pow(2) as f32
63-
+ (z0 - z1).pow(2) as f32
64-
).sqrt()
58+
fn euclidian_dist(x0: isize, y0: isize, z0: isize, x1: isize, y1: isize, z1: isize) -> f32 {
59+
((x0 - x1).pow(2) as f32 + (y0 - y1).pow(2) as f32 + (z0 - z1).pow(2) as f32).sqrt()
6560
}
6661

6762
pub fn chunk_fill_sphere(
6863
chunk: &mut Vec<Vec<Vec<u8>>>,
69-
width: usize, height: usize, depth: usize,
70-
x: isize, y: isize, z: isize,
71-
radius: f32, block: u8
64+
width: usize,
65+
height: usize,
66+
depth: usize,
67+
x: isize,
68+
y: isize,
69+
z: isize,
70+
radius: f32,
71+
block: u8,
7272
) {
7373
let r = radius.ceil().abs() as isize;
7474

0 commit comments

Comments
 (0)