diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chat-server/post.h | 1 | ||||
-rw-r--r-- | src/game-server/accountconnection.cpp | 9 | ||||
-rw-r--r-- | src/game-server/command.cpp | 9 | ||||
-rw-r--r-- | src/game-server/commandhandler.cpp | 24 | ||||
-rw-r--r-- | src/game-server/component.h | 5 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 31 | ||||
-rw-r--r-- | src/game-server/item.cpp | 27 | ||||
-rw-r--r-- | src/game-server/item.h | 31 | ||||
-rw-r--r-- | src/game-server/main-game.cpp | 2 | ||||
-rw-r--r-- | src/game-server/monster.cpp | 13 | ||||
-rw-r--r-- | src/game-server/spawnareacomponent.h | 2 | ||||
-rw-r--r-- | src/game-server/state.cpp | 19 | ||||
-rw-r--r-- | src/game-server/state.h | 24 | ||||
-rw-r--r-- | src/game-server/triggerareacomponent.h | 2 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 14 |
15 files changed, 129 insertions, 84 deletions
diff --git a/src/chat-server/post.h b/src/chat-server/post.h index 2d0b2ae0..bd4009af 100644 --- a/src/chat-server/post.h +++ b/src/chat-server/post.h @@ -27,7 +27,6 @@ #include "../common/inventorydata.h" -class Item; class Character; class Letter diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index 8b517f54..44858fcd 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 e80b9f98..f475f851 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 6de22242..ad6a1bc2 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 a0f85d2a..fd477b90 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 3b356a5c..eeccdcc5 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 c6c9d11c..44029303 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 54112c4a..2ce858c7 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 2533a16b..3a12cdb8 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 3899c792..edc89ec1 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 52399a64..04658448 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 6fdfb6f5..d84f35b2 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 34eb8814..c518972f 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 4fa6e334..2185d8cf 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. diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index dfc1bb0a..0dd6582d 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -471,12 +471,8 @@ static int item_drop(lua_State *s) const int number = luaL_optint(s, 4, 1); MapComposite *map = checkCurrentMap(s); - Item *i = new Item(ic, number); - - i->setMap(map); - Point pos(x, y); - i->setPosition(pos); - GameState::enqueueInsert(i); + Actor *item = Item::create(map, Point(x, y), ic, number); + GameState::enqueueInsert(item); return 0; } @@ -956,9 +952,9 @@ static int chr_inv_change(lua_State *s) nb = inv.insert(id, nb); if (nb) { - Item *item = new Item(ic, nb); - item->setMap(q->getMap()); - item->setPosition(q->getPosition()); + Actor *item = Item::create(q->getMap(), + q->getPosition(), + ic, nb); GameState::enqueueInsert(item); } } |