Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / logic / logic_variable.cpp
blob601fd3929fa1c5556ef4eb1974f79da2d750abe6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "stdlogic.h"
19 #include "nel/logic/logic_variable.h"
21 #include "nel/logic/logic_state_machine.h"
23 using namespace std;
24 using namespace NLMISC;
27 namespace NLLOGIC
31 //---------------------------------------------------
32 // CLogicVariable :
34 //---------------------------------------------------
35 CLogicVariable::CLogicVariable()
37 _Value = 0;
38 _Name = "unamed_var";
39 _Verbose = false;
41 } // CLogicVariable //
45 //---------------------------------------------------
46 // setValue :
48 //---------------------------------------------------
49 void CLogicVariable::setValue( sint64 value )
51 _Value = value;
53 if( _Verbose )
55 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
58 } // setValue //
62 //---------------------------------------------------
63 // applyModification :
65 //---------------------------------------------------
66 void CLogicVariable::applyModification( string op, sint64 value )
68 if( op == "SET" || op == "set" )
70 _Value = value;
72 else
73 if( op == "ADD" || op == "add" )
75 _Value += value;
77 else
78 if( op == "SUB" || op == "sub" )
80 _Value -= value;
82 else
83 if( op == "MUL" || op == "mul")
85 _Value *= value;
87 else
88 if( op == "DIV" || op == "div")
90 if( value != 0 ) _Value /= value;
92 else
94 nlwarning("(LGCS)<CLogicVariable::applyModification> The operator \"%s\" is unknown",op.c_str());
95 return;
98 if( _Verbose )
100 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
103 } // applyModification //
106 //---------------------------------------------------
107 // processLogic :
109 //---------------------------------------------------
110 void CLogicVariable::processLogic()
114 } // processLogic //
117 //---------------------------------------------------
118 // serial :
120 //---------------------------------------------------
121 /*void CLogicVariable::serial( IStream &f )
123 f.xmlPush( "VARIABLE");
125 f.serial( _Value );
126 f.serial( _Name );
128 f.xmlPop();
130 } // serial //*/
132 void CLogicVariable::write (xmlNodePtr node) const
134 xmlNodePtr elmPtr = xmlNewChild ( node, NULL, (const xmlChar*)"VARIABLE", NULL);
135 xmlSetProp (elmPtr, (const xmlChar*)"Name", (const xmlChar*)_Name.c_str());
136 xmlSetProp (elmPtr, (const xmlChar*)"Value", (const xmlChar*)toString(_Value).c_str());
137 xmlSetProp (elmPtr, (const xmlChar*)"Verbose", (const xmlChar*)toString(_Verbose).c_str());
140 void CLogicVariable::read (xmlNodePtr node)
142 xmlCheckNodeName (node, "VARIABLE");
144 _Name = getXMLProp (node, "Name");
145 _Value = atoiInt64 (getXMLProp (node, "Value").c_str());
146 NLMISC::fromString(getXMLProp(node, "Verbose"), _Verbose);
149 //---------------------------------------------------
150 // CLogicCounter :
152 //---------------------------------------------------
153 CLogicCounter::CLogicCounter()
155 _TickCount = 0;
156 _Value = 0;
157 _Name = "unamed_counter";
159 Period.setValue( 10 );
160 Period.setName("Period");
162 Phase.setValue( 0 );
163 Phase.setName("Phase");
165 Step.setValue( 1 );
166 Step.setName("Step");
168 LowLimit.setValue( 0 );
169 LowLimit.setName("LowLimit");
171 HighLimit.setValue( 100 );
172 HighLimit.setName("HighLimit");
174 Mode.setValue( STOP_AT_LIMIT );
175 Mode.setName("Mode");
177 Control.setValue( RUN );
178 Control.setName("Control");
180 } // CLogicCounter //
183 //---------------------------------------------------
184 // update :
186 //---------------------------------------------------
187 void CLogicCounter::update()
189 if( Control.getValue() == STOPPED )
191 return;
194 _TickCount++;
195 if( _TickCount < Period.getValue() )
197 return;
199 else
201 _TickCount = 0;
204 switch( Control.getValue() )
206 case RUN :
208 _Value += Step.getValue();
209 manageRunningMode();
211 break;
213 case REWIND :
215 _Value = LowLimit.getValue();
216 Control.setValue( RUN );
218 break;
220 case FAST_FORWARD :
222 _Value = HighLimit.getValue();
223 Control.setValue( RUN );
225 break;
229 if( _Verbose )
231 nlinfo("variable \"%s\" value is now %f",_Name.c_str(),(double)_Value);
234 } // update //
237 //---------------------------------------------------
238 // manageRunningMode :
240 //---------------------------------------------------
241 void CLogicCounter::manageRunningMode()
243 // loop on one value
244 if( HighLimit.getValue() == LowLimit.getValue() )
246 _Value = HighLimit.getValue();
247 return;
250 switch( Mode.getValue() )
252 case STOP_AT_LIMIT :
254 if( _Value > HighLimit.getValue() )
256 _Value = HighLimit.getValue();
258 if( _Value < LowLimit.getValue() )
260 _Value = LowLimit.getValue();
263 break;
265 case LOOP :
267 // value is higher than high limit
268 if( _Value > HighLimit.getValue() )
270 _Value = LowLimit.getValue() + _Value - HighLimit.getValue() - 1;
272 // value is lower than low limit
273 else
275 if( _Value < LowLimit.getValue() )
277 _Value = HighLimit.getValue() - (LowLimit.getValue() -_Value) + 1;
281 break;
283 case SHUTTLE :
285 // value is higher than high limit
286 if( _Value > HighLimit.getValue() )
288 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
289 Step.setValue( -Step.getValue() );
291 // value is lower than low limit
292 else
294 if( _Value < LowLimit.getValue() )
296 _Value = LowLimit.getValue() + LowLimit.getValue() - _Value;
297 Step.setValue( -Step.getValue() );
301 break;
303 case DOWN_UP :
305 // low limit reached, we go up
306 if( _Value < LowLimit.getValue() )
308 _Value = LowLimit.getValue() + LowLimit.getValue() - _Value;
309 Step.setValue( -Step.getValue() );
311 else
313 // high limit reached we stop
314 if( _Value > HighLimit.getValue() )
316 _Value = HighLimit.getValue();
320 break;
322 case UP_DOWN :
324 // high limit reached, we go down
325 if( _Value > HighLimit.getValue() )
327 _Value = HighLimit.getValue() - (_Value - HighLimit.getValue());
328 Step.setValue( -Step.getValue() );
330 else
332 // low limit reached, we stop
333 if( _Value < LowLimit.getValue() )
335 _Value = LowLimit.getValue();
341 } // manageRunningMode //
344 //---------------------------------------------------
345 // serial :
347 //---------------------------------------------------
348 /*void CLogicCounter::serial( IStream &f )
350 f.xmlPush( "COUNTER");
352 f.serial( _Value );
353 f.serial( _Name );
354 f.serial( Period );
355 f.serial( Phase );
356 f.serial( Step );
357 f.serial( LowLimit );
358 f.serial( HighLimit );
359 f.serial( Mode );
361 f.xmlPop();
363 } // serial //*/
365 void CLogicCounter::write (xmlNodePtr node) const
367 xmlNodePtr elmPtr = xmlNewChild ( node, NULL, (const xmlChar*)"COUNTER", NULL);
368 xmlSetProp (elmPtr, (const xmlChar*)"Name", (const xmlChar*)_Name.c_str());
369 xmlSetProp (elmPtr, (const xmlChar*)"Value", (const xmlChar*)toString(_Value).c_str());
370 xmlSetProp (elmPtr, (const xmlChar*)"Verbose", (const xmlChar*)toString(_Verbose).c_str());
371 xmlSetProp (elmPtr, (const xmlChar*)"Period", (const xmlChar*)toString(Period.getValue()).c_str());
372 xmlSetProp (elmPtr, (const xmlChar*)"Phase", (const xmlChar*)toString(Phase.getValue()).c_str());
373 xmlSetProp (elmPtr, (const xmlChar*)"Step", (const xmlChar*)toString(Step.getValue()).c_str());
374 xmlSetProp (elmPtr, (const xmlChar*)"LowLimit", (const xmlChar*)toString(LowLimit.getValue()).c_str());
375 xmlSetProp (elmPtr, (const xmlChar*)"HighLimit", (const xmlChar*)toString(HighLimit.getValue()).c_str());
376 xmlSetProp (elmPtr, (const xmlChar*)"Mode", (const xmlChar*)toString(Mode.getValue()).c_str());
377 xmlSetProp (elmPtr, (const xmlChar*)"Control", (const xmlChar*)toString(Control.getValue()).c_str());
380 void CLogicCounter::read (xmlNodePtr node)
382 xmlCheckNodeName (node, "COUNTER");
384 _Name = getXMLProp (node, "Name");
385 _Value = atoiInt64 (getXMLProp (node, "Value").c_str());
386 NLMISC::fromString(getXMLProp (node, "Verbose"), _Verbose);
387 Period.setValue(atoiInt64(getXMLProp (node, "Period").c_str()));
388 Phase.setValue(atoiInt64(getXMLProp (node, "Phase").c_str()));
389 Step.setValue(atoiInt64(getXMLProp (node, "Step").c_str()));
390 LowLimit.setValue(atoiInt64(getXMLProp (node, "LowLimit").c_str()));
391 HighLimit.setValue(atoiInt64(getXMLProp (node, "HighLimit").c_str()));
392 Mode.setValue(atoiInt64(getXMLProp (node, "Mode").c_str()));
393 Control.setValue(atoiInt64(getXMLProp (node, "Control").c_str()));