src/game/game_mode.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  * Wormux configuration : all main configuration variables have there default
00020  * value here. They should all be modifiable using the xml config file
00021  *****************************************************************************/
00022 
00023 #include "game_mode.h"
00024 #include <iostream>
00025 #include "config.h"
00026 #include "game_loop.h"
00027 #include "../tool/file_tools.h"
00028 #include "../tool/i18n.h"
00029 #include "../weapon/all.h"
00030 #include "../weapon/weapons_list.h"
00031 
00032 GameMode * GameMode::singleton = NULL;
00033 
00034 GameMode * GameMode::GetInstance() {
00035   if (singleton == NULL) {
00036     singleton = new GameMode();
00037   }
00038   return singleton;
00039 }
00040 
00041 GameMode::GameMode()
00042 {
00043   max_characters = 6;
00044   max_teams = 4;
00045   duration_turn = 60;
00046   duration_exchange_player = 2;
00047   duration_before_death_mode = 20 * 60;
00048   damage_per_turn_during_death_mode = 5;
00049   gravity = 9.81;
00050   safe_fall = 10;
00051   damage_per_fall_unit = 7;
00052   duration_move_player = 3;
00053   allow_character_selection = BEFORE_FIRST_ACTION_AND_END_TURN;
00054   character.init_energy = 100; /* overvriten whenreading XML */
00055   character.max_energy = 100; /* overvriten whenreading XML */
00056   character.mass = 100;
00057   character.air_resist_factor = 1.0;
00058   character.jump_strength = 8;
00059   character.jump_angle = -60;
00060   character.super_jump_strength = 11;
00061   character.super_jump_angle = -80;
00062   character.back_jump_strength = 9;
00063   character.back_jump_angle = -100;
00064 }
00065 
00066 // Load data options from the selected game_mode
00067 bool GameMode::LoadXml(xmlpp::Element *xml)
00068 {
00069   std::string txt;
00070   if (XmlReader::ReadString(xml, "allow_character_selection", txt))
00071   {
00072     if (txt == "always")
00073       allow_character_selection = ALWAYS;
00074     else if (txt == "never")
00075       allow_character_selection = NEVER;
00076     else if (txt == "change_on_end_turn")
00077       allow_character_selection = CHANGE_ON_END_TURN;
00078     else if (txt == "before_first_action_and_end_turn")
00079       allow_character_selection = BEFORE_FIRST_ACTION_AND_END_TURN;
00080     else if (txt == "before_first_action")
00081       allow_character_selection = BEFORE_FIRST_ACTION;
00082   }
00083 
00084   XmlReader::ReadUint(xml, "duration_turn", duration_turn);
00085   XmlReader::ReadUint(xml, "duration_move_player", duration_move_player);
00086   XmlReader::ReadUint(xml, "duration_exchange_player", duration_exchange_player);
00087   XmlReader::ReadUint(xml, "duration_before_death_mode", duration_before_death_mode);
00088   XmlReader::ReadUint(xml, "damage_per_turn_during_death_mode", damage_per_turn_during_death_mode);
00089   XmlReader::ReadUint(xml, "max_teams", max_teams);
00090   XmlReader::ReadUint(xml, "max_characters", max_characters);
00091   XmlReader::ReadDouble(xml, "gravity", gravity);
00092   XmlReader::ReadDouble(xml, "safe_fall", safe_fall);
00093   XmlReader::ReadDouble(xml, "damage_per_fall_unit", damage_per_fall_unit);
00094 
00095   // Character options
00096   xmlpp::Element *character_xml = XmlReader::GetMarker(xml, "character");
00097   if (character_xml != NULL)
00098   {
00099     xmlpp::Element *item = XmlReader::GetMarker(character_xml, "energy");
00100     if (item != NULL) {
00101       XmlReader::ReadUintAttr(item, "initial", character.init_energy);
00102       XmlReader::ReadUintAttr(item, "maximum", character.max_energy);
00103       if (character.init_energy==0) character.init_energy = 1;
00104       if (character.max_energy==0) character.max_energy = 1;
00105     }
00106     XmlReader::ReadUint(character_xml, "mass", character.mass);
00107     XmlReader::ReadDouble(character_xml, "air_resist_factor", character.air_resist_factor);
00108     item = XmlReader::GetMarker(character_xml, "jump");
00109     if (item != NULL) {
00110       int angle_deg;
00111       XmlReader::ReadUintAttr(item, "strength", character.jump_strength);
00112       XmlReader::ReadIntAttr(item, "angle", angle_deg);
00113       character.jump_angle = static_cast<double>(angle_deg) * M_PI / 180;
00114     }
00115 
00116     item = XmlReader::GetMarker(character_xml, "super_jump");
00117     if (item != NULL) {
00118       int angle_deg;
00119       XmlReader::ReadUintAttr(item, "strength", character.super_jump_strength);
00120       XmlReader::ReadIntAttr(item, "angle", angle_deg);
00121       character.super_jump_angle = static_cast<double>(angle_deg) * M_PI / 180;
00122     }
00123     item = XmlReader::GetMarker(character_xml, "back_jump");
00124     if (item != NULL) {
00125       int angle_deg;
00126       XmlReader::ReadUintAttr(item, "strength", character.back_jump_strength);
00127       XmlReader::ReadIntAttr(item, "angle", angle_deg);
00128       character.back_jump_angle = static_cast<double>(angle_deg) * M_PI / 180;
00129     }
00130     xmlpp::Element *explosion = XmlReader::GetMarker(character_xml, "death_explosion");
00131     if (explosion != NULL)
00132       death_explosion_cfg.LoadXml(explosion);
00133   }
00134 
00135   // Barrel explosion
00136   xmlpp::Element *barrel_xml = XmlReader::GetMarker(xml, "barrel");
00137   if(barrel_xml != NULL) {
00138     xmlpp::Element *barrel_explosion = XmlReader::GetMarker(barrel_xml, "explosion");
00139     if (barrel_explosion != NULL)
00140       barrel_explosion_cfg.LoadXml(barrel_explosion);
00141   }
00142 
00143   // Bonus box explosion
00144   xmlpp::Element *bonus_box_xml = XmlReader::GetMarker(xml, "bonus_box");
00145   if(bonus_box_xml != NULL) {
00146     xmlpp::Element *bonus_box_explosion = XmlReader::GetMarker(bonus_box_xml, "explosion");
00147     if (bonus_box_explosion != NULL)
00148       bonus_box_explosion_cfg.LoadXml(bonus_box_explosion);
00149   }
00150 
00151   //=== Weapons ===
00152   xmlpp::Element *armes = XmlReader::GetMarker(xml, "weapons");
00153   if (armes != NULL)
00154   {
00155     std::list<Weapon*> l_weapons_list = Config::GetInstance()->GetWeaponsList()->GetList() ;
00156     std::list<Weapon*>::iterator
00157       itw = l_weapons_list.begin(),
00158       end = l_weapons_list.end();
00159 
00160     for (; itw != end ; ++itw) (*itw)->LoadXml(armes);
00161   }
00162 
00163   return true;
00164 }
00165 
00166 bool GameMode::Load(const std::string &mode)
00167 {
00168   if (mode == m_current) return true;
00169   m_current = mode;
00170 
00171   std::string fullname;
00172   try
00173   {
00174     XmlReader doc;
00175     std::string filename =
00176       PATH_SEPARATOR
00177       + std::string("game_mode")
00178       + std::string(PATH_SEPARATOR)
00179       + mode
00180       + std::string(".xml");
00181 
00182     Config * config = Config::GetInstance();
00183     fullname = config->GetPersonalDir() + filename;
00184 
00185     if(!IsFileExist(fullname))
00186       fullname = config->GetDataDir() + filename;
00187     if(!doc.Load(fullname))
00188       return false;
00189     if(!LoadXml(doc.GetRoot()))
00190       return false;
00191   }
00192   catch (const xmlpp::exception &e)
00193   {
00194     std::cerr << Format(_("Error while loading game mode %s (file %s):"),
00195                         mode.c_str(), fullname.c_str())
00196                           << std::endl << e.what() << std::endl;
00197     return false;
00198   }
00199   return true;
00200 }
00201 
00202 bool GameMode::AllowCharacterSelection() const
00203 {
00204   switch (allow_character_selection)
00205   {
00206   case GameMode::ALWAYS: break;
00207 
00208   case GameMode::BEFORE_FIRST_ACTION:
00209   case GameMode::BEFORE_FIRST_ACTION_AND_END_TURN:
00210           return (GameLoop::GetInstance()->ReadState() == GameLoop::PLAYING) && !GameLoop::GetInstance()->character_already_chosen;
00211 
00212   case GameMode::CHANGE_ON_END_TURN:
00213   case GameMode::NEVER:
00214           return false;
00215   }
00216 
00217   return true;
00218 }
00219 

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