Attributes

[ LiB ]

As I mentioned in Chapter 7, "Designing the SimpleMUD," players and items will have groupings of attributes. In a simple MUD such as this, you could make an individual variable for each attribute within a player or item, but that gets to be unmanageable at a certain level. Whenever you add stuff, your code becomes more and more messy. You can find all the attribute- related stuff in the Attributes.h file.

Attribute Class

Instead of making an individual variable for each attribute within a player or an item, I've decided that it's easier to create an enumeration:

 enum Attribute {     STRENGTH        = 0,     HEALTH          = 1,     AGILITY         = 2,     MAXHITPOINTS    = 3,     ACCURACY        = 4,     DODGING         = 5,     STRIKEDAMAGE    = 6,     DAMAGEABSORB    = 7,     HPREGEN         = 8 }; const int NUMATTRIBUTES = 9; 

Those are all nine of the attributes of players and items, including the first three base attributes, as I showed you in Chapter 7. There's also an array of strings called ATTRIBUTESTRINGS , which contains the names of all the attributes; I don't show them here.

There are also these two functions:

 Attribute GetAttribute( string p_attr ); string GetAttributeString( Attribute p_attr ); 

I'm not going to bother showing you the code, because it's pretty boring; it only converts a string to an Attribute , and vice versa.

Attribute Sets

When creating the item and player classes, it would make sense to give them an array of attributes. However, it would make even more sense to create a custom class whose only purpose is to group a collection of attributes together. For this purpose, I've created the AttributeSet class, which will act like an array, and provide other basic features as well:

 class AttributeSet { public:     AttributeSet();     int& operator[]( int p_attr );     friend ostream& operator<<( ostream& p_stream, const AttributeSet& a );     friend istream& operator>>( istream& p_stream, AttributeSet& a ); protected:     int m_attributes[NUMATTRIBUTES]; }; 

Simply put, the constructor loops through all the attributes in the m_attributes array, and clears them to zero. You can use the operator[] to treat an attribute set just like an array:

 AttributeSet s; s[HEALTH] = 10; s[STRENGTH] = 9; 

It's the little things like that which make your code so much easier to read and understand.

You may have noticed that attribute sets can also be inserted into and extracted from streams using the standard operators << and >> . The main reason for this is that, for file storage, all the file formats I'm using for SimpleMUD are going to be plain text ASCII files. I'll touch on the reasons more when I show you the Item and Player classes. Here's the code for the first stream function:

 ostream& operator<<( ostream& p_stream, const AttributeSet& a ) {     for( int i = 0; i < NUMATTRIBUTES; i++ ) {         p_stream << "[" << GetAttributeString( (Attribute)i ) <<                     "] " << a.m_attributes[i] << "\n";     }     return p_stream; } 

The function loops through all the attributes within the set, and prints within square brackets first the name , and then the value. The output of this function looks something like this:

 [STRENGTH] 10 <snip> [HPREGEN] 5 

And so on. I didn't paste all nine attributes, just the first and last ones to save on space. Likewise, here's the stream extraction function:

 istream& operator>>( istream& p_stream, AttributeSet& a ) {     std::string temp;     for( int i = 0; i < NUMATTRIBUTES; i++ ) {         p_stream >> temp >> a.m_attributes[i];     }     return p_stream; } 

As you can see from the function, there is a temporary string named temp . This is used to "eat" the labels of each attribute. Whenever the line [STRENGTH] 10 is read in, the [STRENGTH] part is read into temp and discarded, and the value 10 is read into the current attribute. Because this function ignores line labels, the order of the attributes within a stream must remain in the same order as they were originally printed. The line labels are there primarily to help users of the system figure out the meanings of the values.

[ LiB ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net