From 2eb027b0f7a67706df68fe29e605c5be7fc1d5c9 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Wed, 11 Mar 2009 14:30:23 -0600 Subject: Remove some duplication in hair loading code --- src/being.cpp | 75 ++++++++++++++++++----------------------------------------- 1 file changed, 23 insertions(+), 52 deletions(-) (limited to 'src/being.cpp') diff --git a/src/being.cpp b/src/being.cpp index 4d3bcfa0..bf49e852 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -58,7 +58,9 @@ #define BEING_EFFECTS_FILE "effects.xml" #define HAIR_FILE "hair.xml" +int Being::mNumberOfHairColors = 1; int Being::mNumberOfHairstyles = 1; +std::vector Being::hairColors; std::vector Being::emotionSet; static const int X_SPEECH_OFFSET = 18; @@ -750,58 +752,41 @@ void Being::internalTriggerEffect(int effectId, bool sfx, bool gfx) } } - - - -static int hairStylesNr; -static int hairColorsNr; -static std::vector hairColors; - -static void initializeHair(); - -int Being::getHairStylesNr() +int Being::getHairStyleCount() { - initializeHair(); - return hairStylesNr; + return mNumberOfHairstyles; } -int Being::getHairColorsNr() +int Being::getHairColorCount() { - initializeHair(); - return hairColorsNr; + return mNumberOfHairColors; } std::string Being::getHairColor(int index) { - initializeHair(); - if (index < 0 || index >= hairColorsNr) + if (index < 0 || index >= mNumberOfHairColors) return "#000000"; return hairColors[index]; } -static bool hairInitialized = false; - -static void initializeHair() +void Being::initializeHair() { - if (hairInitialized) - return; - - // Hairstyles are encoded as negative numbers. Count how far negative we - // can go. - int hairstylesCtr = -1; - while (ItemDB::get(hairstylesCtr).getSprite(GENDER_MALE) != "error.xml") - --hairstylesCtr; - - hairStylesNr = -hairstylesCtr; // done. - if (hairStylesNr == 0) - hairStylesNr = 1; // No hair style -> no hair - - hairColorsNr = 0; + // Hairstyles are encoded as negative numbers. Count how far negative + // we can go. + int hairstyles = -1; + while (ItemDB::get(hairstyles).getSprite(GENDER_MALE) != "error.xml") + { + hairstyles--; + } + mNumberOfHairstyles = -hairstyles; XML::Document doc(HAIR_FILE); xmlNodePtr root = doc.rootNode(); + // Add an initial hair color + hairColors.resize(1, "#000000"); + if (!root || !xmlStrEqual(root->name, BAD_CAST "colors")) { logger->log("Error loading being hair configuration file"); @@ -814,22 +799,15 @@ static void initializeHair() std::string value = XML::getProperty(node, "value", ""); if (index >= 0 && !value.empty()) { - if (index >= hairColorsNr) { - hairColorsNr = index + 1; - hairColors.resize(hairColorsNr, "#000000"); + if (index >= mNumberOfHairColors) { + mNumberOfHairColors = index + 1; + hairColors.resize(mNumberOfHairColors, "#000000"); } hairColors[index] = value; } } } - } // done initializing - - if (hairColorsNr == 0) { // No colors -> black only - hairColorsNr = 1; - hairColors.resize(hairColorsNr, "#000000"); } - - hairInitialized = 1; } void Being::load() @@ -844,14 +822,7 @@ void Being::load() emotionSet.push_back(AnimatedSprite::load(file, variant)); } - // Hairstyles are encoded as negative numbers. Count how far negative - // we can go. - int hairstyles = 1; - while (ItemDB::get(-hairstyles).getSprite(GENDER_MALE) != "error.xml") - { - hairstyles++; - } - mNumberOfHairstyles = hairstyles; + initializeHair(); } void Being::cleanup() -- cgit v1.2.3-70-g09d2