From 585a33e221a7ee392791f4fd5a5ec9214b8fe868 Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Sat, 30 Mar 2013 09:29:08 +0100 Subject: Moved fighting code into a component All damage dealing is now handeled via CombatComponent. Monsters use a derived MonsterCombatComponent since they can have a damage mutation and have a seperate script callback. The wirering with Being is still not optional since most of the stuff does not exist as components. Things done: - Seperated the fighting code from Being and only let Characters and Monsters add the Component (less overhead for npcs) - Added a getter for Attribute values to prevent searching it all the time in non Being members - Fixed the type if the damage mutation to double (no idea why it was int) I did not want to copy it over incorrectly - Removed the addAttack/removeAttack overrides in Character and made the knuckleAttack being added based on newly added signals Future TODOS: - Remove depedency on Being as soon all needed dependencies are available as components of Entity - Move the monster script callback into the general combatcomponent and make it usuable for characters too --- src/game-server/character.cpp | 55 ++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'src/game-server/character.cpp') diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index 8cc0e65a..994e3311 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -22,8 +22,10 @@ #include "common/configuration.h" #include "game-server/accountconnection.h" +#include "game-server/attack.h" #include "game-server/attributemanager.h" #include "game-server/buysell.h" +#include "game-server/combatcomponent.h" #include "game-server/inventory.h" #include "game-server/item.h" #include "game-server/itemmanager.h" @@ -99,14 +101,13 @@ Character::Character(MessageIn &msg): setWalkMask(Map::BLOCKMASK_WALL); setBlockType(BLOCKTYPE_CHARACTER); - // Get character data. - mDatabaseID = msg.readInt32(); - setName(msg.readString()); - deserializeCharacterData(*this, msg); - mOld = getPosition(); - Inventory(this).initialize(); - modifiedAllAttribute(); - setSize(16); + + CombatComponent *combatcomponent = new CombatComponent(*this); + addComponent(combatcomponent); + combatcomponent->getAttacks().attack_added.connect( + sigc::mem_fun(this, &Character::attackAdded)); + combatcomponent->getAttacks().attack_removed.connect( + sigc::mem_fun(this, &Character::attackRemoved)); // Default knuckle attack int damageBase = this->getModifiedAttribute(ATTR_STR); @@ -121,7 +122,16 @@ Character::Character(MessageIn &msg): knuckleDamage.range = DEFAULT_TILE_LENGTH; mKnuckleAttackInfo = new AttackInfo(0, knuckleDamage, 7, 3, 0); - addAttack(mKnuckleAttackInfo); + combatcomponent->addAttack(mKnuckleAttackInfo); + + // Get character data. + mDatabaseID = msg.readInt32(); + setName(msg.readString()); + deserializeCharacterData(*this, msg); + mOld = getPosition(); + Inventory(this).initialize(); + modifiedAllAttribute(); + setSize(16); } Character::~Character() @@ -199,7 +209,7 @@ void Character::respawn() // Make it alive again setAction(STAND); // Reset target - mTarget = NULL; + getComponent()->clearTarget(); // Execute respawn callback when set if (executeCallback(mDeathAcceptedCallback, this)) @@ -422,7 +432,7 @@ void Character::sendStatus() { int attr = *i; attribMsg.writeInt16(attr); - attribMsg.writeInt32(getAttribute(attr) * 256); + attribMsg.writeInt32(getAttributeBase(attr) * 256); attribMsg.writeInt32(getModifiedAttribute(attr) * 256); } if (attribMsg.getLength() > 2) gameHandler->sendTo(this, attribMsg); @@ -497,7 +507,7 @@ void Character::flagAttribute(int attr) { // Inform the client of this attribute modification. accountHandler->updateAttributes(getDatabaseID(), attr, - getAttribute(attr), + getAttributeBase(attr), getModifiedAttribute(attr)); mModifiedAttributes.insert(attr); } @@ -655,7 +665,7 @@ AttribmodResponseCode Character::useCharacterPoint(size_t attribute) return ATTRIBMOD_NO_POINTS_LEFT; --mCharacterPoints; - setAttribute(attribute, getAttribute(attribute) + 1); + setAttribute(attribute, getAttributeBase(attribute) + 1); updateDerivedAttributes(attribute); return ATTRIBMOD_OK; } @@ -666,12 +676,12 @@ AttribmodResponseCode Character::useCorrectionPoint(size_t attribute) return ATTRIBMOD_INVALID_ATTRIBUTE; if (!mCorrectionPoints) return ATTRIBMOD_NO_POINTS_LEFT; - if (getAttribute(attribute) <= 1) + if (getAttributeBase(attribute) <= 1) return ATTRIBMOD_DENIED; --mCorrectionPoints; ++mCharacterPoints; - setAttribute(attribute, getAttribute(attribute) - 1); + setAttribute(attribute, getAttributeBase(attribute) - 1); updateDerivedAttributes(attribute); return ATTRIBMOD_OK; } @@ -704,19 +714,20 @@ void Character::resumeNpcThread() } } -void Character::addAttack(AttackInfo *attackInfo) +void Character::attackAdded(Attack &attack) { // Remove knuckle attack - Being::addAttack(attackInfo); - Being::removeAttack(mKnuckleAttackInfo); + if (attack.getAttackInfo() != mKnuckleAttackInfo) + getComponent()->removeAttack(mKnuckleAttackInfo); } -void Character::removeAttack(AttackInfo *attackInfo) +void Character::attackRemoved(Attack &attack) { - Being::removeAttack(attackInfo); // Add knuckle attack - if (mAttacks.getNumber() == 0) - Being::addAttack(mKnuckleAttackInfo); + CombatComponent *combatComponent = getComponent(); + // 1 since the attack is not really removed yet. + if (combatComponent->getAttacks().getNumber() == 1) + combatComponent->addAttack(mKnuckleAttackInfo); } void Character::disconnected() -- cgit v1.2.3-60-g2f50