NumberParser.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 
00022 // NumberParser.cpp: implementation of the NumberParser class.
00023 //
00024 //////////////////////////////////////////////////////////////////////
00025 
00026 #include <common/RandomGenerator.h>
00027 #include <engine/ActionController.h>
00028 #include <common/NumberParser.h>
00029 #include <string>
00030 #include <list>
00031 #include <sstream>
00032 
00033 //////////////////////////////////////////////////////////////////////
00034 // Construction/Destruction
00035 //////////////////////////////////////////////////////////////////////
00036 
00037 NumberParser::NumberParser()
00038 {
00039 
00040 }
00041 
00042 NumberParser::NumberParser(fixed value)
00043 {
00044         this->setExpression(value);
00045 }
00046 
00047 NumberParser::~NumberParser()
00048 {
00049 
00050 }
00051 
00052 
00053 bool NumberParser::getOperands()
00054 {
00055         operands_.clear();
00056         int count = 0;
00057         int nextPos = 0;
00058         std::string value;
00059         int pos = (int) expression_.find('(',0);
00060         if (pos == std::string::npos)
00061         {
00062                 //value = expression_.substr(pos + 1, nextPos - pos + 1);
00063                 operands_.push_back(fixed(expression_.c_str()));
00064                 return true;
00065         }
00066 
00067         pos += 1;
00068         while (pos < (int) expression_.length())
00069         {
00070                 nextPos = (int) expression_.find_first_of(",)", pos);
00071                 if (nextPos == std::string::npos)
00072                         nextPos = (int) expression_.length() -1;
00073                 value = expression_.substr(pos, nextPos - pos);
00074                 operands_.push_back(fixed(value.c_str()));
00075                 pos = nextPos + 1;
00076         }
00077         return true;
00078 }
00079 
00080 
00081 bool NumberParser::setExpression(const char *expression)
00082 {
00083         expression_ = expression;
00084         // test to ensure it's valid! TODO
00085         this->getOperands();
00086 
00087         return true;
00088 }
00089 
00090 bool NumberParser::setExpression(fixed value)
00091 {
00092         expression_ = value.asString();
00093         this->getOperands();
00094 
00095         return true;
00096 }
00097 
00098 fixed NumberParser::getValue(ScorchedContext &context) //RandomGenerator &generator)
00099 {
00100         // Examples: 10, RANGE(1,10)
00101         fixed value = 0;
00102         // Constant
00103         if (operands_.size() == 1)
00104                 return operands_.front();
00105 
00106         step_ = 0;
00107         std::list<fixed>::iterator itor;
00108         itor = operands_.begin();
00109                 RandomGenerator &random = context.getActionController().getRandom();
00110 
00111         if (expression_.find("RANGE",0) != std::string::npos)
00112         {
00113                 min_ = *itor;
00114                 max_ = *(++itor);
00115                 if (operands_.size() >= 3)
00116                         step_ = *(++itor);
00117 
00118                 if (step_ == 0)
00119                         value = random.getRandFixed() * (max_ - min_) + min_;
00120                 else
00121                                         value = fixed(((random.getRandFixed() * (max_ - min_) /  step_ ).asInt()) * step_.asInt()) + min_;
00122                 return value;
00123         }
00124 
00125         else if (expression_.find("DISTRIBUTION",0) != std::string::npos)
00126         {
00127                 if (operands_.size() < 2)
00128                         S3D::dialogExit("NumberParser.cpp",
00129                                 S3D::formatStringBuffer("Invalid DISTRIBUTION expression: \"%s\"",
00130                                 expression_.c_str()));
00131                                 int operandNo = (random.getRandFixed() * fixed((unsigned int) operands_.size())).asInt();
00132                 for (int i = 0; i <= operandNo; i++) itor++;
00133                 value = *itor;
00134                 return value;
00135         }
00136 
00137         S3D::dialogExit("NumberParser.cpp",
00138                 S3D::formatStringBuffer("Invalid fixed expression: \"%s\"",
00139                 expression_.c_str()));
00140         return false;  // VC++ complains
00141 }
00142 
00143 // Allow for default values to be passed along for optional attributes
00144 fixed NumberParser::getValue(ScorchedContext &context, fixed defaultValue)
00145 {
00146         if (expression_.size() == 0)
00147                 this->setExpression(defaultValue);
00148 
00149         return getValue(context);
00150 }
00151 
00152 unsigned int NumberParser::getUInt(ScorchedContext &context)
00153 {
00154         fixed result = getValue(context);
00155         return result.asInt();
00156 }
00157 
00158 int NumberParser::getInt(ScorchedContext &context)
00159 {
00160         fixed result = getValue(context);
00161         return result.asInt();
00162 }
00163 

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