/* $Id: mapstuffset.cpp,v 1.9 2005/06/28 13:55:20 chfreund Exp $ */ /**********************************************************/ #include "mapstuffset.hpp" #include "global.hpp" #include "spriteset.cpp" #include "spritesequence.cpp" /**********************************************************/ // setting attributes #define SSSATTR (Setting::COMMA_SEPARATED | \ Setting::EXPLICIT_ASSIGNMENT | \ Setting::CONFIG_FILE_ONLY) #define SSSATTRM (SSSATTR | Setting::MANDATORY) #define SETTING_STRING(ID) MapStuffSet::m_SettingDef[ID].m_IDString /********************************************************** * constant static members **********************************************************/ const SettingDef MapStuffSet:: m_SettingDef[MapStuffSet::NUM_SETTING_DEFS+1] = { SettingDef("odds", NULL, Setting::INT, 1, -1, SSSATTR ), SettingDef("material", NULL, Setting::STRING, 1, 1, SSSATTRM), SettingDef() }; const char* const MapStuffSet::m_SectionName = "MapStuffSet"; const char* const MapStuffSet::m_MaterialString[NUM_MATERIALS+1] = { "DIGGABLE", "UNDIGGABLE", "INDESTRUCTIBLE", NULL }; /********************************************************** * Member functions of MapStuffSet **********************************************************/ MapStuffSet::MapStuffSet( const int randomseed ) : m_Random(randomseed) { } /**********************************************************/ MapStuffSet::~MapStuffSet() { reset(); } /**********************************************************/ const Sprite* MapStuffSet::getRandomItem() { int seq = 0, // sequence index fra = 0; // frame index DBG(1) { if( !CHECK( getSize() > 0, "MapStuffSet::getRandomItem: no map stuff " "present\n" ) ) return NULL; } // get a random number in the range of sequences' indices // If there are no sequences, we have left the function before. seq = m_Random.getWeightedUint32(); // select a frame in the sequence DBG(1) ASSERT( m_Data[seq]->getSize() > 0, "MapStuffSet::getRandomItem: empty sequence " "[%d] causes operation modulo 0\n", seq ); fra = m_Random.getUint32() % m_Data[seq]->getSize(); return (*m_Data[seq])[fra]; } /**********************************************************/ int MapStuffSet::load( const char* const path, const char* const filename ) { const char fn[] = "MapStuffSet::load"; DBG(2) INFO( "%s: loading map stuff set from \"%s\"\n", fn, filename ); // reset the class reset(); // load the graphics using the loading function of the base // class SpriteSet. Since SpriteSet only considers parts of // the configuration file in the specific sections, the // additional part for class MapStuffSet is simply ignored. if( CHECK(SpriteSet::load(path, filename) > 0, "%s: could not load graphics for map stuff set from " "\"%s/%s\"\n", fn, path, filename) // load the settings specific for mapstuff. Here only the // specific section for MapStuffSet is regarded. && CHECK(loadMapStuffSettings( (char*)((String(path)+='/') +=String(filename)) ), "%s: could not load settings for map stuff set\n", fn) ) { return -1; } return getSize(); } /**********************************************************/ void MapStuffSet::reset() { SpriteSet::reset(); } /**********************************************************/ bool MapStuffSet:: loadMapStuffSettings( const char* const fullpath ) { SettingDataBase settings( true ); settings.restrictToSection( m_SectionName ); return // load settings settings.readSettings( MapStuffSet::m_SettingDef, fullpath, true, NULL ) // check settings && settings.finalCheck( m_SettingDef ) // process settings && processSettings( settings ); } /**********************************************************/ bool MapStuffSet:: processSettings( const SettingDataBase& settings ) { const char fn[] = "MapStuffSet::processSettings"; Setting* setting = NULL; // process the weights for choosing probabilities setting = settings.getSetting( SETTING_STRING(ODDS_SETTING_DEFS) ); if( setting != NULL ) { if( CHECK( setting->getNumParameters() == getSize(), "%s: number of specified probability weights (%d) and " "number of loaded graphic sets (%d) do not match -> " "using uniform distribution\n", fn, setting->getNumParameters(), getSize() ) ) { // set the weights m_Random.setWeights( getSize(), setting->getIntPtr() ); } } else { // just fix the range of the random numbers m_Random.setWeights( getSize() ); } // get the material enumeration constant const char* const material = SETTING_STRING( MATERIAL_SETTING_DEFS ); m_Material = settings.mapStringToIndex( material, (const char**)m_MaterialString, 0, true ); if( !CHECK(m_Material >= 0, "%s: could not map the material string \"%s\" to one of " "the known materials -> fix the configuration file\n", fn, settings.getSetting(material)->getString()) ) { return false; } return true; } /**********************************************************/