Posts Tagged Finite State Machine
I fail at blogging. It’s Tuesday night and I still haven’t put anything up here. Who wants to administer the mandatory kick in the pants?
So, moving on again…
With the leg-and-torso system in place I was up and running, adding more possibilities and developing the creature AI and visualisation. Colours! Necks and tails! FSM’s!
… yes, FSM’s. No, not Flying Spaghetti Monsters: That’s what I thought when I first came across the acronym too, but here I’m talking about “Finite State Machines.” If that sounds like a doomsday device to you, awesome. Keep believing that, because a doomsday device would be so much cooler than an actual Finite State Machine.
Long story short, a finite state machine is a term in AI programming that refers to a list of simple, mutually exclusive states that an entity (an enemy, an NPC, a creature, etc) can be in, along with a set of rules as to which states can go to which other states. A typical example for an FPS enemy would be as follows:
As you can see in the above example, an enemy has to be startled before they’ll start attacking. This might give you a chance to play a ‘jump’ animation (for example, the grunts in Halo), or to get them to shout for help, before they target the player.
Be separating the code into mutually exclusive segments like this, it becomes easier to debug, easier to change and actually has better performance: I don’t have to check for cover, see whether the enemy can shoot, or run any collision or movement AI for an enemy marked as “Idle”, since he would just stand around. It also makes the enemies easier to read and predict, which is an essential element for many games.
Back to Species: the FSM for Species is a lot less regimented, and a lot more complex because creatures can go from one state to almost any another pretty much at any point, with “moving” used as a hub. By using “moving” in this fashion, a creature has to turn and walk towards it’s target before it starts eating/attacking/mating with it, meaning that under most circumstances creatures will be facing the object they are interacting with as they interact with it.
Dead is the exception: if I tell a creature to die, it dies immediately, no matter what state it’s in.
*footnote: This is actually an idea I had while drawing this diagram. Currently, “playing” is an utterly useless behavior, and creatures jump between Moving and Mating freely, but if it serves as an entry point to mating then sexual selection becomes much easier to implement.
Now, at this stage in the chronological saga, I’ve implemented Idle and Moving, and am now adding Eating (which, because I considered this need when making my Vegetation System, isn’t too much of a problem), but I want to discuss the FSM as a whole and nobody can stop me, so I’m going to, temporal paradoxes be damned!
Something you’ll notice about this FSM is that a creature can’t actually decide what they’re going to do with an object until they reach it. If they see another creature but aren’t close enough to perform an action, they have to go to “moving” until they reach it. Even if they are close enough, they still have to pass through moving to get to the correct state. They can’t just go to “attack”, or to “mate”, because there’s no “moving” code in those states: they’d just stand there staring longingly at the creature they want to attack or mate with (or worse, they’d try to damage or impregnate the other creature from a distance, with no physical contact. Hmm… note to self: develop a device to do that in real life).
This directly affected the eventual structure of the behavioral system, which has undergone more iterations and revisions than Doctor Who canon: I couldn’t just give a creature a high aggression and expect it to attack. Predators need to combine a high curiosity level with a high aggression, so that they seek out other creatures and then choose to attack them.
Okay, now returning to chronology.
With the structure of an FSM set up, my randomly generated protoCreature wanders the world, going to each plant, eating it, and moving onto the next. Given a couple years, she might be able to eat the entire map (or those parts of it she can get to, anyway).
Oh heck… NON-SEQITOR SCREENSHOT LIST!
Because why not? (because I couldn’t come up with a friggin segue, that’s why not. Bah! Humbug)
Picking up where I left off…But for the other states in the finite state machine, I would need to implement a lot more than I had done. Attacking requires other creatures and a health system. Mating requires other creatures and a reproduction system, which in turn required a mutation system. Even eating was far from complete, being purely herbivorous and lacking an energy system entirely. And we’d need a system for dying when creatures ran out of health, and a system for tying physiological differences to survival traits like speed and stamina, and a per-creature-perception system, and a behavioral modification system, and a…
Wow. When you actually list them out like that, it sounds like a much larger endeavor than I thought it was at the time.
A common thread through all of these systems is that they are purely environmental: they control the universe in which the creatures live, not the survival of the creatures themselves. The creatures ability to reproduce, and thus any evolution stemming from natural selection, are emergent, not inbuilt.
Anyway, the next system on the agenda is preparing the ubervirus for release on an unsuspecting populace. If we can get a large enough infection rate in the first few days we can turn 90% of them into ravenous undead monsters by the end of- wait. Wrong agenda. Which agenda was I talking about? Oh, right, the one involving interior decoration and making everything look faaaabulous- no, not that one either?
Dammit, someone give me the secret agenda I’m supposed to be reading from! Geez!
Alright, it says here that the next system on the agenda is making large populations of randomly generated creatures, to pave the way for creature interactions.
for(int i = 0; i < 500; i++)
Well that was easy. No point doing a whole post about that. Tell you what, I’ll go move ahead and schedule something more interesting for next time.
Man I’m all over the place today! Is it possible to internally generate alchohol? Cause I think I’m doing it. God I suck so much at blogging.
Administering mandatory kick in the pants in 3… 2… 1…