2222#include " moving_objects.h"
2323#include " doors_manager.h"
2424
25- CAI_Space* g_ai_space = 0 ;
25+ static CAI_Space g_ai_space;
2626
27- CAI_Space::CAI_Space ()
27+ CAI_Space& CAI_Space::GetInstance ()
2828{
29- m_ef_storage = 0 ;
30- m_cover_manager = 0 ;
31- m_alife_simulator = 0 ;
32- m_moving_objects = 0 ;
33- m_doors_manager = 0 ;
29+ auto & instance = g_ai_space;
30+ if (!instance.m_inited )
31+ {
32+ instance.init ();
33+ }
34+ return instance;
3435}
3536
3637void CAI_Space::init ()
3738{
38- if (GEnv.isDedicatedServer )
39- return ;
40- AISpaceBase::Initialize ();
41- VERIFY (!m_ef_storage);
42- m_ef_storage = new CEF_Storage ();
43-
44- VERIFY (!m_cover_manager);
45- m_cover_manager = new CCoverManager ();
46-
47- VERIFY (!m_moving_objects);
48- m_moving_objects = new ::moving_objects ();
49- VERIFY (!GEnv.ScriptEngine );
50- GEnv.ScriptEngine = new CScriptEngine ();
51- SetupScriptEngine ();
39+ R_ASSERT (!m_inited);
40+
41+ if (!GEnv.isDedicatedServer )
42+ {
43+ AISpaceBase::Initialize ();
44+
45+ m_ef_storage = std::make_unique<CEF_Storage>();
46+ m_cover_manager = std::make_unique<CCoverManager>();
47+ m_moving_objects = std::make_unique<::moving_objects>();
48+
49+ VERIFY (!GEnv.ScriptEngine );
50+ GEnv.ScriptEngine = new CScriptEngine ();
51+ SetupScriptEngine ();
52+ }
53+
54+ m_inited = true ;
5255}
5356
5457CAI_Space::~CAI_Space ()
5558{
5659 unload ();
5760 xr_delete (GEnv.ScriptEngine ); // XXX: wrapped into try..catch(...) in vanilla source
58- xr_delete (m_doors_manager);
59- xr_delete (m_moving_objects);
60- xr_delete (m_cover_manager);
61- xr_delete (m_ef_storage);
6261}
6362
6463void CAI_Space::RegisterScriptClasses ()
@@ -147,8 +146,7 @@ void CAI_Space::load(LPCSTR level_name)
147146 m_cover_manager->compute_static_cover ();
148147 m_moving_objects->on_level_load ();
149148
150- VERIFY (!m_doors_manager);
151- m_doors_manager = new ::doors::manager (level_graph ().header ().box ());
149+ m_doors_manager.reset (new ::doors::manager (level_graph ().header ().box ()));
152150
153151#ifdef DEBUG
154152 Msg (" * Loading ai space is successfully completed (%.3fs, %7.3f Mb)" , timer.GetElapsed_sec (),
@@ -161,7 +159,7 @@ void CAI_Space::unload(bool reload)
161159 if (GEnv.isDedicatedServer )
162160 return ;
163161 GEnv.ScriptEngine ->unload ();
164- xr_delete ( m_doors_manager);
162+ m_doors_manager. reset ( nullptr );
165163 AISpaceBase::Unload (reload);
166164}
167165
0 commit comments