summaryrefslogtreecommitdiffhomepage
path: root/src/world.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/world.cpp')
-rw-r--r--src/world.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/world.cpp b/src/world.cpp
index d1c5dd43..edab80b6 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -2,6 +2,8 @@
#include "chunk.hpp"
#include "object.hpp"
#include "critter.hpp"
+#include "scenery.hpp"
+#include "light.hpp"
#include "compat/shared-ptr-wrapper.hpp"
#include "compat/int-hash.hpp"
#include "compat/exception.hpp"
@@ -263,4 +265,27 @@ shared_ptr_wrapper<critter> world::ensure_player_character(object_id& id_, critt
return ret;
}
+template<typename T>
+std::shared_ptr<T> world::find_object(object_id id)
+{
+ static_assert(std::is_base_of_v<object, T>);
+ // make it a dependent name so that including "src/object.hpp" isn't needed
+ using U = std::conditional_t<std::is_same_v<T, object>, T, object>;
+ if (std::shared_ptr<U> ptr = find_object_(id); !ptr)
+ 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);
+ return static_pointer_cast<T>(move(ptr));
+ }
+}
+
+template std::shared_ptr<object> world::find_object(object_id id);
+template std::shared_ptr<critter> world::find_object(object_id id);
+template std::shared_ptr<scenery> world::find_object(object_id id);
+template std::shared_ptr<light> world::find_object(object_id id);
+
} // namespace floormat