Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyx14 committed Nov 9, 2023
1 parent 98f0128 commit 4956c1b
Show file tree
Hide file tree
Showing 43 changed files with 253 additions and 204 deletions.
9 changes: 9 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ coreDirectory = "data"
-- NOTE: Will only display logs with level higher or equal the one set.
logLevel = "info"

--- Toggles the server's maintenance mode.
-- When enabled, it restricts user access and indicates maintenance operations.
-- @field [parent=#global] #boolean toggleMaintainMode false by default, set to true to enable maintenance mode.
toggleMaintainMode = false
--- Message displayed during maintenance mode.
-- Should inform the expected downtime or resumption details succinctly.
-- @field [parent=#global] #string maintainModeMessage an empty string by default, set a custom message if needed.
maintainModeMessage = ""

-- Combat settings
-- NOTE: valid values for worldType are: "pvp", "no-pvp" and "pvp-enforced"
worldType = "pvp"
Expand Down
10 changes: 8 additions & 2 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ int CanaryServer::run() {

g_game().start(&serviceManager);
g_game().setGameState(GAME_STATE_NORMAL);

g_webhook().sendMessage("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE);
if (g_configManager().getBoolean(TOGGLE_MAINTAIN_MODE)) {
g_game().setGameState(GAME_STATE_CLOSED);
g_logger().warn("Initialized in maintain mode!");
g_webhook().sendMessage("Server is now online", "The server is now online. Access is currently restricted to administrators only.", WEBHOOK_COLOR_ONLINE);
} else {
g_game().setGameState(GAME_STATE_NORMAL);
g_webhook().sendMessage("Server is now online", "Server has successfully started.", WEBHOOK_COLOR_ONLINE);
}

loaderStatus = LoaderStatus::LOADED;
} catch (FailedToInitializeCanary &err) {
Expand Down
5 changes: 2 additions & 3 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ enum booleanConfig_t {
RESET_SESSIONS_ON_STARTUP,
TOGGLE_WHEELSYSTEM,
TOGGLE_ATTACK_SPEED_ONFIST,

VIP_SYSTEM_ENABLED,
VIP_AUTOLOOT_VIP_ONLY,
VIP_STAY_ONLINE,

REWARD_CHEST_COLLECT_ENABLED,
TOGGLE_MOUNT_IN_PZ,
TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART,

TOGGLE_RECEIVE_REWARD,
TOGGLE_MAINTAIN_MODE,

LAST_BOOLEAN_CONFIG
};
Expand Down Expand Up @@ -128,6 +126,7 @@ enum stringConfig_t {
FORGE_FIENDISH_INTERVAL_TIME,
TIBIADROME_CONCOCTION_TICK_TYPE,
M_CONST,
MAINTAIN_MODE_MESSAGE,

LAST_STRING_CONFIG
};
Expand Down
2 changes: 2 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ bool ConfigManager::load() {
boolean[BIND_ONLY_GLOBAL_ADDRESS] = getGlobalBoolean(L, "bindOnlyGlobalAddress", false);
boolean[OPTIMIZE_DATABASE] = getGlobalBoolean(L, "startupDatabaseOptimization", true);
boolean[TOGGLE_MAP_CUSTOM] = getGlobalBoolean(L, "toggleMapCustom", true);
boolean[TOGGLE_MAINTAIN_MODE] = getGlobalBoolean(L, "toggleMaintainMode", false);
string[MAINTAIN_MODE_MESSAGE] = getGlobalString(L, "maintainModeMessage", "");

string[IP] = getGlobalString(L, "ip", "127.0.0.1");
string[MAP_NAME] = getGlobalString(L, "mapName", "canary");
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ std::vector<std::pair<Position, std::vector<uint32_t>>> Combat::pickChainTargets

std::vector<std::pair<Position, std::vector<uint32_t>>> resultMap;
std::vector<std::shared_ptr<Creature>> targets;
std::set<uint32_t> visited;
phmap::flat_hash_set<uint32_t> visited;

if (initialTarget && initialTarget != caster) {
targets.push_back(initialTarget);
Expand Down
52 changes: 28 additions & 24 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ void Creature::onThink(uint32_t interval) {
};

if (isUpdatingPath) {
g_dispatcher().asyncEvent([self = getCreature(), onThink = std::move(onThink)] {
self->isUpdatingPath = false;
self->goToFollowCreature_async(onThink);
});
isUpdatingPath = false;
goToFollowCreature_async(onThink);
return;
}

Expand Down Expand Up @@ -433,22 +431,22 @@ void Creature::checkSummonMove(const Position &newPos, bool teleportSummon) {
if (hasSummons()) {
std::vector<std::shared_ptr<Creature>> despawnMonsterList;
for (const auto &summon : getSummons()) {
const Position &pos = summon->getPosition();
std::shared_ptr<Monster> monster = summon->getMonster();
auto tile = getTile();
const auto &pos = summon->getPosition();
const auto &monster = summon->getMonster();
const auto &tile = getTile();
bool protectionZoneCheck = tile ? tile->hasFlag(TILESTATE_PROTECTIONZONE) : false;
// Check if any of our summons is out of range (+/- 0 floors or 15 tiles away)
bool checkSummonDist = Position::getDistanceZ(newPos, pos) > 0 || (std::max<int32_t>(Position::getDistanceX(newPos, pos), Position::getDistanceY(newPos, pos)) > 15);
// Check if any of our summons is out of range (+/- 2 floors or 30 tiles away)
bool checkRemoveDist = Position::getDistanceZ(newPos, pos) > 2 || (std::max<int32_t>(Position::getDistanceX(newPos, pos), Position::getDistanceY(newPos, pos)) > 30);

if (monster && monster->isFamiliar() && checkSummonDist || teleportSummon && !protectionZoneCheck && checkSummonDist) {
auto creatureMaster = summon->getMaster();
const auto &creatureMaster = summon->getMaster();
if (!creatureMaster) {
continue;
}

if (std::shared_ptr<Tile> masterTile = creatureMaster->getTile()) {
if (const auto &masterTile = creatureMaster->getTile()) {
if (masterTile->hasFlag(TILESTATE_TELEPORT)) {
g_logger().warn("[{}] cannot teleport summon, position has teleport. {}", __FUNCTION__, creatureMaster->getPosition().toString());
} else {
Expand All @@ -463,7 +461,7 @@ void Creature::checkSummonMove(const Position &newPos, bool teleportSummon) {
}
}

for (std::shared_ptr<Creature> despawnCreature : despawnMonsterList) {
for (const auto &despawnCreature : despawnMonsterList) {
if (!despawnMonsterList.empty()) {
g_game().removeCreature(despawnCreature, true);
}
Expand Down Expand Up @@ -632,6 +630,14 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s
void Creature::onDeath() {
bool lastHitUnjustified = false;
bool mostDamageUnjustified = false;
std::shared_ptr<Creature> lastHitCreature = g_game().getCreatureByID(lastHitCreatureId);
std::shared_ptr<Creature> lastHitCreatureMaster;
if (lastHitCreature) {
lastHitUnjustified = lastHitCreature->onKilledCreature(static_self_cast<Creature>(), true);
lastHitCreatureMaster = lastHitCreature->getMaster();
} else {
lastHitCreatureMaster = nullptr;
}

std::shared_ptr<Creature> mostDamageCreature = nullptr;

Expand Down Expand Up @@ -673,15 +679,11 @@ void Creature::onDeath() {
it.first->onGainExperience(it.second, getCreature());
}

std::shared_ptr<Creature> mostDamageCreatureMaster = nullptr;
if (mostDamageCreature) {
mostDamageCreatureMaster = mostDamageCreature->getMaster();
mostDamageUnjustified = mostDamageCreature->onKilledCreature(getCreature(), false);
}

std::shared_ptr<Creature> lastHitCreature = g_game().getCreatureByID(lastHitCreatureId);
if (lastHitCreature && lastHitCreature != mostDamageCreature && lastHitCreature != mostDamageCreatureMaster) {
lastHitUnjustified = lastHitCreature->onKilledCreature(getCreature(), true);
if (mostDamageCreature && mostDamageCreature != lastHitCreature && mostDamageCreature != lastHitCreatureMaster) {
auto mostDamageCreatureMaster = mostDamageCreature->getMaster();
if (lastHitCreature != mostDamageCreatureMaster && (lastHitCreatureMaster == nullptr || mostDamageCreatureMaster != lastHitCreatureMaster)) {
mostDamageUnjustified = mostDamageCreature->onKilledCreature(static_self_cast<Creature>(), false);
}
}

bool droppedCorpse = dropCorpse(lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
Expand Down Expand Up @@ -1245,13 +1247,16 @@ bool Creature::setMaster(std::shared_ptr<Creature> newMaster, bool reloadCreatur
g_game().reloadCreature(self);
}
if (newMaster) {
newMaster->m_summons.insert(self);
newMaster->m_summons.emplace_back(self);
}

m_master = newMaster;

if (oldMaster) {
oldMaster->m_summons.erase(self);
const auto &it = std::ranges::find(oldMaster->m_summons, self);
if (it != oldMaster->m_summons.end()) {
oldMaster->m_summons.erase(it);
}
}
return true;
}
Expand Down Expand Up @@ -1798,9 +1803,8 @@ void Creature::setIncreasePercent(CombatType_t combat, int32_t value) {
}
}

phmap::flat_hash_set<std::shared_ptr<Zone>> Creature::getZones() {
auto tile = getTile();
if (tile) {
std::unordered_set<std::shared_ptr<Zone>> Creature::getZones() {
if (const auto &tile = getTile()) {
return tile->getZones();
}
return {};
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class Creature : virtual public Thing, public SharedObject {
return ZONE_NORMAL;
}

phmap::flat_hash_set<std::shared_ptr<Zone>> getZones();
std::unordered_set<std::shared_ptr<Zone>> getZones();

// walk functions
void startAutoWalk(const std::vector<Direction> &listDir, bool ignoreConditions = false);
Expand Down Expand Up @@ -341,7 +341,7 @@ class Creature : virtual public Thing, public SharedObject {
return m_master.lock();
}

const phmap::flat_hash_set<std::shared_ptr<Creature>> &getSummons() const {
const auto &getSummons() const {
return m_summons;
}

Expand Down Expand Up @@ -666,7 +666,7 @@ class Creature : virtual public Thing, public SharedObject {

CountMap damageMap;

phmap::flat_hash_set<std::shared_ptr<Creature>> m_summons;
std::vector<std::shared_ptr<Creature>> m_summons;
CreatureEventList eventsList;
ConditionList conditions;

Expand Down
13 changes: 7 additions & 6 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Creature;
class Game;
class Spawn;

using CreatureHashSet = phmap::flat_hash_set<std::shared_ptr<Creature>>;
using CreatureList = std::list<std::shared_ptr<Creature>>;

using CreatureWeakHashMap = phmap::flat_hash_map<uint32_t, std::weak_ptr<Creature>>;
Expand Down Expand Up @@ -99,7 +98,7 @@ class Monster final : public Creature {
if (master && master->getMonster()) {
return master->getMonster()->isEnemyFaction(faction);
}
return mType->info.enemyFactions.empty() ? false : mType->info.enemyFactions.find(faction) != mType->info.enemyFactions.end();
return mType->info.enemyFactions.empty() ? false : mType->info.enemyFactions.contains(faction);
}

bool isPushable() override {
Expand Down Expand Up @@ -204,17 +203,19 @@ class Monster final : public Creature {
}
return list;
}
CreatureHashSet getFriendList() {
CreatureHashSet set;

std::vector<std::shared_ptr<Creature>> getFriendList() {
std::vector<std::shared_ptr<Creature>> list;

for (auto it = friendList.begin(); it != friendList.end();) {
if (auto friendCreature = it->second.lock()) {
set.insert(friendCreature);
list.emplace_back(friendCreature);
++it;
} else {
it = friendList.erase(it);
}
}
return set;
return list;
}

bool isTarget(std::shared_ptr<Creature> creature);
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monsters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class MonsterType {
bool targetPreferMaster = false;

Faction_t faction = FACTION_DEFAULT;
phmap::flat_hash_set<Faction_t> enemyFactions;
stdext::vector_set<Faction_t> enemyFactions;

bool canPushItems = false;
bool canPushCreatures = false;
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void Npc::onPlayerAppear(std::shared_ptr<Player> player) {
if (player->hasFlag(PlayerFlags_t::IgnoredByNpcs) || playerSpectators.contains(player)) {
return;
}
playerSpectators.emplace_back(player);
playerSpectators.emplace(player);
manageIdle();
}

Expand Down Expand Up @@ -531,7 +531,7 @@ void Npc::onThinkWalk(uint32_t interval) {

void Npc::onCreatureWalk() {
Creature::onCreatureWalk();
playerSpectators.erase_if([this](const auto &creature) { return !this->canSee(creature->getPosition()); });
phmap::erase_if(playerSpectators, [this](const auto &creature) { return !this->canSee(creature->getPosition()); });
}

void Npc::onPlacedCreature() {
Expand All @@ -542,7 +542,7 @@ void Npc::loadPlayerSpectators() {
auto spec = Spectators().find<Player>(position, true);
for (const auto &creature : spec) {
if (!creature->getPlayer()->hasFlag(PlayerFlags_t::IgnoredByNpcs)) {
playerSpectators.emplace_back(creature->getPlayer());
playerSpectators.emplace(creature->getPlayer());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Npc final : public Creature {

bool ignoreHeight;

stdext::vector_set<std::shared_ptr<Player>> playerSpectators;
phmap::flat_hash_set<std::shared_ptr<Player>> playerSpectators;
Position masterPos;

friend class LuaScriptInterface;
Expand Down
Loading

0 comments on commit 4956c1b

Please sign in to comment.