summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-11-02 22:46:30 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2010-11-02 22:46:30 +0100
commitbfdc59942a39661eac1a72e80f25fe9e039e4e50 (patch)
treec87a4d79d483197192407c46b4585c61946c7ddf
parent63ff01f1d53f2c362beb60fdba664efe19593131 (diff)
downloadmanaserv-bfdc59942a39661eac1a72e80f25fe9e039e4e50.tar.gz
manaserv-bfdc59942a39661eac1a72e80f25fe9e039e4e50.tar.bz2
manaserv-bfdc59942a39661eac1a72e80f25fe9e039e4e50.tar.xz
manaserv-bfdc59942a39661eac1a72e80f25fe9e039e4e50.zip
Some random code cleanups.
Also renamed MonsterClass::getType() to getId() since it was misleading with the use of ThingType::getType() and didn't represent the actual meaning, IMHO. Trivial.
-rw-r--r--src/game-server/monster.cpp104
-rw-r--r--src/game-server/monster.hpp10
-rw-r--r--src/game-server/spawnarea.cpp23
-rw-r--r--src/game-server/state.cpp47
4 files changed, 112 insertions, 72 deletions
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp
index 4df000ed..56391aa5 100644
--- a/src/game-server/monster.cpp
+++ b/src/game-server/monster.cpp
@@ -53,9 +53,9 @@ struct MonsterTargetEventDispatch: EventDispatch
{
MonsterTargetEventDispatch()
{
- typedef EventListenerFactory< Monster, &Monster::mTargetListener > Factory;
+ typedef EventListenerFactory<Monster, &Monster::mTargetListener> Factory;
removed = &Factory::create< Thing, &Monster::forgetTarget >::function;
- died = &Factory::create< Thing, &Monster::forgetTarget, Being >::function;
+ died = &Factory::create<Thing, &Monster::forgetTarget, Being>::function;
}
};
@@ -69,20 +69,20 @@ Monster::Monster(MonsterClass *specy):
mOwner(NULL),
mCurrentAttack(NULL)
{
- LOG_DEBUG("Monster spawned!");
+ LOG_DEBUG("Monster spawned! (id: " << mSpecy->getId() << ").");
/*
* Initialise the attribute structures.
*/
-
- const AttributeScopes &mobAttr = attributeManager->getAttributeInfoForType(ATTR_MOB);
+ const AttributeScopes &mobAttr = attributeManager->getAttributeInfoForType(
+ ATTR_MOB);
for (AttributeScopes::const_iterator it = mobAttr.begin(),
- it_end = mobAttr.end();
- it != it_end;
- ++it)
+ it_end = mobAttr.end(); it != it_end; ++it)
+ {
mAttributes.insert(std::pair< unsigned int, Attribute >
(it->first, Attribute(*it->second)));
+ }
/*
* Set the attributes to the values defined by the associated monster
@@ -92,9 +92,7 @@ Monster::Monster(MonsterClass *specy):
int mutation = specy->getMutation();
for (AttributeMap::iterator it2 = mAttributes.begin(),
- it2_end = mAttributes.end();
- it2 != it2_end;
- ++it2)
+ it2_end = mAttributes.end(); it2 != it2_end; ++it2)
{
double attr = 0.0f;
@@ -107,7 +105,9 @@ Monster::Monster(MonsterClass *specy):
attr);
}
- setAttribute(ATTR_MOVE_SPEED_RAW, utils::tpsToSpeed(getAttribute(ATTR_MOVE_SPEED_TPS))); // Put in tiles per second.
+ // Set the speed in tiles per second.
+ setAttribute(ATTR_MOVE_SPEED_RAW,
+ utils::tpsToSpeed(getAttribute(ATTR_MOVE_SPEED_TPS)));
setSize(specy->getSize());
// Set positions relative to target from which the monster can attack
@@ -117,7 +117,7 @@ Monster::Monster(MonsterClass *specy):
mAttackPositions.push_back(AttackPosition(0, -dist, DIRECTION_DOWN));
mAttackPositions.push_back(AttackPosition(0, dist, DIRECTION_UP));
- //load default script
+ // Load default script
loadScript(specy->getScript());
}
@@ -134,7 +134,7 @@ Monster::~Monster()
i->first->removeListener(&mTargetListener);
}
- // free map position
+ // Free the map position
if (getMap())
{
Point oldP = getPosition();
@@ -155,7 +155,8 @@ void Monster::perform()
{
if (!isTimerRunning(T_M_ATTACK_TIME))
{
- setTimerHard(T_M_ATTACK_TIME, mCurrentAttack->aftDelay + mCurrentAttack->preDelay);
+ setTimerHard(T_M_ATTACK_TIME, mCurrentAttack->aftDelay
+ + mCurrentAttack->preDelay);
Damage dmg(getModifiedAttribute(MOB_ATTR_PHY_ATK_MIN) *
mCurrentAttack->damageFactor,
getModifiedAttribute(MOB_ATTR_PHY_ATK_DELTA) *
@@ -165,7 +166,8 @@ void Monster::perform()
mCurrentAttack->type,
mCurrentAttack->range);
- int hit = performAttack(mTarget, mCurrentAttack->range, dmg);
+ int hit = performAttack(mTarget, mCurrentAttack->range,
+ dmg);
if (! mCurrentAttack->scriptFunction.empty()
&& mScript
@@ -180,8 +182,11 @@ void Monster::perform()
}
}
}
- } else
+ }
+ else
+ {
setAction(STAND);
+ }
}
}
@@ -194,7 +199,7 @@ void Monster::update()
mOwner = NULL;
}
- // If dead do nothing but rot
+ // If dead, remove it
if (mAction == DEAD)
{
if (!isTimerRunning(T_M_DECAY))
@@ -212,7 +217,8 @@ void Monster::update()
}
// Cancel the rest when we are currently performing an attack
- if (isTimerRunning(T_M_ATTACK_TIME)) return;
+ if (isTimerRunning(T_M_ATTACK_TIME))
+ return;
// Check potential attack positions
Being *bestAttackTarget = mTarget = NULL;
@@ -222,15 +228,18 @@ void Monster::update()
// Iterate through objects nearby
int aroundArea = Configuration::getValue("game_visualRange", 448);
- for (BeingIterator i(getMap()->getAroundBeingIterator(this, aroundArea)); i; ++i)
+ for (BeingIterator i(getMap()->getAroundBeingIterator(this, aroundArea));
+ i; ++i)
{
// We only want to attack player characters
- if ((*i)->getType() != OBJECT_CHARACTER) continue;
+ if ((*i)->getType() != OBJECT_CHARACTER)
+ continue;
Being *target = static_cast<Being *> (*i);
// Dead characters are ignored
- if (target->getAction() == DEAD) continue;
+ if (target->getAction() == DEAD)
+ continue;
// Determine how much we hate the target
int targetPriority = 0;
@@ -251,8 +260,7 @@ void Monster::update()
// Check all attack positions
for (std::list<AttackPosition>::iterator j = mAttackPositions.begin();
- j != mAttackPositions.end();
- j++)
+ j != mAttackPositions.end(); j++)
{
Point attackPosition = (*i)->getPosition();
attackPosition.x += (*j).x;
@@ -273,7 +281,7 @@ void Monster::update()
// Check if an enemy has been found
if (bestAttackTarget)
{
- //check which attacks have a chance to hit the target
+ // Check which attacks have a chance to hit the target
MonsterAttacks allAttacks = mSpecy->getAttacks();
std::map<int, MonsterAttack *> workingAttacks;
int prioritySum = 0;
@@ -282,29 +290,34 @@ void Monster::update()
i != allAttacks.end();
i++)
{
- int distx = this->getPosition().x - bestAttackTarget->getPosition().x;
- int disty = this->getPosition().y - bestAttackTarget->getPosition().y;
+ int distx = this->getPosition().x
+ - bestAttackTarget->getPosition().x;
+ int disty = this->getPosition().y
+ - bestAttackTarget->getPosition().y;
int distSquare = (distx * distx + disty * disty);
int maxDist = (*i)->range + bestAttackTarget->getSize();
+
if (maxDist * maxDist >= distSquare)
{
prioritySum += (*i)->priority;
workingAttacks[prioritySum] = (*i);
}
}
+
if (workingAttacks.empty() || !prioritySum)
{ //when no attack can hit move closer to attack position
setDestination(bestAttackPosition);
}
else
{
- //prepare for using a random attack which can hit the enemy
- //stop movement
+ // Prepare for using a random attack which can hit the enemy
+ // Stop movement
setDestination(getPosition());
- //turn into direction of enemy
+ // Turn into direction of enemy
setDirection(bestAttackDirection);
- //perform a random attack based on priority
- mCurrentAttack = workingAttacks.upper_bound(rand()%prioritySum)->second;
+ // Perform a random attack based on priority
+ mCurrentAttack =
+ workingAttacks.upper_bound(rand()%prioritySum)->second;
setAction(ATTACK);
raiseUpdateFlags(UPDATEFLAG_ATTACK);
}
@@ -319,8 +332,10 @@ void Monster::update()
unsigned range = mSpecy->getStrollRange();
if (range)
{
- Point randomPos(rand() % (range * 2 + 1) - range + getPosition().x,
- rand() % (range * 2 + 1) - range + getPosition().y);
+ Point randomPos(rand() % (range * 2 + 1)
+ - range + getPosition().x,
+ rand() % (range * 2 + 1)
+ - range + getPosition().y);
setDestination(randomPos);
}
setTimerHard(T_M_STROLL, 10 + rand() % 10);
@@ -340,13 +355,16 @@ void Monster::loadScript(const std::string &scriptName)
std::stringstream filename;
filename << "scripts/monster/" << scriptName;
- if (ResourceManager::exists(filename.str())) // file exists!
+ if (ResourceManager::exists(filename.str()))
{
LOG_INFO("Loading monster script: " << filename.str());
mScript = Script::create("lua");
mScript->loadFile(filename.str());
- } else {
- LOG_WARN("Could not find script file \"" << filename.str() << "\" for monster");
+ }
+ else
+ {
+ LOG_WARN("Could not find script file \""
+ << filename.str() << "\" for monster");
}
}
@@ -422,6 +440,7 @@ int Monster::damage(Actor *source, const Damage &damage)
{
changeAnger(source, HPLoss);
}
+
if (HPLoss && source && source->getType() == OBJECT_CHARACTER)
{
Character *s = static_cast< Character * >(source);
@@ -472,23 +491,24 @@ void Monster::died()
float expPerChar = (float)mSpecy->getExp() / mExpReceivers.size();
- for (iChar = mExpReceivers.begin(); iChar != mExpReceivers.end(); iChar++)
+ for (iChar = mExpReceivers.begin(); iChar != mExpReceivers.end();
+ iChar++)
{
Character *character = (*iChar).first;
std::set<size_t> *skillSet = &(*iChar).second;
if (mLegalExpReceivers.find(character) == mLegalExpReceivers.end()
|| skillSet->size() < 1)
- {
continue;
- }
+
int expPerSkill = int(expPerChar / skillSet->size());
- for (iSkill = skillSet->begin(); iSkill != skillSet->end(); iSkill++)
+ for (iSkill = skillSet->begin(); iSkill != skillSet->end();
+ iSkill++)
{
character->receiveExperience(*iSkill, expPerSkill,
mSpecy->getOptimalLevel());
}
- character->incrementKillCount(mSpecy->getType());
+ character->incrementKillCount(mSpecy->getId());
}
}
}
diff --git a/src/game-server/monster.hpp b/src/game-server/monster.hpp
index b25e9c17..2c4d59e2 100644
--- a/src/game-server/monster.hpp
+++ b/src/game-server/monster.hpp
@@ -69,7 +69,7 @@ class MonsterClass
{
public:
MonsterClass(int id):
- mID(id),
+ mId(id),
mSpeed(1),
mSize(16),
mExp(-1),
@@ -83,10 +83,10 @@ class MonsterClass
{}
/**
- * Returns monster type. This is the ID of the monster class.
+ * Returns monster type. This is the Id of the monster class.
*/
- int getType() const
- { return mID; }
+ int getId() const
+ { return mId; }
/**
* Sets monster drops. These are the items the monster drops when it
@@ -186,7 +186,7 @@ class MonsterClass
ItemClass *getRandomDrop() const;
private:
- unsigned short mID;
+ unsigned short mId;
MonsterDrops mDrops;
std::map<int, double> mAttributes; /**< Base attributes of the monster. */
float mSpeed; /**< The monster class speed in tiles per second */
diff --git a/src/game-server/spawnarea.cpp b/src/game-server/spawnarea.cpp
index 19e665ad..105523cf 100644
--- a/src/game-server/spawnarea.cpp
+++ b/src/game-server/spawnarea.cpp
@@ -84,20 +84,24 @@ void SpawnArea::update()
if (being->getModifiedAttribute(ATTR_MAX_HP) <= 0)
{
- //LOG_WARN("Refusing to spawn dead monster " << mSpecy->getType());
+ LOG_WARN("Refusing to spawn dead monster " << mSpecy->getId());
delete being;
being = 0;
}
- if (being) {
- do {
+ if (being)
+ {
+ do
+ {
position = Point(x + rand() % width, y + rand() % height);
c--;
- } while (!realMap->getWalk(position.x / realMap->getTileWidth(),
- position.y / realMap->getTileHeight(),
- being->getWalkMask()) && c);
+ }
+ while (!realMap->getWalk(position.x / realMap->getTileWidth(),
+ position.y / realMap->getTileHeight(),
+ being->getWalkMask()) && c);
- if (c) {
+ if (c)
+ {
being->addListener(&mSpawnedListener);
being->setMap(map);
being->setPosition(position);
@@ -106,9 +110,10 @@ void SpawnArea::update()
mNumBeings++;
}
- else {
+ else
+ {
LOG_WARN("Unable to find a free spawn location for monster "
- << mSpecy->getType() << " on map " << map->getName()
+ << mSpecy->getId() << " on map " << map->getName()
<< " (" << x << ',' << y << ','
<< width << ',' << height << ')');
delete being;
diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp
index 6d0f68bc..2c7a5755 100644
--- a/src/game-server/state.cpp
+++ b/src/game-server/state.cpp
@@ -285,7 +285,7 @@ static void informPlayer(MapComposite *map, Character *p)
case OBJECT_MONSTER:
{
Monster *q = static_cast< Monster * >(o);
- enterMsg.writeShort(q->getSpecy()->getType());
+ enterMsg.writeShort(q->getSpecy()->getId());
enterMsg.writeString(q->getName());
} break;
@@ -555,7 +555,8 @@ bool GameState::insert(Thing *ptr)
switch (obj->getType())
{
case OBJECT_ITEM:
- LOG_DEBUG("Item inserted: " << static_cast<Item*>(obj)->getItemClass()->getDatabaseID());
+ LOG_DEBUG("Item inserted: "
+ << static_cast<Item*>(obj)->getItemClass()->getDatabaseID());
break;
case OBJECT_NPC:
@@ -563,15 +564,18 @@ bool GameState::insert(Thing *ptr)
break;
case OBJECT_CHARACTER:
- LOG_DEBUG("Player inserted: " << static_cast<Being*>(obj)->getName());
+ LOG_DEBUG("Player inserted: "
+ << static_cast<Being*>(obj)->getName());
break;
case OBJECT_EFFECT:
- LOG_DEBUG("Effect inserted: " << static_cast<Effect*>(obj)->getEffectId());
+ LOG_DEBUG("Effect inserted: "
+ << static_cast<Effect*>(obj)->getEffectId());
break;
case OBJECT_MONSTER:
- LOG_DEBUG("Monster inserted: " << static_cast<Monster*>(obj)->getSpecy()->getType());
+ LOG_DEBUG("Monster inserted: "
+ << static_cast<Monster*>(obj)->getSpecy()->getId());
break;
case OBJECT_ACTOR:
@@ -619,7 +623,8 @@ void GameState::remove(Thing *ptr)
switch (ptr->getType())
{
case OBJECT_ITEM:
- LOG_DEBUG("Item removed: " << static_cast<Item*>(ptr)->getItemClass()->getDatabaseID());
+ LOG_DEBUG("Item removed: "
+ << static_cast<Item*>(ptr)->getItemClass()->getDatabaseID());
break;
case OBJECT_NPC:
@@ -627,15 +632,18 @@ void GameState::remove(Thing *ptr)
break;
case OBJECT_CHARACTER:
- LOG_DEBUG("Player removed: " << static_cast<Being*>(ptr)->getName());
+ LOG_DEBUG("Player removed: "
+ << static_cast<Being*>(ptr)->getName());
break;
case OBJECT_EFFECT:
- LOG_DEBUG("Effect removed: " << static_cast<Effect*>(ptr)->getEffectId());
+ LOG_DEBUG("Effect removed: "
+ << static_cast<Effect*>(ptr)->getEffectId());
break;
case OBJECT_MONSTER:
- LOG_DEBUG("Monster removed: " << static_cast<Monster*>(ptr)->getSpecy()->getType());
+ LOG_DEBUG("Monster removed: "
+ << static_cast<Monster*>(ptr)->getSpecy()->getId());
break;
case OBJECT_ACTOR:
@@ -660,9 +668,11 @@ void GameState::remove(Thing *ptr)
msg.writeShort(obj->getPublicID());
Point objectPos = obj->getPosition();
- for (CharacterIterator p(map->getAroundActorIterator(obj, visualRange)); p; ++p)
+ for (CharacterIterator p(map->getAroundActorIterator(obj, visualRange));
+ p; ++p)
{
- if (*p != obj && objectPos.inRangeOf((*p)->getPosition(), visualRange))
+ if (*p != obj && objectPos.inRangeOf((*p)->getPosition(),
+ visualRange))
{
gameHandler->sendTo(*p, msg);
}
@@ -770,11 +780,16 @@ void GameState::sayTo(Actor *destination, Actor *source, const std::string &text
return; //only characters will read it anyway
MessageOut msg(GPMSG_SAY);
- if (source == NULL) {
+ if (source == NULL)
+ {
msg.writeShort(0);
- } else if (!source->canMove()) {
+ }
+ else if (!source->canMove())
+ {
msg.writeShort(65535);
- } else {
+ }
+ else
+ {
msg.writeShort(static_cast< Actor * >(source)->getPublicID());
}
msg.writeString(text);
@@ -786,10 +801,10 @@ void GameState::sayToAll(const std::string &text)
{
MessageOut msg(GPMSG_SAY);
- // message will show as from server
+ // The message will be shown as if it was from the server
msg.writeShort(0);
msg.writeString(text);
- // sends to everyone connected to the game server
+ // Sends it to everyone connected to the game server
gameHandler->sendToEveryone(msg);
}