Skip to content

Commit

Permalink
Merge pull request #90 from Jupeyy/pr_editor_improvements4
Browse files Browse the repository at this point in the history
Implement the auto mapper & creator.
  • Loading branch information
Jupeyy authored Jan 31, 2025
2 parents 3e5c97c + 61d26e9 commit 709f594
Show file tree
Hide file tree
Showing 45 changed files with 3,190 additions and 607 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ members = [
"lib/steam",
"game/game-state-wasm", "lib/native-display", "src/assets-server", "game/assets-base", "src/community-server",
"src/community-register-server",
"game/community", "lib/input-binds", "game/ghost", "game/client-ghost", "game/client-replay", "game/editor-wasm", "game/client-notifications", "src/editor-server", "src/extra-convert",
"game/community", "lib/input-binds", "game/ghost", "game/client-ghost", "game/client-replay", "game/editor-wasm", "game/client-notifications", "src/editor-server", "src/extra-convert", "game/editor-interface", "game/editor-auto-mapper-wasm",
"game/api-auto-mapper",
"examples/wasm-modules/auto-mapper",
]

[package]
Expand Down
18 changes: 18 additions & 0 deletions examples/wasm-modules/auto-mapper/Cargo.toml
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"]
126 changes: 126 additions & 0 deletions examples/wasm-modules/auto-mapper/src/lib.rs
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 })
}
}
12 changes: 12 additions & 0 deletions game/api-auto-mapper/Cargo.toml
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"
57 changes: 57 additions & 0 deletions game/api-auto-mapper/src/lib.rs
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> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::ops::{Index, IndexMut};
use map::map::groups::layers::tiles::{rotation_180, rotation_270, TileFlags, ROTATION_90};
use math::math::vector::{ivec2, ubvec4, vec2};

use super::graphic_tile::tile_flags_to_uv;

pub(super) type GraphicsBorderTilePos = vec2;
pub(super) type GraphicsBorderTileTex = ubvec4;

Expand Down Expand Up @@ -154,41 +156,7 @@ fn fill_tmp_tile(
scale: i32,
) {
// tile tex
let mut x0: u8 = 0;
let mut y0: u8 = 0;
let mut x1: u8 = x0 + 1;
let mut y1: u8 = y0;
let mut x2: u8 = x0 + 1;
let mut y2: u8 = y0 + 1;
let mut x3: u8 = x0;
let mut y3: u8 = y0 + 1;

if !(flags & TileFlags::XFLIP).is_empty() {
x0 = x2;
x1 = x3;
x2 = x3;
x3 = x0;
}

if !(flags & TileFlags::YFLIP).is_empty() {
y0 = y3;
y2 = y1;
y3 = y1;
y1 = y0;
}

if !(flags & TileFlags::ROTATE).is_empty() {
let mut tmp = x0;
x0 = x3;
x3 = x2;
x2 = x1;
x1 = tmp;
tmp = y0;
y0 = y3;
y3 = y2;
y2 = y1;
y1 = tmp;
}
let (x0, y0, x1, y1, x2, y2, x3, y3) = tile_flags_to_uv(flags);

tmp_tile.tex_coord_top_left.x = x0;
tmp_tile.tex_coord_top_left.y = y0;
Expand Down
10 changes: 8 additions & 2 deletions game/client-render-base/src/map/map_buffered/graphic_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ fn fill_tmp_tile_speedup(
);
}

fn fill_tmp_tile(tmp_tile: &mut GraphicTile, flags: TileFlags, index: u8, x: i32, y: i32) {
// tile tex
pub fn tile_flags_to_uv(flags: TileFlags) -> (u8, u8, u8, u8, u8, u8, u8, u8) {
let mut x0: u8 = 0;
let mut y0: u8 = 0;
let mut x1: u8 = x0 + 1;
Expand Down Expand Up @@ -170,6 +169,13 @@ fn fill_tmp_tile(tmp_tile: &mut GraphicTile, flags: TileFlags, index: u8, x: i32
y1 = tmp;
}

(x0, y0, x1, y1, x2, y2, x3, y3)
}

fn fill_tmp_tile(tmp_tile: &mut GraphicTile, flags: TileFlags, index: u8, x: i32, y: i32) {
// tile tex
let (x0, y0, x1, y1, x2, y2, x3, y3) = tile_flags_to_uv(flags);

tmp_tile.tex_coord_top_left.x |= x0;
tmp_tile.tex_coord_top_left.x |= y0 << 1;
tmp_tile.tex_coord_bottom_left.x |= x3;
Expand Down
1 change: 1 addition & 0 deletions game/client-ui/src/actionfeed/kill_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn render(
rect.min.y + rect.height() / 2.0,
),
vec2::new(tee_size * 3.0 / 2.0, tee_size / 2.0),
None,
);
ui.add_space(5.0);
}
Expand Down
1 change: 1 addition & 0 deletions game/client-ui/src/hud/main_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub fn render(ui: &mut egui::Ui, pipe: &mut UiRenderPipe<UserData>, ui_state: &m
rect.center().y - tee_size / 4.0,
),
vec2::new(tee_size / 2.0, tee_size),
None,
);
}

Expand Down
Loading

0 comments on commit 709f594

Please sign in to comment.