diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-01-22 13:31:13 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-01-22 13:31:13 +0000 |
commit | bd56bf8afdab16383ed8ad08412a8c807f84af85 (patch) | |
tree | 0e963ada63bcbe3c50dd77986aaa15b9ba49816a /src/gui | |
parent | 5359640b6f271af31f6423df9d661433eff89a3e (diff) | |
download | mana-bd56bf8afdab16383ed8ad08412a8c807f84af85.tar.gz mana-bd56bf8afdab16383ed8ad08412a8c807f84af85.tar.bz2 mana-bd56bf8afdab16383ed8ad08412a8c807f84af85.tar.xz mana-bd56bf8afdab16383ed8ad08412a8c807f84af85.zip |
Merged NETWORK branch (includes BEING_OVERHAUL).
Diffstat (limited to 'src/gui')
49 files changed, 376 insertions, 1356 deletions
diff --git a/src/gui/buddywindow.cpp b/src/gui/buddywindow.cpp index c37f2a6d..cfa21b63 100644 --- a/src/gui/buddywindow.cpp +++ b/src/gui/buddywindow.cpp @@ -29,6 +29,8 @@ #include "chat.h" #include "scrollarea.h" +#include "../resources/buddylist.h" + extern ChatWindow *chatWindow; BuddyWindow::BuddyWindow(): @@ -36,8 +38,10 @@ BuddyWindow::BuddyWindow(): { setContentSize(124, 202); + mBuddyList = new BuddyList(); + mListbox = new gcn::ListBox(); - mListbox->setListModel(this); + mListbox->setListModel(mBuddyList); ScrollArea *scrollArea = new ScrollArea(mListbox); scrollArea->setDimension(gcn::Rectangle( @@ -68,7 +72,7 @@ void BuddyWindow::action(const std::string& eventId) int selected = mListbox->getSelected(); if ( selected > -1 ) { - std::string who = getElementAt(selected); + std::string who = mBuddyList->getElementAt(selected); chatWindow->setInputText(who +": "); } } @@ -76,8 +80,8 @@ void BuddyWindow::action(const std::string& eventId) int selected = mListbox->getSelected(); if ( selected > -1 ) { - std::string who = getElementAt(selected); - removeBuddy(who); + std::string who = mBuddyList->getElementAt(selected); + mBuddyList->removeBuddy(who); } } else if (eventId == "Cancel") { diff --git a/src/gui/buddywindow.h b/src/gui/buddywindow.h index 82ef9935..6eeb7999 100644 --- a/src/gui/buddywindow.h +++ b/src/gui/buddywindow.h @@ -30,15 +30,14 @@ #include "../guichanfwd.h" -#include "../resources/buddylist.h" +class BuddyList; /** * Window showing buddy list. * * \ingroup Interface */ -class BuddyWindow : public Window, public BuddyList, - public gcn::ActionListener +class BuddyWindow : public Window, public gcn::ActionListener { public: /** @@ -52,6 +51,7 @@ class BuddyWindow : public Window, public BuddyList, void action(const std::string &actionId); private: + BuddyList *mBuddyList; gcn::ListBox *mListbox; }; diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp index 1c0b2ea7..65f2e525 100644 --- a/src/gui/buy.cpp +++ b/src/gui/buy.cpp @@ -33,7 +33,7 @@ #include "shop.h" #include "slider.h" -#include "../game.h" +#include "../npc.h" #include "../resources/iteminfo.h" #include "../resources/itemmanager.h" @@ -42,8 +42,8 @@ #include "../net/protocol.h" -BuyDialog::BuyDialog(): - Window("Buy"), +BuyDialog::BuyDialog(Network *network): + Window("Buy"), mNetwork(network), m_money(0), m_amountItems(0), m_maxItems(0) { itemList = new ListBox(this); @@ -221,7 +221,7 @@ void BuyDialog::action(const std::string& eventId) // there a better way to ensure this fails in an _obivous_ way in C++? else if (eventId == "buy" && (m_amountItems > 0 && m_amountItems <= m_maxItems)) { - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_NPC_BUY_REQUEST); outMsg.writeInt16(8); outMsg.writeInt16(m_amountItems); diff --git a/src/gui/buy.h b/src/gui/buy.h index 7e9ef069..6a1c9829 100644 --- a/src/gui/buy.h +++ b/src/gui/buy.h @@ -34,6 +34,8 @@ #include "../guichanfwd.h" +class Network; + /** * The buy dialog. * @@ -48,7 +50,7 @@ class BuyDialog : public Window, public gcn::ActionListener, * * @see Window::Window */ - BuyDialog(); + BuyDialog(Network *network); /** * Resets the dialog, clearing shop inventory. @@ -86,6 +88,7 @@ class BuyDialog : public Window, public gcn::ActionListener, std::string getElementAt(int i); private: + Network *mNetwork; gcn::Button *buyButton; gcn::Button *quitButton; gcn::Button *increaseButton; diff --git a/src/gui/buysell.cpp b/src/gui/buysell.cpp index 178c2719..6547a849 100644 --- a/src/gui/buysell.cpp +++ b/src/gui/buysell.cpp @@ -25,17 +25,14 @@ #include "button.h" -#include "../game.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../npc.h" BuySellDialog::BuySellDialog(): Window("Shop") { - buyButton = new Button("Buy"); - sellButton = new Button("Sell"); - cancelButton = new Button("Cancel"); + gcn::Button *buyButton = new Button("Buy"); + gcn::Button *sellButton = new Button("Sell"); + gcn::Button *cancelButton = new Button("Cancel"); buyButton->setPosition(10, 10); sellButton->setPosition( @@ -63,22 +60,13 @@ BuySellDialog::BuySellDialog(): void BuySellDialog::action(const std::string& eventId) { - int actionId = -1; - if (eventId == "buy") { - actionId = 0; + current_npc->buy(); } else if (eventId == "sell") { - actionId = 1; + current_npc->sell(); } else if (eventId == "cancel") { current_npc = 0; } - if (actionId > -1) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_NPC_BUY_SELL_REQUEST); - outMsg.writeInt32(current_npc); - outMsg.writeInt8(actionId); - } - setVisible(false); } diff --git a/src/gui/buysell.h b/src/gui/buysell.h index a3e54df0..2d3c7bd3 100644 --- a/src/gui/buysell.h +++ b/src/gui/buysell.h @@ -28,8 +28,6 @@ #include "window.h" -#include "../guichanfwd.h" - /** * A dialog to choose between buying or selling at a shop. * @@ -50,11 +48,6 @@ class BuySellDialog : public Window, public gcn::ActionListener * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - - private: - gcn::Button *buyButton; - gcn::Button *sellButton; - gcn::Button *cancelButton; }; #endif diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index 209284f7..b86a01e3 100644 --- a/src/gui/char_select.cpp +++ b/src/gui/char_select.cpp @@ -25,66 +25,52 @@ #include <sstream> #include <string> -#include <SDL.h> #include <guichan/widgets/label.hpp> #include "button.h" +#include "confirm_dialog.h" #include "ok_dialog.h" #include "playerbox.h" #include "textfield.h" #include "windowcontainer.h" -#include "../being.h" + #include "../game.h" -#include "../log.h" +#include "../localplayer.h" #include "../main.h" -#include "../playerinfo.h" -#include "../net/messagein.h" #include "../net/messageout.h" -#include "../net/network.h" -#include "../net/protocol.h" -CharSelectDialog::CharDeleteConfirm::CharDeleteConfirm(CharSelectDialog *m): +/** + * Listener for confirming character deletion. + */ +class CharDeleteConfirm : public ConfirmDialog +{ + public: + CharDeleteConfirm(CharSelectDialog *master); + void action(const std::string &eventId); + private: + CharSelectDialog *master; +}; + +CharDeleteConfirm::CharDeleteConfirm(CharSelectDialog *m): ConfirmDialog(m, "Confirm", "Are you sure you want to delete this character?"), - master(m), mStatus(0) + master(m) { } -void CharSelectDialog::CharDeleteConfirm::action(const std::string &eventId) +void CharDeleteConfirm::action(const std::string &eventId) { //ConfirmDialog::action(eventId); if (eventId == "yes") { master->attemptCharDelete(); - ConfirmDialog::yesButton->setEnabled(false); - ConfirmDialog::noButton->setEnabled(false); - mStatus = 1; - } - else - { - ConfirmDialog::action(eventId); } + ConfirmDialog::action(eventId); } -void CharSelectDialog::CharDeleteConfirm::logic() -{ - if (mStatus == 1) - { - if (packetReady()) - { - master->checkCharDelete(); - ConfirmDialog::action("yes"); - } - else - { - flush(); - } - } -} - -CharSelectDialog::CharSelectDialog(): - Window("Select Character"), mStatus(0), mCurrentSlot(0) +CharSelectDialog::CharSelectDialog(Network *network, LockedArray<LocalPlayer*> *charInfo): + Window("Select Character"), mNetwork(network), mCharInfo(charInfo) { selectButton = new Button("Ok"); cancelButton = new Button("Cancel"); @@ -148,25 +134,7 @@ CharSelectDialog::CharSelectDialog(): selectButton->requestFocus(); setLocationRelativeTo(getParent()); - setPlayerInfo(NULL); -} - -void CharSelectDialog::changeSlot(int slot) -{ - mCurrentSlot = slot; - if (mCurrentSlot < 0) - { - mCurrentSlot = MAX_SLOT; - } - else if (mCurrentSlot > MAX_SLOT) - { - mCurrentSlot = 0; - } - - if (char_info[mCurrentSlot] == NULL) - { - newCharButton->setEnabled(true); - } + updatePlayerInfo(); } void CharSelectDialog::action(const std::string& eventId) @@ -180,7 +148,6 @@ void CharSelectDialog::action(const std::string& eventId) previousButton->setEnabled(false); nextButton->setEnabled(false); attemptCharSelect(); - mStatus = 1; } else if (eventId == "cancel") { @@ -191,33 +158,36 @@ void CharSelectDialog::action(const std::string& eventId) if (n_character < MAX_SLOT + 1) { // Start new character dialog - new CharCreateDialog(this, mCurrentSlot); + new CharCreateDialog(this, mCharInfo->getPos(), mNetwork); + mCharInfo->lock(); } } else if (eventId == "delete") { // Delete character - if (n_character > 0) + if (mCharInfo->getEntry()) { new CharDeleteConfirm(this); } } else if (eventId == "previous") { - changeSlot(mCurrentSlot - 1); + mCharInfo->prev(); } else if (eventId == "next") { - changeSlot(mCurrentSlot + 1); + mCharInfo->next(); } } -void CharSelectDialog::setPlayerInfo(PLAYER_INFO *pi) +void CharSelectDialog::updatePlayerInfo() { + LocalPlayer *pi = mCharInfo->getEntry(); + if (pi) { std::stringstream nameCaption, levelCaption, jobCaption, moneyCaption; - nameCaption << pi->name; + nameCaption << pi->getName(); levelCaption << "Lvl: " << pi->lvl; jobCaption << "Job Lvl: " << pi->jobLvl; moneyCaption << "Gold: " << pi->gp; @@ -226,14 +196,11 @@ void CharSelectDialog::setPlayerInfo(PLAYER_INFO *pi) levelLabel->setCaption(levelCaption.str()); jobLevelLabel->setCaption(jobCaption.str()); moneyLabel->setCaption(moneyCaption.str()); - if (mStatus != 1) - { - newCharButton->setEnabled(false); - delCharButton->setEnabled(true); - selectButton->setEnabled(true); - } - playerBox->hairStyle = pi->hairStyle - 1; - playerBox->hairColor = pi->hairColor - 1; + newCharButton->setEnabled(false); + delCharButton->setEnabled(true); + selectButton->setEnabled(true); + playerBox->hairStyle = pi->getHairStyle() - 1; + playerBox->hairColor = pi->getHairColor() - 1; playerBox->showPlayer = true; } else { @@ -254,141 +221,29 @@ void CharSelectDialog::setPlayerInfo(PLAYER_INFO *pi) void CharSelectDialog::attemptCharDelete() { // Request character deletion - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(0x0068); - outMsg.writeInt32(char_info[mCurrentSlot]->id); + outMsg.writeInt32(mCharInfo->getEntry()->mLoginId); outMsg.writeString("a@a.com", 40); -} - -void CharSelectDialog::checkCharDelete() -{ - MessageIn msg = get_next_message(); - - if (msg.getId() == 0x006f) - { - skip(msg.getLength()); - delete char_info[mCurrentSlot]; - n_character--; - char_info[mCurrentSlot] = NULL; - setPlayerInfo(NULL); - new OkDialog(this, "Info", "Player deleted"); - } - else if (msg.getId() == 0x0070) - { - new OkDialog(this, "Error", "Failed to delete character."); - skip(msg.getLength()); - } - else { - new OkDialog(this, "Error", "Unknown"); - skip(msg.getLength()); - } + mCharInfo->lock(); } void CharSelectDialog::attemptCharSelect() { // Request character selection - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(0x0066); - outMsg.writeInt8(mCurrentSlot); -} - -void -CharSelectDialog::checkCharSelect() -{ - // Receive reply - MessageIn msg = get_next_message(); - if (state == ERROR_STATE) - { - return; - } - - logger->log("CharSelect: Packet ID: %x, Length: %d, in_size: %d", - msg.getId(), msg.getLength(), in_size); - - if (msg.getId() == 0x0071) - { - char_ID = msg.readInt32(); - map_path = "maps/" + msg.readString(16); - map_path = map_path.substr(0, map_path.rfind(".")) + ".tmx.gz"; - map_address = msg.readInt32(); - map_port = msg.readInt16(); - player_info = char_info[mCurrentSlot]; - // Clear unselected players infos - for (int i = 0; i < MAX_SLOT + 1; i++) - { - if (i != mCurrentSlot) - { - delete char_info[i]; - } - } - free(char_info); - state = CONNECTING_STATE; - - logger->log("CharSelect: Map: %s", map_path.c_str()); - logger->log("CharSelect: Server: %s:%i", iptostring(map_address), - map_port); - closeConnection(); - } - else if (msg.getId() == 0x006c) - { - switch (msg.readInt8()) { - case 0: - errorMessage = "Access denied"; - break; - case 1: - errorMessage = "Cannot use this ID"; - break; - default: - errorMessage = "Unknown failure to select character"; - break; - } - skip(msg.getLength()); - } - else if (msg.getId() == 0x0081) - { - switch (msg.readInt8()) { - case 1: - errorMessage = "Map server offline"; - break; - case 3: - errorMessage = "Speed hack detected"; - break; - case 8: - errorMessage = "Duplicated login"; - break; - default: - errorMessage = "Unkown error with 0x0081"; - break; - } - closeConnection(); - state = ERROR_STATE; - } - - // Todo: add other packets + outMsg.writeInt8(mCharInfo->getPos()); + mCharInfo->lock(); } void CharSelectDialog::logic() { - if (n_character > 0) - { - setPlayerInfo(char_info[mCurrentSlot]); - } - - if (mStatus == 1) - { - if (packetReady()) - { - checkCharSelect(); - } - else - { - flush(); - } - } + updatePlayerInfo(); } -CharCreateDialog::CharCreateDialog(Window *parent, int slot): - Window("Create Character", true, parent), mStatus(0), mSlot(slot) +CharCreateDialog::CharCreateDialog(Window *parent, int slot, Network *network): + Window("Create Character", true, parent), mNetwork(network), mSlot(slot) { nameField = new TextField(""); nameLabel = new gcn::Label("Name:"); @@ -454,21 +309,6 @@ CharCreateDialog::CharCreateDialog(Window *parent, int slot): setLocationRelativeTo(getParent()); } -void CharCreateDialog::logic() -{ - if (mStatus == 1) - { - if (packetReady()) - { - checkCharCreate(); - } - else - { - flush(); - } - } -} - void CharCreateDialog::action(const std::string& eventId) { if (eventId == "create") { @@ -476,7 +316,7 @@ void CharCreateDialog::action(const std::string& eventId) // Attempt to create the character createButton->setEnabled(false); attemptCharCreate(); - mStatus = 1; + windowContainer->scheduleDelete(this); } else { new OkDialog(this, "Error", @@ -511,7 +351,7 @@ std::string CharCreateDialog::getName() void CharCreateDialog::attemptCharCreate() { // Send character infos - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(0x0067); outMsg.writeString(getName(), 24); outMsg.writeInt8(5); @@ -524,73 +364,3 @@ void CharCreateDialog::attemptCharCreate() outMsg.writeInt16(playerBox->hairColor + 1); outMsg.writeInt16(playerBox->hairStyle + 1); } - -void CharCreateDialog::checkCharCreate() -{ - MessageIn msg = get_next_message(); - - if (msg.getId() == 0x006d) - { - PLAYER_INFO *tempPlayer = new PLAYER_INFO; - - tempPlayer->id = msg.readInt32(); - tempPlayer->xp = msg.readInt32(); - tempPlayer->gp = msg.readInt32(); - tempPlayer->jobXp = msg.readInt32(); - tempPlayer->jobLvl = msg.readInt32(); - msg.skip(8); // unknown - msg.readInt32(); // option - msg.readInt32(); // karma - msg.readInt32(); // manner - msg.skip(2); // unknown - tempPlayer->hp = msg.readInt16(); - tempPlayer->maxHp = msg.readInt16(); - tempPlayer->mp = msg.readInt16(); - tempPlayer->maxMp = msg.readInt16(); - msg.readInt16(); // speed - msg.readInt16(); // class - tempPlayer->hairStyle = msg.readInt16(); - tempPlayer->weapon = msg.readInt16(); - tempPlayer->lvl = msg.readInt16(); - msg.readInt16(); // skill point - msg.readInt16(); // head bottom - msg.readInt16(); // shield - msg.readInt16(); // head option top - msg.readInt16(); // head option mid - tempPlayer->hairColor = msg.readInt16(); - msg.readInt16(); // unknown - tempPlayer->name = msg.readString(24); - tempPlayer->STR = msg.readInt8(); - tempPlayer->AGI = msg.readInt8(); - tempPlayer->VIT = msg.readInt8(); - tempPlayer->INT = msg.readInt8(); - tempPlayer->DEX = msg.readInt8(); - tempPlayer->LUK = msg.readInt8(); - int slot = msg.readInt8(); // character slot - msg.readInt8(); // unknown - - n_character++; - char_info[slot] = tempPlayer; - windowContainer->scheduleDelete(this); - } - else if (msg.getId() == 0x006e) - { - new OkDialog(this, "Error", "Failed to create character"); - createButton->setEnabled(true); - } - else - { - new OkDialog(this, "Error", "Unknown error"); - createButton->setEnabled(true); - } - - skip(msg.getLength()); -} - -void charSelectInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; - } -} diff --git a/src/gui/char_select.h b/src/gui/char_select.h index 7db67699..94d69e06 100644 --- a/src/gui/char_select.h +++ b/src/gui/char_select.h @@ -24,15 +24,14 @@ #ifndef _CHAR_SELECT_H #define _CHAR_SELECT_H -#include "confirm_dialog.h" #include "window.h" #include "../guichanfwd.h" +#include "../lockedarray.h" -#include <SDL_events.h> - +class LocalPlayer; +class Network; class PlayerBox; -struct PLAYER_INFO; /** * Character selection dialog. @@ -42,18 +41,22 @@ struct PLAYER_INFO; class CharSelectDialog : public Window, public gcn::ActionListener { public: + friend class CharDeleteConfirm; /** * Constructor. */ - CharSelectDialog(); + CharSelectDialog(Network *network, LockedArray<LocalPlayer*> *charInfo); void action(const std::string& eventId); - void setPlayerInfo(PLAYER_INFO* pi); + void updatePlayerInfo(); void logic(); private: + Network *mNetwork; + LockedArray<LocalPlayer*> *mCharInfo; + gcn::Button *selectButton; gcn::Button *cancelButton; gcn::Button *newCharButton; @@ -65,47 +68,18 @@ class CharSelectDialog : public Window, public gcn::ActionListener gcn::Label *levelLabel; gcn::Label *jobLevelLabel; gcn::Label *moneyLabel; - - int mStatus; - int mCurrentSlot; PlayerBox *playerBox; - - void changeSlot(int slot); /** * Communicate character deletion to the server. */ void attemptCharDelete(); - - /** - * Check server answer. - */ - void checkCharDelete(); /** * Communicate character selection to the server. */ void attemptCharSelect(); - - /** - * Check server answer. - */ - void checkCharSelect(); - - /** - * Listener for confirming character deletion. - */ - class CharDeleteConfirm : public ConfirmDialog - { - public: - CharDeleteConfirm(CharSelectDialog *master); - void action(const std::string &eventId); - void logic(); - private: - CharSelectDialog *master; - int mStatus; - }; }; /** @@ -119,15 +93,14 @@ class CharCreateDialog : public Window, public gcn::ActionListener /** * Constructor. */ - CharCreateDialog(Window *parent = NULL, int slot = 0); - - void logic(); + CharCreateDialog(Window *parent, int slot, Network *network); void action(const std::string& eventId); std::string getName(); private: + Network *mNetwork; gcn::TextField *nameField; gcn::Label *nameLabel; gcn::Button *nextHairColorButton; @@ -140,21 +113,13 @@ class CharCreateDialog : public Window, public gcn::ActionListener gcn::Button *cancelButton; PlayerBox *playerBox; - - int mStatus; + int mSlot; /** * Communicate character creation to the server. */ void attemptCharCreate(); - - /** - * Receive new char info. - */ - void checkCharCreate(); }; -void charSelectInputHandler(SDL_KeyboardEvent *keyEvent); - #endif diff --git a/src/gui/char_server.cpp b/src/gui/char_server.cpp index ed0818c8..c66d4436 100644 --- a/src/gui/char_server.cpp +++ b/src/gui/char_server.cpp @@ -24,26 +24,18 @@ #include "char_server.h" #include <sstream> -#include <SDL.h> #include "button.h" #include "listbox.h" -#include "ok_dialog.h" #include "scrollarea.h" -#include "../log.h" #include "../main.h" -#include "../playerinfo.h" #include "../serverinfo.h" -#include "../net/messagein.h" -#include "../net/messageout.h" -#include "../net/network.h" - extern SERVER_INFO **server_info; ServerSelectDialog::ServerSelectDialog(): - Window("Select Server"), mStatus(NET_IDLE) + Window("Select Server") { serverListModel = new ServerListModel(); serverList = new ListBox(serverListModel); @@ -98,51 +90,17 @@ void ServerSelectDialog::action(const std::string& eventId) { if (eventId == "ok") { - int index = serverList->getSelected(); - const char *host = iptostring(server_info[index]->address); - short port = server_info[index]->port; - openConnection(host, port); okButton->setEnabled(false); - //cancelButton->setEnabled(false); - mStatus = NET_CONNECTING; + state = CHAR_CONNECT_STATE; } else if (eventId == "cancel") { state = LOGIN_STATE; } } -void -ServerSelectDialog::logic() +SERVER_INFO* ServerSelectDialog::getServerInfo() { - switch (mStatus) - { - case NET_CONNECTING: - mStatus = pollConnection(); - break; - case NET_ERROR: - logger->log("ServerSelect::Unable to connect"); - errorMessage = "Unable to connect to char server"; - state = ERROR_STATE; - closeConnection(); - break; - case NET_CONNECTED: - attemptServerSelect(serverList->getSelected()); - mStatus = NET_DATA; - break; - case NET_DATA: - // TODO: cannot substitute with packetReady() because of eAthena - // sending 4 unknown bytes. - if (in_size > 6) - { - skip(4); - checkServerSelect(); - } - else - { - flush(); - } - break; - } + return server_info[serverList->getSelected()]; } int @@ -158,113 +116,3 @@ ServerListModel::getElementAt(int i) s << server_info[i]->name << " (" << server_info[i]->online_users << ")"; return s.str(); } - -void -charServerInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = LOGIN_STATE; - } -} - -void -ServerSelectDialog::attemptServerSelect(int index) -{ - // Send login infos - MessageOut outMsg; - outMsg.writeInt16(0x0065); - outMsg.writeInt32(account_ID); - outMsg.writeInt32(session_ID1); - outMsg.writeInt32(session_ID2); - outMsg.writeInt16(0); // unknown - outMsg.writeInt8(sex); -} - -void -ServerSelectDialog::checkServerSelect() -{ - MessageIn msg = get_next_message(); - - if (msg.getId() == 0x006b) - { - // Skip length word and an additional mysterious 20 bytes - msg.skip(2 + 20); - - // Derive number of characters from message length - n_character = (msg.getLength() - 24) / 106; - char_info = (PLAYER_INFO**)malloc(sizeof(PLAYER_INFO*) * (MAX_SLOT+1)); - for (int i = 0; i < MAX_SLOT + 1; i++) - char_info[i] = NULL; - - for (int i = 0; i < n_character; i++) - { - PLAYER_INFO *tempPlayer = new PLAYER_INFO; - - tempPlayer->totalWeight = 0; - tempPlayer->maxWeight = 0; - tempPlayer->lastAttackTime = 0; - tempPlayer->id = msg.readInt32(); - tempPlayer->xp = msg.readInt32(); - tempPlayer->gp = msg.readInt32(); - tempPlayer->jobXp = msg.readInt32(); - tempPlayer->jobLvl = msg.readInt32(); - msg.skip(8); // unknown - msg.readInt32(); // option - msg.readInt32(); // karma - msg.readInt32(); // manner - msg.skip(2); // unknown - tempPlayer->hp = msg.readInt16(); - tempPlayer->maxHp = msg.readInt16(); - tempPlayer->mp = msg.readInt16(); - tempPlayer->maxMp = msg.readInt16(); - msg.readInt16(); // speed - msg.readInt16(); // class - tempPlayer->hairStyle = msg.readInt16(); - tempPlayer->weapon = msg.readInt16(); - tempPlayer->lvl = msg.readInt16(); - msg.readInt16(); // skill point - msg.readInt16(); // head bottom - msg.readInt16(); // shield - msg.readInt16(); // head option top - msg.readInt16(); // head option mid - tempPlayer->hairColor = msg.readInt16(); - msg.readInt16(); // unknown - tempPlayer->name = msg.readString(24); - tempPlayer->STR = msg.readInt8(); - tempPlayer->AGI = msg.readInt8(); - tempPlayer->VIT = msg.readInt8(); - tempPlayer->INT = msg.readInt8(); - tempPlayer->DEX = msg.readInt8(); - tempPlayer->LUK = msg.readInt8(); - int slot = msg.readInt8(); // character slot - msg.readInt8(); // unknown - - char_info[slot] = tempPlayer; - - logger->log("CharServer: Player: %s (%d)", - char_info[slot]->name.c_str(), slot); - } - - state = CHAR_SELECT_STATE; - skip(msg.getLength()); - } - else if (msg.getId() == 0x006c) - { - std::string errorStr; - switch (msg.readInt8()) { - case 0: errorStr = "Access denied"; break; - case 1: errorStr = "Cannot use this ID"; break; - default: errorStr = "Rejected from server"; break; - } - new OkDialog("Error", errorStr); - skip(msg.getLength()); - closeConnection(); - } - else - { - new OkDialog("Error", "Unknown error"); - skip(msg.getLength()); - } - // Todo: add other packets -} diff --git a/src/gui/char_server.h b/src/gui/char_server.h index f36ee76e..ed6e4c14 100644 --- a/src/gui/char_server.h +++ b/src/gui/char_server.h @@ -26,12 +26,13 @@ #include <guichan/actionlistener.hpp> #include <guichan/listmodel.hpp> -#include <SDL_events.h> #include "window.h" #include "../guichanfwd.h" +class SERVER_INFO; + /** * The list model for the server list. */ @@ -68,9 +69,9 @@ class ServerSelectDialog : public Window, public gcn::ActionListener { void action(const std::string& eventId); /** - * Updates dialog logic + * Returns the index of the selected server */ - void logic(); + SERVER_INFO* getServerInfo(); private: ServerListModel *serverListModel; @@ -78,12 +79,6 @@ class ServerSelectDialog : public Window, public gcn::ActionListener { gcn::Button *okButton; gcn::Button *cancelButton; gcn::ScrollArea *scrollArea; - int mStatus; - - void attemptServerSelect(int index); - void checkServerSelect(); }; -void charServerInputHandler(SDL_KeyboardEvent *keyEvent); - #endif diff --git a/src/gui/chargedialog.cpp b/src/gui/chargedialog.cpp index 4ef28e16..da02cada 100644 --- a/src/gui/chargedialog.cpp +++ b/src/gui/chargedialog.cpp @@ -28,7 +28,7 @@ #include "progressbar.h" -#include "../playerinfo.h" +#include "../localplayer.h" ChargeDialog::ChargeDialog(): Window("") @@ -42,15 +42,15 @@ ChargeDialog::ChargeDialog(): void ChargeDialog::logic() { // calculate time since the last attack was made - player_info->lastAttackTime += .01; // this a hack until someone explains + player_node->lastAttackTime += .01; // this a hack until someone explains // to me how to work the timer - if (player_info->lastAttackTime > 1) + if (player_node->lastAttackTime > 1) { - player_info->lastAttackTime = 1; + player_node->lastAttackTime = 1; } // reset the progress bar to display accurate time since attack - progBar->setProgress(player_info->lastAttackTime); + progBar->setProgress(player_node->lastAttackTime); Window::logic(); } diff --git a/src/gui/chargedialog.h b/src/gui/chargedialog.h index d17bdeb2..22dc5d23 100644 --- a/src/gui/chargedialog.h +++ b/src/gui/chargedialog.h @@ -23,8 +23,6 @@ #ifndef _TMW_CHARGE_H #define _TMW_CHARGE_H -#include <guichan/actionlistener.hpp> - #include "window.h" class ProgressBar; diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 748f4848..29fbebfe 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -34,16 +34,17 @@ #include "../game.h" #include "../graphics.h" +#include "../localplayer.h" #include "../log.h" -#include "../playerinfo.h" #include "../net/messageout.h" #include "../net/protocol.h" extern Graphics *graphics; -ChatWindow::ChatWindow(const std::string &logfile): +ChatWindow::ChatWindow(const std::string &logfile, Network *network): Window(""), + mNetwork(network), mTmpVisible(false) { setWindowName("Chat"); @@ -206,7 +207,7 @@ ChatWindow::action(const std::string& eventId) curHist = history.end(); // Send the message to the server - chatSend(player_info->name.c_str(), message.c_str()); + chatSend(player_node->getName().c_str(), message.c_str()); // Clear the text from the chat input chatInput->setText(""); @@ -262,7 +263,7 @@ ChatWindow::chatSend(std::string nick, std::string msg) if (msg.substr(0, IS_ANNOUNCE_LENGTH) == IS_ANNOUNCE) { msg.erase(0, IS_ANNOUNCE_LENGTH); - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(0x0099); outMsg.writeInt16(msg.length() + 4); outMsg.writeString(msg, msg.length()); @@ -281,7 +282,7 @@ ChatWindow::chatSend(std::string nick, std::string msg) } else if (msg.substr(0, IS_WHO_LENGTH) == IS_WHO) { - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(0x00c1); } else @@ -295,7 +296,7 @@ ChatWindow::chatSend(std::string nick, std::string msg) nick += msg; msg = nick; - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_CHAT_MESSAGE); outMsg.writeInt16(msg.length() + 4); outMsg.writeString(msg, msg.length()); diff --git a/src/gui/chat.h b/src/gui/chat.h index 5b470e27..63b30db3 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -36,6 +36,7 @@ #include "../guichanfwd.h" class BrowserBox; +class Network; class ScrollArea; #define BY_GM 0 // those should be self-explanatory =) @@ -116,7 +117,7 @@ class ChatWindow : public Window, public gcn::ActionListener, /** * Constructor. */ - ChatWindow(const std::string &logfile); + ChatWindow(const std::string &logfile, Network *network); /** * Destructor. @@ -193,6 +194,7 @@ class ChatWindow : public Window, public gcn::ActionListener, void setVisible(bool visible); private: + Network *mNetwork; std::ofstream chatlog_file; bool mTmpVisible; diff --git a/src/gui/connection.cpp b/src/gui/connection.cpp index c7eb3667..372537a8 100644 --- a/src/gui/connection.cpp +++ b/src/gui/connection.cpp @@ -23,22 +23,29 @@ #include "connection.h" +#include <guichan/actionlistener.hpp> + #include <guichan/widgets/label.hpp> #include "button.h" #include "progressbar.h" -#include "../game.h" -#include "../log.h" #include "../main.h" -#include "../net/messagein.h" -#include "../net/messageout.h" -#include "../net/network.h" -#include "../net/protocol.h" +class ConnectionActionListener : public gcn::ActionListener +{ + public: + void action(const std::string& eventId) + { + if (eventId == "cancel") + { + state = EXIT_STATE; + } + } +} connectionActionListener; ConnectionDialog::ConnectionDialog(): - Window("Info"), mProgress(0), mStatus(NET_CONNECTING) + Window("Info"), mProgress(0) { setContentSize(200, 100); @@ -46,7 +53,7 @@ ConnectionDialog::ConnectionDialog(): cancelButton = new Button("Cancel"); cancelButton->setPosition(5, 100 - 5 - cancelButton->getHeight()); cancelButton->setEventId("cancel"); - cancelButton->addActionListener(this); + cancelButton->addActionListener(&connectionActionListener); mProgressBar = new ProgressBar(0.0, 5, cancelButton->getY() - 25, 200 - 10, 20, 128, 128, 128); @@ -59,9 +66,6 @@ ConnectionDialog::ConnectionDialog(): add(mProgressBar); setLocationRelativeTo(getParent()); - - const char *host = iptostring(map_address); - openConnection(host, map_port); } void ConnectionDialog::logic() @@ -73,91 +77,4 @@ void ConnectionDialog::logic() } mProgressBar->setProgress(mProgress); Window::logic(); - - switch (mStatus) - { - case NET_CONNECTING: - mStatus = pollConnection(); - break; - case NET_ERROR: - logger->log("Connection::Unable to connect"); - errorMessage = "Unable to connect to map server"; - state = ERROR_STATE; - closeConnection(); - break; - case NET_CONNECTED: - attemptMapLogin(); - mStatus = NET_DATA; - break; - case NET_DATA: - if (in_size > 6) - { - skip(4); - checkMapLogin(); - state = GAME_STATE; - } - else - { - flush(); - } - break; - } -} - -void ConnectionDialog::action(const std::string& eventId) -{ - if (eventId == "cancel") - { - state = EXIT_STATE; - } -} - -void ConnectionDialog::attemptMapLogin() -{ - // Send login infos - MessageOut outMsg; - outMsg.writeInt16(0x0072); - outMsg.writeInt32(account_ID); - outMsg.writeInt32(char_ID); - outMsg.writeInt32(session_ID1); - outMsg.writeInt32(session_ID2); - outMsg.writeInt8(sex); -} - -void ConnectionDialog::checkMapLogin() -{ - MessageIn msg = get_next_message(); - - if (msg.getId() == SMSG_LOGIN_SUCCESS) - { - unsigned char direction; - msg.readInt32(); // server tick - msg.readCoordinates(startX, startY, direction); - msg.skip(2); // unknown - logger->log("Protocol: Player start position: (%d, %d), Direction: %d", - startX, startY, direction); - } - else if (msg.getId() == 0x0081) - { - logger->log("Warning: Map server D/C"); - } - else - { - logger->error("Unknown packet: map_start"); - } - - skip(msg.getLength()); - - // Send "map loaded" - // TODO: be able to reuse the same msg - MessageOut newMsg; - newMsg.writeInt16(0x007d); -} - -void connectionInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; - } } diff --git a/src/gui/connection.h b/src/gui/connection.h index 342b9f8d..7a072d2e 100644 --- a/src/gui/connection.h +++ b/src/gui/connection.h @@ -24,14 +24,8 @@ #ifndef _TMW_CONNECTION_H #define _TMW_CONNECTION_H -#include <iosfwd> -#include <guichan/actionlistener.hpp> -#include <SDL_events.h> - #include "window.h" -#include "../guichanfwd.h" - class ProgressBar; /** @@ -39,7 +33,7 @@ class ProgressBar; * * \ingroup Interface */ -class ConnectionDialog : public Window, public gcn::ActionListener +class ConnectionDialog : public Window { public: /** @@ -49,25 +43,11 @@ class ConnectionDialog : public Window, public gcn::ActionListener */ ConnectionDialog(); - /** - * Called when receiving actions from the widgets. - */ - void action(const std::string& eventId); - void logic(); private: ProgressBar *mProgressBar; float mProgress; - int mStatus; - - void attemptMapLogin(); - void checkMapLogin(); }; -/** - * Handle input - */ -void connectionInputHandler(SDL_KeyboardEvent *keyEvent); - #endif diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp index d3b5fc6b..f1b9b4ba 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -35,8 +35,8 @@ #include <sstream> -EquipmentWindow::EquipmentWindow(): - Window("Equipment") +EquipmentWindow::EquipmentWindow(Equipment *equipment): + Window("Equipment"), mEquipment(equipment) { setWindowName("Equipment"); setDefaultSize(5, 230, 200, 90); @@ -57,7 +57,6 @@ void EquipmentWindow::draw(gcn::Graphics *graphics) // Draw window graphics Window::draw(graphics); - Equipment *equipment = Equipment::getInstance(); Item *item; Image *image; @@ -66,7 +65,7 @@ void EquipmentWindow::draw(gcn::Graphics *graphics) graphics->drawRectangle(gcn::Rectangle(10 + 36 * (i % 4), 36 * (i / 4) + 25, 32, 32)); - if (!(item = equipment->getEquipment(i))) { + if (!(item = mEquipment->getEquipment(i))) { continue; } @@ -78,7 +77,7 @@ void EquipmentWindow::draw(gcn::Graphics *graphics) graphics->setColor(gcn::Color(0, 0, 0)); graphics->drawRectangle(gcn::Rectangle(160, 25, 32, 32)); - if (!(item = equipment->getArrows())) { + if (!(item = mEquipment->getArrows())) { return; } diff --git a/src/gui/equipmentwindow.h b/src/gui/equipmentwindow.h index a3241057..651c593a 100644 --- a/src/gui/equipmentwindow.h +++ b/src/gui/equipmentwindow.h @@ -24,10 +24,9 @@ #ifndef _TMW_EQUIPMENT_H #define _TMW_EQUIPMENT_H -#include <guichan/actionlistener.hpp> - #include "window.h" +class Equipment; class Spriteset; /** @@ -41,7 +40,7 @@ class EquipmentWindow : public Window /** * Constructor. */ - EquipmentWindow(); + EquipmentWindow(Equipment *equipment); /** * Destructor. @@ -56,6 +55,8 @@ class EquipmentWindow : public Window private: Spriteset *itemset; + Equipment *mEquipment; + }; extern EquipmentWindow *equipmentWindow; diff --git a/src/gui/error.cpp b/src/gui/error.cpp index 42f4b217..4a980caa 100644 --- a/src/gui/error.cpp +++ b/src/gui/error.cpp @@ -41,11 +41,3 @@ void ErrorDialog::action(const std::string& eventId) state = LOGIN_STATE; } } - -void errorInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; - } -} diff --git a/src/gui/error.h b/src/gui/error.h index 9eb0cbde..feb864f2 100644 --- a/src/gui/error.h +++ b/src/gui/error.h @@ -25,7 +25,6 @@ #define _TMW_ERROR_H #include <iosfwd> -#include <SDL_events.h> #include "ok_dialog.h" @@ -54,9 +53,4 @@ class ErrorDialog : public OkDialog { void action(const std::string& eventId); }; -/** - * Handle input - */ -void errorInputHandler(SDL_KeyboardEvent *keyEvent); - #endif diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 7149e5e8..491a4009 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -39,24 +39,22 @@ #include "windowcontainer.h" #include "../being.h" +#include "../beingmanager.h" #include "../configlistener.h" #include "../configuration.h" #include "../engine.h" #include "../floor_item.h" -#include "../game.h" #include "../graphics.h" +#include "../localplayer.h" #include "../log.h" #include "../main.h" #include "../map.h" - -#include "../net/protocol.h" +#include "../npc.h" #include "../resources/image.h" #include "../resources/resourcemanager.h" #include "../resources/sdlimageloader.h" -extern Being* autoTarget; - // Guichan stuff Gui *gui; gcn::SDLInput *guiInput; // GUI input @@ -262,13 +260,12 @@ Gui::mousePress(int mx, int my, int button) Being *being; FloorItem *floorItem; - if ((being = findNode(tilex, tiley)) && being != player_node) + if ((being = beingManager->findBeing(tilex, tiley)) && being->getType() != Being::LOCALPLAYER) { showPopup(mx, my, being); return; } - else if((floorItem = find_floor_item_by_id( - find_floor_item_by_cor(tilex, tiley)))) + else if((floorItem = find_floor_item_by_cor(tilex, tiley))) { showPopup(mx, my, floorItem); return; @@ -287,27 +284,23 @@ Gui::mousePress(int mx, int my, int button) if (button == gcn::MouseInput::LEFT) { Being *being; - Uint32 floorItemId; + FloorItem *item; // Interact with some being - if ((being = findNode(tilex, tiley))) + if ((being = beingManager->findBeing(tilex, tiley))) { switch (being->getType()) { case Being::NPC: - talk(being); - current_npc = being->getId(); + dynamic_cast<NPC*>(being)->talk(); break; case Being::MONSTER: case Being::PLAYER: - if (being->action == Being::MONSTER_DEAD || - player_node->action != Being::STAND || - being == player_node) + if (being->action == Being::MONSTER_DEAD) break; - autoTarget = being; - attack(being); + player_node->attack(being, true); break; default: @@ -315,13 +308,13 @@ Gui::mousePress(int mx, int my, int button) } } // Pick up some item - else if ((floorItemId = find_floor_item_by_cor(tilex, tiley))) + else if ((item = find_floor_item_by_cor(tilex, tiley))) { int dx = tilex - player_node->x; int dy = tiley - player_node->y; if ((dx * dx + dy * dy) < 4) - pickUp(floorItemId); + player_node->pickUp(item); } // Just walk around else if (engine->getCurrentMap()->getWalk(tilex, tiley)) @@ -330,10 +323,8 @@ Gui::mousePress(int mx, int my, int button) Uint8 *keys = SDL_GetKeyState(NULL); if (!(keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT])) { - walk(tilex, tiley, 0); player_node->setDestination(tilex, tiley); - - autoTarget = NULL; + player_node->stopAttack(); } } } diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 0b8a37a7..3b786f62 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -36,14 +36,11 @@ #include "itemcontainer.h" #include "scrollarea.h" -#include "../inventory.h" #include "../item.h" -#include "../playerinfo.h" +#include "../localplayer.h" #include "../resources/iteminfo.h" -extern Inventory *inventory; - InventoryWindow::InventoryWindow(): Window("Inventory") { @@ -56,7 +53,7 @@ InventoryWindow::InventoryWindow(): useButton = new Button("Use"); dropButton = new Button("Drop"); - items = new ItemContainer(inventory); + items = new ItemContainer(player_node->mInventory); invenScroll = new ScrollArea(items); invenScroll->setPosition(8, 8); invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -98,8 +95,8 @@ void InventoryWindow::logic() // Update weight information std::stringstream tempstr; - tempstr << "Total Weight: " << player_info->totalWeight - << " - Maximum Weight: " << player_info->maxWeight; + tempstr << "Total Weight: " << player_node->totalWeight + << " - Maximum Weight: " << player_node->maxWeight; weightLabel->setCaption(tempstr.str()); weightLabel->adjustSize(); } @@ -115,14 +112,14 @@ void InventoryWindow::action(const std::string &eventId) if (eventId == "use") { if (item->isEquipment()) { if (item->isEquipped()) { - inventory->unequipItem(item); + player_node->unequipItem(item); } else { - inventory->equipItem(item); + player_node->equipItem(item); } } else { - inventory->useItem(item); + player_node->useItem(item); } } else if (eventId == "drop") diff --git a/src/gui/item_amount.cpp b/src/gui/item_amount.cpp index d92f53e1..2ac46283 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -28,10 +28,8 @@ #include "slider.h" #include "trade.h" -#include "../inventory.h" #include "../item.h" - -extern Inventory *inventory; +#include "../localplayer.h" ItemAmountWindow::ItemAmountWindow(int usage, Window *parent, Item *item): Window("Select amount of items to drop.", true, parent), @@ -114,7 +112,7 @@ void ItemAmountWindow::action(const std::string& eventId) } else if (eventId == "Drop") { - inventory->dropItem(mItem, mItemAmountTextBox->getInt()); + player_node->dropItem(mItem, mItemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "AddTrade") diff --git a/src/gui/linkhandler.h b/src/gui/linkhandler.h index 65c0d433..33416ec7 100644 --- a/src/gui/linkhandler.h +++ b/src/gui/linkhandler.h @@ -34,9 +34,6 @@ class LinkHandler virtual ~LinkHandler() { } virtual void handleLink(const std::string& link) = 0; - - protected: - LinkHandler() { } }; #endif diff --git a/src/gui/login.cpp b/src/gui/login.cpp index 9c198dbb..696e4c6f 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -25,27 +25,20 @@ #include <string> #include <sstream> -#include <SDL.h> #include <guichan/widgets/label.hpp> #include "../main.h" #include "../configuration.h" -#include "../graphics.h" #include "../log.h" -#include "../serverinfo.h" +#include "../logindata.h" #include "button.h" #include "checkbox.h" +#include "ok_dialog.h" #include "passwordfield.h" #include "textfield.h" -#include "../net/messagein.h" -#include "../net/messageout.h" -#include "../net/network.h" - -SERVER_INFO **server_info; - void WrongDataNoticeListener::setTarget(gcn::TextField *textField) { @@ -64,8 +57,8 @@ WrongDataNoticeListener::action(const std::string &eventId) } } -LoginDialog::LoginDialog(): - Window("Login"), mStatus(NET_IDLE) +LoginDialog::LoginDialog(LoginData *loginData): + Window("Login"), mLoginData(loginData) { userLabel = new gcn::Label("Name:"); passLabel = new gcn::Label("Password:"); @@ -144,10 +137,6 @@ LoginDialog::LoginDialog(): wrongDataNoticeListener = NULL; } -LoginDialog::~LoginDialog() -{ -} - void LoginDialog::action(const std::string& eventId) { @@ -179,14 +168,16 @@ LoginDialog::action(const std::string& eventId) } else { - const std::string host(config.getValue("host", "animesites.de")); - short port = (short)config.getValue("port", 0); - // Attempt to connect to login server - openConnection(host.c_str(), port); + mLoginData->hostname = config.getValue("host", "animesites.de"); + mLoginData->port = (short)config.getValue("port", 0); + mLoginData->username = userField->getText(); + mLoginData->password = passField->getText(); + okButton->setEnabled(false); //cancelButton->setEnabled(false); registerButton->setEnabled(false); - mStatus = NET_CONNECTING; + + state = ACCOUNT_STATE; } } else if (eventId == "cancel") @@ -198,147 +189,3 @@ LoginDialog::action(const std::string& eventId) state = REGISTER_STATE; } } - -void -LoginDialog::logic() -{ - switch (mStatus) - { - case NET_CONNECTING: - mStatus = pollConnection(); - break; - case NET_ERROR: - logger->log("Login::Unable to connect"); - errorMessage = "Unable to connect to login server"; - state = ERROR_STATE; - closeConnection(); - logger->log("Connection closed"); - break; - case NET_DATA: - if (packetReady()) - { - checkLogin(); - closeConnection(); - } - else - { - flush(); - } - break; - case NET_CONNECTED: - logger->log("Connected..."); - std::string user = userField->getText(); - const std::string password = passField->getText(); - attemptLogin(user, password); - mStatus = NET_DATA; - break; - } -} - -void -loginInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; - } -} - -void -LoginDialog::attemptLogin(const std::string& user, const std::string& pass) -{ - // Send login infos - MessageOut outMsg; - outMsg.writeInt16(0x0064); - outMsg.writeInt32(0); // client version - outMsg.writeString(user, 24); - outMsg.writeString(pass, 24); - outMsg.writeInt8(0); // unknown -} - -void -LoginDialog::checkLogin() -{ - // Receive reply - MessageIn msg = get_next_message(); - if (state == ERROR_STATE) - { - return; - } - - // Login ok - if (msg.getId() == 0x0069) - { - // Skip the length word - msg.skip(2); - - n_server = (msg.getLength() - 47) / 32; - server_info = (SERVER_INFO**)malloc(sizeof(SERVER_INFO*) * n_server); - - session_ID1 = msg.readInt32(); - account_ID = msg.readInt32(); - session_ID2 = msg.readInt32(); - msg.skip(30); // unknown - sex = msg.readInt8(); - - for (int i = 0; i < n_server; i++) - { - server_info[i] = new SERVER_INFO; - - server_info[i]->address = msg.readInt32(); - server_info[i]->port = msg.readInt16(); - server_info[i]->name = msg.readString(20); - server_info[i]->online_users = msg.readInt32(); - msg.skip(2); // unknown - - logger->log("Network: Server: %s (%s:%d)", - server_info[i]->name.c_str(), - iptostring(server_info[i]->address), - server_info[i]->port); - } - skip(msg.getLength()); - - state = CHAR_SERVER_STATE; - } - else if (msg.getId() == 0x006a) - { - int loginError = msg.readInt8(); - logger->log("Login::error code: %i", loginError); - - switch (loginError) { - case 0: - errorMessage = "Unregistered ID"; - break; - case 1: - errorMessage = "Wrong password"; - break; - case 2: - errorMessage = "Account expired"; - break; - case 3: - errorMessage = "Rejected from server"; - break; - case 4: - errorMessage = "You have been blocked by the GM Team"; - break; - case 6: - errorMessage = "You have been banned for 5 minutes"; - break; - case 9: - errorMessage = "This account is already logged in"; - break; - default: - errorMessage = "Unknown error"; - break; - } - skip(msg.getLength()); - state = ERROR_STATE; - } - else { - skip(msg.getLength()); - logger->log("Login::Unknown error"); - errorMessage = "Unknown error"; - state = ERROR_STATE; - } - // Todo: add other packets, also encrypted -} diff --git a/src/gui/login.h b/src/gui/login.h index cc6ace3d..669752c5 100644 --- a/src/gui/login.h +++ b/src/gui/login.h @@ -26,13 +26,11 @@ #include <iosfwd> #include <guichan/actionlistener.hpp> -#include <SDL_events.h> -#include "ok_dialog.h" #include "window.h" #include "../guichanfwd.h" -class LoginDialog; +class LoginData; /** * Listener used for handling wrong data. @@ -57,23 +55,13 @@ class LoginDialog : public Window, public gcn::ActionListener { * * @see Window::Window */ - LoginDialog(); - - /** - * Destructor - */ - ~LoginDialog(); + LoginDialog(LoginData *loginData); /** * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - /** - * Updates dialog logic. - */ - void logic(); - // Made them public to have the possibility to request focus // from external functions. gcn::TextField *userField; @@ -88,17 +76,10 @@ class LoginDialog : public Window, public gcn::ActionListener { gcn::Button *okButton; gcn::Button *cancelButton; gcn::Button *registerButton; - int mStatus; - - void attemptLogin(const std::string& user, const std::string& pass); - void checkLogin(); WrongDataNoticeListener *wrongDataNoticeListener; -}; -/** - * Handle input - */ -void loginInputHandler(SDL_KeyboardEvent *keyEvent); + LoginData *mLoginData; +}; #endif diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index b23d7b1c..1165d7bb 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -24,6 +24,7 @@ #include "minimap.h" #include "../being.h" +#include "../beingmanager.h" #include "../graphics.h" #include "../map.h" @@ -86,38 +87,36 @@ void Minimap::draw(gcn::Graphics *graphics) mMapImage, getPadding(), getTitleBarHeight()); } - std::list<Being*>::iterator bi; + Beings *beings = beingManager->getAll(); + Beings::iterator bi; - for (bi = beings.begin(); bi != beings.end(); bi++) + for (bi = beings->begin(); bi != beings->end(); bi++) { Being *being = (*bi); + int dotSize = 1; - if (being == player_node) - { - // Player dot - graphics->setColor(gcn::Color(209, 52, 61)); - graphics->fillRectangle(gcn::Rectangle( - being->x / 2 + getPadding() - 1, - being->y / 2 + getTitleBarHeight() - 1, 3, 3)); - } - else - { - switch (being->getType()) { - case Being::PLAYER: - graphics->setColor(gcn::Color(61, 52, 209)); - break; - - case Being::MONSTER: - graphics->setColor(gcn::Color(209, 52, 61)); - break; - - default: - break; - } - - graphics->fillRectangle(gcn::Rectangle( - being->x / 2 + getPadding(), - being->y / 2 + getTitleBarHeight(), 1, 1)); + switch (being->getType()) { + case Being::LOCALPLAYER: + dotSize = 3; + graphics->setColor(gcn::Color(209, 52, 61)); + break; + + case Being::PLAYER: + graphics->setColor(gcn::Color(61, 52, 209)); + break; + + case Being::MONSTER: + graphics->setColor(gcn::Color(209, 52, 61)); + break; + + default: + break; } + + int offset = (dotSize - 1) / 2; + + graphics->fillRectangle(gcn::Rectangle( + being->x / 2 + getPadding() - offset, + being->y / 2 + getTitleBarHeight() - offset, 1, 1)); } } diff --git a/src/gui/ministatus.cpp b/src/gui/ministatus.cpp index 6820d561..515a4014 100644 --- a/src/gui/ministatus.cpp +++ b/src/gui/ministatus.cpp @@ -30,7 +30,7 @@ #include <guichan/imagefont.hpp> #include "progressbar.h" -#include "../playerinfo.h" +#include "../localplayer.h" MiniStatusWindow::MiniStatusWindow(): Window("") @@ -67,13 +67,13 @@ MiniStatusWindow::MiniStatusWindow(): void MiniStatusWindow::update() { // HP Bar coloration - if (player_info->hp < int(player_info->maxHp / 3)) + if (player_node->hp < int(player_node->maxHp / 3)) { hpBar->setColor(223, 32, 32); // Red } else { - if (player_info->hp < int((player_info->maxHp / 3) * 2)) + if (player_node->hp < int((player_node->maxHp / 3) * 2)) { hpBar->setColor(230, 171, 34); // Orange } @@ -83,16 +83,16 @@ void MiniStatusWindow::update() } } - hpBar->setProgress((float)player_info->hp / (float)player_info->maxHp); - // mpBar->setProgress((float)player_info->mp / (float)player_info->maxMp); + hpBar->setProgress((float)player_node->hp / (float)player_node->maxHp); + // mpBar->setProgress((float)player_node->mp / (float)player_node->maxMp); // Update and center labels std::stringstream updatedText; - updatedText << player_info->hp; + updatedText << player_node->hp; hpLabel->setCaption(updatedText.str()); hpLabel->adjustSize(); updatedText.str(""); - updatedText << player_info->mp; + updatedText << player_node->mp; mpLabel->setCaption(updatedText.str()); mpLabel->adjustSize(); hpLabel->setPosition(hpBar->getX() + int((hpBar->getWidth() / 2) - (hpLabel->getWidth() / 2)), diff --git a/src/gui/ministatus.h b/src/gui/ministatus.h index f10717bc..0b081f47 100644 --- a/src/gui/ministatus.h +++ b/src/gui/ministatus.h @@ -26,21 +26,19 @@ #include <iosfwd> -#include <guichan/actionlistener.hpp> - #include "window.h" #include "../guichanfwd.h" class ProgressBar; - /** * The player mini-status dialog. * * \ingroup Interface */ -class MiniStatusWindow : public Window, public gcn::ActionListener { +class MiniStatusWindow : public Window +{ public: /** * Constructor. @@ -52,14 +50,9 @@ class MiniStatusWindow : public Window, public gcn::ActionListener { */ void draw(gcn::Graphics *graphics); - /** - * Does Nothing - **/ - void action(const std::string&) {}; - private: /** - * Updates this dialog with values from PLAYER_INFO *char_info + * Updates this dialog with values from player_node */ void update(); diff --git a/src/gui/npc_text.cpp b/src/gui/npc_text.cpp index f4bdfb0f..0769021e 100644 --- a/src/gui/npc_text.cpp +++ b/src/gui/npc_text.cpp @@ -23,22 +23,21 @@ #include "npc_text.h" +#include <string> + #include "scrollarea.h" #include "button.h" #include "textbox.h" -#include "../game.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../npc.h" NpcTextDialog::NpcTextDialog(): Window("NPC") { - textBox = new TextBox(); - textBox->setEditable(false); - scrollArea = new ScrollArea(textBox); - okButton = new Button("OK"); + mTextBox = new TextBox(); + mTextBox->setEditable(false); + gcn::ScrollArea *scrollArea = new ScrollArea(mTextBox); + Button *okButton = new Button("OK"); setContentSize(260, 175); scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -61,13 +60,13 @@ NpcTextDialog::NpcTextDialog(): void NpcTextDialog::setText(const char *text) { - textBox->setText(text); + mTextBox->setText(text); } void NpcTextDialog::addText(const std::string &text) { - textBox->setText(textBox->getText() + text + "\n"); + mTextBox->setText(mTextBox->getText() + text + "\n"); } void @@ -75,11 +74,9 @@ NpcTextDialog::action(const std::string& eventId) { if (eventId == "ok") { - MessageOut outMsg; - outMsg.writeInt16(CMSG_NPC_NEXT_REQUEST); - outMsg.writeInt32(current_npc); setText(""); setVisible(false); + current_npc->nextDialog(); current_npc = 0; } } diff --git a/src/gui/npc_text.h b/src/gui/npc_text.h index 4a0e8b49..a1d75aab 100644 --- a/src/gui/npc_text.h +++ b/src/gui/npc_text.h @@ -25,7 +25,6 @@ #define _TMW_NPC_TEXT_H #include <iosfwd> -#include <string> #include <guichan/actionlistener.hpp> #include "window.h" @@ -70,9 +69,7 @@ class NpcTextDialog : public Window, public gcn::ActionListener addText(const std::string &string); private: - gcn::Button *okButton; - gcn::TextBox *textBox; - gcn::ScrollArea *scrollArea; + gcn::TextBox *mTextBox; }; #endif diff --git a/src/gui/npc.cpp b/src/gui/npclistdialog.cpp index de491b73..9b5239f5 100644 --- a/src/gui/npc.cpp +++ b/src/gui/npclistdialog.cpp @@ -21,7 +21,7 @@ * $Id$ */ -#include "npc.h" +#include "npclistdialog.h" #include <sstream> @@ -29,10 +29,7 @@ #include "scrollarea.h" #include "listbox.h" -#include "../game.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../npc.h" NpcListDialog::NpcListDialog(): Window("NPC") @@ -118,12 +115,9 @@ NpcListDialog::action(const std::string& eventId) if (choice) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_NPC_LIST_CHOICE); - outMsg.writeInt32(current_npc); - outMsg.writeInt8(choice); setVisible(false); reset(); + current_npc->dialogChoice(choice); current_npc = 0; } } diff --git a/src/gui/npc.h b/src/gui/npclistdialog.h index 6ab49138..8b98076e 100644 --- a/src/gui/npc.h +++ b/src/gui/npclistdialog.h @@ -21,8 +21,8 @@ * $Id$ */ -#ifndef _TMW_NPC_H -#define _TMW_NPC_H +#ifndef _TMW_GUI_NPCLISTDIALOG_H +#define _TMW_GUI_NPCLISTDIALOG_H #include <iosfwd> #include <vector> diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp index 8d0ace57..464242f1 100644 --- a/src/gui/popupmenu.cpp +++ b/src/gui/popupmenu.cpp @@ -35,19 +35,14 @@ #include "../being.h" #include "../floor_item.h" -#include "../game.h" -#include "../graphics.h" -#include "../inventory.h" #include "../item.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../localplayer.h" +#include "../npc.h" #include "../resources/iteminfo.h" #include "../resources/itemmanager.h" -extern Being* autoTarget; -extern Inventory* inventory; +extern std::string tradePartnerName; PopupMenu::PopupMenu(): Window(), @@ -129,11 +124,7 @@ void PopupMenu::handleLink(const std::string& link) mBeing->getType() == Being::NPC && current_npc == 0) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_NPC_TALK); - outMsg.writeInt32(mBeing->getId()); - outMsg.writeInt8(0); - current_npc = mBeing->getId(); + dynamic_cast<NPC*>(mBeing)->talk(); } // Trade action @@ -141,11 +132,7 @@ void PopupMenu::handleLink(const std::string& link) mBeing != NULL && mBeing->getType() == Being::PLAYER) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_TRADE_REQUEST); - outMsg.writeInt32(mBeing->getId()); - //tradePartner.flush(); - //tradePartner << "Trade: You and " << being->name<< ""; + player_node->trade(mBeing); tradePartnerName = mBeing->getName(); } @@ -154,8 +141,7 @@ void PopupMenu::handleLink(const std::string& link) mBeing != NULL && mBeing->getType() == Being::PLAYER) { - autoTarget = mBeing; - attack(mBeing); + player_node->attack(mBeing, true); } /* @@ -177,7 +163,7 @@ void PopupMenu::handleLink(const std::string& link) // Pick Up Floor Item action else if ((link == "pickup") && mFloorItem != NULL) { - pickUp(mFloorItem->getId()); + player_node->pickUp(mFloorItem); } // Look To action @@ -192,16 +178,16 @@ void PopupMenu::handleLink(const std::string& link) { if (mItem->isEquipped()) { - inventory->unequipItem(mItem); + player_node->unequipItem(mItem); } else { - inventory->equipItem(mItem); + player_node->equipItem(mItem); } } else { - inventory->useItem(mItem); + player_node->useItem(mItem); } } diff --git a/src/gui/register.cpp b/src/gui/register.cpp index 71d1a740..9b4cf79d 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -25,32 +25,24 @@ #include <string> #include <sstream> -#include <SDL.h> #include <guichan/widgets/label.hpp> #include "../main.h" #include "../configuration.h" -#include "../graphics.h" #include "../log.h" -#include "../serverinfo.h" +#include "../logindata.h" #include "button.h" #include "checkbox.h" +#include "login.h" #include "passwordfield.h" #include "radiobutton.h" #include "textfield.h" #include "ok_dialog.h" -#include "../net/messagein.h" -#include "../net/messageout.h" -#include "../net/network.h" - -//OkDialog *wrongLoginNotice = NULL; -extern SERVER_INFO **server_info; - -RegisterDialog::RegisterDialog(): - Window("Register"), mStatus(NET_IDLE) +RegisterDialog::RegisterDialog(LoginData *loginData): + Window("Register"), mLoginData(loginData) { userLabel = new gcn::Label("Name:"); passwordLabel = new gcn::Label("Password:"); @@ -214,6 +206,7 @@ RegisterDialog::action(const std::string& eventId) if (wrongRegisterNotice) { delete wrongRegisterNotice; + wrongRegisterNotice = NULL; } wrongRegisterNotice = new OkDialog("Error", errorMsg.str(), wrongDataNoticeListener); @@ -221,159 +214,15 @@ RegisterDialog::action(const std::string& eventId) else { // No errors detected, register the new user. - const std::string host(config.getValue("host", "animesites.de")); - short port = (short)config.getValue("port", 0); - // Attempt to connect to login server - openConnection(host.c_str(), port); registerButton->setEnabled(false); - mStatus = NET_CONNECTING; - } - } -} - -void -RegisterDialog::logic() -{ - switch (mStatus) - { - case NET_CONNECTING: - mStatus = pollConnection(); - break; - case NET_ERROR: - logger->log("Register::Unable to connect"); - errorMessage = "Unable to connect to login server"; - state = ERROR_STATE; - closeConnection(); - logger->log("Connection closed"); - break; - case NET_DATA: - if (packetReady()) - { - checkRegistration(); - closeConnection(); - } - else - { - flush(); - } - break; - case NET_CONNECTED: - logger->log("Connected..."); - std::string user = userField->getText(); - const std::string password = passwordField->getText(); - if (femaleButton->isMarked()) - { - user += "_F"; - } - else - { - user += "_M"; - } - attemptRegistration(user, password); - mStatus = NET_DATA; - break; - } -} -void -RegisterDialog::attemptRegistration(const std::string& user, - const std::string& pass) -{ - // Send login infos - MessageOut outMsg; - outMsg.writeInt16(0x0064); - outMsg.writeInt32(0); // client version - outMsg.writeString(user, 24); - outMsg.writeString(pass, 24); - outMsg.writeInt8(0); // unknown -} + mLoginData->hostname = config.getValue("host", "animesites.de"); + mLoginData->port = (short)config.getValue("port", 0); + mLoginData->username = userField->getText(); + mLoginData->password = passwordField->getText(); + mLoginData->username += femaleButton->isMarked() ? "_F" : "_M"; -void -RegisterDialog::checkRegistration() -{ - // Receive reply - MessageIn msg = get_next_message(); - if (state == ERROR_STATE) - { - return; - } - - // Login ok - if (msg.getId() == 0x0069) - { - // Skip the length word - msg.skip(2); - - n_server = (msg.getLength() - 47) / 32; - server_info = (SERVER_INFO**)malloc(sizeof(SERVER_INFO*) * n_server); - - session_ID1 = msg.readInt32(); - account_ID = msg.readInt32(); - session_ID2 = msg.readInt32(); - msg.skip(30); // unknown - sex = msg.readInt8(); - - for (int i = 0; i < n_server; i++) - { - server_info[i] = new SERVER_INFO; - - server_info[i]->address = msg.readInt32(); - server_info[i]->port = msg.readInt16(); - server_info[i]->name = msg.readString(20); - server_info[i]->online_users = msg.readInt32(); - msg.skip(2); // unknown - - logger->log("Network: Server: %s (%s:%d)", - server_info[i]->name.c_str(), - iptostring(server_info[i]->address), - server_info[i]->port); + state = ACCOUNT_STATE; } - skip(msg.getLength()); - - state = CHAR_SERVER_STATE; - } - else if (msg.getId() == 0x006a) - { - int loginError = msg.readInt8(); - logger->log("Login::error code: %i", loginError); - - switch (loginError) { - case 0: - errorMessage = "Unregistered ID"; - break; - case 1: - errorMessage = "Wrong password"; - break; - case 2: - errorMessage = "Account expired"; - break; - case 3: - errorMessage = "Rejected from server"; - break; - case 4: - errorMessage = "You have been blocked by the GM Team"; - break; - case 9: - errorMessage = "This account is already logged in"; - break; - } - skip(msg.getLength()); - state = ERROR_STATE; - } - else { - skip(msg.getLength()); - logger->log("Login::Unknown error"); - errorMessage = "Unknown error"; - state = ERROR_STATE; - } - // Todo: add other packets, also encrypted -} - -void -registerInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; } } diff --git a/src/gui/register.h b/src/gui/register.h index 25a867bd..34892ec0 100644 --- a/src/gui/register.h +++ b/src/gui/register.h @@ -26,12 +26,14 @@ #include <iosfwd> #include <guichan/actionlistener.hpp> -#include <SDL_events.h> #include "window.h" -#include "login.h" #include "../guichanfwd.h" +class LoginData; +class OkDialog; +class WrongDataNoticeListener; + /** * The login dialog. * @@ -44,18 +46,13 @@ class RegisterDialog : public Window, public gcn::ActionListener { * * @see Window::Window */ - RegisterDialog(); + RegisterDialog(LoginData *loginData); /** * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - /** - * Updates dialog logic. - */ - void logic(); - // Made them public to have the possibility to request focus // from external functions. gcn::TextField *userField; @@ -72,19 +69,11 @@ class RegisterDialog : public Window, public gcn::ActionListener { gcn::Button *cancelButton; gcn::RadioButton *maleButton; gcn::RadioButton *femaleButton; - int mStatus; - void attemptRegistration(const std::string& user, - const std::string& pass); - void checkRegistration(); - WrongDataNoticeListener *wrongDataNoticeListener; OkDialog *wrongRegisterNotice; -}; -/** - * Handle input - */ -void registerInputHandler(SDL_KeyboardEvent *keyEvent); + LoginData *mLoginData; +}; #endif diff --git a/src/gui/requesttrade.cpp b/src/gui/requesttrade.cpp index 075567af..54f7c208 100644 --- a/src/gui/requesttrade.cpp +++ b/src/gui/requesttrade.cpp @@ -23,53 +23,46 @@ #include "requesttrade.h" -#include <sstream> #include <guichan/widgets/label.hpp> #include "button.h" -#include "../net/messageout.h" -#include "../net/protocol.h" - -bool requestTradeDialogOpen = false; +#include "../localplayer.h" RequestTradeDialog::RequestTradeDialog(const std::string &name): Window("Request for Trade", true) { + gcn::Label *nameLabel[2]; nameLabel[0] = new gcn::Label(""); nameLabel[1] = new gcn::Label(""); - acceptButton = new Button("Accept"); - cancelButton = new Button("Cancel"); + mAcceptButton = new Button("Accept"); + mCancelButton = new Button("Cancel"); setContentSize(260, 75); nameLabel[0]->setPosition(5, 30); nameLabel[1]->setPosition(5, 40); - cancelButton->setPosition( - 260 - 5 - cancelButton->getWidth(), - 75 - 5 - cancelButton->getHeight()); - acceptButton->setPosition( - cancelButton->getX() - 5 - acceptButton->getWidth(), - cancelButton->getY()); + mCancelButton->setPosition( + 260 - 5 - mCancelButton->getWidth(), + 75 - 5 - mCancelButton->getHeight()); + mAcceptButton->setPosition( + mCancelButton->getX() - 5 - mAcceptButton->getWidth(), + mCancelButton->getY()); - acceptButton->setEventId("accept"); - cancelButton->setEventId("cancel"); + mAcceptButton->setEventId("accept"); + mCancelButton->setEventId("cancel"); - acceptButton->addActionListener(this); - cancelButton->addActionListener(this); + mAcceptButton->addActionListener(this); + mCancelButton->addActionListener(this); add(nameLabel[0]); add(nameLabel[1]); - add(acceptButton); - add(cancelButton); - - std::stringstream cap[2]; - cap[0] << name << " wants to trade with you."; - cap[1] << "Do you want to accept?"; + add(mAcceptButton); + add(mCancelButton); - nameLabel[0]->setCaption(cap[0].str()); + nameLabel[0]->setCaption(name + " wants to trade with you."); nameLabel[0]->adjustSize(); - nameLabel[1]->setCaption(cap[1].str()); + nameLabel[1]->setCaption("Do you want to accept?"); nameLabel[1]->adjustSize(); setLocationRelativeTo(getParent()); @@ -77,17 +70,12 @@ RequestTradeDialog::RequestTradeDialog(const std::string &name): void RequestTradeDialog::action(const std::string& eventId) { - int choice = 4; // 4 means trade canceled + bool accept = false; if (eventId == "accept") { - choice = 3; // ok to trade - } - else if (eventId == "cancel") { - requestTradeDialogOpen = false; + accept = true; } - MessageOut outMsg; - outMsg.writeInt16(CMSG_TRADE_RESPONSE); - outMsg.writeInt8(choice); + player_node->tradeReply(accept); scheduleDelete(); } diff --git a/src/gui/requesttrade.h b/src/gui/requesttrade.h index 706b0b0e..d9ea67ed 100644 --- a/src/gui/requesttrade.h +++ b/src/gui/requesttrade.h @@ -30,8 +30,6 @@ #include "window.h" #include "../guichanfwd.h" -extern bool requestTradeDialogOpen; - /** * The request trade dialog. * @@ -53,9 +51,8 @@ class RequestTradeDialog : public Window, public gcn::ActionListener void action(const std::string& eventId); private: - gcn::Button *acceptButton; - gcn::Button *cancelButton; - gcn::Label *nameLabel[2]; + gcn::Button *mAcceptButton; + gcn::Button *mCancelButton; }; #endif diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index ea5b948f..f89055b4 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -34,8 +34,8 @@ #include "shop.h" #include "slider.h" -#include "../game.h" #include "../item.h" +#include "../npc.h" #include "../resources/iteminfo.h" #include "../resources/itemmanager.h" @@ -44,8 +44,9 @@ #include "../net/protocol.h" -SellDialog::SellDialog(): +SellDialog::SellDialog(Network *network): Window("Sell"), + mNetwork(network), m_maxItems(0), m_amountItems(0) { itemList = new ListBox(this); @@ -216,7 +217,7 @@ void SellDialog::action(const std::string& eventId) // Attempt sell assert(m_amountItems > 0 && m_amountItems <= m_maxItems); - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_NPC_SELL_REQUEST); outMsg.writeInt16(8); outMsg.writeInt16(shopInventory[selectedItem].index); diff --git a/src/gui/sell.h b/src/gui/sell.h index 142819b5..1a5fabf5 100644 --- a/src/gui/sell.h +++ b/src/gui/sell.h @@ -34,6 +34,7 @@ #include "../guichanfwd.h" class Item; +class Network; struct ITEM_SHOP; @@ -51,7 +52,7 @@ class SellDialog : public Window, public gcn::ActionListener, * * @see Window::Window */ - SellDialog(); + SellDialog(Network *network); /** * Resets the dialog, clearing inventory. @@ -84,6 +85,7 @@ class SellDialog : public Window, public gcn::ActionListener, std::string getElementAt(int i); private: + Network *mNetwork; gcn::Button *sellButton; gcn::Button *quitButton; gcn::Button *increaseButton; diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp index 2114e635..34f5b360 100644 --- a/src/gui/setup.cpp +++ b/src/gui/setup.cpp @@ -23,18 +23,24 @@ #include "setup.h" -#include <iostream> #include <sstream> #include <guichan/widgets/container.hpp> #include <guichan/widgets/label.hpp> #include "button.h" +#include "chat.h" #include "checkbox.h" +#include "equipmentwindow.h" +#include "help.h" +#include "inventorywindow.h" #include "listbox.h" #include "ok_dialog.h" +#include "minimap.h" #include "scrollarea.h" +#include "skill.h" #include "slider.h" +#include "status.h" #include "tabbedcontainer.h" #include "../configuration.h" @@ -44,13 +50,8 @@ #include "../sound.h" extern Graphics *graphics; -#include "chat.h" -#include "equipmentwindow.h" -#include "help.h" -#include "inventorywindow.h" -#include "minimap.h" -#include "skill.h" -#include "status.h" + +extern SDL_Joystick *joypad; extern SDL_Joystick *joypad; diff --git a/src/gui/skill.cpp b/src/gui/skill.cpp index 8a073bdd..ba117b67 100644 --- a/src/gui/skill.cpp +++ b/src/gui/skill.cpp @@ -29,10 +29,7 @@ #include "listbox.h" #include "scrollarea.h" -#include "../playerinfo.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../localplayer.h" #include "../graphics.h" extern Graphics *graphics; @@ -64,8 +61,7 @@ const char *skill_db[] = { SkillDialog::SkillDialog(): - Window("Skills"), - skillPoints(0) + Window("Skills") { setWindowName("Skills"); setDefaultSize(graphics->getWidth() - 255, 25, 240, 240); @@ -119,18 +115,16 @@ void SkillDialog::action(const std::string& eventId) { // Increment skill int selectedSkill = skillListBox->getSelected(); - if (player_info->skillPoint > 0 && selectedSkill >= 0) + if (selectedSkill >= 0) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_SKILL_LEVELUP_REQUEST); - outMsg.writeInt16(skillList[selectedSkill]->id); + player_node->raiseSkill(skillList[selectedSkill]->id); } } else if (eventId == "skill") { incButton->setEnabled( skillListBox->getSelected() > -1 && - skillPoints > 0); + player_node->skillPoint > 0); } else if (eventId == "close") { @@ -138,17 +132,15 @@ void SkillDialog::action(const std::string& eventId) } } -void SkillDialog::setPoints(int i) +void SkillDialog::update() { - skillPoints = i; - if (pointsLabel != NULL) { char tmp[128]; - sprintf(tmp, "Skill points: %i", skillPoints); + sprintf(tmp, "Skill points: %i", player_node->skillPoint); pointsLabel->setCaption(tmp); } - incButton->setEnabled(skillListBox->getSelected() > -1 && skillPoints > 0); + incButton->setEnabled(skillListBox->getSelected() > -1 && player_node->skillPoint > 0); } int SkillDialog::getNumberOfElements() diff --git a/src/gui/skill.h b/src/gui/skill.h index b4c2522f..98a21438 100644 --- a/src/gui/skill.h +++ b/src/gui/skill.h @@ -46,17 +46,6 @@ struct SKILL { class SkillDialog : public Window, public gcn::ActionListener, public gcn::ListModel { - private: - gcn::ListBox *skillListBox; - gcn::ScrollArea *skillScrollArea; - gcn::Label *pointsLabel; - gcn::Button *incButton; - gcn::Button *useButton; - gcn::Button *closeButton; - - std::vector<SKILL*> skillList; - int skillPoints; - public: /** * Constructor. @@ -70,7 +59,7 @@ class SkillDialog : public Window, public gcn::ActionListener, void action(const std::string&); - void setPoints(int i); + void update(); int getNumberOfElements(); std::string getElementAt(int); @@ -79,6 +68,16 @@ class SkillDialog : public Window, public gcn::ActionListener, void addSkill(int id, int lv, int sp); void setSkill(int id, int lv, int sp); void cleanList(); + + private: + gcn::ListBox *skillListBox; + gcn::ScrollArea *skillScrollArea; + gcn::Label *pointsLabel; + gcn::Button *incButton; + gcn::Button *useButton; + gcn::Button *closeButton; + + std::vector<SKILL*> skillList; }; extern SkillDialog *skillDialog; diff --git a/src/gui/status.cpp b/src/gui/status.cpp index 43456a29..c6416409 100644 --- a/src/gui/status.cpp +++ b/src/gui/status.cpp @@ -29,17 +29,15 @@ #include "button.h" #include "progressbar.h" -#include "../playerinfo.h" - -#include "../net/messageout.h" -#include "../net/protocol.h" +#include "../localplayer.h" #include "../graphics.h" extern Graphics *graphics; -StatusWindow::StatusWindow(): - Window(player_info->name) +StatusWindow::StatusWindow(LocalPlayer *player): + Window(player->getName()), + mPlayer(player) { setWindowName("Status"); setResizable(true); @@ -222,46 +220,46 @@ void StatusWindow::update() // Status Part // ----------- updateText.str(""); - updateText << "Level: " << player_info->lvl; + updateText << "Level: " << mPlayer->lvl; lvlLabel->setCaption(updateText.str()); lvlLabel->adjustSize(); updateText.str(""); - updateText << "Money: " << player_info->gp << " GP"; + updateText << "Money: " << mPlayer->gp << " GP"; gpLabel->setCaption(updateText.str()); gpLabel->adjustSize(); updateText.str(""); - updateText << "Job: " << player_info->jobLvl; + updateText << "Job: " << mPlayer->jobLvl; jobXpLabel->setCaption(updateText.str()); jobXpLabel->adjustSize(); updateText.str(""); - updateText << player_info->hp << "/" << player_info->maxHp; + updateText << mPlayer->hp << "/" << mPlayer->maxHp; hpValueLabel->setCaption(updateText.str()); hpValueLabel->adjustSize(); updateText.str(""); - updateText << player_info->mp << "/" << player_info->maxMp; + updateText << mPlayer->mp << "/" << mPlayer->maxMp; mpValueLabel->setCaption(updateText.str()); mpValueLabel->adjustSize(); updateText.str(""); - updateText << (int)player_info->xp << "/" << (int)player_info->xpForNextLevel; + updateText << (int)mPlayer->xp << "/" << (int)mPlayer->xpForNextLevel; xpValueLabel->setCaption(updateText.str()); xpValueLabel->adjustSize(); updateText.str(""); - updateText << (int)player_info->jobXp << "/" << (int)player_info->jobXpForNextLevel; + updateText << (int)mPlayer->jobXp << "/" << (int)mPlayer->jobXpForNextLevel; jobValueLabel->setCaption(updateText.str()); jobValueLabel->adjustSize(); // HP Bar coloration - if (player_info->hp < int(player_info->maxHp / 3)) + if (mPlayer->hp < int(mPlayer->maxHp / 3)) { hpBar->setColor(223, 32, 32); // Red } - else if (player_info->hp < int((player_info->maxHp / 3) * 2)) + else if (mPlayer->hp < int((mPlayer->maxHp / 3) * 2)) { hpBar->setColor(230, 171, 34); // Orange } @@ -270,61 +268,48 @@ void StatusWindow::update() hpBar->setColor(0, 171, 34); // Green } - hpBar->setProgress((float)player_info->hp / (float)player_info->maxHp); - // mpBar->setProgress((float)player_info->mp / (float)player_info->maxMp); + hpBar->setProgress((float)mPlayer->hp / (float)mPlayer->maxHp); + // mpBar->setProgress((float)mPlayer->mp / (float)mPlayer->maxMp); xpBar->setProgress( - (float)player_info->xp / (float)player_info->xpForNextLevel); + (float)mPlayer->xp / (float)mPlayer->xpForNextLevel); jobXpBar->setProgress( - (float)player_info->jobXp / (float)player_info->jobXpForNextLevel); + (float)mPlayer->jobXp / (float)mPlayer->jobXpForNextLevel); // Stats Part // ---------- - std::stringstream statsStr[6]; - std::stringstream figureStr[6]; - std::stringstream pointsStr[6]; - - statsStr[0] << "Strength:"; - figureStr[0] << (int)player_info->STR; - statsStr[1] << "Agility:"; - figureStr[1] << (int)player_info->AGI; - statsStr[2] << "Vitality:"; - figureStr[2] << (int)player_info->VIT; - statsStr[3] << "Intelligence:"; - figureStr[3] << (int)player_info->INT; - statsStr[4] << "Dexterity:"; - figureStr[4] << (int)player_info->DEX; - statsStr[5] << "Luck:"; - figureStr[5] << (int)player_info->LUK; - - int statusPoints = player_info->statsPointsToAttribute; + static const std::string attrNames[6] = { + "Strength", + "Agility", + "Vitality", + "Intelligence", + "Dexterity", + "Luck" + }; + + int statusPoints = mPlayer->statsPointsToAttribute; updateText.str(""); updateText << "Remaining Status Points: " << statusPoints; - pointsStr[0] << (int)player_info->STRUp; - pointsStr[1] << (int)player_info->AGIUp; - pointsStr[2] << (int)player_info->VITUp; - pointsStr[3] << (int)player_info->INTUp; - pointsStr[4] << (int)player_info->DEXUp; - pointsStr[5] << (int)player_info->LUKUp; - - // Enable buttons for which there are enough status points - statsButton[0]->setEnabled(player_info->STRUp <= statusPoints); - statsButton[1]->setEnabled(player_info->AGIUp <= statusPoints); - statsButton[2]->setEnabled(player_info->VITUp <= statusPoints); - statsButton[3]->setEnabled(player_info->INTUp <= statusPoints); - statsButton[4]->setEnabled(player_info->DEXUp <= statusPoints); - statsButton[5]->setEnabled(player_info->LUKUp <= statusPoints); - // Update labels for (int i = 0; i < 6; i++) { - statsLabel[i]->setCaption(statsStr[i].str()); + std::stringstream sstr; + + statsLabel[i]->setCaption(attrNames[i]); statsLabel[i]->adjustSize(); - statsDisplayLabel[i]->setCaption(figureStr[i].str()); + + sstr.str(""); + sstr << (int)mPlayer->ATTR[i]; + statsDisplayLabel[i]->setCaption(sstr.str()); statsDisplayLabel[i]->adjustSize(); - pointsLabel[i]->setCaption(pointsStr[i].str()); + + sstr.str(""); + sstr << (int)mPlayer->ATTR_UP[i]; + pointsLabel[i]->setCaption(sstr.str()); pointsLabel[i]->adjustSize(); + + statsButton[i]->setEnabled(mPlayer->ATTR_UP[i] <= statusPoints); } remainingStatsPointsLabel->setCaption(updateText.str()); remainingStatsPointsLabel->adjustSize(); @@ -333,43 +318,43 @@ void StatusWindow::update() // Attack TODO: Count equipped Weapons and items attack bonuses updateText.str(""); - updateText << int(player_info->ATK + player_info->ATKBonus); + updateText << int(mPlayer->ATK + mPlayer->ATK_BONUS); statsAttackPoints->setCaption(updateText.str()); statsAttackPoints->adjustSize(); // Defense TODO: Count equipped Armors and items defense bonuses updateText.str(""); - updateText << int(player_info->DEF + player_info->DEFBonus); + updateText << int(mPlayer->DEF + mPlayer->DEF_BONUS); statsDefensePoints->setCaption(updateText.str()); statsDefensePoints->adjustSize(); // Magic Attack TODO: Count equipped items M.Attack bonuses updateText.str(""); - updateText << int(player_info->MATK + player_info->MATKBonus); + updateText << int(mPlayer->MATK + mPlayer->MATK_BONUS); statsMagicAttackPoints->setCaption(updateText.str()); statsMagicAttackPoints->adjustSize(); // Magic Defense TODO: Count equipped items M.Defense bonuses updateText.str(""); - updateText << int(player_info->MDEF + player_info->MDEFBonus); + updateText << int(mPlayer->MDEF + mPlayer->MDEF_BONUS); statsMagicDefensePoints->setCaption(updateText.str()); statsMagicDefensePoints->adjustSize(); // Accuracy % updateText.str(""); - updateText << (int)player_info->HIT; + updateText << (int)mPlayer->HIT; statsAccuracyPoints->setCaption(updateText.str()); statsAccuracyPoints->adjustSize(); // Evasion % updateText.str(""); - updateText << (int)player_info->FLEE; + updateText << (int)mPlayer->FLEE; statsEvadePoints->setCaption(updateText.str()); statsEvadePoints->adjustSize(); // Reflex % updateText.str(""); - updateText << ((int)player_info->DEX / 4); // + counter + updateText << ((int)mPlayer->DEX / 4); // + counter statsReflexPoints->setCaption(updateText.str()); statsReflexPoints->adjustSize(); @@ -402,33 +387,29 @@ void StatusWindow::action(const std::string& eventId) // Stats Part if (eventId.length() == 3) { - MessageOut outMsg; - outMsg.writeInt16(CMSG_STAT_UPDATE_REQUEST); - if (eventId == "STR") { - outMsg.writeInt16(0x000d); + player_node->raiseAttribute(LocalPlayer::STR); } if (eventId == "AGI") { - outMsg.writeInt16(0x000e); + player_node->raiseAttribute(LocalPlayer::AGI); } if (eventId == "VIT") { - outMsg.writeInt16(0x000f); + player_node->raiseAttribute(LocalPlayer::VIT); } if (eventId == "INT") { - outMsg.writeInt16(0x0010); + player_node->raiseAttribute(LocalPlayer::INT); } if (eventId == "DEX") { - outMsg.writeInt16(0x0011); + player_node->raiseAttribute(LocalPlayer::DEX); } if (eventId == "LUK") { - outMsg.writeInt16(0x0012); + player_node->raiseAttribute(LocalPlayer::LUK); } - outMsg.writeInt8(1); } } diff --git a/src/gui/status.h b/src/gui/status.h index 1df4e346..c01b5676 100644 --- a/src/gui/status.h +++ b/src/gui/status.h @@ -32,6 +32,7 @@ #include "../guichanfwd.h" +class LocalPlayer; class ProgressBar; @@ -45,7 +46,7 @@ class StatusWindow : public Window, public gcn::ActionListener { /** * Constructor. */ - StatusWindow(); + StatusWindow(LocalPlayer *player); /** * Called when receiving actions from widget. @@ -63,6 +64,7 @@ class StatusWindow : public Window, public gcn::ActionListener { void update(); private: + LocalPlayer *mPlayer; /** * Status Part diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 767888f4..f7286fd2 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -43,8 +43,9 @@ #include "../resources/iteminfo.h" -TradeWindow::TradeWindow(): - Window("Trade: You") +TradeWindow::TradeWindow(Network *network): + Window("Trade: You"), + mNetwork(network) { setContentSize(322, 150); @@ -186,8 +187,8 @@ void TradeWindow::increaseQuantity(int index, bool own, int quantity) void TradeWindow::reset() { - myInventory->resetItems(); - partnerInventory->resetItems(); + myInventory->clear(); + partnerInventory->clear(); tradeButton->setEnabled(false); okButton->setEnabled(true); ok_other = false; @@ -227,7 +228,7 @@ void TradeWindow::receivedOk(bool own) void TradeWindow::tradeItem(Item *item, int quantity) { - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST); outMsg.writeInt16(item->getInvIndex()); outMsg.writeInt32(quantity); @@ -299,7 +300,7 @@ void TradeWindow::action(const std::string &eventId) } else if (eventId == "cancel") { - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_TRADE_CANCEL_REQUEST); } else if (eventId == "ok") @@ -312,7 +313,7 @@ void TradeWindow::action(const std::string &eventId) tempMoney[1] << tempInt; moneyField->setText(tempMoney[1].str()); - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_TRADE_ITEM_ADD_REQUEST); outMsg.writeInt16(0); outMsg.writeInt32(tempInt); @@ -320,12 +321,12 @@ void TradeWindow::action(const std::string &eventId) moneyField->setText(""); } moneyField->setEnabled(false); - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_TRADE_ADD_COMPLETE); } else if (eventId == "trade") { - MessageOut outMsg; + MessageOut outMsg(mNetwork); outMsg.writeInt16(CMSG_TRADE_OK); } } diff --git a/src/gui/trade.h b/src/gui/trade.h index 1a2d0d99..d95cd9ce 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -33,6 +33,7 @@ class Inventory; class Item; class ItemContainer; +class Network; class ScrollArea; /** @@ -46,7 +47,7 @@ class TradeWindow : public Window, gcn::ActionListener /** * Constructor. */ - TradeWindow(); + TradeWindow(Network *network); /** * Destructor. @@ -115,6 +116,7 @@ class TradeWindow : public Window, gcn::ActionListener ItemContainer *partnerItemContainer; private: + Network *mNetwork; gcn::Label *itemNameLabel; gcn::Label *itemDescriptionLabel; gcn::Label *moneyLabel; diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 6d4f93df..59382a4e 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -315,14 +315,6 @@ void UpdaterWindow::download() } } -void updateInputHandler(SDL_KeyboardEvent *keyEvent) -{ - if (keyEvent->keysym.sym == SDLK_ESCAPE) - { - state = EXIT_STATE; - } -} - void UpdaterWindow::logic() { // Update Scroll logic diff --git a/src/gui/updatewindow.h b/src/gui/updatewindow.h index 4fd29ff2..ad554375 100644 --- a/src/gui/updatewindow.h +++ b/src/gui/updatewindow.h @@ -27,7 +27,6 @@ #include <guichan/actionlistener.hpp> #include <string> #include <vector> -#include <SDL_events.h> #include "window.h" @@ -194,6 +193,4 @@ class UpdaterWindow : public Window, public gcn::ActionListener ScrollArea *mScrollArea; /**< Used to scroll news box. */ }; -void updateInputHandler(SDL_KeyboardEvent *keyEvent); - #endif |