[WIP] skycraft server
This commit is contained in:
2
skycraft/server/.cargo/config.toml
Normal file
2
skycraft/server/.cargo/config.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[build]
|
||||
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
|
1
skycraft/server/.gitignore
vendored
Normal file
1
skycraft/server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
1864
skycraft/server/Cargo.lock
generated
Normal file
1864
skycraft/server/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
skycraft/server/Cargo.toml
Normal file
12
skycraft/server/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "skycraft-server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tide = "0.14.0"
|
||||
async-std = { version = "1.6.0", features = ["attributes"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
262
skycraft/server/src/main.rs
Normal file
262
skycraft/server/src/main.rs
Normal file
@@ -0,0 +1,262 @@
|
||||
use tide::Request;
|
||||
use tide::Response;
|
||||
use tide::prelude::*;
|
||||
//use serde_json::json;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
#[derive(Default, Serialize)]
|
||||
struct SystemBody {
|
||||
name: String,
|
||||
mass: f32,
|
||||
seed: u32,
|
||||
spin: [f32; 3],
|
||||
// geometry: ,
|
||||
glow_color: [f32; 3],
|
||||
orbit: KeplerOrbit,
|
||||
children: Vec<SystemBody>,
|
||||
}
|
||||
|
||||
#[derive(Default, Serialize)]
|
||||
struct KeplerOrbit {
|
||||
excentricity: f32,
|
||||
semimajor_axis: f32,
|
||||
inclination: f32,
|
||||
ascending_node_longitude: f32,
|
||||
periapsis_argument: f32,
|
||||
t0: f32,
|
||||
}
|
||||
|
||||
const CHUNKSIZE: usize = 32;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Chunk {
|
||||
position: [f32; 3],
|
||||
layout: [i32; 3],
|
||||
#[serde(serialize_with = "<[_]>::serialize")]
|
||||
blocks: [u32; CHUNKSIZE.pow(3)],
|
||||
seed: u32,
|
||||
underground: bool,
|
||||
}
|
||||
|
||||
impl PartialEq for Chunk {
|
||||
fn eq(&self, other: &Chunk) -> bool {
|
||||
self.seed == other.seed && self.position == other.position
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> tide::Result<()> {
|
||||
let mut app = tide::new();
|
||||
app.at("/api/system/:seed").get(get_system);
|
||||
app.at("/api/body/:seed").get(get_body);
|
||||
app.listen("127.0.0.1:8080").await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_sun_chunk(seed: u32, pos: [i32; 3]) -> Chunk {
|
||||
let cs = CHUNKSIZE as i32;
|
||||
Chunk {
|
||||
position: pos.map(|x| ((x * cs - cs / 2) as f32)),
|
||||
layout: pos,
|
||||
blocks: [8; CHUNKSIZE.pow(3)],
|
||||
seed: seed,
|
||||
underground: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_dirt_chunk(seed: u32, pos: [i32; 3]) -> Chunk {
|
||||
let cs = CHUNKSIZE as i32;
|
||||
Chunk {
|
||||
position: pos.map(|x| ((x * cs - cs / 2) as f32)),
|
||||
layout: pos,
|
||||
blocks: [2; CHUNKSIZE.pow(3)],
|
||||
seed: seed,
|
||||
underground: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_chunk(seed: u32, pos: [i32; 3]) -> Option<Chunk>{
|
||||
match seed {
|
||||
0 => match pos {
|
||||
[0, 0, 0] => Some(get_sun_chunk(seed, pos)),
|
||||
_ => None,
|
||||
},
|
||||
_ => match pos {
|
||||
[0, 0, 0] => Some(get_dirt_chunk(seed, pos)),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_body_chunks(seed: u32) -> Vec<Chunk> {
|
||||
let mut chunks = vec![];
|
||||
let mut tocheck = vec![[0, 0, 0]];
|
||||
|
||||
while tocheck.len() > 0 {
|
||||
let pos = tocheck.pop().unwrap();
|
||||
let thischunk = get_chunk(seed, pos);
|
||||
if thischunk.is_none() || chunks.contains(thischunk.as_ref().unwrap()) {
|
||||
continue;
|
||||
}
|
||||
chunks.push(thischunk.unwrap());
|
||||
println!("Adding chunk for seed {seed} at position {:?}", pos);
|
||||
|
||||
let [ci, cj, ck] = pos;
|
||||
tocheck.push([ci - 1, cj, ck]);
|
||||
tocheck.push([ci + 1, cj, ck]);
|
||||
tocheck.push([ci, cj - 1, ck]);
|
||||
tocheck.push([ci, cj + 1, ck]);
|
||||
tocheck.push([ci, cj, ck - 1]);
|
||||
tocheck.push([ci, cj, ck + 1]);
|
||||
}
|
||||
|
||||
chunks
|
||||
}
|
||||
|
||||
async fn get_body(req: Request<()>) -> tide::Result {
|
||||
let seed: u32 = req.param("seed")?.parse()?;
|
||||
let chunks = get_body_chunks(seed);
|
||||
let response = Response::builder(200)
|
||||
.body(serde_json::to_string(&chunks)?)
|
||||
.content_type("application/json")
|
||||
.header("access-control-allow-origin", "*")
|
||||
.build();
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
// TODO
|
||||
// ----
|
||||
// [ ] serve chunks
|
||||
// [ ] universe simulation?
|
||||
// [ ] websocket with client updates
|
||||
|
||||
async fn get_system(_req: Request<()>) -> tide::Result {
|
||||
// let seed: i32 = req.param("seed")?.parse()?;
|
||||
let response = Response::builder(200)
|
||||
.content_type("application/json")
|
||||
.header("access-control-allow-origin", "*")
|
||||
.body(serde_json::to_string(&SystemBody
|
||||
{
|
||||
name: "Tat".to_string(),
|
||||
seed: 0,
|
||||
|
||||
mass: 1000.0,
|
||||
spin: [0.0, 0.0, 0.2],
|
||||
|
||||
// geometry: getBodyGeometry(0),
|
||||
glow_color: [0.5, 0.5, 0.46],
|
||||
|
||||
children: Vec::from([
|
||||
SystemBody {
|
||||
name: "Quicksilver".to_string(),
|
||||
seed: 1336,
|
||||
|
||||
mass: 0.1,
|
||||
spin: [0.0, 0.0, 0.05],
|
||||
|
||||
// geometry: makeCube([0, 4]),
|
||||
|
||||
orbit: KeplerOrbit {
|
||||
excentricity: 0.0,
|
||||
semimajor_axis: 200.0,
|
||||
inclination: 0.8,
|
||||
ascending_node_longitude: 0.0,
|
||||
periapsis_argument: 0.0,
|
||||
t0: 0.0,
|
||||
},
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
SystemBody {
|
||||
name: "Satourne".to_string(),
|
||||
seed: 1338,
|
||||
|
||||
mass: 0.1,
|
||||
spin: [0.0, 0.5, 0.0],
|
||||
|
||||
// geometry: makeCube([0, 5]),
|
||||
|
||||
orbit: KeplerOrbit {
|
||||
excentricity: 0.0,
|
||||
semimajor_axis: 900.0,
|
||||
inclination: 0.0,
|
||||
ascending_node_longitude: 0.0,
|
||||
periapsis_argument: 0.0,
|
||||
t0: 0.0,
|
||||
},
|
||||
|
||||
children: Vec::from([
|
||||
SystemBody {
|
||||
name: "Kyoujin".to_string(),
|
||||
seed: 13381,
|
||||
|
||||
mass: 0.01,
|
||||
spin: [0.0, 0.0, 0.05],
|
||||
|
||||
// geometry: makeCube([0, 6]),
|
||||
|
||||
orbit: KeplerOrbit {
|
||||
excentricity: 0.0,
|
||||
semimajor_axis: 20.0,
|
||||
inclination: (PI / 2.0) as f32,
|
||||
ascending_node_longitude: 0.0,
|
||||
periapsis_argument: 0.0,
|
||||
t0: 0.0,
|
||||
},
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
]),
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
SystemBody {
|
||||
name: "Tataooine".to_string(),
|
||||
seed: 1337,
|
||||
|
||||
mass: 50.0,
|
||||
spin: [0.0, 0.0, 0.05],
|
||||
|
||||
// geometry: getBodyGeometry(1337),
|
||||
|
||||
orbit: KeplerOrbit {
|
||||
excentricity: 0.3,
|
||||
semimajor_axis: 500.0,
|
||||
inclination: 0.0,
|
||||
ascending_node_longitude: 0.0,
|
||||
periapsis_argument: 0.0,
|
||||
t0: 0.0,
|
||||
},
|
||||
|
||||
children: Vec::from([
|
||||
SystemBody {
|
||||
name: "Mun".to_string(),
|
||||
seed: 13371,
|
||||
|
||||
mass: 0.01,
|
||||
spin: [0.0, 0.0, 0.05],
|
||||
|
||||
// geometry: makeCube([0, 7]),
|
||||
|
||||
orbit: KeplerOrbit {
|
||||
excentricity: 0.0,
|
||||
semimajor_axis: 50.0,
|
||||
inclination: (PI / 2.0) as f32,
|
||||
ascending_node_longitude: 0.0,
|
||||
periapsis_argument: 0.0,
|
||||
t0: 0.0,
|
||||
},
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
]),
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
]),
|
||||
|
||||
..Default::default()
|
||||
})?)
|
||||
.build();
|
||||
Ok(response)
|
||||
}
|
Reference in New Issue
Block a user