diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.cpp | 12 | ||||
-rw-r--r-- | src/game.cpp | 12 | ||||
-rw-r--r-- | src/gui/outfitwindow.cpp | 12 | ||||
-rw-r--r-- | src/gui/specialswindow.cpp | 2 | ||||
-rw-r--r-- | src/gui/specialswindow.h | 2 | ||||
-rw-r--r-- | src/gui/trade.cpp | 2 | ||||
-rw-r--r-- | src/gui/widgets/itemshortcutcontainer.cpp | 10 | ||||
-rw-r--r-- | src/itemshortcut.cpp | 4 | ||||
-rw-r--r-- | src/localplayer.cpp | 60 | ||||
-rw-r--r-- | src/localplayer.h | 52 | ||||
-rw-r--r-- | src/net/manaserv/inventoryhandler.cpp | 10 | ||||
-rw-r--r-- | src/net/manaserv/playerhandler.cpp | 2 | ||||
-rw-r--r-- | src/net/manaserv/tradehandler.cpp | 9 | ||||
-rw-r--r-- | src/net/tmwa/buysellhandler.cpp | 2 | ||||
-rw-r--r-- | src/net/tmwa/inventoryhandler.cpp | 4 | ||||
-rw-r--r-- | src/net/tmwa/inventoryhandler.h | 10 | ||||
-rw-r--r-- | src/net/tmwa/tradehandler.cpp | 15 | ||||
-rw-r--r-- | src/playerinfo.cpp | 223 | ||||
-rw-r--r-- | src/playerinfo.h | 192 |
19 files changed, 415 insertions, 220 deletions
diff --git a/src/client.cpp b/src/client.cpp index ea35f76d..a2f70900 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -25,6 +25,8 @@ #include "chatlog.h" #include "configuration.h" #include "emoteshortcut.h" +#include "event.h" +#include "eventmanager.h" #include "game.h" #include "itemshortcut.h" #include "keyboardconfig.h" @@ -427,6 +429,9 @@ Client::Client(const Options &options): SDL_initFramerate(&mFpsManager); config.addListener("fpslimit", this); optionChanged("fpslimit"); + + // Initialize PlayerInfo + PlayerInfo::init(); } Client::~Client() @@ -587,6 +592,13 @@ int Client::exec() if (mState != mOldState) { + { + Mana::Event event("StateChange"); + event.setInt("oldState", mOldState); + event.setInt("newState", mState); + Mana::EventManager::trigger("Client", event); + } + Net::GeneralHandler *generalHandler = Net::getGeneralHandler(); if (generalHandler) generalHandler->stateChanged(mOldState, mState); diff --git a/src/game.cpp b/src/game.cpp index c867bda9..4d101fd5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -28,6 +28,8 @@ #include "commandhandler.h" #include "configuration.h" #include "effectmanager.h" +#include "event.h" +#include "eventmanager.h" #include "emoteshortcut.h" #include "graphics.h" #include "itemshortcut.h" @@ -147,10 +149,10 @@ static void createGuiWindows() // Create dialogs chatWindow = new ChatWindow; tradeWindow = new TradeWindow; - equipmentWindow = new EquipmentWindow(player_node->mEquipment.get()); + equipmentWindow = new EquipmentWindow(PlayerInfo::getEquipment()); statusWindow = new StatusWindow; miniStatusWindow = new MiniStatusWindow; - inventoryWindow = new InventoryWindow(player_node->getInventory()); + inventoryWindow = new InventoryWindow(PlayerInfo::getInventory()); skillDialog = new SkillDialog; minimap = new Minimap; helpWindow = new HelpWindow; @@ -250,6 +252,9 @@ Game::Game(): joystick = new Joystick(0); setupWindow->setInGame(true); + + Mana::Event event("Constructed"); + Mana::EventManager::trigger("Game", event); } Game::~Game() @@ -269,6 +274,9 @@ Game::~Game() del_0(mCurrentMap) mInstance = 0; + + Mana::Event event("Destructed"); + Mana::EventManager::trigger("Game", event); } static bool saveScreenshot() diff --git a/src/gui/outfitwindow.cpp b/src/gui/outfitwindow.cpp index 89bf47da..51b4b5c5 100644 --- a/src/gui/outfitwindow.cpp +++ b/src/gui/outfitwindow.cpp @@ -22,12 +22,12 @@ #include "outfitwindow.h" #include "configuration.h" -#include "localplayer.h" +#include "equipment.h" #include "graphics.h" #include "inventory.h" -#include "equipment.h" #include "item.h" #include "log.h" +#include "playerinfo.h" #include "gui/chat.h" @@ -168,7 +168,7 @@ void OutfitWindow::wearOutfit(int outfit) Item *item; for (int i = 0; i < OUTFIT_ITEM_COUNT; i++) { - item = player_node->getInventory()->findItem(mItems[outfit][i]); + item = PlayerInfo::getInventory()->findItem(mItems[outfit][i]); if (item && !item->isEquipped() && item->getQuantity()) { if (item->isEquipment()) @@ -206,7 +206,7 @@ void OutfitWindow::draw(gcn::Graphics *graphics) } Item *item = - player_node->getInventory()->findItem(mItems[mCurrentOutfit][i]); + PlayerInfo::getInventory()->findItem(mItems[mCurrentOutfit][i]); if (item) { // Draw item icon. @@ -245,7 +245,7 @@ void OutfitWindow::mouseDragged(gcn::MouseEvent &event) const int itemId = mItems[mCurrentOutfit][index]; if (itemId < 0) return; - Item *item = player_node->getInventory()->findItem(itemId); + Item *item = PlayerInfo::getInventory()->findItem(itemId); if (item) { mItemMoved = item; @@ -319,7 +319,7 @@ int OutfitWindow::getIndexFromGrid(int pointX, int pointY) const void OutfitWindow::unequipNotInOutfit(int outfit) { - Inventory *inventory = player_node->getInventory(); + Inventory *inventory = PlayerInfo::getInventory(); if (!inventory) return; diff --git a/src/gui/specialswindow.cpp b/src/gui/specialswindow.cpp index 4f9a9652..c751d6cb 100644 --- a/src/gui/specialswindow.cpp +++ b/src/gui/specialswindow.cpp @@ -123,7 +123,7 @@ void SpecialsWindow::action(const gcn::ActionEvent &event) void SpecialsWindow::draw(gcn::Graphics *graphics) { // update the progress bars - std::map<int, Special> specialData = player_node->getSpecialStatus(); + std::map<int, Special> specialData = PlayerInfo::getSpecialStatus(); bool foundNew = false; unsigned int found = 0; // number of entries in specialData which match mEntries diff --git a/src/gui/specialswindow.h b/src/gui/specialswindow.h index 634727e5..85abe3a4 100644 --- a/src/gui/specialswindow.h +++ b/src/gui/specialswindow.h @@ -25,7 +25,7 @@ #include "guichanfwd.h" -#include "localplayer.h" +#include "playerinfo.h" #include "gui/widgets/window.h" diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index d500999e..4a86c235 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -286,7 +286,7 @@ void TradeWindow::action(const gcn::ActionEvent &event) { setVisible(false); reset(); - player_node->setTrading(false); + PlayerInfo::setTrading(false); Net::getTradeHandler()->cancel(); } diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index 83efd4d4..b3cefc5f 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -27,7 +27,7 @@ #include "item.h" #include "itemshortcut.h" #include "keyboardconfig.h" -#include "localplayer.h" +#include "playerinfo.h" #include "gui/inventorywindow.h" #include "gui/itempopup.h" @@ -94,7 +94,7 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics) continue; Item *item = - player_node->getInventory()->findItem(itemShortcut->getItem(i)); + PlayerInfo::getInventory()->findItem(itemShortcut->getItem(i)); if (item) { @@ -151,7 +151,7 @@ void ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event) if (itemId < 0) return; - Item *item = player_node->getInventory()->findItem(itemId); + Item *item = PlayerInfo::getInventory()->findItem(itemId); if (item) { @@ -187,7 +187,7 @@ void ItemShortcutContainer::mousePressed(gcn::MouseEvent &event) } else if (event.getButton() == gcn::MouseEvent::RIGHT) { - Item *item = player_node->getInventory()-> + Item *item = PlayerInfo::getInventory()-> findItem(itemShortcut->getItem(index)); if (!item) @@ -240,7 +240,7 @@ void ItemShortcutContainer::mouseMoved(gcn::MouseEvent &event) if (itemId < 0) return; - Item *item = player_node->getInventory()->findItem(itemId); + Item *item = PlayerInfo::getInventory()->findItem(itemId); if (item) { diff --git a/src/itemshortcut.cpp b/src/itemshortcut.cpp index 88b04347..064fdbc6 100644 --- a/src/itemshortcut.cpp +++ b/src/itemshortcut.cpp @@ -23,7 +23,7 @@ #include "inventory.h" #include "item.h" #include "itemshortcut.h" -#include "localplayer.h" +#include "playerinfo.h" #include "net/inventoryhandler.h" #include "net/net.h" @@ -66,7 +66,7 @@ void ItemShortcut::useItem(int index) { if (mItems[index]) { - Item *item = player_node->getInventory()->findItem(mItems[index]); + Item *item = PlayerInfo::getInventory()->findItem(mItems[index]); if (item && item->getQuantity()) { if (item->isEquipment()) diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 511b5ed6..b2061643 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -24,22 +24,20 @@ #include "client.h" #include "configuration.h" #include "effectmanager.h" -#include "equipment.h" #include "flooritem.h" #include "graphics.h" #include "guild.h" -#include "inventory.h" #include "item.h" #include "log.h" #include "map.h" #include "particle.h" +#include "playerinfo.h" #include "simpleanimation.h" #include "sound.h" #include "statuseffect.h" #include "text.h" #include "gui/gui.h" -#include "gui/inventorywindow.h" #include "gui/ministatus.h" #include "gui/okdialog.h" #include "gui/skilldialog.h" @@ -59,7 +57,6 @@ #include "resources/animation.h" #include "resources/imageset.h" -#include "resources/itemdb.h" #include "resources/iteminfo.h" #include "resources/resourcemanager.h" @@ -79,19 +76,16 @@ LocalPlayer *player_node = NULL; LocalPlayer::LocalPlayer(int id, int subtype): Being(id, PLAYER, subtype, 0), - mEquipment(new Equipment), mAttackRange(0), mTargetTime(-1), mLastTarget(-1), - mSpecialRechargeUpdateNeeded(0), mTarget(NULL), mPlayerFollowed(""), mPickUpTarget(NULL), - mTrading(false), mGoingToTarget(false), mKeepAttacking(false), + mGoingToTarget(false), mKeepAttacking(false), mLastAction(-1), mWalkingDir(0), mPathSetByMouse(false), - mInventory(new Inventory(Inventory::INVENTORY)), mLocalWalkTime(-1), mMessageTime(0), mAwayDialog(0), @@ -108,8 +102,6 @@ LocalPlayer::LocalPlayer(int id, int subtype): LocalPlayer::~LocalPlayer() { - delete mInventory; - config.removeListener("showownname", this); delete mAwayDialog; @@ -146,21 +138,7 @@ void LocalPlayer::logic() mMessageTime--; } - if ((mSpecialRechargeUpdateNeeded%11) == 0) - { - mSpecialRechargeUpdateNeeded = 0; - for (std::map<int, Special>::iterator i = mSpecials.begin(); - i != mSpecials.end(); - i++) - { - i->second.currentMana += i->second.recharge; - if (i->second.currentMana > i->second.neededMana) - { - i->second.currentMana = i->second.neededMana; - } - } - } - mSpecialRechargeUpdateNeeded++; + PlayerInfo::logic(); // Targeting allowed 4 times a second if (get_elapsed_time(mLastTarget) >= 250) @@ -659,21 +637,6 @@ void LocalPlayer::inviteToGuild(Being *being) } } -void LocalPlayer::clearInventory() -{ - mEquipment->clear(); - mInventory->clear(); -} - -void LocalPlayer::setInvItem(int index, int id, int amount) -{ - bool equipment = false; - int itemType = ItemDB::get(id).getType(); - if (itemType != ITEM_UNUSABLE && itemType != ITEM_USABLE) - equipment = true; - mInventory->setItem(index, id, amount, equipment); -} - void LocalPlayer::pickUp(FloorItem *item) { if (!item) @@ -927,20 +890,6 @@ void LocalPlayer::emote(Uint8 emotion) Net::getPlayerHandler()->emote(emotion); } -void LocalPlayer::useSpecial(int special) -{ - Net::getSpecialHandler()->use(special); -} - -void LocalPlayer::setSpecialStatus(int id, int current, int max, int recharge) -{ - logger->log("SpecialUpdate Skill #%d -- (%d/%d) -> %d", id, current, max, - recharge); - mSpecials[id].currentMana = current; - mSpecials[id].neededMana = max; - mSpecials[id].recharge = recharge; -} - void LocalPlayer::attack(Being *target, bool keep) { if (Net::getNetworkType() == ServerInfo::MANASERV) @@ -1081,7 +1030,8 @@ int LocalPlayer::getAttackRange() } else { - Item *weapon = mEquipment->getEquipment(EQUIP_FIGHT1_SLOT); + // TODO: Fix this to be more generic + Item *weapon = PlayerInfo::getEquipment(EQUIP_FIGHT1_SLOT); if (weapon) { const ItemInfo info = weapon->getInfo(); diff --git a/src/localplayer.h b/src/localplayer.h index e42e305f..2bcf90f8 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -33,22 +33,12 @@ #include <vector> class ChatTab; -class Equipment; class FloorItem; class ImageSet; -class Inventory; class Item; class Map; class OkDialog; - -struct Special -{ - int currentMana; - int neededMana; - int recharge; -}; - class AwayListener : public gcn::ActionListener { public: @@ -93,11 +83,6 @@ class LocalPlayer : public Being, public ActorSpriteListener virtual void nextTile(unsigned char dir); /** - * Returns the player's inventory. - */ - Inventory *getInventory() const { return mInventory; } - - /** * Check the player has permission to invite users to specific guild */ bool checkInviteRights(const std::string &guildName); @@ -107,9 +92,6 @@ class LocalPlayer : public Being, public ActorSpriteListener */ void inviteToGuild(Being *being); - void clearInventory(); - void setInvItem(int index, int id, int amount); - void pickUp(FloorItem *item); /** @@ -127,26 +109,6 @@ class LocalPlayer : public Being, public ActorSpriteListener * Gets the attack range. */ int getAttackRange(); - - /** - * Returns true when the player is ready to accept a trade offer. - * Returns false otherwise. - */ - bool tradeRequestOk() const { return !mTrading; } - - /** - * Sets the trading state of the player, i.e. whether or not he is - * currently involved into some trade. - */ - void setTrading(bool trading) { mTrading = trading; } - - void useSpecial(int id); - - void setSpecialStatus(int id, int current, int max, int recharge); - - const std::map<int, Special> &getSpecialStatus() const - { return mSpecials; } - void attack(Being *target = NULL, bool keep = false); void setGMLevel(int level); @@ -275,11 +237,7 @@ class LocalPlayer : public Being, public ActorSpriteListener */ bool getCheckNameSetting() const { return mUpdateName; } - /** Keeps the Equipment related values */ - const std::auto_ptr<Equipment> mEquipment; - protected: - /** Whether or not the name settings have changed */ bool mUpdateName; @@ -292,11 +250,6 @@ class LocalPlayer : public Being, public ActorSpriteListener int mTargetTime; /** How long the being has been targeted **/ int mLastTarget; /** Time stamp of last targeting action, -1 if none. */ - // Character status: - int mLevelProgress; - std::map<int, Special> mSpecials; - char mSpecialRechargeUpdateNeeded; - int mGMLevel; Being *mTarget; @@ -308,7 +261,6 @@ class LocalPlayer : public Being, public ActorSpriteListener FloorItem *mPickUpTarget; - bool mTrading; bool mGoingToTarget; bool mKeepAttacking; /** Whether or not to continue to attack */ int mLastAction; /**< Time stamp of the last action, -1 if none. */ @@ -317,13 +269,11 @@ class LocalPlayer : public Being, public ActorSpriteListener std::vector<int> mStatusEffectIcons; - Inventory *mInventory; - int mLocalWalkTime; /**< Timestamp used to control keyboard walk messages flooding */ typedef std::pair<std::string, int> MessagePair; - /** Queued exp messages*/ + /** Queued messages*/ std::list<MessagePair> mMessages; int mMessageTime; AwayListener *mAwayListener; diff --git a/src/net/manaserv/inventoryhandler.cpp b/src/net/manaserv/inventoryhandler.cpp index bf5682c7..3f8aa8b5 100644 --- a/src/net/manaserv/inventoryhandler.cpp +++ b/src/net/manaserv/inventoryhandler.cpp @@ -61,8 +61,8 @@ void InventoryHandler::handleMessage(Net::MessageIn &msg) switch (msg.getId()) { case GPMSG_INVENTORY_FULL: - player_node->clearInventory(); - player_node->mEquipment->setBackend(&mEquips); + PlayerInfo::clearInventory(); + PlayerInfo::getEquipment()->setBackend(&mEquips); // no break! case GPMSG_INVENTORY: @@ -83,7 +83,7 @@ void InventoryHandler::handleMessage(Net::MessageIn &msg) else if (slot >= 32 && slot < 32 + getSize(Inventory::INVENTORY)) { int amount = id ? msg.readInt8() : 0; - player_node->setInvItem(slot - 32, id, amount); + PlayerInfo::setInventoryItem(slot - 32, id, amount); } }; break; @@ -131,7 +131,7 @@ bool InventoryHandler::canSplit(const Item *item) void InventoryHandler::splitItem(const Item *item, int amount) { - int newIndex = player_node->getInventory()->getFreeSlot(); + int newIndex = PlayerInfo::getInventory()->getFreeSlot(); if (newIndex > Inventory::NO_SLOT_INDEX) { MessageOut msg(PGMSG_MOVE_ITEM); @@ -150,7 +150,7 @@ void InventoryHandler::moveItem(int oldIndex, int newIndex) MessageOut msg(PGMSG_MOVE_ITEM); msg.writeInt8(oldIndex); msg.writeInt8(newIndex); - msg.writeInt8(player_node->getInventory()->getItem(oldIndex) + msg.writeInt8(PlayerInfo::getInventory()->getItem(oldIndex) ->getQuantity()); gameServerConnection->send(msg); } diff --git a/src/net/manaserv/playerhandler.cpp b/src/net/manaserv/playerhandler.cpp index cef75472..bc03cc25 100644 --- a/src/net/manaserv/playerhandler.cpp +++ b/src/net/manaserv/playerhandler.cpp @@ -241,7 +241,7 @@ void PlayerHandler::handleMessage(Net::MessageIn &msg) int current = msg.readInt32(); int max = msg.readInt32(); int recharge = msg.readInt32(); - player_node->setSpecialStatus(id, current, max, recharge); + PlayerInfo::setSpecialStatus(id, current, max, recharge); } } break; /* diff --git a/src/net/manaserv/tradehandler.cpp b/src/net/manaserv/tradehandler.cpp index b6169ac9..d5505075 100644 --- a/src/net/manaserv/tradehandler.cpp +++ b/src/net/manaserv/tradehandler.cpp @@ -24,6 +24,7 @@ #include "actorspritemanager.h" #include "item.h" #include "localplayer.h" +#include "playerinfo.h" #include "gui/confirmdialog.h" #include "gui/trade.h" @@ -110,7 +111,7 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) respond(false); break; } - player_node->setTrading(true); + PlayerInfo::setTrading(true); tradePartnerName = being->getName(); tradePartnerID = being->getId(); ConfirmDialog *dlg = new ConfirmDialog(_("Request for Trade"), @@ -149,14 +150,14 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) localChatTab->chatLog(_("Trade canceled."), BY_SERVER); tradeWindow->setVisible(false); tradeWindow->reset(); - player_node->setTrading(false); + PlayerInfo::setTrading(false); break; case GPMSG_TRADE_COMPLETE: localChatTab->chatLog(_("Trade completed."), BY_SERVER); tradeWindow->setVisible(false); tradeWindow->reset(); - player_node->setTrading(false); + PlayerInfo::setTrading(false); break; } } @@ -177,7 +178,7 @@ void TradeHandler::respond(bool accept) gameServerConnection->send(msg); if (!accept) - player_node->setTrading(false); + PlayerInfo::setTrading(false); } void TradeHandler::addItem(Item *item, int amount) diff --git a/src/net/tmwa/buysellhandler.cpp b/src/net/tmwa/buysellhandler.cpp index cd12939b..d0c8fd40 100644 --- a/src/net/tmwa/buysellhandler.cpp +++ b/src/net/tmwa/buysellhandler.cpp @@ -99,7 +99,7 @@ void BuySellHandler::handleMessage(Net::MessageIn &msg) int value = msg.readInt32(); msg.readInt32(); // OCvalue - Item *item = player_node->getInventory()->getItem(index); + Item *item = PlayerInfo::getInventory()->getItem(index); if (item && !(item->isEquipped())) dialog->addItem(item, value); diff --git a/src/net/tmwa/inventoryhandler.cpp b/src/net/tmwa/inventoryhandler.cpp index fd979dc6..41049c3d 100644 --- a/src/net/tmwa/inventoryhandler.cpp +++ b/src/net/tmwa/inventoryhandler.cpp @@ -126,7 +126,7 @@ void InventoryHandler::handleMessage(Net::MessageIn &msg) int number, flag; int index, amount, itemId, equipType, arrow; int identified, cards[4], itemType; - Inventory *inventory = player_node->getInventory(); + Inventory *inventory = PlayerInfo::getInventory(); switch (msg.getId()) { @@ -136,7 +136,7 @@ void InventoryHandler::handleMessage(Net::MessageIn &msg) { // Clear inventory - this will be a complete refresh mEquips.clear(); - player_node->mEquipment->setBackend(&mEquips); + PlayerInfo::getEquipment()->setBackend(&mEquips); inventory->clear(); } diff --git a/src/net/tmwa/inventoryhandler.h b/src/net/tmwa/inventoryhandler.h index 82738afe..8e6e12ca 100644 --- a/src/net/tmwa/inventoryhandler.h +++ b/src/net/tmwa/inventoryhandler.h @@ -24,7 +24,7 @@ #include "equipment.h" #include "inventory.h" -#include "localplayer.h" +#include "playerinfo.h" #include "gui/inventorywindow.h" @@ -51,7 +51,7 @@ class EquipBackend : public Equipment::Backend { { return NULL; } - return player_node->getInventory()->getItem(invyIndex); + return PlayerInfo::getInventory()->getItem(invyIndex); } void clear() @@ -60,7 +60,7 @@ class EquipBackend : public Equipment::Backend { { if (mEquipment[i] != -1) { - Item* item = player_node->getInventory()->getItem(i); + Item* item = PlayerInfo::getInventory()->getItem(i); if (item) { item->setEquipped(false); @@ -74,7 +74,7 @@ class EquipBackend : public Equipment::Backend { void setEquipment(int index, int inventoryIndex) { // Unequip existing item - Item* item = player_node->getInventory()->getItem(mEquipment[index]); + Item* item = PlayerInfo::getInventory()->getItem(mEquipment[index]); if (item) { item->setEquipped(false); @@ -82,7 +82,7 @@ class EquipBackend : public Equipment::Backend { mEquipment[index] = inventoryIndex; - item = player_node->getInventory()->getItem(inventoryIndex); + item = PlayerInfo::getInventory()->getItem(inventoryIndex); if (item) { item->setEquipped(true); diff --git a/src/net/tmwa/tradehandler.cpp b/src/net/tmwa/tradehandler.cpp index 9089f8e6..2c82f1e2 100644 --- a/src/net/tmwa/tradehandler.cpp +++ b/src/net/tmwa/tradehandler.cpp @@ -24,6 +24,7 @@ #include "inventory.h" #include "item.h" #include "localplayer.h" +#include "playerinfo.h" #include "playerrelations.h" #include "gui/confirmdialog.h" @@ -96,14 +97,14 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) if (player_relations.hasPermission(tradePartnerName, PlayerRelation::TRADE)) { - if (!player_node->tradeRequestOk() || confirmDlg) + if (PlayerInfo::isTrading() || confirmDlg) { Net::getTradeHandler()->respond(false); break; } tradePartnerName = tradePartnerNameTemp; - player_node->setTrading(true); + PlayerInfo::setTrading(true); confirmDlg = new ConfirmDialog(_("Request for Trade"), strprintf(_("%s wants to trade with you, do you " "accept?"), tradePartnerName.c_str())); @@ -147,7 +148,7 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) // otherwise ignore silently tradeWindow->setVisible(false); - player_node->setTrading(false); + PlayerInfo::setTrading(false); break; default: // Shouldn't happen as well, but to be sure localChatTab->chatLog(_("Unhandled trade cancel packet."), @@ -177,7 +178,7 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) // Trade: New Item add response (was 0x00ea, now 01b1) { const int index = msg.readInt16() - INVENTORY_OFFSET; - Item *item = player_node->getInventory()->getItem(index); + Item *item = PlayerInfo::getInventory()->getItem(index); if (!item) { tradeWindow->receivedOk(true); @@ -224,14 +225,14 @@ void TradeHandler::handleMessage(Net::MessageIn &msg) localChatTab->chatLog(_("Trade canceled."), BY_SERVER); tradeWindow->setVisible(false); tradeWindow->reset(); - player_node->setTrading(false); + PlayerInfo::setTrading(false); break; case SMSG_TRADE_COMPLETE: localChatTab->chatLog(_("Trade completed."), BY_SERVER); tradeWindow->setVisible(false); tradeWindow->reset(); - player_node->setTrading(false); + PlayerInfo::setTrading(false); break; } } @@ -245,7 +246,7 @@ void TradeHandler::request(Being *being) void TradeHandler::respond(bool accept) { if (!accept) - player_node->setTrading(false); + PlayerInfo::setTrading(false); MessageOut outMsg(CMSG_TRADE_RESPONSE); outMsg.writeInt8(accept ? 3 : 4); diff --git a/src/playerinfo.cpp b/src/playerinfo.cpp index 203c26cc..e63db6cf 100644 --- a/src/playerinfo.cpp +++ b/src/playerinfo.cpp @@ -20,19 +20,59 @@ #include "playerinfo.h" +#include "client.h" +#include "equipment.h" #include "event.h" #include "eventmanager.h" +#include "inventory.h" +#include "listener.h" +#include "log.h" -PlayerInfoBackend PlayerInfo::mData; +#include "resources/itemdb.h" +#include "resources/iteminfo.h" -void PlayerInfo::setBackend(const PlayerInfoBackend &backend) +namespace PlayerInfo { + +class PlayerInfoListener; + +PlayerInfoListener *mListener = 0; + +PlayerInfoBackend mData; + +Inventory *mInventory = 0; +Equipment *mEquipment = 0; + +std::map<int, Special> mSpecials; +char mSpecialRechargeUpdateNeeded = 0; + +bool mTrading = false; +int mLevelProgress = 0; + +// --- Triggers --------------------------------------------------------------- + +void triggerAttr(int id) { - mData = backend; + Mana::Event event("UpdateAttribute"); + event.setInt("id", id); + event.setInt("value", mData.mAttributes.find(id)->second); + Mana::EventManager::trigger("Attributes", event); } -// ------------------------------- Attributes -------------------------------------- +void triggerStat(int id) +{ + StatMap::iterator it = mData.mStats.find(id); + Mana::Event event("UpdateStat"); + event.setInt("id", id); + event.setInt("base", it->second.base); + event.setInt("mod", it->second.mod); + event.setInt("exp", it->second.exp); + event.setInt("expneeded", it->second.expneed); + Mana::EventManager::trigger("Attributes", event); +} -int PlayerInfo::getAttribute(int id) +// --- Attributes ------------------------------------------------------------- + +int getAttribute(int id) { IntMap::const_iterator it = mData.mAttributes.find(id); if (it != mData.mAttributes.end()) @@ -41,16 +81,16 @@ int PlayerInfo::getAttribute(int id) return 0; } -void PlayerInfo::setAttribute(int id, int value, bool notify) +void setAttribute(int id, int value, bool notify) { mData.mAttributes[id] = value; if (notify) triggerAttr(id); } -// ------------------------------- Stats -------------------------------------- +// --- Stats ------------------------------------------------------------------ -int PlayerInfo::getStatBase(int id) +int getStatBase(int id) { StatMap::const_iterator it = mData.mStats.find(id); if (it != mData.mStats.end()) @@ -59,14 +99,14 @@ int PlayerInfo::getStatBase(int id) return 0; } -void PlayerInfo::setStatBase(int id, int value, bool notify) +void setStatBase(int id, int value, bool notify) { mData.mStats[id].base = value; if (notify) triggerStat(id); } -int PlayerInfo::getStatMod(int id) +int getStatMod(int id) { StatMap::const_iterator it = mData.mStats.find(id); @@ -76,14 +116,14 @@ int PlayerInfo::getStatMod(int id) return 0; } -void PlayerInfo::setStatMod(int id, int value, bool notify) +void setStatMod(int id, int value, bool notify) { mData.mStats[id].mod = value; if (notify) triggerStat(id); } -int PlayerInfo::getStatEffective(int id) +int getStatEffective(int id) { StatMap::const_iterator it = mData.mStats.find(id); if (it != mData.mStats.end()) @@ -92,7 +132,7 @@ int PlayerInfo::getStatEffective(int id) return 0; } -std::pair<int, int> PlayerInfo::getStatExperience(int id) +std::pair<int, int> getStatExperience(int id) { StatMap::const_iterator it = mData.mStats.find(id); int a, b; @@ -109,7 +149,7 @@ std::pair<int, int> PlayerInfo::getStatExperience(int id) return std::pair<int, int>(a, b); } -void PlayerInfo::setStatExperience(int id, int have, int need, bool notify) +void setStatExperience(int id, int have, int need, bool notify) { mData.mStats[id].exp = have; mData.mStats[id].expneed = need; @@ -117,24 +157,149 @@ void PlayerInfo::setStatExperience(int id, int have, int need, bool notify) triggerStat(id); } -// ------------------------------- Triggers -------------------------------------- +// --- Inventory / Equipment -------------------------------------------------- -void PlayerInfo::triggerAttr(int id) +Inventory *getInventory() { - Mana::Event event("UpdateAttribute"); - event.setInt("id", id); - event.setInt("value", mData.mAttributes.find(id)->second); - Mana::EventManager::trigger("Attributes", event); + return mInventory; } -void PlayerInfo::triggerStat(int id) +void clearInventory() { - StatMap::iterator it = mData.mStats.find(id); - Mana::Event event("UpdateStat"); - event.setInt("id", id); - event.setInt("base", it->second.base); - event.setInt("mod", it->second.mod); - event.setInt("exp", it->second.exp); - event.setInt("expneeded", it->second.expneed); - Mana::EventManager::trigger("Attributes", event); + mEquipment->clear(); + mInventory->clear(); +} + +void setInventoryItem(int index, int id, int amount) +{ + bool equipment = false; + int itemType = ItemDB::get(id).getType(); + if (itemType != ITEM_UNUSABLE && itemType != ITEM_USABLE) + equipment = true; + mInventory->setItem(index, id, amount, equipment); +} + +Equipment *getEquipment() +{ + return mEquipment; +} + +Item *getEquipment(unsigned int slot) +{ + return mEquipment->getEquipment(slot); } + +void setEquipmentBackend(Equipment::Backend *backend) +{ + mEquipment->setBackend(backend); +} + +// --- Specials --------------------------------------------------------------- + +void setSpecialStatus(int id, int current, int max, int recharge) +{ + logger->log("SpecialUpdate Skill #%d -- (%d/%d) -> %d", id, current, max, + recharge); + mSpecials[id].currentMana = current; + mSpecials[id].neededMana = max; + mSpecials[id].recharge = recharge; +} + +const SpecialsMap &getSpecialStatus() +{ + return mSpecials; +} + +// --- Misc ------------------------------------------------------------------- + +void setBackend(const PlayerInfoBackend &backend) +{ + mData = backend; +} + +void logic() +{ + if ((mSpecialRechargeUpdateNeeded%11) == 0) + { + mSpecialRechargeUpdateNeeded = 0; + for (SpecialsMap::iterator it = mSpecials.begin(), + it_end = mSpecials.end(); it != it_end; it++) + { + it->second.currentMana += it->second.recharge; + if (it->second.currentMana > it->second.neededMana) + { + it->second.currentMana = it->second.neededMana; + } + } + } + mSpecialRechargeUpdateNeeded++; +} + +bool isTrading() +{ + return mTrading; +} + +void setTrading(bool trading) +{ + bool notify = mTrading != trading; + mTrading = trading; + + if (notify) + { + Mana::Event event("Trading"); + event.setInt("trading", trading); + Mana::EventManager::trigger("Status", event); + } +} + +class PlayerInfoListener : Mana::Listener +{ +public: + PlayerInfoListener() + { + listen("Client"); + listen("Game"); + } + + void event(const std::string &channel, const Mana::Event &event) + { + if (channel == "Client") + { + if (event.getName() == "StateChange") + { + int newState = event.getInt("newState"); + + if (newState == STATE_GAME) + { + if (mInventory == 0) + { + mInventory = new Inventory(Inventory::INVENTORY); + mEquipment = new Equipment(); + } + } + } + } + else if (channel == "Game") + { + if (event.getName() == "Destructed") + { + delete mInventory; + delete mEquipment; + + mInventory = 0; + mEquipment = 0; + } + } + } +}; + +void init() +{ + if (mListener) + return; + + mListener = new PlayerInfoListener(); +} + +} // namespace PlayerInfo diff --git a/src/playerinfo.h b/src/playerinfo.h index 421e93bc..1c722a7c 100644 --- a/src/playerinfo.h +++ b/src/playerinfo.h @@ -24,6 +24,9 @@ #include <map> #include <string> +/** + * Standard attributes for players. + */ enum Attribute { LEVEL, @@ -36,6 +39,9 @@ enum Attribute CHAR_POINTS, CORR_POINTS }; +/** + * Stat information storage structure. + */ struct Stat { int base; @@ -47,59 +53,161 @@ struct Stat typedef std::map<int, int> IntMap; typedef std::map<int, Stat> StatMap; +/** + * Backend for core player information. + */ struct PlayerInfoBackend { - public: IntMap mAttributes; StatMap mStats; }; +class Equipment; +class Inventory; +class Item; + /** - * A database like class which holds global info about the localplayer + * Special information storage structure. */ -class PlayerInfo +struct Special { - // NOTE: All agruements for 'bool notify' is to determine if - // a event is to be triggered - public: - static void setBackend(const PlayerInfoBackend &backend); - - /** - * Attributes for things like money and exp - */ - static int getAttribute(int id); - - static void setAttribute(int id, int value, bool notify = true); - - - /** - * Stats are modifiable attributes basicilly, like str, crit, trade - */ - - static int getStatBase(int id); - - static void setStatBase(int id, int value, bool notify = true); - - static int getStatMod(int id); - - static void setStatMod(int id, int value, bool notify = true); - - // Base + mod - static int getStatEffective(int id); - - static void setStatLevel(int id, int value, bool notify = true); - - static std::pair<int, int> getStatExperience(int id); - - static void setStatExperience(int id, int have, int need, bool notify = true); + int currentMana; + int neededMana; + int recharge; +}; - private: - // Triggers send events for action. - static void triggerAttr(int id); +typedef std::map<int, Special> SpecialsMap; - static void triggerStat(int id); +/** + * A database like namespace which holds global info about the localplayer + * + * NOTE: 'bool notify' is used to determine if a event is to be triggered. + */ +namespace PlayerInfo +{ - static PlayerInfoBackend mData; -}; +// --- Attributes ------------------------------------------------------------- + + /** + * Returns the value of the given attribute. + */ + int getAttribute(int id); + + /** + * Changes the value of the given attribute. + */ + void setAttribute(int id, int value, bool notify = true); + +// --- Stats ------------------------------------------------------------------ + + /** + * Returns the base value of the given stat. + */ + int getStatBase(int id); + + /** + * Changes the base value of the given stat. + */ + void setStatBase(int id, int value, bool notify = true); + + /** + * Returns the modifier for the given stat. + */ + int getStatMod(int id); + + /** + * Changes the modifier for the given stat. + */ + void setStatMod(int id, int value, bool notify = true); + + /** + * Returns the current effective value of the given stat. Effective is base + * + mod + */ + int getStatEffective(int id); + + /** + * Changes the level of the given stat. + */ + void setStatLevel(int id, int value, bool notify = true); + + /** + * Returns the experience of the given stat. + */ + std::pair<int, int> getStatExperience(int id); + + /** + * Changes the experience of the given stat. + */ + void setStatExperience(int id, int have, int need, bool notify = true); + +// --- Inventory / Equipment -------------------------------------------------- + + /** + * Returns the player's inventory. + */ + Inventory *getInventory(); + + /** + * Clears the player's inventory and equipment. + */ + void clearInventory(); + + /** + * Changes the inventory item at the given slot. + */ + void setInventoryItem(int index, int id, int amount); + + /** + * Returns the player's equipment. + */ + Equipment *getEquipment(); + + /** + * Returns the player's equipment at the given slot. + */ + Item *getEquipment(unsigned int slot); + +// --- Specials --------------------------------------------------------------- + + /** + * Changes the status of the given special. + */ + void setSpecialStatus(int id, int current, int max, int recharge); + + /** + * Returns the status of the given special. + */ + const SpecialsMap &getSpecialStatus(); + +// --- Misc ------------------------------------------------------------------- + + /** + * Changes the internal PlayerInfoBackend reference; + */ + void setBackend(const PlayerInfoBackend &backend); + + /** + * Does necessary updates every tick. + */ + void logic(); + + /** + * Returns true if the player is involved in a trade at the moment, false + * otherwise. + */ + bool isTrading(); + + /** + * Sets whether the player is currently involved in trade or not. + */ + void setTrading(bool trading); + + /** + * Initializes some internals. + */ + void init(); + +} // namespace PlayerInfo #endif |