Skip to content

Commit

Permalink
improve: more weak_ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Sep 16, 2023
1 parent e48eb75 commit 79a8809
Show file tree
Hide file tree
Showing 29 changed files with 436 additions and 186 deletions.
17 changes: 13 additions & 4 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ Position Spells::getCasterPosition(std::shared_ptr<Creature> creature, Direction

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

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

bool CombatSpell::castSpell(std::shared_ptr<Creature> creature) {
Expand All @@ -270,6 +270,11 @@ bool CombatSpell::castSpell(std::shared_ptr<Creature> creature) {
pos = creature->getPosition();
}

auto combat = getCombat();
if (!combat) {
return false;
}

if (soundCastEffect != SoundEffect_t::SILENCE) {
combat->setParam(COMBAT_PARAM_CASTSOUND, static_cast<uint32_t>(soundCastEffect));
}
Expand All @@ -283,9 +288,13 @@ bool CombatSpell::castSpell(std::shared_ptr<Creature> creature) {
}

bool CombatSpell::castSpell(std::shared_ptr<Creature> creature, std::shared_ptr<Creature> target) {
auto combat = getCombat();
if (!combat) {
return false;
}

if (isLoadedCallback()) {
LuaVariant var;

if (combat->hasArea()) {
var.type = VARIANT_POSITION;

Expand Down
4 changes: 2 additions & 2 deletions src/creatures/combat/spells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ class CombatSpell final : public Script, public BaseSpell, public std::enable_sh

bool loadScriptCombat();
std::shared_ptr<Combat> getCombat() {
return combat;
return m_combat.lock();
}

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

std::shared_ptr<Combat> combat;
std::weak_ptr<Combat> m_combat;

bool needDirection;
bool needTarget;
Expand Down
3 changes: 0 additions & 3 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ void Creature::onAttacking(uint32_t interval) {
}

onAttacked();
if (!attackedCreature) {
return;
}
attackedCreature->onAttacked();

if (g_game().isSightClear(getPosition(), attackedCreature->getPosition(), true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class Creature : virtual public Thing, public SharedObject {
return m_master.lock();
}

const std::list<std::weak_ptr<Creature>> &getSummons() const {
const weak::list<Creature> &getSummons() const {
return m_summons;
}

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

CountMap damageMap;

std::list<std::weak_ptr<Creature>> m_summons;
weak::list<Creature> m_summons;
CreatureEventList eventsList;
ConditionList conditions;

Expand Down
8 changes: 4 additions & 4 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ bool Monster::getDanceStep(const Position &creaturePos, Direction &moveDirection
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_NORTH)) {
bool result = true;

if (attackedCreature && keepAttack) {
if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x, creaturePos.y - 1, creaturePos.z), attackedCreature));
}

Expand All @@ -1292,7 +1292,7 @@ bool Monster::getDanceStep(const Position &creaturePos, Direction &moveDirection
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_SOUTH)) {
bool result = true;

if (attackedCreature && keepAttack) {
if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x, creaturePos.y + 1, creaturePos.z), attackedCreature));
}

Expand All @@ -1307,7 +1307,7 @@ bool Monster::getDanceStep(const Position &creaturePos, Direction &moveDirection
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_EAST)) {
bool result = true;

if (attackedCreature && keepAttack) {
if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x + 1, creaturePos.y, creaturePos.z), attackedCreature));
}

Expand All @@ -1322,7 +1322,7 @@ bool Monster::getDanceStep(const Position &creaturePos, Direction &moveDirection
if (tmpDist == centerToDist && canWalkTo(creaturePos, DIRECTION_WEST)) {
bool result = true;

if (attackedCreature && keepAttack) {
if (keepAttack) {
result = (!canDoAttackNow || canUseAttack(Position(creaturePos.x - 1, creaturePos.y, creaturePos.z), attackedCreature));
}

Expand Down
8 changes: 5 additions & 3 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,15 @@ void Npc::addShopPlayer(std::shared_ptr<Player> player) {
}

void Npc::removeShopPlayer(std::shared_ptr<Player> player) {
if (player) {
shopPlayerSet.erase(player);
if (!player) {
return;
}
weak::erase(shopPlayerSet, player);
}

void Npc::closeAllShopWindows() {
for (auto shopPlayer : shopPlayerSet) {
for (auto shopPlayerPtr : shopPlayerSet) {
auto shopPlayer = shopPlayerPtr.lock();
if (shopPlayer) {
shopPlayer->closeShopWindow();
}
Expand Down
3 changes: 2 additions & 1 deletion src/creatures/npcs/npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "creatures/npcs/npcs.hpp"
#include "creatures/players/player.hpp"
#include "declarations.hpp"
#include "items/tile.hpp"
#include "lib/di/container.hpp"
Expand Down Expand Up @@ -174,7 +175,7 @@ class Npc final : public Creature {

std::map<uint32_t, uint16_t> playerInteractions;

std::set<std::shared_ptr<Player>> shopPlayerSet;
weak::parallel_flat_hash_set<Player> shopPlayerSet;

NpcType* npcType;
SpawnNpc* spawnNpc = nullptr;
Expand Down
14 changes: 10 additions & 4 deletions src/creatures/players/grouping/guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@

void Guild::addMember(std::shared_ptr<Player> player) {
membersOnline.push_back(player);
for (std::shared_ptr<Player> member : membersOnline) {
for (auto member : getMembersOnline()) {
g_game().updatePlayerHelpers(member);
}
}

void Guild::removeMember(std::shared_ptr<Player> player) {
membersOnline.remove(player);
for (std::shared_ptr<Player> member : membersOnline) {
g_game().updatePlayerHelpers(member);
// loop over to udpate all members and delete the player from the list
for (auto it = membersOnline.begin(); it != membersOnline.end(); ++it) {
if (auto member = it->lock()) {
if (member == player) {
it = membersOnline.erase(it);
} else {
g_game().updatePlayerHelpers(member);
}
}
}

g_game().updatePlayerHelpers(player);
Expand Down
13 changes: 10 additions & 3 deletions src/creatures/players/grouping/guild.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ class Guild : public Bankable {
const std::string &getName() const {
return name;
}
const std::list<std::shared_ptr<Player>> &getMembersOnline() const {
return membersOnline;
std::vector<std::shared_ptr<Player>> getMembersOnline() const {
auto result = std::vector<std::shared_ptr<Player>>(membersOnline.size());
std::transform(membersOnline.begin(), membersOnline.end(), result.begin(), [](const std::weak_ptr<Player> &weak) {
return weak.lock();
});
return result;
}
uint32_t getMemberCountOnline() const {
return membersOnline.size();
}
uint32_t getMemberCount() const {
return memberCount;
Expand Down Expand Up @@ -81,7 +88,7 @@ class Guild : public Bankable {
}

private:
std::list<std::shared_ptr<Player>> membersOnline;
weak::list<Player> membersOnline;
std::vector<GuildRank_ptr> ranks;
std::string name;
uint64_t bankBalance = 0;
Expand Down
Loading

0 comments on commit 79a8809

Please sign in to comment.