forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[TUT] What is a finite state machine?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri Oct 07, 2005 9:34 am    Post subject: [TUT] What is a finite state machine? Reply with quote

You'll find full explanation regarding finite state machines [FSM] at: http://en.wikipedia.org/wiki/Finite_state_machine

We could use an event driven FSM for our game development. An example below best illustrates how it is implemented. The example is a resumable LED Timer, slightly modded one from the standard distribution (ver 0.11) created by me and changed for new Timer class by Shine. It would be handy to show the "mickey mouse" diagram on blackboard but impossible here. Instead, I'll try to explain in written form.

I am using 6 states for the example. The description for each is given below.
Code:

   State 0:
       reset Timer
       display Dial

   State 1:
       start Timer
       change State to 2

   State 2:
       display Dial
       if Timer exceeds Dial's range,
           change State to 0

   State 3
       stop Timer
       change State to 4

   State 4:
       display Dial

   State 5
       start Timer

       change State to 2

If you like, draw 6 big circles in a row and mark them with numbers 0,1,2,3,4, and 5, one inside each circle.

Event driven occurance is done by pressing X button. It changes the state 0 to state 1 to start, state 2 to state 3 to stop and state 4 to state 5 to resume.

Draw curved arrows marked with "X" between them as explained here.

All states 1, 3, 5 are changed immediately to the next state.

Draw curved dashed arrows between them as described showing the state changing automatically.

The states 0, 2, and 4 employ the displaying the dial indefinitely until the button X is pushed.

A special case when the dial is about out of range: change state 2 to state 0.

Draw the dashed arrow from state 2 to state 0 showing the special case.

Another short dashed arrow to state 0 from outside showing the initialization.

Pressing button O can occur anytime from any state to state 0 to reset the timer. No need to draw any further unless you wish to do so.

You might ask me why not changing state 5 to state 1 without needing to restart the timer in the state 5. It does the same thing, isn't it? Yes, it does BUT Shine explained that Controls.read() delays somewhat in the PSP hardware and the pad is read twice in the getState() function before resuming the timer. So it is better to restart immediately in the state 5.

The state machine can be modded with only 4 states. I'll leave that for your homework.

Here is the modded code for the "game loop" to show the complete state machine...
Code:

local timer = Timer.new()
local state = 0

repeat
  state = getState(state)
 
  if state == 0 then       -- reset timer
    timer:reset()
    displayDial(timer)
   
  elseif state == 1 then   -- start timer
    timer:start()
    state = 2
   
  elseif state == 2 then
    if displayDial(timer) then state = 0 end
   
  elseif state == 3 then   -- stop timer
    timer:stop()
    state = 4
   
  elseif state == 4 then
    displayDial(timer)

  elseif state == 5 then   -- resume timer
    timer:start()
    state = 2
  end
 
until state == -1


Here is the screenshot:


And you can download the fully packed louser compatible zipfile at my webpage.

Hope you enjoy the tutorial.
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
arrggg



Joined: 29 Sep 2005
Posts: 11

PostPosted: Fri Oct 07, 2005 11:45 pm    Post subject: Reply with quote

Thanks for the great tutorial! I got a couple ideas for this now.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group