src/object/bonus_box.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  * Bonus box : fall from the sky at random time.
00020  * The box can contain bonus or mallus (dynamite, Guns, loss of energy etc).
00021  *****************************************************************************/
00022 
00023 #include "bonus_box.h"
00024 #include <sstream>
00025 #include <iostream>
00026 #include <math.h>
00027 #include "../game/game_mode.h"
00028 #include "../game/game_loop.h"
00029 #include "../game/time.h"
00030 #include "../graphic/sprite.h"
00031 #include "../include/app.h"
00032 #include "../interface/game_msg.h"
00033 #include "../map/camera.h"
00034 #include "../map/map.h"
00035 #include "../network/randomsync.h"
00036 #include "../object/objects_list.h"
00037 #include "../team/macro.h"
00038 #include "../tool/debug.h"
00039 #include "../tool/i18n.h"
00040 #include "../tool/resource_manager.h"
00041 #include "../weapon/explosion.h"
00042 #include "../weapon/weapons_list.h"
00043 
00044 const uint SPEED = 5; // meter / seconde
00045 const uint SPEED_PARACHUTE = 170; // ms per frame
00046 const uint NB_MAX_TRY = 20;
00047 
00048 BonusBox::BonusBox()
00049   : PhysicalObj("bonus_box"){
00050   SetTestRect (29, 29, 63, 6);
00051   m_allow_negative_y = true;
00052   enable = false;
00053 
00054   Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false);
00055   anim = resource_manager.LoadSprite( res, "objet/caisse");
00056   resource_manager.UnLoadXMLProfile(res);
00057 
00058   SetSize(anim->GetSize());
00059   anim->animation.SetLoopMode(false);
00060   anim->SetCurrentFrame(0);
00061 
00062   parachute = true;
00063 
00064   //these values will get read from XML soon
00065   life_points = 41;
00066   nbr_ammo = 3;
00067 
00068   SetSpeed (SPEED, M_PI_2);
00069   PickRandomWeapon();
00070 }
00071 
00072 BonusBox::~BonusBox(){
00073   delete anim;
00074 }
00075 
00076 void BonusBox::Draw()
00077 {
00078   anim->Draw(GetPosition());
00079 }
00080 
00081 void BonusBox::Refresh()
00082 {
00083   // If we touch a character, we remove the bonus box
00084   FOR_ALL_LIVING_CHARACTERS(team, character)
00085   {
00086     if( ObjTouche(*character) )
00087     {
00088       // here is the gift (truly a gift ?!? :)
00089       ApplyBonus (**team, *character);
00090       Ghost();
00091       return;
00092     }
00093   }
00094 
00095   // Refresh animation
00096   if (!anim->IsFinished() && !parachute) anim->Update();
00097 }
00098 
00099 // Say hello to the ground
00100 void BonusBox::SignalCollision()
00101 {
00102   SetAirResistFactor(1.0);
00103 
00104   MSG_DEBUG("bonus", "End of the fall: parachute=%d", parachute);
00105   if (!parachute) return;
00106 
00107   MSG_DEBUG("bonus", "Start of the animation 'fold of the parachute'.");
00108   parachute = false;
00109 
00110   anim->SetCurrentFrame(0);
00111   anim->Start();
00112 }
00113 
00114 // Boxes can explode too ...
00115 void BonusBox::SignalGhostState(bool was_already_dead)
00116 {
00117   if(life_points > 0) return;
00118   ParticleEngine::AddNow(GetCenter() , 10, particle_FIRE, true);
00119   ApplyExplosion(GetCenter(), GameMode::GetInstance()->bonus_box_explosion_cfg);
00120 }
00121 
00122 void BonusBox::PickRandomWeapon() {
00123 
00124   int weapon_count = 0;
00125   int weapon_num = 0;
00126   WeaponsList::weapons_list_it it;
00127   for(it = Config::GetInstance()->GetWeaponsList()->GetList().begin(); it != Config::GetInstance()->GetWeaponsList()->GetList().end(); it++) {
00128     weapon_count++;
00129   }
00130   weapon_num = (int)randomSync.GetDouble(1,weapon_count);
00131   it = Config::GetInstance()->GetWeaponsList()->GetList().begin();
00132   int a=0;
00133   while(a < weapon_num && it != Config::GetInstance()->GetWeaponsList()->GetList().end()) {
00134     it++;
00135     a++;
00136   }
00137   contents = (*it)->GetType();
00138   std::cout<<"Out of "<<weapon_count<<" weapons, weapon "<<weapon_num<<" was selected."<<std::endl;
00139 }
00140 
00141 void BonusBox::ApplyBonus(Team &equipe, Character &ver) {
00142   std::ostringstream txt;
00143     if(ActiveTeam().ReadNbAmmos(Config::GetInstance()->GetWeaponsList()->GetWeapon(contents)->GetName())!=INFINITE_AMMO) {
00144         equipe.m_nb_ammos[ Config::GetInstance()->GetWeaponsList()->GetWeapon(contents)->GetName() ] += nbr_ammo;
00145         txt << Format(ngettext(
00146                 "%s team has won %u %s!",
00147                 "%s team has won %u %ss!",
00148                 2),
00149             ActiveTeam().GetName().c_str(), nbr_ammo, Config::GetInstance()->GetWeaponsList()->GetWeapon(contents)->GetName().c_str());
00150     }
00151     else {
00152         txt << Format(gettext("%s team already has infinite ammo for the %s!"),
00153             ActiveTeam().GetName().c_str(), Config::GetInstance()->GetWeaponsList()->GetWeapon(contents)->GetName().c_str());
00154     }
00155   GameMessages::GetInstance()->Add (txt.str());
00156 }
00157 
00158 
00159 //-----------------------------------------------------------------------------
00160 //-----------------------------------------------------------------------------
00161 // Static methods
00162 bool BonusBox::enable = false;
00163 
00164 // Active les caisses ?
00165 void BonusBox::Enable (bool _enable)
00166 {
00167   MSG_DEBUG("bonus", "Enable ? %d", _enable);
00168   enable = _enable;
00169 }
00170 
00171 uint BonusBox::CountTeams() {
00172   uint nbr_teams=0;
00173   FOR_EACH_TEAM(team) { 
00174     nbr_teams++;
00175   }
00176   return nbr_teams;
00177 }
00178 
00179 bool BonusBox::PlaceBonusBox (BonusBox& bonus_box)
00180 {
00181   if (!bonus_box.PutRandomly(true, 0)) return false;
00182   return true;
00183 }
00184 
00185 bool BonusBox::NewBonusBox()
00186 {
00187 
00188   if (!enable) {
00189      enable=true;
00190     return false;
00191   }
00192   uint nbr_teams=CountTeams();
00193   if(nbr_teams<=1) {
00194         MSG_DEBUG("bonus", "There is less than 2 teams in the game");
00195     return false;
00196   }
00197   // .7 is a magic number to get the probability of boxes falling once every round close to .333
00198   double randValue = randomSync.GetDouble();
00199   if(randValue > (1-pow(.7,1.0/nbr_teams))) {
00200        return false;
00201   }
00202 
00203   BonusBox * box = new BonusBox();
00204   if (!PlaceBonusBox(*box)) {
00205     MSG_DEBUG("bonus", "Missed to put the bonus box");
00206     delete box;
00207   } else {
00208     lst_objects.AddObject(box);
00209     camera.FollowObject(box, true, true);
00210     GameMessages::GetInstance()->Add (_("Is it a gift?"));
00211     return true;
00212   }
00213 
00214   return false;
00215 }

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7