Skip to content

Commit

Permalink
cleanup: remove unecessary inheritance from lua interface classes
Browse files Browse the repository at this point in the history
Fixed circular dependencies
Removed various unecessary inheritance from lua interface
Removed legacy unused codes
  • Loading branch information
dudantas committed Nov 1, 2024
1 parent 653e064 commit 1663a47
Show file tree
Hide file tree
Showing 50 changed files with 617 additions and 428 deletions.
5 changes: 3 additions & 2 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "canary_server.hpp"

#include "core.hpp"
#include "config/configmanager.hpp"
#include "creatures/npcs/npcs.hpp"
#include "creatures/players/grouping/familiars.hpp"
Expand All @@ -31,8 +32,7 @@
#include "server/network/protocol/protocollogin.hpp"
#include "server/network/protocol/protocolstatus.hpp"
#include "server/network/webhook/webhook.hpp"

#include "core.hpp"
#include "creatures/players/vocations/vocation.hpp"

CanaryServer::CanaryServer(
Logger &logger,
Expand Down Expand Up @@ -322,6 +322,7 @@ void CanaryServer::initializeDatabase() {
&& !DatabaseManager::optimizeTables()) {
logger.debug("No tables were optimized");
}
g_logger().info("Database connection established!");
}

void CanaryServer::loadModules() {
Expand Down
2 changes: 2 additions & 0 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#include "creatures/monsters/monster.hpp"
#include "creatures/monsters/monsters.hpp"
#include "creatures/players/grouping/party.hpp"
#include "creatures/players/player.hpp"
#include "creatures/players/imbuements/imbuements.hpp"
#include "creatures/players/wheel/player_wheel.hpp"
#include "game/game.hpp"
#include "game/scheduling/dispatcher.hpp"
#include "io/iobestiary.hpp"
#include "io/ioprey.hpp"
#include "creatures/players/vocations/vocation.hpp"
#include "items/weapons/weapons.hpp"
#include "lib/metrics/metrics.hpp"
#include "lua/callbacks/event_callback.hpp"
Expand Down
88 changes: 64 additions & 24 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include "game/game.hpp"
#include "lua/global/lua_variant.hpp"
#include "lua/scripts/lua_environment.hpp"
#include "lua/scripts/luascript.hpp"
#include "lua/scripts/scripts.hpp"
#include "lib/di/container.hpp"

std::array<int32_t, static_cast<uint8_t>(WheelSpellBoost_t::TOTAL_COUNT)> wheelOfDestinyRegularBoost = { 0 };
std::array<int32_t, static_cast<uint8_t>(WheelSpellBoost_t::TOTAL_COUNT)> wheelOfDestinyUpgradedBoost = { 0 };
Expand Down Expand Up @@ -277,29 +278,45 @@ Position Spells::getCasterPosition(const std::shared_ptr<Creature> &creature, Di
return getNextPosition(dir, creature->getPosition());
}

LuaScriptInterface* BaseSpell::getScriptInterface() const {
return &g_scripts().getScriptInterface();
}

bool BaseSpell::loadScriptId() {
LuaScriptInterface &luaInterface = g_scripts().getScriptInterface();
m_scriptId = luaInterface.getEvent();
if (m_scriptId == -1) {
g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName());
return false;
}

return true;
}

int32_t BaseSpell::getScriptId() const {
return m_scriptId;
}

void BaseSpell::setScriptId(int32_t newScriptId) {
m_scriptId = newScriptId;
}

bool BaseSpell::isLoadedScriptId() const {
return m_scriptId != 0;
}

CombatSpell::CombatSpell(const std::shared_ptr<Combat> &newCombat, bool newNeedTarget, bool newNeedDirection) :
Script(&g_spells().getScriptInterface()),
m_combat(newCombat),
needDirection(newNeedDirection),
needTarget(newNeedTarget) {
// Empty
}

bool CombatSpell::loadScriptCombat() {
m_combat = g_luaEnvironment().getCombatObject(g_luaEnvironment().lastCombatId);
return m_combat != nullptr;
}

std::shared_ptr<Combat> CombatSpell::getCombat() const {
return m_combat;
}

std::string CombatSpell::getScriptTypeName() const {
return "onCastSpell";
}

bool CombatSpell::castSpell(const std::shared_ptr<Creature> &creature) {
if (isLoadedCallback()) {
if (isLoadedScriptId()) {
LuaVariant var;
var.type = VARIANT_POSITION;

Expand Down Expand Up @@ -346,7 +363,7 @@ bool CombatSpell::castSpell(const std::shared_ptr<Creature> &creature, const std
return false;
}

if (isLoadedCallback()) {
if (isLoadedScriptId()) {
LuaVariant var;
if (combat->hasArea()) {
var.type = VARIANT_POSITION;
Expand Down Expand Up @@ -412,6 +429,8 @@ bool CombatSpell::executeCastSpell(const std::shared_ptr<Creature> &creature, co
return getScriptInterface()->callFunction(2);
}

Spell::Spell() = default;

bool Spell::playerSpellCheck(const std::shared_ptr<Player> &player) const {
if (player->hasFlag(PlayerFlags_t::CannotUseSpells)) {
return false;
Expand Down Expand Up @@ -1030,6 +1049,8 @@ void Spell::setLockedPZ(bool b) {
pzLocked = b;
}

InstantSpell::InstantSpell() = default;

bool InstantSpell::playerCastInstant(const std::shared_ptr<Player> &player, std::string &param) const {
if (!playerSpellCheck(player)) {
return false;
Expand Down Expand Up @@ -1161,10 +1182,6 @@ bool InstantSpell::canThrowSpell(const std::shared_ptr<Creature> &creature, cons
return true;
}

std::string InstantSpell::getScriptTypeName() const {
return "onCastSpell";
}

bool InstantSpell::castSpell(const std::shared_ptr<Creature> &creature) {
LuaVariant var;
var.instantName = getName();
Expand Down Expand Up @@ -1294,6 +1311,33 @@ bool InstantSpell::canCast(const std::shared_ptr<Player> &player) const {
return false;
}

LuaScriptInterface* RuneSpell::getScriptInterface() const {
return &g_scripts().getScriptInterface();
}

bool RuneSpell::loadScriptId() {
LuaScriptInterface &luaInterface = g_scripts().getScriptInterface();
m_scriptId = luaInterface.getEvent();
if (m_scriptId == -1) {
g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName());
return false;
}

return true;
}

int32_t RuneSpell::getScriptId() const {
return m_scriptId;
}

void RuneSpell::setScriptId(int32_t newScriptId) {
m_scriptId = newScriptId;
}

bool RuneSpell::isLoadedScriptId() const {
return m_scriptId != 0;
}

ReturnValue RuneSpell::canExecuteAction(const std::shared_ptr<Player> &player, const Position &toPos) {
if (player->hasFlag(PlayerFlags_t::CannotUseSpells)) {
return RETURNVALUE_CANNOTUSETHISOBJECT;
Expand Down Expand Up @@ -1329,7 +1373,7 @@ bool RuneSpell::executeUse(const std::shared_ptr<Player> &player, const std::sha
}

// If script not loaded correctly, return
if (!isLoadedCallback()) {
if (!isLoadedScriptId()) {
return false;
}

Expand Down Expand Up @@ -1391,13 +1435,9 @@ bool RuneSpell::castSpell(const std::shared_ptr<Creature> &creature, const std::
return internalCastSpell(creature, var, false);
}

std::string RuneSpell::getScriptTypeName() const {
return "onCastSpell";
}

bool RuneSpell::internalCastSpell(const std::shared_ptr<Creature> &creature, const LuaVariant &var, bool isHotkey) const {
bool result;
if (isLoadedCallback()) {
if (isLoadedScriptId()) {
result = executeCastSpell(creature, var, isHotkey);
} else {
result = false;
Expand Down
40 changes: 26 additions & 14 deletions src/creatures/combat/spells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@
#pragma once

#include "lua/creature/actions.hpp"
#include "lua/scripts/scripts.hpp"

enum class WheelSpellBoost_t : uint8_t;
enum class WheelSpellGrade_t : uint8_t;

class InstantSpell;
class RuneSpell;
class Spell;
class Combat;
class Player;
class Creature;
class LuaScriptInterface;

struct LuaVariant;
struct Position;

using VocSpellMap = std::map<uint16_t, bool>;

class Spells final : public Scripts {
class Spells {
public:
Spells();
~Spells();
Expand Down Expand Up @@ -79,11 +83,19 @@ class BaseSpell {
virtual bool castSpell(const std::shared_ptr<Creature> &creature) = 0;
virtual bool castSpell(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &target) = 0;

LuaScriptInterface* getScriptInterface() const;
bool loadScriptId();
int32_t getScriptId() const;
void setScriptId(int32_t newScriptId);
bool isLoadedScriptId() const;

int32_t m_scriptId {};

SoundEffect_t soundImpactEffect = SoundEffect_t::SILENCE;
SoundEffect_t soundCastEffect = SoundEffect_t::SPELL_OR_RUNE;
};

class CombatSpell final : public Script, public BaseSpell, public std::enable_shared_from_this<CombatSpell> {
class CombatSpell final : public BaseSpell, public std::enable_shared_from_this<CombatSpell> {
public:
// Constructor
CombatSpell(const std::shared_ptr<Combat> &newCombat, bool newNeedTarget, bool newNeedDirection);
Expand All @@ -98,12 +110,9 @@ class CombatSpell final : public Script, public BaseSpell, public std::enable_sh
// Scripting spell
bool executeCastSpell(const std::shared_ptr<Creature> &creature, const LuaVariant &var) const;

bool loadScriptCombat();
std::shared_ptr<Combat> getCombat() const;

private:
std::string getScriptTypeName() const override;

std::shared_ptr<Combat> m_combat;

bool needDirection;
Expand All @@ -112,7 +121,7 @@ class CombatSpell final : public Script, public BaseSpell, public std::enable_sh

class Spell : public BaseSpell {
public:
Spell() = default;
Spell();

[[nodiscard]] const std::string &getName() const;
void setName(std::string n);
Expand Down Expand Up @@ -268,10 +277,9 @@ class Spell : public BaseSpell {
friend class SpellFunctions;
};

class InstantSpell final : public Script, public Spell {
class InstantSpell final : public Spell {
public:
using Script::Script;

InstantSpell();
bool playerCastInstant(const std::shared_ptr<Player> &player, std::string &param) const;

bool castSpell(const std::shared_ptr<Creature> &creature) override;
Expand All @@ -295,8 +303,6 @@ class InstantSpell final : public Script, public Spell {
bool canThrowSpell(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &target) const;

private:
[[nodiscard]] std::string getScriptTypeName() const override;

bool needDirection = false;
bool hasParam = false;
bool hasPlayerNameParam = false;
Expand All @@ -308,6 +314,12 @@ class RuneSpell final : public Action, public Spell {
public:
using Action::Action;

LuaScriptInterface* getScriptInterface() const;
bool loadScriptId();
int32_t getScriptId() const;
void setScriptId(int32_t newScriptId);
bool isLoadedScriptId() const;

ReturnValue canExecuteAction(const std::shared_ptr<Player> &player, const Position &toPos) override;
bool hasOwnErrorHandler() override;
std::shared_ptr<Thing> getTarget(const std::shared_ptr<Player> &, const std::shared_ptr<Creature> &targetCreature, const Position &, uint8_t) const override;
Expand All @@ -327,10 +339,10 @@ class RuneSpell final : public Action, public Spell {
void setCharges(uint32_t c);

private:
[[nodiscard]] std::string getScriptTypeName() const override;

bool internalCastSpell(const std::shared_ptr<Creature> &creature, const LuaVariant &var, bool isHotkey) const;

int32_t m_scriptId {};

uint16_t runeId = 0;
uint32_t charges = 0;
bool hasCharges = false;
Expand Down
1 change: 1 addition & 0 deletions src/creatures/monsters/monsters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "game/game.hpp"
#include "items/weapons/weapons.hpp"
#include "lua/scripts/luascript.hpp"
#include "lib/di/container.hpp"

void MonsterType::loadLoot(const std::shared_ptr<MonsterType> &monsterType, LootBlock lootBlock) const {
if (lootBlock.childLoot.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void SpawnMonster::scheduleSpawn(uint32_t spawnMonsterId, spawnBlock_t &sb, cons
}

void SpawnMonster::cleanup() {
for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end(); ) {
for (auto it = spawnedMonsterMap.begin(); it != spawnedMonsterMap.end();) {
const auto &monster = it->second;
if (!monster || monster->isRemoved()) {
auto spawnIt = spawnMonsterMap.find(it->first);
Expand Down
1 change: 1 addition & 0 deletions src/creatures/npcs/npcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lua/scripts/lua_environment.hpp"
#include "lua/scripts/luascript.hpp"
#include "lua/scripts/scripts.hpp"
#include "lib/di/container.hpp"

bool NpcType::canSpawn(const Position &pos) const {
bool canSpawn = true;
Expand Down
1 change: 1 addition & 0 deletions src/game/functions/game_reload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "lua/modules/modules.hpp"
#include "lua/scripts/lua_environment.hpp"
#include "lua/scripts/scripts.hpp"
#include "creatures/players/vocations/vocation.hpp"

GameReload::GameReload() = default;
GameReload::~GameReload() = default;
Expand Down
1 change: 1 addition & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "server/server.hpp"
#include "utils/tools.hpp"
#include "utils/wildcardtree.hpp"
#include "creatures/players/vocations/vocation.hpp"

#include "enums/account_coins.hpp"
#include "enums/account_errors.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/game/game_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum Faction_t {
FACTION_LAST = FACTION_FAFNAR,
};

enum LightState_t {
enum LightState_t : uint8_t {
LIGHT_STATE_DAY,
LIGHT_STATE_NIGHT,
LIGHT_STATE_SUNSET,
Expand Down
Loading

0 comments on commit 1663a47

Please sign in to comment.