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

#include <NPCComponent.hpp>

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

Public Member Functions

 NPCComponent ()
 
 NPCComponent (std::shared_ptr< GameLogicProcess > container)
 
 ~NPCComponent ()
 
void sync_definitions (std::shared_ptr< sol::state > state)
 
void sync_data_types (std::shared_ptr< sol::state > state)
 
void sync_functions (std::shared_ptr< sol::state > state)
 
void add_npc_to_db (uint32_t guid, std::shared_ptr< npc_db_data > const &data)
 
std::shared_ptr< npc_db_dataget_npc_from_db (uint32_t guid)
 
void contact_npc_for_player (std::shared_ptr< Units::Player > player, uint32_t npc_guid)
 
void continue_npc_script_for_player (std::shared_ptr< Units::Player > player, uint32_t npc_guid, uint32_t select_idx=0)
 
- Public Member Functions inherited from Horizon::Zone::LUAComponent
 LUAComponent ()
 
 LUAComponent (std::shared_ptr< GameLogicProcess > container)
 
 ~LUAComponent ()
 
virtual void sync_definitions (std::shared_ptr< sol::state > state)=0
 
virtual void sync_data_types (std::shared_ptr< sol::state > state)=0
 
virtual void sync_functions (std::shared_ptr< sol::state > state)=0
 
std::shared_ptr< GameLogicProcessget_container ()
 

Public Attributes

LockedLookupTable< uint32_t, std::shared_ptr< npc_db_data > > _npc_db
 

Constructor & Destructor Documentation

◆ NPCComponent() [1/2]

Horizon::Zone::NPCComponent::NPCComponent ( )
inline
44{ }

◆ NPCComponent() [2/2]

Horizon::Zone::NPCComponent::NPCComponent ( std::shared_ptr< GameLogicProcess container)
inline
45: LUAComponent(container) { }
LUAComponent()
Definition: LUAComponent.hpp:42

◆ ~NPCComponent()

Horizon::Zone::NPCComponent::~NPCComponent ( )
inline
46{}

Member Function Documentation

◆ add_npc_to_db()

void Horizon::Zone::NPCComponent::add_npc_to_db ( uint32_t  guid,
std::shared_ptr< npc_db_data > const &  data 
)
inline
52{ _npc_db.insert(guid, data); }
LockedLookupTable< uint32_t, std::shared_ptr< npc_db_data > > _npc_db
Definition: NPCComponent.hpp:62
void insert(const Key &key, const Value &value)
Definition: LockedLookupTable.hpp:68

References _npc_db, and LockedLookupTable< Key, Value, Hash >::insert().

Referenced by sync_functions().

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

◆ contact_npc_for_player()

void NPCComponent::contact_npc_for_player ( std::shared_ptr< Units::Player player,
uint32_t  npc_guid 
)
203{
204 std::shared_ptr<npc_db_data> const &nd = _npc_db.at(npc_guid);
205
206 if (nd->script_is_file)
207 player->set_npc_contact_guid(npc_guid);
208
209 try {
210 std::string script_root_path = sZone->config().get_script_root_path().string();
211 sol::protected_function fx = player->lua_state()->load_file(script_root_path + "/internal/script_command_main.lua");
212 sol::protected_function_result result = fx(player, nd->_npc, nd->script, nd->script_is_file);
213 if (!result.valid()) {
214 sol::error err = result;
215 HLog(error) << "ScriptManager::contact_npc_for_player: " << err.what();
216 }
217 } catch (sol::error &e) {
218 HLog(error) << e.what();
219 }
220}
#define HLog(type)
Definition: Logger.hpp:122
#define sZone
Definition: Zone.hpp:247
Value at(Key const &key, Value const &default_value=Value()) const
Definition: LockedLookupTable.hpp:63

References _npc_db, LockedLookupTable< Key, Value, Hash >::at(), HLog, and sZone.

+ Here is the call graph for this function:

◆ continue_npc_script_for_player()

void NPCComponent::continue_npc_script_for_player ( std::shared_ptr< Units::Player player,
uint32_t  npc_guid,
uint32_t  select_idx = 0 
)
223{
224 sol::thread cr_thread = (*player->lua_state())["script_exec_routine"];
225 sol::state_view cr_state = cr_thread.state();
226 sol::coroutine cr = cr_state["script_exec"];
227
228 // Set npc menu selection index (if any, defaulted to 0).
229 cr_state["script_commands"]["select_idx"] = select_idx;
230
231 sol::protected_function_result result = cr(cr_state["script_commands"]);
232 if (!result.valid()) {
233 sol::error err = result;
234 HLog(error) << "ScriptManager::continue_npc_script_for_player: " << err.what();
235 }
236}

References HLog.

◆ get_npc_from_db()

std::shared_ptr< npc_db_data > Horizon::Zone::NPCComponent::get_npc_from_db ( uint32_t  guid)
inline
53{ return _npc_db.at(guid); }

References _npc_db, and LockedLookupTable< Key, Value, Hash >::at().

Referenced by sync_functions().

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

◆ sync_data_types()

void NPCComponent::sync_data_types ( std::shared_ptr< sol::state >  state)
virtual

Implements Horizon::Zone::LUAComponent.

46{
47 sol::usertype<NPC> config_1 = state->new_usertype<NPC>("NPC");
48 config_1["unit"] = [](std::shared_ptr<Horizon::Zone::Units::NPC> npc) { return npc->shared_from_this(); };
49 config_1["name"] = &NPC::name;
50 config_1["map_coords"] = &NPC::map_coords;
51 config_1["get_nearby_unit"] = &NPC::get_nearby_unit;
52 config_1["init"] = &NPC::initialize;
53 config_1["set_map"] = &NPC::set_map;
54}
Definition: NPC.hpp:46

◆ sync_definitions()

void NPCComponent::sync_definitions ( std::shared_ptr< sol::state >  state)
virtual

Implements Horizon::Zone::LUAComponent.

42{
43}

◆ sync_functions()

void NPCComponent::sync_functions ( std::shared_ptr< sol::state >  state)
virtual

Implements Horizon::Zone::LUAComponent.

57{
58 state->set_function("cast_unit_to_npc",
59 [](std::shared_ptr<Unit> e)
60 {
61 return e->template downcast<Horizon::Zone::Units::NPC>();
62 });
63
64 state->set_function("NewNPC",
65 [this] (std::string const &name, std::string const &map_name, uint16_t x, uint16_t y, uint32_t job_id, directions dir, std::string const &script_file) {
66 std::shared_ptr<Map> map;
67
68 int segment_number = sZone->get_segment_number_for_resource<Horizon::Zone::GameLogicProcess, RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(Horizon::System::RUNTIME_GAMELOGIC, map_name, nullptr);
69
70 if (segment_number == 0)
71 return;
72
73 map = sZone->get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, segment_number)->get_resource_manager().template get_resource<RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(map_name, nullptr);
74
75 if (map == nullptr)
76 return;
77
78 std::shared_ptr<NPC> npc = std::make_shared<NPC>(name, map, x, y, job_id, dir);
79 npc->initialize();
80
81 if (get_npc_from_db(npc->guid()) != nullptr)
82 return;
83
84 npc_db_data nd;
85 nd.npc_name = name;
86 nd.map_name = map->get_name();
87 nd.coords = MapCoords(x, y);
88 nd.direction = dir;
89 nd.script = script_file;
90 nd.script_is_file = true;
91 nd._npc = npc;
92
93 std::shared_ptr<npc_db_data> p_nd = std::make_shared<npc_db_data>(nd);
94
95 add_npc_to_db(npc->guid(), p_nd);
96 });
97
98 state->set_function("DupNPC",
99 [this] (std::string const &name, std::string const &map_name, uint16_t x, uint16_t y, uint32_t job_id, directions dir, std::shared_ptr<NPC> duplicate) {
100
101 std::shared_ptr<Map> map;
102
103 int segment_number = sZone->get_segment_number_for_resource<Horizon::Zone::GameLogicProcess, RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(Horizon::System::RUNTIME_GAMELOGIC, map_name, nullptr);
104
105 if (segment_number == 0)
106 return;
107
108 map = sZone->get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, segment_number)->get_resource_manager().template get_resource<RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(map_name, nullptr);
109
110 if (map == nullptr)
111 return;
112
113 std::shared_ptr<NPC> npc = std::make_shared<NPC>(name, map, x, y, job_id, dir);
114 npc->initialize();
115
116 if (get_npc_from_db(npc->guid()) != nullptr)
117 return;
118
119 npc_db_data nd;
120 nd.npc_name = name;
121 nd.map_name = map->get_name();
122 nd.coords = MapCoords(x, y);
123 nd.direction = dir;
124 nd._npc = npc;
125
126 std::shared_ptr<npc_db_data> dup_nd = _npc_db.at(duplicate->guid());
127
128 nd.script = dup_nd->script;
129 nd.script_is_file = true;
130
131 std::shared_ptr<npc_db_data> p_nd = std::make_shared<npc_db_data>(nd);
132
133 add_npc_to_db(npc->guid(), p_nd);
134 });
135
136 state->set_function("SilentNPC",
137 [this] (std::string const &name, std::string const &map_name, uint16_t x, uint16_t y, uint32_t job_id, directions dir) {
138 std::shared_ptr<Map> map;
139
140 int segment_number = sZone->get_segment_number_for_resource<Horizon::Zone::GameLogicProcess, RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(Horizon::System::RUNTIME_GAMELOGIC, map_name, nullptr);
141
142 if (segment_number == 0)
143 return;
144
145 map = sZone->get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, segment_number)->get_resource_manager().template get_resource<RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(map_name, nullptr);
146
147 if (map == nullptr)
148 return;
149
150 std::shared_ptr<NPC> npc = std::make_shared<NPC>(name, map, x, y, job_id, dir);
151 npc->initialize();
152
153 if (get_npc_from_db(npc->guid()) != nullptr)
154 return;
155
156 npc_db_data nd;
157 nd.npc_name = name;
158 nd.map_name = map->get_name();
159 nd.coords = MapCoords(x, y);
160 nd.direction = dir;
161 nd._npc = npc;
162
163 std::shared_ptr<npc_db_data> p_nd = std::make_shared<npc_db_data>(nd);
164
165 add_npc_to_db(npc->guid(), p_nd);
166 });
167
168 state->set_function("Warp",
169 [this] (std::string const &name, std::string const &map_name, uint16_t x, uint16_t y, std::string const &script, uint16_t trigger_range)
170 {
171 std::shared_ptr<Map> map;
172
173 int segment_number = sZone->get_segment_number_for_resource<Horizon::Zone::GameLogicProcess, RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(Horizon::System::RUNTIME_GAMELOGIC, map_name, nullptr);
174
175 if (segment_number == 0)
176 return;
177
178 map = sZone->get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC, segment_number)->get_resource_manager().template get_resource<RESOURCE_PRIORITY_PRIMARY, std::string, std::shared_ptr<Map>>(map_name, nullptr);
179
180 if (map == nullptr)
181 return;
182
183 std::shared_ptr<NPC> npc = std::make_shared<NPC>(name, map, x, y, script);
184
185 npc->initialize();
186
187 npc_db_data nd;
188 nd.npc_name = name;
189 nd.map_name = map->get_name();
190 nd.coords = MapCoords(x, y);
191 nd.script = script;
192 nd.script_is_file = false;
193 nd.trigger_range = trigger_range;
194 nd._npc = npc;
195
196 std::shared_ptr<npc_db_data> p_nd = std::make_shared<npc_db_data>(nd);
197
198 add_npc_to_db(npc->guid(), p_nd);
199 });
200}
Coordinates< MAX_CELLS_PER_MAP > MapCoords
Definition: GridDefinitions.hpp:83
@ RESOURCE_PRIORITY_PRIMARY
Definition: Server.hpp:79
directions
Definition: UnitDefinitions.hpp:75
Definition: GameLogicProcess.hpp:48
std::shared_ptr< npc_db_data > get_npc_from_db(uint32_t guid)
Definition: NPCComponent.hpp:53
void add_npc_to_db(uint32_t guid, std::shared_ptr< npc_db_data > const &data)
Definition: NPCComponent.hpp:52
@ RUNTIME_GAMELOGIC
Definition: System.hpp:86
Definition: NPCDefinitions.hpp:66
directions direction
Definition: NPCDefinitions.hpp:70
MapCoords coords
Definition: NPCDefinitions.hpp:69
bool script_is_file
Definition: NPCDefinitions.hpp:73
std::string script
Definition: NPCDefinitions.hpp:72
std::string npc_name
Definition: NPCDefinitions.hpp:67
uint16_t trigger_range
Definition: NPCDefinitions.hpp:74
std::shared_ptr< Horizon::Zone::Units::NPC > _npc
Definition: NPCDefinitions.hpp:75
std::string map_name
Definition: NPCDefinitions.hpp:68

References npc_db_data::_npc, _npc_db, add_npc_to_db(), LockedLookupTable< Key, Value, Hash >::at(), npc_db_data::coords, npc_db_data::direction, get_npc_from_db(), npc_db_data::map_name, npc_db_data::npc_name, RESOURCE_PRIORITY_PRIMARY, Horizon::System::RUNTIME_GAMELOGIC, npc_db_data::script, npc_db_data::script_is_file, sZone, and npc_db_data::trigger_range.

+ Here is the call graph for this function:

Member Data Documentation

◆ _npc_db

LockedLookupTable<uint32_t, std::shared_ptr<npc_db_data> > Horizon::Zone::NPCComponent::_npc_db

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