diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-04-10 23:04:42 +0200 |
---|---|---|
committer | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-04-11 13:47:17 +0200 |
commit | d7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da (patch) | |
tree | 1b4d200ba66c6afcdb7763951980476756339cf1 /src/game-server | |
parent | aa04597c5f8bb806996d604699fc8ebff6d53bdd (diff) | |
download | manaserv-d7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da.tar.gz manaserv-d7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da.tar.bz2 manaserv-d7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da.tar.xz manaserv-d7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da.zip |
Converted Being into a Component
I did not really care too much about staying consistent with the use of
static_casts to Actors since they are only temporary anyway until Actor
is a component too.
Diffstat (limited to 'src/game-server')
39 files changed, 841 insertions, 683 deletions
diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index bdf44b4e..622fc1d6 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.cpp @@ -155,7 +155,8 @@ void AccountConnection::processMessage(MessageIn &msg) case AGMSG_PLAYER_ENTER: { std::string token = msg.readString(MAGIC_TOKEN_LENGTH); - Being *character = new Being(OBJECT_CHARACTER); + Entity *character = new Actor(OBJECT_CHARACTER); + character->addComponent(new BeingComponent(*character)); character->addComponent(new CharacterComponent(*character, msg)); gameHandler->addPendingCharacter(token, character); } break; @@ -243,7 +244,7 @@ void AccountConnection::processMessage(MessageIn &msg) case CGMSG_POST_RESPONSE: { // get the character - Being *character = postMan->getCharacter(msg.readInt32()); + Entity *character = postMan->getCharacter(msg.readInt32()); // check character is still valid if (!character) @@ -261,7 +262,7 @@ void AccountConnection::processMessage(MessageIn &msg) case CGMSG_STORE_POST_RESPONSE: { // get character - Being *character = postMan->getCharacter(msg.readInt32()); + Entity *character = postMan->getCharacter(msg.readInt32()); // check character is valid if (!character) diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp index b50a5fac..ad3fcfc7 100644 --- a/src/game-server/being.cpp +++ b/src/game-server/being.cpp @@ -38,11 +38,10 @@ #include "scripting/scriptmanager.h" -Script::Ref Being::mRecalculateDerivedAttributesCallback; -Script::Ref Being::mRecalculateBaseAttributeCallback; +Script::Ref BeingComponent::mRecalculateDerivedAttributesCallback; +Script::Ref BeingComponent::mRecalculateBaseAttributeCallback; -Being::Being(EntityType type): - Actor(type), +BeingComponent::BeingComponent(Entity &entity): mAction(STAND), mGender(GENDER_UNSPECIFIED), mDirection(DOWN), @@ -60,10 +59,12 @@ Being::Being(EntityType type): LOG_DEBUG("Attempting to create attribute '" << it1->first << "'."); mAttributes.insert(std::make_pair(it1->first, Attribute(*it1->second))); - } - signal_inserted.connect(sigc::mem_fun(this, &Being::inserted)); + clearDestination(entity); + + entity.signal_inserted.connect(sigc::mem_fun(this, + &BeingComponent::inserted)); // TODO: Way to define default base values? // Should this be handled by the virtual modifiedAttribute? @@ -78,17 +79,20 @@ Being::Being(EntityType type): #endif } -void Being::triggerEmote(int id) +void BeingComponent::triggerEmote(Entity &entity, int id) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + mEmoteId = id; if (id > -1) - raiseUpdateFlags(UPDATEFLAG_EMOTE); + actor.raiseUpdateFlags(UPDATEFLAG_EMOTE); } -void Being::heal() +void BeingComponent::heal(Entity &entity) { Attribute &hp = mAttributes.at(ATTR_HP); Attribute &maxHp = mAttributes.at(ATTR_MAX_HP); @@ -97,10 +101,10 @@ void Being::heal() // Reset all modifications present in hp. hp.clearMods(); - setAttribute(ATTR_HP, maxHp.getModifiedAttribute()); + setAttribute(entity, ATTR_HP, maxHp.getModifiedAttribute()); } -void Being::heal(int gain) +void BeingComponent::heal(Entity &entity, int gain) { Attribute &hp = mAttributes.at(ATTR_HP); Attribute &maxHp = mAttributes.at(ATTR_MAX_HP); @@ -108,44 +112,71 @@ void Being::heal(int gain) return; // Full hp, do nothing. // Cannot go over maximum hitpoints. - setAttribute(ATTR_HP, hp.getBase() + gain); + setAttribute(entity, ATTR_HP, hp.getBase() + gain); if (hp.getModifiedAttribute() > maxHp.getModifiedAttribute()) - setAttribute(ATTR_HP, maxHp.getModifiedAttribute()); + setAttribute(entity, ATTR_HP, maxHp.getModifiedAttribute()); } -void Being::died() +void BeingComponent::died(Entity &entity) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + if (mAction == DEAD) return; - LOG_DEBUG("Being " << getPublicID() << " died."); - setAction(DEAD); + LOG_DEBUG("Being " << actor.getPublicID() << " died."); + setAction(entity, DEAD); // dead beings stay where they are - clearDestination(); + clearDestination(entity); - signal_died.emit(this); + signal_died.emit(&entity); } -void Being::setDestination(const Point &dst) +void BeingComponent::setDestination(Entity &entity, const Point &dst) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + mDst = dst; - raiseUpdateFlags(UPDATEFLAG_NEW_DESTINATION); + actor.raiseUpdateFlags(UPDATEFLAG_NEW_DESTINATION); mPath.clear(); } -Path Being::findPath() +void BeingComponent::clearDestination(Entity &entity) { - Map *map = getMap()->getMap(); + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + + setDestination(entity, actor.getPosition()); +} + +void BeingComponent::setDirection(Entity &entity, BeingDirection direction) +{ + // Temponary until all dependencies are available as components + Actor &actor = static_cast<Actor &>(entity); + mDirection = direction; + actor.raiseUpdateFlags(UPDATEFLAG_DIRCHANGE); +} + +Path BeingComponent::findPath(Entity &entity) +{ + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + + Map *map = entity.getMap()->getMap(); int tileWidth = map->getTileWidth(); int tileHeight = map->getTileHeight(); - int startX = getPosition().x / tileWidth; - int startY = getPosition().y / tileHeight; + int startX = actor.getPosition().x / tileWidth; + int startY = actor.getPosition().y / tileHeight; int destX = mDst.x / tileWidth, destY = mDst.y / tileHeight; - return map->findPath(startX, startY, destX, destY, getWalkMask()); + return map->findPath(startX, startY, destX, destY, actor.getWalkMask()); } -void Being::updateDirection(const Point ¤tPos, const Point &destPos) +void BeingComponent::updateDirection(Entity &entity, + const Point ¤tPos, + const Point &destPos) { // We update the being direction on each tile to permit other beings // entering in range to always see the being with a direction value. @@ -161,18 +192,18 @@ void Being::updateDirection(const Point ¤tPos, const Point &destPos) if (currentPos.x == destPos.x) { if (currentPos.y > destPos.y) - setDirection(UP); + setDirection(entity, UP); else - setDirection(DOWN); + setDirection(entity, DOWN); return; } if (currentPos.y == destPos.y) { if (currentPos.x > destPos.x) - setDirection(LEFT); + setDirection(entity, LEFT); else - setDirection(RIGHT); + setDirection(entity, RIGHT); return; } @@ -186,9 +217,9 @@ void Being::updateDirection(const Point ¤tPos, const Point &destPos) // Compute tan of the angle if ((currentPos.y - destPos.y) / (destPos.x - currentPos.x) < 1) // The angle is less than 45°, we look to the right - setDirection(RIGHT); + setDirection(entity, RIGHT); else - setDirection(UP); + setDirection(entity, UP); return; } else // Down-right @@ -196,9 +227,9 @@ void Being::updateDirection(const Point ¤tPos, const Point &destPos) // Compute tan of the angle if ((destPos.y - currentPos.y) / (destPos.x - currentPos.x) < 1) // The angle is less than 45°, we look to the right - setDirection(RIGHT); + setDirection(entity, RIGHT); else - setDirection(DOWN); + setDirection(entity, DOWN); return; } } @@ -210,9 +241,9 @@ void Being::updateDirection(const Point ¤tPos, const Point &destPos) // Compute tan of the angle if ((currentPos.y - destPos.y) / (currentPos.x - destPos.x) < 1) // The angle is less than 45°, we look to the left - setDirection(LEFT); + setDirection(entity, LEFT); else - setDirection(UP); + setDirection(entity, UP); return; } else // Down-left @@ -220,16 +251,19 @@ void Being::updateDirection(const Point ¤tPos, const Point &destPos) // Compute tan of the angle if ((destPos.y - currentPos.y) / (currentPos.x - destPos.x) < 1) // The angle is less than 45°, we look to the left - setDirection(LEFT); + setDirection(entity, LEFT); else - setDirection(DOWN); + setDirection(entity, DOWN); return; } } } -void Being::move() +void BeingComponent::move(Entity &entity) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + // Immobile beings cannot move. if (!checkAttributeExists(ATTR_MOVE_SPEED_RAW) || !getModifiedAttribute(ATTR_MOVE_SPEED_RAW)) @@ -238,10 +272,10 @@ void Being::move() // Remember the current position before moving. This is used by // MapComposite::update() to determine whether a being has moved from one // zone to another. - mOld = getPosition(); + mOld = actor.getPosition(); // Ignore not moving beings - if (mAction == STAND && mDst == getPosition()) + if (mAction == STAND && mDst == actor.getPosition()) return; if (mMoveTime > WORLD_TICK_MS) @@ -251,22 +285,22 @@ void Being::move() return; } - Map *map = getMap()->getMap(); + Map *map = entity.getMap()->getMap(); int tileWidth = map->getTileWidth(); int tileHeight = map->getTileHeight(); - int tileSX = getPosition().x / tileWidth; - int tileSY = getPosition().y / tileHeight; + int tileSX = actor.getPosition().x / tileWidth; + int tileSY = actor.getPosition().y / tileHeight; int tileDX = mDst.x / tileWidth; int tileDY = mDst.y / tileHeight; if (tileSX == tileDX && tileSY == tileDY) { if (mAction == WALK) - setAction(STAND); + setAction(entity, STAND); // Moving while staying on the same tile is free // We only update the direction in that case. - updateDirection(getPosition(), mDst); - setPosition(mDst); + updateDirection(entity, actor.getPosition(), mDst); + actor.setPosition(mDst); mMoveTime = 0; return; } @@ -281,7 +315,8 @@ void Being::move() for (PathIterator pathIterator = mPath.begin(); pathIterator != mPath.end(); pathIterator++) { - if (!map->getWalk(pathIterator->x, pathIterator->y, getWalkMask())) + if (!map->getWalk(pathIterator->x, pathIterator->y, + actor.getWalkMask())) { mPath.clear(); break; @@ -292,20 +327,20 @@ void Being::move() { // No path exists: the walkability of cached path has changed, the // destination has changed, or a path was never set. - mPath = findPath(); + mPath = findPath(entity); } if (mPath.empty()) { if (mAction == WALK) - setAction(STAND); + setAction(entity, STAND); // no path was found mDst = mOld; mMoveTime = 0; return; } - setAction(WALK); + setAction(entity, WALK); Point prev(tileSX, tileSY); Point pos; @@ -330,15 +365,15 @@ void Being::move() pos.y = next.y * tileHeight + (tileHeight / 2); } while (mMoveTime < WORLD_TICK_MS); - setPosition(pos); + actor.setPosition(pos); mMoveTime = mMoveTime > WORLD_TICK_MS ? mMoveTime - WORLD_TICK_MS : 0; // Update the being direction also - updateDirection(mOld, pos); + updateDirection(entity, mOld, pos); } -int Being::directionToAngle(int direction) +int BeingComponent::directionToAngle(int direction) { switch (direction) { @@ -350,37 +385,42 @@ int Being::directionToAngle(int direction) } } -void Being::setAction(BeingAction action) +void BeingComponent::setAction(Entity &entity, BeingAction action) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + mAction = action; if (action != ATTACK && // The players are informed about these actions action != WALK) // by other messages { - raiseUpdateFlags(UPDATEFLAG_ACTIONCHANGE); + actor.raiseUpdateFlags(UPDATEFLAG_ACTIONCHANGE); } } -void Being::applyModifier(unsigned attr, double value, unsigned layer, - unsigned duration, unsigned id) +void BeingComponent::applyModifier(Entity &entity, unsigned attr, double value, + unsigned layer, unsigned duration, + unsigned id) { mAttributes.at(attr).add(duration, value, layer, id); - updateDerivedAttributes(attr); + updateDerivedAttributes(entity, attr); } -bool Being::removeModifier(unsigned attr, double value, unsigned layer, - unsigned id, bool fullcheck) +bool BeingComponent::removeModifier(Entity &entity, unsigned attr, + double value, unsigned layer, + unsigned id, bool fullcheck) { bool ret = mAttributes.at(attr).remove(value, layer, id, fullcheck); - updateDerivedAttributes(attr); + updateDerivedAttributes(entity, attr); return ret; } -void Being::setGender(BeingGender gender) +void BeingComponent::setGender(BeingGender gender) { mGender = gender; } -void Being::setAttribute(unsigned id, double value) +void BeingComponent::setAttribute(Entity &entity, unsigned id, double value) { AttributeMap::iterator ret = mAttributes.find(id); if (ret == mAttributes.end()) @@ -396,35 +436,35 @@ void Being::setAttribute(unsigned id, double value) else { ret->second.setBase(value); - updateDerivedAttributes(id); + updateDerivedAttributes(entity, id); } } -void Being::createAttribute(unsigned id, const AttributeManager::AttributeInfo +void BeingComponent::createAttribute(unsigned id, const AttributeManager::AttributeInfo &attributeInfo) { mAttributes.insert(std::pair<unsigned, Attribute> (id,Attribute(attributeInfo))); } -const Attribute *Being::getAttribute(unsigned id) const +const Attribute *BeingComponent::getAttribute(unsigned id) const { AttributeMap::const_iterator ret = mAttributes.find(id); if (ret == mAttributes.end()) { - LOG_DEBUG("Being::getAttribute: Attribute " + LOG_DEBUG("BeingComponent::getAttribute: Attribute " << id << " not found! Returning 0."); return 0; } return &ret->second; } -double Being::getAttributeBase(unsigned id) const +double BeingComponent::getAttributeBase(unsigned id) const { AttributeMap::const_iterator ret = mAttributes.find(id); if (ret == mAttributes.end()) { - LOG_DEBUG("Being::getAttributeBase: Attribute " + LOG_DEBUG("BeingComponent::getAttributeBase: Attribute " << id << " not found! Returning 0."); return 0; } @@ -432,32 +472,32 @@ double Being::getAttributeBase(unsigned id) const } -double Being::getModifiedAttribute(unsigned id) const +double BeingComponent::getModifiedAttribute(unsigned id) const { AttributeMap::const_iterator ret = mAttributes.find(id); if (ret == mAttributes.end()) { - LOG_DEBUG("Being::getModifiedAttribute: Attribute " + LOG_DEBUG("BeingComponent::getModifiedAttribute: Attribute " << id << " not found! Returning 0."); return 0; } return ret->second.getModifiedAttribute(); } -void Being::setModAttribute(unsigned, double) +void BeingComponent::setModAttribute(unsigned, double) { // No-op to satisfy shared structure. // The game-server calculates this manually. return; } -void Being::recalculateBaseAttribute(unsigned attr) +void BeingComponent::recalculateBaseAttribute(Entity &entity, unsigned attr) { LOG_DEBUG("Being: Received update attribute recalculation request for " << attr << "."); if (!mAttributes.count(attr)) { - LOG_DEBUG("Being::recalculateBaseAttribute: " << attr << " not found!"); + LOG_DEBUG("BeingComponent::recalculateBaseAttribute: " << attr << " not found!"); return; } @@ -467,7 +507,7 @@ void Being::recalculateBaseAttribute(unsigned attr) double newBase = utils::tpsToRawSpeed( getModifiedAttribute(ATTR_MOVE_SPEED_TPS)); if (newBase != getAttributeBase(attr)) - setAttribute(attr, newBase); + setAttribute(entity, attr, newBase); return; } @@ -476,14 +516,17 @@ void Being::recalculateBaseAttribute(unsigned attr) Script *script = ScriptManager::currentState(); script->prepare(mRecalculateBaseAttributeCallback); - script->push(this); + script->push(&entity); script->push(attr); - script->execute(getMap()); + script->execute(entity.getMap()); } -void Being::updateDerivedAttributes(unsigned attr) +void BeingComponent::updateDerivedAttributes(Entity &entity, unsigned attr) { - signal_attribute_changed.emit(this, attr); + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + + signal_attribute_changed.emit(&entity, attr); LOG_DEBUG("Being: Updating derived attribute(s) of: " << attr); @@ -492,12 +535,12 @@ void Being::updateDerivedAttributes(unsigned attr) { case ATTR_MAX_HP: case ATTR_HP: - raiseUpdateFlags(UPDATEFLAG_HEALTHCHANGE); + actor.raiseUpdateFlags(UPDATEFLAG_HEALTHCHANGE); break; case ATTR_MOVE_SPEED_TPS: // Does not make a lot of sense to have in the scripts. // So handle it here: - recalculateBaseAttribute(ATTR_MOVE_SPEED_RAW); + recalculateBaseAttribute(entity, ATTR_MOVE_SPEED_RAW); break; } @@ -506,12 +549,12 @@ void Being::updateDerivedAttributes(unsigned attr) Script *script = ScriptManager::currentState(); script->prepare(mRecalculateDerivedAttributesCallback); - script->push(this); + script->push(&entity); script->push(attr); - script->execute(getMap()); + script->execute(entity.getMap()); } -void Being::applyStatusEffect(int id, int timer) +void BeingComponent::applyStatusEffect(int id, int timer) { if (mAction == DEAD) return; @@ -529,12 +572,12 @@ void Being::applyStatusEffect(int id, int timer) } } -void Being::removeStatusEffect(int id) +void BeingComponent::removeStatusEffect(int id) { setStatusEffectTime(id, 0); } -bool Being::hasStatusEffect(int id) const +bool BeingComponent::hasStatusEffect(int id) const { StatusEffects::const_iterator it = mStatus.begin(); while (it != mStatus.end()) @@ -546,21 +589,24 @@ bool Being::hasStatusEffect(int id) const return false; } -unsigned Being::getStatusEffectTime(int id) const +unsigned BeingComponent::getStatusEffectTime(int id) const { StatusEffects::const_iterator it = mStatus.find(id); if (it != mStatus.end()) return it->second.time; else return 0; } -void Being::setStatusEffectTime(int id, int time) +void BeingComponent::setStatusEffectTime(int id, int time) { StatusEffects::iterator it = mStatus.find(id); if (it != mStatus.end()) it->second.time = time; } -void Being::update() +void BeingComponent::update(Entity &entity) { + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + int oldHP = getModifiedAttribute(ATTR_HP); int newHP = oldHP; int maxHP = getModifiedAttribute(ATTR_MAX_HP); @@ -579,8 +625,8 @@ void Being::update() // Only update HP when it actually changed to avoid network noise if (newHP != oldHP) { - setAttribute(ATTR_HP, newHP); - raiseUpdateFlags(UPDATEFLAG_HEALTHCHANGE); + setAttribute(entity, ATTR_HP, newHP); + actor.raiseUpdateFlags(UPDATEFLAG_HEALTHCHANGE); } // Update lifetime of effects. @@ -589,7 +635,7 @@ void Being::update() ++it) { if (it->second.tick()) - updateDerivedAttributes(it->first); + updateDerivedAttributes(entity, it->first); } // Update and run status effects @@ -598,7 +644,7 @@ void Being::update() { it->second.time--; if (it->second.time > 0 && mAction != DEAD) - it->second.status->tick(this, it->second.time); + it->second.status->tick(entity, it->second.time); if (it->second.time <= 0 || mAction == DEAD) { @@ -614,15 +660,16 @@ void Being::update() // Check if being died if (getModifiedAttribute(ATTR_HP) <= 0 && mAction != DEAD) - died(); - - Actor::update(); + died(entity); } -void Being::inserted(Entity *) +void BeingComponent::inserted(Entity *entity) { + // Temporary until all depdencies are available as component + Actor *actor = static_cast<Actor *>(entity); + // Reset the old position, since after insertion it is important that it is // in sync with the zone that we're currently present in. - mOld = getPosition(); + mOld = actor->getPosition(); } diff --git a/src/game-server/being.h b/src/game-server/being.h index de30ad60..dd9790db 100644 --- a/src/game-server/being.h +++ b/src/game-server/being.h @@ -32,7 +32,7 @@ #include "game-server/attack.h" #include "game-server/timeout.h" -class Being; +class BeingComponent; class MapComposite; class StatusEffect; @@ -50,29 +50,31 @@ typedef std::map< int, Status > StatusEffects; * Generic being (living actor). Keeps direction, destination and a few other * relevant properties. Used for characters & monsters (all animated objects). */ -class Being : public Actor +class BeingComponent : public Component, public sigc::trackable { public: + static const ComponentType type = CT_Being; + /** * Proxy constructor. */ - Being(EntityType type); + BeingComponent(Entity &entity); /** * Update being state. */ - virtual void update(); + virtual void update(Entity &entity); /** Restores all hit points of the being */ - void heal(); + void heal(Entity &entity); /** Restores a specific number of hit points of the being */ - void heal(int hp); + void heal(Entity &entity, int hp); /** * Changes status and calls all the "died" listeners. */ - virtual void died(); + virtual void died(Entity &entity); /** * Gets the destination coordinates of the being. @@ -83,14 +85,13 @@ class Being : public Actor /** * Sets the destination coordinates of the being. */ - void setDestination(const Point &dst); + void setDestination(Entity &entity, const Point &dst); /** * Sets the destination coordinates of the being to the current * position. */ - void clearDestination() - { setDestination(getPosition()); } + void clearDestination(Entity &entity); /** * Gets the old coordinates of the being. @@ -101,15 +102,14 @@ class Being : public Actor /** * Sets the facing direction of the being. */ - void setDirection(BeingDirection direction) - { mDirection = direction; raiseUpdateFlags(UPDATEFLAG_DIRCHANGE); } + void setDirection(Entity &entity, BeingDirection direction); BeingDirection getDirection() const { return mDirection; } /** * Sets the current action. */ - void setAction(BeingAction action); + void setAction(Entity &entity, BeingAction action); /** * Sets the current action. @@ -120,12 +120,12 @@ class Being : public Actor /** * Moves the being toward its destination. */ - void move(); + void move(Entity &entity); /** * Returns the path to the being's current destination. */ - virtual Path findPath(); + virtual Path findPath(Entity &); /** Gets the gender of the being (male or female). */ BeingGender getGender() const @@ -137,7 +137,7 @@ class Being : public Actor /** * Sets an attribute. */ - void setAttribute(unsigned id, double value); + void setAttribute(Entity &entity, unsigned id, double value); /** * Creates an Attribute that did not exist before @@ -188,11 +188,13 @@ class Being : public Actor * @param lvl If non-zero, indicates that a temporary modifier can be * dispelled prematuraly by a spell of given level. */ - void applyModifier(unsigned attr, double value, unsigned layer, - unsigned duration = 0, unsigned id = 0); + void applyModifier(Entity &entity, unsigned attr, double value, + unsigned layer, unsigned duration = 0, + unsigned id = 0); - bool removeModifier(unsigned attr, double value, unsigned layer, - unsigned id = 0, bool fullcheck = false); + bool removeModifier(Entity &entity, unsigned attr, double value, + unsigned layer, unsigned id = 0, + bool fullcheck = false); /** * Called when an attribute modifier is changed. @@ -200,14 +202,14 @@ class Being : public Actor * attributes if it has changed. * @returns Whether it was changed. */ - void recalculateBaseAttribute(unsigned); + void recalculateBaseAttribute(Entity &, unsigned); /** * Attribute has changed, recalculate base value of dependant * attributes (and handle other actions for the modified * attribute) */ - void updateDerivedAttributes(unsigned); + void updateDerivedAttributes(Entity &entity, unsigned); /** * Sets a statuseffect on this being @@ -256,13 +258,13 @@ class Being : public Actor static void setRecalculateBaseAttributeCallback(Script *script) { script->assignCallback(mRecalculateBaseAttributeCallback); } - sigc::signal<void, Being *> signal_died; - sigc::signal<void, Being *, unsigned> signal_attribute_changed; + sigc::signal<void, Entity *> signal_died; + sigc::signal<void, Entity *, unsigned> signal_attribute_changed; /** * Activate an emote flag on the being. */ - void triggerEmote(int id); + void triggerEmote(Entity &entity, int id); /** * Tells the last emote used. @@ -274,7 +276,8 @@ class Being : public Actor * Update the being direction when moving so avoid directions desyncs * with other clients. */ - void updateDirection(const Point ¤tPos, + void updateDirection(Entity &entity, + const Point ¤tPos, const Point &destPos); protected: @@ -290,8 +293,8 @@ class Being : public Actor BeingGender mGender; /**< Gender of the being. */ private: - Being(const Being &rhs); - Being &operator=(const Being &rhs); + BeingComponent(const BeingComponent &rhs); + BeingComponent &operator=(const BeingComponent &rhs); /** * Connected to signal_inserted to reset the old position. diff --git a/src/game-server/buysell.cpp b/src/game-server/buysell.cpp index e73e13c5..3ba29ef6 100644 --- a/src/game-server/buysell.cpp +++ b/src/game-server/buysell.cpp @@ -30,7 +30,7 @@ #include <algorithm> -BuySell::BuySell(Being *c, bool sell): +BuySell::BuySell(Actor *c, bool sell): mCurrencyId(ATTR_GP), mChar(c), mSell(sell) { c->getComponent<CharacterComponent>()->setBuySell(this); @@ -156,22 +156,28 @@ void BuySell::perform(unsigned id, int amount) for (TradedItems::iterator i = mItems.begin(), i_end = mItems.end(); i != i_end; ++i) { - if (i->itemId != id) continue; - if (i->amount && i->amount <= amount) amount = i->amount; + auto *beingComponent = mChar->getComponent<BeingComponent>(); + + if (i->itemId != id) + continue; + if (i->amount && i->amount <= amount) + amount = i->amount; if (mSell) { amount -= inv.remove(id, amount); - mChar->setAttribute(mCurrencyId, - mChar->getAttributeBase(mCurrencyId) + - amount * i->cost); + const double currentMoney = + beingComponent->getAttributeBase(mCurrencyId); + beingComponent->setAttribute(*mChar, mCurrencyId, + currentMoney + amount * i->cost); } else { - amount = std::min(amount, ((int) mChar->getAttributeBase(mCurrencyId)) / i->cost); + const double currentMoney = + beingComponent->getAttributeBase(mCurrencyId); + amount = std::min(amount, ((int)currentMoney) / i->cost); amount -= inv.insert(id, amount); - mChar->setAttribute(mCurrencyId, - mChar->getAttributeBase(mCurrencyId) - - amount * i->cost); + beingComponent->setAttribute(*mChar, mCurrencyId, + currentMoney - amount * i->cost); } if (i->amount) { diff --git a/src/game-server/buysell.h b/src/game-server/buysell.h index 8450aff1..88904f4d 100644 --- a/src/game-server/buysell.h +++ b/src/game-server/buysell.h @@ -23,8 +23,8 @@ #include <vector> -class Being; class Actor; +class Entity; class BuySell { @@ -33,7 +33,7 @@ class BuySell /** * Sets up a trade between a character and an NPC. */ - BuySell(Being *, bool sell); + BuySell(Actor *, bool sell); /** * Cancels the trade. @@ -80,7 +80,7 @@ class BuySell /** The attribute ID of the currency to use. Hardcoded for now (FIXME) */ unsigned mCurrencyId; - Being *mChar; /**< Character involved. */ + Actor *mChar; /**< Character involved. */ TradedItems mItems; /**< Traded items. */ bool mSell; /**< Are items sold? */ }; diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index 63621491..8f35fef4 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -91,16 +91,19 @@ CharacterComponent::CharacterComponent(Entity &entity, MessageIn &msg): mBaseEntity(&entity) { // Temporary until all dependencies are in a component - Being &being = static_cast<Being &>(entity); + Actor &being = static_cast<Actor &>(entity); mCharacterData = new CharacterData(&being, this); + auto *beingComponent = entity.getComponent<BeingComponent>(); + const AttributeManager::AttributeScope &attributes = attributeManager->getAttributeScope(CharacterScope); LOG_DEBUG("Character creation: initialisation of " << attributes.size() << " attributes."); for (auto attributeScope : attributes) - being.createAttribute(attributeScope.first, *attributeScope.second); + beingComponent->createAttribute(attributeScope.first, + *attributeScope.second); being.setWalkMask(Map::BLOCKMASK_WALL); @@ -115,7 +118,7 @@ CharacterComponent::CharacterComponent(Entity &entity, MessageIn &msg): sigc::mem_fun(this, &CharacterComponent::attackRemoved)); // Default knuckle attack - int damageBase = being.getModifiedAttribute(ATTR_STR); + int damageBase = beingComponent->getModifiedAttribute(ATTR_STR); int damageDelta = damageBase / 2; Damage knuckleDamage; knuckleDamage.skill = skillManager->getDefaultSkillId(); @@ -131,7 +134,7 @@ CharacterComponent::CharacterComponent(Entity &entity, MessageIn &msg): // Get character data. mDatabaseID = msg.readInt32(); - being.setName(msg.readString()); + beingComponent->setName(msg.readString()); deserializeCharacterData(*mCharacterData, msg); @@ -139,7 +142,7 @@ CharacterComponent::CharacterComponent(Entity &entity, MessageIn &msg): modifiedAllAttributes(entity); being.setSize(16); - being.signal_attribute_changed.connect(sigc::mem_fun( + beingComponent->signal_attribute_changed.connect(sigc::mem_fun( this, &CharacterComponent::attributeChanged)); } @@ -151,9 +154,6 @@ CharacterComponent::~CharacterComponent() void CharacterComponent::update(Entity &entity) { - // Temporary until all dependencies are available as component - Being &being = static_cast<Being &>(entity); - // Update character level if needed. if (mRecalculateLevel) { @@ -162,7 +162,7 @@ void CharacterComponent::update(Entity &entity) } // Dead character: don't regenerate anything else - if (being.getAction() == DEAD) + if (entity.getComponent<BeingComponent>()->getAction() == DEAD) return; // Update special recharge @@ -192,25 +192,24 @@ void CharacterComponent::update(Entity &entity) } } -void CharacterComponent::characterDied(Being *being) +void CharacterComponent::characterDied(Entity *being) { executeCallback(mDeathCallback, *being); } void CharacterComponent::respawn(Entity &entity) { - // Temporary until all dependencies are available as component - Being &being = static_cast<Being &>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); - if (being.getAction() != DEAD) + if (beingComponent->getAction() != DEAD) { - LOG_WARN("Character \"" << being.getName() + LOG_WARN("Character \"" << beingComponent->getName() << "\" tried to respawn without being dead"); return; } // Make it alive again - being.setAction(STAND); + beingComponent->setAction(entity, STAND); // Reset target entity.getComponent<CombatComponent>()->clearTarget(); @@ -219,13 +218,14 @@ void CharacterComponent::respawn(Entity &entity) return; // No script respawn callback set - fall back to hardcoded logic - being.setAttribute(ATTR_HP, being.getModifiedAttribute(ATTR_MAX_HP)); + const double maxHp = beingComponent->getModifiedAttribute(ATTR_MAX_HP); + beingComponent->setAttribute(entity, ATTR_HP, maxHp); // Warp back to spawn point. int spawnMap = Configuration::getValue("char_respawnMap", 1); int spawnX = Configuration::getValue("char_respawnX", 1024); int spawnY = Configuration::getValue("char_respawnY", 1024); - GameState::enqueueWarp(&being, MapManager::getMap(spawnMap), + GameState::enqueueWarp(&entity, MapManager::getMap(spawnMap), Point(spawnX, spawnY)); } @@ -259,7 +259,7 @@ bool CharacterComponent::specialUseCheck(SpecialMap::iterator it) return true; } -void CharacterComponent::useSpecialOnBeing(Entity &user, int id, Being *b) +void CharacterComponent::useSpecialOnBeing(Entity &user, int id, Entity *b) { SpecialMap::iterator it = mSpecials.find(id); if (!specialUseCheck(it)) @@ -419,17 +419,15 @@ void CharacterComponent::setBuySell(BuySell *t) void CharacterComponent::sendStatus(Entity &entity) { - // Temporary until all dependencies are available as components - Being &being = static_cast<Being &>(entity); - + auto *beingComponent = entity.getComponent<BeingComponent>(); MessageOut attribMsg(GPMSG_PLAYER_ATTRIBUTE_CHANGE); for (std::set<size_t>::const_iterator i = mModifiedAttributes.begin(), i_end = mModifiedAttributes.end(); i != i_end; ++i) { int attr = *i; attribMsg.writeInt16(attr); - attribMsg.writeInt32(being.getAttributeBase(attr) * 256); - attribMsg.writeInt32(being.getModifiedAttribute(attr) * 256); + attribMsg.writeInt32(beingComponent->getAttributeBase(attr) * 256); + attribMsg.writeInt32(beingComponent->getModifiedAttribute(attr) * 256); } if (attribMsg.getLength() > 2) gameHandler->sendTo(mClient, attribMsg); mModifiedAttributes.clear(); @@ -458,23 +456,24 @@ void CharacterComponent::sendStatus(Entity &entity) void CharacterComponent::modifiedAllAttributes(Entity &entity) { - // Temporary until all dependencies are available as components - Being &being = static_cast<Being &>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); LOG_DEBUG("Marking all attributes as changed, requiring recalculation."); - for (auto attribute : being.getAttributes()) + for (auto attribute : beingComponent->getAttributes()) { - being.recalculateBaseAttribute(attribute.first); + beingComponent->recalculateBaseAttribute(entity, attribute.first); mModifiedAttributes.insert(attribute.first); } } -void CharacterComponent::attributeChanged(Being *being, unsigned attr) +void CharacterComponent::attributeChanged(Entity *entity, unsigned attr) { + auto *beingComponent = entity->getComponent<BeingComponent>(); + // Inform the client of this attribute modification. accountHandler->updateAttributes(getDatabaseID(), attr, - being->getAttributeBase(attr), - being->getModifiedAttribute(attr)); + beingComponent->getAttributeBase(attr), + beingComponent->getModifiedAttribute(attr)); mModifiedAttributes.insert(attr); // Update the knuckle Attack if required @@ -482,7 +481,7 @@ void CharacterComponent::attributeChanged(Being *being, unsigned attr) { // TODO: dehardcode this Damage &knuckleDamage = mKnuckleAttackInfo->getDamage(); - knuckleDamage.base = being->getModifiedAttribute(ATTR_STR); + knuckleDamage.base = beingComponent->getModifiedAttribute(ATTR_STR); knuckleDamage.delta = knuckleDamage.base / 2; } } @@ -617,9 +616,6 @@ int CharacterComponent::getExpGot(size_t skill) const void CharacterComponent::levelup(Entity &entity) { - // Temporary until all dependencies are available as Component - Being &being = static_cast<Being &>(entity); - mLevel++; mCharacterPoints += CHARPOINTS_PER_LEVELUP; @@ -632,14 +628,14 @@ void CharacterComponent::levelup(Entity &entity) levelupMsg.writeInt16(mCharacterPoints); levelupMsg.writeInt16(mCorrectionPoints); gameHandler->sendTo(mClient, levelupMsg); - LOG_INFO(being.getName() << " reached level " << mLevel); + LOG_INFO(entity.getComponent<BeingComponent>()->getName() + << " reached level " << mLevel); } AttribmodResponseCode CharacterComponent::useCharacterPoint(Entity &entity, size_t attribute) { - // Temporary until all dependencies are available as Component - Being &being = static_cast<Being &>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); if (!attributeManager->isAttributeDirectlyModifiable(attribute)) return ATTRIBMOD_INVALID_ATTRIBUTE; @@ -647,27 +643,30 @@ AttribmodResponseCode CharacterComponent::useCharacterPoint(Entity &entity, return ATTRIBMOD_NO_POINTS_LEFT; --mCharacterPoints; - being.setAttribute(attribute, being.getAttributeBase(attribute) + 1); - being.updateDerivedAttributes(attribute); + + const double base = beingComponent->getAttributeBase(attribute); + beingComponent->setAttribute(entity, attribute, base + 1); + beingComponent->updateDerivedAttributes(entity, attribute); return ATTRIBMOD_OK; } AttribmodResponseCode CharacterComponent::useCorrectionPoint(Entity &entity, size_t attribute) { - // Temporary until all dependencies are available as Component - Being &being = static_cast<Being &>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); if (!attributeManager->isAttributeDirectlyModifiable(attribute)) return ATTRIBMOD_INVALID_ATTRIBUTE; if (!mCorrectionPoints) return ATTRIBMOD_NO_POINTS_LEFT; - if (being.getAttributeBase(attribute) <= 1) + if (beingComponent->getAttributeBase(attribute) <= 1) return ATTRIBMOD_DENIED; --mCorrectionPoints; ++mCharacterPoints; - being.setAttribute(attribute, being.getAttributeBase(attribute) - 1); + + const double base = beingComponent->getAttributeBase(attribute); + beingComponent->setAttribute(entity, attribute, base - 1); return ATTRIBMOD_OK; } @@ -718,13 +717,10 @@ void CharacterComponent::attackRemoved(CombatComponent *combatComponent, void CharacterComponent::disconnected(Entity &entity) { - // Temporary until all dependencies are available as a Component - Being &being = static_cast<Being &>(entity); - mConnected = false; // Make the dead characters respawn, even in case of disconnection. - if (being.getAction() == DEAD) + if (entity.getComponent<BeingComponent>()->getAction() == DEAD) respawn(entity); else GameState::remove(&entity); diff --git a/src/game-server/character.h b/src/game-server/character.h index 4b5089d6..74d08d30 100644 --- a/src/game-server/character.h +++ b/src/game-server/character.h @@ -69,7 +69,7 @@ typedef std::map<unsigned, SpecialValue> SpecialMap; class CharacterData { public: - CharacterData(Being *being, CharacterComponent *characterComponent); + CharacterData(Actor *actor, CharacterComponent *characterComponent); void setGender(BeingGender); BeingGender getGender() const; @@ -123,7 +123,7 @@ public: Possessions &getPossessions() const; private: - Being *mBeing; + Actor *mActor; CharacterComponent *mCharacterComponent; }; @@ -154,7 +154,7 @@ class CharacterComponent : public Component /** * Executes the global die script */ - virtual void characterDied(Being *); + virtual void characterDied(Entity *); /** * makes the character respawn @@ -165,7 +165,7 @@ class CharacterComponent : public Component * makes the character perform a special action on a being * when it is allowed to do so */ - void useSpecialOnBeing(Entity &user, int id, Being *b); + void useSpecialOnBeing(Entity &user, int id, Entity *b); /** * makes the character perform a special action on a map point @@ -312,7 +312,7 @@ class CharacterComponent : public Component * @param being th being of which the attribute was changed * @param attributeId the changed id */ - void attributeChanged(Being *being, unsigned attributeId); + void attributeChanged(Entity *being, unsigned attributeId); /** * Calls all the "disconnected" listener. @@ -578,55 +578,55 @@ class CharacterComponent : public Component }; -inline CharacterData::CharacterData(Being *being, +inline CharacterData::CharacterData(Actor *actor, CharacterComponent *characterComponent): - mBeing(being), + mActor(actor), mCharacterComponent(characterComponent) {} inline void CharacterData::setGender(BeingGender gender) { - mBeing->setGender(gender); + mActor->getComponent<BeingComponent>()->setGender(gender); } inline BeingGender CharacterData::getGender() const { - return mBeing->getGender(); + return mActor->getComponent<BeingComponent>()->getGender(); } inline void CharacterData::setMapId(int id) { - mBeing->setMap(MapManager::getMap(id)); + mActor->setMap(MapManager::getMap(id)); } inline int CharacterData::getMapId() const { - return mBeing->getMap()->getID(); + return mActor->getMap()->getID(); } inline void CharacterData::setPosition(Point &point) { - mBeing->setPosition(point); + mActor->setPosition(point); } inline const Point &CharacterData::getPosition() const { - return mBeing->getPosition(); + return mActor->getPosition(); } inline void CharacterData::setAttribute(int id, int base) { - mBeing->setAttribute(id, base); + mActor->getComponent<BeingComponent>()->setAttribute(*mActor, id, base); } inline void CharacterData::setModAttribute(int id, int mod) { - mBeing->setModAttribute(id, mod); + mActor->getComponent<BeingComponent>()->setModAttribute(id, mod); } inline const AttributeMap &CharacterData::getAttributes() const { - return mBeing->getAttributes(); + return mActor->getComponent<BeingComponent>()->getAttributes(); } inline void CharacterData::setCharacterPoints(int characterPoints) @@ -711,22 +711,22 @@ inline const std::map<int, int>::const_iterator CharacterData::getSkillEnd() con inline void CharacterData::applyStatusEffect(int status, int time) { - mBeing->applyStatusEffect(status, time); + mActor->getComponent<BeingComponent>()->applyStatusEffect(status, time); } inline int CharacterData::getStatusEffectSize() const { - return mBeing->getStatusEffects().size(); + return mActor->getComponent<BeingComponent>()->getStatusEffects().size(); } inline const std::map<int, Status>::const_iterator CharacterData::getStatusEffectBegin() const { - return mBeing->getStatusEffects().begin(); + return mActor->getComponent<BeingComponent>()->getStatusEffects().begin(); } inline const std::map<int, Status>::const_iterator CharacterData::getStatusEffectEnd() const { - return mBeing->getStatusEffects().end(); + return mActor->getComponent<BeingComponent>()->getStatusEffects().end(); } inline int CharacterData::getKillCountSize() const diff --git a/src/game-server/combatcomponent.cpp b/src/game-server/combatcomponent.cpp index d71f86a3..df2d034d 100644 --- a/src/game-server/combatcomponent.cpp +++ b/src/game-server/combatcomponent.cpp @@ -25,12 +25,12 @@ #include "utils/logger.h" -CombatComponent::CombatComponent(Being &being): - mTarget(0), - mCurrentAttack(0) +CombatComponent::CombatComponent(Entity &being): + mTarget(nullptr), + mCurrentAttack(nullptr) { - being.signal_died.connect(sigc::mem_fun(this, - &CombatComponent::diedOrRemoved)); + being.getComponent<BeingComponent>()->signal_died.connect(sigc::mem_fun( + this, &CombatComponent::diedOrRemoved)); being.signal_removed.connect(sigc::mem_fun(this, &CombatComponent::diedOrRemoved)); } @@ -44,10 +44,11 @@ void CombatComponent::update(Entity &entity) // Temporary for as long as Being is not split into Components // Prevents to implement all at once // TODO: remove this as soon as possible - Being &being = static_cast<Being&>(entity); + Actor &actor = static_cast<Actor&>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); - if (being.getAction() != ATTACK || !mTarget) + if (beingComponent->getAction() != ATTACK || !mTarget) return; std::vector<Attack *> attacksReady; @@ -55,7 +56,7 @@ void CombatComponent::update(Entity &entity) if (Attack *triggerableAttack = mAttacks.getTriggerableAttack()) { - processAttack(being, *triggerableAttack); + processAttack(entity, *triggerableAttack); mAttacks.markAttackAsTriggered(); } @@ -69,11 +70,11 @@ void CombatComponent::update(Entity &entity) it_end = attacksReady.end(); it != it_end; ++it) { // check if target is in range using the pythagorean theorem - int distx = being.getPosition().x - mTarget->getPosition().x; - int disty = being.getPosition().y - mTarget->getPosition().y; + int distx = actor.getPosition().x - mTarget->getPosition().x; + int disty = actor.getPosition().y - mTarget->getPosition().y; int distSquare = (distx * distx + disty * disty); AttackInfo *info = (*it)->getAttackInfo(); - int maxDist = info->getDamage().range + being.getSize(); + int maxDist = info->getDamage().range + actor.getSize(); if (distSquare <= maxDist * maxDist && (!highestPriorityAttack || @@ -87,9 +88,9 @@ void CombatComponent::update(Entity &entity) { mAttacks.startAttack(highestPriorityAttack); mCurrentAttack = highestPriorityAttack; - being.setDestination(being.getPosition()); + beingComponent->setDestination(entity, actor.getPosition()); // TODO: Turn into direction of enemy - being.raiseUpdateFlags(UPDATEFLAG_ATTACK); + actor.raiseUpdateFlags(UPDATEFLAG_ATTACK); } } @@ -98,8 +99,12 @@ void CombatComponent::update(Entity &entity) * stats, deducts the result from the hitpoints and adds the result to * the HitsTaken list. */ -int CombatComponent::damage(Being &target, Being *source, const Damage &damage) +int CombatComponent::damage(Entity &target, + Entity *source, + const Damage &damage) { + auto *beingComponent = target.getComponent<BeingComponent>(); + int HPloss = damage.base; if (damage.delta) HPloss += rand() * (damage.delta + 1) / RAND_MAX; @@ -108,8 +113,10 @@ int CombatComponent::damage(Being &target, Being *source, const Damage &damage) switch (damage.type) { case DAMAGE_PHYSICAL: - if (!damage.trueStrike && - rand()%((int) target.getModifiedAttribute(ATTR_DODGE) + 1) > + { + const double dodge = + beingComponent->getModifiedAttribute(ATTR_DODGE); + if (!damage.trueStrike && rand()%((int)dodge + 1) > rand()%(damage.cth + 1)) { HPloss = 0; @@ -118,18 +125,19 @@ int CombatComponent::damage(Being &target, Being *source, const Damage &damage) } else { - HPloss = HPloss * (1.0 - (0.0159375f * - target.getModifiedAttribute(ATTR_DEFENSE)) / - (1.0 + 0.017 * - target.getModifiedAttribute(ATTR_DEFENSE))) + - (rand()%((HPloss >> 4) + 1)); + const double defense = + beingComponent->getModifiedAttribute(ATTR_DEFENSE); + HPloss = HPloss * (1.0 - (0.0159375f * defense) / + (1.0 + 0.017 * defense)) + + (rand()%((HPloss / 16) + 1)); // TODO Process triggers for receiving damage here. // If there is an attacker included, also process triggers for the attacker (successful physical strike) } break; + } case DAMAGE_MAGICAL: #if 0 - target.getModifiedAttribute(BASE_ELEM_BEGIN + damage.element); + beingComponent.getModifiedAttribute(BASE_ELEM_BEGIN + damage.element); #else LOG_WARN("Attempt to use magical type damage! This has not been" "implemented yet and should not be used!"); @@ -146,12 +154,13 @@ int CombatComponent::damage(Being &target, Being *source, const Damage &damage) if (HPloss > 0) { mHitsTaken.push_back(HPloss); - const Attribute *HP = target.getAttribute(ATTR_HP); - LOG_DEBUG("Being " << target.getPublicID() << " suffered " << HPloss + const Attribute *HP = beingComponent->getAttribute(ATTR_HP); + LOG_DEBUG("Being " << static_cast<Actor &>(target).getPublicID() + << " suffered " << HPloss << " damage. HP: " << HP->getModifiedAttribute() << "/" - << target.getModifiedAttribute(ATTR_MAX_HP)); - target.setAttribute(ATTR_HP, HP->getBase() - HPloss); + << beingComponent->getModifiedAttribute(ATTR_MAX_HP)); + beingComponent->setAttribute(target, ATTR_HP, HP->getBase() - HPloss); // No HP regen after being hit if this is set. // TODO: Reenable this once the attributes are available as a component // A bit too fuzzy to implement at the moment @@ -170,7 +179,7 @@ int CombatComponent::damage(Being &target, Being *source, const Damage &damage) /** * Performs an attack */ -void CombatComponent::processAttack(Being &source, Attack &attack) +void CombatComponent::processAttack(Entity &source, Attack &attack) { performAttack(source, attack.getAttackInfo()->getDamage()); } @@ -194,12 +203,12 @@ void CombatComponent::removeAttack(AttackInfo *attackInfo) /** * Performs an attack. */ -int CombatComponent::performAttack(Being &source, const Damage &dmg) +int CombatComponent::performAttack(Entity &source, const Damage &dmg) { // check target legality if (!mTarget || mTarget == &source - || mTarget->getAction() == DEAD + || mTarget->getComponent<BeingComponent>()->getAction() == DEAD || !mTarget->canFight()) return -1; diff --git a/src/game-server/combatcomponent.h b/src/game-server/combatcomponent.h index 033b543d..393eb612 100644 --- a/src/game-server/combatcomponent.h +++ b/src/game-server/combatcomponent.h @@ -29,8 +29,8 @@ #include "game-server/attack.h" +class Entity; class Actor; -class Being; /** * Type definition for a list of hits @@ -42,7 +42,7 @@ class CombatComponent: public Component, public sigc::trackable public: static const ComponentType type = CT_Fighting; - CombatComponent(Being &being); + CombatComponent(Entity &being); virtual ~CombatComponent(); void update(Entity &entity); @@ -54,23 +54,23 @@ public: const Hits &getHitsTaken() const; void clearHitsTaken(); - int performAttack(Being &source, const Damage &dmg); - virtual int damage(Being &target, Being *source, const Damage &damage); + int performAttack(Entity &source, const Damage &dmg); + virtual int damage(Entity &target, Entity *source, const Damage &damage); int getAttackId() const; - Being *getTarget() const; - void setTarget(Being *target); + Actor *getTarget() const; + void setTarget(Actor *target); void clearTarget(); void diedOrRemoved(Entity *entity); - sigc::signal<void, Being *, const Damage &, int> signal_damaged; + sigc::signal<void, Entity *, const Damage &, int> signal_damaged; protected: - virtual void processAttack(Being &source, Attack &attack); + virtual void processAttack(Entity &source, Attack &attack); - Being *mTarget; + Actor *mTarget; Attacks mAttacks; Attack *mCurrentAttack; // Last used attack Hits mHitsTaken; //List of punches taken since last update. @@ -111,7 +111,7 @@ inline int CombatComponent::getAttackId() const /** * Get Target */ -inline Being *CombatComponent::getTarget() const +inline Actor *CombatComponent::getTarget() const { return mTarget; } @@ -119,7 +119,7 @@ inline Being *CombatComponent::getTarget() const /** * Set Target */ -inline void CombatComponent::setTarget(Being *target) +inline void CombatComponent::setTarget(Actor *target) { mTarget = target; } @@ -129,7 +129,7 @@ inline void CombatComponent::setTarget(Being *target) */ inline void CombatComponent::clearTarget() { - mTarget = 0; + mTarget = nullptr; } /** diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp index 63b960d3..8a8e1380 100644 --- a/src/game-server/commandhandler.cpp +++ b/src/game-server/commandhandler.cpp @@ -48,44 +48,44 @@ struct CmdRef const char *cmd; const char *usage; const char *help; - void (*func)(Being*, std::string&) ; + void (*func)(Entity*, std::string&) ; }; -static void handleHelp(Being*, std::string&); -static void handleReport(Being*, std::string&); -static void handleWhere(Being*, std::string&); -static void handleRights(Being*, std::string&); -static void handleWarp(Being*, std::string&); -static void handleCharWarp(Being*, std::string&); -static void handleGoto(Being*, std::string&); -static void handleRecall(Being*, std::string&); -static void handleBan(Being*, std::string&); -static void handleItem(Being*, std::string&); -static void handleDrop(Being*, std::string&); -static void handleMoney(Being*, std::string&); -static void handleSpawn(Being*, std::string&); -static void handleAttribute(Being*, std::string&); -static void handleReload(Being*, std::string&); -static void handlePermissions(Being*, std::string&); -static void handleGivePermission(Being*, std::string&); -static void handleTakePermission(Being*, std::string&); -static void handleAnnounce(Being*, std::string&); -static void handleHistory(Being*, std::string&); -static void handleMute(Being*, std::string&); -static void handleDie(Being*, std::string&); -static void handleKill(Being*, std::string&); -static void handleKick(Being*, std::string&); -static void handleLog(Being*, std::string&); -static void handleLogsay(Being*, std::string&); -static void handleKillMonsters(Being*, std::string&); -static void handleCraft(Being*, std::string&); -static void handleGetPos(Being*, std::string&); -static void handleSkills(Being*, std::string&); -static void handleEffect(Being*, std::string&); -static void handleGiveSpecial(Being*, std::string&); -static void handleTakeSpecial(Being*, std::string&); -static void handleRechargeSpecial(Being*, std::string&); -static void handleListSpecials(Being*, std::string&); +static void handleHelp(Entity*, std::string&); +static void handleReport(Entity*, std::string&); +static void handleWhere(Entity*, std::string&); +static void handleRights(Entity*, std::string&); +static void handleWarp(Entity*, std::string&); +static void handleCharWarp(Entity*, std::string&); +static void handleGoto(Entity*, std::string&); +static void handleRecall(Entity*, std::string&); +static void handleBan(Entity*, std::string&); +static void handleItem(Entity*, std::string&); +static void handleDrop(Entity*, std::string&); +static void handleMoney(Entity*, std::string&); +static void handleSpawn(Entity*, std::string&); +static void handleAttribute(Entity*, std::string&); +static void handleReload(Entity*, std::string&); +static void handlePermissions(Entity*, std::string&); +static void handleGivePermission(Entity*, std::string&); +static void handleTakePermission(Entity*, std::string&); +static void handleAnnounce(Entity*, std::string&); +static void handleHistory(Entity*, std::string&); +static void handleMute(Entity*, std::string&); +static void handleDie(Entity*, std::string&); +static void handleKill(Entity*, std::string&); +static void handleKick(Entity*, std::string&); +static void handleLog(Entity*, std::string&); +static void handleLogsay(Entity*, std::string&); +static void handleKillMonsters(Entity*, std::string&); +static void handleCraft(Entity*, std::string&); +static void handleGetPos(Entity*, std::string&); +static void handleSkills(Entity*, std::string&); +static void handleEffect(Entity*, std::string&); +static void handleGiveSpecial(Entity*, std::string&); +static void handleTakeSpecial(Entity*, std::string&); +static void handleRechargeSpecial(Entity*, std::string&); +static void handleListSpecials(Entity*, std::string&); static CmdRef const cmdRef[] = { @@ -171,9 +171,9 @@ static CmdRef const cmdRef[] = }; -static void say(const std::string &message, Being *player) +static void say(const std::string &message, Entity *player) { - GameState::sayTo(player, NULL, message); + GameState::sayTo(static_cast<Actor*>(player), nullptr, message); } /* @@ -193,7 +193,7 @@ static bool checkPermission(Character *player, unsigned permissions) * Returns the next argument, and remove it from the given string. */ -static std::string playerRights(Being *ch) +static std::string playerRights(Entity *ch) { std::stringstream str; str << (unsigned)ch->getComponent<CharacterComponent>()->getAccountLevel(); @@ -263,7 +263,7 @@ static std::string getArgument(std::string &args) return argument; } -static void handleHelp(Being *player, std::string &args) +static void handleHelp(Entity *player, std::string &args) { if (args.empty()) { @@ -302,7 +302,7 @@ static void handleHelp(Being *player, std::string &args) } } -static void handleWarp(Being *player, std::string &args) +static void handleWarp(Entity *player, std::string &args) { int x, y; MapComposite *map; @@ -388,11 +388,11 @@ static void handleWarp(Being *player, std::string &args) ss.str()); } -static void handleCharWarp(Being *player, std::string &args) +static void handleCharWarp(Entity *player, std::string &args) { int x, y; MapComposite *map; - Being *other; + Entity *other; // get the arguments std::string character = getArgument(args); @@ -485,16 +485,16 @@ static void handleCharWarp(Being *player, std::string &args) // log transaction std::stringstream ss; - ss << "User warped " << other->getName() << " to " << map->getName() << - " (" << x << ", " << y << ")"; + ss << "User warped " << other->getComponent<BeingComponent>()->getName() + << " to " << map->getName() << " (" << x << ", " << y << ")"; int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_WARP, ss.str()); } -static void handleItem(Being *player, std::string &args) +static void handleItem(Entity *player, std::string &args) { - Being *other; + Entity *other; ItemClass *ic; int value = 0; @@ -570,7 +570,7 @@ static void handleItem(Being *player, std::string &args) accountHandler->sendTransaction(databaseId, TRANS_CMD_ITEM, str.str()); } -static void handleDrop(Being *player, std::string &args) +static void handleDrop(Entity *player, std::string &args) { ItemClass *ic; int amount = 0; @@ -620,7 +620,7 @@ static void handleDrop(Being *player, std::string &args) } Entity *item = Item::create(player->getMap(), - player->getPosition(), + static_cast<Actor*>(player)->getPosition(), ic, amount); GameState::insertOrDelete(item); @@ -633,9 +633,9 @@ static void handleDrop(Being *player, std::string &args) accountHandler->sendTransaction(databaseId, TRANS_CMD_DROP, str.str()); } -static void handleMoney(Being *player, std::string &args) +static void handleMoney(Entity *player, std::string &args) { - Being *other; + Entity *other; int value; // get arguments @@ -676,8 +676,11 @@ static void handleMoney(Being *player, std::string &args) // change value into an integer value = utils::stringToInt(valuestr); + auto *beingComponent = other->getComponent<BeingComponent>(); + // change how much money the player has - other->setAttribute(ATTR_GP , other->getAttributeBase(ATTR_GP) + value); + const double previousMoney = beingComponent->getAttributeBase(ATTR_GP); + beingComponent->setAttribute(*player, ATTR_GP , previousMoney + value); // log transaction std::string msg = "User created " + valuestr + " money"; @@ -686,11 +689,11 @@ static void handleMoney(Being *player, std::string &args) accountHandler->sendTransaction(databaseId, TRANS_CMD_MONEY, msg); } -static void handleSpawn(Being *player, std::string &args) +static void handleSpawn(Entity *player, std::string &args) { MonsterClass *mc; MapComposite *map = player->getMap(); - const Point &pos = player->getPosition(); + const Point &pos = static_cast<Actor*>(player)->getPosition(); int value = 0; // get the arguments @@ -741,11 +744,11 @@ static void handleSpawn(Being *player, std::string &args) // create the monsters and put them on the map for (int i = 0; i < value; ++i) { - Being *monster = new Being(OBJECT_MONSTER); + Actor *monster = new Actor(OBJECT_MONSTER); + monster->setPosition(pos); + monster->addComponent(new BeingComponent(*monster)); monster->addComponent(new MonsterComponent(*monster, mc)); monster->setMap(map); - monster->setPosition(pos); - monster->clearDestination(); if (!GameState::insertOrDelete(monster)) { // The map is full. Break out. @@ -753,16 +756,17 @@ static void handleSpawn(Being *player, std::string &args) } // log transaction - std::string msg = "User created monster " + monster->getName(); + std::string msg = "User created monster " + + monster->getComponent<BeingComponent>()->getName(); int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_SPAWN, msg); } } -static void handleGoto(Being *player, std::string &args) +static void handleGoto(Entity *player, std::string &args) { - Being *other; + Entity *other; // get the arguments std::string character = getArgument(args); @@ -785,20 +789,21 @@ static void handleGoto(Being *player, std::string &args) // move the player to where the other player is MapComposite *map = other->getMap(); - const Point &pos = other->getPosition(); + const Point &pos = static_cast<Actor*>(other)->getPosition(); GameState::warp(player, map, pos); // log transaction std::stringstream msg; - msg << "User warped own character to " << other->getName(); + msg << "User warped own character to " + << other->getComponent<BeingComponent>()->getName(); int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_GOTO, msg.str()); } -static void handleRecall(Being *player, std::string &args) +static void handleRecall(Entity *player, std::string &args) { - Being *other; + Entity *other; // get the arguments std::string character = getArgument(args); @@ -821,20 +826,20 @@ static void handleRecall(Being *player, std::string &args) // move the other player to where the player is MapComposite *map = player->getMap(); - const Point &pos = player->getPosition(); + const Point &pos = static_cast<Actor*>(player)->getPosition(); GameState::warp(other, map, pos); } -static void handleReload(Being *, std::string &) +static void handleReload(Entity *, std::string &) { // reload the items and monsters itemManager->reload(); monsterManager->reload(); } -static void handleBan(Being *player, std::string &args) +static void handleBan(Entity *player, std::string &args) { - Being *other; + Entity *other; int length; int lengthMutiplier = 0; @@ -900,15 +905,16 @@ static void handleBan(Being *player, std::string &args) characterComponent->getClient()->disconnect(kickmsg); // feedback for command user - std::string msg = "You've banned " + other->getName() + " for " + utils::toString(length) + " minutes"; + std::string otherName = other->getComponent<BeingComponent>()->getName(); + std::string msg = "You've banned " + otherName + " for " + utils::toString(length) + " minutes"; say(msg.c_str(), player); // log transaction - msg = "User banned " + other->getName() + " for " + utils::toString(length) + " minutes"; + msg = "User banned " + otherName + " for " + utils::toString(length) + " minutes"; accountHandler->sendTransaction(characterComponent->getDatabaseID(), TRANS_CMD_BAN, msg); } -static void handlePermissions(Being *player, std::string &args) +static void handlePermissions(Entity *player, std::string &args) { std::string character = getArgument(args); if (character.empty()) @@ -918,20 +924,20 @@ static void handlePermissions(Being *player, std::string &args) return; } - Being *other = gameHandler->getCharacterByNameSlow(character); + Entity *other = gameHandler->getCharacterByNameSlow(character); if (!other) { say("Invalid character", player); return; } - say(other->getName() + " has the permissions: " + + say(other->getComponent<BeingComponent>()->getName() + " has the permissions: " + playerRights(other), player); } -static void handleGivePermission(Being *player, std::string &args) +static void handleGivePermission(Entity *player, std::string &args) { - Being *other; + Entity *other; // get the arguments std::string character = getArgument(args); @@ -974,7 +980,8 @@ static void handleGivePermission(Being *player, std::string &args) if (permission & characterComponent->getAccountLevel()) { - say(player->getName()+" already has the permission "+strPermission, player); + say(player->getComponent<BeingComponent>()->getName() + +" already has the permission "+strPermission, player); } else { @@ -984,19 +991,21 @@ static void handleGivePermission(Being *player, std::string &args) accountHandler->changeAccountLevel(other, permission); // log transaction - std::string msg = "User gave right " + strPermission + " to " + other->getName(); + std::string msg = "User gave right " + strPermission + " to " + + other->getComponent<BeingComponent>()->getName(); accountHandler->sendTransaction(characterComponent->getDatabaseID(), TRANS_CMD_SETGROUP, msg); - say("You gave " + other->getName() + + say("You gave " + other->getComponent<BeingComponent>()->getName() + " the rights of a " + strPermission, player); - say("Congratulations, " + player->getName() + + say("Congratulations, " + + player->getComponent<BeingComponent>()->getName() + " gave you the rights of a " + strPermission, other); } } -static void handleTakePermission(Being *player, std::string &args) +static void handleTakePermission(Entity *player, std::string &args) { - Being *other; + Entity *other; // get the arguments std::string character = getArgument(args); @@ -1039,7 +1048,8 @@ static void handleTakePermission(Being *player, std::string &args) if (!(permission & characterComponent->getAccountLevel())) { - say(player->getName()+" hasn't got the permission "+strPermission, player); + say(player->getComponent<BeingComponent>()->getName() + +" hasn't got the permission "+strPermission, player); } else { permission = characterComponent->getAccountLevel() - permission; // change the player's account level @@ -1047,17 +1057,19 @@ static void handleTakePermission(Being *player, std::string &args) accountHandler->changeAccountLevel(other, permission); // log transaction - std::string msg = "User took right " + strPermission + " from " + other->getName(); + std::string msg = "User took right " + strPermission + " from " + + other->getComponent<BeingComponent>()->getName(); accountHandler->sendTransaction(characterComponent->getDatabaseID(), TRANS_CMD_SETGROUP, msg); - say("Sorry, "+player->getName()+" revoked your rights of a "+strPermission, other); + say("Sorry, "+player->getComponent<BeingComponent>()->getName() + +" revoked your rights of a "+strPermission, other); } } -static void handleAttribute(Being *player, std::string &args) +static void handleAttribute(Entity *player, std::string &args) { - Being *other; + Entity *other; int attr, value; // get arguments @@ -1114,19 +1126,22 @@ static void handleAttribute(Being *player, std::string &args) return; } + auto *beingComponent = other->getComponent<BeingComponent>(); + // change the player's attribute - other->setAttribute(attr, value); + beingComponent->setAttribute(*other, attr, value); // log transaction std::stringstream msg; - msg << "User changed attribute " << attr << " of player " << other->getName() + msg << "User changed attribute " << attr << " of player " + << beingComponent->getName() << " to " << value; int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_ATTRIBUTE, msg.str()); } -static void handleReport(Being *player, std::string &args) +static void handleReport(Entity *player, std::string &args) { std::string bugReport = getArgument(args); @@ -1140,7 +1155,7 @@ static void handleReport(Being *player, std::string &args) // TODO: Send the report to a developer or something } -static void handleAnnounce(Being *player, std::string &args) +static void handleAnnounce(Entity *player, std::string &args) { if (args.empty()) { @@ -1153,36 +1168,37 @@ static void handleAnnounce(Being *player, std::string &args) msg.writeString(args); msg.writeInt16(player->getComponent<CharacterComponent>() ->getDatabaseID()); - msg.writeString(player->getName()); + msg.writeString(player->getComponent<BeingComponent>()->getName()); accountHandler->send(msg); } -static void handleWhere(Being *player, std::string &) +static void handleWhere(Entity *player, std::string &) { + Actor *actor = static_cast<Actor*>(player); std::stringstream str; str << "Your current location is map " << player->getMap()->getID() << " [" - << player->getPosition().x + << actor->getPosition().x << ":" - << player->getPosition().y + << actor->getPosition().y << "]"; say (str.str(), player); } -static void handleRights(Being *player, std::string &) +static void handleRights(Entity *player, std::string &) { say("Your rights level is: " + playerRights(player), player); } -static void handleHistory(Being *, std::string &) +static void handleHistory(Entity *, std::string &) { // TODO: Get args number of transactions and show them to the player } -static void handleMute(Being *player, std::string &args) +static void handleMute(Entity *player, std::string &args) { - Being *other; + Entity *other; int length; // Get arguments. @@ -1213,21 +1229,26 @@ static void handleMute(Being *player, std::string &args) // Mute the player. other->getComponent<CharacterComponent>()->mute(length); + const std::string &playerName = + player->getComponent<BeingComponent>()->getName(); + const std::string &otherName = + other->getComponent<BeingComponent>()->getName(); + // Feedback. std::stringstream targetMsg; std::stringstream userMsg; if (length > 0) { - targetMsg << player->getName() << " muted you for " + targetMsg << playerName << " muted you for " << length << " seconds."; - userMsg << "You muted " << other->getName() + userMsg << "You muted " << otherName << " for " << length << " seconds."; } else { - targetMsg << player->getName() << " unmuted you."; - userMsg << "You unmuted " << other->getName() << "."; + targetMsg << playerName << " unmuted you."; + userMsg << "You unmuted " << otherName << "."; } say(targetMsg.str(), other); say(userMsg.str(), player); @@ -1236,24 +1257,24 @@ static void handleMute(Being *player, std::string &args) std::stringstream msg; if (length > 0) { - msg << "User muted " << other->getName() << " for " << length << " seconds."; + msg << "User muted " << otherName << " for " << length << " seconds."; } else { - msg << "User unmuted " << other->getName(); + msg << "User unmuted " << otherName; } int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_MUTE, msg.str()); } -static void handleDie(Being *player, std::string &) +static void handleDie(Entity *player, std::string &) { - player->setAttribute(ATTR_HP, 0); + player->getComponent<BeingComponent>()->setAttribute(*player, ATTR_HP, 0); say("You've killed yourself.", player); } -static void handleKill(Being *player, std::string &args) +static void handleKill(Entity *player, std::string &args) { - Being *other; + Entity *other; // get arguments std::string character = getArgument(args); @@ -1267,27 +1288,30 @@ static void handleKill(Being *player, std::string &args) } // kill the player - other->setAttribute(ATTR_HP, 0); + other->getComponent<BeingComponent>()->setAttribute(*player, ATTR_HP, 0); // feedback std::stringstream targetMsg; std::stringstream userMsg; - targetMsg << "You were killed by server command from "<< player->getName() << "."; - userMsg << "You killed " << other->getName() << "."; + targetMsg << "You were killed by server command from " + << player->getComponent<BeingComponent>()->getName() << "."; + userMsg << "You killed " + << other->getComponent<BeingComponent>()->getName() << "."; say(targetMsg.str(), other); say(userMsg.str(), player); // log transaction std::stringstream logMsg; - logMsg << "User killed " << other->getName(); + logMsg << "User killed " + << other->getComponent<BeingComponent>()->getName(); int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_KILL, logMsg.str()); } -static void handleKick(Being *player, std::string &args) +static void handleKick(Entity *player, std::string &args) { - Being *other; + Entity *other; // get arguments std::string character = getArgument(args); @@ -1302,7 +1326,8 @@ static void handleKick(Being *player, std::string &args) // send feedback std::stringstream userMsg; - userMsg << "You kicked " << other->getName() << "."; + userMsg << "You kicked " + << other->getComponent<BeingComponent>()->getName() << "."; say(userMsg.str(), player); @@ -1316,14 +1341,15 @@ static void handleKick(Being *player, std::string &args) // log transaction std::stringstream logMsg; - logMsg << "User kicked " << other->getName(); + logMsg << "User kicked " + << other->getComponent<BeingComponent>()->getName(); int databaseId = player->getComponent<CharacterComponent>()->getDatabaseID(); accountHandler->sendTransaction(databaseId, TRANS_CMD_KICK, logMsg.str()); } -static void handleLog(Being *player, std::string &msg) +static void handleLog(Entity *player, std::string &msg) { if (msg.empty()) { @@ -1342,7 +1368,7 @@ static void handleLog(Being *player, std::string &msg) say("Message logged", player); } -static void handleLogsay(Being *player, std::string &msg) +static void handleLogsay(Entity *player, std::string &msg) { if (msg.empty()) { @@ -1351,7 +1377,7 @@ static void handleLogsay(Being *player, std::string &msg) return; } - GameState::sayAround(player, msg); + GameState::sayAround(static_cast<Actor*>(player), msg); // log transaction std::string logmsg = "[public] " + msg; @@ -1363,16 +1389,17 @@ static void handleLogsay(Being *player, std::string &msg) say("Message logged", player); } -static void handleKillMonsters(Being *player, std::string &) +static void handleKillMonsters(Entity *player, std::string &) { const MapComposite *map = player->getMap(); int count = 0; for (BeingIterator it(map->getWholeMapIterator()); it; ++it) { - if ((*it)->getType() == OBJECT_MONSTER && (*it)->getAction() != DEAD) + if ((*it)->getType() == OBJECT_MONSTER && + (*it)->getComponent<BeingComponent>()->getAction() != DEAD) { - (*it)->died(); + (*it)->getComponent<BeingComponent>()->died(**it); count++; } } @@ -1388,7 +1415,7 @@ static void handleKillMonsters(Being *player, std::string &) accountHandler->sendTransaction(databaseId, TRANS_CMD_KILLMONSTERS, msg); } -static void handleCraft(Being *player, std::string &args) +static void handleCraft(Entity *player, std::string &args) { std::stringstream errMsg; std::list<InventoryItem> recipe; @@ -1461,7 +1488,7 @@ static void handleCraft(Being *player, std::string &args) } } -static void handleGetPos(Being *player, std::string &args) +static void handleGetPos(Entity *player, std::string &args) { std::string character = getArgument(args); if (character.empty()) @@ -1470,14 +1497,14 @@ static void handleGetPos(Being *player, std::string &args) say("Usage: @getpos <character>", player); return; } - Being *other; + Entity *other; other = gameHandler->getCharacterByNameSlow(character); if (!other) { say("Invalid character, or player is offline.", player); return; } - const Point &pos = other->getPosition(); + const Point &pos = static_cast<Actor*>(other)->getPosition(); std::stringstream str; str << "The current location of " << character @@ -1491,7 +1518,7 @@ static void handleGetPos(Being *player, std::string &args) say(str.str(), player); } -static void handleSkills(Being *player, std::string &args) +static void handleSkills(Entity *player, std::string &args) { std::string character = getArgument(args); if (character.empty()) @@ -1500,7 +1527,7 @@ static void handleSkills(Being *player, std::string &args) say("Usage: @skills <character>", player); return; } - Being *other; + Entity *other; if (character == "#") other = player; else @@ -1515,7 +1542,8 @@ static void handleSkills(Being *player, std::string &args) auto *characterComponent = player->getComponent<CharacterComponent>(); - say("List of skills of player '" + other->getName() + "':", player); + say("List of skills of player '" + + other->getComponent<BeingComponent>()->getName() + "':", player); std::map<int, int>::const_iterator it = characterComponent->getSkillBegin(); std::map<int, int>::const_iterator it_end = @@ -1536,7 +1564,7 @@ static void handleSkills(Being *player, std::string &args) } } -static void handleEffect(Being *player, std::string &args) +static void handleEffect(Entity *player, std::string &args) { std::vector<std::string> arguments; for (std::string arg = getArgument(args); !arg.empty(); @@ -1548,18 +1576,18 @@ static void handleEffect(Being *player, std::string &args) if (arguments.size() == 1) { int id = utils::stringToInt(arguments[0]); - Effects::show(id, player); + Effects::show(id, static_cast<Actor*>(player)); } else if (arguments.size() == 2) { int id = utils::stringToInt(arguments[0]); - Being *p = gameHandler->getCharacterByNameSlow(arguments[1]); + Entity *p = gameHandler->getCharacterByNameSlow(arguments[1]); if (!p) { say("Invalid target player.", player); return; } - Effects::show(id, p); + Effects::show(id, static_cast<Actor*>(p)); } else if (arguments.size() == 3) { @@ -1576,7 +1604,7 @@ static void handleEffect(Being *player, std::string &args) } } -static void handleGiveSpecial(Being *player, std::string &args) +static void handleGiveSpecial(Entity *player, std::string &args) { std::string character = getArgument(args); std::string special = getArgument(args); @@ -1587,7 +1615,7 @@ static void handleGiveSpecial(Being *player, std::string &args) return; } - Being *other; + Entity *other; if (character == "#") other = player; else @@ -1613,7 +1641,7 @@ static void handleGiveSpecial(Being *player, std::string &args) } } -static void handleTakeSpecial(Being *player, std::string &args) +static void handleTakeSpecial(Entity *player, std::string &args) { std::string character = getArgument(args); std::string special = getArgument(args); @@ -1624,7 +1652,7 @@ static void handleTakeSpecial(Being *player, std::string &args) return; } - Being *other; + Entity *other; if (character == "#") other = player; else @@ -1654,7 +1682,7 @@ static void handleTakeSpecial(Being *player, std::string &args) } } -static void handleRechargeSpecial(Being *player, std::string &args) +static void handleRechargeSpecial(Entity *player, std::string &args) { std::string character = getArgument(args); std::string special = getArgument(args); @@ -1666,7 +1694,7 @@ static void handleRechargeSpecial(Being *player, std::string &args) return; } - Being *other; + Entity *other; if (character == "#") other = player; else @@ -1714,7 +1742,7 @@ static void handleRechargeSpecial(Being *player, std::string &args) } } -static void handleListSpecials(Being *player, std::string &args) +static void handleListSpecials(Entity *player, std::string &args) { std::string character = getArgument(args); if (character.empty()) @@ -1724,7 +1752,7 @@ static void handleListSpecials(Being *player, std::string &args) return; } - Being *other; + Entity *other; if (character == "#") other = player; else @@ -1739,7 +1767,8 @@ static void handleListSpecials(Being *player, std::string &args) auto *characterComponent = other->getComponent<CharacterComponent>(); - say("Specials of character " + other->getName() + ":", player); + say("Specials of character " + + other->getComponent<BeingComponent>()->getName() + ":", player); for (SpecialMap::const_iterator it = characterComponent->getSpecialBegin(), it_end = characterComponent->getSpecialEnd(); it != it_end; ++it) { @@ -1751,7 +1780,7 @@ static void handleListSpecials(Being *player, std::string &args) } } -void CommandHandler::handleCommand(Being *player, +void CommandHandler::handleCommand(Entity *player, const std::string &command) { // get command type, and arguments diff --git a/src/game-server/commandhandler.h b/src/game-server/commandhandler.h index b5a268f5..c947a4dc 100644 --- a/src/game-server/commandhandler.h +++ b/src/game-server/commandhandler.h @@ -23,14 +23,14 @@ #include <string> -class Being; +class Entity; namespace CommandHandler { /** * Parse and handle the given command. */ - void handleCommand(Being *player, const std::string &command); + void handleCommand(Entity *player, const std::string &command); } #endif //SERVER_COMMANDHANDLER_H diff --git a/src/game-server/component.h b/src/game-server/component.h index 45fe742e..c3f54839 100644 --- a/src/game-server/component.h +++ b/src/game-server/component.h @@ -26,6 +26,7 @@ class Entity; enum ComponentType { CT_Character, + CT_Being, CT_Effect, CT_Fighting, CT_Item, diff --git a/src/game-server/effect.cpp b/src/game-server/effect.cpp index f994ec77..b8e03dab 100644 --- a/src/game-server/effect.cpp +++ b/src/game-server/effect.cpp @@ -45,7 +45,7 @@ namespace Effects GameState::enqueueInsert(effect); } - void show(int id, Being *b) + void show(int id, Actor *b) { EffectComponent *effectComponent = new EffectComponent(id); effectComponent->setBeing(b); diff --git a/src/game-server/effect.h b/src/game-server/effect.h index 4ac63611..4c355d67 100644 --- a/src/game-server/effect.h +++ b/src/game-server/effect.h @@ -24,7 +24,8 @@ #include "game-server/component.h" -class Being; +class Actor; +class Entity; class MapComposite; class Point; @@ -42,7 +43,7 @@ class EffectComponent : public Component int getEffectId() const { return mEffectId; } - Being *getBeing() const + Entity *getBeing() const { return mBeing; } /** @@ -56,13 +57,13 @@ class EffectComponent : public Component void setShown() { mHasBeenShown = true; } - void setBeing(Being *b) + void setBeing(Entity *b) { mBeing = b; } private: int mEffectId; bool mHasBeenShown; - Being *mBeing; + Entity *mBeing; }; @@ -72,7 +73,7 @@ namespace Effects * Convenience methods to show an effect. */ void show(int id, MapComposite *map, const Point &pos); - void show(int id, Being *b); + void show(int id, Actor *b); // TODO: get this in sync with effects.xml enum { diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 55588b61..b01c5002 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -153,7 +153,7 @@ static Actor *findActorNear(Actor *p, int id) return 0; } -static Being *findBeingNear(Actor *p, int id) +static Entity *findBeingNear(Actor *p, int id) { MapComposite *map = p->getMap(); const Point &ppos = p->getPosition(); @@ -161,7 +161,7 @@ static Being *findBeingNear(Actor *p, int id) const int pixelDist = DEFAULT_TILE_LENGTH * TILES_TO_BE_NEAR; for (BeingIterator i(map->getAroundPointIterator(ppos, pixelDist)); i; ++i) { - Being *b = *i; + Actor *b = static_cast<Actor *>(*i); if (b->getPublicID() != id) continue; return ppos.inRangeOf(b->getPosition(), pixelDist) ? b : 0; @@ -169,7 +169,7 @@ static Being *findBeingNear(Actor *p, int id) return 0; } -static Being *findCharacterNear(Actor *p, int id) +static Entity *findCharacterNear(Actor *p, int id) { MapComposite *map = p->getMap(); const Point &ppos = p->getPosition(); @@ -178,7 +178,7 @@ static Being *findCharacterNear(Actor *p, int id) for (CharacterIterator i(map->getAroundPointIterator(ppos, pixelDist)); i; ++i) { - Being *c = *i; + Actor *c = static_cast<Actor *>(*i); if (c->getPublicID() != id) continue; @@ -337,7 +337,7 @@ void GameHandler::sendTo(GameClient *client, MessageOut &msg) client->send(msg); } -void GameHandler::addPendingCharacter(const std::string &token, Being *ch) +void GameHandler::addPendingCharacter(const std::string &token, Entity *ch) { /* First, check if the character is already on the map. This may happen if a client just lost its connection, and logged to the account server @@ -348,7 +348,7 @@ void GameHandler::addPendingCharacter(const std::string &token, Being *ch) i_end = clients.end(); i != i_end; ++i) { GameClient *c = static_cast< GameClient * >(*i); - Being *old_ch = c->character; + Entity *old_ch = c->character; const int oldId = old_ch->getComponent<CharacterComponent>() ->getDatabaseID(); if (old_ch && oldId == id) @@ -377,7 +377,7 @@ void GameHandler::addPendingCharacter(const std::string &token, Being *ch) mTokenCollector.addPendingConnect(token, ch); } -void GameHandler::tokenMatched(GameClient *computer, Being *character) +void GameHandler::tokenMatched(GameClient *computer, Entity *character) { computer->character = character; computer->status = CLIENT_CONNECTED; @@ -421,19 +421,19 @@ void GameHandler::deletePendingClient(GameClient *computer) computer->disconnect(msg); } -void GameHandler::deletePendingConnect(Being *character) +void GameHandler::deletePendingConnect(Entity *character) { delete character; } -Being *GameHandler::getCharacterByNameSlow(const std::string &name) const +Entity *GameHandler::getCharacterByNameSlow(const std::string &name) const { for (NetComputers::const_iterator i = clients.begin(), i_end = clients.end(); i != i_end; ++i) { GameClient *c = static_cast< GameClient * >(*i); - Being *ch = c->character; - if (ch && ch->getName() == name && + Entity *ch = c->character; + if (ch && ch->getComponent<BeingComponent>()->getName() == name && c->status == CLIENT_CONNECTED) { return ch; @@ -455,11 +455,11 @@ void GameHandler::handleSay(GameClient &client, MessageIn &message) } if (!client.character->getComponent<CharacterComponent>()->isMuted()) { - GameState::sayAround(client.character, say); + GameState::sayAround(static_cast<Actor *>(client.character), say); } else { - GameState::sayTo(client.character, NULL, + GameState::sayTo(static_cast<Actor *>(client.character), nullptr, "You are not allowed to talk right now."); } } @@ -467,15 +467,12 @@ void GameHandler::handleSay(GameClient &client, MessageIn &message) void GameHandler::handleNpc(GameClient &client, MessageIn &message) { int id = message.readInt16(); - Actor *actor = findActorNear(client.character, id); + Actor *actor = findActorNear(static_cast<Actor *>(client.character), id); if (!actor || actor->getType() != OBJECT_NPC) { sendNpcError(client, id, "Not close enough to NPC\n"); return; } - - Being *npc = static_cast<Being*>(actor); - switch (message.getId()) { case PGMSG_NPC_SELECT: @@ -488,7 +485,7 @@ void GameHandler::handleNpc(GameClient &client, MessageIn &message) Npc::stringReceived(client.character, message.readString()); break; case PGMSG_NPC_TALK: - Npc::start(npc, client.character); + Npc::start(actor, client.character); break; case PGMSG_NPC_TALK_NEXT: default: @@ -501,7 +498,7 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message) { const int x = message.readInt16(); const int y = message.readInt16(); - const Point ppos = client.character->getPosition(); + const Point ppos = static_cast<Actor *>(client.character)->getPosition(); // TODO: use a less arbitrary value. if (std::abs(x - ppos.x) + std::abs(y - ppos.y) < 48) @@ -510,8 +507,8 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message) Point ipos(x, y); for (FixedActorIterator i(map->getAroundPointIterator(ipos, 0)); i; ++i) { - Actor *o = *i; - Point opos = o->getPosition(); + Entity *o = *i; + Point opos = static_cast<Actor *>(o)->getPosition(); if (o->getType() == OBJECT_ITEM && opos.x == x && opos.y == y) { @@ -553,7 +550,7 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message) void GameHandler::handleUseItem(GameClient &client, MessageIn &message) { - if (client.character->getAction() == DEAD) + if (client.character->getComponent<BeingComponent>()->getAction() == DEAD) return; const int slot = message.readInt16(); @@ -587,7 +584,7 @@ void GameHandler::handleDrop(GameClient &client, MessageIn &message) { int nb = inv.removeFromSlot(slot, amount); MapComposite *map = client.character->getMap(); - Point pos = client.character->getPosition(); + Point pos = static_cast<Actor *>(client.character)->getPosition(); Entity *item = Item::create(map, pos, ic, amount - nb); @@ -625,7 +622,8 @@ void GameHandler::handleWalk(GameClient &client, MessageIn &message) const int y = message.readInt16(); Point dst(x, y); - client.character->setDestination(dst); + client.character->getComponent<BeingComponent>()->setDestination( + *client.character, dst); } void GameHandler::handleEquip(GameClient &client, MessageIn &message) @@ -672,28 +670,32 @@ void GameHandler::handleMoveItem(GameClient &client, MessageIn &message) void GameHandler::handleAttack(GameClient &client, MessageIn &message) { int id = message.readInt16(); - LOG_DEBUG("Character " << client.character->getPublicID() + Actor *actor = static_cast<Actor *>(client.character); + LOG_DEBUG("Character " << actor->getPublicID() << " attacked being " << id); - Being *being = findBeingNear(client.character, id); + Actor *being = static_cast<Actor *>(findBeingNear(actor, id)); if (being && being->getType() != OBJECT_NPC) { client.character->getComponent<CombatComponent>()->setTarget(being); - client.character->setAction(ATTACK); + client.character->getComponent<BeingComponent>()->setAction(*actor, + ATTACK); } } void GameHandler::handleUseSpecialOnBeing(GameClient &client, MessageIn &message) { - if (client.character->getAction() == DEAD) + if (client.character->getComponent<BeingComponent>()->getAction() == DEAD) return; + Actor *actor = static_cast<Actor *>(client.character); + const int specialID = message.readInt8(); const int targetID = message.readInt16(); // 0 when no target is selected - Being *being = 0; + Entity *being = 0; if (targetID != 0) - being = findBeingNear(client.character, targetID); - LOG_DEBUG("Character " << client.character->getPublicID() + being = findBeingNear(actor, targetID); + LOG_DEBUG("Character " << actor->getPublicID() << " tries to use his special attack " << specialID); auto *characterComponent = client.character ->getComponent<CharacterComponent>(); @@ -702,14 +704,15 @@ void GameHandler::handleUseSpecialOnBeing(GameClient &client, MessageIn &message void GameHandler::handleUseSpecialOnPoint(GameClient &client, MessageIn &message) { - if (client.character->getAction() == DEAD) + if (client.character->getComponent<BeingComponent>()->getAction() == DEAD) return; const int specialID = message.readInt8(); const int x = message.readInt16(); const int y = message.readInt16(); - LOG_DEBUG("Character " << client.character->getPublicID() + LOG_DEBUG("Character " + << static_cast<Actor *>(client.character)->getPublicID() << " tries to use his special attack " << specialID); auto *characterComponent = client.character ->getComponent<CharacterComponent>(); @@ -718,8 +721,10 @@ void GameHandler::handleUseSpecialOnPoint(GameClient &client, MessageIn &message void GameHandler::handleActionChange(GameClient &client, MessageIn &message) { + auto *beingComponent = client.character->getComponent<BeingComponent>(); + const BeingAction action = (BeingAction) message.readInt8(); - const BeingAction current = (BeingAction) client.character->getAction(); + const BeingAction current = (BeingAction) beingComponent->getAction(); bool logActionChange = true; switch (action) @@ -727,14 +732,14 @@ void GameHandler::handleActionChange(GameClient &client, MessageIn &message) case STAND: if (current == SIT) { - client.character->setAction(STAND); + beingComponent->setAction(*client.character, STAND); logActionChange = false; } break; case SIT: if (current == STAND) { - client.character->setAction(SIT); + beingComponent->setAction(*client.character, SIT); logActionChange = false; } break; @@ -761,7 +766,8 @@ void GameHandler::handleActionChange(GameClient &client, MessageIn &message) void GameHandler::handleDirectionChange(GameClient &client, MessageIn &message) { const BeingDirection direction = (BeingDirection) message.readInt8(); - client.character->setDirection(direction); + client.character->getComponent<BeingComponent>() + ->setDirection(*client.character, direction); } void GameHandler::handleDisconnect(GameClient &client, MessageIn &message) @@ -804,7 +810,7 @@ void GameHandler::handleTradeRequest(GameClient &client, MessageIn &message) if (t->request(client.character, id)) return; - Being *q = findCharacterNear(client.character, id); + Entity *q = findCharacterNear(static_cast<Actor *>(client.character), id); if (!q || characterComponent->isBusy()) { client.send(MessageOut(GPMSG_TRADE_CANCEL)); @@ -815,7 +821,8 @@ void GameHandler::handleTradeRequest(GameClient &client, MessageIn &message) // log transaction std::string str; - str = "User requested trade with " + q->getName(); + str = "User requested trade with " + q->getComponent<BeingComponent>() + ->getName(); accountHandler->sendTransaction(characterComponent->getDatabaseID(), TRANS_TRADE_REQUEST, str); } @@ -952,16 +959,18 @@ void GameHandler::handlePartyInvite(GameClient &client, MessageIn &message) const int visualRange = Configuration::getValue("game_visualRange", 448); std::string invitee = message.readString(); - if (invitee == client.character->getName()) + if (invitee == client.character->getComponent<BeingComponent>()->getName()) return; for (CharacterIterator it(map->getWholeMapIterator()); it; ++it) { - if ((*it)->getName() == invitee) + if ((*it)->getComponent<BeingComponent>()->getName() == invitee) { // calculate if the invitee is within the visual range - const int xInviter = client.character->getPosition().x; - const int yInviter = client.character->getPosition().y; + const int xInviter = + static_cast<Actor *>(client.character)->getPosition().x; + const int yInviter = + static_cast<Actor *>(client.character)->getPosition().y; const int xInvitee = (*it)->getPosition().x; const int yInvitee = (*it)->getPosition().y; const int dx = std::abs(xInviter - xInvitee); @@ -969,7 +978,8 @@ void GameHandler::handlePartyInvite(GameClient &client, MessageIn &message) if (visualRange > std::max(dx, dy)) { MessageOut out(GCMSG_PARTY_INVITE); - out.writeString(client.character->getName()); + out.writeString(client.character + ->getComponent<BeingComponent>()->getName()); out.writeString(invitee); accountHandler->send(out); return; @@ -988,7 +998,8 @@ void GameHandler::handleTriggerEmoticon(GameClient &client, MessageIn &message) { const int id = message.readInt16(); if (emoteManager->isIdAvailable(id)) - client.character->triggerEmote(id); + client.character->getComponent<BeingComponent>()->triggerEmote( + *client.character, id); } void GameHandler::sendNpcError(GameClient &client, int id, diff --git a/src/game-server/gamehandler.h b/src/game-server/gamehandler.h index 70facb4c..d66caf52 100644 --- a/src/game-server/gamehandler.h +++ b/src/game-server/gamehandler.h @@ -21,11 +21,12 @@ #ifndef SERVER_GAMEHANDLER_H #define SERVER_GAMEHANDLER_H -#include "game-server/character.h" #include "net/connectionhandler.h" #include "net/netcomputer.h" #include "utils/tokencollector.h" +class Entity; + enum { CLIENT_LOGIN = 0, @@ -37,8 +38,8 @@ enum struct GameClient: NetComputer { GameClient(ENetPeer *peer) - : NetComputer(peer), character(NULL), status(CLIENT_LOGIN) {} - Being *character; + : NetComputer(peer), character(nullptr), status(CLIENT_LOGIN) {} + Entity *character; int status; }; @@ -86,13 +87,13 @@ class GameHandler: public ConnectionHandler * Registers a character that should soon be claimed by a client. * @param token token used by the client when connecting. */ - void addPendingCharacter(const std::string &token, Being *); + void addPendingCharacter(const std::string &token, Entity *); /** * Combines a client with its character. * (Needed for TokenCollector) */ - void tokenMatched(GameClient *computer, Being *character); + void tokenMatched(GameClient *computer, Entity *character); /** * Deletes a pending client's data. @@ -104,13 +105,13 @@ class GameHandler: public ConnectionHandler * Deletes a pending connection's data. * (Needed for TokenCollector) */ - void deletePendingConnect(Being *character); + void deletePendingConnect(Entity *character); /** * Gets the character associated to a character name. This method is * slow, so it should never be called for regular operations. */ - Being *getCharacterByNameSlow(const std::string &) const; + Entity *getCharacterByNameSlow(const std::string &) const; protected: NetComputer *computerConnected(ENetPeer *); @@ -161,7 +162,7 @@ class GameHandler: public ConnectionHandler /** * Container for pending clients and pending connections. */ - TokenCollector<GameHandler, GameClient *, Being *> mTokenCollector; + TokenCollector<GameHandler, GameClient *, Entity *> mTokenCollector; }; extern GameHandler *gameHandler; diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index 2219ada1..465cf8fa 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -29,13 +29,13 @@ #include "net/messageout.h" #include "utils/logger.h" -Inventory::Inventory(Being *p): +Inventory::Inventory(Entity *p): mPoss(&p->getComponent<CharacterComponent>()->getPossessions()), mCharacter(p) { } -Inventory::Inventory(Being *p, Possessions &possessions): +Inventory::Inventory(Entity *p, Possessions &possessions): mPoss(&possessions), mCharacter(p) { @@ -97,7 +97,7 @@ void Inventory::initialize() { LOG_WARN("Inventory: deleting unknown item type " << it1->second.itemId << " from the inventory of '" - << mCharacter->getName() + << mCharacter->getComponent<BeingComponent>()->getName() << "'!"); mPoss->inventory.erase(it1++); } @@ -124,7 +124,7 @@ void Inventory::initialize() { LOG_WARN("Equipment: deleting unknown item id " << it2->second.itemId << " from the equipment of '" - << mCharacter->getName() + << mCharacter->getComponent<BeingComponent>()->getName() << "'!"); mPoss->equipSlots.erase(it2++); continue; @@ -164,7 +164,8 @@ unsigned Inventory::insert(unsigned itemId, unsigned amount) unsigned maxPerSlot = item->getMaxPerSlot(); LOG_DEBUG("Inventory: Inserting " << amount << " item(s) Id: " << itemId - << " for character '" << mCharacter->getName() << "'."); + << " for character '" + << mCharacter->getComponent<BeingComponent>()->getName() << "'."); InventoryData::iterator it, it_end = mPoss->inventory.end(); // Add to slots with existing items of this type first. @@ -278,7 +279,8 @@ unsigned Inventory::remove(unsigned itemId, unsigned amount) return amount; LOG_DEBUG("Inventory: Request remove of " << amount << " item(s) id: " - << itemId << " for character: '" << mCharacter->getName() + << itemId << " for character: '" + << mCharacter->getComponent<BeingComponent>()->getName() << "'."); MessageOut invMsg(GPMSG_INVENTORY); @@ -338,7 +340,8 @@ unsigned Inventory::move(unsigned slot1, unsigned slot2, unsigned amount) { LOG_DEBUG(amount << " item(s) requested to move from: " << slot1 << " to " - << slot2 << " for character: '" << mCharacter->getName() << "'."); + << slot2 << " for character: '" + << mCharacter->getComponent<BeingComponent>()->getName() << "'."); if (!amount || slot1 == slot2 || slot2 >= INVENTORY_SLOTS) return amount; @@ -467,7 +470,8 @@ unsigned Inventory::removeFromSlot(unsigned slot, unsigned amount) return amount; LOG_DEBUG("Inventory: Request Removal of " << amount << " item(s) in slot: " - << slot << " for character: '" << mCharacter->getName() << "'."); + << slot << " for character: '" + << mCharacter->getComponent<BeingComponent>()->getName() << "'."); MessageOut invMsg(GPMSG_INVENTORY); // Check if an item of the same id exists elsewhere in the inventory @@ -666,7 +670,8 @@ bool Inventory::equip(int inventorySlot) { // Something went wrong even when we tested the unequipment process. LOG_WARN("Unable to unequip even when unequip was tested. " - "Character : " << mCharacter->getName() + "Character : " + << mCharacter->getComponent<BeingComponent>()->getName() << ", unequip slot: " << *it3); return false; } @@ -837,6 +842,7 @@ bool Inventory::unequip(unsigned itemInstance) void Inventory::checkLookchanges(unsigned slotTypeId) { + Actor *actor = static_cast<Actor*>(mCharacter); if (itemManager->isEquipSlotVisible(slotTypeId)) - mCharacter->raiseUpdateFlags(UPDATEFLAG_LOOKSCHANGE); + actor->raiseUpdateFlags(UPDATEFLAG_LOOKSCHANGE); } diff --git a/src/game-server/inventory.h b/src/game-server/inventory.h index 75336ace..1740a8d9 100644 --- a/src/game-server/inventory.h +++ b/src/game-server/inventory.h @@ -36,8 +36,8 @@ class Inventory /** * Creates a view on the possessions of a character. */ - explicit Inventory(Being *); - Inventory(Being *, Possessions &possessions); + explicit Inventory(Entity *); + Inventory(Entity *, Possessions &possessions); /** * Commits delayed changes if applicable. @@ -190,7 +190,7 @@ class Inventory Possessions *mPoss; /**< Pointer to the modified possessions. */ - Being *mCharacter; /**< Character to notify. */ + Entity *mCharacter; /**< Character to notify. */ }; #endif diff --git a/src/game-server/item.cpp b/src/game-server/item.cpp index 7295b95a..2b2d4620 100644 --- a/src/game-server/item.cpp +++ b/src/game-server/item.cpp @@ -33,28 +33,33 @@ #include <map> #include <string> -bool ItemEffectAttrMod::apply(Being *itemUser) +bool ItemEffectAttrMod::apply(Entity *itemUser) { LOG_DEBUG("Applying modifier."); - itemUser->applyModifier(mAttributeId, mMod, mAttributeLayer, - mDuration, mId); + itemUser->getComponent<BeingComponent>()->applyModifier(*itemUser, + mAttributeId, mMod, + mAttributeLayer, + mDuration, mId); return false; } -void ItemEffectAttrMod::dispell(Being *itemUser) +void ItemEffectAttrMod::dispell(Entity *itemUser) { LOG_DEBUG("Dispelling modifier."); - itemUser->removeModifier(mAttributeId, mMod, mAttributeLayer, - mId, !mDuration); + itemUser->getComponent<BeingComponent>()->removeModifier(*itemUser, + mAttributeId, + mMod, + mAttributeLayer, + mId, !mDuration); } -bool ItemEffectAttack::apply(Being *itemUser) +bool ItemEffectAttack::apply(Entity *itemUser) { itemUser->getComponent<CombatComponent>()->addAttack(mAttackInfo); return false; } -void ItemEffectAttack::dispell(Being *itemUser) +void ItemEffectAttack::dispell(Entity *itemUser) { itemUser->getComponent<CombatComponent>()->removeAttack(mAttackInfo); } @@ -63,7 +68,7 @@ ItemEffectScript::~ItemEffectScript() { } -bool ItemEffectScript::apply(Being *itemUser) +bool ItemEffectScript::apply(Entity *itemUser) { if (mActivateEventName.empty()) return false; @@ -82,7 +87,7 @@ bool ItemEffectScript::apply(Being *itemUser) return false; } -void ItemEffectScript::dispell(Being *itemUser) +void ItemEffectScript::dispell(Entity *itemUser) { if (mDispellEventName.empty()) return; @@ -123,7 +128,7 @@ void ItemClass::addEffect(ItemEffectInfo *effect, mDispells.insert(std::make_pair(dispell, effect)); } -bool ItemClass::useTrigger(Being *itemUser, ItemTriggerType trigger) +bool ItemClass::useTrigger(Entity *itemUser, ItemTriggerType trigger) { if (!trigger) return false; diff --git a/src/game-server/item.h b/src/game-server/item.h index e88c1f98..e436afdc 100644 --- a/src/game-server/item.h +++ b/src/game-server/item.h @@ -28,7 +28,7 @@ #include "game-server/attack.h" #include "scripting/script.h" -class Being; +class Entity; class ItemClass; class MapComposite; @@ -105,8 +105,8 @@ class ItemEffectInfo public: virtual ~ItemEffectInfo() {} - virtual bool apply(Being *itemUser) = 0; - virtual void dispell(Being *itemUser) = 0; + virtual bool apply(Entity *itemUser) = 0; + virtual void dispell(Entity *itemUser) = 0; }; class ItemEffectAttrMod : public ItemEffectInfo @@ -118,8 +118,8 @@ class ItemEffectAttrMod : public ItemEffectInfo mMod(value), mDuration(duration), mId(id) {} - bool apply(Being *itemUser); - void dispell(Being *itemUser); + bool apply(Entity *itemUser); + void dispell(Entity *itemUser); private: unsigned mAttributeId; @@ -136,8 +136,8 @@ class ItemEffectAttack : public ItemEffectInfo mAttackInfo(attackInfo) {} - bool apply(Being *itemUser); - void dispell(Being *itemUser); + bool apply(Entity *itemUser); + void dispell(Entity *itemUser); private: AttackInfo *mAttackInfo; }; @@ -145,9 +145,9 @@ class ItemEffectAttack : public ItemEffectInfo class ItemEffectConsumes : public ItemEffectInfo { public: - bool apply(Being *) + bool apply(Entity *) { return true; } - void dispell(Being *) + void dispell(Entity *) {} }; @@ -164,8 +164,8 @@ class ItemEffectScript : public ItemEffectInfo ~ItemEffectScript(); - bool apply(Being *itemUser); - void dispell(Being *itemUser); + bool apply(Entity *itemUser); + void dispell(Entity *itemUser); private: ItemClass *mItemClass; @@ -206,7 +206,7 @@ class ItemClass * Applies the modifiers of an item to a given user. * @return true if item should be removed. */ - bool useTrigger(Being *itemUser, ItemTriggerType trigger); + bool useTrigger(Entity *itemUser, ItemTriggerType trigger); /** * Gets unit cost of these items. diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index e35c68fb..61e0a8b9 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -182,7 +182,7 @@ CharacterIterator::CharacterIterator(const ZoneIterator &it) while (iterator && (*iterator)->nbCharacters == 0) ++iterator; if (iterator) { - current = static_cast< Being * >((*iterator)->objects[pos]); + current = (*iterator)->objects[pos]; } } @@ -195,7 +195,7 @@ void CharacterIterator::operator++() } if (iterator) { - current = static_cast< Being * >((*iterator)->objects[pos]); + current = static_cast< Actor * >((*iterator)->objects[pos]); } } @@ -205,7 +205,7 @@ BeingIterator::BeingIterator(const ZoneIterator &it) while (iterator && (*iterator)->nbMovingObjects == 0) ++iterator; if (iterator) { - current = static_cast< Being * >((*iterator)->objects[pos]); + current = static_cast< Actor * >((*iterator)->objects[pos]); } } @@ -218,7 +218,7 @@ void BeingIterator::operator++() } if (iterator) { - current = static_cast< Being * >((*iterator)->objects[pos]); + current = static_cast< Actor * >((*iterator)->objects[pos]); } } @@ -541,10 +541,12 @@ ZoneIterator MapComposite::getInsideRectangleIterator(const Rectangle &p) const return ZoneIterator(r, mContent); } -ZoneIterator MapComposite::getAroundBeingIterator(Being *obj, int radius) const +ZoneIterator MapComposite::getAroundBeingIterator(Actor *obj, int radius) const { MapRegion r1; - mContent->fillRegion(r1, obj->getOldPosition(), radius); + mContent->fillRegion(r1, + obj->getComponent<BeingComponent>()->getOldPosition(), + radius); MapRegion r2 = r1; for (MapRegion::iterator i = r1.begin(), i_end = r1.end(); i != i_end; ++i) { @@ -570,7 +572,7 @@ bool MapComposite::insert(Entity *ptr) { if (ptr->isVisible()) { - if (ptr->canMove() && !mContent->allocate(static_cast< Being * >(ptr))) + if (ptr->canMove() && !mContent->allocate(static_cast< Actor * >(ptr))) { return false; } @@ -591,10 +593,9 @@ void MapComposite::remove(Entity *ptr) { if ((*i)->canFight()) { - Being *being = static_cast<Being*>(*i); - if (being->getComponent<CombatComponent>()->getTarget() == ptr) + if ((*i)->getComponent<CombatComponent>()->getTarget() == ptr) { - being->getComponent<CombatComponent>()->clearTarget(); + (*i)->getComponent<CombatComponent>()->clearTarget(); } } if (*i == ptr) @@ -610,7 +611,7 @@ void MapComposite::remove(Entity *ptr) if (ptr->canMove()) { - mContent->deallocate(static_cast< Being * >(ptr)); + mContent->deallocate(obj); } } } @@ -636,7 +637,7 @@ void MapComposite::update() // Move objects around and update zones. for (BeingIterator it(getWholeMapIterator()); it; ++it) { - (*it)->move(); + (*it)->getComponent<BeingComponent>()->move(**it); } for (int i = 0; i < mContent->mapHeight * mContent->mapWidth; ++i) @@ -651,18 +652,19 @@ void MapComposite::update() if (!(*i)->canMove()) continue; - Being *obj = static_cast< Being * >(*i); + Actor *actor = static_cast<Actor *>(*i); - const Point &pos1 = obj->getOldPosition(), - &pos2 = obj->getPosition(); + const Point &pos1 = + actor->getComponent<BeingComponent>()->getOldPosition(); + const Point &pos2 = actor->getPosition(); MapZone &src = mContent->getZone(pos1), &dst = mContent->getZone(pos2); if (&src != &dst) { addZone(src.destinations, &dst - mContent->zones); - src.remove(obj); - dst.insert(obj); + src.remove(actor); + dst.insert(actor); } } } diff --git a/src/game-server/mapcomposite.h b/src/game-server/mapcomposite.h index c8eb2e88..9a6bde07 100644 --- a/src/game-server/mapcomposite.h +++ b/src/game-server/mapcomposite.h @@ -30,7 +30,6 @@ #include "game-server/map.h" class Actor; -class Being; class CharacterComponent; class Map; class MapComposite; @@ -76,11 +75,11 @@ struct CharacterIterator { ZoneIterator iterator; unsigned short pos; - Being *current; + Actor *current; CharacterIterator(const ZoneIterator &); void operator++(); - Being *operator*() const { return current; } + Actor *operator*() const { return current; } operator bool() const { return iterator; } }; @@ -91,11 +90,11 @@ struct BeingIterator { ZoneIterator iterator; unsigned short pos; - Being *current; + Actor *current; BeingIterator(const ZoneIterator &); void operator++(); - Being *operator*() const { return current; } + Actor *operator*() const { return current; } operator bool() const { return iterator; } }; @@ -106,11 +105,11 @@ struct FixedActorIterator { ZoneIterator iterator; unsigned short pos; - Actor *current; + Entity *current; FixedActorIterator(const ZoneIterator &); void operator++(); - Actor *operator*() const { return current; } + Entity *operator*() const { return current; } operator bool() const { return iterator; } }; @@ -315,7 +314,7 @@ class MapComposite * Gets an iterator on the objects around the old and new positions of * a character (including the ones that were but are now elsewhere). */ - ZoneIterator getAroundBeingIterator(Being *, int radius) const; + ZoneIterator getAroundBeingIterator(Actor *, int radius) const; /** * Gets everything related to the map. diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp index 39b62593..e3acf323 100644 --- a/src/game-server/monster.cpp +++ b/src/game-server/monster.cpp @@ -53,14 +53,16 @@ double MonsterClass::getVulnerability(Element element) const return it->second; } -MonsterComponent::MonsterComponent(Being &being, MonsterClass *specy): +MonsterComponent::MonsterComponent(Actor &actor, MonsterClass *specy): mSpecy(specy), mOwner(NULL) { LOG_DEBUG("Monster spawned! (id: " << mSpecy->getId() << ")."); - being.setWalkMask(Map::BLOCKMASK_WALL | Map::BLOCKMASK_CHARACTER); - being.setBlockType(BLOCKTYPE_MONSTER); + auto *beingComponent = actor.getComponent<BeingComponent>(); + + actor.setWalkMask(Map::BLOCKMASK_WALL | Map::BLOCKMASK_CHARACTER); + actor.setBlockType(BLOCKTYPE_MONSTER); /* * Initialise the attribute structures. @@ -68,7 +70,7 @@ MonsterComponent::MonsterComponent(Being &being, MonsterClass *specy): for (auto attrInfo : attributeManager->getAttributeScope(MonsterScope)) { - being.createAttribute(attrInfo.first, *attrInfo.second); + beingComponent->createAttribute(attrInfo.first, *attrInfo.second); } /* @@ -86,13 +88,13 @@ MonsterComponent::MonsterComponent(Being &being, MonsterClass *specy): double factor = 100 + (rand() % (mutation * 2)) - mutation; attributeValue = attributeValue * factor / 100.0; } - being.setAttribute(attribute.first, attributeValue); + beingComponent->setAttribute(actor, attribute.first, attributeValue); } - being.setSize(specy->getSize()); - being.setGender(specy->getGender()); + actor.setSize(specy->getSize()); + beingComponent->setGender(specy->getGender()); - being.signal_died.connect(sigc::mem_fun(this, + beingComponent->signal_died.connect(sigc::mem_fun(this, &MonsterComponent::monsterDied)); // Set positions relative to target from which the monster can attack @@ -103,8 +105,8 @@ MonsterComponent::MonsterComponent(Being &being, MonsterClass *specy): mAttackPositions.push_back(AttackPosition(0, dist, UP)); MonsterCombatComponent *combatComponent = - new MonsterCombatComponent(being, specy); - being.addComponent(combatComponent); + new MonsterCombatComponent(actor, specy); + actor.addComponent(combatComponent); double damageMutation = mutation ? (100.0 + (rand() % (mutation * 2)) - mutation) / 100.0 : 1.0; @@ -124,13 +126,15 @@ void MonsterComponent::update(Entity &entity) mOwner = NULL; // Temporary until all depedencies are available as component - Being &being = static_cast<Being &>(entity); + Actor &being = static_cast<Actor &>(entity); + + auto *beingComponent = entity.getComponent<BeingComponent>(); // If dead, remove it - if (being.getAction() == DEAD) + if (beingComponent->getAction() == DEAD) { if (mDecayTimeout.expired()) - GameState::enqueueRemove(&being); + GameState::enqueueRemove(static_cast<Actor*>(&entity)); return; } @@ -151,7 +155,7 @@ void MonsterComponent::update(Entity &entity) // We have no target - let's wander around if (mStrollTimeout.expired() && - being.getPosition() == being.getDestination()) + being.getPosition() == beingComponent->getDestination()) { if (mKillStealProtectedTimeout.expired()) { @@ -165,7 +169,7 @@ void MonsterComponent::update(Entity &entity) // Don't allow negative destinations, to avoid rounding // problems when divided by tile size if (randomPos.x >= 0 && randomPos.y >= 0) - being.setDestination(randomPos); + beingComponent->setDestination(entity, randomPos); } mStrollTimeout.set(10 + rand() % 10); } @@ -174,16 +178,17 @@ void MonsterComponent::update(Entity &entity) void MonsterComponent::refreshTarget(Entity &entity) { - // Temporary until all depedencies are available as component - Being &being = static_cast<Being &>(entity); + // Temporary until all depdencies are available as component + Actor &actor = static_cast<Actor &>(entity); + auto *beingComponent = entity.getComponent<BeingComponent>(); // We are dead and sadly not possible to keep attacking :( - if (being.getAction() == DEAD) + if (beingComponent->getAction() == DEAD) return; // Check potential attack positions int bestTargetPriority = 0; - Being *bestTarget = 0; + Actor *bestTarget = 0; Point bestAttackPosition; // reset Target. We will find a new one if possible @@ -191,7 +196,7 @@ void MonsterComponent::refreshTarget(Entity &entity) // Iterate through objects nearby int aroundArea = Configuration::getValue("game_visualRange", 448); - for (BeingIterator i(entity.getMap()->getAroundBeingIterator(&being, + for (BeingIterator i(entity.getMap()->getAroundBeingIterator(&actor, aroundArea)); i; ++i) { @@ -199,15 +204,16 @@ void MonsterComponent::refreshTarget(Entity &entity) if ((*i)->getType() != OBJECT_CHARACTER) continue; - Being *target = static_cast<Being *>(*i); + Actor *target = *i; // Dead characters are ignored - if (target->getAction() == DEAD) + if (beingComponent->getAction() == DEAD) continue; // Determine how much we hate the target int targetPriority = 0; - std::map<Being *, AggressionInfo>::iterator angerIterator = mAnger.find(target); + std::map<Entity *, AggressionInfo>::iterator angerIterator = + mAnger.find(target); if (angerIterator != mAnger.end()) { const AggressionInfo &aggressionInfo = angerIterator->second; @@ -244,15 +250,15 @@ void MonsterComponent::refreshTarget(Entity &entity) if (bestTarget) { entity.getComponent<CombatComponent>()->setTarget(bestTarget); - if (bestAttackPosition == being.getPosition()) + if (bestAttackPosition == actor.getPosition()) { - being.setAction(ATTACK); - being.updateDirection(being.getPosition(), - bestTarget->getPosition()); + beingComponent->setAction(entity, ATTACK); + beingComponent->updateDirection(actor, actor.getPosition(), + bestTarget->getPosition()); } else { - being.setDestination(bestAttackPosition); + beingComponent->setDestination(entity, bestAttackPosition); } } } @@ -297,7 +303,7 @@ int MonsterComponent::calculatePositionPriority(Entity &entity, void MonsterComponent::forgetTarget(Entity *entity) { - Being *b = static_cast< Being * >(entity); + Entity *b = static_cast< Entity * >(entity); { AggressionInfo &aggressionInfo = mAnger[b]; aggressionInfo.removedConnection.disconnect(); @@ -307,42 +313,41 @@ void MonsterComponent::forgetTarget(Entity *entity) if (b->getType() == OBJECT_CHARACTER) { - Being *c = static_cast< Being * >(b); + Entity *c = static_cast< Entity * >(b); mExpReceivers.erase(c); mLegalExpReceivers.erase(c); } } -void MonsterComponent::changeAnger(Actor *target, int amount) +void MonsterComponent::changeAnger(Entity *target, int amount) { const EntityType type = target->getType(); if (type != OBJECT_MONSTER && type != OBJECT_CHARACTER) return; - Being *being = static_cast< Being * >(target); - - if (mAnger.find(being) != mAnger.end()) + if (mAnger.find(target) != mAnger.end()) { - mAnger[being].anger += amount; + mAnger[target].anger += amount; } else { - AggressionInfo &aggressionInfo = mAnger[being]; + AggressionInfo &aggressionInfo = mAnger[target]; aggressionInfo.anger = amount; // Forget target either when it's removed or died, whichever // happens first. aggressionInfo.removedConnection = - being->signal_removed.connect(sigc::mem_fun(this, &MonsterComponent::forgetTarget)); - aggressionInfo.diedConnection = - being->signal_died.connect(sigc::mem_fun(this, &MonsterComponent::forgetTarget)); + target->signal_removed.connect(sigc::mem_fun(this, &MonsterComponent::forgetTarget)); + aggressionInfo.diedConnection = target->getComponent<BeingComponent>() + ->signal_died.connect( + sigc::mem_fun(this, &MonsterComponent::forgetTarget)); } } -std::map<Being *, int> MonsterComponent::getAngerList() const +std::map<Entity *, int> MonsterComponent::getAngerList() const { - std::map<Being *, int> result; - std::map<Being *, AggressionInfo>::const_iterator i, i_end; + std::map<Entity *, int> result; + std::map<Entity *, AggressionInfo>::const_iterator i, i_end; for (i = mAnger.begin(), i_end = mAnger.end(); i != i_end; ++i) { @@ -353,8 +358,11 @@ std::map<Being *, int> MonsterComponent::getAngerList() const return result; } -void MonsterComponent::monsterDied(Being *monster) +void MonsterComponent::monsterDied(Entity *monster) { + // Temporary until all depdencies are available as component + Actor *actor = static_cast<Actor *>(monster); + mDecayTimeout.set(DECAY_TIME); if (mExpReceivers.size() > 0) @@ -369,14 +377,14 @@ void MonsterComponent::monsterDied(Being *monster) if (p <= drop.probability) { Actor *item = Item::create(monster->getMap(), - monster->getPosition(), + actor->getPosition(), drop.item, 1); GameState::enqueueInsert(item); } } // Distribute exp reward. - std::map<Being *, std::set <size_t> > ::iterator iChar; + std::map<Entity *, std::set <size_t> > ::iterator iChar; std::set<size_t>::iterator iSkill; @@ -408,7 +416,7 @@ void MonsterComponent::monsterDied(Being *monster) } -void MonsterComponent::receivedDamage(Being *source, const Damage &damage, int hpLoss) +void MonsterComponent::receivedDamage(Entity *source, const Damage &damage, int hpLoss) { if (source) changeAnger(source, hpLoss); diff --git a/src/game-server/monster.h b/src/game-server/monster.h index e4d986b3..1b17bfd6 100644 --- a/src/game-server/monster.h +++ b/src/game-server/monster.h @@ -280,7 +280,7 @@ class MonsterComponent : public Component /** Time in game ticks until ownership of a monster can change. */ static const int KILLSTEAL_PROTECTION_TIME = 100; - MonsterComponent(Being &being, MonsterClass *); + MonsterComponent(Actor &actor, MonsterClass *); ~MonsterComponent(); /** @@ -299,16 +299,16 @@ class MonsterComponent : public Component /** * Signal handler */ - void monsterDied(Being *monster); + void monsterDied(Entity *monster); - void receivedDamage(Being *attacker, const Damage &damage, int hpLoss); + void receivedDamage(Entity *attacker, const Damage &damage, int hpLoss); /** * Alters hate for the monster */ - void changeAnger(Actor *target, int amount); + void changeAnger(Entity *target, int amount); - std::map<Being *, int> getAngerList() const; + std::map<Entity *, int> getAngerList() const; /** * Removes a being from the anger list. @@ -334,21 +334,21 @@ class MonsterComponent : public Component sigc::connection removedConnection; sigc::connection diedConnection; }; - std::map<Being *, AggressionInfo> mAnger; + std::map<Entity *, AggressionInfo> mAnger; /** * Character who currently owns this monster (killsteal protection). */ - Being *mOwner; + Entity *mOwner; /** List of characters and their skills that attacked this monster. */ - std::map<Being *, std::set <size_t> > mExpReceivers; + std::map<Entity *, std::set <size_t> > mExpReceivers; /** * List of characters who are entitled to receive exp (killsteal * protection). */ - std::set<Being *> mLegalExpReceivers; + std::set<Entity *> mLegalExpReceivers; /** * Set positions relative to target from which the monster can attack. diff --git a/src/game-server/monstercombatcomponent.cpp b/src/game-server/monstercombatcomponent.cpp index 0fc0b483..35e2f249 100644 --- a/src/game-server/monstercombatcomponent.cpp +++ b/src/game-server/monstercombatcomponent.cpp @@ -23,7 +23,7 @@ #include "game-server/monster.h" #include "scripting/scriptmanager.h" -MonsterCombatComponent::MonsterCombatComponent(Being &monster, +MonsterCombatComponent::MonsterCombatComponent(Entity &monster, MonsterClass *specy): CombatComponent(monster), mDamageMutation(0) @@ -37,11 +37,11 @@ MonsterCombatComponent::MonsterCombatComponent(Being &monster, /** * Performs an attack */ -void MonsterCombatComponent::processAttack(Being *source, Attack &attack) +void MonsterCombatComponent::processAttack(Entity *source, Attack &attack) { if (!mTarget) { - source->setAction(STAND); + source->getComponent<BeingComponent>()->setAction(*source, STAND); return; } @@ -69,8 +69,8 @@ void MonsterCombatComponent::processAttack(Being *source, Attack &attack) /** * Calls the damage function in Being and updates the aggro list */ -int MonsterCombatComponent::damage(Being &target, - Being *source, +int MonsterCombatComponent::damage(Entity &target, + Entity *source, const Damage &damage) { Damage newDamage = damage; diff --git a/src/game-server/monstercombatcomponent.h b/src/game-server/monstercombatcomponent.h index b65c48f4..36afa26e 100644 --- a/src/game-server/monstercombatcomponent.h +++ b/src/game-server/monstercombatcomponent.h @@ -31,10 +31,10 @@ class MonsterClass; class MonsterCombatComponent: public CombatComponent { public: - MonsterCombatComponent(Being &monster, MonsterClass *specy); + MonsterCombatComponent(Entity &monster, MonsterClass *specy); - void processAttack(Being *source, Attack &attack); - int damage(Being &target, Being *source, const Damage &damage); + void processAttack(Entity *source, Attack &attack); + int damage(Entity &target, Entity *source, const Damage &damage); void setDamageMutation(double mutation); diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp index e7a2988b..f9e714b9 100644 --- a/src/game-server/npc.cpp +++ b/src/game-server/npc.cpp @@ -73,7 +73,7 @@ void NpcComponent::setUpdateCallback(Script::Ref function) -static Script *prepareResume(Being *ch, Script::ThreadState expectedState) +static Script *prepareResume(Entity *ch, Script::ThreadState expectedState) { Script::Thread *thread = ch->getComponent<CharacterComponent>()->getNpcThread(); @@ -85,7 +85,7 @@ static Script *prepareResume(Being *ch, Script::ThreadState expectedState) return script; } -void Npc::start(Being *npc, Being *ch) +void Npc::start(Entity *npc, Entity *ch) { NpcComponent *npcComponent = npc->getComponent<NpcComponent>(); @@ -100,17 +100,17 @@ void Npc::start(Being *npc, Being *ch) script->push(npc); script->push(ch); ch->getComponent<CharacterComponent>() - ->startNpcThread(thread, npc->getPublicID()); + ->startNpcThread(thread, static_cast<Actor*>(npc)->getPublicID()); } } -void Npc::resume(Being *ch) +void Npc::resume(Entity *ch) { if (prepareResume(ch, Script::ThreadPaused)) ch->getComponent<CharacterComponent>()->resumeNpcThread(); } -void Npc::integerReceived(Being *ch, int value) +void Npc::integerReceived(Entity *ch, int value) { if (Script *script = prepareResume(ch, Script::ThreadExpectingNumber)) { @@ -119,7 +119,7 @@ void Npc::integerReceived(Being *ch, int value) } } -void Npc::stringReceived(Being *ch, const std::string &value) +void Npc::stringReceived(Entity *ch, const std::string &value) { if (Script *script = prepareResume(ch, Script::ThreadExpectingString)) { diff --git a/src/game-server/npc.h b/src/game-server/npc.h index 63d4ee8b..98fbd64d 100644 --- a/src/game-server/npc.h +++ b/src/game-server/npc.h @@ -88,22 +88,22 @@ namespace Npc { /** * Starts a conversation with the NPC. */ -void start(Being *npc, Being *ch); +void start(Entity *npc, Entity *ch); /** * Resumes an NPC conversation. */ -void resume(Being *ch); +void resume(Entity *ch); /** * The player has made a choice or entered an integer. */ -void integerReceived(Being *ch, int value); +void integerReceived(Entity *ch, int value); /** * The player has entered an string. */ -void stringReceived(Being *ch, const std::string &value); +void stringReceived(Entity *ch, const std::string &value); } // namespace Npc diff --git a/src/game-server/postman.h b/src/game-server/postman.h index 8862ccf6..145db9e7 100644 --- a/src/game-server/postman.h +++ b/src/game-server/postman.h @@ -24,12 +24,14 @@ #include <map> #include <string> -class Being; +#include "game-server/character.h" +#include "game-server/entity.h" + class Script; struct PostCallback { - void (*handler)(Being *, + void (*handler)(Entity *, const std::string &sender, const std::string &letter, Script *); @@ -40,9 +42,9 @@ struct PostCallback class PostMan { public: - Being *getCharacter(int id) const + Entity *getCharacter(int id) const { - std::map<int, Being*>::const_iterator itr = mCharacters.find(id); + std::map<int, Entity*>::const_iterator itr = mCharacters.find(id); if (itr != mCharacters.end()) return itr->second; return 0; @@ -52,23 +54,22 @@ public: { int dataBaseId = player->getComponent<CharacterComponent>() ->getDatabaseID(); - std::map<int, Being*>::iterator itr = mCharacters.find(dataBaseId); + std::map<int, Entity*>::iterator itr = mCharacters.find(dataBaseId); if (itr == mCharacters.end()) { - Being *being = static_cast<Being *>(player); - mCharacters.insert(std::pair<int, Being*>(dataBaseId, being)); + mCharacters.insert(std::pair<int, Entity*>(dataBaseId, player)); } } - void getPost(Being *player, PostCallback &f) + void getPost(Entity *player, PostCallback &f) { - mCallbacks.insert(std::pair<Being*, PostCallback>(player, f)); + mCallbacks.insert(std::pair<Entity*, PostCallback>(player, f)); accountHandler->getPost(player); } - void gotPost(Being *player, std::string sender, std::string letter) + void gotPost(Entity *player, std::string sender, std::string letter) { - std::map<Being*, PostCallback>::iterator itr = mCallbacks.find(player); + std::map<Entity*, PostCallback>::iterator itr = mCallbacks.find(player); if (itr != mCallbacks.end()) { itr->second.handler(player, sender, letter, itr->second.script); @@ -76,8 +77,8 @@ public: } private: - std::map<int, Being*> mCharacters; - std::map<Being*, PostCallback> mCallbacks; + std::map<int, Entity*> mCharacters; + std::map<Entity*, PostCallback> mCallbacks; }; extern PostMan *postMan; diff --git a/src/game-server/quest.cpp b/src/game-server/quest.cpp index 803ec60b..515e6200 100644 --- a/src/game-server/quest.cpp +++ b/src/game-server/quest.cpp @@ -36,7 +36,7 @@ typedef std::map< std::string, QuestCallbacks > PendingVariables; struct PendingQuest { - Being *character; + Entity *character; sigc::connection removedConnection; sigc::connection disconnectedConnection; PendingVariables variables; @@ -46,7 +46,7 @@ typedef std::map< int, PendingQuest > PendingQuests; static PendingQuests pendingQuests; -bool getQuestVar(Being *ch, const std::string &name, std::string &value) +bool getQuestVar(Entity *ch, const std::string &name, std::string &value) { std::map< std::string, std::string >::iterator i = ch->getComponent<CharacterComponent>()->questCache.find(name); @@ -56,7 +56,7 @@ bool getQuestVar(Being *ch, const std::string &name, std::string &value) return true; } -void setQuestVar(Being *ch, const std::string &name, +void setQuestVar(Entity *ch, const std::string &name, const std::string &value) { auto *characterComponent = @@ -79,7 +79,7 @@ void setQuestVar(Being *ch, const std::string &name, accountHandler->updateCharacterVar(ch, name, value); } -void QuestRefCallback::triggerCallback(Being *ch, +void QuestRefCallback::triggerCallback(Entity *ch, const std::string &value) const { if (!mRef.isValid()) @@ -120,7 +120,7 @@ static void fullRemove(Entity &ch) pendingQuests.erase(id); } -void recoverQuestVar(Being *ch, const std::string &name, +void recoverQuestVar(Entity *ch, const std::string &name, QuestCallback *f) { auto *characterComponent = @@ -171,7 +171,7 @@ void recoveredQuestVar(int id, return; } - Being *ch = pendingQuest.character; + Entity *ch = pendingQuest.character; auto *characterComponent = ch->getComponent<CharacterComponent>(); characterComponent->questCache[name] = value; diff --git a/src/game-server/quest.h b/src/game-server/quest.h index 13f91cc2..bfc1f874 100644 --- a/src/game-server/quest.h +++ b/src/game-server/quest.h @@ -25,7 +25,7 @@ #include "scripting/scriptmanager.h" -class Being; +class Entity; class Script; @@ -35,14 +35,14 @@ class QuestCallback virtual ~QuestCallback() { } - virtual void triggerCallback(Being *ch, + virtual void triggerCallback(Entity *ch, const std::string &value) const = 0; }; class QuestThreadCallback : public QuestCallback { public: - typedef void (*Handler)(Being *, + typedef void (*Handler)(Entity *, const std::string &value, Script *mScript); @@ -52,7 +52,7 @@ class QuestThreadCallback : public QuestCallback mScript(script) { } - void triggerCallback(Being *ch, const std::string &value) const + void triggerCallback(Entity *ch, const std::string &value) const { mHandler(ch, value, mScript); } private: @@ -67,7 +67,7 @@ class QuestRefCallback : public QuestCallback mQuestName(questName) { script->assignCallback(mRef); } - void triggerCallback(Being *ch, const std::string &value) const; + void triggerCallback(Entity *ch, const std::string &value) const; private: Script::Ref mRef; @@ -78,18 +78,18 @@ class QuestRefCallback : public QuestCallback * Gets the value associated to a quest variable. * @return false if no value was in cache. */ -bool getQuestVar(Being *, const std::string &name, std::string &value); +bool getQuestVar(Entity *, const std::string &name, std::string &value); /** * Sets the value associated to a quest variable. */ -void setQuestVar(Being *, const std::string &name, const std::string &value); +void setQuestVar(Entity *, const std::string &name, const std::string &value); /** * Starts the recovery of a variable and returns immediatly. The callback will * be called once the value has been recovered. */ -void recoverQuestVar(Being *, const std::string &name, QuestCallback *); +void recoverQuestVar(Entity *, const std::string &name, QuestCallback *); /** * Called by the handler of the account server when a value is received. diff --git a/src/game-server/spawnareacomponent.cpp b/src/game-server/spawnareacomponent.cpp index 557a2da5..843371a7 100644 --- a/src/game-server/spawnareacomponent.cpp +++ b/src/game-server/spawnareacomponent.cpp @@ -67,10 +67,12 @@ void SpawnAreaComponent::update(Entity &entity) const int width = mZone.w; const int height = mZone.h; - Being *being = new Being(OBJECT_MONSTER); + Actor *being = new Actor(OBJECT_MONSTER); + auto *beingComponent = new BeingComponent(*being); + being->addComponent(beingComponent); being->addComponent(new MonsterComponent(*being, mSpecy)); - if (being->getModifiedAttribute(ATTR_MAX_HP) <= 0) + if (beingComponent->getModifiedAttribute(ATTR_MAX_HP) <= 0) { LOG_WARN("Refusing to spawn dead monster " << mSpecy->getId()); delete being; @@ -96,7 +98,7 @@ void SpawnAreaComponent::update(Entity &entity) being->setMap(map); being->setPosition(position); - being->clearDestination(); + beingComponent->clearDestination(*being); GameState::enqueueInsert(being); mNumBeings++; diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index f1ed99a0..87213f61 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -79,7 +79,7 @@ static std::map< std::string, std::string > mScriptVariables; /** * Sets message fields describing character look. */ -static void serializeLooks(Being *ch, MessageOut &msg) +static void serializeLooks(Entity *ch, MessageOut &msg) { const EquipData &equipData = ch->getComponent<CharacterComponent>() ->getPossessions().getEquipment(); @@ -128,11 +128,12 @@ static void serializeLooks(Being *ch, MessageOut &msg) /** * Informs a player of what happened around the character. */ -static void informPlayer(MapComposite *map, Being *p) +static void informPlayer(MapComposite *map, Actor *p) { MessageOut moveMsg(GPMSG_BEINGS_MOVE); MessageOut damageMsg(GPMSG_BEINGS_DAMAGE); - const Point &pold = p->getOldPosition(), ppos = p->getPosition(); + const Point &pold = p->getComponent<BeingComponent>()->getOldPosition(); + const Point &ppos = p->getPosition(); int pid = p->getPublicID(), pflags = p->getUpdateFlags(); int visualRange = Configuration::getValue("game_visualRange", 448); @@ -140,9 +141,11 @@ static void informPlayer(MapComposite *map, Being *p) for (BeingIterator it(map->getAroundBeingIterator(p, visualRange)); it; ++it) { - Being *o = *it; + Actor *o = *it; - const Point &oold = o->getOldPosition(), opos = o->getPosition(); + const Point &oold = + o->getComponent<BeingComponent>()->getOldPosition(); + const Point &opos = o->getPosition(); int otype = o->getType(); int oid = o->getPublicID(), oflags = o->getUpdateFlags(); int flags = 0; @@ -166,7 +169,8 @@ static void informPlayer(MapComposite *map, Being *p) { MessageOut AttackMsg(GPMSG_BEING_ATTACK); AttackMsg.writeInt16(oid); - AttackMsg.writeInt8(o->getDirection()); + AttackMsg.writeInt8( + o->getComponent<BeingComponent>()->getDirection()); CombatComponent *combatComponent = o->getComponent<CombatComponent>(); AttackMsg.writeInt8(combatComponent->getAttackId()); @@ -178,7 +182,8 @@ static void informPlayer(MapComposite *map, Being *p) { MessageOut ActionMsg(GPMSG_BEING_ACTION_CHANGE); ActionMsg.writeInt16(oid); - ActionMsg.writeInt8(static_cast< Being * >(o)->getAction()); + ActionMsg.writeInt8( + o->getComponent<BeingComponent>()->getAction()); gameHandler->sendTo(p, ActionMsg); } @@ -192,14 +197,16 @@ static void informPlayer(MapComposite *map, Being *p) o->getComponent<CharacterComponent>(); LooksMsg.writeInt16(characterComponent->getHairStyle()); LooksMsg.writeInt16(characterComponent->getHairColor()); - LooksMsg.writeInt16(o->getGender()); + LooksMsg.writeInt16( + o->getComponent<BeingComponent>()->getGender()); gameHandler->sendTo(p, LooksMsg); } // Send emote messages. if (oflags & UPDATEFLAG_EMOTE) { - int emoteId = o->getLastEmote(); + int emoteId = + o->getComponent<BeingComponent>()->getLastEmote(); if (emoteId > -1) { MessageOut EmoteMsg(GPMSG_BEING_EMOTE); @@ -214,7 +221,8 @@ static void informPlayer(MapComposite *map, Being *p) { MessageOut DirMsg(GPMSG_BEING_DIR_CHANGE); DirMsg.writeInt16(oid); - DirMsg.writeInt8(o->getDirection()); + DirMsg.writeInt8( + o->getComponent<BeingComponent>()->getDirection()); gameHandler->sendTo(p, DirMsg); } @@ -254,18 +262,20 @@ static void informPlayer(MapComposite *map, Being *p) MessageOut enterMsg(GPMSG_BEING_ENTER); enterMsg.writeInt8(otype); enterMsg.writeInt16(oid); - enterMsg.writeInt8(static_cast< Being *>(o)->getAction()); + enterMsg.writeInt8(o->getComponent<BeingComponent>()->getAction()); enterMsg.writeInt16(opos.x); enterMsg.writeInt16(opos.y); - enterMsg.writeInt8(o->getDirection()); - enterMsg.writeInt8(o->getGender()); + enterMsg.writeInt8( + o->getComponent<BeingComponent>()->getDirection()); + enterMsg.writeInt8(o->getComponent<BeingComponent>()->getGender()); switch (otype) { case OBJECT_CHARACTER: { auto *characterComponent = o->getComponent<CharacterComponent>(); - enterMsg.writeString(o->getName()); + enterMsg.writeString( + o->getComponent<BeingComponent>()->getName()); enterMsg.writeInt8(characterComponent->getHairStyle()); enterMsg.writeInt8(characterComponent->getHairColor()); serializeLooks(o, enterMsg); @@ -276,14 +286,17 @@ static void informPlayer(MapComposite *map, Being *p) MonsterComponent *monsterComponent = o->getComponent<MonsterComponent>(); enterMsg.writeInt16(monsterComponent->getSpecy()->getId()); - enterMsg.writeString(o->getName()); + enterMsg.writeString( + o->getComponent<BeingComponent>()->getName()); } break; case OBJECT_NPC: { - NpcComponent *npcComponent = o->getComponent<NpcComponent>(); + NpcComponent *npcComponent = + o->getComponent<NpcComponent>(); enterMsg.writeInt16(npcComponent->getNpcId()); - enterMsg.writeString(o->getName()); + enterMsg.writeString( + o->getComponent<BeingComponent>()->getName()); } break; default: @@ -319,7 +332,8 @@ static void informPlayer(MapComposite *map, Being *p) // to get it within a byte with decimal precision. // For instance, a value of 4.5 will be sent as 45. moveMsg.writeInt8((unsigned short) - (o->getModifiedAttribute(ATTR_MOVE_SPEED_TPS) * 10)); + (o->getComponent<BeingComponent>() + ->getModifiedAttribute(ATTR_MOVE_SPEED_TPS) * 10)); } } @@ -336,7 +350,7 @@ static void informPlayer(MapComposite *map, Being *p) // Inform client about health change of party members for (CharacterIterator i(map->getWholeMapIterator()); i; ++i) { - Being *c = *i; + Actor *c = *i; // Make sure its not the same character if (c == p) @@ -349,10 +363,14 @@ static void informPlayer(MapComposite *map, Being *p) int cflags = c->getUpdateFlags(); if (cflags & UPDATEFLAG_HEALTHCHANGE) { + auto *beingComponent = c->getComponent<BeingComponent>(); + MessageOut healthMsg(GPMSG_BEING_HEALTH_CHANGE); healthMsg.writeInt16(c->getPublicID()); - healthMsg.writeInt16(c->getModifiedAttribute(ATTR_HP)); - healthMsg.writeInt16(c->getModifiedAttribute(ATTR_MAX_HP)); + healthMsg.writeInt16( + beingComponent->getModifiedAttribute(ATTR_HP)); + healthMsg.writeInt16( + beingComponent->getModifiedAttribute(ATTR_MAX_HP)); gameHandler->sendTo(p, healthMsg); } } @@ -363,7 +381,7 @@ static void informPlayer(MapComposite *map, Being *p) for (FixedActorIterator it(map->getAroundBeingIterator(p, visualRange)); it; ++it) { - Actor *o = *it; + Actor *o = static_cast<Actor *>(*it); assert(o->getType() == OBJECT_ITEM || o->getType() == OBJECT_EFFECT); @@ -409,11 +427,12 @@ static void informPlayer(MapComposite *map, Being *p) if (!(oflags & UPDATEFLAG_NEW_ON_MAP)) break; - if (Being *b = e->getBeing()) + if (Entity *b = e->getBeing()) { MessageOut effectMsg(GPMSG_CREATE_EFFECT_BEING); effectMsg.writeInt16(e->getEffectId()); - effectMsg.writeInt16(b->getPublicID()); + effectMsg.writeInt16(static_cast<Actor*>(b) + ->getPublicID()); gameHandler->sendTo(p, effectMsg); } else { MessageOut effectMsg(GPMSG_CREATE_EFFECT_POS); @@ -503,7 +522,7 @@ void GameState::update(int tick) case EVENT_WARP: assert(o->getType() == OBJECT_CHARACTER); - warp(static_cast<Being *>(o), e.map, e.point); + warp(o, e.map, e.point); break; } } @@ -562,7 +581,7 @@ bool GameState::insert(Entity *ptr) case OBJECT_CHARACTER: LOG_DEBUG("Player inserted: " - << static_cast<Being*>(obj)->getName()); + << obj->getComponent<BeingComponent>()->getName()); break; case OBJECT_EFFECT: @@ -639,7 +658,7 @@ void GameState::remove(Entity *ptr) case OBJECT_CHARACTER: LOG_DEBUG("Player removed: " - << static_cast<Being*>(ptr)->getName()); + << ptr->getComponent<BeingComponent>()->getName()); break; case OBJECT_EFFECT: @@ -711,12 +730,15 @@ void GameState::remove(Entity *ptr) map->remove(ptr); } -void GameState::warp(Being *ptr, MapComposite *map, const Point &point) +void GameState::warp(Entity *ptr, MapComposite *map, const Point &point) { remove(ptr); + + Actor *actor = static_cast<Actor *>(ptr); + ptr->setMap(map); - ptr->setPosition(point); - ptr->clearDestination(); + actor->setPosition(point); + ptr->getComponent<BeingComponent>()->clearDestination(*ptr); /* Force update of persistent data on map change, so that characters can respawn at the start of the map after a death or a disconnection. */ @@ -778,7 +800,7 @@ void GameState::enqueueRemove(Actor *ptr) enqueueEvent(ptr, event); } -void GameState::enqueueWarp(Being *ptr, +void GameState::enqueueWarp(Entity *ptr, MapComposite *map, const Point &point) { @@ -794,7 +816,7 @@ void GameState::enqueueWarp(Being *ptr, event.type = EVENT_WARP; event.point = point; event.map = map; - enqueueEvent(ptr, event); + enqueueEvent(static_cast<Actor *>(ptr), event); } void GameState::sayAround(Actor *obj, const std::string &text) diff --git a/src/game-server/state.h b/src/game-server/state.h index 512e9afd..becfd597 100644 --- a/src/game-server/state.h +++ b/src/game-server/state.h @@ -26,7 +26,6 @@ #include <string> class Actor; -class Being; class Entity; class ItemClass; class MapComposite; @@ -71,7 +70,7 @@ namespace GameState * @note No update may be in progress. * @note The character is destroyed, if needed. */ - void warp(Being *, MapComposite *, const Point &point); + void warp(Entity *, MapComposite *, const Point &point); /** * Enqueues an insert event. @@ -90,7 +89,7 @@ namespace GameState * Enqueues a warp event. * @note The event will be executed at end of update. */ - void enqueueWarp(Being *, MapComposite *, const Point &point); + void enqueueWarp(Entity *, MapComposite *, const Point &point); /** * Says something to an actor. diff --git a/src/game-server/statuseffect.cpp b/src/game-server/statuseffect.cpp index b5b988f4..75fae986 100644 --- a/src/game-server/statuseffect.cpp +++ b/src/game-server/statuseffect.cpp @@ -32,14 +32,14 @@ StatusEffect::~StatusEffect() { } -void StatusEffect::tick(Being *target, int count) +void StatusEffect::tick(Entity &target, int count) { if (mTickCallback.isValid()) { Script *s = ScriptManager::currentState(); s->prepare(mTickCallback); - s->push(target); + s->push(&target); s->push(count); - s->execute(target->getMap()); + s->execute(target.getMap()); } } diff --git a/src/game-server/statuseffect.h b/src/game-server/statuseffect.h index 7da5fdf9..3c38944b 100644 --- a/src/game-server/statuseffect.h +++ b/src/game-server/statuseffect.h @@ -23,7 +23,7 @@ #include "scripting/script.h" -class Being; +class Entity; class StatusEffect { @@ -31,7 +31,7 @@ class StatusEffect StatusEffect(int id); ~StatusEffect(); - void tick(Being *target, int count); + void tick(Entity &target, int count); int getId() const { return mId; } diff --git a/src/game-server/trade.cpp b/src/game-server/trade.cpp index 9e9e05fb..1d863137 100644 --- a/src/game-server/trade.cpp +++ b/src/game-server/trade.cpp @@ -37,11 +37,11 @@ * TRADE_AGREE_WAIT : One player has agreed, waiting for the other one */ -Trade::Trade(Being *c1, Being *c2): +Trade::Trade(Entity *c1, Entity *c2): mChar1(c1), mChar2(c2), mMoney1(0), mMoney2(0), mState(TRADE_INIT), mCurrencyId(ATTR_GP) { MessageOut msg(GPMSG_TRADE_REQUEST); - msg.writeInt16(c1->getPublicID()); + msg.writeInt16(static_cast<Actor *>(c1)->getPublicID()); c2->getComponent<CharacterComponent>()->getClient()->send(msg); c1->getComponent<CharacterComponent>()->setTrading(this); c2->getComponent<CharacterComponent>()->setTrading(this); @@ -61,10 +61,11 @@ void Trade::cancel() delete this; } -bool Trade::request(Being *c, int id) +bool Trade::request(Entity *c, int id) { //The trade isn't confirmed, the player which is request is the same. - if (mState != TRADE_INIT || c != mChar2 || mChar1->getPublicID() != id) + if (mState != TRADE_INIT || c != mChar2 || + static_cast<Actor *>(mChar1)->getPublicID() != id) { /* This is not an ack for the current transaction. So assume a new one is about to start and cancel the current one. */ @@ -99,7 +100,7 @@ bool Trade::perform(TradedItems items, Inventory &inv1, Inventory &inv2) return true; } -void Trade::agree(Being *c) +void Trade::agree(Entity *c) { // No player agreed if (mState == TRADE_CONFIRMED) @@ -131,15 +132,23 @@ void Trade::agree(Being *c) // Check if both player has the objects in their inventories // and enouth money, then swap them. Inventory v1(mChar1), v2(mChar2); - if (mChar1->getAttributeBase(mCurrencyId) >= mMoney1 - mMoney2 && - mChar2->getAttributeBase(mCurrencyId) >= mMoney2 - mMoney1 && + + const double moneyChar1 = mChar1->getComponent<BeingComponent>() + ->getAttributeBase(mCurrencyId); + const double moneyChar2 = mChar2->getComponent<BeingComponent>() + ->getAttributeBase(mCurrencyId); + + if (moneyChar1 >= mMoney1 - mMoney2 && + moneyChar2 >= mMoney2 - mMoney1 && perform(mItems1, v1, v2) && perform(mItems2, v2, v1)) { - mChar1->setAttribute(mCurrencyId, mChar1->getAttributeBase(mCurrencyId) - - mMoney1 + mMoney2); - mChar2->setAttribute(mCurrencyId, mChar2->getAttributeBase(mCurrencyId) - - mMoney2 + mMoney1); + mChar1->getComponent<BeingComponent>() + ->setAttribute(*mChar1, mCurrencyId, + moneyChar1 - mMoney1 + mMoney2); + mChar2->getComponent<BeingComponent>() + ->setAttribute(*mChar2, mCurrencyId, + moneyChar2 - mMoney2 + mMoney1); } else { @@ -153,7 +162,7 @@ void Trade::agree(Being *c) delete this; } -void Trade::confirm(Being *c) +void Trade::confirm(Entity *c) { if (mState == TRADE_CONFIRMED || mState == TRADE_AGREE_WAIT) return; @@ -189,7 +198,7 @@ void Trade::confirm(Being *c) mChar2->getComponent<CharacterComponent>()->getClient()->send(msg); } -void Trade::setMoney(Being *c, int amount) +void Trade::setMoney(Entity *c, int amount) { //If the player has already confirmed, exit. if ((mState != TRADE_RUN && (mState != TRADE_CONFIRM_WAIT || c != mChar1)) @@ -218,13 +227,13 @@ void Trade::setMoney(Being *c, int amount) mState = TRADE_RUN; } -void Trade::addItem(Being *c, int slot, int amount) +void Trade::addItem(Entity *c, int slot, int amount) { //If the player has already confirmed, exit. if ((mState != TRADE_RUN && (mState != TRADE_CONFIRM_WAIT || c != mChar1)) || amount < 0) return; - Being *other; + Entity *other; TradedItems *items; if (c == mChar1) { diff --git a/src/game-server/trade.h b/src/game-server/trade.h index be527e86..72ec0b3d 100644 --- a/src/game-server/trade.h +++ b/src/game-server/trade.h @@ -23,7 +23,7 @@ #include <vector> -class Being; +class Entity; class Inventory; class Trade @@ -34,7 +34,7 @@ class Trade * Sets up a trade between two characters. * Asks for an acknowledgment from the second one. */ - Trade(Being *, Being *); + Trade(Entity *, Entity *); /** * Cancels a trade by a given character (optional). @@ -49,27 +49,27 @@ class Trade * otherwise. * @return true if the current trade keeps going. */ - bool request(Being *, int); + bool request(Entity *, int); /** * Confirm the trade. */ - void confirm(Being *); + void confirm(Entity *); /* * Agree to complete the trade */ - void agree(Being *c); + void agree(Entity *c); /** * Adds some items to the trade. */ - void addItem(Being *, int slot, int amount); + void addItem(Entity *, int slot, int amount); /** * Adds some money to the trade. */ - void setMoney(Being *, int amount); + void setMoney(Entity *, int amount); private: @@ -98,7 +98,7 @@ class Trade static bool perform(TradedItems items, Inventory &inv1, Inventory &inv2); - Being *mChar1, *mChar2; /**< Characters involved. */ + Entity *mChar1, *mChar2; /**< Characters involved. */ TradedItems mItems1, mItems2; /**< Traded items. */ int mMoney1, mMoney2; /**< Traded money. */ TradeState mState; /**< State of transaction. */ diff --git a/src/game-server/triggerareacomponent.cpp b/src/game-server/triggerareacomponent.cpp index d4736e4e..224b17f9 100644 --- a/src/game-server/triggerareacomponent.cpp +++ b/src/game-server/triggerareacomponent.cpp @@ -34,7 +34,7 @@ void WarpAction::process(Actor *obj) { if (obj->getType() == OBJECT_CHARACTER) { - GameState::enqueueWarp(static_cast<Being *>(obj), mMap, mTargetPoint); + GameState::enqueueWarp(obj, mMap, mTargetPoint); } } |