Hi folks,
Almost all of my programs are either desktop-apps for work, or shiny techdemos for personal enjoyment. Both kinds of apps don’t really deal with AI, so I haven’t got any real experience with AI and the lot. I have read quite a bit about FiniteStateMachines, and I think I can say I grasp the concept. The only thing that kind of worries me, is how to manage that ‘condition matrix’ that seems to grow exponentially with the number of states. You can be in only 1 state at on time, so the current state must be able to handle pretty much all kinds of input/triggers. It seems to me this results in a lot of redundant code in all those states. Maybe this hase been long solved, but from what I read about it, it’s pretty spaghetti code from the start. (correct?)
My alternative:
Now I had this idea to have a stack of States in the StateMachine. The StateMachine has Triggers that have a name, and once they fire, they peek() the state-stack, and call the on{triggerName}(StateMachine sm) method of that State, for example the Trigger named “hungry” invokes “state.onHungry(sm)” if such a method exists in that State (checked with Reflection). If it doesn’t exists, the State is pop()ed off the stack, and the parent will be checked for that method, etc etc.
So lets say you are in the Idle-state, which pushed Work-state in the stack, and the hungry-Trigger is fired, workState.onHungry(…) will be invoked, or idleState.onHungry(…) will be invoked, if workState does not declare that method. This way you let only the States that are interested in certain events, handle them, and if something else happens, it pops itself off the stack and gives the ‘responsibility’ to it’s parent.
What we have now is a StateMachine where you can override certain ‘base behaviour’ by declaring a trigger-event-catching method that the parent also declared.
So if this a good idea? (it already works here, so it can’t be that bad)
Has this problem been solved ages ago?
How does everybody else solve this AI problem…?