Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
extern Game g_game;
extern Spells* g_spells;
extern Actions* g_actions;
extern ConfigManager g_config;

Actions::Actions() : scriptInterface("Action Interface") { scriptInterface.initState(); }

Expand Down Expand Up @@ -403,7 +402,7 @@ static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t coun

bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
{
int32_t cooldown = g_config.getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL);
int32_t cooldown = getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL);
player->setNextAction(OTSYS_TIME() + cooldown);
player->sendUseItemCooldown(cooldown);
if (item->isSupply()) {
Expand All @@ -416,7 +415,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
player->getItemTypeCount(item->getID(), subType != item->getItemCount() ? subType : -1));
}

if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);
Expand All @@ -442,7 +441,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
bool Actions::useItemEx(Player* player, const Position& fromPos, const Position& toPos, uint8_t toStackPos, Item* item,
bool isHotkey, Creature* creature /* = nullptr*/)
{
int32_t cooldown = g_config.getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL);
int32_t cooldown = getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL);
player->setNextAction(OTSYS_TIME() + cooldown);
player->sendUseItemCooldown(cooldown);

Expand All @@ -464,7 +463,7 @@ bool Actions::useItemEx(Player* player, const Position& fromPos, const Position&
player->getItemTypeCount(item->getID(), subType != item->getItemCount() ? subType : -1));
}

if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);
Expand Down
3 changes: 1 addition & 2 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

extern Game g_game;
extern Weapons* g_weapons;
extern ConfigManager g_config;
extern Events* g_events;

std::vector<Tile*> getList(const MatrixArea& area, const Position& targetPos, const Direction dir)
Expand Down Expand Up @@ -272,7 +271,7 @@ bool Combat::isInPvpZone(const Creature* attacker, const Creature* target)

bool Combat::isProtected(const Player* attacker, const Player* target)
{
uint32_t protectionLevel = g_config.getNumber(ConfigManager::PROTECTION_LEVEL);
uint32_t protectionLevel = getNumber(ConfigManager::PROTECTION_LEVEL);
if (target->getLevel() < protectionLevel || attacker->getLevel() < protectionLevel) {
return true;
}
Expand Down
29 changes: 16 additions & 13 deletions src/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ extern Game g_game;

namespace {

std::array<std::string, ConfigManager::LAST_STRING_CONFIG> string = {};
std::array<int32_t, ConfigManager::LAST_INTEGER_CONFIG> integer = {};
std::array<bool, ConfigManager::LAST_BOOLEAN_CONFIG> boolean = {};

using ExperienceStages = std::vector<std::tuple<uint32_t, uint32_t, float>>;
ExperienceStages expStages;

bool loaded = false;

template <typename T>
auto getEnv(const char* envVar, T&& defaultValue)
{
Expand Down Expand Up @@ -81,12 +90,6 @@ bool getGlobalBoolean(lua_State* L, const char* identifier, const bool defaultVa
return val != 0;
}

} // namespace

ConfigManager::ConfigManager() { string[CONFIG_FILE] = "config.lua"; }

namespace {

ExperienceStages loadLuaStages(lua_State* L)
{
ExperienceStages stages;
Expand Down Expand Up @@ -161,7 +164,8 @@ bool ConfigManager::load()

luaL_openlibs(L);

if (luaL_dofile(L, getString(CONFIG_FILE).c_str())) {
string[CONFIG_FILE] = "config.lua";
if (luaL_dofile(L, string[CONFIG_FILE].data())) {
std::cout << "[Error - ConfigManager::load] " << lua_tostring(L, -1) << std::endl;
lua_close(L);
return false;
Expand Down Expand Up @@ -302,18 +306,17 @@ bool ConfigManager::load()
return true;
}

static std::string dummyStr;

const std::string& ConfigManager::getString(string_config_t what) const
const std::string& ConfigManager::getString(string_config_t what)
{
static std::string dummyStr;
if (what >= LAST_STRING_CONFIG) {
std::cout << "[Warning - ConfigManager::getString] Accessing invalid index: " << what << std::endl;
return dummyStr;
}
return string[what];
}

int32_t ConfigManager::getNumber(integer_config_t what) const
int32_t ConfigManager::getNumber(integer_config_t what)
{
if (what >= LAST_INTEGER_CONFIG) {
std::cout << "[Warning - ConfigManager::getNumber] Accessing invalid index: " << what << std::endl;
Expand All @@ -322,7 +325,7 @@ int32_t ConfigManager::getNumber(integer_config_t what) const
return integer[what];
}

bool ConfigManager::getBoolean(boolean_config_t what) const
bool ConfigManager::getBoolean(boolean_config_t what)
{
if (what >= LAST_BOOLEAN_CONFIG) {
std::cout << "[Warning - ConfigManager::getBoolean] Accessing invalid index: " << what << std::endl;
Expand All @@ -331,7 +334,7 @@ bool ConfigManager::getBoolean(boolean_config_t what) const
return boolean[what];
}

float ConfigManager::getExperienceStage(uint32_t level) const
float ConfigManager::getExperienceStage(uint32_t level)
{
auto it = std::find_if(expStages.begin(), expStages.end(), [level](auto&& stage) {
auto&& [minLevel, maxLevel, _] = stage;
Expand Down
Loading