PDA

View Full Version : Artificial Intelligence



Marz
March 3rd, 2003, 02:37 AM
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 :)

upuaut
March 3rd, 2003, 03:01 AM
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. :P )

lostinbeta
March 3rd, 2003, 03:03 AM
I can't wait man. These tutorials are going to be INCREDIBLY useful.

Marz
March 3rd, 2003, 03:40 AM
*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.



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.



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..



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...



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..



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.



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.



} 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.



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.



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 ::



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

eilsoe
March 3rd, 2003, 03:59 AM
oh neato =)

gonna go experiment a bit... :beam:

Marz
March 3rd, 2003, 04:12 AM
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:



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.. 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..




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

if(action >= 0 && 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.



// 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

kode
March 3rd, 2003, 06:51 AM
great info playa !! thanks :)


by the way .. :sigh:

Math.ceil(Math.random()*10); will return a whole number between 0 and 10

Kitiara
March 3rd, 2003, 06:51 AM
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? :hat:

upuaut
March 3rd, 2003, 07:05 AM
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."

ahmed
March 3rd, 2003, 07:09 AM
nice stuff :)..
"artificial intelligence is better than none." :P

Marz
March 3rd, 2003, 01:45 PM
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

kode
March 3rd, 2003, 01:55 PM
Math.ceil(Math.random()*10); will never return 0 :sigh:

Marz
March 3rd, 2003, 01:58 PM
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

kode
March 3rd, 2003, 01:59 PM
i told you .. great info =)

i really want to read the whole thing when it's finished ;)

lostinbeta
March 3rd, 2003, 02:12 PM
Yep Playa, you wrote this around 3am...lol. I am surprised there aren't more mistakes :crazy:

Marz
March 3rd, 2003, 03:28 PM
Time to spit out tutorial number 2..

:: Distance AI ::

Out of all the Ai's you'd less suspect to be one would be this. Why would this be an AI, how to use this AI and what are some of the advantages and disadvantages of this AI system will be discussed below.

The first thing we need to know is what exactly is distance AI and how does it work? Well..

Distance AI : Is an AI system that uses locations relative to the player character (PC) and then decides on a set number of actions based on how far away the character is.

It's typically found in most beat em up games like Double Dragon and Final Fight. It can prove to be very useful in other situations as well..

Finding Distance

The easiest and best method for finding diatance is the system of relativity.

PC._x - Enemy._x;
PC._y - Enemy._y;

That would find the distance between the PC and the enemy in both the x and y directions.. Notice though.. That sometimes one of these values can be negative or can be positive. We can take care of that later then.

So we have ourselvs a set of equations finally.. But what use are they and how would we use this to our advantage.. Well.. There are two different methods we could use to change the 2 equatiosn above into something useful..

The first way is relative math.



xdistance = pc._x - enemy._x;
ydistance = pc._y - enemy._y;
distance = (xdistance + ydistance) / 2;


That would give us a well rounded number that could equal the distance.. But what's wrong with this picture.. Well if you think about it, if we take the pc and the enemy.. And draw a line in between them.. The line probably won't be horizontal or vertical all the time. It'll be angled.. So the number this system spits out will be a rounded off number. It's very useful for those types of games where getting faster is better instead of getting exact..

The next system is the exact math version..



xdistance = pc._x - enemy._x;
ydistance = pc._y - enemy._y;
distance = Math.sqrt(Math.abs((xdistance*xdistance)+(ydistanc e*ydistance)));


Wow... That's a bigger chunk of code... But why.. And what?! Huh? hehehe.. I'll explain now..

When you have an angled line you can use the Pythagorean Theorum to figure out how long that line is exactly. By taking the two sides that we know.. a(x) and b(y) and popping them in the following equation. We can figure it out then..

c<sup>2</sup> = a<sup>2</sup>+b<sup>2</sup>;
-= or =-
c = Square Root((a * a) + (b * b));

Allright. Well that's not so difficult looking then right? Well. Squareroot only deals wiht positive numbers so.. We then have to use the Math.abs function.. Also.. We needthe actual square root fucntion that we will be using.

Math.abs() : Takes the number in its argument and gives you the absolute value of it...

-20 would be 20
20 would still be 20

Math.sqrt() : Takes the number in its argument line and returns th square root of that number.

25 would be 5
100 would be 10

So.. Then to get the entire equations right..

distance = Math.sqrt(Math.abs((xdistance*xdistance)+(ydistanc e*ydistance)));

Now.. we have two basic methods for finding out the distance from your player character to the enemy.. Which is the best one to choose for your game.. Well unless you are trying o find "exact" locations. I'd suggest just using the relative equation. It'll be alot faster running and it'll save a lot of testing hassle in the long run.

Let's use the relative one for our example. Now... We have something that will give us the distance.. Woohooo.. That's what we wanted right?... Right! So let's continue on.

We can use this number to tell us how far away the enemy might be and we can also use the xdistance and ydistance values to determine if he's north, south, east or west of my character. :)

Now say I wanted my enemy to do the following actions at these distances.

Far : Throw Rocks
Mid : Defend
Close : Punch

Easy enough. Let's think about how we would do that... Well.. If the distance was over 200 pixels. That would be pretty far.. And if it was between 100 and 200 that could be classified as mid range.. And then the rest would be close range... Let's write a quick algorithm to see if we can figure this out.



xdistance = player x location - enemy x location;
ydistance = player y location - enemy y location;
distance = (xdistance + ydistance)/2;

if(distance is far ranged (200 pixels+))
{
have enemy perform the rock throw;
} else if (distance is mid range (100 px to 200 px))
{
have the enemy go into defensive position;
}
else
{
have the enemy punch your guy;
}


Now that looks fine and dandy to most people.. But then.. What do we wanna do if say.. the player is on the right and the guy is punching or throwing rocks on the left... Well.. We need to determine which way the player is in relation to the enemy.

If xdistance is a negative number. That means that the enemy's x location was higher than the players. xdistance = 200 - 300; That means that the enemy is to the right of the character.

If xdistance is a positive number. That means that the enemy's x location was lower than the players. xdistance = 200 - 100; That means that the enemy is to the left of the character.

If ydistance is a negative number. That means that the enemy's y location was higher than the players. ydistance = 200 - 300; That means that the enemy is to the bottom of the character.

If ydistance is a positive number. That means that the enemy's y location was lower than the players. ydistance = 200 - 100; That means that the enemy is to the top of the character.

Well.. Now that we know how to find out where the enemy is direction wise. We can figure out which way to have the enemy throw rocks or punch. Let's throw this in the algorithm we created above. We won't have to check y locations because we don't care whether the enemy throws it up or down right now. We just wanna have the enemy throw it either left or right. Your game may need to use top and bottom and for those cases please use the above diagrams to help you out.



xdistance = player x location - enemy x location;
ydistance = player y location - enemy y location;
distance = (xdistance + ydistance)/2;

if(distance is far ranged (200 pixels+))
{
if(xdistance is positive)
{
have enemy perform the rock throw to the right;
}
else
have enemy perform the rock throw to the left;
}
} else if (distance is mid range (100 px to 200 px))
{
have the enemy go into defensive position;
}
else
{
if(xdistance is positive)
{
have enemy throw a punch to the right;
}
else
have enemy throw a punch to the left;
}
}


Well This code surely is developing.. But like my random Ai example before. This is just an algorithm to get use where we want. The Real Code :) Let's break this up and run by with the real code.



xdistance = player x location - enemy x location;
ydistance = player y location - enemy y location;
distance = (xdistance + ydistance)/2;


I think I can just jump over this one seeing as how we discussed this over. Place either the relative distance or exact distance equations here.



xdistance = pc._x - enemy._x;
ydistance = pc._y - enemy._y;
distance = (xdistance + ydistance) / 2;


Now onward.



if(distance is far ranged (200 pixels+))
{
if(xdistance is positive)
{
have enemy perform the rock throw to the right;
}
else
have enemy perform the rock throw to the left;
}
}


We would writie this up as...



if(distance > 200)
{
if(xdistance >= 0)
{
enemy.performAttack("rockthrow", "right");
}
else
{
enemy.performAttack("rockthrow", "left");
}
}


Easy enough.. Just a couple of simple if statements and some function calls. :)



else if (distance is mid range (100 px to 200 px))
{
have the enemy go into defensive position;
}


Now into real code.



else if(distance > 100 && distance <= 200)
{
enemy.performDefense();
}


Now.. Onto the last bit of code.



else
{
if(xdistance is positive)
{
have enemy throw a punch to the right;
}
else
have enemy throw a punch to the left;
}
}


Easy enough.. This almost looks like the first one cept with an else..



else
{
if(xdistance >= 0)
{
enemy.performAttack("punch", "right");
}
else
{
enemy.performAttack("punch", "left");
}
}


And.. That leads us to throwing it all together into one nice little happy family.



xdistance = pc._x - enemy._x;
ydistance = pc._y - enemy._y;
distance = (xdistance + ydistance) / 2;

if(distance > 200)
{
if(xdistance >= 0)
{
enemy.performAttack("rockthrow", "right");
}
else
{
enemy.performAttack("rockthrow", "left");
}
}else if(distance > 100 && distance <= 200)
{
enemy.performDefense();
}
else
{
if(xdistance >= 0)
{
enemy.performAttack("punch", "right");
}
else
{
enemy.performAttack("punch", "left");
}
}


Well everyone.. That's the final piece of code... Not too bad huh? It's funny how something so advanced can amount to around 20-40 lines of code.. Funny huh? Well.. Like the Random AI you can use this in many different ways. You can use this in a function or you can just plop this in your code like it is. Either way, please comment me back with as much info as possible :)

Thanks and Good Day
Marz
mentalconcepts@hotmail.com

Jubba
March 3rd, 2003, 03:34 PM
Awesome Marz... man, when did you get motivated to actually do something constructive? lol :P :trout:

Marz
March 3rd, 2003, 03:37 PM
:trout::trout:

Heck if I know.. Just be glad :-p *lol*

lostinbeta
March 3rd, 2003, 03:39 PM
Originally posted by Jubba
Awesome Marz... man, when did you get motivated to actually do something constructive? lol :P :trout:

Marz is always productive. He helps out a lot on the forum (-:

And... niiiiice.

nobody
March 3rd, 2003, 04:15 PM
wow.. this stuff is amazing... i would never have thought to do a lot of these things.. but i am just getting into actionscript, so what do you expect?
i really wanna try out some of these things once i get better..

in other words

great job!:A+:

Guig0
March 3rd, 2003, 04:27 PM
you´re really doing something great for us, thanks a lot dude! =)

Marz
March 3rd, 2003, 08:54 PM
Thanks.. I'm just glad that I can help sometime....

:):-\

Marz
March 4th, 2003, 02:00 AM
I was gonna spit out another tutorial but my head is aching... Had to help a gal friend paint her room tonight and I got a headache off those god awful fumes.. Blah../

Sorry guys :p

sintax321
March 4th, 2003, 12:25 PM
This thread should be striped of spam and made a sticky or something. Really good stuff marz

lostinbeta
March 4th, 2003, 01:16 PM
It will be stripped of spam and moved to the best of kirupa section.

sintax321
March 4th, 2003, 01:16 PM
Cool. That will make it much more handy to read and use.

Marz
March 10th, 2003, 04:32 PM
Allright now that he ahs the new forums up.. I'll start posting the Logical AI tutorials up...

Don't know when.. Tongiht? Tomorrow?

Jubba
March 10th, 2003, 05:08 PM
:beam:

pom
March 19th, 2003, 05:33 AM
Very interesting, Playa :)

Just as a side-note, when you compute the distance, you don't really have to take the square root of the value you have. It takes time, and you could perefctly compare the square of the distance in your AI thinggy afterwards.
//instead of testing
if(distance > 200)
//you could test this to get rid of the Math.sqrt
if(distance² > 40000)pom :)

raichu
May 17th, 2003, 02:13 AM
think u could make a DL file? it's erm id uno how it works...

Jubba
May 17th, 2003, 02:16 AM
This is just teaching you the concept. There wouldn't be a file to download.

hojo
May 17th, 2003, 12:03 PM
i think these would be alot more useful for me if i knew what the hell most of that meant :-\

Marz
May 17th, 2003, 03:11 PM
*lmao*.... Man.. I almost forgot I made these sections.. Hell I have the AI notes laying around my room somewhere... Probably shoved in some random computer book... Wouldn't doubt it..

**** jobs have been making me tired and my lack of mental concentration lately is disturbing.

playamarz

andr.in
May 17th, 2003, 03:53 PM
Don't have time to read posts except the first one but I know that this is the as of the gods and will come in handy for sure.. one day! :)

Marz
May 17th, 2003, 11:19 PM
Yeah man.. I hope it is... I love this kind of stuff...

playamarz :player:

Marz
December 22nd, 2003, 06:06 PM
Ohh Hot ****... I never finished off these tutorials..

I've gotta get my laptop up to GNC so I can start pumping these things out.... :)

Still.. here are some tutorials on Basic AI.. I will start the others then ;)

Marz
December 29th, 2003, 11:05 PM
Long overdue... And has been in production for a while now....

I now bring you...!!

:: Logical AI ::

Logical AI is a way of making an Artificial Intelligence for such games like Tic-Tac-Toe or Chess. It gives the computer the most sense of realism that any game can have and... is one of the hardest to program for as well.

You have to put yourself in the control set of a computer. How would you perform if you could only do things by doing tests and then checking out these tests against a set of equations and logarithms?

Now.. it gets ten times harder by doing this over the course of time by saying that your AI needs to be able to think into the future to make sure it isn't doing a bad move for a later reference. But... At the same time, during all of these processes, you still wnat to make the computer beatbale, so ou have him screw up every so often.. And if the player doesn't catch that screw up, then he will most likely lose.

Wow.. We are in for a big treat now. But the game I'm doing won't be tic-tac-toe.. nor will it be checkers or chess. The game AI I plan on tlaking about today will be lines. You've all probably played lines at least once. It's that fun game where you have a grid of dots. And you and another friend (imaginary or real ;) ) would take turns drawing lines, trying to form boxes out of the lines. When you closed off a box, you got to take another turn and vice versa. Person with the most blocks after all lines have been used up WINS!

Now... This seems rather easy.. But where do we start... First off.. Let's see what we will need to get this thing going..

We will start this thing off by making ourselves a grid, like such.

http://www.mentalconcepts.com/game/dotz.jpg

Allright. Sorry about the intermission folks... Was getting lightheaded at the office, so I decided to come here and relax for a little bit... **** flu.. Anyways.. Back to what i was pointing out.

We have ourselves a grid here. Ths is a rather small grid for a game of lines, but it will suffice to give us all the information that we shall need :) Using this grid we shall need to find out how many lines we can draw on here to reach the maximum number of lines being drawn.

http://www.mentalconcepts.com/game/linenumbers.jpg

By the looks of it. We can have a total of 24 lines possible. With a total of 4 x 4 or 16 dots. Now.. What if it were a 3 x 3 grid of dotz.. How many lines would you have then? We will have to set up an equation for us to figue this out for al possible grid sizes.

How do you get 24 out of a 4 x 4 grid ? Or.. How do we get 12 out of a 3 x 3 grid?! Simple...

Number of Lines = rows * ((rows - 1) + (cols - 1));

So....

5 x 5 : Numlines = 5 * ((5 - 1) + (5 - 1)) : 5 * (4 + 4) : 5 * 8 : 40
4 x 4 : Numlines = 4 * ((4 - 1) + (4 - 1)) : 4 * (3 + 3) : 4 * 6 : 24
3 x 3 : Numlines = 3 * ((3 - 1) + (3 - 1)) : 3 * (2 + 2) : 3 * 4 : 12

So.. We now have an equation to figure out how many lines a particular grid can hold and this comes to play when we are testing about combos and different line combo testing.

Now.. We need to come up with a process of figuring out which one of these lines are open or not open. What better way to do this than an array?! It can hold a certain amount of data and it can be checked in a very fast manner as well!

lineHolder[] = open [1] or not open [0]

lineHolder[0] = 0
lineHolder[1] = 1
lineHolder[2] = 1
lineHolder[3] = 0

So... We have ourselves a nice establised system with how we can check if a line is availible or not and how many lines there are. Let's get to cracking on how we can tell if placing a line is a good thing or not. :)

Let's go through some of the possibilities of the line game through a series of small images...

http://www.mentalconcepts.com/game/line1.jpg

Clear field, you can place a line anywhere on the field and not have to worry about it. These lines should be labaled as the possibility of the greatest move, or a good move.

http://www.mentalconcepts.com/game/line2.jpg

Not so clear of a field, but the list of possibilities are still very good. All moves in this catagory would still rank as a good move, except for the lines located in the same box set as the line. In which case, you are setting yourself closer to a box, which can possibly make that a bad move as well as a good move, so we dub this move as a good move at times....

http://www.mentalconcepts.com/game/line3.jpg

This is the worst of all cases, the only moves available are a bad move. This is the worst possible move available and should ONLY be done, if it is the last availible move.

http://www.mentalconcepts.com/game/line4.jpg

This, has the best move possible available to the computer player, this SHOULD be TAKEN at all costs.. This will give you a space for yourself and add an extra move for the next turn.

So.. Now we have all of the possible moves available to the computer player.. Let's give em each numbers and place them on a chart...

Image 4 can be classified as the best possible move, so we will give this the number 3. Image 3 can be classified as the worst possible move, so we classify this as 0. The first image is a better option than the second image. So it will be set as 2 and the second image as 1.

So.. With four options... We will have to check what I would like to call as a combo set. A combo set is a set of 4 dots, that if all were joined by lines... they would form a box. We need to set an equation with the lineHolders that we have to be able to go through all of the combo sets to check and make sure the above number system for possible moves can be reached.

Now... I'd hate to stop this now.. But it's getting late and I need some shut eye.. I'll cover the final bit of this, possibly tomorrow :)

SeiferTim
January 8th, 2004, 03:27 PM
Originally posted by Kitiara
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? :hat:

This should be made official! This is very, very awesome! Great work, Marz! Wanna come work for me? :thumb:

mooskating
July 13th, 2004, 11:39 AM
Hey, can one of you global mods or admins please make another topic for the replies. It's harder to read when all of it's in a different spot. lol, Just a suggestion, you obviously don't need to listen to me.

Nice Marz. You're like, really good! *lol*, How did you guys get all of these people to your forum? Spamming other sites? Sites you never wanted to join, but joined to spam and get banned? lol man, thats fun, I don't get why people complain, it's not like they can possibly use all of their storage up anyways. I wanna start doing that! Is it against the terms of service here?

elPooter
July 17th, 2004, 12:36 PM
great AI tute :D

dal platinum
July 20th, 2004, 08:19 AM
nice work. I was really after the pathfinding tut though :(

jerez_z
July 20th, 2004, 11:06 AM
ya me too..... I worked at it for a week and this is the best I got: SWF (http://www30.brinkster.com/funded/pathfinding3.swf)

I think I'm going about it all wrong....

NOTE: Key down to turn on adjust mode, then click and drag to change the squares. Key down again to turn it off.

Marz
August 31st, 2004, 04:58 AM
ya me too..... I worked at it for a week and this is the best I got: SWF (http://www30.brinkster.com/funded/pathfinding3.swf)

I think I'm going about it all wrong....

NOTE: Key down to turn on adjust mode, then click and drag to change the squares. Key down again to turn it off.

Wow...... Just like the rest of my stuff.... Everything kind-a faded away...

Well I finally got my laptop up and running and a stable internet connection at the house... I'll see what I can do about sprucing up the AI and Video Game Programming tutorials... And maybe actually finishing up some rudimentary stuff for all of this....

I'll *try* and start throwing some things up mid-September... We shall see... :)

tommythewolfboy
September 14th, 2004, 05:43 AM
All good stuff so far - thanks for your efforts Marz. Looking forward to some future updates if you get the chance :¬)

t

Marz
September 15th, 2004, 12:57 AM
Thanks man... :) I look forward to updating these when I have time.

Skribble
September 15th, 2004, 08:16 AM
yeh marz ur awesome. I knew jack all about AI less than a month ago. but i read through ur tuts and i understood the concept straight away and now i use the method for a ton of other stuff as well.

great job and look forward to seeing sum more in the future =D

JoMan
September 15th, 2004, 10:59 AM
yeh marz ur awesome. I knew jack all about AI less than a month ago. but i read through ur tuts and i understood the concept straight away and now i use the method for a ton of other stuff as well.

great job and look forward to seeing sum more in the future =D
I couldnt agree more :D. Hey Marz, you should put a tutorial up on the tutorial section =).

Marz
September 17th, 2004, 03:21 AM
I might just do that.... With the laptop around.. it's easier to get some **** done... I might have to update the old tutorials with some new mx2004 class nodes or something like that too... Who knows? :) I'm working on an inventory system right now before I do the rest of these.. :) thanks guys.. Muy appreciano.

Commandersa
September 17th, 2004, 07:51 PM
Really awesome tutorials. I understood the whole thing so easily. You write good tutorials. I can't wait for the others.

ozzy_fan666
September 18th, 2004, 01:59 AM
These tutorials are a gift from above. I can finally understand AI thanks to Marz.

Marz
September 18th, 2004, 03:46 AM
*blush* Thanks guys... I'll be sure to format these ones better so that they are even easier to read... And I'll try building some examples too... I have a guy willing to do some graphics for my tutorials.. so that'll be nice :)

Look forward to more tutes guys... And if you ever need help... Post of send it to info@codedaddy.com

Mazzy
September 23rd, 2004, 08:22 AM
How do u make it so, thats say your character has a sword and you what him to chop the enemy up what would you do hittest

Dr Warm
September 23rd, 2004, 09:06 AM
Was that a question?!? i'm an aussie and i didn't understand it! do u want to know if when u hit an enemy with a sword he dies, cause that isn't relevant to this thread, i suggest u make a new one

Skinny_T
October 11th, 2004, 10:17 PM
Great work Marz...so far. I know your a busy man and all but you promised 5 and so far we've seen 3. Instead of having to constantly postup questions about ai I was gonna use this thread to learn all I need to know but it seems I cant do that until its finished.

Dr Warm
October 12th, 2004, 03:40 AM
Man everyones so demanding ;) (not saying i'm not). but i agree with skinny_t, we want more, we want more, rah rah rah etc!!!

Marz
October 12th, 2004, 04:36 AM
Yeah yeah yeah.... Well I've been through a harsh 3 weeks... Lost my main job, quit my business, getting kicked out of my house and multiple other things. I will get back to these as soon as possible.

Dr Warm
October 12th, 2004, 08:32 AM
jeez, i'd say thats a pretty good excuse, hope u'll manage through your tough times

tommythewolfboy
October 12th, 2004, 08:43 AM
Hope things look up for you Marz. Sounds like you MUST be due a lucky break soon though ... :¬)

Marz
October 13th, 2004, 11:42 PM
I sure as hell hope so... *lol*... :)

zendo49
November 1st, 2004, 09:52 PM
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:



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.. 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..




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

if(action >= 0 && 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.



// 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



I am not understanding what you are saying do u think you could make a simple fla file for me to look at if you could thanks so much
zendo49

Steveo
November 9th, 2004, 07:31 AM
hey marz,

these are great. only just found them today. like a few others - im looking forward to the pathfinding tut. keep em coming:)

signifer123
December 25th, 2004, 11:20 AM
Did yuo forget this i was looking for your pathfinding tut or are you just taking your time :P

JoMan
December 29th, 2004, 12:55 AM
Why does this thread only have four stars? It should have a five star rating!

peace

Marz
January 12th, 2005, 11:26 PM
Like always.... I try to work on tutorials and it never happens... Working so much to make a living and trying to keep my sanity evades my presence in this place.... Which is a shame because I have so much brainwork to offer.... Just no time to do it... *sigh*....

I doubt I'll ever get to do the pathfinding tutorial... But you never know... Maybe if I can sneak my laptop into work more often and do it there I might have a better chance... I guess we shall see how it all evolves...

*sigh*

Sammo
January 14th, 2005, 01:10 PM
Thanks Marz, can't wait :)

JoMan
January 14th, 2005, 02:42 PM
Like always.... I try to work on tutorials and it never happens... Working so much to make a living and trying to keep my sanity evades my presence in this place.... Which is a shame because I have so much brainwork to offer.... Just no time to do it... *sigh*....

I doubt I'll ever get to do the pathfinding tutorial... But you never know... Maybe if I can sneak my laptop into work more often and do it there I might have a better chance... I guess we shall see how it all evolves...

*sigh*
No worries ;). Being out and in the real world, is a better use of time than sitting on the com anyways.

peace

marcelozep
January 25th, 2005, 02:05 PM
OKOK
But what about a racing game? Top viewed? You can see all the track and the motorcycles. It has the user motorcycle (already programmed with acceleration, rotation reverse speed etc) and the other three motorcycles. How to program they?
Theres an example in my post in game/AI, the topic of the post is "Hi masters of game programming", its an swf, take a look its exactly what I want (but my game is with motorcycle, its the only diference).

Thanks
marcelozep

Marz
January 25th, 2005, 10:28 PM
Allright... Allright... After re-evaluating what I have up here already.... And seeing as how I lost my job again... *sighs*... I will start from the beginning and writ e a new diagram on paper on what routes I'm going to take.... Dividing up on how to use each A.I. type in which game and such and such.... And covering some special A.I. sets that can't be governed by the main five...

I will produce a guideline on what I will be producing in a matter of days.

See ya guys,
MarZ

signifer123
January 26th, 2005, 08:27 AM
YAY, so you are going ot do the pathfinding tut?

Marz
January 26th, 2005, 10:09 PM
In a way, yes... I plan on coverring more of a wider spectrum though... Instead of just teaching the basics.... I'll start going into game types...

For instance...

A top view perspective racing game would use the following types of A.I.

- Random
- Distance
- Direction
- Pathfinding - Waypoints

So... You see what I mean? Now... What are some types of Artificial Intelligence modules then... Well..

1. Random
2. Direction
3. Distance
4. Logical
5. Mathematical
6. Pathfinding
a. waypoints
b. navigational


These are just a few of the A.I. modules that I could come up with off the top of my head... But you get the point now.

Anyways... Going out tonight to have a drink and think about some things. Have a good night guys.

JoMan
January 27th, 2005, 01:16 AM
Enjoy your drink, and I hope you get your job back:beer:!

peace

NiñoScript
July 19th, 2005, 11:38 AM
and?
have you been 7 months drinking that beer, or what?
:P

i just would like to see your job finished :)

Marz
July 21st, 2005, 11:23 PM
*lol*... It would be nice, wouldn't it...

No, I finally got my computer back and running after a couple of problems here and there (hard drive failures and memory corruption... along with having problems with my own life)... I'll have to relook over my tutorials and see what I can offer, especially since I'm on my own again... Don't have to worry about some woman bugging me :D

NiñoScript
July 23rd, 2005, 01:16 AM
along with having problems with my own life

Don't have to worry about some woman bugging me

lol

Insane Knux
July 23rd, 2005, 01:43 AM
Especially since I'm on my own again... Don't have to worry about some woman bugging me.:D
Is that good or not?

Marz
July 24th, 2005, 11:51 AM
It's a very good thing for my situation.. Single-mingle life is the way for me right now.

GPP
November 20th, 2005, 12:44 AM
Ive been waiting for a while.. Marz u eva gonna finish this ?

aussiebloke
November 20th, 2005, 01:02 AM
lol yer these are some good tutes? wouldnt mind some more!

NiñoScript
November 20th, 2005, 09:01 AM
maybe its another 7 months beer... :lol:

GPP
November 20th, 2005, 06:32 PM
Yeah its been like 2 years scince he hasnt finished these lol

Ravmaster
November 26th, 2005, 01:16 PM
O_O i need these they are awesome :D and very helpful :D

GPP
November 26th, 2005, 04:37 PM
They are hella betta then awsome you can learn to program the greatest of games with this sutf

darkmikey
August 19th, 2006, 07:25 AM
Please finish these soon!;(

Lord Impero
August 19th, 2006, 10:26 PM
Last Activity: 06-26-2006 11:49 AM
Ouch!:|
BTW i want to see it done too!

Timmytots
August 26th, 2006, 09:56 AM
for somebody so clever, you're an awful speller.

Sphynxinator
June 21st, 2007, 07:12 PM
Marz, can you send a fla. and .swf please?

aiMarz
January 13th, 2008, 12:06 AM
for somebody so clever, you're an awful speller.
I'm not an awful speller at all actually. Just a rather quick typer and if you can get the messages out of my posts that I intend to deliver than I'm not going to bother going through the entire thing and spell-check everything. Now of course, that was before I started using FireFox with its automatic spell-checker!

Haha... Anyways, it does seems like it has been a long time since I've pulled up these rusty old tutorials, I'm hoping that they are still be used by everyone who needs them. I'm hoping to get a little more active now and get a couple more posts up here and there but I guess we shall see.

Marz
January 14th, 2008, 02:29 PM
Wow... SO yeah. After looking over everythign I wrote. I am supremely disgusted with how much I have lacked in helping in the past 3 years. Couldn't help most of it though but I plan on changing that now.

I will say one thing though. I will be restructuring everything in the A.I. Tutorials Section and in the Game Programming section but I'm not saying when or how I'm going to do this. Need to do a little more research into the new data that is being presented by AS3.0 and do some of my own research in the time being.

And to answer the person that was asking about an AS School I was actually hoping to comment on that. I had a huge number of posts and pm's asking me about it and I have been looking into it. The problem is for me right now, figuring out time slots and other schedules. Once I do that, the rest is a breeze.

Stevai0
January 15th, 2008, 10:49 PM
mmm i would LOVE to see the pathfinding stuff! using a*?

youngtrill
June 9th, 2008, 05:32 AM
how would i imply these into tonypas system of tiled based enemy