Back to index

supertuxkart  0.5+dfsg1
kart_properties.cpp
Go to the documentation of this file.
00001 //  $Id: kart_properties.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 SuperTuxKart-Team
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program 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 this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <iostream>
00021 #include <stdexcept>
00022 #include <plib/ssg.h>
00023 #include "material_manager.hpp"
00024 #include "lisp/parser.hpp"
00025 #include "lisp/lisp.hpp"
00026 #include "loader.hpp"
00027 #include "file_manager.hpp"
00028 #include "string_utils.hpp"
00029 #include "kart_properties.hpp"
00030 #include "stk_config.hpp"
00031 #include "translation.hpp"
00032 #include "ssg_help.hpp"
00033 #if defined(WIN32) && !defined(__CYGWIN__)
00034 #  define snprintf _snprintf
00035 #endif
00036 
00037 // This constructor would be a bit more useful, nicer, if we could call
00038 // init_defaults() and load from here. Unfortunately, this object is used
00039 // as a base class for STKConfig, which has to overwrite
00040 // init_defaults() and getAllData(). But during the call of this constructor,
00041 // the STKConfig object does not (yet) exist, so the overwriting
00042 // functions do NOT get called, only the virtual functions here would be
00043 // called. Therefore, a two step initialisation is necessary: the constructor
00044 // doing not much, but then in load the overwriting functions can be used.
00045 KartProperties::KartProperties() : m_icon_material(0), m_model(0)
00046 {}   // KartProperties
00047 
00048 //-----------------------------------------------------------------------------
00049 void KartProperties::load(const std::string filename, const std::string node,
00050                           bool dont_load_models, bool dont_load_materials)
00051 {
00052 
00053     init_defaults();
00054 
00055     const lisp::Lisp* root = 0;
00056     m_ident = StringUtils::basename(StringUtils::without_extension(filename));
00057 
00058     try
00059     {
00060         lisp::Parser parser;
00061         root = parser.parse(filename);
00062 
00063         const lisp::Lisp* const LISP = root->getLisp(node);
00064         if(!LISP)
00065         {
00066             char msg[MAX_ERROR_MESSAGE_LENGTH];
00067             snprintf(msg, sizeof(msg), "No '%s' node found.", node.c_str());
00068             throw std::runtime_error(msg);
00069         }
00070         getAllData(LISP);
00071     }
00072     catch(std::exception& err)
00073     {
00074         fprintf(stderr, "Error while parsing KartProperties '%s':\n", 
00075                 filename.c_str());
00076         fprintf(stderr, err.what());
00077         fprintf(stderr, "\n");
00078     }
00079     delete root;
00080 
00081     if(!dont_load_materials)
00082     {
00083         // Load material
00084         std::string materials_file = file_manager->getKartFile("materials.dat",getIdent());
00085         file_manager->pushModelSearchPath(file_manager->getKartFile("", getIdent()));
00086         file_manager->pushTextureSearchPath(file_manager->getKartFile("", getIdent()));
00087 
00088         // addShared makes sure that these textures/material infos stay in memory
00089         material_manager->addSharedMaterial(materials_file);
00090         m_icon_material = material_manager->getMaterial(m_icon_file);
00091     }
00092     // Load model, except when called as part of --list-karts
00093     if(m_model_file.length()>0 && !dont_load_models)
00094     {
00095         m_model = loader->load(m_model_file, CB_KART, false);
00096         if(!m_model)
00097         {
00098             fprintf(stderr, "Can't find kart model '%s'.\n",m_model_file.c_str());
00099             file_manager->popTextureSearchPath();
00100             file_manager->popModelSearchPath();
00101             return;
00102         }
00103         ssgStripify(m_model);
00104         float x_min, x_max, y_min, y_max, z_min, z_max;
00105         MinMax(m_model, &x_min, &x_max, &y_min, &y_max, &z_min, &z_max);
00106         if(getName()=="Hexley" || getName()=="Wilber")
00107         {
00108             // These kart models are too small, so we get problems with stability. 
00109             // Till we find either better (bigger) models or improve their physics 
00110             // parameters to become playable, we just adjust the size of their 
00111             // physical models to be the same as the tuxkart model
00112             x_min=-0.473799f;
00113             x_max= 0.486361f;
00114             y_min=-0.772244f;
00115             y_max= 0.739075f;
00116             z_min= 0.002806f;
00117             z_max= 0.701095f;
00118         }
00119         m_kart_width  = x_max - x_min;
00120         m_kart_length = y_max - y_min;
00121         m_kart_height = z_max - z_min;
00122         if(m_kart_length<1.2) m_kart_length=1.5f;
00123         m_model->ref();
00124     }  // if
00125     if(!dont_load_materials)
00126     {
00127         file_manager->popTextureSearchPath();
00128         file_manager->popModelSearchPath();
00129     }
00130 
00131 }   // load
00132 
00133 //-----------------------------------------------------------------------------
00134 KartProperties::~KartProperties()
00135 {
00136     ssgDeRefDelete(m_model);
00137 }   // ~KartProperties
00138 
00139 //-----------------------------------------------------------------------------
00140 void KartProperties::getAllData(const lisp::Lisp* lisp)
00141 {
00142     lisp->get("name",                    m_name);
00143     lisp->get("model-file",              m_model_file);
00144     lisp->get("icon-file",               m_icon_file);
00145     lisp->get("shadow-file",             m_shadow_file);
00146     lisp->get("red",                     m_color[0]);
00147     lisp->get("green",                   m_color[1]);
00148     lisp->get("blue",                    m_color[2]);
00149 
00150     lisp->get("wheel-base",              m_wheel_base);
00151     lisp->get("heightCOG",               m_height_cog);
00152     lisp->get("engine-power",            m_engine_power);
00153     lisp->get("time-full-steer",         m_time_full_steer);
00154     lisp->get("brake-factor",            m_brake_factor);
00155     lisp->get("mass",                    m_mass);
00156     lisp->get("max-steer-angle",         m_max_steer_angle);
00157     lisp->get("wheelie-max-speed-ratio", m_wheelie_max_speed_ratio );
00158     lisp->get("wheelie-max-pitch",       m_wheelie_max_pitch       );
00159     lisp->get("wheelie-pitch-rate",      m_wheelie_pitch_rate      );
00160     lisp->get("wheelie-restore-rate",    m_wheelie_restore_rate    );
00161     lisp->get("wheelie-speed-boost",     m_wheelie_speed_boost     );
00162     lisp->get("wheelie-lean-recovery",   m_wheelie_lean_recovery   );
00163     lisp->get("wheelie-step",            m_wheelie_step            );
00164     lisp->get("wheelie-balance-recovery",m_wheelie_balance_recovery);
00165     lisp->get("wheelie-power-boost",     m_wheelie_power_boost     );
00166 
00167     //bullet physics data
00168     lisp->get("suspension-stiffness",      m_suspension_stiffness     );
00169     lisp->get("wheel-damping-relaxation",  m_wheel_damping_relaxation );
00170     lisp->get("wheel-damping-compression", m_wheel_damping_compression);
00171     lisp->get("friction-slip",             m_friction_slip            );
00172     lisp->get("roll-influence",            m_roll_influence           );
00173     lisp->get("wheel-radius",              m_wheel_radius             );
00174     lisp->get("wheel-width",               m_wheel_width              );
00175     lisp->get("chassis-linear-damping",    m_chassis_linear_damping   );
00176     lisp->get("chassis-angular-damping",   m_chassis_angular_damping  );
00177     lisp->get("max-speed-reverse-ratio",   m_max_speed_reverse_ratio  );
00178     lisp->get("maximum-speed",             m_maximum_speed            );
00179     lisp->get("gravity-center-shift",      m_gravity_center_shift     );
00180     lisp->get("suspension-rest",           m_suspension_rest          );
00181     lisp->get("jump-velocity",             m_jump_velocity            );
00182     lisp->get("upright-tolerance",         m_upright_tolerance        );
00183     lisp->get("upright-max-force",         m_upright_max_force        );
00184     // getVector appends to existing vectors, so a new one must be used to load
00185     std::vector<float> temp;
00186     lisp->getVector("gear-switch-ratio",   temp);
00187     if(temp.size()>0) m_gear_switch_ratio = temp;
00188     temp.clear();
00189     lisp->getVector("gear-power-increase", temp);
00190     if(temp.size()>0) m_gear_power_increase = temp;
00191     
00192     // Camera
00193     lisp->get("camera-max-accel",             m_camera_max_accel);
00194     lisp->get("camera-max-brake",             m_camera_max_brake);
00195     lisp->get("camera-distance",              m_camera_distance );
00196 
00197 }   // getAllData
00198 
00199 //-----------------------------------------------------------------------------
00200 void KartProperties::init_defaults()
00201 {
00202 
00203     m_name          = "Tux";
00204     m_ident         = "tux";
00205     m_model_file    = "tuxkart.ac";
00206     m_icon_file     = "tuxicon.png";
00207     m_shadow_file   = "tuxkartshadow.png";
00208 
00209     m_color[0] = 1.0f; m_color[1] = 0.0f; m_color[2] = 0.0f;
00210 
00211     m_kart_width                = 1.0f;
00212     m_kart_length               = 1.5f;
00213     m_wheel_base                = stk_config->m_wheel_base;
00214     m_height_cog                = stk_config->m_height_cog;
00215     m_engine_power              = stk_config->m_engine_power;
00216     m_time_full_steer           = stk_config->m_time_full_steer;
00217     m_brake_factor              = stk_config->m_brake_factor;
00218     m_mass                      = stk_config->m_mass;
00219     m_max_steer_angle           = stk_config->m_max_steer_angle;
00220     m_wheelie_max_speed_ratio   = stk_config->m_wheelie_max_speed_ratio;
00221     m_wheelie_max_pitch         = stk_config->m_wheelie_max_pitch;
00222     m_wheelie_pitch_rate        = stk_config->m_wheelie_pitch_rate;
00223     m_wheelie_restore_rate      = stk_config->m_wheelie_restore_rate;
00224     m_wheelie_speed_boost       = stk_config->m_wheelie_speed_boost;
00225     m_wheelie_lean_recovery     = stk_config->m_wheelie_lean_recovery;
00226     m_wheelie_balance_recovery  = stk_config->m_wheelie_balance_recovery;
00227     m_wheelie_step              = stk_config->m_wheelie_step;
00228     m_wheelie_power_boost       = stk_config->m_wheelie_power_boost;
00229 
00230     //bullet physics data
00231     m_suspension_stiffness      = stk_config->m_suspension_stiffness;
00232     m_wheel_damping_relaxation  = stk_config->m_wheel_damping_relaxation;
00233     m_wheel_damping_compression = stk_config->m_wheel_damping_compression;
00234     m_friction_slip             = stk_config->m_friction_slip;
00235     m_roll_influence            = stk_config->m_roll_influence;
00236     m_wheel_radius              = stk_config->m_wheel_radius;
00237     m_wheel_width               = stk_config->m_wheel_width;
00238     m_chassis_linear_damping    = stk_config->m_chassis_linear_damping;
00239     m_chassis_angular_damping   = stk_config->m_chassis_angular_damping;
00240     m_maximum_speed             = stk_config->m_maximum_speed;
00241     m_max_speed_reverse_ratio   = stk_config->m_max_speed_reverse_ratio;
00242     m_gravity_center_shift      = stk_config->m_gravity_center_shift;
00243     m_suspension_rest           = stk_config->m_suspension_rest;
00244     m_jump_velocity             = stk_config->m_jump_velocity;
00245     m_gear_switch_ratio         = stk_config->m_gear_switch_ratio;
00246     m_gear_power_increase       = stk_config->m_gear_power_increase;
00247     m_upright_tolerance         = stk_config->getUprightTolerance();
00248     m_upright_max_force         = stk_config->getUprightMaxForce();
00249     m_camera_max_accel          = stk_config->getCameraMaxAccel();
00250     m_camera_max_brake          = stk_config->getCameraMaxBrake();
00251     m_camera_distance           = stk_config->getCameraDistance();
00252 }   // init_defaults
00253 
00254 /* EOF */