From b4d7fc2c5b440f25a0cc0d77196e95b443484b95 Mon Sep 17 00:00:00 2001 From: Gtker Date: Tue, 11 Oct 2022 17:37:02 +0200 Subject: [PATCH 1/2] Make C API pointers const This ensures that the underlying object can still be mutated but the pointer location can not. This just ensures that calling functions don't have to be overly defensive about what is happening with pointers passed to the API. For example the Map pointer can't be changed to a nullptr, or to another Map object. --- MapBuilder/MapBuilder_c_bindings.cpp | 2 +- MapBuilder/mapbuilder_c_bindings.h | 2 +- pathfind/pathfind_c_bindings.cpp | 30 +++++++++++------------ pathfind/pathfind_c_bindings.hpp | 36 ++++++++++++++-------------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/MapBuilder/MapBuilder_c_bindings.cpp b/MapBuilder/MapBuilder_c_bindings.cpp index 706aa6b..a22b323 100644 --- a/MapBuilder/MapBuilder_c_bindings.cpp +++ b/MapBuilder/MapBuilder_c_bindings.cpp @@ -11,7 +11,7 @@ extern "C" { MapBuildResultType mapbuild_build_bvh(const char* const data_path, const char* const output_path, uint32_t threads, - uint32_t* amount_of_bvhs_built) + uint32_t* const amount_of_bvhs_built) { std::string outputPath = output_path; diff --git a/MapBuilder/mapbuilder_c_bindings.h b/MapBuilder/mapbuilder_c_bindings.h index 1a3a70c..ff84c5d 100644 --- a/MapBuilder/mapbuilder_c_bindings.h +++ b/MapBuilder/mapbuilder_c_bindings.h @@ -8,7 +8,7 @@ typedef uint8_t MapBuildResultType; MapBuildResultType mapbuild_build_bvh(const char* const data_path, const char* const output_path, uint32_t threads, - uint32_t* amount_of_bvhs_built); + uint32_t* const amount_of_bvhs_built); MapBuildResultType mapbuild_build_map(const char* const data_path, const char* const output_path, diff --git a/pathfind/pathfind_c_bindings.cpp b/pathfind/pathfind_c_bindings.cpp index d7f300f..6c41fba 100644 --- a/pathfind/pathfind_c_bindings.cpp +++ b/pathfind/pathfind_c_bindings.cpp @@ -23,11 +23,11 @@ pathfind::Map* pathfind_new_map(const char* const data_path, const char* const m } } -void pathfind_free_map(pathfind::Map* map) { +void pathfind_free_map(pathfind::Map* const map) { delete map; } -PathfindResultType pathfind_load_all_adts(pathfind::Map* map, int32_t* amount_of_adts_loaded) { +PathfindResultType pathfind_load_all_adts(pathfind::Map* const map, int32_t* const amount_of_adts_loaded) { try { *amount_of_adts_loaded = map->LoadAllADTs(); return static_cast(Result::SUCCESS); @@ -40,7 +40,7 @@ PathfindResultType pathfind_load_all_adts(pathfind::Map* map, int32_t* amount_of } } -PathfindResultType pathfind_load_adt(pathfind::Map* map, int adt_x, int adt_y, float* out_adt_x, float* out_adt_y) { +PathfindResultType pathfind_load_adt(pathfind::Map* const map, int adt_x, int adt_y, float* const out_adt_x, float* const out_adt_y) { try { if (!map->HasADT(adt_x, adt_y)) { return static_cast(Result::MAP_DOES_NOT_HAVE_ADT); @@ -63,7 +63,7 @@ PathfindResultType pathfind_load_adt(pathfind::Map* map, int adt_x, int adt_y, f } } -PathfindResultType pathfind_load_adt_at(pathfind::Map* map, float x, float y, float* out_adt_x, float* out_adt_y) { +PathfindResultType pathfind_load_adt_at(pathfind::Map* const map, float x, float y, float* const out_adt_x, float* const out_adt_y) { try { int adt_y = 0; int adt_x = 0; @@ -91,12 +91,12 @@ PathfindResultType pathfind_load_adt_at(pathfind::Map* map, float x, float y, fl } } -PathfindResultType pathfind_get_zone_and_area(pathfind::Map* map, +PathfindResultType pathfind_get_zone_and_area(pathfind::Map* const map, float x, float y, float z, - unsigned int* out_zone, - unsigned int* out_area) + unsigned int* const out_zone, + unsigned int* const out_area) { unsigned int zone = -1; unsigned int area = -1; @@ -122,16 +122,16 @@ PathfindResultType pathfind_get_zone_and_area(pathfind::Map* map, } } -PathfindResultType pathfind_find_path(pathfind::Map* map, +PathfindResultType pathfind_find_path(pathfind::Map* const map, float start_x, float start_y, float start_z, float stop_x, float stop_y, float stop_z, - Vertex* buffer, + Vertex* const buffer, unsigned int buffer_length, - unsigned int* amount_of_vertices) + unsigned int* const amount_of_vertices) { const math::Vertex start {start_x, start_y, start_z}; const math::Vertex stop {stop_x, stop_y, stop_z}; @@ -168,12 +168,12 @@ PathfindResultType pathfind_find_path(pathfind::Map* map, } } -PathfindResultType pathfind_find_heights(pathfind::Map* map, +PathfindResultType pathfind_find_heights(pathfind::Map* const map, float x, float y, - float* buffer, + float* const buffer, unsigned int buffer_length, - unsigned int* amount_of_heights) + unsigned int* const amount_of_heights) { std::vector height_values; @@ -202,10 +202,10 @@ PathfindResultType pathfind_find_heights(pathfind::Map* map, } } -PathfindResultType pathfind_find_height(pathfind::Map* map, float start_x, +PathfindResultType pathfind_find_height(pathfind::Map* const map, float start_x, float start_y, float start_z, float stop_x, float stop_y, - float* stop_z) + float* const stop_z) { try { diff --git a/pathfind/pathfind_c_bindings.hpp b/pathfind/pathfind_c_bindings.hpp index 87d4851..8a4ac78 100644 --- a/pathfind/pathfind_c_bindings.hpp +++ b/pathfind/pathfind_c_bindings.hpp @@ -17,37 +17,37 @@ pathfind::Map* pathfind_new_map(const char* const data_path, const char* const map_name, PathfindResultTypePtr result); -void pathfind_free_map(pathfind::Map* map); +void pathfind_free_map(pathfind::Map* const map); -PathfindResultType pathfind_load_all_adts(pathfind::Map* map, - int32_t* amount_of_adts_loaded); +PathfindResultType pathfind_load_all_adts(pathfind::Map* const map, + int32_t* const amount_of_adts_loaded); -PathfindResultType pathfind_load_adt(pathfind::Map* map, int adt_x, int adt_y, - float* out_adt_x, float* out_adt_y); +PathfindResultType pathfind_load_adt(pathfind::Map* const map, int adt_x, int adt_y, + float* const out_adt_x, float* const out_adt_y); -PathfindResultType pathfind_load_adt_at(pathfind::Map* map, float x, float y, - float* out_adt_x, float* out_adt_y); +PathfindResultType pathfind_load_adt_at(pathfind::Map* const map, float x, float y, + float* const out_adt_x, float* const out_adt_y); -PathfindResultType pathfind_get_zone_and_area(pathfind::Map* map, float x, +PathfindResultType pathfind_get_zone_and_area(pathfind::Map* const map, float x, float y, float z, - unsigned int* out_zone, - unsigned int* out_area); + unsigned int* const out_zone, + unsigned int* const out_area); -PathfindResultType pathfind_find_path(pathfind::Map* map, float start_x, +PathfindResultType pathfind_find_path(pathfind::Map* const map, float start_x, float start_y, float start_z, float stop_x, float stop_y, float stop_z, - Vertex* buffer, + Vertex* const buffer, unsigned int buffer_length, - unsigned int* amount_of_vertices); + unsigned int* const amount_of_vertices); -PathfindResultType pathfind_find_heights(pathfind::Map* map, float x, float y, - float* buffer, +PathfindResultType pathfind_find_heights(pathfind::Map* const map, float x, float y, + float* const buffer, unsigned int buffer_length, - unsigned int* amount_of_heights); + unsigned int* const amount_of_heights); -PathfindResultType pathfind_find_height(pathfind::Map* map, float start_x, +PathfindResultType pathfind_find_height(pathfind::Map* const map, float start_x, float start_y, float start_z, float stop_x, float stop_y, - float* stop_z); + float* const stop_z); } // extern "C" From a06858db24f080e24d37aebfba0b5b8c294968e4 Mon Sep 17 00:00:00 2001 From: Gtker Date: Tue, 11 Oct 2022 17:37:02 +0200 Subject: [PATCH 2/2] Add line of sight to C API --- pathfind/pathfind_c_bindings.cpp | 24 ++++++++++++++++++++++++ pathfind/pathfind_c_bindings.hpp | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/pathfind/pathfind_c_bindings.cpp b/pathfind/pathfind_c_bindings.cpp index 6c41fba..584dbe7 100644 --- a/pathfind/pathfind_c_bindings.cpp +++ b/pathfind/pathfind_c_bindings.cpp @@ -228,4 +228,28 @@ PathfindResultType pathfind_find_height(pathfind::Map* const map, float start_x, return static_cast(Result::UNKNOWN_EXCEPTION); } } + +PathfindResultType pathfind_line_of_sight(pathfind::Map* map, + float start_x, float start_y, float start_z, + float stop_x, float stop_y, float stop_z, + uint8_t* const line_of_sight) { + try + { + if (map->LineOfSight({start_x, start_y, start_z}, {stop_x, stop_y, stop_z})) { + *line_of_sight = 1; + } else { + *line_of_sight = 0; + } + + return static_cast(Result::SUCCESS); + } + catch (utility::exception& e) + { + return static_cast(e.ResultCode()); + } + catch (...) + { + return static_cast(Result::UNKNOWN_EXCEPTION); + } +} } // extern "C" diff --git a/pathfind/pathfind_c_bindings.hpp b/pathfind/pathfind_c_bindings.hpp index 8a4ac78..f5fd9ad 100644 --- a/pathfind/pathfind_c_bindings.hpp +++ b/pathfind/pathfind_c_bindings.hpp @@ -49,5 +49,10 @@ PathfindResultType pathfind_find_height(pathfind::Map* const map, float start_x, float start_y, float start_z, float stop_x, float stop_y, float* const stop_z); + +PathfindResultType pathfind_line_of_sight(pathfind::Map* const map, + float start_x, float start_y, float start_z, + float stop_x, float stop_y, float stop_z, + uint8_t* const line_of_sight); } // extern "C"