WeaponGiveAccessory.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 <weapons/WeaponGiveAccessory.h>
00022 #include <weapons/AccessoryStore.h>
00023 #include <engine/ActionController.h>
00024 #include <tank/TankContainer.h>
00025 #include <tank/TankAccessories.h>
00026 #include <tank/TankScore.h>
00027 #include <common/Defines.h>
00028 #include <common/ChannelManager.h>
00029 #include <lang/LangResource.h>
00030 
00031 REGISTER_ACCESSORY_SOURCE(WeaponGiveAccessory);
00032 
00033 WeaponGiveAccessory::WeaponGiveAccessory()
00034 {
00035 
00036 }
00037 
00038 WeaponGiveAccessory::~WeaponGiveAccessory()
00039 {
00040 
00041 }
00042 
00043 bool WeaponGiveAccessory::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
00044 {
00045         if (!Weapon::parseXML(context, accessoryNode)) return false;
00046 
00047         std::string giveaccessory;
00048         while (accessoryNode->getNamedChild("giveaccessory", giveaccessory, false))
00049         {
00050                 Accessory *accessory = context.getAccessoryStore().
00051                         findByPrimaryAccessoryName(giveaccessory.c_str());
00052                 if (!accessory)
00053                 {
00054                         return accessoryNode->returnError(
00055                                 S3D::formatStringBuffer("Failed to find accessory named %s",
00056                                         giveaccessory.c_str()));
00057                 }
00058                 giveAccessories_.push_back(accessory);
00059         }
00060 
00061         if (!accessoryNode->getNamedChild("number", number_)) return false;
00062 
00063         return true;
00064 }
00065 
00066 void WeaponGiveAccessory::fireWeapon(ScorchedContext &context,
00067         WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
00068 {
00069         context.getActionController().addAction(
00070                 new CallbackWeapon("WeaponGiveAccessory", this, 0, 0, 
00071                         weaponContext, position, velocity));
00072 }
00073 
00074 void WeaponGiveAccessory::weaponCallback(
00075         ScorchedContext &context,
00076         WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity,
00077         unsigned int userData)
00078 {
00079         Tank *tank = context.getTankContainer().getTankById(weaponContext.getPlayerId());
00080         if (!tank) return;
00081 
00082         std::vector<Accessory *>::iterator itor;
00083         for (itor = giveAccessories_.begin();
00084                 itor != giveAccessories_.end();
00085                 itor++)
00086         {
00087                 Accessory *accessory = (*itor);
00088 
00089                 if (number_ > 0)
00090                 {
00091                         if (tank->getAccessories().accessoryAllowed(accessory, number_))
00092                         {
00093                                 tank->getAccessories().add(accessory, number_);
00094                                 {
00095                                         ChannelText text("combat", 
00096                                                 LANG_RESOURCE_3("TANK_GET_ACCESSORY",
00097                                                 "[p:{0}] received {1} * [w:{2}]", 
00098                                                 tank->getTargetName(),
00099                                                 S3D::formatStringBuffer("%i", number_), 
00100                                                 accessory->getName()));
00101                                         ChannelManager::showText(context, text);
00102                                 }
00103                         }
00104                         else
00105                         {
00106                                 int money = accessory->getSellPrice() * number_;
00107                                 tank->getScore().setMoney(tank->getScore().getMoney() + money);
00108                                 {
00109                                         ChannelText text("combat", 
00110                                                 LANG_RESOURCE_2("TANK_GET_MONEY",
00111                                                 "[p:{0}] received ${1}", 
00112                                                 tank->getTargetName(), 
00113                                                 S3D::formatStringBuffer("%i", money)));
00114                                         ChannelManager::showText(context, text);
00115                                 }
00116                         }
00117                 }
00118                 else
00119                 {
00120                         int count = tank->getAccessories().getAccessoryCount(accessory);
00121                         if (count > 0)
00122                         {
00123                                 int loose = MIN(count, -number_);
00124 
00125                                 tank->getAccessories().rm(accessory, loose);
00126                                 {
00127                                         ChannelText text("combat", 
00128                                                 LANG_RESOURCE_3("TANK_LOST_ACCESSORY",
00129                                                 "[p:{0}] lost {1} * [w:{2}]", 
00130                                                 tank->getTargetName(),
00131                                                 S3D::formatStringBuffer("%i", loose), 
00132                                                 accessory->getName()));
00133                                         ChannelManager::showText(context, text);
00134                                 }
00135                         }
00136                 }
00137         }
00138 }

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