Browse Source

Move this from private repo

Johann Woelper 7 years ago
commit
b6707bca0e
4 changed files with 397 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 164 0
      server/Cargo.lock
  3. 10 0
      server/Cargo.toml
  4. 222 0
      server/src/main.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+target/

+ 164 - 0
server/Cargo.lock

@@ -0,0 +1,164 @@
+[[package]]
+name = "bitflags"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "cloudabi"
+version = "0.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "dtoa"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "fuchsia-zircon"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "fuchsia-zircon-sys"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "itoa"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "libc"
+version = "0.2.42"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "proc-macro2"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "serde"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "serde_derive"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "server"
+version = "0.1.0"
+dependencies = [
+ "rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "syn"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789"
+"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
+"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
+"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
+"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
+"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
+"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1"
+"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6"
+"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035"
+"checksum rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a89abf8d34faf9783692392dca7bcdc6e82fa84eca86ccb6301ec87f3497185"
+"checksum rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1b7a5f27547c49e5ccf8a586db3f3782fd93cf849780b21853b9d981db203302"
+"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95"
+"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79"
+"checksum serde_json 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "93aee34bb692dde91e602871bc792dd319e489c7308cdbbe5f27cf27c64280f5"
+"checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d"
+"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
+"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
+"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 10 - 0
server/Cargo.toml

@@ -0,0 +1,10 @@
+[package]
+name = "server"
+version = "0.1.0"
+authors = ["Johann Woelper <johann.woelper@king.com>"]
+
+[dependencies]
+serde = "1.0"
+serde_derive = "1.0"
+serde_json = "1.0"
+rand = "0.5"

+ 222 - 0
server/src/main.rs

@@ -0,0 +1,222 @@
+use std::time::{SystemTime};
+use rand::Rng;
+
+
+#[macro_use]
+extern crate serde_derive;
+
+extern crate rand;
+extern crate serde;
+extern crate serde_json;
+
+
+/*
+UNITS
+
+mass = kilogram
+time = sec
+distance = meter
+speed = meter / tick (s)
+
+*/
+
+// Time constants
+const TICK: i32 = 1;
+const SECOND: i32 = TICK;
+const MINUTE: i32 = SECOND*60;
+const HOUR: i32 = MINUTE*60;
+const DAY: i32 = HOUR*24;
+const MONTH: i32 = DAY*30;
+const YEAR: i32 = MONTH*12;
+// weight constants
+const KG: f32 = 1.0;
+const TON: f32 = KG*1000.0;
+
+#[derive(Serialize, Deserialize, Debug)]
+enum Diets {
+    Carnivore,
+    Herbivore,
+    Omnivore,
+    Photovore
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+enum LifeformKind {
+    Fauna,
+    Flora,
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+struct Biome {
+    width: f32,
+    height: f32,
+    water: f32,
+    biomass: f32,
+    lifeforms: Vec<Lifeform>
+}
+
+
+impl Biome {
+    fn tick(&mut self){
+
+        for lifeform in &mut self.lifeforms {
+            if lifeform.alive {
+                lifeform.tick();
+                self.water -= lifeform.water_intake;
+            } else {
+                self.biomass += lifeform.mass;
+                lifeform.mass = 0.0;
+            }
+
+
+
+            //keep lifeform in bounds
+            //TODO: clamp
+            if lifeform.position[0] < 0.0 {
+                lifeform.position[0] = 0.0;
+            }
+            if lifeform.position[0] > self.width {
+                lifeform.position[0] = self.width;
+            }
+
+            if lifeform.position[2] > self.height {
+                lifeform.position[2] = self.height;
+            }
+            if lifeform.position[2] < 0.0 {
+                lifeform.position[2] = 0.0;
+            }
+
+        }
+    }
+
+    // fn lf_move(&mut self, mut lf: &Lifeform) {
+       
+    // }
+
+    // fn lf_eat(&mut self) {
+    //     for lf in self.lifeforms {
+
+    //     }
+    // }
+}
+
+#[derive(Serialize, Deserialize, Debug)]
+struct Lifeform {
+    name: String,
+    mass: f32,
+    age: i32,
+    maxage: i32,
+    water_intake: f32,
+    nutrient_intake: f32,
+    diet: Diets,
+    kind: LifeformKind,
+    alive: bool,
+    position: Vec<f32>,
+    speed: f32,
+}
+
+
+// TODO: add position
+impl Lifeform {
+    fn tick(&mut self){
+        if self.alive {
+            self.mass += self.nutrient_intake;
+            self.age += 1;
+        }
+        if self.age > self.maxage {
+            self.alive = false;
+            println!("Died: {:?}", self);
+        }
+        self.move_random();
+    }
+    fn move_random(&mut self) {
+        let mut rng = rand::thread_rng();
+        let rx = rng.gen_range::<f32>(-1.0, 1.0);
+        let rz = rng.gen_range::<f32>(-1.0, 1.0);
+        self.position[0] += rx*self.speed;
+        self.position[2] += rz*self.speed;
+    }
+}
+
+impl Default for Lifeform {
+    fn default() -> Lifeform {
+        Lifeform {
+            name: "Generic Plant".to_string(),
+            mass: 1.0,
+            age: 0,
+            water_intake: 1.0/DAY as f32,
+            nutrient_intake: 1.0/DAY as f32,
+            diet: Diets::Herbivore,
+            kind: LifeformKind::Flora,
+            maxage: YEAR,
+            alive: true,
+            position: vec![0.0, 0.0, 0.0],
+            speed: 0.0,
+        }
+    }
+}
+
+fn main() {
+    let start = SystemTime::now();
+    println!("{:?}", start);
+
+    let mut biome = Biome {width: 1000.0, height: 1000.0, water: 1000.0, lifeforms: Vec::new(), biomass: TON};
+
+
+    // add some trees
+    for _i in 1..400 {
+        let mut rng = rand::thread_rng();
+        let age = rng.gen_range::<i32>(1*MONTH, 10*YEAR);
+        let maxage = rng.gen_range::<i32>(5*YEAR, 50*YEAR);
+        let mass = rng.gen_range::<f32>(50.0, 6.0*TON);
+        let water_intake = rng.gen_range::<f32>(50.0, 150.0)/DAY as f32;
+        let mut tree = Lifeform {
+            name: "Tree".to_string(),
+            age: age,
+            maxage: maxage,
+            mass: mass,
+            water_intake: water_intake,
+            diet: Diets::Photovore,
+            alive: true,
+            kind: LifeformKind::Flora,
+            ..Default::default()
+            };
+
+        biome.lifeforms.push(tree);
+    }
+
+    // add deer
+    for _i in 1..10 {
+        let mut rng = rand::thread_rng();
+        let age = rng.gen_range::<i32>(1*MONTH, 5*YEAR);
+        let maxage = rng.gen_range::<i32>(1*YEAR, 5*YEAR);
+        let mass = rng.gen_range::<f32>(4.0, 17.0);
+        let water_intake = rng.gen_range::<f32>(0.5, 1.0)/DAY as f32;
+        let speed = rng.gen_range::<f32>(0.0/HOUR as f32, 10000.0/HOUR as f32) as f32;
+        
+        let mut deer = Lifeform {
+            name: "Deer".to_string(),
+            kind: LifeformKind::Fauna,
+            age: age,
+            maxage: maxage,
+            mass: mass,
+            water_intake: water_intake,
+            speed: speed,
+            ..Default::default()};
+        biome.lifeforms.push(deer);
+    }
+
+
+    
+
+
+    for _i in 1..1000 {
+        biome.tick();
+    }
+
+    let serialized = serde_json::to_string(&biome).unwrap();
+    println!("serialized = {}", serialized);
+
+    //let deserialized: Plant = serde_json::from_str(&serialized).unwrap();
+    //println!("deserialized = {:?}", deserialized);
+}