Skip to content

Commit

Permalink
checkpoint-kai-1700856110
Browse files Browse the repository at this point in the history
  • Loading branch information
coilysiren committed Nov 24, 2023
1 parent 80a3f4e commit 8be56c8
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 56 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ license = "AGPL"
crate-type = ["cdylib", "rlib"]
path = "src/rust/lib.rs"

[features]
default = ["console_error_panic_hook"]

[dependencies]
console_error_panic_hook = { version = "^0.1", optional = true }
console_error_panic_hook = "^0.1"
specs = "^0.12"
specs-derive = "^0.2"
rand = "^0.5"
wasm-bindgen = "^0.2"
getrandom = { version = "^0.2", features = ["js"] }
getrandom = { version = "^0.2", features = ["js", "wasm-bindgen"] }

[dev-dependencies]
cargo-watch = "^7"
Expand Down
52 changes: 43 additions & 9 deletions src/js/lib/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ const wasm = import("galaxy_gen_backend/galaxy_gen_backend");

export function Interface() {
const [galaxySize, setGalaxySize] = React.useState(100);
const [wasmModule, setWasmModule] = React.useState(null);
const [galaxySeedMass, setGalaxySeedMass] = React.useState(3);
const [galaxyGravityReach, setGalaxyGravityReach] = React.useState(10);
let wasmModule: any = null;
let galaxyFrontend: galaxy.Frontend = null;

wasm.then((module) => {
console.log("module", module)
setWasmModule(module);
console.log("wasm module loaded: ", module)
wasmModule = module;
});

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const handleGalaxySizeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setGalaxySize(parseInt(event.target.value));
};

Expand All @@ -24,7 +26,35 @@ export function Interface() {
<span className="input-group-text" id="basic-addon1">Galaxy Size:</span>
<input
type="text" className="form-control" placeholder="Username" name="galaxySize"
value={galaxySize.toString()} onChange={handleChange}
value={galaxySize.toString()} onChange={handleGalaxySizeChange}
/>
</div>
)

const handleGalaxySeedMassChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setGalaxySeedMass(parseInt(event.target.value));
};

const galaxySeedMassInput = (
<div className="input-group mb-3">
<span className="input-group-text" id="basic-addon1">Seed Mass:</span>
<input
type="text" className="form-control" placeholder="Username" name="galaxySeedMass"
value={galaxySeedMass.toString()} onChange={handleGalaxySeedMassChange}
/>
</div>
)

const handleGalaxyGravityReachChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setGalaxySeedMass(parseInt(event.target.value));
};

const galaxyGravityReachInput = (
<div className="input-group mb-3">
<span className="input-group-text" id="basic-addon1">Gravity Reach:</span>
<input
type="text" className="form-control" placeholder="Username" name="galaxyGravityReach"
value={galaxyGravityReach.toString()} onChange={handleGalaxyGravityReachChange}
/>
</div>
)
Expand All @@ -33,6 +63,7 @@ export function Interface() {
if (wasmModule === null) {
console.error("wasm not yet loaded");
} else {
console.log("initializing galaxy");
galaxyFrontend = new galaxy.Frontend(galaxySize);
}
};
Expand All @@ -44,10 +75,11 @@ export function Interface() {
)

const handleSeedClick = () => {
if (wasmModule === null) {
console.error("wasm not yet loaded");
if (galaxyFrontend === null) {
console.error("galaxy not yet initialized");
} else {
galaxyFrontend.seed();
console.log("seeding galaxy");
galaxyFrontend.seed(galaxySeedMass);
}
};

Expand All @@ -58,7 +90,7 @@ export function Interface() {
)

const handleTickClick = () => {
galaxyFrontend.tick();
galaxyFrontend.tick(galaxyGravityReach);
};

const tickButton = (
Expand All @@ -72,6 +104,8 @@ export function Interface() {
<h1>Galaxy Generator</h1>
<h2><small className="text-muted">( rust =&gt; wasm =&gt; js ) galaxy generation simulation</small></h2>
{galaxySizeInput}
{galaxySeedMassInput}
{galaxyGravityReachInput}
<div className="d-flex justify-content-between">
{initButton}
{seedButton}
Expand Down
10 changes: 5 additions & 5 deletions src/js/lib/galaxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ export class Frontend {
this.galaxySize = galaxySize;
}

public seed(): void {
this.galaxy.seed();
public seed(additionalMass: number): void {
this.galaxy.seed(additionalMass);
}

public tick(): void {
this.galaxy.tick();
public tick(gravityReach: number): void {
this.galaxy.tick(gravityReach);
}

public cells(): Cell[] {
// Uint16Array to list of numbers
let cells: Cell[] = [];
const mass = Array.from(this.galaxy.cell_mass());
const mass = Array.from(this.galaxy.mass());
mass.forEach((element, index) => {
cells.push({
mass: element,
Expand Down
10 changes: 5 additions & 5 deletions src/rust/cell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasm_bindgen::prelude::*;
use crate::galaxy::Galaxy;

// internal constants
impl Cell {
Expand Down Expand Up @@ -28,7 +28,7 @@ impl Default for Cell {
// public methods
impl Cell {
pub fn get_type(&self) -> u8 {
if self.mass < Cell::MIN_MASS_STAR {
if self.mass < Galaxy::MIN_MASS_STAR {
return Cell::TYPE_INDEX_GAS;
} else {
return Cell::TYPE_INDEX_STAR;
Expand All @@ -45,9 +45,9 @@ impl Cell {
// internal methods
impl Cell {
pub fn check_if_type(&self, type_index: u8) -> bool {
if (type_index == Cell::TYPE_INDEX_GAS) & (self.mass < Cell::MIN_MASS_STAR) {
if (type_index == Cell::TYPE_INDEX_GAS) & (self.mass < Galaxy::MIN_MASS_STAR) {
return true;
} else if (type_index == Cell::TYPE_INDEX_STAR) & (self.mass >= Cell::MIN_MASS_STAR) {
} else if (type_index == Cell::TYPE_INDEX_STAR) & (self.mass >= Galaxy::MIN_MASS_STAR) {
return true;
} else {
return false;
Expand All @@ -69,7 +69,7 @@ mod tests_cell_types {
#[test]
fn test_star_cell() {
let cell = Cell {
mass: Cell::MIN_MASS_STAR * 2,
mass: Galaxy::MIN_MASS_STAR * 2,
..Default::default()
};
assert_eq!(cell.is_star(), true);
Expand Down
67 changes: 50 additions & 17 deletions src/rust/galaxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use cell::*;
use rand::Rng;
use wasm_bindgen::prelude::*;

use crate::utils;

// types
#[wasm_bindgen]
pub struct Galaxy {
Expand All @@ -16,7 +14,9 @@ pub struct Galaxy {
impl Galaxy {
#[wasm_bindgen(constructor)]
pub fn new(size: u16, mass: u16) -> Galaxy {
utils::set_panic_hook();
// create a new galaxy
// https://github.com/rustwasm/console_error_panic_hook#readme
console_error_panic_hook::set_once();
return Galaxy {
size,
cells: vec![
Expand All @@ -28,23 +28,35 @@ impl Galaxy {
],
};
}
pub fn seed(&mut self) {
pub fn seed(&self, additional: u16) -> Galaxy {
// add mass to the galaxy
let mut next = Vec::with_capacity((self.size as usize).pow(2));
for cell_index in 0..self.size.pow(2) {
self.cells[cell_index as usize] = Cell {
mass: rand::thread_rng().gen_range(0, self.size),
let mass = self.cells[cell_index as usize].mass;
next.push(Cell {
mass: mass + rand::thread_rng().gen_range(0, additional + 1),
..Default::default()
};
});
}
return Galaxy {
size: self.size,
cells: next,
};
}
pub fn tick(&mut self) {
let next = self.cells.clone();
pub fn tick(&self, reach: u16) -> Galaxy {
// advance the galaxy one tick
// TODO: not yet implemented
let next = Vec::with_capacity((self.size as usize).pow(2));
for index in 0..(self.size - 1) {
let _cell = self.cells[index as usize];
let _neighbours = self.neighbours(index, 1);
let _neighbours = self.neighbours(index, reach);
}
self.cells = next;
return Galaxy {
size: self.size,
cells: next,
};
}
pub fn cell_mass(&self) -> Vec<u16> {
pub fn mass(&self) -> Vec<u16> {
// for every cell, get the mass
let mut mass = Vec::new();
for cell in self.cells.iter() {
Expand Down Expand Up @@ -130,22 +142,43 @@ mod tests_intial_generation {
}
#[test]
fn test_seed_no_panic() {
Galaxy::new(10, 0).seed();
Galaxy::new(10, 0).seed(1);
}
#[test]
fn test_seed_tick_no_panic() {
let mut galaxy = Galaxy::new(10, 1);
galaxy.seed();
galaxy.tick();
Galaxy::new(10, 1).seed(1).tick(1);
}
#[test]
fn test_seed_alters_data() {
let mut galaxy = Galaxy::new(10, 0);
let cells_before = galaxy.cells.clone();
galaxy.seed();
galaxy = galaxy.seed(1);
let cells_after = galaxy.cells.clone();
assert_ne!(cells_before, cells_after);
}
#[test]
fn test_seed_doesnt_alter_when_zero() {
let mut galaxy = Galaxy::new(10, 0);
let cells_before = galaxy.cells.clone();
galaxy = galaxy.seed(0);
let cells_after = galaxy.cells.clone();
assert_eq!(cells_before, cells_after);
}
#[test]
fn test_seed_alters_data_twice() {
let mut galaxy = Galaxy::new(10, 0);
let cells_first = galaxy.cells.clone();

galaxy = galaxy.seed(1);
let cells_second = galaxy.cells.clone();
assert_ne!(cells_first, cells_second);

galaxy = galaxy.seed(1);
let cells_third = galaxy.cells.clone();
assert_ne!(cells_first, cells_second);
assert_ne!(cells_first, cells_third);
assert_ne!(cells_second, cells_third);
}
}

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions src/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ extern crate wasm_bindgen;
pub mod cell;
pub mod galaxy;
pub mod galaxyecs;
mod utils;

use cell::Cell;
use galaxy::Galaxy;

// public constants
impl Cell {
pub const MIN_MASS_STAR: u16 = 10000;
}
impl Galaxy {
pub const MIN_MASS_STAR: u16 = 10000;
pub const GAS_REACH_MODIFIER: u16 = 10;
}
10 changes: 0 additions & 10 deletions src/rust/utils.rs

This file was deleted.

0 comments on commit 8be56c8

Please sign in to comment.