BuyAccessoryDialog.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <dialogs/BuyAccessoryDialog.h>
00022 #include <dialogs/GiftMoneyDialog.h>
00023 #include <GLW/GLWWindowManager.h>
00024 #include <GLW/GLWTextButton.h>
00025 #include <GLW/GLWIcon.h>
00026 #include <GLEXT/GLViewPort.h>
00027 #include <client/ClientState.h>
00028 #include <client/ScorchedClient.h>
00029 #include <common/OptionsScorched.h>
00030 #include <graph/OptionsDisplay.h>
00031 #include <common/OptionsTransient.h>
00032 #include <common/Defines.h>
00033 #include <coms/ComsMessageSender.h>
00034 #include <coms/ComsBuyAccessoryMessage.h>
00035 #include <weapons/AccessoryStore.h>
00036 #include <tank/TankContainer.h>
00037 #include <tank/TankScore.h>
00038 #include <tank/TankAccessories.h>
00039 #include <stdio.h>
00040 
00041 BuyAccessoryDialog *BuyAccessoryDialog::instance_ = 0;
00042 
00043 BuyAccessoryDialog *BuyAccessoryDialog::instance()
00044 {
00045         if (!instance_)
00046         {
00047                 instance_ = new BuyAccessoryDialog;
00048         }
00049         return instance_;
00050 }
00051 
00052 BuyAccessoryDialog::BuyAccessoryDialog() : 
00053         GLWWindow("", 10.0f, 10.0f, 465.0f, 300.0f, 0,
00054                 "Allows the current player to buy and sell\n"
00055                 "weapons and other accessories."),
00056         firstDrawTime_(true), sellTab_(0), flag_(0)
00057 {
00058         okId_ = addWidget(new GLWTextButton(LANG_RESOURCE("OK", "Ok"), 400, 10, 55, this, 
00059                 GLWButton::ButtonFlagOk | GLWButton::ButtonFlagCenterX))->getId();
00060 
00061         if (!ScorchedClient::instance()->getOptionsGame().getTutorial()[0])
00062         {
00063                 giftId_ = addWidget(new GLWTextButton(LANG_RESOURCE("GIFT", "Gift"), 320, 10, 70, this, 
00064                         GLWButton::ButtonFlagCenterX))->getId();
00065         }
00066 
00067         topPanel_ = (GLWPanel *)
00068                 addWidget(new GLWPanel(10, 265, 450, 50));
00069 
00070         defaultTab_ = (GLWCheckBoxText *) 
00071                 addWidget(new GLWCheckBoxText(18.0f, 230.0f, LANG_RESOURCE("DEFAULT_TAB","Default Tab"), false, 3.0f));
00072         defaultTab_->getCheckBox().setW(12);
00073         defaultTab_->getCheckBox().setH(12);
00074         defaultTab_->getLabel().setSize(10);
00075         defaultTab_->getCheckBox().setHandler(this);
00076 
00077         addWidget(new GLWLabel(15, 9, LANG_RESOURCE("SORT_LABEL", "Sort by:")));
00078 
00079         sortDropDown_ = (GLWDropDownText *) addWidget(new GLWDropDownText(100, 9, 100));
00080 
00081         sortDropDown_->addText(LANG_RESOURCE("SORT_NOTHING", "Nothing"), "Nothing");
00082         sortDropDown_->addText(LANG_RESOURCE("SORT_NAME", "Name"), "Name");
00083         sortDropDown_->addText(LANG_RESOURCE("SORT_PRICE", "Price"), "Price");
00084         sortDropDown_->setName("Sort");
00085         sortDropDown_->setHandler(this);
00086 }
00087 
00088 BuyAccessoryDialog::~BuyAccessoryDialog()
00089 {
00090 }
00091 
00092 void BuyAccessoryDialog::draw()
00093 {
00094         if (sellTab_ && firstDrawTime_)
00095         {
00096                 firstDrawTime_ = false;
00097                 float screenHeight = (float) GLViewPort::getHeight();
00098                 float addition = 0;
00099                 if (screenHeight > 340) addition = screenHeight - 340;
00100                 if (addition > 200) addition = 200;
00101 
00102                 setH(300 + addition);
00103                 topPanel_->setY(240 + addition);
00104                 defaultTab_->setY(190 + addition);
00105 
00106                 std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00107                 for (itor = getWidgets().begin();
00108                         itor != getWidgets().end();
00109                         itor++)
00110                 {
00111                         GLWPanel::GLWPanelEntry &entry = (*itor);
00112                         if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00113                         {
00114                                 GLWTab *tab = (GLWTab *) entry.widget;
00115                                 tab->setH(165 + addition);
00116                         }
00117                 }
00118 
00119                 needCentered_ = true;
00120         }
00121 
00122         GLWWindow::draw();
00123 
00124         LANG_RESOURCE_VAR(BUY, "BUY", "Buy");
00125         LANG_RESOURCE_VAR(SELL, "SELL", "Sell");
00126         Vector red(0.7f, 0.0f, 0.0f);
00127         Vector green(0.0f, 0.4f, 0.0f);
00128         GLWFont::instance()->getGameFont()->draw(
00129                 green, 12.0f, x_ + 260.0f, y_ + topPanel_->getY() - 50.0f, 0.0f, BUY);
00130         GLWFont::instance()->getGameFont()->draw(
00131                 red, 12.0f, x_ + 360.0f, y_ + topPanel_->getY() - 50.0f, 0.0f, SELL);
00132 }
00133 
00134 void BuyAccessoryDialog::addPlayerName()
00135 {
00136         float flagOffset = 0.0f;
00137         if (flag_) flagOffset = flag_->getOffset();
00138         topPanel_->clear();
00139 
00140         Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00141         if (!tank) return;
00142         flag_ = (GLWFlag *) topPanel_->addWidget(new GLWFlag(tank->getColor(), 5, 15, 60));
00143         flag_->setOffset(flagOffset);
00144         topPanel_->addWidget(new GLWLabel(75, 10, tank->getTargetName()));
00145         topPanel_->addWidget(new GLWLabel(260, 20, 
00146                 LANG_STRING(S3D::formatStringBuffer("$%i", tank->getScore().getMoney()))));
00147         topPanel_->addWidget(new GLWLabel(260, 0, 
00148                 LANG_RESOURCE_2("ROUND_OF", "Round {0} of {1}",
00149                 S3D::formatStringBuffer("%i", ScorchedClient::instance()->getOptionsTransient().getCurrentRoundNo()),
00150                 S3D::formatStringBuffer("%i", ScorchedClient::instance()->getOptionsGame().getNoRounds()))));
00151 }
00152 
00153 void BuyAccessoryDialog::addTabs()
00154 {
00155         if (sellTab_) return;
00156 
00157         std::set<std::string> &groupNames =
00158                 ScorchedClient::instance()->
00159                         getAccessoryStore().getTabGroupNames();
00160         std::set<std::string>::reverse_iterator itor;
00161         for (itor = groupNames.rbegin();
00162                 itor != groupNames.rend();
00163                 itor++)
00164         {
00165                 std::string name = (*itor);
00166                 GLWTab *tab = (GLWTab *)
00167                         addWidget(new GLWTab(name, LANG_RESOURCE(name, name), 10, 40, 450, 160));
00168                 buyTabs_[name] = tab;
00169                 tab->setHandler(this);
00170         }
00171         sellTab_ = (GLWTab *)
00172                 addWidget(new GLWTab("Inv", LANG_RESOURCE("INVENTORY_TAB", "Inv"), 10, 40, 450, 160));
00173         sellTab_->setHandler(this);
00174 
00175         favouritesTab_ = (GLWTab *)
00176                 addWidget(new GLWTab("Fav", LANG_RESOURCE("FAVOURITES_TAB", "Fav"), 10, 40, 450, 160));
00177         favouritesTab_->setHandler(this);
00178 }
00179 
00180 void BuyAccessoryDialog::playerRefresh()
00181 {
00182         addPlayerName();
00183         addPlayerWeapons();
00184 }
00185 
00186 void BuyAccessoryDialog::playerRefreshKeepPos()
00187 {
00188         std::map<std::string, int> scrollPositions;
00189         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00190         for (itor = getWidgets().begin();
00191                 itor != getWidgets().end();
00192                 itor++)
00193         {
00194                 GLWPanel::GLWPanelEntry &entry = (*itor);
00195                 if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00196                 {
00197                         GLWTab *tab = (GLWTab *) entry.widget;
00198                         scrollPositions[tab->getName()] = tab->getScrollBar().getCurrent();
00199                 }
00200         }
00201 
00202         addPlayerName();
00203         addPlayerWeapons();
00204 
00205         for (itor = getWidgets().begin();
00206                 itor != getWidgets().end();
00207                 itor++)
00208         {
00209                 GLWPanel::GLWPanelEntry &entry = (*itor);
00210                 if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00211                 {
00212                         GLWTab *tab = (GLWTab *) entry.widget;
00213                         tab->getScrollBar().setCurrent(scrollPositions[tab->getName()]);
00214                 }
00215         }
00216 }
00217 
00218 void BuyAccessoryDialog::addPlayerWeapons()
00219 {
00220         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00221         for (itor = getWidgets().begin();
00222                 itor != getWidgets().end();
00223                 itor++)
00224         {
00225                 GLWPanel::GLWPanelEntry &entry = (*itor);
00226                 if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00227                 {
00228                         GLWTab *tab = (GLWTab *) entry.widget;
00229                         tab->clear();
00230                 }
00231         }
00232 
00233         addPlayerFavorites();
00234         addPlayerWeaponsSell();
00235 
00236         std::set<std::string> &groupNames =
00237                 ScorchedClient::instance()->
00238                         getAccessoryStore().getTabGroupNames();
00239         std::set<std::string>::iterator groupItor;
00240         for (groupItor = groupNames.begin();
00241                 groupItor != groupNames.end();
00242                 groupItor++)
00243         {
00244                 addPlayerWeaponsBuy(buyTabs_[*groupItor], (*groupItor).c_str());
00245         }
00246 }
00247 
00248 void BuyAccessoryDialog::addPlayerFavorites()
00249 {
00250         Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00251         if (!tank) return;
00252 
00253         float height = 10.0f;
00254         std::list<Accessory *> acessories = 
00255                 ScorchedClient::instance()->
00256                         getAccessoryStore().getAllAccessories(
00257                                 OptionsDisplay::instance()->getAccessorySortKey());
00258         std::list<Accessory *>::reverse_iterator itor;
00259         for (itor = acessories.rbegin();
00260                 itor != acessories.rend();
00261                 itor++)
00262         {
00263                 Accessory *current = *itor;
00264                 if (favorites_.find(current->getName()) != favorites_.end())
00265                 {
00266                         if (addAccessory(tank, favouritesTab_, height, current)) height += 24.0f;
00267                 }
00268         }
00269 }
00270 
00271 void BuyAccessoryDialog::addPlayerWeaponsBuy(GLWTab *tab, const char *group)
00272 {
00273         Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00274         if (!tank) return;
00275 
00276         std::list<Accessory *> weapons = ScorchedClient::instance()->
00277                 getAccessoryStore().getAllAccessoriesByTabGroup(
00278                         group,
00279                         OptionsDisplay::instance()->getAccessorySortKey());
00280 
00281         float height = 10.0f;
00282         std::list<Accessory *>::reverse_iterator itor2;
00283         for (itor2 = weapons.rbegin();
00284                 itor2 != weapons.rend();
00285                 itor2++)
00286         {
00287                 Accessory *current = (*itor2);
00288                 if (addAccessory(tank, tab, height, current)) height += 24.0f;
00289         }
00290 }
00291 
00292 void BuyAccessoryDialog::addPlayerWeaponsSell()
00293 {
00294         Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00295         if (!tank) return;
00296 
00297         float height = 10.0f;
00298         std::list<Accessory *> tankAccessories;
00299         tank->getAccessories().getAllAccessories(
00300                 tankAccessories);
00301         ScorchedClient::instance()->getAccessoryStore().sortList(tankAccessories,
00302                 OptionsDisplay::instance()->getAccessorySortKey());
00303         std::list<Accessory *>::reverse_iterator itor;
00304         for (itor = tankAccessories.rbegin();
00305                 itor != tankAccessories.rend();
00306                 itor++)
00307         {
00308                 Accessory *current = *itor;
00309                 if (addAccessory(tank, sellTab_, height, current)) height += 24.0f;
00310         }
00311 }
00312 
00313 bool BuyAccessoryDialog::addAccessory(
00314         Tank *tank, GLWTab *tab, 
00315         float height, Accessory *current)
00316 {
00317         if (!tank->getAccessories().accessoryAllowed(current, 0)) return false;
00318         if (current->getNoBuy()) return false;
00319 
00320         int currentNumber = 
00321                 tank->getAccessories().getAccessoryCount(current);
00322 
00323         // Panel
00324         GLWPanel *newPanel = (GLWPanel *)
00325                 tab->addWidget(new GLWPanel(5.0f, (float) height, 415.0f, 22.0f, true));
00326         newPanel->setToolTip(&current->getToolTip());
00327 
00328         // Favorites checkbox
00329         GLWCheckBox *sortBox = (GLWCheckBox *) newPanel->addWidget(new GLWCheckBox(2, 4, false));
00330         sortBox->setHandler(this);
00331         sortBox->setW(14);
00332         sortBox->setH(14);
00333         sortBox->setToolTip(new ToolTip(ToolTip::ToolTipHelp, 
00334                 LANG_RESOURCE("FAVOURITE_WEAPON", "Favorite Weapon"),
00335                 LANG_RESOURCE("FAVOURITE_WEAPON_TOOLTIP", "Set/unset this weapon as a favorite.\n"
00336                 "Favorite weapons will show in the\n"
00337                 "favorites tab.")));
00338         sortBox->setState(favorites_.find(current->getName()) != favorites_.end());
00339         favMap_[sortBox->getId()] = current;
00340         
00341         // Others
00342         newPanel->addWidget(new GLWLabel(20, 0, 
00343                 tank->getAccessories().getAccessoryCountString(current), 12.0f));
00344         newPanel->addWidget(new GLWIcon(45, 4, 16, 16, current->getTexture()));
00345         newPanel->addWidget(new GLWLabel(65, 0, LANG_RESOURCE(current->getName(), current->getName()), 12.0f));
00346 
00347         // Buy Button
00348         if (tank->getAccessories().accessoryAllowed(current, current->getBundle()) && 
00349                 current->getPrice() <= tank->getScore().getMoney())
00350         {
00351                 GLWTextButton *button = (GLWTextButton *)
00352                         newPanel->addWidget(new GLWTextButton(
00353                                 LANG_STRING(S3D::formatStringBuffer("$%i/%i",
00354                                         current->getPrice(), current->getBundle())), 
00355                                         210, 2, 100, this, 
00356                         GLWButton::ButtonFlagCenterX, 12.0f));
00357                 button->setColor(Vector(0.0f, 0.4f, 0.0f));
00358                 button->setToolTip(new ToolTip(ToolTip::ToolTipHelp, 
00359                         LANG_RESOURCE("BUY", "Buy"), 
00360                         LANG_RESOURCE_3("BUY_TOOLTIP", "Buy {0} {1}(s) for ${2}",
00361                                 S3D::formatStringBuffer("%i", current->getBundle()),
00362                                 current->getName(),
00363                                 S3D::formatStringBuffer("%i", current->getPrice()))));
00364                 button->setH(button->getH() - 2.0f);
00365                 buyMap_[button->getId()] = current;
00366         }
00367         else
00368         {
00369                 GLWLabel *label = (GLWLabel *)
00370                         newPanel->addWidget(new GLWLabel(
00371                                 260, 0, 
00372                                 LANG_STRING(S3D::formatStringBuffer("$%i/%i",
00373                                         current->getPrice(), current->getBundle())), 12.0f));
00374                 label->setX(label->getX() - label->getW() / 2);
00375                 label->setColor(Vector(0.4f, 0.4f, 0.4f));
00376         }
00377 
00378         // Sell Button
00379         if (currentNumber > 0)
00380         {
00381                 GLWTextButton *button = (GLWTextButton *)
00382                         newPanel->addWidget(new GLWTextButton(
00383                                 LANG_STRING(S3D::formatStringBuffer("$%i/%i",
00384                                         current->getSellPrice(), 1)), 
00385                                         312, 2, 100, this,
00386                         GLWButton::ButtonFlagCenterX, 12.0f));
00387                 button->setColor(Vector(0.7f, 0.0f, 0.0f));
00388                 button->setToolTip(new ToolTip(ToolTip::ToolTipHelp, 
00389                         LANG_RESOURCE("SELL", "Sell"), 
00390                         LANG_RESOURCE_2("SELL_TOOLTIP", "Sell 1 {0} for ${1}",
00391                                 current->getName(),
00392                                 S3D::formatStringBuffer("%i", current->getSellPrice()))));
00393                 button->setH(button->getH() - 2.0f);
00394                 sellMap_[button->getId()] = current;
00395         }
00396 
00397         return true;
00398 }
00399 
00400 void BuyAccessoryDialog::display()
00401 {
00402         addTabs();
00403         loadFavorites();
00404 
00405         sortDropDown_->setHandler(0);
00406 
00407         switch (OptionsDisplay::instance()->getAccessorySortKey())
00408         {
00409         case AccessoryStore::SortName:
00410                 sortDropDown_->setCurrentText(LANG_RESOURCE("SORT_NAME", "Name"));
00411                 break;
00412 
00413         case AccessoryStore::SortPrice:
00414                 sortDropDown_->setCurrentText(LANG_RESOURCE("SORT_PRICE", "Price"));
00415                 break;
00416 
00417         case AccessoryStore::SortNothing:
00418                 sortDropDown_->setCurrentText(LANG_RESOURCE("SORT_NOTHING", "Nothing"));
00419                 break;
00420         }
00421 
00422         sortDropDown_->setHandler(this);
00423 
00424         Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00425         if (tank)
00426         {
00427                 if (buyTabs_.find("weapon") != buyTabs_.end())
00428                 {
00429                         buyTabs_["weapon"]->setDepressed();
00430                 }
00431                 const char *buyTab = OptionsDisplay::instance()->getBuyTab();
00432                 std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00433                 for (itor = getWidgets().begin();
00434                         itor != getWidgets().end();
00435                         itor++)
00436                 {
00437                         GLWPanel::GLWPanelEntry &entry = (*itor);
00438                         if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00439                         {
00440                                 GLWTab *tab = (GLWTab *) entry.widget;
00441                                 if (0 == strcmp(buyTab, tab->getName()))
00442                                 {
00443                                         tab->setDepressed();
00444                                         break;
00445                                 }
00446                         }
00447                 }
00448                 tabDown(0);
00449         
00450                 playerRefresh();
00451         }
00452 }
00453 
00454 void BuyAccessoryDialog::tabDown(unsigned int id)
00455 {
00456         const char *buyTab = OptionsDisplay::instance()->getBuyTab();
00457         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00458         for (itor = getWidgets().begin();
00459                 itor != getWidgets().end();
00460                 itor++)
00461         {
00462                 GLWPanel::GLWPanelEntry &entry = (*itor);
00463                 if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00464                 {
00465                         GLWTab *tab = (GLWTab *) entry.widget;
00466                         if (tab->getDepressed())
00467                         {
00468                                 if (0 == strcmp(buyTab, tab->getName()))
00469                                 {
00470                                         defaultTab_->getCheckBox().setState(true);
00471                                 }
00472                                 else
00473                                 {
00474                                         defaultTab_->getCheckBox().setState(false);
00475                                 }
00476                                 break;
00477                         }
00478                 }
00479         }
00480 }
00481 
00482 void BuyAccessoryDialog::select(unsigned int id, const int pos, GLWSelectorEntry value)
00483 {
00484         if (id == sortDropDown_->getId())
00485         {
00486                 OptionsDisplay *display = OptionsDisplay::instance();
00487                 const char *dataText = value.getDataText();
00488 
00489                 if (strcmp(dataText, "Name") == 0)
00490                         display->getAccessorySortKeyEntry().setValue(AccessoryStore::SortName);
00491                 else if (strcmp(dataText, "Price") == 0)
00492                         display->getAccessorySortKeyEntry().setValue(AccessoryStore::SortPrice);
00493                 else
00494                         display->getAccessorySortKeyEntry().setValue(AccessoryStore::SortNothing);
00495 
00496                 playerRefreshKeepPos();
00497         }
00498 }
00499 
00500 void BuyAccessoryDialog::stateChange(bool state, unsigned int id)
00501 {
00502         if (id == defaultTab_->getCheckBox().getId())
00503         {
00504                 if (defaultTab_->getCheckBox().getState())
00505                 {
00506                         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00507                         for (itor = getWidgets().begin();
00508                                 itor != getWidgets().end();
00509                                 itor++)
00510                         {
00511                                 GLWPanel::GLWPanelEntry &entry = (*itor);
00512                                 if (entry.widget->getMetaClassId() == sellTab_->getMetaClassId())
00513                                 {
00514                                         GLWTab *tab = (GLWTab *) entry.widget;
00515                                         if (tab->getDepressed())
00516                                         {
00517                                                 OptionsDisplay::instance()->getBuyTabEntry().setValue(tab->getName());
00518                                                 break;
00519                                         }
00520                                 }
00521                         }
00522                 }
00523                 else
00524                 {
00525                         OptionsDisplay::instance()->getBuyTabEntry().setValue("");
00526                 }
00527         }
00528         else
00529         {
00530                 // The favorites check box has been clicked
00531                 std::map<unsigned int, Accessory *>::iterator findItor
00532                         = favMap_.find(id);
00533                 if (findItor != favMap_.end())
00534                 {
00535                         if (state)
00536                         {
00537                                 favorites_.insert((*findItor).second->getName());
00538                         }
00539                         else
00540                         {
00541                                 favorites_.erase((*findItor).second->getName());
00542                         }
00543 
00544                         playerRefreshKeepPos();
00545                         favouritesTab_->calculateVisible();
00546                         playerRefreshKeepPos();
00547                 }
00548         }
00549 }
00550 
00551 void BuyAccessoryDialog::buttonDown(unsigned int id)
00552 {
00553         if (id == okId_)
00554         {
00555                 saveFavorites();
00556                 ScorchedClient::instance()->getGameState().stimulate(ClientState::StimAutoDefense);
00557         }
00558         else if (id == giftId_)
00559         {
00560                 GLWWindowManager::instance()->showWindow(
00561                         GiftMoneyDialog::instance()->getId());
00562                 GLWWindowManager::instance()->hideWindow(
00563                         getId());
00564         }
00565         else
00566         {
00567                 Tank *tank = ScorchedClient::instance()->getTankContainer().getCurrentTank();
00568                 if (!tank) return;
00569 
00570                 std::map<unsigned int, Accessory *>::iterator itor;
00571                 itor = buyMap_.find(id);
00572                 if (itor != buyMap_.end())
00573                 {
00574                         // Tell the server to add the accessory
00575                         Accessory *acc = itor->second;
00576                         ComsBuyAccessoryMessage buyMessage(tank->getPlayerId(), acc->getAccessoryId());
00577                         ComsMessageSender::sendToServer(buyMessage);
00578 
00579                         // Add the accessory
00580                         tank->getAccessories().add(acc, acc->getBundle());
00581                         tank->getScore().setMoney(tank->getScore().getMoney() - acc->getPrice());
00582 
00583                         // Refresh the window
00584                         playerRefreshKeepPos();
00585                 }
00586                 else
00587                 {
00588                         itor = sellMap_.find(id);
00589                         if (itor != sellMap_.end())
00590                         {
00591                                 // Tell the server to add the accessory
00592                                 Accessory *acc = itor->second;
00593                                 ComsBuyAccessoryMessage buyMessage(tank->getPlayerId(), acc->getAccessoryId(), false);
00594                                 ComsMessageSender::sendToServer(buyMessage);
00595 
00596                                 // Add the accessory
00597                                 tank->getAccessories().rm(acc, 1);
00598                                 tank->getScore().setMoney(tank->getScore().getMoney() + acc->getSellPrice());
00599 
00600                                 // Refresh the window
00601                                 playerRefreshKeepPos();
00602                         }
00603                 }
00604         }
00605 }
00606 
00607 void BuyAccessoryDialog::loadFavorites()
00608 {
00609         std::string filename = 
00610                 S3D::getSettingsFile(S3D::formatStringBuffer("weaponfavorites-%s.xml", 
00611                         ScorchedClient::instance()->getOptionsGame().getMod()));
00612 
00613         favorites_.clear();
00614         XMLFile file;
00615         if (!file.readFile(filename))
00616         {
00617                 S3D::dialogMessage("BuyAccessoryDialog", S3D::formatStringBuffer(
00618                                           "Failed to parse \"%s\"\n%s", 
00619                                           filename.c_str(),
00620                                           file.getParserError()));
00621                 return;
00622         }
00623         if (!file.getRootNode()) return; // Empty File
00624 
00625         std::string accessory;
00626         while (file.getRootNode()->getNamedChild("accessory", accessory, false))
00627         {
00628                 favorites_.insert(accessory);
00629         }
00630 }
00631 
00632 void BuyAccessoryDialog::saveFavorites()
00633 {
00634         std::string filename = 
00635                 S3D::getSettingsFile(S3D::formatStringBuffer("weaponfavorites-%s.xml", 
00636                         ScorchedClient::instance()->getOptionsGame().getMod()));
00637 
00638         XMLNode node("accessories");
00639         std::set<std::string>::iterator itor;
00640         for (itor = favorites_.begin();
00641                 itor != favorites_.end();
00642                 itor++)
00643         {
00644                 std::string accessory = *itor;
00645 
00646                 XMLNode *accessoryNode = 
00647                         new XMLNode("accessory", accessory.c_str());
00648                 node.addChild(accessoryNode);
00649         }
00650         node.writeToFile(filename);
00651 }

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3