TankWeapon.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/AccessoryStore.h>
00022 #include <weapons/WeaponMoveTank.h>
00023 #include <tank/TankWeapon.h>
00024 #include <tank/TankContainer.h>
00025 #include <tank/TankAccessories.h>
00026 #include <target/TargetLife.h>
00027 #include <engine/ScorchedContext.h>
00028 #include <common/Defines.h>
00029 #include <common/ChannelManager.h>
00030 #ifndef S3D_SERVER
00031         #include <client/ScorchedClient.h>
00032         #include <client/ClientState.h>
00033         #include <landscape/Landscape.h>
00034 #endif
00035 #include <landscapemap/LandscapeMaps.h>
00036 #include <landscapemap/MovementMap.h>
00037 #include <lang/LangResource.h>
00038 
00039 TankWeapon::TankWeapon(ScorchedContext &context) : 
00040         currentWeapon_(0), context_(context),
00041         tank_(0)
00042 {
00043 }
00044 
00045 TankWeapon::~TankWeapon()
00046 {
00047 }
00048 
00049 void TankWeapon::newMatch()
00050 {
00051         setCurrentWeapon(0);
00052 }
00053 
00054 void TankWeapon::changed()
00055 {
00056         if (!tank_->getAccessories().canUse(currentWeapon_) ||
00057                 currentWeapon_ == 0)
00058         {
00059                 setCurrentWeapon(0);
00060                 std::list<Accessory *> &result =
00061                         tank_->getAccessories().getAllAccessoriesByGroup("weapon");
00062                 std::list<Accessory *>::iterator itor;
00063                 for (itor = result.begin();
00064                         itor != result.end();
00065                         itor++)
00066                 {
00067                         if (tank_->getAccessories().canUse(*itor))
00068                         {
00069                                 setWeapon(*itor);
00070                                 break;
00071                         }
00072                 }
00073         }
00074 }
00075 
00076 bool TankWeapon::setWeapon(Accessory *wp)
00077 {
00078         if (tank_->getAccessories().canUse(wp))
00079         {
00080                 setCurrentWeapon(wp);
00081                 return true;
00082         }
00083         return false;
00084 }
00085 
00086 Accessory *TankWeapon::getCurrent()
00087 {
00088         return currentWeapon_;
00089 }
00090 
00091 void TankWeapon::setCurrentWeapon(Accessory *wp)
00092 {
00093 #ifndef S3D_SERVER
00094         if (!context_.getServerMode())
00095         {
00096                 // Only show this information on this tanks client
00097                 if (ScorchedClient::instance()->getTankContainer().getCurrentDestinationId() ==
00098                         tank_->getDestinationId() &&
00099                         ScorchedClient::instance()->getGameState().getState() == ClientState::StatePlaying)
00100                 {
00101 
00102                 // Turn off fuel display (if any)
00103                 if (currentWeapon_ && 
00104                         currentWeapon_->getPositionSelect() != Accessory::ePositionSelectNone)
00105                 {
00106                         Landscape::instance()->restoreLandscapeTexture();
00107                 }
00108                 // Turn on selection display (if any)
00109                 if (wp &&
00110                         wp->getPositionSelect() != Accessory::ePositionSelectNone)
00111                 {
00112                         if (wp->getPositionSelect() == Accessory::ePositionSelectFuel)
00113                         {
00114                                 WeaponMoveTank *moveWeapon = (WeaponMoveTank *)
00115                                         context_.getAccessoryStore().findAccessoryPartByAccessoryId(
00116                                                 wp->getAccessoryId(), "WeaponMoveTank");
00117                                 if (moveWeapon)
00118                                 {
00119                                         MovementMap mmap(
00120                                                 tank_, 
00121                                                 context_);
00122                                         mmap.calculateAllPositions(mmap.getFuel(moveWeapon));
00123                                         mmap.movementTexture(); 
00124                                 }
00125                         }
00126                         else if (wp->getPositionSelect() == Accessory::ePositionSelectFuelLimit)
00127                         {
00128                                 MovementMap mmap(
00129                                         tank_, 
00130                                         context_);
00131                                 mmap.calculateAllPositions(fixed(wp->getPositionSelectLimit()));
00132                                 mmap.movementTexture(); 
00133                         }
00134                         else if (wp->getPositionSelect() == Accessory::ePositionSelectLimit)
00135                         {
00136                                 MovementMap::limitTexture(tank_->getLife().getTargetPosition(), 
00137                                         wp->getPositionSelectLimit());
00138                         }
00139 
00140                         ChannelText text("banner",
00141                                 LANG_RESOURCE_1(
00142                                         "GROUND_WEAPON_ACTIVATE", 
00143                                         "Click ground to activate {0}",
00144                                         wp->getName()));
00145                         ChannelManager::showText(ScorchedClient::instance()->getContext(), 
00146                                 text);
00147                 }
00148 
00149                 }
00150         }
00151 #endif
00152 
00153         currentWeapon_ = wp;
00154 }
00155 
00156 const char *TankWeapon::getWeaponString()
00157 {
00158         if (!getCurrent()) return "";
00159 
00160         static char buffer[256];
00161         int count = tank_->getAccessories().getAccessoryCount(getCurrent());
00162         snprintf(buffer, 256, ((count>0)?"%s (%i)":"%s (In)"),
00163                 getCurrent()->getName(), count);
00164         return buffer;
00165 }

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