00001 00005 #ifndef _SG_PERSPARAM_HXX 00006 #define _SG_PERSPARAM_HXX 1 00007 00008 #include <simgear/math/sg_random.h> 00009 00010 00011 template <class T> 00012 class SGPersonalityParameter { 00013 public: 00014 SGPersonalityParameter(const SGPropertyNode *props, const char *name, T defval ) 00015 : _var( defval ), _min( defval ), _max( defval ) { 00016 const SGPropertyNode* node = props->getNode( name ); 00017 if ( node != 0 ) { 00018 const SGPropertyNode* rand_n = node->getNode( "random" ); 00019 if ( rand_n != 0 ) { 00020 _min = getNodeValue( rand_n, "min", (T)0 ); 00021 _max = getNodeValue( rand_n, "max", (T)1 ); 00022 shuffle(); 00023 } else { 00024 _var = _min = _max = getNodeValue( props, name, defval ); 00025 } 00026 } 00027 } 00028 SGPersonalityParameter<T> &operator=( T v ) { _var = v; return *this; } 00029 SGPersonalityParameter<T> &operator+=( T v ) { _var += v; return *this; } 00030 SGPersonalityParameter<T> &operator-=( T v ) { _var -= v; return *this; } 00031 T shuffle() { return ( _var = _min + sg_random() * ( _max - _min ) ); } 00032 T value() const { return _var; } 00033 T getNodeValue(const SGPropertyNode *props, const char *name, T defval ) const; 00034 operator T() const { return _var; } 00035 00036 private: 00037 T _var; 00038 T _min; 00039 T _max; 00040 }; 00041 00042 template <> double 00043 SGPersonalityParameter<double>::getNodeValue( const SGPropertyNode *props, 00044 const char *name, 00045 double defval ) const; 00046 00047 #endif // _SG_PERSPARAM_HXX 00048