Skip to content

Commit

Permalink
SpawnNpc to smart pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Sep 22, 2023
1 parent 638a7ad commit d28646d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 56 deletions.
9 changes: 3 additions & 6 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ int32_t Npc::despawnRadius;
uint32_t Npc::npcAutoID = 0x80000000;

std::shared_ptr<Npc> Npc::createNpc(const std::string &name) {
NpcType* npcType = g_npcs().getNpcType(name);
if (!npcType) {
return nullptr;
}
return std::make_shared<Npc>(npcType);
const auto &npcType = g_npcs().getNpcType(name);
return npcType ? std::make_shared<Npc>(npcType) : nullptr;
}

Npc::Npc(NpcType* npcType) :
Npc::Npc(const std::shared_ptr<NpcType> &npcType) :
Creature(),
strDescription(npcType->nameDescription),
npcType(npcType) {
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Npc final : public Creature {
static int32_t despawnRange;
static int32_t despawnRadius;

explicit Npc(NpcType* npcType);
explicit Npc(const std::shared_ptr<NpcType> &npcType);
Npc() = default;
~Npc();

Expand Down Expand Up @@ -177,7 +177,7 @@ class Npc final : public Creature {

phmap::flat_hash_map<uint32_t, std::weak_ptr<Player>> shopPlayerMap;

NpcType* npcType;
std::shared_ptr<NpcType> npcType;
SpawnNpc* spawnNpc = nullptr;

uint8_t speechBubble;
Expand Down
12 changes: 3 additions & 9 deletions src/creatures/npcs/npcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool NpcType::loadCallback(LuaScriptInterface* scriptInterface) {
return true;
}

void NpcType::loadShop(NpcType* npcType, ShopBlock shopBlock) {
void NpcType::loadShop(const std::shared_ptr<NpcType> &npcType, ShopBlock shopBlock) {
ItemType &iType = Item::items.getItemType(shopBlock.itemId);

// Registering item prices globaly.
Expand Down Expand Up @@ -134,19 +134,13 @@ bool Npcs::reload() {
return false;
}

NpcType* Npcs::getNpcType(const std::string &name, bool create /* = false*/) {
std::shared_ptr<NpcType> Npcs::getNpcType(const std::string &name, bool create /* = false*/) {
std::string key = asLowerCaseString(name);
auto it = npcs.find(key);

if (it != npcs.end()) {
return it->second;
}

if (!create) {
return nullptr;
}

npcs[key] = new NpcType(name);

return npcs[key];
return create ? npcs[key] = std::make_shared<NpcType>(name) : nullptr;
}
6 changes: 3 additions & 3 deletions src/creatures/npcs/npcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class NpcType {
std::string nameDescription;
NpcInfo info;

void loadShop(NpcType* npcType, ShopBlock shopBlock);
void loadShop(const std::shared_ptr<NpcType> &npcType, ShopBlock shopBlock);

bool loadCallback(LuaScriptInterface* scriptInterface);
bool canSpawn(const Position &pos);
Expand All @@ -103,15 +103,15 @@ class Npcs {
return inject<Npcs>();
}

NpcType* getNpcType(const std::string &name, bool create = false);
std::shared_ptr<NpcType> getNpcType(const std::string &name, bool create = false);

// Reset npcs informations on reload
bool load(bool loadLibs = true, bool loadNpcs = true, bool reloading = false) const;
bool reload();

private:
std::unique_ptr<LuaScriptInterface> scriptInterface;
std::map<std::string, NpcType*> npcs;
std::map<std::string, std::shared_ptr<NpcType>> npcs;
};

constexpr auto g_npcs = Npcs::getInstance;
4 changes: 2 additions & 2 deletions src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool SpawnNpc::isInSpawnNpcZone(const Position &pos) {
return SpawnsNpc::isInZone(centerPos, radius, pos);
}

bool SpawnNpc::spawnNpc(uint32_t spawnId, NpcType* npcType, const Position &pos, Direction dir, bool startup /*= false*/) {
bool SpawnNpc::spawnNpc(uint32_t spawnId, const std::shared_ptr<NpcType> &npcType, const Position &pos, Direction dir, bool startup /*= false*/) {
auto npc = std::make_shared<Npc>(npcType);
if (startup) {
// No need to send out events to the surrounding since there is no one out there to listen!
Expand Down Expand Up @@ -246,7 +246,7 @@ void SpawnNpc::cleanup() {
}

bool SpawnNpc::addNpc(const std::string &name, const Position &pos, Direction dir, uint32_t scheduleInterval) {
NpcType* npcType = g_npcs().getNpcType(name);
const auto &npcType = g_npcs().getNpcType(name);
if (!npcType) {
g_logger().error("Can not find {}", name);
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/npcs/spawns/spawn_npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NpcType;

struct spawnBlockNpc_t {
Position pos;
NpcType* npcType;
std::shared_ptr<NpcType> npcType;
int64_t lastSpawnNpc;
uint32_t interval;
Direction direction;
Expand Down Expand Up @@ -63,7 +63,7 @@ class SpawnNpc {
uint32_t checkSpawnNpcEvent = 0;

static bool findPlayer(const Position &pos);
bool spawnNpc(uint32_t spawnId, NpcType* npcType, const Position &pos, Direction dir, bool startup = false);
bool spawnNpc(uint32_t spawnId, const std::shared_ptr<NpcType> &npcType, const Position &pos, Direction dir, bool startup = false);
void checkSpawnNpc();
void scheduleSpawnNpc(uint32_t spawnId, spawnBlockNpc_t &sb, uint16_t interval);
};
Expand Down
Loading

0 comments on commit d28646d

Please sign in to comment.