diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/game-server/character.h | 1 | ||||
-rw-r--r-- | src/game-server/component.h | 49 | ||||
-rw-r--r-- | src/game-server/entity.cpp | 22 | ||||
-rw-r--r-- | src/game-server/entity.h | 51 | ||||
-rw-r--r-- | src/game-server/mapcomposite.cpp | 22 | ||||
-rw-r--r-- | src/game-server/monster.h | 3 | ||||
-rw-r--r-- | src/game-server/spawnareacomponent.cpp (renamed from src/game-server/spawnarea.cpp) | 31 | ||||
-rw-r--r-- | src/game-server/spawnareacomponent.h (renamed from src/game-server/spawnarea.h) | 22 | ||||
-rw-r--r-- | src/game-server/triggerareacomponent.cpp (renamed from src/game-server/trigger.cpp) | 13 | ||||
-rw-r--r-- | src/game-server/triggerareacomponent.h (renamed from src/game-server/trigger.h) | 24 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 12 |
12 files changed, 198 insertions, 61 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 629df5ee..17a27892 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -215,6 +215,7 @@ SET(SRCS_MANASERVGAME game-server/command.cpp game-server/commandhandler.cpp game-server/commandhandler.h + game-server/component.h game-server/effect.h game-server/effect.cpp game-server/emotemanager.h @@ -248,8 +249,8 @@ SET(SRCS_MANASERVGAME game-server/quest.cpp game-server/skillmanager.h game-server/skillmanager.cpp - game-server/spawnarea.h - game-server/spawnarea.cpp + game-server/spawnareacomponent.h + game-server/spawnareacomponent.cpp game-server/specialmanager.cpp game-server/specialmanager.h game-server/state.h @@ -262,8 +263,8 @@ SET(SRCS_MANASERVGAME game-server/timeout.cpp game-server/trade.h game-server/trade.cpp - game-server/trigger.h - game-server/trigger.cpp + game-server/triggerareacomponent.h + game-server/triggerareacomponent.cpp scripting/script.h scripting/script.cpp scripting/scriptmanager.h diff --git a/src/game-server/character.h b/src/game-server/character.h index 9c9a5459..2b755bdb 100644 --- a/src/game-server/character.h +++ b/src/game-server/character.h @@ -33,6 +33,7 @@ #include "utils/logger.h" #include <map> +#include <set> #include <string> #include <vector> diff --git a/src/game-server/component.h b/src/game-server/component.h new file mode 100644 index 00000000..a0f85d2a --- /dev/null +++ b/src/game-server/component.h @@ -0,0 +1,49 @@ +/* + * The Mana Server + * Copyright (C) 2012 The Mana Developers + * + * This file is part of The Mana Server. + * + * The Mana Server is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef COMPONENT_H +#define COMPONENT_H + +class Entity; + +enum ComponentType +{ + TriggerArea, + SpawnArea, + + ComponentTypeCount +}; + +/** + * A component of an entity. + */ +class Component +{ + public: + virtual ~Component() {} + + /** + * Updates the internal status. The \a entity is the owner of this + * component. + */ + virtual void update(Entity &entity) = 0; +}; + +#endif // COMPONENT_H diff --git a/src/game-server/entity.cpp b/src/game-server/entity.cpp index 6cb61e58..3593d902 100644 --- a/src/game-server/entity.cpp +++ b/src/game-server/entity.cpp @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2007-2010 The Mana World Development Team + * Copyright (C) 2012 The Mana Developers * * This file is part of The Mana Server. * @@ -19,3 +20,24 @@ */ #include "game-server/entity.h" + +Entity::Entity(EntityType type, MapComposite *map) : + mMap(map), + mType(type) +{ + for (int i = 0; i < ComponentTypeCount; ++i) + mComponents[i] = 0; +} + +Entity::~Entity() +{ + for (int i = 0; i < ComponentTypeCount; ++i) + delete mComponents[i]; +} + +void Entity::update() +{ + for (int i = 0; i < ComponentTypeCount; ++i) + if (mComponents[i]) + mComponents[i]->update(*this); +} diff --git a/src/game-server/entity.h b/src/game-server/entity.h index 91f13699..7aed29e6 100644 --- a/src/game-server/entity.h +++ b/src/game-server/entity.h @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2004-2010 The Mana World Development Team + * Copyright (C) 2012 The Mana Developers * * This file is part of The Mana Server. * @@ -23,28 +24,29 @@ #include "common/manaserv_protocol.h" -#include <set> +#include "game-server/component.h" #include <sigc++/signal.h> #include <sigc++/trackable.h> +#include <cassert> + using namespace ManaServ; class MapComposite; /** - * Base class for in-game objects. Knows only its type and the map it resides - * on. Provides listeners. + * Base class for in-game objects. + * + * Knows its type, the map it resides on and is host to a number of optional + * components. */ class Entity : public sigc::trackable { public: - Entity(EntityType type, MapComposite *map = 0) - : mMap(map), - mType(type) - {} + Entity(EntityType type, MapComposite *map = 0); - virtual ~Entity() {} + virtual ~Entity(); /** * Gets type of this entity. @@ -55,6 +57,32 @@ class Entity : public sigc::trackable { return mType; } /** + * Adds a component. Only one component of a given type can be added. + * Entity takes ownership of \a component. + */ + template <class T> + void addComponent(T *component) + { + assert(!mComponents[T::type]); + mComponents[T::type] = component; + } + + /** + * Returns the component of the given type, or 0 when no such component + * was set. + */ + Component *getComponent(ComponentType type) const + { return mComponents[type]; } + + /** + * Get a component by its class. Avoids the need for doing a static- + * cast in the calling code. + */ + template <class T> + T *getComponent() const + { return static_cast<T*>(getComponent(T::type)); } + + /** * Returns whether this entity is visible on the map or not. (Actor) */ bool isVisible() const @@ -74,9 +102,10 @@ class Entity : public sigc::trackable { return mType == OBJECT_CHARACTER || mType == OBJECT_MONSTER; } /** - * Updates the internal status. + * Updates the internal status. By default, calls update on all its + * components. */ - virtual void update() = 0; + virtual void update(); /** * Gets the map this entity is located on. @@ -96,6 +125,8 @@ class Entity : public sigc::trackable private: MapComposite *mMap; /**< Map the entity is on */ EntityType mType; /**< Type of this entity. */ + + Component *mComponents[ComponentTypeCount]; }; #endif // ENTITY_H diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index 99d2734f..a1a3f989 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -1,7 +1,7 @@ /* * The Mana Server * Copyright (C) 2006-2010 The Mana World Development Team - * Copyright (C) 2010-2011 The Mana Development Team + * Copyright (C) 2010-2012 The Mana Development Team * * This file is part of The Mana Server. * @@ -31,8 +31,8 @@ #include "game-server/mapmanager.h" #include "game-server/mapreader.h" #include "game-server/monstermanager.h" -#include "game-server/spawnarea.h" -#include "game-server/trigger.h" +#include "game-server/spawnareacomponent.h" +#include "game-server/triggerareacomponent.h" #include "scripting/script.h" #include "scripting/scriptmanager.h" #include "utils/logger.h" @@ -800,9 +800,12 @@ void MapComposite::initializeContent() if (destMap && destX && destY) { + Entity *entity = new Entity(OBJECT_OTHER, this); WarpAction *action = new WarpAction(destMap, destX, destY); - insert(new TriggerArea(this, object->getBounds(), - action, false)); + entity->addComponent( + new TriggerAreaComponent(object->getBounds(), + action, false)); + insert(entity); } else { @@ -838,8 +841,13 @@ void MapComposite::initializeContent() if (monster && maxBeings && spawnRate) { - insert(new SpawnArea(this, monster, object->getBounds(), - maxBeings, spawnRate)); + Entity *entity = new Entity(OBJECT_OTHER, this); + SpawnAreaComponent *spawnArea = + new SpawnAreaComponent(monster, object->getBounds(), + maxBeings, spawnRate); + + entity->addComponent(spawnArea); + insert(entity); } } else if (utils::compareStrI(type, "NPC") == 0) diff --git a/src/game-server/monster.h b/src/game-server/monster.h index 6bf06d2b..8e927f92 100644 --- a/src/game-server/monster.h +++ b/src/game-server/monster.h @@ -27,8 +27,9 @@ #include "utils/string.h" #include <map> -#include <vector> +#include <set> #include <string> +#include <vector> #include <sigc++/connection.h> diff --git a/src/game-server/spawnarea.cpp b/src/game-server/spawnareacomponent.cpp index 73dca6b2..d71c2502 100644 --- a/src/game-server/spawnarea.cpp +++ b/src/game-server/spawnareacomponent.cpp @@ -18,19 +18,19 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#include "game-server/spawnarea.h" +#include "game-server/spawnareacomponent.h" #include "game-server/mapcomposite.h" #include "game-server/monster.h" #include "game-server/state.h" #include "utils/logger.h" -SpawnArea::SpawnArea(MapComposite *map, - MonsterClass *specy, - const Rectangle &zone, - int maxBeings, - int spawnRate): - Entity(OBJECT_OTHER, map), +const ComponentType SpawnAreaComponent::type; + +SpawnAreaComponent::SpawnAreaComponent(MonsterClass *specy, + const Rectangle &zone, + int maxBeings, + int spawnRate): mSpecy(specy), mZone(zone), mMaxBeings(maxBeings), @@ -40,14 +40,14 @@ SpawnArea::SpawnArea(MapComposite *map, { } -void SpawnArea::update() +void SpawnAreaComponent::update(Entity &entity) { if (mNextSpawn > 0) mNextSpawn--; if (mNextSpawn == 0 && mNumBeings < mMaxBeings && mSpawnRate > 0) { - MapComposite *map = getMap(); + MapComposite *map = entity.getMap(); const Map *realMap = map->getMap(); // Reset the spawn area to the whole map in case of dimensionless zone @@ -60,7 +60,7 @@ void SpawnArea::update() } // Find a free spawn location. Give up after 10 tries - int c = 10; + int triesLeft = 10; Point position; const int x = mZone.x; const int y = mZone.y; @@ -81,16 +81,17 @@ void SpawnArea::update() do { position = Point(x + rand() % width, y + rand() % height); - c--; + triesLeft--; } while (!realMap->getWalk(position.x / realMap->getTileWidth(), position.y / realMap->getTileHeight(), - being->getWalkMask()) && c); + being->getWalkMask()) + && triesLeft); - if (c) + if (triesLeft) { being->signal_removed.connect( - sigc::mem_fun(this, &SpawnArea::decrease)); + sigc::mem_fun(this, &SpawnAreaComponent::decrease)); being->setMap(map); being->setPosition(position); @@ -114,7 +115,7 @@ void SpawnArea::update() } } -void SpawnArea::decrease(Entity *) +void SpawnAreaComponent::decrease(Entity *) { --mNumBeings; } diff --git a/src/game-server/spawnarea.h b/src/game-server/spawnareacomponent.h index 628c072e..52399a64 100644 --- a/src/game-server/spawnarea.h +++ b/src/game-server/spawnareacomponent.h @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2006-2010 The Mana World Development Team + * Copyright (C) 2012 The Mana Developers * * This file is part of The Mana Server. * @@ -18,26 +19,29 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef SPAWNAREA_H -#define SPAWNAREA_H +#ifndef SPAWNAREACOMPONENT_H +#define SPAWNAREACOMPONENT_H + +#include "game-server/component.h" -#include "game-server/entity.h" #include "utils/point.h" -class Being; class MonsterClass; /** * A spawn area, where monsters spawn. The area is a rectangular field and will * spawn a certain number of a given monster type. */ -class SpawnArea : public Entity +class SpawnAreaComponent : public Component { public: - SpawnArea(MapComposite *, MonsterClass *, const Rectangle &zone, - int maxBeings, int spawnRate); + static const ComponentType type = SpawnArea; + + SpawnAreaComponent(MonsterClass *, + const Rectangle &zone, + int maxBeings, int spawnRate); - void update(); + void update(Entity &entity); /** * Keeps track of the number of spawned being. @@ -55,4 +59,4 @@ class SpawnArea : public Entity friend struct SpawnAreaEventDispatch; }; -#endif +#endif // SPAWNAREACOMPONENT_H diff --git a/src/game-server/trigger.cpp b/src/game-server/triggerareacomponent.cpp index 10e50ecb..ebfe8580 100644 --- a/src/game-server/trigger.cpp +++ b/src/game-server/triggerareacomponent.cpp @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2006-2010 The Mana World Development Team + * Copyright (C) 2012 The Mana Developers * * This file is part of The Mana Server. * @@ -18,7 +19,7 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#include "game-server/trigger.h" +#include "game-server/triggerareacomponent.h" #include "game-server/character.h" #include "game-server/mapcomposite.h" @@ -56,12 +57,16 @@ void ScriptAction::process(Actor *obj) mScript->execute(obj->getMap()); } -void TriggerArea::update() +const ComponentType TriggerAreaComponent::type; + +void TriggerAreaComponent::update(Entity &entity) { + MapComposite *map = entity.getMap(); std::set<Actor*> insideNow; - for (BeingIterator i(getMap()->getInsideRectangleIterator(mZone)); i; ++i) + + for (BeingIterator i(map->getInsideRectangleIterator(mZone)); i; ++i) { - // Don't deal with unitialized actors. + // Don't deal with uninitialized actors if (!(*i) || !(*i)->isPublicIdValid()) continue; diff --git a/src/game-server/trigger.h b/src/game-server/triggerareacomponent.h index fbd41fa8..4fa6e334 100644 --- a/src/game-server/trigger.h +++ b/src/game-server/triggerareacomponent.h @@ -1,6 +1,7 @@ /* * The Mana Server * Copyright (C) 2006-2010 The Mana World Development Team + * Copyright (C) 2012 The Mana Developers * * This file is part of The Mana Server. * @@ -18,13 +19,15 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef TRIGGER_H -#define TRIGGER_H +#ifndef TRIGGERAREACOMPONENT_H +#define TRIGGERAREACOMPONENT_H #include "game-server/entity.h" #include "scripting/script.h" #include "utils/point.h" +#include <set> + class Actor; class TriggerAction @@ -60,16 +63,23 @@ class ScriptAction : public TriggerAction int mArg; // Argument passed to script function (meaning is function-specific) }; -class TriggerArea : public Entity +class TriggerAreaComponent : public Component { public: + static const ComponentType type = TriggerArea; + /** * Creates a rectangular trigger for a given map. */ - TriggerArea(MapComposite *m, const Rectangle &r, TriggerAction *ptr, bool once) - : Entity(OBJECT_OTHER, m), mZone(r), mAction(ptr), mOnce(once) {} + TriggerAreaComponent(const Rectangle &r, + TriggerAction *ptr, + bool once) : + mZone(r), + mAction(ptr), + mOnce(once) + {} - virtual void update(); + virtual void update(Entity &entity); private: Rectangle mZone; @@ -78,4 +88,4 @@ class TriggerArea : public Entity std::set<Actor *> mInside; }; -#endif +#endif // TRIGGERAREACOMPONENT_H diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 838ced6d..dfc1bb0a 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -1,7 +1,7 @@ /* * The Mana Server * Copyright (C) 2007-2010 The Mana World Development Team - * Copyright (C) 2010 The Mana Developers + * Copyright (C) 2010-2013 The Mana Developers * * This file is part of The Mana Server. * @@ -49,7 +49,7 @@ extern "C" { #include "game-server/state.h" #include "game-server/statuseffect.h" #include "game-server/statusmanager.h" -#include "game-server/trigger.h" +#include "game-server/triggerareacomponent.h" #include "net/messageout.h" #include "scripting/luautil.h" #include "scripting/luascript.h" @@ -407,14 +407,18 @@ static int trigger_create(lua_State *s) script->assignCallback(function); lua_pop(s, 1); + Entity *triggerEntity = new Entity(OBJECT_OTHER, m); + ScriptAction *action = new ScriptAction(script, function, id); Rectangle r = { x, y, width, height }; - TriggerArea *area = new TriggerArea(m, r, action, once); + TriggerAreaComponent *area = new TriggerAreaComponent(r, action, once); + + triggerEntity->addComponent(area); LOG_INFO("Created script trigger at " << x << "," << y << " (" << width << "x" << height << ") id: " << id); - bool ret = GameState::insert(area); + bool ret = GameState::insertOrDelete(triggerEntity); lua_pushboolean(s, ret); return 1; } |