mirror of
https://github.com/G2-Games/minecraft_alpha_server.git
synced 2025-04-19 07:12:58 -05:00
Added chunk stuff
This commit is contained in:
parent
d4bee28781
commit
b43c372cc4
5 changed files with 163 additions and 25 deletions
41
Cargo.lock
generated
41
Cargo.lock
generated
|
@ -2,6 +2,12 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
|
@ -66,6 +72,12 @@ version = "1.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "colog"
|
||||
version = "1.3.0"
|
||||
|
@ -93,6 +105,15 @@ dependencies = [
|
|||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_filter"
|
||||
version = "0.1.2"
|
||||
|
@ -116,6 +137,16 @@ dependencies = [
|
|||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
|
@ -152,9 +183,19 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"byteorder",
|
||||
"colog",
|
||||
"flate2",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
|
||||
dependencies = [
|
||||
"adler2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.0"
|
||||
|
|
|
@ -6,4 +6,5 @@ edition = "2021"
|
|||
[dependencies]
|
||||
byteorder = "1.5.0"
|
||||
colog = "1.3.0"
|
||||
flate2 = "1.0.34"
|
||||
log = "0.4.22"
|
||||
|
|
112
src/chunk.rs
Normal file
112
src/chunk.rs
Normal file
|
@ -0,0 +1,112 @@
|
|||
use byteorder::{WriteBytesExt, BE};
|
||||
use flate2::write::ZlibEncoder;
|
||||
use flate2::Compression;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use crate::to_bytes::ToBytes;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct MapChunk {
|
||||
chunk_x: i32,
|
||||
chunk_y: i16,
|
||||
chunk_z: i32,
|
||||
size_x: u8,
|
||||
size_y: u8,
|
||||
size_z: u8,
|
||||
compressed_data: BlockArray,
|
||||
}
|
||||
|
||||
impl MapChunk {
|
||||
fn new(chunk_x: i32, chunk_z: i32, compressed_data: BlockArray) -> Self {
|
||||
Self {
|
||||
chunk_x,
|
||||
chunk_y: 0,
|
||||
chunk_z,
|
||||
size_x: 15,
|
||||
size_y: 127,
|
||||
size_z: 15,
|
||||
compressed_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct BlockArray {
|
||||
compressed_size: i32,
|
||||
compressed_data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl BlockArray {
|
||||
fn new_air() -> Self {
|
||||
let mut output_vec = Vec::new();
|
||||
|
||||
for _ in 0..(16 * 127 * 15) {
|
||||
output_vec.push(0);
|
||||
}
|
||||
for _ in 0..(16 * 127 * 15) / 2 {
|
||||
output_vec.push(0);
|
||||
}
|
||||
for _ in 0..(16 * 127 * 15) / 2 {
|
||||
output_vec.push(0);
|
||||
}
|
||||
for _ in 0..(16 * 127 * 15) / 2 {
|
||||
output_vec.push(0);
|
||||
}
|
||||
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
encoder.write(&output_vec).unwrap();
|
||||
|
||||
Self {
|
||||
compressed_size: 1,
|
||||
compressed_data: encoder.finish().unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
enum BlockType {
|
||||
Air,
|
||||
Stone,
|
||||
Grass,
|
||||
Dirt,
|
||||
Cobblestone,
|
||||
Planks,
|
||||
Sapling,
|
||||
Bedrock,
|
||||
}
|
||||
|
||||
impl ToBytes for MapChunk {
|
||||
type Bytes = Vec<u8>;
|
||||
|
||||
fn to_bytes(self) -> Self::Bytes {
|
||||
let mut buffer = Vec::new();
|
||||
buffer.write_i32::<BE>(self.chunk_x).unwrap();
|
||||
buffer.write_i16::<BE>(self.chunk_y).unwrap();
|
||||
buffer.write_i32::<BE>(self.chunk_z).unwrap();
|
||||
buffer.write_u8(self.size_x).unwrap();
|
||||
buffer.write_u8(self.size_y).unwrap();
|
||||
buffer.write_u8(self.size_z).unwrap();
|
||||
|
||||
buffer
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct PreChunk {
|
||||
pub x_coord: i32,
|
||||
pub z_coord: i32,
|
||||
pub mode: bool, // True to load, False to unload
|
||||
}
|
||||
|
||||
impl ToBytes for PreChunk {
|
||||
type Bytes = [u8; 9];
|
||||
|
||||
fn to_bytes(self) -> Self::Bytes {
|
||||
let mut buffer = Vec::new();
|
||||
buffer.write_i32::<BE>(self.x_coord).unwrap();
|
||||
buffer.write_i32::<BE>(self.z_coord).unwrap();
|
||||
buffer.write_u8(self.mode as u8).unwrap();
|
||||
|
||||
buffer.try_into().unwrap()
|
||||
}
|
||||
}
|
22
src/main.rs
22
src/main.rs
|
@ -1,8 +1,10 @@
|
|||
mod utils;
|
||||
mod to_bytes;
|
||||
mod chunk;
|
||||
|
||||
use std::{io::{self, Write}, net::{TcpListener, TcpStream}};
|
||||
|
||||
use chunk::PreChunk;
|
||||
use log::{info, warn};
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, BE};
|
||||
use to_bytes::ToBytes;
|
||||
|
@ -128,23 +130,3 @@ impl ServerLoginPacket {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct PreChunk {
|
||||
x_coord: i32,
|
||||
z_coord: i32,
|
||||
mode: bool, // True to load, False to unload
|
||||
}
|
||||
|
||||
impl ToBytes for PreChunk {
|
||||
type Bytes = [u8; 9];
|
||||
|
||||
fn to_bytes(self) -> Self::Bytes {
|
||||
let mut buffer = Vec::new();
|
||||
buffer.write_i32::<BE>(self.x_coord).unwrap();
|
||||
buffer.write_i32::<BE>(self.z_coord).unwrap();
|
||||
buffer.write_u8(self.mode as u8).unwrap();
|
||||
|
||||
buffer.try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ mod private {
|
|||
pub trait ByteArray {}
|
||||
|
||||
impl<const N: usize> ByteArray for [u8; N] {}
|
||||
impl ByteArray for Vec<u8> {}
|
||||
}
|
||||
|
||||
|
||||
|
@ -21,18 +22,19 @@ pub trait ByteArray:
|
|||
+ AsRef<[u8]>
|
||||
+ AsMut<[u8]>
|
||||
{
|
||||
/// The length of this byte array.
|
||||
const SIZE: usize;
|
||||
|
||||
/// Return the array with all zeros.
|
||||
/// Cannot use `Default` as it is not implemented for all array sizes.
|
||||
fn zeroed() -> Self;
|
||||
}
|
||||
|
||||
impl<const N: usize> ByteArray for [u8; N] {
|
||||
const SIZE: usize = N;
|
||||
|
||||
fn zeroed() -> Self {
|
||||
[0; N]
|
||||
}
|
||||
}
|
||||
|
||||
impl ByteArray for Vec<u8> {
|
||||
fn zeroed() -> Self {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue