# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

logic_variable.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
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 // CLogicVariable :
00040 // 
00041 //---------------------------------------------------
00042 CLogicVariable::CLogicVariable()
00043 {
00044         _Value = 0;
00045         _Name = "unamed_var";
00046         _Verbose = false;
00047 
00048 } // CLogicVariable //
00049 
00050 
00051 
00052 //---------------------------------------------------
00053 // setValue :
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 } // setValue //
00066 
00067 
00068 
00069 //---------------------------------------------------
00070 // applyModification :
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 } // applyModification //
00111 
00112 
00113 //---------------------------------------------------
00114 // processLogic :
00115 // 
00116 //---------------------------------------------------
00117 void CLogicVariable::processLogic()
00118 {
00119 
00120 
00121 } // processLogic //
00122 
00123 
00124 //---------------------------------------------------
00125 // serial :
00126 // 
00127 //---------------------------------------------------
00128 /*void CLogicVariable::serial( IStream &f )
00129 {
00130         f.xmlPush( "VARIABLE");
00131 
00132         f.serial( _Value );
00133         f.serial( _Name );
00134 
00135         f.xmlPop();
00136 
00137 } // serial //*/
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 // CLogicCounter :
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 } // CLogicCounter //
00188 
00189 
00190 //---------------------------------------------------
00191 // update :
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 } // update //
00242 
00243 
00244 //---------------------------------------------------
00245 // manageRunningMode :
00246 // 
00247 //---------------------------------------------------
00248 void CLogicCounter::manageRunningMode()
00249 {
00250         // loop on one value
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                         // value is higher than high limit
00275                         if( _Value > HighLimit.getValue() )
00276                         {
00277                                 _Value = LowLimit.getValue() + _Value - HighLimit.getValue() - 1;
00278                         }
00279                         // value is lower than low limit
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                         // value is higher than high limit
00293                         if( _Value > HighLimit.getValue() )
00294                         {
00295                                 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
00296                                 Step.setValue( -Step.getValue() );
00297                         }
00298                         // value is lower than low limit
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                         // low limit reached, we go up
00313                         if( _Value < LowLimit.getValue() )
00314                         {
00315                                 _Value = LowLimit.getValue() + LowLimit.getValue() - _Value;
00316                                 Step.setValue( -Step.getValue() );
00317                         }
00318                         else
00319                         {
00320                                 // high limit reached we stop
00321                                 if( _Value > HighLimit.getValue() )
00322                                 {
00323                                         _Value = HighLimit.getValue();
00324                                 }
00325                         }
00326                 }
00327                 break;
00328 
00329                 case UP_DOWN :
00330                 {
00331                         // high limit reached, we go down
00332                         if( _Value > HighLimit.getValue() )
00333                         {
00334                                 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
00335                                 Step.setValue( -Step.getValue() );
00336                         }
00337                         else
00338                         {
00339                                 // low limit reached, we stop
00340                                 if( _Value < LowLimit.getValue() )
00341                                 {
00342                                         _Value = LowLimit.getValue();
00343                                 }
00344                         }
00345                 }
00346         }
00347 
00348 } // manageRunningMode //
00349 
00350 
00351 //---------------------------------------------------
00352 // serial :
00353 // 
00354 //---------------------------------------------------
00355 /*void CLogicCounter::serial( IStream &f )
00356 {
00357         f.xmlPush( "COUNTER");
00358 
00359         f.serial( _Value );
00360         f.serial( _Name );
00361         f.serial( Period );
00362         f.serial( Phase );
00363         f.serial( Step );
00364         f.serial( LowLimit );
00365         f.serial( HighLimit );
00366         f.serial( Mode );
00367 
00368         f.xmlPop();
00369 
00370 } // serial //*/
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 }