Horizon Official Technical Documentation
Horizon::Zone::GameLogicProcess Class Reference

#include <GameLogicProcess.hpp>

+ Inheritance diagram for Horizon::Zone::GameLogicProcess:
+ Collaboration diagram for Horizon::Zone::GameLogicProcess:

Classes

class  MonsterSpawnAgent
 

Public Member Functions

 GameLogicProcess (struct s_game_process_configuration config)
 
void initialize (int segment_number=1) override
 
void finalize () override
 
void update (uint64_t diff)
 
bool is_initialized () override
 
bool is_finalized () override
 
bool load_map_cache ()
 
void start_internal ()
 
TaskSchedulergetScheduler ()
 
void on_map_update (int64_t diff)
 
MonsterSpawnAgentget_monster_spawn_agent ()
 
struct s_game_process_configurationgame_config ()
 
ResourceManagerget_resource_manager ()
 
- Public Member Functions inherited from KernelComponent
 KernelComponent (Kernel *kernel, Horizon::System::runtime_module_type module_type)
 
virtual void initialize (int segment_number=1)
 
virtual void finalize ()
 
virtual bool is_initialized ()
 
virtual bool is_finalized ()
 
void set_segment_number (int64_t segment_number)
 
int64_t get_segment_number ()
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContext > context)
 
void system_routine_queue_push (std::shared_ptr< Horizon::System::RuntimeContextChain > context)
 
void system_routine_process_queue ()
 
void system_routine_register (Horizon::System::runtime_module_type module_t, Horizon::System::runtime_synchronization_method sync_t, std::shared_ptr< Horizon::System::RuntimeContext > context)
 
const std::string get_uuid_string ()
 
const std::string get_type_string ()
 
Horizon::System::SystemRoutineManagerget_system_routine_manager ()
 
Kernelget_kernel ()
 
void set_thread_cpu_id (int cpu_id)
 
int get_thread_cpu_id ()
 
void set_thread_update_rate (double rate)
 
double get_thread_update_rate ()
 
void set_total_execution_time (int time)
 
int get_total_execution_time ()
 
void calculate_and_set_cpu_load ()
 

Protected Types

using PrimaryResource = SharedPriorityResourceMedium< s_segment_storage< std::string, std::shared_ptr< Map > > >
 
using SecondaryResource = SharedPriorityResourceMedium< s_segment_storage< uint64_t, std::shared_ptr< Units::Player > > >
 
using TertiaryResource = SharedPriorityResourceMedium< s_segment_storage< uint64_t, std::shared_ptr< Unit > > >
 
using ResourceManager = SharedPriorityResourceManager< PrimaryResource, SecondaryResource, TertiaryResource >
 

Protected Attributes

std::atomic< bool > _is_initialized {false}
 
std::atomic< bool > _is_finalized {false}
 
ResourceManager _resource_manager
 

Private Attributes

MonsterSpawnAgent _monster_spawn_agent
 
TaskScheduler _scheduler
 
std::thread _thread
 
std::map< std::string, map_data_maps
 
struct s_game_process_configuration _config
 

Member Typedef Documentation

◆ PrimaryResource

◆ ResourceManager

◆ SecondaryResource

◆ TertiaryResource

Constructor & Destructor Documentation

◆ GameLogicProcess()

GameLogicProcess::GameLogicProcess ( struct s_game_process_configuration  config)
60 PrimaryResource(RESOURCE_PRIORITY_PRIMARY, std::make_shared<s_segment_storage<std::string, std::shared_ptr<Map>>>()),
61 SecondaryResource(RESOURCE_PRIORITY_SECONDARY, std::make_shared<s_segment_storage<uint64_t, std::shared_ptr<Units::Player>>>()),
62 TertiaryResource(RESOURCE_PRIORITY_TERTIARY, std::make_shared<s_segment_storage<uint64_t, std::shared_ptr<Unit>>>())
63),
64_config(config)
65{
66}
@ RESOURCE_PRIORITY_SECONDARY
Definition: Server.hpp:80
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
@ RESOURCE_PRIORITY_TERTIARY
Definition: Server.hpp:81
#define sZone
Definition: Zone.hpp:247
SharedPriorityResourceMedium< s_segment_storage< uint64_t, std::shared_ptr< Unit > > > TertiaryResource
Definition: GameLogicProcess.hpp:109
SharedPriorityResourceMedium< s_segment_storage< std::string, std::shared_ptr< Map > > > PrimaryResource
Definition: GameLogicProcess.hpp:107
ResourceManager _resource_manager
Definition: GameLogicProcess.hpp:111
struct s_game_process_configuration _config
Definition: GameLogicProcess.hpp:120
SharedPriorityResourceMedium< s_segment_storage< uint64_t, std::shared_ptr< Units::Player > > > SecondaryResource
Definition: GameLogicProcess.hpp:108
KernelComponent(Kernel *kernel, Horizon::System::runtime_module_type module_type)
Definition: Server.hpp:194
@ RUNTIME_GAMELOGIC
Definition: System.hpp:86
Definition: Server.hpp:93

Member Function Documentation

◆ finalize()

void GameLogicProcess::finalize ( )
overridevirtual

Reimplemented from KernelComponent.

99{
103
105
106 if (_thread.joinable()) {
107 try {
108 _thread.join();
109 } catch(std::exception &e) {
110 HLog(error) << "GameLogicProcess::finalize: " << e.what();
111 }
112 }
113
114 _is_finalized.exchange(true);
115}
#define HLog(type)
Definition: Logger.hpp:122
void clear_monster_spawn_info()
Definition: GameLogicProcess.hpp:73
std::atomic< bool > _is_finalized
Definition: GameLogicProcess.hpp:105
MonsterSpawnAgent & get_monster_spawn_agent()
Definition: GameLogicProcess.hpp:99
std::thread _thread
Definition: GameLogicProcess.hpp:118
ResourceManager & get_resource_manager()
Definition: GameLogicProcess.hpp:113
void clear()
Definition: Server.hpp:181

References _is_finalized, _thread, SharedPriorityResourceManager< SharedPriorityResourceMediums >::clear(), Horizon::Zone::GameLogicProcess::MonsterSpawnAgent::clear_monster_spawn_info(), get_monster_spawn_agent(), get_resource_manager(), HLog, RESOURCE_PRIORITY_PRIMARY, RESOURCE_PRIORITY_SECONDARY, and RESOURCE_PRIORITY_TERTIARY.

+ Here is the call graph for this function:

◆ game_config()

struct s_game_process_configuration & Horizon::Zone::GameLogicProcess::game_config ( )
inline
101{ return _config; }

References _config.

◆ get_monster_spawn_agent()

MonsterSpawnAgent & Horizon::Zone::GameLogicProcess::get_monster_spawn_agent ( )
inline
99{ return _monster_spawn_agent; }
MonsterSpawnAgent _monster_spawn_agent
Definition: GameLogicProcess.hpp:116

References _monster_spawn_agent.

Referenced by finalize(), and initialize().

+ Here is the caller graph for this function:

◆ get_resource_manager()

ResourceManager & Horizon::Zone::GameLogicProcess::get_resource_manager ( )
inline

◆ getScheduler()

TaskScheduler & Horizon::Zone::GameLogicProcess::getScheduler ( )
inline
95{ return _scheduler; }
TaskScheduler _scheduler
Definition: GameLogicProcess.hpp:117

References _scheduler.

Referenced by update().

+ Here is the caller graph for this function:

◆ initialize()

void GameLogicProcess::initialize ( int  segment_number = 1)
overridevirtual

Static Databases

Reimplemented from KernelComponent.

69{
70 set_segment_number(segment_number);
71
75 if (static_db_loaded == false) {
76 ExpDB->load();
77 ExpDB->load_status_point_table();
78 JobDB->load();
79 ItemDB->load();
80 ItemDB->load_refine_db();
81 ItemDB->load_weapon_target_size_modifiers_db();
82 ItemDB->load_weapon_attribute_modifiers_db();
83 StatusEffectDB->load();
84 SkillDB->load();
85 MonsterDB->load();
86 StorageDB->load();
87 static_db_loaded = true;
88 }
89
92
93 get_monster_spawn_agent().initialize(std::static_pointer_cast<GameLogicProcess>(shared_from_this()));
94
95 _is_initialized.exchange(true);
96}
#define ExpDB
Definition: ExpDB.hpp:93
static std::atomic< bool > static_db_loaded
Definition: GameLogicProcess.cpp:55
#define ItemDB
Definition: ItemDB.hpp:119
#define JobDB
Definition: JobDB.hpp:87
#define MonsterDB
Definition: MonsterDB.hpp:101
#define SkillDB
Definition: SkillDB.hpp:145
#define StatusEffectDB
Definition: StatusEffectDB.hpp:77
#define StorageDB
Definition: StorageDB.hpp:79
void initialize(std::shared_ptr< GameLogicProcess > game_logic_process)
Definition: GameLogicProcess.hpp:55
std::atomic< bool > _is_initialized
Definition: GameLogicProcess.hpp:104
void start_internal()
Definition: GameLogicProcess.cpp:159
bool load_map_cache()
Definition: GameLogicProcess.cpp:117
void set_segment_number(int64_t segment_number)
Definition: Server.hpp:206

References _is_initialized, ExpDB, get_monster_spawn_agent(), Horizon::Zone::GameLogicProcess::MonsterSpawnAgent::initialize(), ItemDB, JobDB, load_map_cache(), MonsterDB, KernelComponent::set_segment_number(), SkillDB, start_internal(), static_db_loaded, StatusEffectDB, and StorageDB.

+ Here is the call graph for this function:

◆ is_finalized()

bool Horizon::Zone::GameLogicProcess::is_finalized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

89{ return _is_finalized.load(); }

References _is_finalized.

◆ is_initialized()

bool Horizon::Zone::GameLogicProcess::is_initialized ( )
inlineoverridevirtual

Reimplemented from KernelComponent.

88{ return _is_initialized.load(); }

References _is_initialized.

Referenced by Horizon::Zone::ScriptManager::initialize().

+ Here is the caller graph for this function:

◆ load_map_cache()

bool GameLogicProcess::load_map_cache ( )
118{
120 std::string db_path = sZone->config().get_static_db_path().string();
121
122 m.setMapListPath(db_path + "map_list.lua");
123 m.setMapCachePath(sZone->config().get_mapcache_path().string());
124
126 HLog(error) <<"Could not read map config file '" << m.getMapListPath().string() << "'.";
127 return false;
128 }
129
130 switch (m.ImportFromCacheFile())
131 {
132 default:
133 break;
135 HLog(error) << "Could not open file '" << m.getMapCachePath().string() << "'.";
136 return false;
138 HLog(error) << "Could not read file '" << m.getMapCachePath().string() << "', rebuilding...";
139 return false;
141 HLog(error) << "File cache file '" << m.getMapCachePath().string() << "' is corrupted (invalid checksum), rebuilding...";
142 return false;
144 HLog(error) << "File cache file '" << m.getMapCachePath().string() << "' could not be decompressed, rebuilding...";
145 return false;
147 HLog(error) << "Could not read map information for a map while importing file '" << m.getMapCachePath().string() << "', rebuilding...";
148 return false;
150 HLog(error) << "Could not read cell information for a map while importing file '" << m.getMapCachePath().string() << "', rebuilding...";
151 return false;
152 }
153
154 _maps = m.getMCache()->maps;
155
156 return true;
157}
@ MCACHE_IMPORT_INVALID_CHECKSUM
Definition: MapCache.hpp:146
@ MCACHE_IMPORT_NONEXISTENT_FILE
Definition: MapCache.hpp:144
@ MCACHE_IMPORT_READ_ERROR
Definition: MapCache.hpp:145
@ MCACHE_IMPORT_DECOMPRESS_ERROR
Definition: MapCache.hpp:147
@ MCACHE_IMPORT_MAPINFO_ERROR
Definition: MapCache.hpp:148
@ MCACHE_IMPORT_CELLINFO_ERROR
Definition: MapCache.hpp:149
@ MCACHE_CONFIG_OK
Definition: MapCache.hpp:128
Definition: MapCache.hpp:164
mcache_config_error_type ReadMapListConfig()
Definition: MapCache.cpp:172
void setMapListPath(std::string const &path)
Definition: MapCache.hpp:207
void setMapCachePath(std::string const &file)
Definition: MapCache.hpp:199
boost::filesystem::path & getMapCachePath()
Definition: MapCache.hpp:198
const boost::filesystem::path & getMapListPath() const
Definition: MapCache.hpp:206
std::shared_ptr< map_cache > getMCache()
Definition: MapCache.hpp:230
mcache_import_error_type ImportFromCacheFile()
Definition: MapCache.cpp:56
std::map< std::string, map_data > _maps
Definition: GameLogicProcess.hpp:119

References _maps, Horizon::Libraries::MapCache::getMapCachePath(), Horizon::Libraries::MapCache::getMapListPath(), Horizon::Libraries::MapCache::getMCache(), HLog, Horizon::Libraries::MapCache::ImportFromCacheFile(), MCACHE_CONFIG_OK, MCACHE_IMPORT_CELLINFO_ERROR, MCACHE_IMPORT_DECOMPRESS_ERROR, MCACHE_IMPORT_INVALID_CHECKSUM, MCACHE_IMPORT_MAPINFO_ERROR, MCACHE_IMPORT_NONEXISTENT_FILE, MCACHE_IMPORT_READ_ERROR, Horizon::Libraries::MapCache::ReadMapListConfig(), Horizon::Libraries::MapCache::setMapCachePath(), Horizon::Libraries::MapCache::setMapListPath(), and sZone.

Referenced by initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ on_map_update()

void Horizon::Zone::GameLogicProcess::on_map_update ( int64_t  diff)

◆ start_internal()

void GameLogicProcess::start_internal ( )
160{
161 int total_maps = 0;
162 int mcache_size = _maps.size();
163 int max_maps_per_thread = std::ceil((double) mcache_size / sZone->config().max_game_logic_threads());
164 int container_max = get_segment_number() * max_maps_per_thread;
165 int container_min = (get_segment_number() - 1) * max_maps_per_thread;
166
167 auto map_i = _maps.begin();
168 std::advance(map_i, container_min);
169 for (int i = container_min; i < container_max; i++)
170 {
171 if (map_i == _maps.end())
172 break;
173
174 std::shared_ptr<Map> map = std::make_shared<Map>(std::static_pointer_cast<GameLogicProcess>(shared_from_this()), map_i->second.name(), map_i->second.width(), map_i->second.height(), map_i->second.getCells());
175
176 this->get_resource_manager().add<RESOURCE_PRIORITY_PRIMARY>(map_i->second.name(), map);
177
178 total_maps++;
179 map_i++;
180 }
181
182 HLog(info) << "Started map container " << get_segment_number() << " (" << container_min + 1 << " to " << container_max << " maps) for a total of " << container_max - container_min << " out of " << mcache_size << " maps.";
183
184 _maps.clear();
185
186 _thread = std::thread([this]() {
187 while (!sZone->general_conf().is_test_run_minimal() && get_shutdown_stage() == SHUTDOWN_NOT_STARTED) {
188 this->update(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
189 std::this_thread::sleep_for(std::chrono::microseconds(MAX_CORE_UPDATE_INTERVAL));
190 };
191 });
192}
#define MAX_CORE_UPDATE_INTERVAL
Definition: Horizon.hpp:63
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64
void update(uint64_t diff)
Definition: GameLogicProcess.cpp:194
int64_t get_segment_number()
Definition: Server.hpp:207
void add(Key key, Value value)
Definition: Server.hpp:157
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100

References _maps, _thread, SharedPriorityResourceManager< SharedPriorityResourceMediums >::add(), GridTypeListIterator::count(), get_resource_manager(), KernelComponent::get_segment_number(), get_shutdown_stage(), HLog, MAX_CORE_UPDATE_INTERVAL, RESOURCE_PRIORITY_PRIMARY, SHUTDOWN_NOT_STARTED, sZone, and update().

Referenced by initialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update()

void GameLogicProcess::update ( uint64_t  diff)
195{
196 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
197 // Update the entities.
198 for (auto unit_it : this->get_resource_manager().get_medium<RESOURCE_PRIORITY_TERTIARY>().get_map())
199 unit_it.second->update(diff);
200
202
204
205#if WIN32
206 DWORD cpu = GetCurrentProcessorNumber();
207 if (get_thread_cpu_id() != (int) cpu)
209#elif __linux__
210 int cpu = sched_getcpu();
211 if (get_thread_cpu_id() != cpu)
213#endif
215 std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
216 std::chrono::nanoseconds time_span = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
217 set_total_execution_time(time_span.count());
218}
void process_queue()
Definition: System.hpp:676
TaskScheduler & getScheduler()
Definition: GameLogicProcess.hpp:95
void set_thread_cpu_id(int cpu_id)
Definition: Server.hpp:239
Horizon::System::SystemRoutineManager & get_system_routine_manager()
Definition: Server.hpp:235
void set_total_execution_time(int time)
Definition: Server.hpp:245
void calculate_and_set_cpu_load()
Definition: Server.hpp:257
int get_thread_cpu_id()
Definition: Server.hpp:240
TaskScheduler & Update(success_t const &callback=EmptyCallback)
Update the scheduler to the current time. Calls the optional callback on successfully finish.
Definition: TaskScheduler.cpp:25

References KernelComponent::calculate_and_set_cpu_load(), get_resource_manager(), KernelComponent::get_system_routine_manager(), KernelComponent::get_thread_cpu_id(), getScheduler(), Horizon::System::SystemRoutineManager::process_queue(), KernelComponent::set_thread_cpu_id(), KernelComponent::set_total_execution_time(), and TaskScheduler::Update().

Referenced by start_internal().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _config

struct s_game_process_configuration Horizon::Zone::GameLogicProcess::_config
private

Referenced by game_config().

◆ _is_finalized

std::atomic<bool> Horizon::Zone::GameLogicProcess::_is_finalized {false}
protected

Referenced by finalize(), and is_finalized().

◆ _is_initialized

std::atomic<bool> Horizon::Zone::GameLogicProcess::_is_initialized {false}
protected

Referenced by initialize(), and is_initialized().

◆ _maps

std::map<std::string, map_data> Horizon::Zone::GameLogicProcess::_maps
private

Referenced by load_map_cache(), and start_internal().

◆ _monster_spawn_agent

MonsterSpawnAgent Horizon::Zone::GameLogicProcess::_monster_spawn_agent
private

Referenced by get_monster_spawn_agent().

◆ _resource_manager

ResourceManager Horizon::Zone::GameLogicProcess::_resource_manager
protected

Referenced by get_resource_manager().

◆ _scheduler

TaskScheduler Horizon::Zone::GameLogicProcess::_scheduler
private

Referenced by getScheduler().

◆ _thread

std::thread Horizon::Zone::GameLogicProcess::_thread
private

Referenced by finalize(), and start_internal().


The documentation for this class was generated from the following files: