*cracks knuckles..*
*goes and gets a huge pot of coffee*
*stretches a bit and yawns*..
I guess it's time to do some hardcoring.. hehe.. :)
:: Random AI ::
When you think of Random Ai what comes to Mind... Random Numbers and random actions? Random pieces of information.. Random stuff right? Well what exactly is random and how can we use it to our advantage in Flash AI.. Well..
Math.random : Creates a random number from 0 to 1 that can be changed and rounded off to equal something else.
With this little fucntion right here we can explore many different possibilities. It's like rolling a die and wondering what face will land up. You never know til you let it go. But... What use is a number from 0 to 1?! Well let's see..
Math.random()*10;
This multiplies the number you get from 0 to 1 by ten and basically states that you can have a possibility of 0 to 10. Well. That's great but you still have those numbers like 2.346 Well that's pretty weird huh? hehe.. Well let's change that to something more useful..
Math.ceil and Math.floor : Two very useful functions that will :: ceil will give you the number to the whole rounding up. 1.1 would be 2.. While the floor will give you a whole number while rounding down.
So...
Math.ceil(Math.random()*10); will return a whole number between 1 and 10 and
Math.floor(Math.random()*10); will return a whole number between 0 and 9.
So.. Now that we know how to use random.. We can use it for building AI Scripts. YaY! =)
The first thing you have to know about random AI scripts is that they are percentage based... You will have to sit down and say.. Whatall do I wnat my enemy to do.. Well.. let's take a small example.. let's say.. You are fighting a giant ape.. And you wnat him to have special attacks... Well.. let's list all the types of options he can have..
Big Ape Butt Squash
Ape Slap
Ape Roll
Ape Defense
Allright.. Well.. It will randomly go through and pick one of those right? Well Kind-a.. Now you have to choose out of 100%.. How many times do you think you'd like this to happen over the next item.. Let's say you want the ape to have a better chance of performing the Ape Slap that the Ape Roll.. But it's defense should be down low.. Well..
25% - Big Ape Butt Squash
50% - Ape Slap
15% - Ape Roll
10% - Ape Defense
------
100%
Now that we have that taken care of.. We now know that we want the ape to 50% of the time slap, 25% of the time butt squash, 15 % of the time roll and the other 10% of the time stay on defense. Well that's all great and everything.. But how do we tie up the random number thing and the percentage thing to make some big thing?!
Let's take a look at what we have.. We have a rnadom number generator that can return any whole number from 0 to whatever we specifiy and we have a list of percentages to do things. Let's say we.. Make the random number generatorcreate a random number from 0-100 and then have that choose which action to do?!
Why 100 though? Because the basics for percentages are based around 100%.
Let's take a look at this small algorithm.
PHP Code:
number = create random number from 0-100;
if(number is between 0 and 50)
{
perform the ape slap;
} else if(number is between 50 and 75)
{
perform the ape butt squash;
} else if(number is between 75 and 90)
{
perform the ape roll;
}
else
{
perform the ape defense;
}
Seems pretty simple doesn't it? Let's take a look at each of these algorithmic code sections and decide what rela coding would go into each section and what the final code would look like.
PHP Code:
number = create random number from 0-100;
This states right here that we want to have a variable that will hold a random number from 0 - 100. Well we already learned about the random fucntions so let's put that into play here..
PHP Code:
action = Math.ceil(Math.random()*100);
Well.. That wasn't so hard now was it? I didn't think so.. :) So... Now the next line of code...
PHP Code:
if(number is between 0 and 50)
{
perform the ape slap;
}
This states here that if that random number we created so happens to be in betwene the number 0 and 50 to perform this action right here..
PHP Code:
if(action >= 0 && action < 50)
{
ape.performAttack("slap");
}
Or course we didn't define a function like that but it's something that would possibly be used in a game like this. On to the next couple of lines of code.
PHP Code:
else if(number is between 50 and 75)
{
perform the ape butt squash;
} else if(number is between 75 and 90)
{
perform the ape roll;
}
I combined the next two sections because they look basically the same except with different numbers. The one states that if the random number we specified just so happens to be in between 50 and 75 that the butt squash be performed or if the action is in between 75 and 90 that the ape roll be performed. Else if basically says that.. if the first statement IF is not true then it goes on to this one and test it out again.
PHP Code:
} else if(action >= 50 && action < 75)
{
ape.performAttack("buttsquash");
} else if(action >= 75 && action <90)
{
ape.performAttack("roll");
}
Now for the last couple of lines of code.. This one tops it all off.
PHP Code:
else
{
perform the ape defense;
}
Basically this line of code says... Well If all of the other if and elseif statements fail then I will be performed. So if the number just so happens to be 90+ then it'll be left to the else statement.
PHP Code:
else
{
ape.performDefense();
}
And that's that for random AI coding.. That wasn't as hard as you guys thought it might be is it? I didn't think so... What does our final pieced together code look like and how can we use it in our game.. well here is the code ::
PHP Code:
action = Math.ceil(Math.random()*100);
if(action >= 0 && action < 50)
{
ape.performAttack("slap");
} else if(action >= 50 && action < 75)
{
ape.performAttack("buttsquash");
} else if(action >= 75 && action <90)
{
ape.performAttack("roll");
}
else
{
ape.performDefense();
}
Now how would I use a piece of code like this... I would most likely suggest creating a function and placing this inside of there.. You could have the actions then be equal to return ("attack1") or something like that to whatever called it. However you wanna use this is up to you.. But it's actually a very powerful and very easy way to add a sense of AI to your game.
Thank You and I hope this tutorial has proven to be useful.. Please send all comments and suggestions to
mentalconcepts@hotmail.com