MODULE: StateMachine - A fresh look at Finite State Machines, from 2009! :D

Started by monkey0506, Thu 07/07/2011 08:10:12

Previous topic - Next topic

monkey0506

In response to the overwhelming demand for me to release more modules ( ::) ), I've dug this one up from 2009. But don't worry, it's still entirely viable.

Download

For those of you who don't know what a finite state machine is, it's basically a way of looking at the relations between different states. For example, you could say that the grocery store is east of your house, so if you were at your house (state: "house"), and you went east (transition: "east"), then you would arrive at the grocery store (state: "store").

Another example would be to think of turn-based RPGs. The character could be in a number of different states. An FSM wouldn't really be the best option for storing say, the character's health, but you could use it to store other information. For example, their normal state could be Healthy (state: "Healthy"). If an enemy casts a spell on them, they might get poisoned (transition: "poison spell"). After the enemy cast the spell, their state would now be Poisoned (state: "poisoned").

This is where the FSM steps in. The "machine" lets you define what "states" exist:

Code: text
// pseudo-code
STATE:Healthy
STATE:Poisoned
STATE:Confused
STATE:Asleep


From there, you can define "transitions" which indicate the relation between different states. Each transition has a "from state" and a "to state", the "before" and "after":

Code: text
// pseudo-code
FROMSTATE:Healthy
TRANSITION:Poison Spell
TOSTATE:Poisoned
FROMSTATE:Poisoned
TRANSITION:Use Antidote
TOSTATE:Healthy


So now we have defined some of the relations between some of our states. The transitions are basically "what happens" that causes a shift or change from one state to another. For more information, there's some further examples in the StateMachine header file (.ash), which includes full documentation, and of course, Google is your friend too! :=

I've included a script file FSM.asc. You can't import it directly into your games (it's not an exported module), but if you want to plug it in you can just open it in a text editor (Notepad) and copy it into a script that way. This script file is the source of the included demo, which is a simple Interactive-Fiction style implementation of an FSM. Some of it might look complicated, but don't be intimidated, using this module is actually quite simple. Once you've implemented a few states and transitions, you should be able to get the hang of it.

Download

NOTE: HeirOfNorton once released an FSM module that was a raging success at going largely unnoticed. := I've taken a slightly different approach to the implementation, and hopefully the demo and some of the examples will help exemplify what makes FSMs so powerful.

Dualnames

I remember that module, in fact i was reading this whole topic to see if you were going to credit the idea :P

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Kweepa

Very nice!

Spoiler

I don't understand why the FSM would kill you. Surely He is benevolent.
[close]
Still waiting for Purity of the Surf II

monkey0506

Yes, just to elaborate a bit more, HoN's module relied on integer states and transitions whereas I am using Strings, which lets you be a bit more descriptive or verbose according to your needs and desires. For example, in the included demo, the button text you see is the exact input passed into the machine to effect the transition.

Glad to have your approval Bora--er--Steve! Oh, and:

Spoiler
I'm pretty sure he was just in a mean mood after falling in that lava pit one too many times in his search for treasure.
[close]

SMF spam blocked by CleanTalk