src/weapon/weapons_list.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Refresh des armes.
00020  *****************************************************************************/
00021 
00022 #include "weapons_list.h"
00023 //-----------------------------------------------------------------------------
00024 #include <algorithm>
00025 #include "all.h"
00026 #include "explosion.h"
00027 #include "../game/game_loop.h"
00028 #include "../game/time.h"
00029 #include "../interface/interface.h"
00030 #include "../object/objects_list.h"
00031 #include "../map/camera.h"
00032 #include "../team/macro.h"
00033 #include "../map/maps_list.h"
00034 //-----------------------------------------------------------------------------
00035 
00036 //-----------------------------------------------------------------------------
00037 
00038 WeaponsList::~WeaponsList()
00039 {
00040   weapons_list_it it=m_weapons_list.begin(), end=m_weapons_list.end();
00041   for (; it != end; ++it)
00042   {
00043     delete *it;
00044   }
00045 }
00046 
00047 //-----------------------------------------------------------------------------
00048 
00049 void WeaponsList::AddToList(Weapon* arme, uint num_sort)
00050 {
00051   // insert the pointer
00052   m_weapons_list.push_back(arme);
00053 
00054   m_weapons_map.insert(keybind(num_sort, arme));
00055 
00056   Interface::GetInstance()->weapons_menu.NewItem(arme,num_sort);
00057 }
00058 
00059 //-----------------------------------------------------------------------------
00060 
00061 WeaponsList::WeaponsList()
00062 {
00063   weapons_res_profile = resource_manager.LoadXMLProfile( "weapons.xml", false);
00064   Bazooka* bazooka = new Bazooka;
00065   SubMachineGun* submachine_gun = new SubMachineGun;
00066   Gun* gun = new Gun;
00067   Shotgun* shotgun = new Shotgun;
00068   SnipeRifle* snipe_rifle = new SnipeRifle;
00069   RiotBomb* riot_bomb = new RiotBomb;
00070   AutomaticBazooka* auto_bazooka = new AutomaticBazooka;
00071   Dynamite* dynamite = new Dynamite;
00072   GrenadeLauncher* grenade_launcher = new GrenadeLauncher;
00073   DiscoGrenadeLauncher* disco_grenade_launcher = new DiscoGrenadeLauncher;
00074   ClusterLauncher* cluster_launcher = new ClusterLauncher;
00075   Baseball* baseball = new Baseball;
00076   Mine* mine = new Mine;
00077   AirAttack* air_attack = new AirAttack;
00078   AnvilLauncher* anvil = new AnvilLauncher;
00079   TuxLauncher* tux = new TuxLauncher;
00080   GnuLauncher* gnu_launcher = new GnuLauncher;
00081   PolecatLauncher* polecat_launcher = new PolecatLauncher;
00082   BounceBallLauncher* bounce_ball_launcher = new BounceBallLauncher;
00083   Teleportation* teleportation = new Teleportation;
00084   Parachute* parachute = new Parachute;
00085   Suicide* suicide = new Suicide;
00086   SkipTurn* skipturn = new SkipTurn;
00087   JetPack* jetpack = new JetPack;
00088   Airhammer* airhammer = new Airhammer;
00089   Construct* construct = new Construct;
00090   LowGrav* lowgrav = new LowGrav;
00091   NinjaRope* ninjarope = new NinjaRope;
00092   Blowtorch* blowtorch = new Blowtorch;
00093   Syringe* syringe = new Syringe;
00094 
00095   // Category 1
00096   AddToList(bazooka, 1);
00097   AddToList(submachine_gun, 1);
00098   AddToList(snipe_rifle, 1);
00099   AddToList(gun, 1);
00100   AddToList(shotgun, 1);
00101   AddToList(riot_bomb, 1);
00102   AddToList(auto_bazooka, 1);
00103 
00104   // Category 2
00105   AddToList(dynamite,2);
00106   AddToList(grenade_launcher, 2);
00107   AddToList(cluster_launcher, 2);
00108   AddToList(disco_grenade_launcher, 2);
00109   AddToList(mine,2);
00110 
00111   // Category 3
00112   AddToList(baseball, 3);
00113   AddToList(syringe,3);
00114   AddToList(tux,3);
00115   AddToList(gnu_launcher,3);
00116   AddToList(polecat_launcher,3);
00117   AddToList(air_attack,3);
00118   AddToList(anvil,3);
00119   AddToList(bounce_ball_launcher,3);
00120 
00121   // Category 4
00122   AddToList(ninjarope,4);
00123   AddToList(jetpack,4);
00124   AddToList(parachute,4);
00125   AddToList(teleportation,4);
00126   AddToList(lowgrav,4);
00127 
00128   // Category 5
00129   AddToList(skipturn,5);
00130   AddToList(airhammer,5);
00131   AddToList(construct,5);
00132   AddToList(blowtorch,5);
00133   AddToList(suicide,5);
00134 }
00135 
00136 //-----------------------------------------------------------------------------
00137 
00138 void WeaponsList::Refresh ()
00139 {
00140   ActiveTeam().AccessWeapon().Manage();
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 
00145 WeaponsList::weapons_list_type& WeaponsList::GetList()
00146 {
00147   return m_weapons_list;
00148 }
00149 
00150 //-----------------------------------------------------------------------------
00151 
00152 Weapon* WeaponsList::GetNextWeapon(uint sort, uint index)
00153 {
00154         uint n = m_weapons_map.count(sort);
00155         index = index % n;  // index can't be greater than number of weapons
00156         weapons_map_it it = m_weapons_map.lower_bound(sort);
00157 
00158         if(index + 1 < n)
00159         {
00160                 for(uint i=0; i < index + 1; i++)
00161                         it++;
00162         }
00163 
00164         return it->second;
00165 }
00166 
00167 //-----------------------------------------------------------------------------
00168 
00169 bool WeaponsList::GetWeaponBySort(uint sort, Weapon::Weapon_type &type)
00170 {
00171   uint nb_weapons = m_weapons_map.count(sort);
00172   if (nb_weapons == 0) return false;
00173 
00174   // One or many weapons on this key
00175   std::pair<weapons_map_it, weapons_map_it> p = m_weapons_map.equal_range(sort);
00176   weapons_map_it it = p.first, end = p.second;
00177 
00178   Weapon* next_weapon = it->second;
00179 
00180   if (nb_weapons > 1)
00181     {
00182         // Find index of current weapon
00183         uint current_weapon;
00184         for(current_weapon=0; current_weapon < nb_weapons-1; current_weapon++, it++)
00185         {
00186                 if(it->second == &(ActiveTeam().GetWeapon()))
00187                         break;
00188         }
00189 
00190         // Get next weapon that has enough ammo and can be used on the map
00191         uint i = 0;
00192         do
00193         {
00194                 next_weapon = GetNextWeapon(sort, current_weapon++);
00195         } while(i++ < nb_weapons && (ActiveTeam().ReadNbAmmos(next_weapon->GetName()) == 0 || !(next_weapon->CanBeUsedOnClosedMap() || ActiveMap().IsOpened())));
00196         // this corresponds to:  while (stop-condition && (not-enoughammo || not-usable-on-map))
00197 
00198         // no right weapon has been found
00199         if(i > nb_weapons)
00200                 return false;
00201     }
00202 
00203   type = next_weapon->GetType();
00204   return true;
00205 }
00206 
00207 //-----------------------------------------------------------------------------
00208 
00209 class test_weapon_type {
00210   private:
00211     Weapon::Weapon_type m_type;
00212   public:
00213     test_weapon_type(Weapon::Weapon_type &type) { m_type = type; }
00214         bool operator() ( Weapon* w) { return w->GetType()==m_type; }
00215 };
00216 
00217 Weapon* WeaponsList::GetWeapon (Weapon::Weapon_type type)
00218 {
00219   weapons_list_it it;
00220   it = std::find_if(m_weapons_list.begin(), m_weapons_list.end(), test_weapon_type(type));
00221   assert (it != m_weapons_list.end());
00222   return *it;
00223 }
00224 
00225 //-----------------------------------------------------------------------------

Generated on Mon Jan 1 13:11:00 2007 for Wormux by  doxygen 1.4.7