<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Binary Thought &#187; C#</title>
	<atom:link href="http://www.rebz.org/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rebz.org</link>
	<description>Scratching the surface of my mind.</description>
	<lastBuildDate>Mon, 09 Aug 2010 00:40:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Basic AI: Building a Finite State Machine (FSM) in C#</title>
		<link>http://www.rebz.org/2010/02/basic-ai-building-a-finite-state-machine-fsm-in-c/</link>
		<comments>http://www.rebz.org/2010/02/basic-ai-building-a-finite-state-machine-fsm-in-c/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 05:39:39 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[A.I.]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[fsm]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.rebz.org/?p=154</guid>
		<description><![CDATA[I figured this information may benefit a few people, as well as allow me to get my thoughts out, I&#8217;ll be documenting the process to building a finite state machine (FSM). My main use out of this project will allow me to assign an object a FSM, assign that object specific states, and allow the [...]]]></description>
			<content:encoded><![CDATA[<p>I figured this information may benefit a few people, as well as allow me to get my thoughts out, I&#8217;ll be documenting the process to building a finite state machine (FSM). My main use out of this project will allow me to assign an object a FSM, assign that object specific states, and allow the object to make weighted decisions through a transition table. I plan on using this decision-system to aid in both creating a foundation for the AI of my games, as well as aid in the animation systems that I concoct.</p>
<p><span id="more-154"></span></p>
<h3>Intro</h3>
<p>When creating an A.I. system for your game, you&#8217;re going to want objects to be able to make decisions on their own, or at the very least have some sort of flowchart-like interaction. What if you were able to easily set up those basic interactions, and simply have your objects detect when certain stimuli change their states, and have actions and reactions dependent on the states of an object? We can create seemingly more intelligent objects, and a potentially more immersive game.</p>
<h3>The Individual State</h3>
<p>Each of the states in our machine will have the following information:</p>
<ul>
<li>State Name</li>
<li>Minimum Duration</li>
<li>Maximum Duration</li>
<li>Transitions</li>
</ul>
<p>This way, we can define the state with a human name that would make sense to a designer and something that could be easily scripted, and we can specify a range at which the state can last. With random state lengths, we can make objects seem a bit more organic. For objects that we need specific lengths, we can simply set the two to equal each other! Lastly, we need to know all the different states into which an individual state can transition.</p>
<h3>The Transition Table</h3>
<p>Each State will carry it&#8217;s own &#8220;Transition Table&#8221;, will include two important bits of information per entry:</p>
<ul>
<li>State Name</li>
<li>Weighted Percentage</li>
</ul>
<p>This way, we know which state we are going to transition to, and we are going to allow the definition of a weighted percentage of the states that are chosen. This will allow us to introduce rarity with our transitions.</p>
<h3>The Machine</h3>
<p>Our FSM will have several important actions which we will need to consider:</p>
<p style="padding-left: 30px;"><strong>Add State</strong></p>
<p style="padding-left: 30px;">After our FSM is created for an individual object, we will add the different states that will be valid for this particular object. We will give the option to add a min/max duration of the state, in case we want to attempt to make certain states less predictable than others.</p>
<p style="padding-left: 30px;"><strong>Remove </strong><strong>State</strong></p>
<p style="padding-left: 30px;">We may see a need to remove a state from our object. Consider  enemies that after being provoked will not stop engaging/attacking the player until they kill the player or are destroyed themselves. We could get this feature &#8216;automatically&#8217; if we removed the idle state from this object, and any transitions that may have brought us to this state.</p>
<p style="padding-left: 30px;"><strong>Add Transition</strong></p>
<p style="padding-left: 30px;">In order for a state to transition from one state to the next, we are going to want to add these connections in individually, with a weighted percentage at which they have the possibility of being picked. This can allow for balancing the choosing of states. We will also have this action update transition if it already exists in the table.</p>
<p style="padding-left: 30px;"><strong>Remove Transition</strong></p>
<p style="padding-left: 30px;">We may want to implement conditional transitions, or remove transitions for states that no longer exist.</p>
<p style="padding-left: 30px;"><strong>Set State</strong></p>
<p style="padding-left: 30px;">For setting an initial state on our object, or forcing the object into a specific state.</p>
<p style="padding-left: 30px;"><strong>Choose Next State</strong></p>
<p style="padding-left: 30px;">When the current state expires, the FSM will need to select a transition and set the next state</p>
<p style="padding-left: 30px;">
<h3>Usage</h3>
<p>Usage is fairly simple. You&#8217;ll first want to include the code in your Solution. To every object file you wish to use the FiniteStateMachine in, you need to add a:</p>
<pre class="brush:csharp">using Zapdot;</pre>
<p>At the header with your other using statements.</p>
<p>You will then want to add a class level variable.</p>
<pre class="brush:csharp">class Enemy
{
   FiniteStateMachine mFSM;

   public Enemy()
   {
      setupStateMachine();
   }
}</pre>
<p>Now, for tidiness-sake, create a method that you&#8217;ll use to store the setup for this particular object, to be called once during initialization.</p>
<pre class="brush:csharp"> private void setupStateMachine()
 {
 mFSM = new FiniteStateMachine();
 mFSM.AddState("idle", 2, 3);
 mFSM.AddState("walk", 2, 2);
 mFSM.AddState("run", 1, 3);
 mFSM.AddTransition("idle", "walk", .5);
 mFSM.AddTransition("idle", "run", .5);
 mFSM.AddTransition("walk", "idle", .5);
 mFSM.AddTransition("walk", "run", .5);
 mFSM.AddTransition("run", "idle", .5);
 mFSM.AddTransition("run", "walk", .5);
 // Pick our first state.
 mFSM.SetState("idle");
 }</pre>
<p>This instantiates the new object, and adds three states to the Enemy object. (&#8220;walk&#8221;, &#8220;run&#8221;, and &#8220;idle&#8221;). Both idle and run will have random time between their respective min and maximums, but walk will always be 2 seconds long, no matter what.</p>
<p>We then add the transitions for these states, essentially saying that every state can transition to the other, and that they have equal weight to do so.</p>
<p>Lastly, we tell the object what state to start in.</p>
<p>After this object is setup, we can add it to our update class:</p>
<pre class="brush:csharp">mFSM.Update(gameTime);</pre>
<p>And do some tests on it to affect gameplay.</p>
<pre class="brush:csharp">if ( mFSM.CurrentState.Equals("idle") )
   mVelocity = kIdleSpeed;</pre>
<p>And so on.</p>
<h3>The Code</h3>
<p><a href="http://www.rebz.org/wp-content/uploads/2010/02/FiniteStateMachine.zip">Download</a> (MIT License)</p>
<p>The code is available below, and being released under the MIT License. Feel free to do whatever you want with it, just don&#8217;t blame me. <img src='http://www.rebz.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If for some crazy reason you do start using the code, please tell me what you think, or what cool things you&#8217;re doing with it. <img src='http://www.rebz.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Future Additions!</h3>
<p>This is only the starting point from here, and I hope to be able to add much more. Future additions include wonderful things like multiple concurrent states to give a little more depth to our objects and add for some interesting decision-making!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rebz.org/2010/02/basic-ai-building-a-finite-state-machine-fsm-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
