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 d28646d commit d2ab35f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class Npc final : public Creature {
RespawnType getRespawnType() const {
return npcType->info.respawnType;
}
void setSpawnNpc(SpawnNpc* newSpawn) {
this->spawnNpc = newSpawn;
void setSpawnNpc(const std::shared_ptr<SpawnNpc> &newSpawn) {
spawnNpc = newSpawn;
}

void setPlayerInteraction(uint32_t playerId, uint16_t topicId = 0);
Expand Down Expand Up @@ -178,7 +178,7 @@ class Npc final : public Creature {
phmap::flat_hash_map<uint32_t, std::weak_ptr<Player>> shopPlayerMap;

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

uint8_t speechBubble;

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/npcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Shop {
ShopBlock shopBlock;
};

class NpcType {
class NpcType : public SharedObject {
struct NpcInfo {
LuaScriptInterface* scriptInterface;

Expand Down
18 changes: 8 additions & 10 deletions src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ bool SpawnsNpc::loadFromXml(const std::string &fileNpcName) {
continue;
}

spawnNpcList.emplace_front(centerPos, radius);
SpawnNpc &spawnNpc = spawnNpcList.front();
const auto &spawnNpc = spawnNpcList.emplace_front(std::make_shared<SpawnNpc>(centerPos, radius));

for (auto childNode : spawnNode.children()) {
if (strcasecmp(childNode.name(), "npc") == 0) {
Expand All @@ -84,7 +83,7 @@ bool SpawnsNpc::loadFromXml(const std::string &fileNpcName) {
);
int64_t interval = pugi::cast<int64_t>(childNode.attribute("spawntime").value()) * 1000;
if (interval >= MINSPAWN_INTERVAL && interval <= MAXSPAWN_INTERVAL) {
spawnNpc.addNpc(nameAttribute.as_string(), pos, dir, static_cast<uint32_t>(interval));
spawnNpc->addNpc(nameAttribute.as_string(), pos, dir, static_cast<uint32_t>(interval));
} else {
if (interval <= MINSPAWN_INTERVAL) {
g_logger().warn("[SpawnsNpc::loadFromXml] - {} {} spawntime can not be less than {} seconds", nameAttribute.as_string(), pos.toString(), MINSPAWN_INTERVAL / 1000);
Expand All @@ -103,16 +102,16 @@ void SpawnsNpc::startup() {
return;
}

for (SpawnNpc &spawnNpc : spawnNpcList) {
spawnNpc.startup();
for (const auto &spawnNpc : spawnNpcList) {
spawnNpc->startup();
}

setStarted(true);
}

void SpawnsNpc::clear() {
for (SpawnNpc &spawnNpc : spawnNpcList) {
spawnNpc.stopEvent();
for (const auto &spawnNpc : spawnNpcList) {
spawnNpc->stopEvent();
}
spawnNpcList.clear();

Expand All @@ -137,8 +136,7 @@ void SpawnNpc::startSpawnNpcCheck() {

SpawnNpc::~SpawnNpc() {
for (const auto &it : spawnedNpcMap) {
auto npc = it.second;
npc->setSpawnNpc(nullptr);
it.second->setSpawnNpc(nullptr);
}
}

Expand Down Expand Up @@ -171,7 +169,7 @@ bool SpawnNpc::spawnNpc(uint32_t spawnId, const std::shared_ptr<NpcType> &npcTyp
}

npc->setDirection(dir);
npc->setSpawnNpc(this);
npc->setSpawnNpc(static_self_cast<SpawnNpc>());
npc->setMasterPos(pos);

spawnedNpcMap.insert(spawned_pair(spawnId, npc));
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/npcs/spawns/spawn_npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct spawnBlockNpc_t {
Direction direction;
};

class SpawnNpc {
class SpawnNpc : public SharedObject {
public:
SpawnNpc(Position initPos, int32_t initRadius) :
centerPos(std::move(initPos)), radius(initRadius) { }
Expand Down Expand Up @@ -94,12 +94,12 @@ class SpawnsNpc {
return fileName = setName;
}

std::forward_list<SpawnNpc> &getSpawnNpcList() {
std::forward_list<std::shared_ptr<SpawnNpc>> &getSpawnNpcList() {
return spawnNpcList;
}

private:
std::forward_list<SpawnNpc> spawnNpcList;
std::forward_list<std::shared_ptr<SpawnNpc>> spawnNpcList;
std::string fileName;
bool loaded = false;
bool started = false;
Expand Down

0 comments on commit d2ab35f

Please sign in to comment.