summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-26 20:04:36 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-26 20:04:49 +0200
commit2f68a9da7121ab2adaa70ff257577ee596b4c4ee (patch)
treed858a2eb16f2a89a856015a15396b87b835d12cd
parent53b547b11fb3c34bd6e2ae0bae0090f2677ef529 (diff)
downloadmanaserv-2f68a9da7121ab2adaa70ff257577ee596b4c4ee.tar.gz
manaserv-2f68a9da7121ab2adaa70ff257577ee596b4c4ee.tar.bz2
manaserv-2f68a9da7121ab2adaa70ff257577ee596b4c4ee.tar.xz
manaserv-2f68a9da7121ab2adaa70ff257577ee596b4c4ee.zip
Some general cleanups and merged Being::perform() into update()
The main change here is to remove the separate calling of 'perform' on all beings, and rather rely on the beings to do whatever they were doing in that function when the virtual 'update' function is called. Reviewed-by: Yohann Ferreira
-rw-r--r--src/dal/sqlitedataprovider.cpp14
-rw-r--r--src/dal/sqlitedataprovider.h14
-rw-r--r--src/game-server/being.cpp8
-rw-r--r--src/game-server/being.h7
-rw-r--r--src/game-server/character.cpp16
-rw-r--r--src/game-server/character.h13
-rw-r--r--src/game-server/mapcomposite.cpp6
-rw-r--r--src/game-server/monster.cpp164
-rw-r--r--src/game-server/monster.h4
9 files changed, 117 insertions, 129 deletions
diff --git a/src/dal/sqlitedataprovider.cpp b/src/dal/sqlitedataprovider.cpp
index cc698f61..8054f1f5 100644
--- a/src/dal/sqlitedataprovider.cpp
+++ b/src/dal/sqlitedataprovider.cpp
@@ -20,11 +20,19 @@
#include "sqlitedataprovider.h"
-#include <stdexcept>
-
#include "dalexcept.h"
-#include "../utils/logger.h"
+#include "common/configuration.h"
+#include "utils/logger.h"
+
+#include <stdexcept>
+#include <limits.h>
+
+// sqlite3_int64 is the preferred new datatype for 64-bit int values.
+// see: http://www.sqlite.org/capi3ref.html#sqlite3_int64
+#ifndef sqlite3_int64
+typedef sqlite_int64 sqlite3_int64;
+#endif
namespace dal
{
diff --git a/src/dal/sqlitedataprovider.h b/src/dal/sqlitedataprovider.h
index e948f009..107290b2 100644
--- a/src/dal/sqlitedataprovider.h
+++ b/src/dal/sqlitedataprovider.h
@@ -21,20 +21,10 @@
#ifndef SQLITE_DATA_PROVIDER_H
#define SQLITE_DATA_PROVIDER_H
+#include "dataprovider.h"
+
#include <iosfwd>
-#include "limits.h"
#include <sqlite3.h>
-#include "common/configuration.h"
-
-
-// sqlite3_int64 is the preferred new datatype for 64-bit int values.
-// see: http://www.sqlite.org/capi3ref.html#sqlite3_int64
-#ifndef sqlite3_int64
-typedef sqlite_int64 sqlite3_int64;
-#endif
-
-
-#include "dataprovider.h"
namespace dal
{
diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp
index 3fc0bab1..1669e55d 100644
--- a/src/game-server/being.cpp
+++ b/src/game-server/being.cpp
@@ -180,7 +180,8 @@ void Being::died()
{
const EventListener &l = **i;
++i; // In case the listener removes itself from the list on the fly.
- if (l.dispatch->died) l.dispatch->died(&l, this);
+ if (l.dispatch->died)
+ l.dispatch->died(&l, this);
}
}
@@ -432,7 +433,7 @@ int Being::performAttack(Being *target, const Damage &damage)
// Note: The auto-attack system will handle the delay between two attacks.
- return (mTarget->damage(this, damage));
+ return target->damage(this, damage);
}
void Being::setAction(BeingAction action)
@@ -640,7 +641,8 @@ void Being::update()
//update timers
for (Timers::iterator i = mTimers.begin(); i != mTimers.end(); i++)
{
- if (i->second > -1) i->second--;
+ if (i->second > -1)
+ i->second--;
}
int oldHP = getModifiedAttribute(ATTR_HP);
diff --git a/src/game-server/being.h b/src/game-server/being.h
index c7739cc5..3a76025a 100644
--- a/src/game-server/being.h
+++ b/src/game-server/being.h
@@ -73,7 +73,7 @@ class Being : public Actor
Being(ThingType type);
/**
- * Cleans obsolete attribute modifiers.
+ * Update being state.
*/
virtual void update();
@@ -96,11 +96,6 @@ class Being : public Actor
virtual void died();
/**
- * Performs actions scheduled by the being.
- */
- virtual void perform() {}
-
- /**
* Gets the destination coordinates of the being.
*/
const Point &getDestination() const
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 031cf87c..1a46ce55 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -18,11 +18,6 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <algorithm>
-#include <cassert>
-#include <cmath>
-#include <limits.h>
-
#include "game-server/character.h"
#include "common/configuration.h"
@@ -47,6 +42,11 @@
#include "utils/logger.h"
+#include <algorithm>
+#include <cassert>
+#include <cmath>
+#include <limits.h>
+
// Experience curve related values
const float Character::EXPCURVE_EXPONENT = 3.0f;
const float Character::EXPCURVE_FACTOR = 10.0f;
@@ -174,15 +174,17 @@ void Character::update()
mStatusEffects[it->first] = it->second.time;
it++;
}
+
+ processAttacks();
}
-void Character::perform()
+void Character::processAttacks()
{
// Ticks attacks even when not attacking to permit cooldowns and warmups.
std::list<AutoAttack> attacksReady;
mAutoAttacks.tick(&attacksReady);
- if (mAction != ATTACK || mTarget == NULL)
+ if (mAction != ATTACK || !mTarget)
{
mAutoAttacks.stop();
return;
diff --git a/src/game-server/character.h b/src/game-server/character.h
index f54d4ec4..eb8e432f 100644
--- a/src/game-server/character.h
+++ b/src/game-server/character.h
@@ -21,10 +21,6 @@
#ifndef CHARACTER_H
#define CHARACTER_H
-#include <map>
-#include <string>
-#include <vector>
-
#include "common/defines.h"
#include "common/inventorydata.h"
#include "common/manaserv_protocol.h"
@@ -32,6 +28,10 @@
#include "scripting/script.h"
#include "utils/logger.h"
+#include <map>
+#include <string>
+#include <vector>
+
class BuySell;
class GameClient;
class MessageIn;
@@ -69,10 +69,7 @@ class Character : public Being
*/
void update();
- /**
- * Perform actions.
- */
- void perform();
+ void processAttacks();
/**
* Executes the global die script and calls the base class function
diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp
index 7d377d18..2c601183 100644
--- a/src/game-server/mapcomposite.cpp
+++ b/src/game-server/mapcomposite.cpp
@@ -625,12 +625,6 @@ void MapComposite::update()
s->execute();
}
- // Perform actions
- for (BeingIterator it(getWholeMapIterator()); it; ++it)
- {
- (*it)->perform();
- }
-
// Move objects around and update zones.
for (BeingIterator it(getWholeMapIterator()); it; ++it)
{
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp
index 0bee4c9a..6bc69b7e 100644
--- a/src/game-server/monster.cpp
+++ b/src/game-server/monster.cpp
@@ -118,71 +118,19 @@ Monster::~Monster()
}
}
-void Monster::perform()
-{
- if (mAction == ATTACK)
- {
- if (mTarget)
- {
- if (mCurrentAttack)
- {
- if (!isTimerRunning(T_M_ATTACK_TIME))
- {
- setTimerHard(T_M_ATTACK_TIME, mCurrentAttack->aftDelay
- + mCurrentAttack->preDelay);
- Damage dmg;
- dmg.skill = 0;
- dmg.base = getModifiedAttribute(MOB_ATTR_PHY_ATK_MIN) *
- mCurrentAttack->damageFactor;
- dmg.delta = getModifiedAttribute(MOB_ATTR_PHY_ATK_DELTA) *
- mCurrentAttack->damageFactor;
- dmg.cth = getModifiedAttribute(ATTR_ACCURACY);
- dmg.element = mCurrentAttack->element;
- dmg.range = mCurrentAttack->range;
-
- int hit = performAttack(mTarget, dmg);
-
- if (! mCurrentAttack->scriptEvent.empty()
- && hit > -1)
- {
- Script::Ref function = mSpecy->getEventCallback(mCurrentAttack->scriptEvent);
- if (function.isValid())
- {
- Script *script = ScriptManager::currentState();
- script->setMap(getMap());
- script->prepare(function);
- script->push(this);
- script->push(mTarget);
- script->push(hit);
- script->execute();
- }
- }
- }
- }
- }
- else
- {
- setAction(STAND);
- }
- }
-}
-
void Monster::update()
{
Being::update();
if (isTimerJustFinished(T_M_KILLSTEAL_PROTECTED))
- {
mOwner = NULL;
- }
// If dead, remove it
if (mAction == DEAD)
{
if (!isTimerRunning(T_M_DECAY))
- {
GameState::enqueueRemove(this);
- }
+
return;
}
@@ -199,6 +147,38 @@ void Monster::update()
if (isTimerRunning(T_M_ATTACK_TIME))
return;
+ refreshTarget();
+
+ if (!mTarget)
+ {
+ // We have no target - let's wander around
+ if (!isTimerRunning(T_M_STROLL) && getPosition() == getDestination())
+ {
+ if (!isTimerRunning(T_M_KILLSTEAL_PROTECTED))
+ {
+ unsigned range = mSpecy->getStrollRange();
+ if (range)
+ {
+ Point randomPos(rand() % (range * 2 + 1)
+ - range + getPosition().x,
+ rand() % (range * 2 + 1)
+ - range + getPosition().y);
+ // Don't allow negative destinations, to avoid rounding
+ // problems when divided by tile size
+ if (randomPos.x >= 0 && randomPos.y >= 0)
+ setDestination(randomPos);
+ }
+ setTimerHard(T_M_STROLL, 10 + rand() % 10);
+ }
+ }
+ }
+
+ if (mAction == ATTACK)
+ processAttack();
+}
+
+void Monster::refreshTarget()
+{
// Check potential attack positions
Being *bestAttackTarget = mTarget = NULL;
int bestTargetPriority = 0;
@@ -214,7 +194,7 @@ void Monster::update()
if ((*i)->getType() != OBJECT_CHARACTER)
continue;
- Being *target = static_cast<Being *> (*i);
+ Being *target = static_cast<Being *>(*i);
// Dead characters are ignored
if (target->getAction() == DEAD)
@@ -241,9 +221,9 @@ void Monster::update()
for (std::list<AttackPosition>::iterator j = mAttackPositions.begin();
j != mAttackPositions.end(); j++)
{
- Point attackPosition = (*i)->getPosition();
- attackPosition.x += (*j).x;
- attackPosition.y += (*j).y;
+ Point attackPosition = target->getPosition();
+ attackPosition.x += j->x;
+ attackPosition.y += j->y;
int posPriority = calculatePositionPriority(attackPosition,
targetPriority);
@@ -252,7 +232,7 @@ void Monster::update()
bestAttackTarget = mTarget = target;
bestTargetPriority = posPriority;
bestAttackPosition = attackPosition;
- bestAttackDirection = (*j).direction;
+ bestAttackDirection = j->direction;
}
}
}
@@ -265,16 +245,15 @@ void Monster::update()
std::map<int, MonsterAttack *> workingAttacks;
int prioritySum = 0;
+ const int distX = getPosition().x - bestAttackTarget->getPosition().x;
+ const int distY = getPosition().y - bestAttackTarget->getPosition().y;
+ const int distSquare = (distX * distX + distY * distY);
+
for (MonsterAttacks::iterator i = allAttacks.begin();
i != allAttacks.end();
i++)
{
- 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();
+ int maxDist = (*i)->range + bestAttackTarget->getSize();
if (maxDist * maxDist >= distSquare)
{
@@ -301,27 +280,46 @@ void Monster::update()
raiseUpdateFlags(UPDATEFLAG_ATTACK);
}
}
- else
+}
+
+void Monster::processAttack()
+{
+ if (!mTarget)
{
- // We have no target - let's wander around
- if (!isTimerRunning(T_M_STROLL) && getPosition() == getDestination())
+ setAction(STAND);
+ return;
+ }
+
+ if (!mCurrentAttack)
+ return;
+
+ setTimerHard(T_M_ATTACK_TIME, mCurrentAttack->aftDelay
+ + mCurrentAttack->preDelay);
+
+ float damageFactor = mCurrentAttack->damageFactor;
+
+ Damage dmg;
+ dmg.skill = 0;
+ dmg.base = getModifiedAttribute(MOB_ATTR_PHY_ATK_MIN) * damageFactor;
+ dmg.delta = getModifiedAttribute(MOB_ATTR_PHY_ATK_DELTA) * damageFactor;
+ dmg.cth = getModifiedAttribute(ATTR_ACCURACY);
+ dmg.element = mCurrentAttack->element;
+ dmg.range = mCurrentAttack->range;
+
+ int hit = performAttack(mTarget, dmg);
+
+ if (!mCurrentAttack->scriptEvent.empty() && hit > -1)
+ {
+ Script::Ref function = mSpecy->getEventCallback(mCurrentAttack->scriptEvent);
+ if (function.isValid())
{
- if (!isTimerRunning(T_M_KILLSTEAL_PROTECTED))
- {
- unsigned range = mSpecy->getStrollRange();
- if (range)
- {
- Point randomPos(rand() % (range * 2 + 1)
- - range + getPosition().x,
- rand() % (range * 2 + 1)
- - range + getPosition().y);
- // Don't allow negative destinations, to avoid rounding
- // problems when divided by tile size
- if (randomPos.x >= 0 && randomPos.y >= 0)
- setDestination(randomPos);
- }
- setTimerHard(T_M_STROLL, 10 + rand() % 10);
- }
+ Script *script = ScriptManager::currentState();
+ script->setMap(getMap());
+ script->prepare(function);
+ script->push(this);
+ script->push(mTarget);
+ script->push(hit);
+ script->execute();
}
}
}
diff --git a/src/game-server/monster.h b/src/game-server/monster.h
index e53903f9..6de83685 100644
--- a/src/game-server/monster.h
+++ b/src/game-server/monster.h
@@ -286,10 +286,12 @@ class Monster : public Being
*/
void update();
+ void refreshTarget();
+
/**
* Performs an attack, if needed.
*/
- void perform();
+ void processAttack();
/**
* Loads a script file for this monster