summaryrefslogtreecommitdiffstats
path: root/src/game-server
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-18 22:24:38 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2013-03-25 20:32:36 +0100
commit7aee56f062989c8901322a09b2da40bb028eb222 (patch)
tree2c3712c3926121d35b0ef569e1eeed8f42e34b32 /src/game-server
parent8ebd7ef2c200009e6d22b2cfaa3dd0d849155db6 (diff)
downloadmanaserv-7aee56f062989c8901322a09b2da40bb028eb222.tar.gz
manaserv-7aee56f062989c8901322a09b2da40bb028eb222.tar.xz
manaserv-7aee56f062989c8901322a09b2da40bb028eb222.zip
Changed Item to a component of Actor
Items also have positions, so the ItemComponent only makes sense as part of an Actor. Later on it will probably be part of an entity that also has an ActorComponent. Since it was annoying to update all the places where items were created, I've introduced a function for this. The component types are now prefixed with "CT_" because I wanted to introduce an 'Item' namespace which would otherwise be conflicting. The component types enum isn't used much in the code so it can look a bit ugly. Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/game-server')
-rw-r--r--src/game-server/accountconnection.cpp9
-rw-r--r--src/game-server/command.cpp9
-rw-r--r--src/game-server/commandhandler.cpp24
-rw-r--r--src/game-server/component.h5
-rw-r--r--src/game-server/gamehandler.cpp31
-rw-r--r--src/game-server/item.cpp27
-rw-r--r--src/game-server/item.h31
-rw-r--r--src/game-server/main-game.cpp2
-rw-r--r--src/game-server/monster.cpp13
-rw-r--r--src/game-server/spawnareacomponent.h2
-rw-r--r--src/game-server/state.cpp19
-rw-r--r--src/game-server/state.h24
-rw-r--r--src/game-server/triggerareacomponent.h2
13 files changed, 124 insertions, 74 deletions
diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp
index 8b517f5..44858fc 100644
--- a/src/game-server/accountconnection.cpp
+++ b/src/game-server/accountconnection.cpp
@@ -187,16 +187,15 @@ void AccountConnection::processMessage(MessageIn &msg)
if (ItemClass *ic = itemManager->getItem(itemId))
{
- Item *item = new Item(ic, amount);
- item->setMap(m);
- Point dst(posX, posY);
- item->setPosition(dst);
+ Entity *item = Item::create(m,
+ Point(posX, posY),
+ ic, amount);
if (!GameState::insertOrDelete(item))
{
// The map is full.
LOG_WARN("Couldn't add floor item(s) " << itemId
- << " into map " << mapId);
+ << " into map " << mapId);
return;
}
}
diff --git a/src/game-server/command.cpp b/src/game-server/command.cpp
index e80b9f9..f475f85 100644
--- a/src/game-server/command.cpp
+++ b/src/game-server/command.cpp
@@ -191,11 +191,12 @@ static void money(Character *, Character *q, int nb)
}
*/
-static void drop(Character *from, ItemClass *it, int nb)
+static void drop(Character *from, ItemClass *itemClass, int amount)
{
- Item *item = new Item(it, nb);
- item->setMap(from->getMap());
- item->setPosition(from->getPosition());
+ Entity *item = Item::create(from->getMap(),
+ from->getPosition(),
+ itemClass, amount);
+
GameState::insertOrDelete(item);
}
diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp
index 6de2224..ad6a1bc 100644
--- a/src/game-server/commandhandler.cpp
+++ b/src/game-server/commandhandler.cpp
@@ -568,11 +568,11 @@ static void handleItem(Character *player, std::string &args)
static void handleDrop(Character *player, std::string &args)
{
ItemClass *ic;
- int value = 0;
+ int amount = 0;
// get arguments
std::string itemclass = getArgument(args);
- std::string valuestr = getArgument(args);
+ std::string amountstr = getArgument(args);
// check all arguments are there
if (itemclass.empty())
@@ -598,26 +598,26 @@ static void handleDrop(Character *player, std::string &args)
return;
}
- //identify the amount
- if (valuestr.empty())
+ // identify the amount
+ if (amountstr.empty())
{
- value = 1;
+ amount = 1;
}
- else if (utils::isNumeric(valuestr))
+ else if (utils::isNumeric(amountstr))
{
- value = utils::stringToInt(valuestr);
+ amount = utils::stringToInt(amountstr);
}
// check for valid amount
- if (value <= 0)
+ if (amount <= 0)
{
say("Invalid number of items", player);
return;
}
- // create the integer and put it on the map
- Item *item = new Item(ic, value);
- item->setMap(player->getMap());
- item->setPosition(player->getPosition());
+ Entity *item = Item::create(player->getMap(),
+ player->getPosition(),
+ ic, amount);
+
GameState::insertOrDelete(item);
// log transaction
diff --git a/src/game-server/component.h b/src/game-server/component.h
index a0f85d2..fd477b9 100644
--- a/src/game-server/component.h
+++ b/src/game-server/component.h
@@ -25,8 +25,9 @@ class Entity;
enum ComponentType
{
- TriggerArea,
- SpawnArea,
+ CT_TriggerArea,
+ CT_SpawnArea,
+ CT_Item,
ComponentTypeCount
};
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index 3b356a5..eeccdcc 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -488,15 +488,17 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message)
{
Actor *o = *i;
Point opos = o->getPosition();
+
if (o->getType() == OBJECT_ITEM && opos.x == x && opos.y == y)
{
- Item *item = static_cast< Item * >(o);
+ ItemComponent *item = o->getComponent<ItemComponent>();
ItemClass *ic = item->getItemClass();
int amount = item->getAmount();
+
if (!Inventory(client.character).insert(ic->getDatabaseID(),
- amount))
+ amount))
{
- GameState::remove(item);
+ GameState::remove(o);
// We only do this when items are to be kept in memory
// between two server restart.
@@ -504,8 +506,8 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message)
{
// Remove the floor item from map
accountHandler->removeFloorItems(map->getID(),
- ic->getDatabaseID(),
- amount, x, y);
+ ic->getDatabaseID(),
+ amount, x, y);
}
// log transaction
@@ -556,33 +558,32 @@ void GameHandler::handleDrop(GameClient &client, MessageIn &message)
if (ItemClass *ic = itemManager->getItem(inv.getItem(slot)))
{
int nb = inv.removeFromSlot(slot, amount);
- Item *item = new Item(ic, amount - nb);
- item->setMap(client.character->getMap());
- item->setPosition(client.character->getPosition());
- if (!GameState::insert(item))
+ MapComposite *map = client.character->getMap();
+ Point pos = client.character->getPosition();
+
+ Entity *item = Item::create(map, pos, ic, amount - nb);
+
+ if (!GameState::insertOrDelete(item))
{
// The map is full. Put back into inventory.
inv.insert(ic->getDatabaseID(), amount - nb);
- delete item;
return;
}
- Point pt = client.character->getPosition();
-
// We store the item in database only when the floor items are meant
// to be persistent between two server restarts.
if (!Configuration::getValue("game_floorItemDecayTime", 0))
{
// Create the floor item on map
accountHandler->createFloorItems(client.character->getMap()->getID(),
- ic->getDatabaseID(),
- amount, pt.x, pt.y);
+ ic->getDatabaseID(),
+ amount, pos.x, pos.y);
}
// log transaction
std::stringstream str;
str << "User dropped item " << ic->getDatabaseID()
- << " at " << pt.x << "x" << pt.y;
+ << " at " << pos.x << "x" << pos.y;
accountHandler->sendTransaction(client.character->getDatabaseID(),
TRANS_ITEM_DROP, str.str());
}
diff --git a/src/game-server/item.cpp b/src/game-server/item.cpp
index c6c9d11..4402930 100644
--- a/src/game-server/item.cpp
+++ b/src/game-server/item.cpp
@@ -1,6 +1,7 @@
/*
* The Mana Server
* Copyright (C) 2004-2010 The Mana World Development Team
+ * Copyright (C) 2010-2012 The Mana Developers
*
* This file is part of The Mana Server.
*
@@ -150,19 +151,37 @@ void ItemClass::addAttack(AttackInfo *attackInfo,
addEffect(new ItemEffectAttack(attackInfo), applyTrigger, dispellTrigger);
}
+const ComponentType ItemComponent::type;
-Item::Item(ItemClass *type, int amount)
- : Actor(OBJECT_ITEM), mType(type), mAmount(amount)
+ItemComponent::ItemComponent(ItemClass *type, int amount) :
+ mType(type),
+ mAmount(amount)
{
mLifetime = Configuration::getValue("game_floorItemDecayTime", 0) * 10;
}
-void Item::update()
+void ItemComponent::update(Entity &entity)
{
if (mLifetime)
{
mLifetime--;
if (!mLifetime)
- GameState::enqueueRemove(this);
+ GameState::enqueueRemove(static_cast<Actor*>(&entity));
}
}
+
+namespace Item {
+
+Actor *create(MapComposite *map,
+ Point pos,
+ ItemClass *itemClass,
+ int amount)
+{
+ Actor *itemActor = new Actor(OBJECT_ITEM);
+ itemActor->addComponent(new ItemComponent(itemClass, amount));
+ itemActor->setMap(map);
+ itemActor->setPosition(pos);
+ return itemActor;
+}
+
+} // namespace Item
diff --git a/src/game-server/item.h b/src/game-server/item.h
index 54112c4..2ce858c 100644
--- a/src/game-server/item.h
+++ b/src/game-server/item.h
@@ -1,6 +1,7 @@
/*
* The Mana Server
* Copyright (C) 2004-2010 The Mana World Development Team
+ * Copyright (C) 2010-2012 The Mana Developers
*
* This file is part of The Mana Server.
*
@@ -29,6 +30,7 @@
class Being;
class ItemClass;
+class MapComposite;
// Indicates the equip slot "cost" to equip an item.
struct ItemEquipRequirement {
@@ -294,12 +296,14 @@ class ItemClass
};
/**
- * Class for an item stack laying on the floor in the game world
+ * An item stack lying on the floor in the game world.
*/
-class Item : public Actor
+class ItemComponent : public Component
{
public:
- Item(ItemClass *type, int amount);
+ static const ComponentType type = CT_Item;
+
+ ItemComponent(ItemClass *type, int amount);
ItemClass *getItemClass() const
{ return mType; }
@@ -307,7 +311,7 @@ class Item : public Actor
int getAmount() const
{ return mAmount; }
- virtual void update();
+ virtual void update(Entity &entity);
private:
ItemClass *mType;
@@ -315,4 +319,23 @@ class Item : public Actor
int mLifetime;
};
+namespace Item {
+
+/**
+ * @brief Creates an item actor.
+ *
+ * The returned actor should be inserted into the game state, usually with
+ * either GameState::insertOrDelete or GameState::enqueueInsert.
+ *
+ * @param map the map of the item
+ * @param pos the position of the item
+ * @param itemClass the class of the item
+ * @param amount the amount of items on the stack
+ *
+ * @return the created item
+ */
+Actor *create(MapComposite *map, Point pos, ItemClass *itemClass, int amount);
+
+} // namespace Item
+
#endif // ITEM_H
diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp
index 2533a16..3a12cdb 100644
--- a/src/game-server/main-game.cpp
+++ b/src/game-server/main-game.cpp
@@ -1,7 +1,7 @@
/*
* The Mana Server
* Copyright (C) 2004-2010 The Mana World Development Team
- * Copyright (C) 2010 The Mana Developers
+ * Copyright (C) 2010-2012 The Mana Developers
*
* This file is part of The Mana Server.
*
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp
index 3899c79..edc89ec 100644
--- a/src/game-server/monster.cpp
+++ b/src/game-server/monster.cpp
@@ -414,7 +414,8 @@ int Monster::damage(Actor *source, const Damage &damage)
void Monster::died()
{
- if (mAction == DEAD) return;
+ if (mAction == DEAD)
+ return;
Being::died();
mDecayTimeout.set(DECAY_TIME);
@@ -426,11 +427,13 @@ void Monster::died()
for (unsigned i = 0; i < size; i++)
{
const int p = rand() / (RAND_MAX / 10000);
- if (p <= mSpecy->mDrops[i].probability)
+ const MonsterDrop &drop = mSpecy->mDrops[i];
+
+ if (p <= drop.probability)
{
- Item *item = new Item(mSpecy->mDrops[i].item, 1);
- item->setMap(getMap());
- item->setPosition(getPosition());
+ Actor *item = Item::create(getMap(),
+ getPosition(),
+ drop.item, 1);
GameState::enqueueInsert(item);
}
}
diff --git a/src/game-server/spawnareacomponent.h b/src/game-server/spawnareacomponent.h
index 52399a6..0465844 100644
--- a/src/game-server/spawnareacomponent.h
+++ b/src/game-server/spawnareacomponent.h
@@ -35,7 +35,7 @@ class MonsterClass;
class SpawnAreaComponent : public Component
{
public:
- static const ComponentType type = SpawnArea;
+ static const ComponentType type = CT_SpawnArea;
SpawnAreaComponent(MonsterClass *,
const Rectangle &zone,
diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp
index 6fdfb6f..d84f35b 100644
--- a/src/game-server/state.cpp
+++ b/src/game-server/state.cpp
@@ -37,7 +37,6 @@
#include "scripting/script.h"
#include "scripting/scriptmanager.h"
#include "utils/logger.h"
-#include "utils/point.h"
#include "utils/speedconv.h"
#include <cassert>
@@ -372,20 +371,22 @@ static void informPlayer(MapComposite *map, Character *p)
{
case OBJECT_ITEM:
{
- Item *o = static_cast< Item * >(*it);
+ ItemComponent *item = o->getComponent<ItemComponent>();
+ ItemClass *itemClass = item->getItemClass();
+
if (oflags & UPDATEFLAG_NEW_ON_MAP)
{
/* Send a specific message to the client when an item appears
out of nowhere, so that a sound/animation can be performed. */
MessageOut appearMsg(GPMSG_ITEM_APPEAR);
- appearMsg.writeInt16(o->getItemClass()->getDatabaseID());
+ appearMsg.writeInt16(itemClass->getDatabaseID());
appearMsg.writeInt16(opos.x);
appearMsg.writeInt16(opos.y);
gameHandler->sendTo(p, appearMsg);
}
else
{
- itemMsg.writeInt16(willBeInRange ? o->getItemClass()->getDatabaseID() : 0);
+ itemMsg.writeInt16(willBeInRange ? itemClass->getDatabaseID() : 0);
itemMsg.writeInt16(opos.x);
itemMsg.writeInt16(opos.y);
}
@@ -544,7 +545,7 @@ bool GameState::insert(Entity *ptr)
{
case OBJECT_ITEM:
LOG_DEBUG("Item inserted: "
- << static_cast<Item*>(obj)->getItemClass()->getDatabaseID());
+ << obj->getComponent<ItemComponent>()->getItemClass()->getDatabaseID());
break;
case OBJECT_NPC:
@@ -618,7 +619,7 @@ void GameState::remove(Entity *ptr)
{
case OBJECT_ITEM:
LOG_DEBUG("Item removed: "
- << static_cast<Item*>(ptr)->getItemClass()->getDatabaseID());
+ << ptr->getComponent<ItemComponent>()->getItemClass()->getDatabaseID());
break;
case OBJECT_NPC:
@@ -675,14 +676,14 @@ void GameState::remove(Entity *ptr)
}
else if (ptr->getType() == OBJECT_ITEM)
{
- Item *obj = static_cast< Item * >(ptr);
- Point pos = obj->getPosition();
+ Actor *actor = static_cast<Actor*>(ptr);
+ Point pos = actor->getPosition();
MessageOut msg(GPMSG_ITEMS);
msg.writeInt16(0);
msg.writeInt16(pos.x);
msg.writeInt16(pos.y);
- for (CharacterIterator p(map->getAroundActorIterator(obj, visualRange)); p; ++p)
+ for (CharacterIterator p(map->getAroundActorIterator(actor, visualRange)); p; ++p)
{
if (pos.inRangeOf((*p)->getPosition(), visualRange))
{
diff --git a/src/game-server/state.h b/src/game-server/state.h
index 34eb881..c518972 100644
--- a/src/game-server/state.h
+++ b/src/game-server/state.h
@@ -18,16 +18,18 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef SERVER_STATE_H
-#define SERVER_STATE_H
+#ifndef STATE_H
+#define STATE_H
+
+#include "utils/point.h"
#include <string>
-class MapComposite;
-class Entity;
class Actor;
class Character;
-
+class Entity;
+class ItemClass;
+class MapComposite;
namespace GameState
{
@@ -107,21 +109,21 @@ namespace GameState
void sayToAll(const std::string &text);
/**
- * Gets the cached value of a global script variable
+ * Gets the cached value of a global script variable.
*/
std::string getVariable(const std::string &key);
/**
* Changes a global script variable and notifies the database server
- * about the change
+ * about the change.
*/
- void setVariable (const std::string &key, const std::string &value);
+ void setVariable(const std::string &key, const std::string &value);
/**
* Changes a global variable without notifying the database server
- * about the change
+ * about the change.
*/
- void setVariableFromDbserver (const std::string &key, const std::string &value);
+ void setVariableFromDbserver(const std::string &key, const std::string &value);
/**
* Informs all maps about the change of a variable so the maps can call
@@ -131,4 +133,4 @@ namespace GameState
const std::string &value);
}
-#endif
+#endif // STATE_H
diff --git a/src/game-server/triggerareacomponent.h b/src/game-server/triggerareacomponent.h
index 4fa6e33..2185d8c 100644
--- a/src/game-server/triggerareacomponent.h
+++ b/src/game-server/triggerareacomponent.h
@@ -66,7 +66,7 @@ class ScriptAction : public TriggerAction
class TriggerAreaComponent : public Component
{
public:
- static const ComponentType type = TriggerArea;
+ static const ComponentType type = CT_TriggerArea;
/**
* Creates a rectangular trigger for a given map.