diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-14 08:53:35 +0200 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-14 09:22:54 +0200 |
| commit | 9d35200ea54a0729d489dc2c76823a2a13cdcac4 (patch) | |
| tree | afe10e017dcc226750c0023d0787df912927e6cd /src/world.cpp | |
| parent | c8ae1f808a16d664ec229ca3a2237ac39bf1164d (diff) | |
add find_object specialization for scenery subtypes
Diffstat (limited to 'src/world.cpp')
| -rw-r--r-- | src/world.cpp | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/src/world.cpp b/src/world.cpp index 9f7c0cc3..dd6f2ca7 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -196,7 +196,12 @@ void world::set_object_counter(object_id value) void world::throw_on_wrong_object_type(object_id id, object_type actual, object_type expected) { - fm_throw("object '{}' has wrong object type '{}', should be '{}'"_cf, id, (size_t)actual, (size_t)expected); + fm_throw("object {} has wrong object type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); +} + +void world::throw_on_wrong_scenery_type(object_id id, scenery_type actual, scenery_type expected) +{ + fm_throw("object {} has wrong scenery type {}, should be {}"_cf, id, (size_t)actual, (size_t)expected); } void world::throw_on_empty_scenery_proto(object_id id, global_coords pos, Vector2b offset) @@ -285,18 +290,30 @@ std::shared_ptr<T> world::find_object(object_id id) return {}; else if constexpr(std::is_same_v<T, object>) return ptr; + else if (ptr->type() != object_type_<T>::value) [[unlikely]] + throw_on_wrong_object_type(id, ptr->type(), object_type_<T>::value); + else + return static_pointer_cast<T>(move(ptr)); +} + +template<typename T> +requires is_strict_base_of<scenery, T> +std::shared_ptr<T> world::find_object(object_id id) +{ + if (auto ptr = find_object<scenery>(id); !ptr) + return {}; + else if (ptr->scenery_type() != scenery_type_<T>::value) [[unlikely]] + throw_on_wrong_scenery_type(id, ptr->scenery_type(), scenery_type_<T>::value); else - { - if (!(ptr->type() == object_type_<T>::value)) [[unlikely]] - throw_on_wrong_object_type(id, ptr->type(), object_type_<T>::value); return static_pointer_cast<T>(move(ptr)); - } } template std::shared_ptr<object> world::find_object<object>(object_id id); template std::shared_ptr<critter> world::find_object<critter>(object_id id); template std::shared_ptr<scenery> world::find_object<scenery>(object_id id); template std::shared_ptr<light> world::find_object<light>(object_id id); +template std::shared_ptr<generic_scenery> world::find_object<generic_scenery>(object_id id); +template std::shared_ptr<door_scenery> world::find_object<door_scenery>(object_id id); template<bool sorted> std::shared_ptr<scenery> world::make_scenery(object_id id, global_coords pos, scenery_proto&& proto) |
