1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
19 #include "nel/logic/logic_variable.h"
21 #include "nel/logic/logic_state_machine.h"
24 using namespace NLMISC
;
31 //---------------------------------------------------
34 //---------------------------------------------------
35 CLogicVariable::CLogicVariable()
41 } // CLogicVariable //
45 //---------------------------------------------------
48 //---------------------------------------------------
49 void CLogicVariable::setValue( sint64 value
)
55 nlinfo("variable \"%s\" value is now %f",_Name
.c_str(),(double)_Value
);
62 //---------------------------------------------------
63 // applyModification :
65 //---------------------------------------------------
66 void CLogicVariable::applyModification( string op
, sint64 value
)
68 if( op
== "SET" || op
== "set" )
73 if( op
== "ADD" || op
== "add" )
78 if( op
== "SUB" || op
== "sub" )
83 if( op
== "MUL" || op
== "mul")
88 if( op
== "DIV" || op
== "div")
90 if( value
!= 0 ) _Value
/= value
;
94 nlwarning("(LGCS)<CLogicVariable::applyModification> The operator \"%s\" is unknown",op
.c_str());
100 nlinfo("variable \"%s\" value is now %f",_Name
.c_str(),(double)_Value
);
103 } // applyModification //
106 //---------------------------------------------------
109 //---------------------------------------------------
110 void CLogicVariable::processLogic()
117 //---------------------------------------------------
120 //---------------------------------------------------
121 /*void CLogicVariable::serial( IStream &f )
123 f.xmlPush( "VARIABLE");
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 //---------------------------------------------------
152 //---------------------------------------------------
153 CLogicCounter::CLogicCounter()
157 _Name
= "unamed_counter";
159 Period
.setValue( 10 );
160 Period
.setName("Period");
163 Phase
.setName("Phase");
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 //---------------------------------------------------
186 //---------------------------------------------------
187 void CLogicCounter::update()
189 if( Control
.getValue() == STOPPED
)
195 if( _TickCount
< Period
.getValue() )
204 switch( Control
.getValue() )
208 _Value
+= Step
.getValue();
215 _Value
= LowLimit
.getValue();
216 Control
.setValue( RUN
);
222 _Value
= HighLimit
.getValue();
223 Control
.setValue( RUN
);
231 nlinfo("variable \"%s\" value is now %f",_Name
.c_str(),(double)_Value
);
237 //---------------------------------------------------
238 // manageRunningMode :
240 //---------------------------------------------------
241 void CLogicCounter::manageRunningMode()
244 if( HighLimit
.getValue() == LowLimit
.getValue() )
246 _Value
= HighLimit
.getValue();
250 switch( Mode
.getValue() )
254 if( _Value
> HighLimit
.getValue() )
256 _Value
= HighLimit
.getValue();
258 if( _Value
< LowLimit
.getValue() )
260 _Value
= LowLimit
.getValue();
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
275 if( _Value
< LowLimit
.getValue() )
277 _Value
= HighLimit
.getValue() - (LowLimit
.getValue() -_Value
) + 1;
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
294 if( _Value
< LowLimit
.getValue() )
296 _Value
= LowLimit
.getValue() + LowLimit
.getValue() - _Value
;
297 Step
.setValue( -Step
.getValue() );
305 // low limit reached, we go up
306 if( _Value
< LowLimit
.getValue() )
308 _Value
= LowLimit
.getValue() + LowLimit
.getValue() - _Value
;
309 Step
.setValue( -Step
.getValue() );
313 // high limit reached we stop
314 if( _Value
> HighLimit
.getValue() )
316 _Value
= HighLimit
.getValue();
324 // high limit reached, we go down
325 if( _Value
> HighLimit
.getValue() )
327 _Value
= HighLimit
.getValue() - (_Value
- HighLimit
.getValue());
328 Step
.setValue( -Step
.getValue() );
332 // low limit reached, we stop
333 if( _Value
< LowLimit
.getValue() )
335 _Value
= LowLimit
.getValue();
341 } // manageRunningMode //
344 //---------------------------------------------------
347 //---------------------------------------------------
348 /*void CLogicCounter::serial( IStream &f )
350 f.xmlPush( "COUNTER");
357 f.serial( LowLimit );
358 f.serial( HighLimit );
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()));