Go Back   kirupaForum > Flash > Game/AI Programming

Reply
 
Thread Tools Display Modes
Old 03-03-2003, 01:37 AM   #1
Marz
Artificially Respawned
 
Marz's Avatar
Location Shippensburg, PA

Posts 1,891
Artificial Intelligence

Ahh... This is where I shall be slamming out my AI tutorials.. I'm guessing it'll probably get moved to MX or As or somewhere.. But for now.. This works.

Welcome to AI 101.. I will be writing a total of 5 articles on AI. or Artificial Intelligence. AI is one the hardest things to code in a game. You have to sit down and think about how you wnat to incorporate AI in your game and how it will effect. Do you wnat the computer to be really dumb or do you wnat it to mimick as much of the human brain as you can? It's a possibility to look into..

I will be discussing 5 possible ways to place AI in your game or program.

Random AI : I will be discussing this tonight.. But this uses the random function to determine what move is next and what the computer will do next with chance.

Distance AI : This is the act of using distance forumals to determine what an enemy or object should do.

Logical AI : Checkers, Chess, and Tic-Tac-Toe are just some of the games that use logical math... It steps up about 20 notches to see what will happen in the future to determine what it'll do next.

Pathfinding AI : This should be a subject of it's own.. it's a small version of how you can make a character think when doing pathfinding.

Math AI : How to use Math to your advantage when doing Artificial Intelligence.. TEaches you some of the basic math functions that will help your ai scripts look more advanced but be faster.

And Now.. I will reply with the Random AI tutorial. Thanks for reading these.. I'd appreaciate only collective criticism or friendly responses to thes tutorials

__________________
aiMarz : v. 7.03a - Current Version is : Mildly Disturbed
Marz is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?

Old 03-03-2003, 02:01 AM   #2
upuaut
Amber's last scion
 
upuaut's Avatar
gods above.. thanks Playmarz. We need some good posts about this subject. I look forward to reading your thoughts on the subject. (you're right. it will probebly be moved to MX relatively quickly.. but for now it works. )

__________________
do you miss "ordered"?
upuaut is offline   Reply With Quote
Old 03-03-2003, 02:03 AM   #3
lostinbeta
Halcyon
 
lostinbeta's Avatar
Location Woodbridge, VA

Posts 17,476
I can't wait man. These tutorials are going to be INCREDIBLY useful.

__________________

Meditation Room | Take a deep breath...
lostinbeta is offline   Reply With Quote
Old 03-03-2003, 02:40 AM   #4
Marz
Artificially Respawned
 
Marz's Avatar
Location Shippensburg, PA

Posts 1,891
*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 >= && 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 >= && 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

__________________
aiMarz : v. 7.03a - Current Version is : Mildly Disturbed

Last edited by Marz; 05-14-2004 at 02:08 AM..
Marz is offline   Reply With Quote
Old 03-03-2003, 02:59 AM   #5
eilsoe
mmm, coffee
 
eilsoe's Avatar
Location DK

Posts 6,106
oh neato

gonna go experiment a bit...

__________________
site still under construction... going on 6 years now.
eilsoe is offline   Reply With Quote
Old 03-03-2003, 03:12 AM   #6
Marz
Artificially Respawned
 
Marz's Avatar
Location Shippensburg, PA

Posts 1,891
Addendum to Random AI

It seems I should explain myself more on how to use this code in say... Your main code.. Let's take the example I used above:

PHP Code:
action Math.ceil(Math.random()*100);

if(
action >= && 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.. We have ourselves a solid piece of code.. How would we use this in a game perchance though.. Well.. For one thing.. You wouldn't want the character to perform an action every single frame.. In a 30 fps game that would be a totla of 30 actions every second.. So how would be use this in a more maneagable way.. Well.. First off it all depends on the game.. I'll go over one possible way.. The role playing game way..

With role playing games and fight scenes.. You would have a wait bar that woud have to load up before you could perform the action.. Well.. Let's take this snipplet of code we have and change it into soemthing a role player would most definetly be able to use...

We would first need to create a timer varaible to hold our wait timers...

waitTimer = 180; (30 fps that would be 6 seconds)

We would then have an if statement thast checked to see if the waitTimer was down to 0.. If it was it would do soemthing and if it wasn't.. It would just decrease the timer by 1 every frame until it was equal to 0. But. before we really get heavily into this.. What action would it be claling upon?! Well...

We need to convert what we created above into a nice fucntion... Let's call it..

performAction();

Now perform action will be handled by a monster and so that monster's movieclip will need to be factored in with this.. And if we have multiple player targets like most rpg's do.. We will need that as well.. Soo..

MovieClip.prototype.performAction(targetPC);

That states that we will be performing an action with the MovieClip we specify on the target player character we provide. So..

PHP Code:

MovieClip
.prototype.performAction(targetPC)
{
   
action Math.ceil(Math.random()*100);

   if(
action >= && action 50)
   {
      
this.performAttack("slap"targetPC);
   } else if(
action >= 50 && action 75)
   {
      
this.performAttack("buttsquash"targetPC);
   } else if(
action >= 75 && action <90)
   {
      
this.performAttack("roll"targetPC);
   }
   else
   {
      
this.performDefense();
   }

Now we can just take the timer we were tlaking about before.. Set it so that every 6 seconds the ape will perform an action depending on what the random scores show. Plus we will have it choose a random character to attack.

PHP Code:
// This will be placed in an onLoad event handler some where.
mainTimer 180;

// The rest is handled by the onEnterFrame handlers

_root.onEnterFrame
{
   if(
mainTimer == 0)
   {
      
character Math.ceil(Math.random()*numPCS);
      
ape.performAction(_root["pc"+character]);
      
mainTimer 180;
   }
   else
   {
      
mainTimer--;
   }

And that's all folks.. That's how you would place a small snipplet of Random Ai code in an RPG type game.. or course there are alot more things you'd have to consider when doign this but for the sake of leaving that up to other tutorials and sticking with the subject I won't talk about them here..

Take Care and Good Luck
Marz : gameprogramming@mentalconcepts.com

__________________
aiMarz : v. 7.03a - Current Version is : Mildly Disturbed

Last edited by Marz; 05-14-2004 at 02:09 AM..
Marz is offline   Reply With Quote
Old 03-03-2003, 05:51 AM   #7
kode
enkoded
 
kode's Avatar
great info playa !! thanks


by the way ..
Quote:
Math.ceil(Math.random()*10); will return a whole number between 0 and 10
kode is offline   Reply With Quote
Old 03-03-2003, 05:51 AM   #8
Kitiara
Rolled natural 20
 
Kitiara's Avatar
Location Purgatory (UK) Bans: 3               Unbans: 2        

Posts 5,309
Could we get these things in a proper Kirupa tutorial set? I think that a lot of people would want to read them (myself included) so putting them on the main site seems like a good idea to me.

Maybe we should ask the boss man?

__________________
- Kit
Kitiara is offline   Reply With Quote
Old 03-03-2003, 06:05 AM   #9
upuaut
Amber's last scion
 
upuaut's Avatar
I'll work with Playmarz on it. I think he just wanted to write "freeform" hence the location of the post. We'll get them all formated once he's got everything down.








"artificial intelligence is better than none."

__________________
do you miss "ordered"?
upuaut is offline   Reply With Quote
Old 03-03-2003, 06:09 AM   #10
ahmed
lorem ipsum
Location canada

Posts 3,811
nice stuff ..
Quote:
"artificial intelligence is better than none."

__________________
ahmedabbas.com
ahmed is offline   Reply With Quote
Old 03-03-2003, 12:45 PM   #11
Marz
Artificially Respawned
 
Marz's Avatar
Location Shippensburg, PA

Posts 1,891
I can convert these all to the format then.. But for now.. I just wanted to like david stated "freeform" these..

If we ever get these on the main, we can delete these posts then



And btw KAx.. I did have that down.. Are you asking why it's still 0? Because if I'm thinking.. 0*10 would be 0 then... *shrugs*.. it might make a random number in between and not on those numbers... Hmm... :-p

__________________
aiMarz : v. 7.03a - Current Version is : Mildly Disturbed

Last edited by Marz; 03-03-2003 at 12:48 PM..
Marz is offline   Reply With Quote
Old 03-03-2003, 12:55 PM   #12
kode
enkoded
 
kode's Avatar
Math.ceil(Math.random()*10); will never return 0
kode is offline   Reply With Quote
Old 03-03-2003, 12:58 PM   #13
Marz
Artificially Respawned
 
Marz's Avatar
Location Shippensburg, PA

Posts 1,891
Allright.. Yeha I wasn't too sure about that one... Okay I changed it.. Thanks kax.

Gimme some creds.. it was late last night I'm bound to make some mistakes :-p heh

__________________
aiMarz : v. 7.03a - Current Version is : Mildly Disturbed
Marz is offline   Reply With Quote
Old 03-03-2003, 12:59 PM   #14
kode
enkoded
 
kode's Avatar
i told you .. great info

i really want to read the whole thing when it's finished
kode is offline   Reply With Quote
Old 03-03-2003, 01:12 PM   #15
lostinbeta
Halcyon
 
lostinbeta's Avatar
Location Woodbridge, VA

Posts 17,476
Yep Playa, you wrote this around 3am...lol. I am surprised there aren't more mistakes

__________________

Meditation Room | Take a deep breath...
lostinbeta is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:47 PM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com