OptionsScorched.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D 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 Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/OptionsScorched.h>
00022 #include <common/Logger.h>
00023 #include <engine/ScorchedContext.h>
00024 #include <landscapedef/LandscapeTex.h>
00025 #include <landscapedef/LandscapeDefn.h>
00026 #include <landscapedef/LandscapeDefinitions.h>
00027 #include <landscapedef/LandscapeOptions.h>
00028 #include <landscapedef/LandscapeInclude.h>
00029 #include <net/NetBufferPool.h>
00030 
00031 OptionsScorched::OptionsScorched()
00032 {
00033 }
00034 
00035 OptionsScorched::~OptionsScorched()
00036 {
00037 }
00038 
00039 void OptionsScorched::updateLevelOptions(ScorchedContext &context, LandscapeDefinition &defn)
00040 {
00041         // Get the current level data
00042         LandscapeTex *ltex = context.getLandscapes().getTex(defn.getTex());
00043         LandscapeDefn *ldefn = context.getLandscapes().getDefn(defn.getDefn());
00044 
00045         // Get all of the options specified in the current level
00046         std::map<std::string, OptionEntry *> values;
00047         updateLevelOptions(ltex->texDefn.includes, values);
00048         updateLevelOptions(ldefn->texDefn.includes, values);
00049 
00050         // Iterate over the level and current options
00051         std::list<OptionEntry *> &levelOptions = getLevelOptions().getOptions();
00052         std::list<OptionEntry *> &mainoptions = getMainOptions().getOptions();
00053         std::list<OptionEntry *>::iterator levelitor;
00054         std::list<OptionEntry *>::iterator mainitor;
00055         for (levelitor = levelOptions.begin(), mainitor = mainoptions.begin();
00056                 levelitor != levelOptions.end() && mainitor != mainoptions.end();
00057                 levelitor++, mainitor++)
00058         {
00059                 OptionEntry *mainEntry = (*mainitor);
00060                 OptionEntry *levelEntry = (*levelitor);
00061 
00062                 // Get the current settings value that is in use
00063                 OptionEntry *currentEntry = mainEntry;
00064                 if (levelEntry->isChangedValue()) currentEntry = levelEntry;
00065                 std::string oldValue = currentEntry->getValueAsString();
00066 
00067                 // Reset the level entry
00068                 levelEntry->setNotChanged();
00069 
00070                 // If this level entry has changed set its new value
00071                 std::map<std::string, OptionEntry *>::iterator findItor = 
00072                         values.find(mainEntry->getName());
00073                 if (findItor != values.end())
00074                 {
00075                         levelEntry->setValueFromString((*findItor).second->getValueAsString());
00076                 }
00077 
00078                 // Find out the new settings value that is in use
00079                 currentEntry = mainEntry;
00080                 if (levelEntry->isChangedValue()) currentEntry = levelEntry;
00081                 std::string newValue = currentEntry->getValueAsString();
00082 
00083                 // Log if the value has changed
00084                 if (0 != strcmp(newValue.c_str(), oldValue.c_str()))
00085                 {
00086                         Logger::log(S3D::formatStringBuffer("Level option %s has been changed from %s to %s",
00087                                 mainEntry->getName(),
00088                                 oldValue.c_str(), newValue.c_str()));
00089                 }
00090         }
00091 }
00092 
00093 void OptionsScorched::updateLevelOptions(std::vector<LandscapeInclude *> &options,
00094         std::map<std::string, OptionEntry *> &values)
00095 {
00096         // For each include
00097         std::vector<LandscapeInclude *>::iterator itor;
00098         for (itor = options.begin();
00099                 itor != options.end();
00100                 itor++)
00101         {
00102                 LandscapeInclude *option = (*itor);
00103 
00104                 // For each set of options
00105                 std::vector<LandscapeOptionsType *>::iterator typeItor;
00106                 for (typeItor = option->options.begin();
00107                         typeItor != option->options.end();
00108                         typeItor ++)
00109                 {
00110                         LandscapeOptionsType *optionType = (*typeItor);
00111 
00112                         // For each option
00113                         std::list<OptionEntry *>::iterator srcitor;
00114                         for (srcitor = optionType->options.getOptions().begin();
00115                                 srcitor != optionType->options.getOptions().end();
00116                                 srcitor++)
00117                         {
00118                                 OptionEntry *srcEntry = (*srcitor);
00119                                 if (srcEntry->isChangedValue())
00120                                 {
00121                                         values[srcEntry->getName()] = srcEntry;
00122                                 }
00123                         }
00124                 }
00125         }
00126 }
00127 
00128 void OptionsScorched::updateChangeSet()
00129 {
00130         NetBuffer *defaultBuffer = NetBufferPool::instance()->getFromPool();
00131 
00132         defaultBuffer->reset();
00133         mainOptions_.writeToBuffer(*defaultBuffer, true, true);
00134         NetBufferReader reader(*defaultBuffer);
00135         changedOptions_.readFromBuffer(reader, true, true);
00136 
00137         NetBufferPool::instance()->addToPool(defaultBuffer);
00138 }
00139 
00140 bool OptionsScorched::commitChanges()
00141 {
00142         bool different = false;
00143 
00144         // Compare buffers
00145         std::list<OptionEntry *> &options = mainOptions_.getOptions();
00146         std::list<OptionEntry *> &otheroptions = changedOptions_.getOptions();
00147         std::list<OptionEntry *>::iterator itor;
00148         std::list<OptionEntry *>::iterator otheritor;
00149         for (itor=options.begin(), otheritor=otheroptions.begin();
00150                 itor!=options.end() && otheritor!=otheroptions.end();
00151                 itor++, otheritor++)
00152         {
00153                 OptionEntry *entry = *itor;
00154                 OptionEntry *otherentry = *otheritor;
00155 
00156                 DIALOG_ASSERT(0 == strcmp(entry->getName(), otherentry->getName()));
00157 
00158                 std::string str = entry->getValueAsString();
00159                 std::string otherstr = otherentry->getValueAsString();
00160                 if (str != otherstr)
00161                 {
00162                         if (!(entry->getData() & OptionEntry::DataProtected) &&
00163                                 !(otherentry->getData() & OptionEntry::DataProtected))
00164                         {
00165                                 if (strlen(str.c_str()) < 20 && strlen(otherstr.c_str()) < 20)
00166                                 {
00167                                         Logger::log(S3D::formatStringBuffer("Option %s has been changed from %s to %s",
00168                                                 entry->getName(), str.c_str(), otherstr.c_str()));
00169                                 }
00170                                 else
00171                                 {
00172                                         Logger::log(S3D::formatStringBuffer("Option %s has been changed.",
00173                                                 entry->getName()));
00174                                 }
00175                         }
00176 
00177                         different = true;
00178                         entry->setValueFromString(otherentry->getValueAsString());
00179                 }
00180         }
00181 
00182         return different;
00183 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3