-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from Jupeyy/pr_editor_improvements4
Implement the auto mapper & creator.
- Loading branch information
Showing
45 changed files
with
3,190 additions
and
607 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "auto-mapper" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
math = { path = "../../../lib/math" } | ||
api = { path = "../../../lib/api" } | ||
api-macros = { path = "../../../lib/api-macros" } | ||
|
||
api-auto-mapper = { path = "../../../game/api-auto-mapper" } | ||
editor-interface = { path = "../../../game/editor-interface" } | ||
|
||
log = "0.4.22" | ||
rustc-hash = "2.1.0" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#![allow(clippy::too_many_arguments)] | ||
|
||
use std::hash::Hasher; | ||
|
||
use editor_interface::auto_mapper::{ | ||
AutoMapperInputModes, AutoMapperInterface, AutoMapperModes, AutoMapperOutputModes, | ||
}; | ||
|
||
pub use api::{DB, IO_RUNTIME}; | ||
pub use api_auto_mapper::*; | ||
use math::math::Rng; | ||
|
||
#[no_mangle] | ||
fn mod_auto_mapper_new() -> Result<Box<dyn AutoMapperInterface>, String> { | ||
Ok(Box::new(AutoMapperGrassMain::default())) | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct AutoMapperGrassMain {} | ||
|
||
impl AutoMapperInterface for AutoMapperGrassMain { | ||
fn supported_modes(&self) -> Vec<AutoMapperModes> { | ||
vec![AutoMapperModes::DesignTileLayer { | ||
neighbouring_tiles: Some(2.try_into().unwrap()), | ||
}] | ||
} | ||
|
||
fn run( | ||
&mut self, | ||
seed: u64, | ||
input: AutoMapperInputModes, | ||
) -> Result<AutoMapperOutputModes, String> { | ||
// Very simple edge detection, but programatically. | ||
let AutoMapperInputModes::DesignTileLayer { | ||
mut tiles, | ||
width, | ||
height, | ||
off_x, | ||
off_y, | ||
} = input; | ||
|
||
let mut hasher = rustc_hash::FxHasher::default(); | ||
hasher.write_u64(seed); | ||
hasher.write_u16(off_x); | ||
hasher.write_u16(off_y); | ||
let seed = hasher.finish(); | ||
|
||
// skip, layers of such sizes are not supported. | ||
if width.get() < 2 || height.get() < 2 { | ||
log::info!( | ||
"Skipped layer, since width or height was < 2: {}, {}", | ||
width, | ||
height | ||
); | ||
return Ok(AutoMapperOutputModes::DesignTileLayer { tiles }); | ||
} | ||
|
||
let mut rng = Rng::new(seed); | ||
|
||
let mut changed_tiles = 0; | ||
for y in 1..height.get().saturating_sub(1) as usize { | ||
for x in 1..width.get().saturating_sub(1) as usize { | ||
let y_off = y * width.get() as usize; | ||
let y_off_minus_one = (y - 1) * width.get() as usize; | ||
let y_off_plus_one = (y + 1) * width.get() as usize; | ||
// case 1: top left | ||
if tiles[y_off + x].index != 0 | ||
&& tiles[y_off + x - 1].index == 0 | ||
&& tiles[y_off_minus_one + x - 1].index == 0 | ||
&& tiles[y_off_minus_one + x].index == 0 | ||
{ | ||
changed_tiles += 1; | ||
// set current tile to 32, which is grass top left | ||
tiles[y_off + x].index = 32; | ||
// just to show some randomness | ||
if rng.random_int() % 2 == 0 { | ||
tiles[y_off + x].index = 4; | ||
} | ||
tiles[y_off + x].flags = Default::default(); | ||
} | ||
// case 2: top right | ||
if tiles[y_off + x].index != 0 | ||
&& tiles[y_off + x + 1].index == 0 | ||
&& tiles[y_off_minus_one + x + 1].index == 0 | ||
&& tiles[y_off_minus_one + x].index == 0 | ||
{ | ||
changed_tiles += 1; | ||
// set current tile to 33, which is grass bottom left | ||
tiles[y_off + x].index = 33; | ||
tiles[y_off + x].flags = Default::default(); | ||
} | ||
// case 3: bottom right | ||
if tiles[y_off + x].index != 0 | ||
&& tiles[y_off + x + 1].index == 0 | ||
&& tiles[y_off_plus_one + x + 1].index == 0 | ||
&& tiles[y_off_plus_one + x].index == 0 | ||
{ | ||
changed_tiles += 1; | ||
// set current tile to 34, which is grass bottom right | ||
tiles[y_off + x].index = 34; | ||
tiles[y_off + x].flags = Default::default(); | ||
} | ||
// case 4: bottom left | ||
if tiles[y_off + x].index != 0 | ||
&& tiles[y_off + x - 1].index == 0 | ||
&& tiles[y_off_plus_one + x - 1].index == 0 | ||
&& tiles[y_off_plus_one + x].index == 0 | ||
{ | ||
changed_tiles += 1; | ||
// set current tile to 35, which is grass bottom left | ||
tiles[y_off + x].index = 35; | ||
tiles[y_off + x].flags = Default::default(); | ||
} | ||
} | ||
} | ||
log::info!( | ||
"Changed {} tiles of total {}, w: {}, h: {}", | ||
changed_tiles, | ||
tiles.len(), | ||
width, | ||
height, | ||
); | ||
|
||
Ok(AutoMapperOutputModes::DesignTileLayer { tiles }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "api-auto-mapper" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
editor-interface = { path = "../editor-interface" } | ||
|
||
api = { path = "../../lib/api" } | ||
api-wasm-macros = { path = "../../lib/api-wasm-macros" } | ||
|
||
once_cell = "1.20.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use api_wasm_macros::{guest_func_call_from_host_auto, impl_guest_functions_auto_mapper}; | ||
use editor_interface::auto_mapper::{ | ||
AutoMapperInputModes, AutoMapperInterface, AutoMapperModes, AutoMapperOutputModes, | ||
}; | ||
|
||
use api::read_param_from_host_ex; | ||
use api::upload_return_val; | ||
|
||
extern "Rust" { | ||
/// Returns an instance of the auto mapper | ||
fn mod_auto_mapper_new() -> Result<Box<dyn AutoMapperInterface>, String>; | ||
} | ||
|
||
pub struct ApiAutoMapper { | ||
/// The wasm state object | ||
state: Rc<RefCell<Option<Box<dyn AutoMapperInterface>>>>, | ||
} | ||
|
||
thread_local! { | ||
static API_AUTO_MAPPER: once_cell::unsync::Lazy<ApiAutoMapper> = once_cell::unsync::Lazy::new(|| ApiAutoMapper { state: Default::default(), }); | ||
} | ||
|
||
impl ApiAutoMapper { | ||
fn create(&self) -> Result<(), String> { | ||
let state = unsafe { mod_auto_mapper_new()? }; | ||
*self.state.borrow_mut() = Some(state); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub fn auto_mapper_new() { | ||
let res = API_AUTO_MAPPER.with(|g| g.create()); | ||
upload_return_val(res); | ||
} | ||
|
||
#[no_mangle] | ||
pub fn auto_mapper_drop() { | ||
API_AUTO_MAPPER.with(|g| *g.state.borrow_mut() = None); | ||
} | ||
|
||
#[impl_guest_functions_auto_mapper] | ||
impl AutoMapperInterface for ApiAutoMapper { | ||
#[guest_func_call_from_host_auto(option)] | ||
fn supported_modes(&self) -> Vec<AutoMapperModes> {} | ||
|
||
#[guest_func_call_from_host_auto(option)] | ||
fn run( | ||
&mut self, | ||
seed: u64, | ||
input: AutoMapperInputModes, | ||
) -> Result<AutoMapperOutputModes, String> { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.