rust/lib-hwengine-future/src/lib.rs
branchtransitional_engine
changeset 16049 db18f1a30b0c
parent 16040 6c5b3c576fc6
child 16053 85d7d6b71087
equal deleted inserted replaced
16048:67469603872a 16049:db18f1a30b0c
     1 mod ai;
     1 mod ai;
     2 
     2 
     3 use integral_geometry::{Point, Size};
     3 use integral_geometry::{Point, Size};
     4 
     4 
       
     5 use ai::*;
     5 use landgen::{
     6 use landgen::{
     6     outline_template_based::outline_template::OutlineTemplate,
     7     maze::MazeTemplate, outline_template_based::outline_template::OutlineTemplate,
     7     maze::MazeTemplate,
       
     8     wavefront_collapse::generator::TemplateDescription as WfcTemplate, LandGenerationParameters,
     8     wavefront_collapse::generator::TemplateDescription as WfcTemplate, LandGenerationParameters,
     9     LandGenerator,
     9     LandGenerator,
    10 };
    10 };
    11 use lfprng::LaggedFibonacciPRNG;
    11 use lfprng::LaggedFibonacciPRNG;
    12 use mapgen::{theme::Theme, MapGenerator};
    12 use mapgen::{theme::Theme, MapGenerator};
    13 use std::fs;
    13 use std::fs;
    14 use std::{ffi::CStr, path::Path};
    14 use std::{ffi::CStr, path::Path};
    15 use ai::*;
    15 use std::ptr::slice_from_raw_parts;
    16 
    16 
    17 #[repr(C)]
    17 #[repr(C)]
    18 pub struct GameField {
    18 pub struct GameField {
    19     collision: land2d::Land2D<u16>,
    19     collision: land2d::Land2D<u16>,
    20     pixels: land2d::Land2D<u32>,
    20     pixels: land2d::Land2D<u32>,
    22 }
    22 }
    23 
    23 
    24 #[no_mangle]
    24 #[no_mangle]
    25 pub extern "C" fn get_game_field_parameters(
    25 pub extern "C" fn get_game_field_parameters(
    26     game_field: &GameField,
    26     game_field: &GameField,
    27     width: *mut i32,
    27     width: &mut i32,
    28     height: *mut i32,
    28     height: &mut i32,
    29     play_width: *mut i32,
    29     play_width: &mut i32,
    30     play_height: *mut i32,
    30     play_height: &mut i32,
    31 ) {
    31 ) {
    32     unsafe {
    32     *width = game_field.collision.width() as i32;
    33         *width = game_field.collision.width() as i32;
    33     *height = game_field.collision.height() as i32;
    34         *height = game_field.collision.height() as i32;
    34 
    35 
    35     *play_width = game_field.collision.play_width() as i32;
    36         *play_width = game_field.collision.play_width() as i32;
    36     *play_height = game_field.collision.play_height() as i32;
    37         *play_height = game_field.collision.play_height() as i32;
       
    38     }
       
    39 }
    37 }
    40 
    38 
    41 #[no_mangle]
    39 #[no_mangle]
    42 pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField {
    40 pub extern "C" fn create_empty_game_field(width: u32, height: u32) -> *mut GameField {
    43     let game_field = Box::new(GameField {
    41     let game_field = Box::new(GameField {
    48 
    46 
    49     Box::into_raw(game_field)
    47     Box::into_raw(game_field)
    50 }
    48 }
    51 
    49 
    52 #[no_mangle]
    50 #[no_mangle]
    53 pub extern "C" fn generate_outline_templated_game_field(
    51 pub unsafe extern "C" fn generate_outline_templated_game_field(
    54     feature_size: u32,
    52     feature_size: u32,
    55     seed: *const i8,
    53     seed: *const i8,
    56     template_type: *const i8,
    54     template_type: *const i8,
    57     data_path: *const i8,
    55     data_path: *const i8,
    58 ) -> *mut GameField {
    56 ) -> *mut GameField {
    59     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
    57     let data_path: &str = CStr::from_ptr(data_path).to_str().unwrap();
    60     let data_path = Path::new(&data_path);
    58     let data_path = Path::new(&data_path);
    61 
    59 
    62     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
    60     let seed: &str = CStr::from_ptr(seed).to_str().unwrap();
    63     let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap();
    61     let template_type: &str = CStr::from_ptr(template_type).to_str().unwrap();
    64 
    62 
    65     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
    63     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
    66 
    64 
    67     let yaml_templates =
    65     let yaml_templates =
    68         fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path())
    66         fs::read_to_string(data_path.join(Path::new("map_templates.yaml")).as_path())
    88 
    86 
    89     Box::into_raw(game_field)
    87     Box::into_raw(game_field)
    90 }
    88 }
    91 
    89 
    92 #[no_mangle]
    90 #[no_mangle]
    93 pub extern "C" fn generate_wfc_templated_game_field(
    91 pub unsafe extern "C" fn generate_wfc_templated_game_field(
    94     feature_size: u32,
    92     feature_size: u32,
    95     seed: *const i8,
    93     seed: *const i8,
    96     template_type: *const i8,
    94     template_type: *const i8,
    97     data_path: *const i8,
    95     data_path: *const i8,
    98 ) -> *mut GameField {
    96 ) -> *mut GameField {
    99     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
    97     let data_path: &str = CStr::from_ptr(data_path).to_str().unwrap();
   100     let data_path = Path::new(&data_path);
    98     let data_path = Path::new(&data_path);
   101 
    99 
   102     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
   100     let seed: &str = CStr::from_ptr(seed).to_str().unwrap();
   103     let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap();
   101     let template_type: &str = CStr::from_ptr(template_type).to_str().unwrap();
   104 
   102 
   105     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
   103     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
   106 
   104 
   107     let yaml_templates =
   105     let yaml_templates =
   108         fs::read_to_string(data_path.join(Path::new("wfc_templates.yaml")).as_path())
   106         fs::read_to_string(data_path.join(Path::new("wfc_templates.yaml")).as_path())
   128 
   126 
   129     Box::into_raw(game_field)
   127     Box::into_raw(game_field)
   130 }
   128 }
   131 
   129 
   132 #[no_mangle]
   130 #[no_mangle]
   133 pub extern "C" fn generate_maze_game_field(
   131 pub unsafe extern "C" fn generate_maze_game_field(
   134     feature_size: u32,
   132     feature_size: u32,
   135     seed: *const i8,
   133     seed: *const i8,
   136     template_type: *const i8,
   134     template_type: *const i8,
   137     data_path: *const i8,
   135     data_path: *const i8,
   138 ) -> *mut GameField {
   136 ) -> *mut GameField {
   139     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
   137     let data_path: &str = CStr::from_ptr(data_path).to_str().unwrap();
   140     let data_path = Path::new(&data_path);
   138     let data_path = Path::new(&data_path);
   141 
   139 
   142     let seed: &str = unsafe { CStr::from_ptr(seed) }.to_str().unwrap();
   140     let seed: &str = CStr::from_ptr(seed).to_str().unwrap();
   143     let template_type: &str = unsafe { CStr::from_ptr(template_type) }.to_str().unwrap();
   141     let template_type: &str = CStr::from_ptr(template_type).to_str().unwrap();
   144 
   142 
   145     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
   143     let mut random_numbers_gen = LaggedFibonacciPRNG::new(seed.as_bytes());
   146 
   144 
   147     let yaml_templates =
   145     let yaml_templates =
   148         fs::read_to_string(data_path.join(Path::new("maze_templates.yaml")).as_path())
   146         fs::read_to_string(data_path.join(Path::new("maze_templates.yaml")).as_path())
   171 
   169 
   172     Box::into_raw(game_field)
   170     Box::into_raw(game_field)
   173 }
   171 }
   174 
   172 
   175 #[no_mangle]
   173 #[no_mangle]
   176 pub extern "C" fn apply_theme(
   174 pub unsafe extern "C" fn apply_theme(
   177     game_field: &mut GameField,
   175     game_field: &mut GameField,
   178     data_path: *const i8,
   176     data_path: *const i8,
   179     theme_name: *const i8,
   177     theme_name: *const i8,
   180 ) {
   178 ) {
   181     let data_path: &str = unsafe { CStr::from_ptr(data_path) }.to_str().unwrap();
   179     let data_path: &str = CStr::from_ptr(data_path).to_str().unwrap();
   182     let data_path = Path::new(&data_path);
   180     let data_path = Path::new(&data_path);
   183 
   181 
   184     let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
   182     let theme_name: &str = CStr::from_ptr(theme_name).to_str().unwrap();
   185     let map_gen = MapGenerator::<()>::new(data_path);
   183     let map_gen = MapGenerator::<()>::new(data_path);
   186 
   184 
   187     let theme = Theme::load(
   185     let theme = Theme::load(
   188         data_path
   186         data_path
   189             .join(Path::new("Themes"))
   187             .join(Path::new("Themes"))
   242 pub extern "C" fn land_pixel_row(game_field: &mut GameField, row: i32) -> *mut u32 {
   240 pub extern "C" fn land_pixel_row(game_field: &mut GameField, row: i32) -> *mut u32 {
   243     game_field.pixels[row as usize].as_mut_ptr()
   241     game_field.pixels[row as usize].as_mut_ptr()
   244 }
   242 }
   245 
   243 
   246 #[no_mangle]
   244 #[no_mangle]
   247 pub extern "C" fn dispose_game_field(game_field: *mut GameField) {
   245 pub unsafe extern "C" fn dispose_game_field(game_field: *mut GameField) {
   248     unsafe { drop(Box::from_raw(game_field)) };
   246     drop(Box::from_raw(game_field));
   249 }
   247 }
   250 
   248 
   251 #[no_mangle]
   249 #[no_mangle]
   252 pub extern "C" fn create_ai(game_field: &GameField) -> *mut AI {
   250 pub extern "C" fn create_ai(game_field: &GameField) -> *mut AI {
   253     Box::into_raw(Box::new(AI::new(game_field)))
   251     Box::into_raw(Box::new(AI::new(game_field)))
   257 pub extern "C" fn ai_clear_team(ai: &mut AI) {
   255 pub extern "C" fn ai_clear_team(ai: &mut AI) {
   258     *ai.get_team_mut() = vec![];
   256     *ai.get_team_mut() = vec![];
   259 }
   257 }
   260 
   258 
   261 #[no_mangle]
   259 #[no_mangle]
   262 pub extern "C" fn ai_add_team_hedgehog(ai: &mut AI, x: f32, y: f32) {
   260 pub unsafe extern "C" fn ai_add_team_hedgehog(ai: &mut AI, x: f32, y: f32, ammo_counts: *const u32) {
   263     ai.get_team_mut().push(Hedgehog{x, y});
   261     let ammo_counts = &*slice_from_raw_parts(ammo_counts, crate::ai::ammo::AmmoType::Count as usize);
   264 }
   262     let ammo_counts = std::array::from_fn(|i| ammo_counts[i].clone());
   265 
   263 
   266 #[no_mangle]
   264     ai.get_team_mut().push(Hedgehog { x, y, ammo: ammo_counts });
   267 pub extern "C" fn ai_think(ai: &AI) {
   265 }
   268 
   266 
   269 }
   267 #[no_mangle]
       
   268 pub extern "C" fn ai_think(ai: &AI) {}
   270 
   269 
   271 #[no_mangle]
   270 #[no_mangle]
   272 pub extern "C" fn ai_have_plan(ai: &AI) -> bool {
   271 pub extern "C" fn ai_have_plan(ai: &AI) -> bool {
   273     ai.have_plan()
   272     ai.have_plan()
   274 }
   273 }
   277 pub extern "C" fn ai_get(ai: &AI) -> bool {
   276 pub extern "C" fn ai_get(ai: &AI) -> bool {
   278     ai.have_plan()
   277     ai.have_plan()
   279 }
   278 }
   280 
   279 
   281 #[no_mangle]
   280 #[no_mangle]
   282 pub extern "C" fn dispose_ai(ai: *mut AI) {
   281 pub unsafe extern "C" fn dispose_ai(ai: *mut AI) {
   283     unsafe { drop(Box::from_raw(ai)) };
   282     drop(Box::from_raw(ai));
   284 }
   283 }