Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Work in Progress / Finite State Machine Plugin

Author
Message
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 8th Jan 2007 01:49
I've had the idea for a couple days now to create a plugin that will act as customizable finite state machine for people's games. If you're not familiar with what I'm talking about, I'm going to quote from a book called "Programming Game AI by Example" (pg. 44)

Quote: "The idea behind a finite state machine, therefore, is to decompose an object's behavior into easily manageable "chunks" or states. The light switch on your wall, for example, is a very simple finite state machine. It has two states: on and off. Transitions between states are made by the input of your finger. By flipping the switch down it makes the transition from on to off. There is no output or action associated with the off state (unless you consider the bulb being off as an action), but when it is in the on state electricity is allowed to flow through the switch and light up your room via the filament in a lightbulb.

Of course, the behavior of a game agent is usually much more complex than a lightbulb (thank goodness!). Here are some examples of how finite state machines have been used in games.

- The ghosts' behavior in Pac-Man is implemented as a finite state machine. There is one Evade state, which is the same for all ghosts, and then each ghost has its own Chase state, the actions of which are implemented differently for each ghost. The input of the player eating one of the power pills is the condition for the transition from Chase to Evade. The input of a timer running down is the condition for teh transition from Evade to Chase.

- Quake-style bots are implemented as finite state machines. They have states such as FindArmor, FindHealth, SeekCover, and RunAway. Even the weapons in Quake implement their own mini finite state machines. For example, a rocket may implement states such as Move, TouchObject, and Die.

- Players in sports simulations such as the soccer game FIFA2002 are implemented as state machines. They have states such as Strike, Dribble, ChaseBall, and MarkPlayer. In addition, the teams themselves are often implemented as FSMs and can have states such as KickOff, Defend, or WalkOutOnField.

- The NPCs (non-player characters) in RTSs (real-time strategy games) such as Warcraft make use of finite state machines. They have states such as MoveToPosition, Patrol, and FollowPath."


That small excerpt should give you a better understanding of what I mean. How I plan to turn that into a plugin is by allowing users to use their dbpro user functions as the states. For example, if you set all the current enemy states with the plugin using one simple command you will be able to execute all the functions that are currently set in the state machine, or a paticular state function. Here's what the command would look like to execute all the stored active states:



The simple implementation format I'll be using will reduce the normal amount of code you'd have to use, and help to speed up development. As a quick note, this plugin has not been coded out as of right now, but within the next hour or so I'll be coding it out and posting it up here for users to check out.

Plugin Commands

- MAKE FINITE STATE [function number], [string identifier], [on/off - 0 for off, 1 for on]
- DELETE FINITE STATE [string indentifier]
- SET FINITE STATE [string indentifier], [on/off - 0 for off, 1 for on]
- EXECUTE FINITE STATE ["all" - for all, or string indentifier]

Stay tuned to this thread! If you have any questions or comments, or even want to tell me this is a waste of time post it up here.

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 8th Jan 2007 02:11
Hey Torrey-

How will it be implemented exactly? Is "function number" the number of a DBPro function, kind of like the Lua plugin?

I use finite state machines in my code A LOT as I think it is a great organizational and debugging tool, so I'm interested in what this will do differently than what I do (with global game state variables and select statements within a do..loop)?



Also it looks like you can have more than one state on at a time. Is that a correct FSM? I've always used it to only have a single state active at any given time, but then again I'm not a real programmer with a full CS background.

I'm not a real programmer but I play one with DBPro!
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 8th Jan 2007 02:42
Quote: "How will it be implemented exactly? Is "function number" the number of a DBPro function, kind of like the Lua plugin?"

Function numbers have been in use with the plugins I've created over the last year. These function numbers are referring to dbpro user functions. When dbpro compiles your game/application the functions are compiled in the order they were written. So for example the top most function in your source code would be function number 1, the next being 2, etc.

Quote: "Also it looks like you can have more than one state on at a time. Is that a correct FSM? I've always used it to only have a single state active at any given time, but then again I'm not a real programmer with a full CS background."


An enemy can be in one state at a time, this plugin makes it possible for you to set states for multiple enemies or objects in the game. Although from what you asked in your question, it's common to use one state a time, but if you were trying to emulate more than one behavior from AI you could use this plugin to coordinate that. For example, say you're shooting up an enemy bot and it's starting to get low on health, you could set its states to evade, defend, and find health pack.

Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 8th Jan 2007 05:33 Edited at: 8th Jan 2007 07:09
The plugin is now attached, anyone that has some time give it a try and let me know what you think.

Plugin Commands

- MAKE FINITE STATE [function number], [string identifier], [on/off - 0 for off, 1 for on]
- DELETE FINITE STATE [string indentifier]
- SET FINITE STATE [string indentifier], [on/off - 0 for off, 1 for on]
- EXECUTE FINITE STATE ["all" - for all, or string indentifier]

Notes

- Using EXECUTE FINITE STATE with "all" will execute current states in the order they were created.
- If you use a string indentifier other than "all" with EXECUTE FINITE STATE it will override the current set state for that indentifier and execute the function.
- Function numbers are referring to dbpro user functions. When dbpro compiles your game/application the functions are compiled in the order they were written. So for example the top most function in your source code would be function number 1, the next being 2, etc. If you need further detail about this just ask.
- This plugin also passes the string identifier to the function being called, so you can format for your function with or without that. Here's a quick example of the two if you're not sure what I mean:

You could possibly use this identifier as an id if you have more than 1 object using this state with the same function.

Attachments

Login to view attachments
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 9th Jan 2007 20:23
It has been a decent amount of time since I posted this up, and still haven't received any comments or hate mail, so I'm bumping it.

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 10th Jan 2007 00:52 Edited at: 10th Jan 2007 00:54
Hi Torrey.

Except for hiding the implementation of EXECUTE FINITE STATE "all" and ability to set multiple states for objects, what you have can be done fairly easily with some functions and a select...endselect which calls each function depending on state.

I'm also not sure about having numbered functions. I understand the technical reason why you have to have it... just that I don't want to worry about what order my functions are in the code.

Wouldn't really like using a string for an identifier... since array storage in DBPro is by integer/dword mostly, I'd have to convert back and forth between the identifier and a number when doing game-specific actions and that might get cumbersome. Are you using this with Lua tables or something?

And lastly, most people I've seen do their game AI at the end because it's one of the more complicated things to do. And when projects are not often completed, the last few things on the list don't get touched.

Hey I'm not bashing the plugin just explaining why I'm not using it. I love your other plugins. Hopefully someone will use this one to great effect. Or explain further how it can help in a game beyond what I do manually already, mentioned above.

Oh by the way... how is that Game AI by Example book? Is it really good? Helpful for non-OOP, procedural languages like DBPro? I've been thinking about getting it.

I'm not a real programmer but I play one with DBPro!
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 10th Jan 2007 09:16
Quote: "Except for hiding the implementation of EXECUTE FINITE STATE "all" and ability to set multiple states for objects, what you have can be done fairly easily with some functions and a select...endselect which calls each function depending on state."


I'm not the best at explaining some of my thought processes when creating some of the stuff I do, but I'll try. The spontanious idea I had at the moment of creation was to cut down the coding need for the users program loop. Different parts of the game would be written in managable chunks (dbpro user functions) that could update global variables and the finite state machine so that on the next loop cycle the proper functions would be ran. Here's a pseudo-ish idea of it:



I guess you could say it almost turns your application or game into a object oriented setup. Maybe it's just me, but it seems really easy to maintain this way, and appears more organized.

Quote: "I'm also not sure about having numbered functions. I understand the technical reason why you have to have it... just that I don't want to worry about what order my functions are in the code."


Take a look at the code in this posting. Your functions can really be in any order, but since I'm basically cracking your game in memory while it's running it reads the functions as they appear in memory. I use function numbers because it's the easiest way to reference user functions.

Quote: "Wouldn't really like using a string for an identifier... since array storage in DBPro is by integer/dword mostly, I'd have to convert back and forth between the identifier and a number when doing game-specific actions and that might get cumbersome. Are you using this with Lua tables or something?"


I could change this indentifier really easy to be a integer or dword. At the time I was developing it my mind was thinking people would refer to different game objects like "bigboss1_attack", "spotlight_on", etc. Something simple and organized. There are no Lua tables in my dll, I created my own data structures. In fact it may not be surprising to anyone, I don't like Lua (GameMonkey all the way!!).

Quote: "Oh by the way... how is that Game AI by Example book? Is it really good? Helpful for non-OOP, procedural languages like DBPro? I've been thinking about getting it."


"Programming Game AI by Example" is an excellent book to learn from. The author takes things step by step and shows example source code (C++). So with a little C++ knowledge you'd be able to port the example code to nearly any other programming language you know.

Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 12th Jan 2007 03:02
I think I'm probably missing out on part of the concept here, but as my various projects stand, I generally handle AI by just using a few nested if/endifs (less efficient than select/case I hear but I prefer it for some reason) to determine what package (ie combat, idle) it's going to run. Is there a notable advantage to using this plugin instead?
Torrey
20
Years of Service
User Offline
Joined: 20th Aug 2004
Location: New Jersey
Posted: 12th Jan 2007 08:30
Quote: "I think I'm probably missing out on part of the concept here, but as my various projects stand, I generally handle AI by just using a few nested if/endifs (less efficient than select/case I hear but I prefer it for some reason) to determine what package (ie combat, idle) it's going to run. Is there a notable advantage to using this plugin instead?"


The if/endifs are only less efficient if your code is calling the same functions over again that it could have just called once causing some performance loss. This quickie plugin was created with the notable advantages:

- Reducing the need for select/case and if/endifs in your main program loop. When the finite state machine executes your functions, updates to the state machine can occur by a simple plugin command that will affect which functions execute.

- More organization to your source code. Using this plugin promotes the use of functions. With the use of functions it makes your code more organized by breaking up different chucks of code into easily identifiable areas.

Overall, I believe you can create a few more frames per second with this, reduce the amount of coding, and keep your code organized. It's a super simple, but useful and free plugin.

Login to post a reply

Server time is: 2024-09-29 18:41:51
Your offset time is: 2024-09-29 18:41:51