source: scenarios/MyFriend/Watching2Players/iCatChess/StateMachine.cpp @ 336

Revision 336, 11.5 KB checked in by pedro.cuba, 10 years ago (diff)
Line 
1#include "StateMachine.h"
2#include "stdafx.h"
3
4/* valores anteriores
5int idle_front_weights[7]={60,60,10,10,10,10,5};
6int idle_left_weights[6]={60,60,10,10,10,10};
7int idle_right_weights[6]={60,60,10,10,10,10};
8int interactive_weights[6]={60,30,45,5,10,10};
9*/
10
11//novos pesos - empatia
12
13int idle_front_weights[7]={20,40,10,10,10,10,5};
14int idle_left_weights[6]={60,60,60,60,60,60}; //LADO DO COMPANHEIRO?
15int idle_right_weights[6]={30,30,10,10,10,10};
16int interactive_weights[6]={60,30,15,5,20,5};
17
18const char *idle_front_animations[]={"\\STANDARD\\Modes\\Idle\\IdleFrontStill.raf",    "\\STANDARD\\Modes\\Idle\\IdleFrontBlink.raf",
19                 "\\STANDARD\\Modes\\Idle\\IdleFrontLookDown.raf", "\\STANDARD\\Modes\\Idle\\IdleFrontLookUp.raf",
20                 "\\STANDARD\\Modes\\Idle\\IdleFrontLookLeft.raf", "\\STANDARD\\Modes\\Idle\\IdleFrontLookRight.raf",
21                 "\\STANDARD\\Modes\\Idle\\IdleFrontYawn.raf"};
22const char *idle_left_animations[]={"\\STANDARD\\Modes\\Idle\\IdleLeftStill.raf",    "\\STANDARD\\Modes\\Idle\\IdleLeftBlink.raf",
23                 "\\STANDARD\\Modes\\Idle\\IdleLeftLookDown.raf", "\\STANDARD\\Modes\\Idle\\IdleLeftLookUp.raf",
24                 "\\STANDARD\\Modes\\Idle\\IdleLeftLookLeft.raf", "\\STANDARD\\Modes\\Idle\\IdleLeftLookRight.raf"};
25const char *idle_right_animations[]={"\\STANDARD\\Modes\\Idle\\IdleRightStill.raf",    "\\STANDARD\\Modes\\Idle\\IdleRightBlink.raf",
26                 "\\STANDARD\\Modes\\Idle\\IdleRightLookDown.raf", "\\STANDARD\\Modes\\Idle\\IdleRightLookUp.raf",
27                 "\\STANDARD\\Modes\\Idle\\IdleRightLookLeft.raf", "\\STANDARD\\Modes\\Idle\\IdleRightLookRight.raf"};
28
29const char *interactive_animations[]={"\\STANDARD\\Empathy\\IntrStill.raf",    "\\STANDARD\\Empathy\\IntrBlink.raf",
30                 "\\STANDARD\\Empathy\\IntrLookFront.raf", "\\STANDARD\\Empathy\\IntrLookUp.raf",
31                 "\\STANDARD\\Empathy\\IntrLookLeft.raf", "\\STANDARD\\Empathy\\IntrLookRight.raf"};
32
33//AnimationTable idle_front_table;
34
35//idle_front_table.weights=idle_front_weights;
36
37//idle_front_table.weights[7]={60,60,10,10,10,10,5};
38/*idle_front_table.weights= {60, 60, 10, 10, 10, 10, 5};
39idle_front_table.animations = {"\\STANDARD\\Modes\\Idle\\IdleFrontStill.raf",    "\\STANDARD\\Modes\\Idle\\IdleFrontBlink.raf",
40                 "\\STANDARD\\Modes\\Idle\\IdleFrontLookDown.raf", "\\STANDARD\\Modes\\Idle\\IdleFrontLookUp.raf",
41                 "\\STANDARD\\Modes\\Idle\\IdleFrontLookLeft.raf", "\\STANDARD\\Modes\\Idle\\IdleFrontLookRight.raf",
42                 "\\STANDARD\\Modes\\Idle\\IdleFrontYawn.raf"};
43*/
44
45StateMachine::StateMachine(){
46        _currentState=INTRO01;
47        _nextState=0;
48        _doTransition=false;
49        _sleepingNods=0;
50        _timeout=0;
51        _idle_front_table.size=7;
52        _idle_front_table.animations=idle_front_animations;
53        _idle_front_table.weights=idle_front_weights;
54        _idle_left_table.size=6;
55        _idle_left_table.animations=idle_left_animations;
56        _idle_left_table.weights=idle_left_weights;
57        _idle_right_table.size=6;
58        _idle_right_table.animations=idle_right_animations;
59        _idle_right_table.weights=idle_right_weights;
60        _interactive_table.size=6;
61        _interactive_table.animations=interactive_animations;
62        _interactive_table.weights=interactive_weights;
63        _prevChessState=99;
64        //idle_front_table.weights[7];
65        //={60,60,10,10,10,10,5};
66}
67
68
69void StateMachine::Reset(){
70        _currentAnimation="";
71        _currentState=INTERACTIVE;
72        _sleepingNods=0;
73        _timeout=0;
74        _prevChessState=99;
75}
76
77bool StateMachine::ThrowDice(int threeshold){
78        if ((rand()%1000)<threeshold)
79                return true;
80        else
81                return false;
82}
83
84const char * StateMachine::SelectRandom(AnimationTable at){
85        int total_w=0, dice, sum,i;
86
87        for (i=0; i < at.size; i++)
88                total_w= total_w + at.weights[i];
89
90        dice= rand()%total_w;
91
92        sum=at.weights[0];
93        i=0;
94        while(sum<dice){
95                i++;
96                sum=sum+at.weights[i]; 
97        }
98
99        return at.animations[i];
100}
101
102void StateMachine::GotoState(int nextstate, bool timeout)
103{
104        if (_currentState!= nextstate){
105                _doTransition=true;
106                _nextState=nextstate;
107               
108                if(timeout== true)
109                        _timeout=0;
110        }
111
112}
113
114void StateMachine::setTimeOut(int value)
115{
116        _timeout=value;
117}
118
119int StateMachine::getTimeOut()
120{
121        return _timeout;
122}
123
124int StateMachine::getState(){
125        return _currentState;
126}
127
128int StateMachine::getNextState(){
129        return _nextState;
130}
131
132void StateMachine::update(int state, AnimationModuleInterface * _animation, int channel){
133
134        if(_prevChessState!=state){
135                GotoState(INTERACTIVE,true);
136                _currentAnimation=(char *)GetNextAnimation().pszGetPointer();
137                _animation->PlayAnimation((char *)_currentAnimation.pszGetPointer() ,channel);
138        }
139        else {
140        setTimeOut(getTimeOut() + 1);
141        if (getState() == INTERACTIVE){
142                if (getTimeOut() > 300)
143                        GotoState(IDLE_LOOK_FRONT, true);
144        }
145
146        else if ((getState() == IDLE_LOOK_FRONT) ||
147          (getState() == IDLE_LOOK_LEFT) ||
148          (getState() == IDLE_LOOK_RIGHT))
149      if (getTimeOut() > 300)
150                  GotoState(SLEEPING_BY_NODDING, true);
151        }
152        _prevChessState=state;
153}
154
155DMLString StateMachine::GetTransitionAnimation()
156{
157        DMLString animation;
158        animation="";
159        //DMLString animation;
160   // Check whether a transition needs to be done
161        if (_doTransition)
162        {       
163                if (_nextState == SLEEPING_BY_NODDING){
164                        if (_currentState == IDLE_LOOK_FRONT){
165            animation = "\\STANDARD\\Empathy\\Transitions\\Idle2Sleep.raf";
166                        _currentState = SLEEPING_BY_NODDING;
167                        _doTransition = false;
168                        }
169
170                        else if (_currentState == INTERACTIVE){
171            animation = "\\STANDARD\\Empathy\\Transitions\\Interactive2Sleep.raf";
172            _currentState = SLEEPING_BY_NODDING;
173                        _doTransition = false;
174                        }
175
176                        else if (_currentState == IDLE_LOOK_LEFT){
177            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnLeftReturn.raf";
178            _currentState = IDLE_LOOK_FRONT;
179            GotoState(SLEEPING_BY_NODDING, true);
180                        }
181           
182                        else if (_currentState == IDLE_LOOK_RIGHT){
183            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnRightReturn.raf";
184            _currentState = IDLE_LOOK_FRONT;
185            GotoState(SLEEPING_BY_NODDING, true);
186                        }
187                }
188     
189                else if (_nextState == SLEEPING_BY_FLASHING){
190         animation = "\\STANDARD\\Modes\\Sleeping\\SleepFlash.raf";
191         _currentState = SLEEPING_BY_FLASHING;
192         _doTransition = false;
193                }
194         
195                else if (_nextState == IDLE_LOOK_FRONT){
196         if (_currentState == SLEEPING_BY_NODDING ||
197                         _currentState == SLEEPING_BY_FLASHING){
198            animation = "\\STANDARD\\Empathy\\Transitions\\Sleep2Idle.raf";
199            _currentState = IDLE_LOOK_FRONT;
200            _doTransition = false; 
201                 }
202                else if (_currentState == INTERACTIVE){
203            animation = "\\STANDARD\\Empathy\\Transitions\\Interactive2Idle.raf";
204            _currentState = IDLE_LOOK_FRONT;
205            _doTransition = false;
206                }
207 
208                else if (_currentState == IDLE_LOOK_LEFT){
209            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnLeftReturn.raf";
210            _currentState = IDLE_LOOK_FRONT;
211            _doTransition = false;           
212                }
213                else if (_currentState == IDLE_LOOK_RIGHT){
214            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnRightReturn.raf";
215            _currentState = IDLE_LOOK_FRONT;
216            _doTransition = false;                       
217                }
218                }   
219                else if (_nextState == INTERACTIVE){
220                        if (_currentState == INTRO03){
221            animation = ""; // no transition animation is needed
222            _currentState = INTERACTIVE;
223            _doTransition = false;
224                        }
225         else if ((_currentState == SLEEPING_BY_NODDING)||
226                         (_currentState == SLEEPING_BY_FLASHING)){
227            animation = "\\STANDARD\\Empathy\\Transitions\\Sleep2Interactive.raf";
228            _currentState = INTERACTIVE;
229            _doTransition = false;
230                        }
231
232                 else if (_currentState == IDLE_LOOK_FRONT){
233            animation = "\\STANDARD\\Empathy\\Transitions\\Idle2Interactive.raf";
234            _currentState = INTERACTIVE;
235                        GotoState(INTERACTIVE, true);
236                 }
237           
238                 else if (_currentState == IDLE_LOOK_LEFT){
239            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnLeftReturn.raf";
240            _currentState = IDLE_LOOK_FRONT;
241            GotoState(INTERACTIVE, true);
242                        }
243           
244                 else if (_currentState == IDLE_LOOK_RIGHT){
245            animation = "\\STANDARD\\Modes\\Idle\\IdleTurnRightReturn.raf";
246            _currentState = IDLE_LOOK_FRONT;
247                        GotoState(INTERACTIVE, true);
248                 }
249                }
250                else if (_nextState == IDLE_LOOK_LEFT){
251         animation = "\\STANDARD\\Modes\\Idle\\IdleTurnLeft.raf";
252         _currentState = IDLE_LOOK_LEFT;
253                 _doTransition = false;
254                }                     
255         
256                else if (_nextState == IDLE_LOOK_RIGHT){
257         animation = "\\STANDARD\\Modes\\Idle\\IdleTurnRight.raf";
258         _currentState = IDLE_LOOK_RIGHT;
259                 _doTransition = false;
260                }                     
261                else{
262         animation = "";
263         _doTransition = false;
264         _currentState = _nextState;
265                }         
266       
267      if (_currentState == SLEEPING_BY_NODDING)
268                  _sleepingNods = 0;
269   }
270   return animation;
271}
272
273
274DMLString StateMachine::GetNextAnimation()
275{
276   DMLString animation = "";
277
278
279   // first check whether a transition animation needs to be played
280   // if so, play this animation before playing the next action animation
281   animation = GetTransitionAnimation();
282   if (animation != ""){
283           _currentAnimation=animation;
284           return animation;
285   }
286     
287   // If no transition animation needs to be played then select the next
288   // animation to be played in the current state
289   if (_currentState == INTRO01){
290      animation = "\\STANDARD\\Demo\\Intro01.raf";
291          GotoState (INTRO02, true);
292   }
293
294   else if (_currentState == INTRO02){
295      animation = "\\STANDARD\\Demo\\Intro02.raf";
296      GotoState (INTRO03, true);
297   }
298   else if (_currentState == INTRO03){
299      animation = "\\STANDARD\\Demo\\Intro03.raf";
300          GotoState (INTERACTIVE, true);
301   }
302   else if (_currentState == SLEEPING_BY_NODDING){
303      animation = "\\STANDARD\\Modes\\Sleeping\\SleepNod.raf";     
304      // the transition to an other sleeping animation is
305      // done autonomously and by time
306      _sleepingNods = _sleepingNods + 1;
307      if (_sleepingNods >= 4)
308         GotoState (SLEEPING_BY_FLASHING, false);
309   }
310   else if (_currentState == SLEEPING_BY_FLASHING){
311           animation = "\\STANDARD\\Modes\\Sleeping\\SleepFlash.raf";
312   }
313   else if (_currentState == IDLE_LOOK_FRONT){
314      animation = SelectRandom (_idle_front_table);       
315      // the transition to looking left or right is done autonomously
316      // and randomly
317      if (ThrowDice(100))
318         GotoState (IDLE_LOOK_LEFT, false);
319      else if (ThrowDice(100))
320         GotoState (IDLE_LOOK_RIGHT, false);
321   }
322   else if (_currentState == IDLE_LOOK_LEFT){
323      animation = SelectRandom (_idle_left_table);
324      // the transition to looking to the front is done autonomously
325      if (ThrowDice(300))
326         GotoState (IDLE_LOOK_FRONT, false);
327   }         
328   else if (_currentState == IDLE_LOOK_RIGHT){
329      animation = SelectRandom (_idle_right_table);
330      // the transition to looking to the front is done autonomously
331      if (ThrowDice(300))
332         GotoState (IDLE_LOOK_FRONT, false);
333   }     
334   else if (_currentState == INTERACTIVE)
335      animation = SelectRandom (_interactive_table);
336 
337   _currentAnimation=animation;
338   return animation;
339}
340
341DMLString StateMachine::getCurrentAnimation(){
342        return _currentAnimation;
343}
Note: See TracBrowser for help on using the repository browser.