Code Search for Developers
 
 
  

OptionsScorched.cpp from Scorched 3D at Krugle


Show OptionsScorched.cpp syntax highlighted

////////////////////////////////////////////////////////////////////////////////
//    Scorched3D (c) 2000-2003
//
//    This file is part of Scorched3D.
//
//    Scorched3D is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    Scorched3D is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Scorched3D; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

#include <common/OptionsScorched.h>
#include <common/Logger.h>
#include <engine/ScorchedContext.h>
#include <landscapedef/LandscapeTex.h>
#include <landscapedef/LandscapeDefn.h>
#include <landscapedef/LandscapeDefinitions.h>
#include <landscapedef/LandscapeOptions.h>
#include <landscapedef/LandscapeInclude.h>

OptionsScorched::OptionsScorched()
{
}

OptionsScorched::~OptionsScorched()
{
}

void OptionsScorched::updateLevelOptions(ScorchedContext &context, LandscapeDefinition &defn)
{
	// Get the current level data
	LandscapeTex *ltex = context.landscapes->getTex(defn.getTex());
	LandscapeDefn *ldefn = context.landscapes->getDefn(defn.getDefn());

	// Get all of the options specified in the current level
	std::map<std::string, OptionEntry *> values;
	updateLevelOptions(ltex->texDefn.includes, values);
	updateLevelOptions(ldefn->texDefn.includes, values);

	// Iterate over the level and current options
	std::list<OptionEntry *> &levelOptions = getLevelOptions().getOptions();
	std::list<OptionEntry *> &mainoptions = getMainOptions().getOptions();
	std::list<OptionEntry *>::iterator levelitor;
	std::list<OptionEntry *>::iterator mainitor;
	for (levelitor = levelOptions.begin(), mainitor = mainoptions.begin();
		levelitor != levelOptions.end() && mainitor != mainoptions.end();
		levelitor++, mainitor++)
	{
		OptionEntry *mainEntry = (*mainitor);
		OptionEntry *levelEntry = (*levelitor);

		// Get the current settings value that is in use
		OptionEntry *currentEntry = mainEntry;
		if (levelEntry->isChangedValue()) currentEntry = levelEntry;
		std::string oldValue = currentEntry->getValueAsString();

		// Reset the level entry
		levelEntry->setNotChanged();

		// If this level entry has changed set its new value
		std::map<std::string, OptionEntry *>::iterator findItor = 
			values.find(mainEntry->getName());
		if (findItor != values.end())
		{
			levelEntry->setValueFromString((*findItor).second->getValueAsString());
		}

		// Find out the new settings value that is in use
		currentEntry = mainEntry;
		if (levelEntry->isChangedValue()) currentEntry = levelEntry;
		std::string newValue = currentEntry->getValueAsString();

		// Log if the value has changed
		if (0 != strcmp(newValue.c_str(), oldValue.c_str()))
		{
			Logger::log(formatString("Level option %s has been changed from %s to %s",
				mainEntry->getName(),
				oldValue.c_str(), newValue.c_str()));
		}
	}
}

void OptionsScorched::updateLevelOptions(std::vector<LandscapeInclude *> &options,
	std::map<std::string, OptionEntry *> &values)
{
	// For each include
	std::vector<LandscapeInclude *>::iterator itor;
	for (itor = options.begin();
		itor != options.end();
		itor++)
	{
		LandscapeInclude *option = (*itor);

		// For each set of options
		std::vector<LandscapeOptionsType *>::iterator typeItor;
		for (typeItor = option->options.begin();
			typeItor != option->options.end();
			typeItor ++)
		{
			LandscapeOptionsType *optionType = (*typeItor);

			// For each option
			std::list<OptionEntry *>::iterator srcitor;
			for (srcitor = optionType->options.getOptions().begin();
				srcitor != optionType->options.getOptions().end();
				srcitor++)
			{
				OptionEntry *srcEntry = (*srcitor);
				if (srcEntry->isChangedValue())
				{
					values[srcEntry->getName()] = srcEntry;
				}
			}
		}
	}
}

void OptionsScorched::updateChangeSet()
{
	NetBufferDefault::defaultBuffer.reset();
	mainOptions_.writeToBuffer(NetBufferDefault::defaultBuffer, true, true);
	NetBufferReader reader(NetBufferDefault::defaultBuffer);
	changedOptions_.readFromBuffer(reader, true, true);
}

bool OptionsScorched::commitChanges()
{
	bool different = false;

	// Compare buffers
	std::list<OptionEntry *> &options = mainOptions_.getOptions();
	std::list<OptionEntry *> &otheroptions = changedOptions_.getOptions();
	std::list<OptionEntry *>::iterator itor;
	std::list<OptionEntry *>::iterator otheritor;
	for (itor=options.begin(), otheritor=otheroptions.begin();
		itor!=options.end() && otheritor!=otheroptions.end();
		itor++, otheritor++)
	{
		OptionEntry *entry = *itor;
		OptionEntry *otherentry = *otheritor;

		DIALOG_ASSERT(0 == strcmp(entry->getName(), otherentry->getName()));

		std::string str = entry->getValueAsString();
		std::string otherstr = otherentry->getValueAsString();
		if (str != otherstr)
		{
			if (!(entry->getData() & OptionEntry::DataProtected) &&
				!(otherentry->getData() & OptionEntry::DataProtected))
			{
				if (strlen(str.c_str()) < 20 && strlen(otherstr.c_str()) < 20)
				{
					Logger::log(formatString("Option %s has been changed from %s to %s",
						entry->getName(), str.c_str(), otherstr.c_str()));
				}
				else
				{
					Logger::log(formatString("Option %s has been changed.",
						entry->getName()));
				}
			}

			different = true;
			entry->setValueFromString(otherentry->getValueAsString());
		}
	}

	return different;
}




See more files for this project here

Scorched 3D

Scorched3D is a 3D remake of the popular 2D artillery game Scorched Earth.\r\nScorched3D can be played against the computer, other players and remotely across the internet or LAN.

Project homepage: http://sourceforge.net/projects/scorched3d
Programming language(s): C,C++,XML
License: gpl2

  ARGParser.cpp
  ARGParser.h
  ChannelManager.cpp
  ChannelManager.h
  ChannelText.cpp
  ChannelText.h
  ChannelTextParser.cpp
  ChannelTextParser.h
  Clock.cpp
  Clock.h
  Counter.cpp
  Counter.h
  Defines.cpp
  Defines.h
  DefinesAssert.cpp
  DefinesAssert.h
  DefinesFile.cpp
  DefinesFile.h
  DefinesMath.cpp
  DefinesMath.h
  DefinesScorched.cpp
  DefinesScorched.h
  DefinesString.cpp
  DefinesString.h
  FileLines.cpp
  FileLines.h
  FileList.cpp
  FileList.h
  FileLogger.cpp
  FileLogger.h
  ImageID.cpp
  ImageID.h
  KeyStateTranslate.h
  KeyTranslate.h
  KeyTranslateWx.h
  Keyboard.cpp
  Keyboard.h
  KeyboardHistory.h
  KeyboardKey.cpp
  KeyboardKey.h
  Line.cpp
  Line.h
  Logger.cpp
  Logger.h
  LoggerI.cpp
  LoggerI.h
  ModelID.cpp
  ModelID.h
  NumberParser.cpp
  NumberParser.h
  OptionEntry.cpp
  OptionEntry.h
  OptionsGame.cpp
  OptionsGame.h
  OptionsMasterListServer.cpp
  OptionsMasterListServer.h
  OptionsParameters.cpp
  OptionsParameters.h
  OptionsScorched.cpp
  OptionsScorched.h
  OptionsTransient.cpp
  OptionsTransient.h
  ProgressCounter.cpp
  ProgressCounter.h
  RandomGenerator.cpp
  RandomGenerator.h
  SplineCurve.cpp
  SplineCurve.h
  SplinePath.cpp
  SplinePath.h
  StatsLogger.cpp
  StatsLogger.h
  StatsLoggerDatabase.cpp
  StatsLoggerDatabase.h
  StatsLoggerFile.cpp
  StatsLoggerFile.h
  StatsLoggerMySQL.cpp
  StatsLoggerMySQL.h
  StatsLoggerPGSQL.cpp
  StatsLoggerPGSQL.h
  StdVectorPtr.h
  ToolTip.cpp
  ToolTip.h
  Triangle.cpp
  Triangle.h
  Vector.cpp
  Vector.h
  Vector4.cpp
  Vector4.h
  VectorLib.cpp
  VectorLib.h
  main.h
  sha2.cpp
  sha2.h
  snprintf.c