diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-19 02:12:05 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-19 02:15:16 +0100 |
commit | 94c531bd05ab6b49996113faff9584cb6b534b9c (patch) | |
tree | 273aae5c8ee1f8c2801a2c5ca05ca52422e49a35 | |
parent | 703c19301f2e93fb59b468bdd635a9cc763fad3f (diff) |
loader: chdir to share/floormat
-rw-r--r-- | editor/save.cpp | 9 | ||||
-rw-r--r-- | loader/filesystem.cpp | 3 | ||||
-rw-r--r-- | loader/loader.cpp | 7 | ||||
-rw-r--r-- | loader/loader.hpp | 1 | ||||
-rw-r--r-- | src/entity.cpp | 38 | ||||
-rw-r--r-- | src/entity.hpp | 4 | ||||
-rw-r--r-- | src/world.cpp | 2 | ||||
-rw-r--r-- | test/json.cpp | 4 | ||||
-rw-r--r-- | test/serializer.cpp | 10 |
9 files changed, 49 insertions, 29 deletions
diff --git a/editor/save.cpp b/editor/save.cpp index 6d79bfce..80274875 100644 --- a/editor/save.cpp +++ b/editor/save.cpp @@ -1,16 +1,18 @@ #include "app.hpp" #include "floormat/main.hpp" #include "src/world.hpp" +#include "loader/loader.hpp" #include <Corrade/Utility/Path.h> namespace floormat { -#define save_dir "../save" +#define save_dir "save" #define quicksave_file save_dir "/" "quicksave.dat" #define quicksave_tmp save_dir "/" "quicksave.tmp" static bool ensure_save_directory() { + auto dir = Path::join(loader.TEMP_PATH, save_dir); if (Path::make(save_dir)) return true; else @@ -36,16 +38,17 @@ void app::do_quicksave() void app::do_quickload() { + auto file = Path::join(loader.TEMP_PATH, quicksave_file); kill_popups(true); if (!ensure_save_directory()) return; - if (!Path::exists(quicksave_file)) + if (!Path::exists(file)) { fm_warn("no quicksave"); return; } fputs("quickload... ", stderr); fflush(stderr); - reset_world(world::deserialize(quicksave_file)); + reset_world(world::deserialize(file)); fputs("done\n", stderr); fflush(stderr); } diff --git a/loader/filesystem.cpp b/loader/filesystem.cpp index b86915d4..a7926da1 100644 --- a/loader/filesystem.cpp +++ b/loader/filesystem.cpp @@ -75,7 +75,8 @@ void loader_impl::set_application_working_directory() if (c == '/') c = '\\'; #endif - chdir(path); + chdir(path) && + chdir("share/floormat"_s); } else fm_warn("can't find install prefix!"); diff --git a/loader/loader.cpp b/loader/loader.cpp index e9401385..eccfb350 100644 --- a/loader/loader.cpp +++ b/loader/loader.cpp @@ -33,8 +33,9 @@ StringView loader_::strip_prefix(StringView name) return name; } -const StringView loader_::IMAGE_PATH = "share/floormat/images/"_s; -const StringView loader_::ANIM_PATH = "share/floormat/anim/"_s; -const StringView loader_::SCENERY_PATH = "share/floormat/scenery/"_s; +const StringView loader_::IMAGE_PATH = "images/"_s; +const StringView loader_::ANIM_PATH = "anim/"_s; +const StringView loader_::SCENERY_PATH = "scenery/"_s; +const StringView loader_::TEMP_PATH = "../../../"_s; } // namespace floormat diff --git a/loader/loader.hpp b/loader/loader.hpp index e81abadf..f2617f04 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -41,6 +41,7 @@ struct loader_ static const StringView IMAGE_PATH; static const StringView ANIM_PATH; static const StringView SCENERY_PATH; + static const StringView TEMP_PATH; protected: loader_(); diff --git a/src/entity.cpp b/src/entity.cpp index 70e0ffb5..8df9a9ce 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -88,6 +88,13 @@ size_t entity::index() const return (size_t)std::distance(es.cbegin(), it); } +bool entity::can_rotate(global_coords coord, rotation new_r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) +{ + bbox_offset = rotate_point(bbox_offset, old_r, new_r); + bbox_size = rotate_size(bbox_size, old_r, new_r); + return can_move_to({}, coord, offset, bbox_offset, bbox_size); +} + void entity::rotate(size_t, rotation new_r) { fm_assert(atlas->check_rotation(new_r)); @@ -119,14 +126,14 @@ Pair<global_coords, Vector2b> entity::normalize_coords(global_coords coord, Vect return { coord, Vector2b(off_new) }; } -bool entity::can_move_to(Vector2i delta) +bool entity::can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size) { auto [coord_, offset_] = normalize_coords(coord, offset, delta); auto& w = *c->_world; auto& c_ = coord.chunk() == coord_.chunk() ? *c : w[coord_.chunk()]; const auto center = Vector2(coord_.local())*TILE_SIZE2 + Vector2(offset_) + Vector2(bbox_offset), - half_bbox = Vector2(bbox_size/2), + half_bbox = Vector2(bbox_size)*.5f, min = center - half_bbox, max = min + Vector2(bbox_size); bool ret = true; @@ -140,35 +147,40 @@ bool entity::can_move_to(Vector2i delta) return ret; } +bool entity::can_move_to(Vector2i delta) +{ + return can_move_to(delta, coord, offset, bbox_offset, bbox_size); +} + size_t entity::move(size_t i, Vector2i delta, rotation new_r) { auto& es = c->_entities; fm_debug_assert(i < es.size()); auto e_ = es[i]; - const auto& e = *e_; auto& w = *c->_world; - const auto coord = e.coord; - const auto offset = e.offset; const auto [coord_, offset_] = normalize_coords(coord, offset, delta); + if (!can_rotate(coord, new_r, r, offset, bbox_offset, bbox_size)) + return i; + if (coord_ == coord && offset_ == offset) return i; - if (!e.is_dynamic()) + if (!is_dynamic()) c->mark_scenery_modified(); chunk::bbox bb0, bb1; const auto bb_offset = rotate_point(bbox_offset, r, new_r); const auto bb_size = rotate_size(bbox_size, r, new_r); - bool b0 = c->_bbox_for_scenery(e, bb0), - b1 = c->_bbox_for_scenery(e, coord_.local(), offset_, bb_offset, bb_size, bb1); - const auto ord = e.ordinal(coord_.local(), offset_, e.type); + bool b0 = c->_bbox_for_scenery(*this, bb0), + b1 = c->_bbox_for_scenery(*this, coord_.local(), offset_, bb_offset, bb_size, bb1); + const auto ord = ordinal(coord_.local(), offset_, type); if (coord_.chunk() == coord.chunk()) { c->_replace_bbox(bb0, bb1, b0, b1); auto it_ = std::lower_bound(es.cbegin(), es.cend(), e_, [=](const auto& a, const auto&) { return a->ordinal() < ord; }); - e_->coord = coord_; + const_cast<global_coords&>(coord) = coord_; set_bbox_(offset_, bb_offset, bb_size, pass); const_cast<rotation&>(r) = new_r; auto pos1 = std::distance(es.cbegin(), it_); @@ -187,17 +199,17 @@ size_t entity::move(size_t i, Vector2i delta, rotation new_r) { //fm_debug("change-chunk (%hd;%hd|%hhd;%hhd)", coord_.chunk().x, coord_.chunk().y, coord_.local().x, coord_.local().y); auto& c2 = w[coord_.chunk()]; - if (!e.is_dynamic()) + if (!is_dynamic()) c2.mark_scenery_modified(); c2._add_bbox(bb1); c->remove_entity(i); auto& es = c2._entities; auto it = std::lower_bound(es.cbegin(), es.cend(), e_, [=](const auto& a, const auto&) { return a->ordinal() < ord; }); auto ret = (size_t)std::distance(es.cbegin(), it); - e_->coord = coord_; + const_cast<global_coords&>(coord) = coord_; set_bbox_(offset_, bb_offset, bb_size, pass); const_cast<rotation&>(r) = new_r; - const_cast<struct chunk*&>(e_->c) = &c2; + const_cast<struct chunk*&>(c) = &c2; es.insert(it, std::move(e_)); return ret; } diff --git a/src/entity.hpp b/src/entity.hpp index d8d3c63e..3f82842d 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -41,7 +41,7 @@ struct entity const object_id id = 0; struct chunk* const c; std::shared_ptr<anim_atlas> atlas; - global_coords coord; + const global_coords coord; const Vector2b offset, bbox_offset; const Vector2ub bbox_size; uint16_t delta = 0, frame = 0; @@ -62,10 +62,12 @@ struct entity virtual bool can_activate(size_t i) const; virtual bool activate(size_t i); virtual bool update(size_t i, float dt) = 0; + [[nodiscard]] virtual bool can_rotate(global_coords coord, rotation r, rotation old_r, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size); virtual void rotate(size_t i, rotation r); static Pair<global_coords, Vector2b> normalize_coords(global_coords coord, Vector2b cur_offset, Vector2i delta); [[nodiscard]] virtual bool can_move_to(Vector2i delta); + [[nodiscard]] bool can_move_to(Vector2i delta, global_coords coord, Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_aize); size_t move(size_t i, Vector2i delta, rotation new_r); virtual void set_bbox(Vector2b offset, Vector2b bbox_offset, Vector2ub bbox_size, pass_mode pass); bool is_dynamic() const; diff --git a/src/world.cpp b/src/world.cpp index ef85732e..07cfe196 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -118,7 +118,7 @@ void world::do_make_entity(const std::shared_ptr<entity>& e, global_coords pos, fm_assert(!_entities.contains(e->id)); fm_assert(Vector2ui(e->bbox_size).product() > 0); fm_assert(e->type != entity_type::none); - e->coord = pos; + const_cast<global_coords&>(e->coord) = pos; _entities[e->id] = e; if (sorted) e->c->add_entity(e); diff --git a/test/json.cpp b/test/json.cpp index 7d01a51b..8cc6ed96 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -36,8 +36,8 @@ static chunk make_test_chunk() void test_app::test_json() // NOLINT(readability-convert-member-functions-to-static) { - fm_assert(Path::exists("../CMakeCache.txt")); - constexpr auto output_dir = "../test/."_s; + fm_assert(Path::exists(Path::join(loader.TEMP_PATH, "CMakeCache.txt"))); + const auto output_dir = Path::join(loader.TEMP_PATH, "test/."_s); { auto atlas = loader.tile_atlas("metal1", {2, 2}, pass_mode::pass); json_helper::to_json(atlas, Path::join(output_dir, "atlas.json")); diff --git a/test/serializer.cpp b/test/serializer.cpp index 5a520dbb..62c3365e 100644 --- a/test/serializer.cpp +++ b/test/serializer.cpp @@ -106,16 +106,16 @@ void test_serializer(StringView input, StringView tmp) void test_app::test_serializer_1() { - fm_assert(Path::exists("../CMakeCache.txt")); - constexpr auto tmp_filename = "../test/test-serializer1.dat"_s; + fm_assert(Path::exists(Path::join(loader.TEMP_PATH, "CMakeCache.txt"))); + const auto tmp_filename = Path::join(loader.TEMP_PATH, "test/test-serializer1.dat"_s); test_serializer({}, tmp_filename); } void test_app::test_serializer_2() { - fm_assert(Path::exists("../CMakeCache.txt")); - constexpr auto tmp_filename = "../test/test-serializer2.dat"_s; - constexpr auto dir = "../test/save/"_s; + fm_assert(Path::exists(Path::join(loader.TEMP_PATH, "CMakeCache.txt"))); + const auto tmp_filename = Path::join(loader.TEMP_PATH, "test/test-serializer2.dat"_s); + const auto dir = Path::join(loader.TEMP_PATH, "test/save/"_s); using LF = Path::ListFlag; auto files = Path::list(dir, LF::SkipDirectories|LF::SkipSpecial|LF::SkipDotAndDotDot); fm_assert(files); |