diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-11 04:18:11 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-09-11 14:20:10 +0200 |
commit | be8ab17542bea5d783166f320cf3ed02b7b88b20 (patch) | |
tree | 5bf6fae735faa69b84a87384cb48336e9cfa9fe6 | |
parent | 1af31374db91b0fd6091b27d4c199edaee989612 (diff) |
wip
-rw-r--r-- | src/path-search.cpp | 18 | ||||
-rw-r--r-- | src/path-search.hpp | 5 | ||||
-rw-r--r-- | test/app.hpp | 5 | ||||
-rw-r--r-- | test/path-search.cpp | 29 | ||||
-rw-r--r-- | test/serializer.cpp | 8 |
5 files changed, 50 insertions, 15 deletions
diff --git a/src/path-search.cpp b/src/path-search.cpp index 5558fa19..79ba5fa8 100644 --- a/src/path-search.cpp +++ b/src/path-search.cpp @@ -79,12 +79,11 @@ bool search::sample_rtree(world& w, global_coords coord, Vector2b offset, Vector return sample_rtree(w, coord, Vector2(center), Vector2(size), own_id); } -auto search::make_neighbor_tile_bbox(global_coords coord, Vector2ub own_size, rotation r) -> bbox +auto search::make_neighbor_tile_bbox(Vector2i coord, Vector2ub own_size, rotation r) -> bbox { constexpr auto full_tile = Vector2ui(iTILE_SIZE2*3/4); - constexpr auto tx = iTILE_SIZE2.x()*2u, ty = iTILE_SIZE2.y()*2u; + constexpr auto tx = full_tile.x()*2u, ty = full_tile.y()*2u; - fm_assert(Vector2i(own_size).product() != 0); const auto s = Math::max(Vector2ui(own_size), full_tile); const auto sx = s[0], sy = s[1]; @@ -93,14 +92,14 @@ auto search::make_neighbor_tile_bbox(global_coords coord, Vector2ub own_size, ro switch (r) { - case rotation::N: off = { 0, -1}; size = {sx, ty}; break; case rotation::E: off = { 1, 0}; size = {tx, sy}; break; - case rotation::S: off = { 0, 1}; size = {tx, sy}; break; - case rotation::W: off = {-1, 0}; size = {sx, ty}; break; + case rotation::N: off = { 0, -1}; size = {sx, ty}; break; + case rotation::S: off = { 0, 1}; size = {sx, ty}; break; + case rotation::W: off = {-1, 0}; size = {tx, sy}; break; default: fm_abort("wrong 4-way rotation enum value '%d", (int)r); } - auto center1 = Vector2i(coord.local()) * iTILE_SIZE2; + auto center1 = coord * iTILE_SIZE2; auto center2 = center1 + off*iTILE_SIZE2; auto center = (center1 + center2)/2; @@ -129,12 +128,15 @@ Optional<search_result> search::operator()(world& w, object_id own_id, Optional<search_result> search::operator()(world& w, const object& obj, global_coords to, Vector2b to_offset) { + constexpr auto full_tile = Vector2ub(iTILE_SIZE2*3/4); + auto size = Math::max(obj.bbox_size, full_tile); + // todo fixme // if bbox_offset is added to obj's offset, then all coordinates in the paths are shifted by bbox_offset. // maybe add handling to subtract bbox_offset from the returned path. // for that it needs to be passed into callees separately. fm_assert(obj.bbox_offset.isZero()); - return operator()(w, obj.id, obj.coord, obj.offset, obj.bbox_size, to, to_offset); + return operator()(w, obj.id, obj.coord, obj.offset, size, to, to_offset); } } // namespace floormat diff --git a/src/path-search.hpp b/src/path-search.hpp index 39fde1e4..f044a381 100644 --- a/src/path-search.hpp +++ b/src/path-search.hpp @@ -69,7 +69,6 @@ class search final }; struct obj_position { Vector2 center, size; }; - struct bbox { Vector2 min, max; }; chunk_cache cache; Array<global_coords> output; @@ -80,6 +79,8 @@ class search final void ensure_allocated(chunk_coords a, chunk_coords b); public: + struct bbox { Vector2 min, max; }; + // todo remember to check from.z() == to.z() // todo add simple bresenham short-circuit Optional<search_result> operator()(world& w, object_id own_id, global_coords from, Vector2b from_offset, Vector2ub size, global_coords to, Vector2b to_offset); @@ -89,7 +90,7 @@ public: static bool sample_rtree(world& w, chunk_coords_ ch0, Vector2 center, Vector2 size, object_id own_id); static bool sample_rtree(world& w, global_coords coord, Vector2b offset, Vector2ub size, object_id own_id); - static bbox make_neighbor_tile_bbox(global_coords coord, Vector2ub own_size, rotation r); + static bbox make_neighbor_tile_bbox(Vector2i coord, Vector2ub own_size, rotation r); static neighbors get_walkable_neighbor_tiles(world& w, global_coords pos, Vector2 size, object_id own_id); }; diff --git a/test/app.hpp b/test/app.hpp index 04ed55b7..f3e57da2 100644 --- a/test/app.hpp +++ b/test/app.hpp @@ -17,6 +17,7 @@ namespace floormat { struct chunk_coords; +struct chunk_coords_; struct chunk; struct world; @@ -25,7 +26,11 @@ struct test_app final : private FM_APPLICATION using Application = FM_APPLICATION; explicit test_app(const Arguments& arguments); ~test_app(); + + static chunk& make_test_chunk(world& w, chunk_coords_ ch); + int exec() override; + static void test_json(); static void test_tile_iter(); static void test_const_math(); diff --git a/test/path-search.cpp b/test/path-search.cpp index 7bbd04c0..faa16a06 100644 --- a/test/path-search.cpp +++ b/test/path-search.cpp @@ -1,6 +1,10 @@ #include "app.hpp" -#include "src/path-search.hpp" #include "compat/assert.hpp" +#include "loader/loader.hpp" +#include "src/world.hpp" +#include "src/scenery.hpp" + +#include "src/path-search.hpp" namespace floormat { @@ -8,7 +12,30 @@ namespace { void test_bbox() { + auto metal2 = loader.tile_atlas("metal2", {2, 2}, pass_mode::blocked); + auto table = loader.scenery("table1"); + { + auto w = world(); + auto& c12 = w[chunk_coords_{1, 2, 0}]; + [[maybe_unused]] auto& c11 = w[chunk_coords_{1, 1, 0}]; + c12[{0, 0}].wall_north() = {metal2, 0}; + + constexpr auto sample = [](chunk& c, search::bbox bb) { + return search::sample_rtree_1(c, bb.min, bb.max, (object_id)-1); + }; + + constexpr auto bbox = [](Vector2i coord, rotation r) { + return search::make_neighbor_tile_bbox(coord, {}, r); + }; + + using enum rotation; + fm_assert( !sample(c12, bbox({0, 0}, N)) ); + fm_assert( sample(c12, bbox({0, 0}, E)) ); + fm_assert( sample(c12, bbox({0, 0}, S)) ); + fm_assert( sample(c12, bbox({0, 0}, W)) ); + } + // todo use test chunk } } // namespace diff --git a/test/serializer.cpp b/test/serializer.cpp index 9387f827..dd2d090d 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -13,9 +13,7 @@ namespace floormat { namespace Path = Corrade::Utility::Path; -namespace { - -chunk& make_test_chunk(world& w, chunk_coords_ ch) +chunk& test_app::make_test_chunk(world& w, chunk_coords_ ch) { chunk& c = w[ch]; c.mark_modified(); @@ -47,6 +45,8 @@ chunk& make_test_chunk(world& w, chunk_coords_ ch) return c; } +namespace { + void assert_chunks_equal(const chunk& a, const chunk& b) { fm_assert(a.objects().size() == b.objects().size()); @@ -103,7 +103,7 @@ void test_serializer(StringView input, StringView tmp) { coord = {1, 1, 0}; w = world(); - make_test_chunk(w, coord); + test_app::make_test_chunk(w, coord); } w.serialize(tmp); auto w2 = world::deserialize(tmp); |