00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "nel/logic/logic_state_machine.h"
00028 #include "nel/logic/logic_variable.h"
00029
00030 using namespace std;
00031 using namespace NLMISC;
00032
00033
00034 namespace NLLOGIC
00035 {
00036
00037
00038
00039
00040
00041
00042 CLogicVariable::CLogicVariable()
00043 {
00044 _Value = 0;
00045 _Name = "unamed_var";
00046 _Verbose = false;
00047
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 void CLogicVariable::setValue( sint64 value )
00057 {
00058 _Value = value;
00059
00060 if( _Verbose )
00061 {
00062 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
00063 }
00064
00065 }
00066
00067
00068
00069
00070
00071
00072
00073 void CLogicVariable::applyModification( string op, sint64 value )
00074 {
00075 if( op == "SET" || op == "set" )
00076 {
00077 _Value = value;
00078 }
00079 else
00080 if( op == "ADD" || op == "add" )
00081 {
00082 _Value += value;
00083 }
00084 else
00085 if( op == "SUB" || op == "sub" )
00086 {
00087 _Value -= value;
00088 }
00089 else
00090 if( op == "MUL" || op == "mul")
00091 {
00092 _Value *= value;
00093 }
00094 else
00095 if( op == "DIV" || op == "div")
00096 {
00097 if( value != 0 ) _Value /= value;
00098 }
00099 else
00100 {
00101 nlwarning("(LGCS)<CLogicVariable::applyModification> The operator \"%s\" is unknown",op.c_str());
00102 return;
00103 }
00104
00105 if( _Verbose )
00106 {
00107 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
00108 }
00109
00110 }
00111
00112
00113
00114
00115
00116
00117 void CLogicVariable::processLogic()
00118 {
00119
00120
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 void CLogicVariable::write (xmlNodePtr node) const
00140 {
00141 xmlNodePtr elmPtr = xmlNewChild ( node, NULL, (const xmlChar*)"VARIABLE", NULL);
00142 xmlSetProp (elmPtr, (const xmlChar*)"Name", (const xmlChar*)_Name.c_str());
00143 xmlSetProp (elmPtr, (const xmlChar*)"Value", (const xmlChar*)toString(_Value).c_str());
00144 xmlSetProp (elmPtr, (const xmlChar*)"Verbose", (const xmlChar*)toString(_Verbose).c_str());
00145 }
00146
00147 void CLogicVariable::read (xmlNodePtr node)
00148 {
00149 xmlCheckNodeName (node, "VARIABLE");
00150
00151 _Name = getXMLProp (node, "Name");
00152 _Value = atoiInt64 (getXMLProp (node, "Value").c_str());
00153 _Verbose = atoi(getXMLProp (node, "Verbose").c_str()) == 1;
00154 }
00155
00156
00157
00158
00159
00160 CLogicCounter::CLogicCounter()
00161 {
00162 _TickCount = 0;
00163 _Value = 0;
00164 _Name = "unamed_counter";
00165
00166 Period.setValue( 10 );
00167 Period.setName("Period");
00168
00169 Phase.setValue( 0 );
00170 Phase.setName("Phase");
00171
00172 Step.setValue( 1 );
00173 Step.setName("Step");
00174
00175 LowLimit.setValue( 0 );
00176 LowLimit.setName("LowLimit");
00177
00178 HighLimit.setValue( 100 );
00179 HighLimit.setName("HighLimit");
00180
00181 Mode.setValue( STOP_AT_LIMIT );
00182 Mode.setName("Mode");
00183
00184 Control.setValue( RUN );
00185 Control.setName("Control");
00186
00187 }
00188
00189
00190
00191
00192
00193
00194 void CLogicCounter::update()
00195 {
00196 if( Control.getValue() == STOPPED )
00197 {
00198 return;
00199 }
00200
00201 _TickCount++;
00202 if( _TickCount < Period.getValue() )
00203 {
00204 return;
00205 }
00206 else
00207 {
00208 _TickCount = 0;
00209 }
00210
00211 switch( Control.getValue() )
00212 {
00213 case RUN :
00214 {
00215 _Value += Step.getValue();
00216 manageRunningMode();
00217 }
00218 break;
00219
00220 case REWIND :
00221 {
00222 _Value = LowLimit.getValue();
00223 Control.setValue( RUN );
00224 }
00225 break;
00226
00227 case FAST_FORWARD :
00228 {
00229 _Value = HighLimit.getValue();
00230 Control.setValue( RUN );
00231 }
00232 break;
00233
00234 }
00235
00236 if( _Verbose )
00237 {
00238 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
00239 }
00240
00241 }
00242
00243
00244
00245
00246
00247
00248 void CLogicCounter::manageRunningMode()
00249 {
00250
00251 if( HighLimit.getValue() == LowLimit.getValue() )
00252 {
00253 _Value = HighLimit.getValue();
00254 return;
00255 }
00256
00257 switch( Mode.getValue() )
00258 {
00259 case STOP_AT_LIMIT :
00260 {
00261 if( _Value > HighLimit.getValue() )
00262 {
00263 _Value = HighLimit.getValue();
00264 }
00265 if( _Value < LowLimit.getValue() )
00266 {
00267 _Value = LowLimit.getValue();
00268 }
00269 }
00270 break;
00271
00272 case LOOP :
00273 {
00274
00275 if( _Value > HighLimit.getValue() )
00276 {
00277 _Value = LowLimit.getValue() + _Value - HighLimit.getValue() - 1;
00278 }
00279
00280 else
00281 {
00282 if( _Value < LowLimit.getValue() )
00283 {
00284 _Value = HighLimit.getValue() - (LowLimit.getValue() -_Value) + 1;
00285 }
00286 }
00287 }
00288 break;
00289
00290 case SHUTTLE :
00291 {
00292
00293 if( _Value > HighLimit.getValue() )
00294 {
00295 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
00296 Step.setValue( -Step.getValue() );
00297 }
00298
00299 else
00300 {
00301 if( _Value < LowLimit.getValue() )
00302 {
00303 _Value = LowLimit.getValue() + LowLimit.getValue() - _Value;
00304 Step.setValue( -Step.getValue() );
00305 }
00306 }
00307 }
00308 break;
00309
00310 case DOWN_UP :
00311 {
00312
00313 if( _Value < LowLimit.getValue() )
00314 {
00315 _Value = LowLimit.getValue() + LowLimit.getValue() - _Value;
00316 Step.setValue( -Step.getValue() );
00317 }
00318 else
00319 {
00320
00321 if( _Value > HighLimit.getValue() )
00322 {
00323 _Value = HighLimit.getValue();
00324 }
00325 }
00326 }
00327 break;
00328
00329 case UP_DOWN :
00330 {
00331
00332 if( _Value > HighLimit.getValue() )
00333 {
00334 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
00335 Step.setValue( -Step.getValue() );
00336 }
00337 else
00338 {
00339
00340 if( _Value < LowLimit.getValue() )
00341 {
00342 _Value = LowLimit.getValue();
00343 }
00344 }
00345 }
00346 }
00347
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 void CLogicCounter::write (xmlNodePtr node) const
00373 {
00374 xmlNodePtr elmPtr = xmlNewChild ( node, NULL, (const xmlChar*)"COUNTER", NULL);
00375 xmlSetProp (elmPtr, (const xmlChar*)"Name", (const xmlChar*)_Name.c_str());
00376 xmlSetProp (elmPtr, (const xmlChar*)"Value", (const xmlChar*)toString(_Value).c_str());
00377 xmlSetProp (elmPtr, (const xmlChar*)"Verbose", (const xmlChar*)toString(_Verbose).c_str());
00378 xmlSetProp (elmPtr, (const xmlChar*)"Period", (const xmlChar*)toString(Period.getValue()).c_str());
00379 xmlSetProp (elmPtr, (const xmlChar*)"Phase", (const xmlChar*)toString(Phase.getValue()).c_str());
00380 xmlSetProp (elmPtr, (const xmlChar*)"Step", (const xmlChar*)toString(Step.getValue()).c_str());
00381 xmlSetProp (elmPtr, (const xmlChar*)"LowLimit", (const xmlChar*)toString(LowLimit.getValue()).c_str());
00382 xmlSetProp (elmPtr, (const xmlChar*)"HighLimit", (const xmlChar*)toString(HighLimit.getValue()).c_str());
00383 xmlSetProp (elmPtr, (const xmlChar*)"Mode", (const xmlChar*)toString(Mode.getValue()).c_str());
00384 xmlSetProp (elmPtr, (const xmlChar*)"Control", (const xmlChar*)toString(Control.getValue()).c_str());
00385 }
00386
00387 void CLogicCounter::read (xmlNodePtr node)
00388 {
00389 xmlCheckNodeName (node, "COUNTER");
00390
00391 _Name = getXMLProp (node, "Name");
00392 _Value = atoiInt64 (getXMLProp (node, "Value").c_str());
00393 _Verbose = atoi(getXMLProp (node, "Verbose").c_str()) == 1;
00394 Period.setValue(atoiInt64(getXMLProp (node, "Period").c_str()));
00395 Phase.setValue(atoiInt64(getXMLProp (node, "Phase").c_str()));
00396 Step.setValue(atoiInt64(getXMLProp (node, "Step").c_str()));
00397 LowLimit.setValue(atoiInt64(getXMLProp (node, "LowLimit").c_str()));
00398 HighLimit.setValue(atoiInt64(getXMLProp (node, "HighLimit").c_str()));
00399 Mode.setValue(atoiInt64(getXMLProp (node, "Mode").c_str()));
00400 Control.setValue(atoiInt64(getXMLProp (node, "Control").c_str()));
00401 }
00402
00403
00404 }