00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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
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)
00099 {
00100
00101 fixed value = 0;
00102
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;
00141 }
00142
00143
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