summaryrefslogtreecommitdiff
path: root/src/game-server/monster.cpp
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-04-10 23:04:42 +0200
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-04-11 13:47:17 +0200
commitd7fa7ea64f6bb0bc0b097e4bf1ceb4bd9620d0da (patch)
tree1b4d200ba66c6afcdb7763951980476756339cf1 /src/game-server/monster.cpp
parentaa04597c5f8bb806996d604699fc8ebff6d53bdd (diff)
downloadmanaserv-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/monster.cpp')
-rw-r--r--src/game-server/monster.cpp100
1 files changed, 54 insertions, 46 deletions
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);