00:00
00:00

AS: Artificial Thinking

7,762 Views | 30 Replies

AS: Artificial Thinking 2006-03-19 17:25:11

AS: Main. the source of good, evil and actionscripting
AS: Artificial thinking
related topics:
AS: Random
AS: Conditions/loops
this is about getting your mind into the computer’s mind. Making it think like you think, act stupid but not too stupid. All that crap to make give your game that edge above the competition.
getting started
every time that I’m about to code an AI there is one thing that I think of. The AI does not have a fast reaction time. so the odds of it attacking when it get’s the chance aren’t 100%. So to start of we’ll set up a variable to see whether it will attack.

onClipEvent(enterFrame){
a = int(Math.random()*5)
if(a==4){
//we’ll add what it does in here
}
}

ok. What that does is set up ‘a’ to be a random integer (whole number) every frame and if a = 4 that it will attack.
Now, we don’t want our AI to be a complete dunce and attack when you’re nowhere near it. So we’ll have to set up a variable to calculate the distance between the two objects.

onClipEvent(enterFrame){
distance=_root.enemy._x-_root.player._x
}

that will measure the enemys _x and compare it with the player’s _x to calculate how far apart they are.
but what are we supposed to do with this information?
Well, now that we’ve got the ability to check the distance between the object’s and the ability to make it attack at a random time. we’ll combine them together.

onClipEvent(enterFrame){
distance=_root.enemy._x-_root.player._x
if(distance<100 && distance>-100){
a = int(Math.random()*5)
if(a==4){
_root.enemy.gotoAndStop(attackFrame)
}
}
}

that’s all well and good but how do I make it follow?
as I am writing this I am making a game kind of like unreal flash. So I am assuming that that is the type of game you are coding. Now for this type of game the AI is going to need to get power ups and everything. So it won’t always just hunt down the player.
So what I did is make a movieclip containing all the main point’s in the level and the player’s _x. the timeline was like this:
1----- -----2----- -----3----- ----4---- -----5----- ----6----- ----7------ ----8----- -----9
point-----point------point-- ---point-----point-----point--- ---point------point---player

on every point frame I put the actions:
_root.POINTX=_x+_root.point._x
_root.POINTY=_y+_root.point._y

and then on the player’s one I had:
_root.POINTX=_root.player._x
_root.POINTY=_root.player._y

Now, this makes the POINTX and POINTY variables equal to what the point would be on the point’s movieclip.
Go onto the main timeline and click on the point’s movieclip. Give it the instance name of “points” and give it these actions:
_visible=false
gotoAndStop(int(Math.random()*_totalframes
)+1)
}

All that does is make the point’s movieclip invisible and make it go to a random frame.
Now back to the enemy movieclip. We want to make it so that he will go looking for the _root.POINTX and _root.POINTY. getting him to the _root.POINTY is not as important as the POINTX and it’s much harder to incorporate.
We’ll start off by making him run after POINTX:
onClipEvent(enterFrame){
if(_root.POINTX>this._x){
_x+=6
} else if(_root.POINTX<this._x){
_x-=6
}
if(this._x-3>_root.POINTX && this._x+3<_root.POINTX){
_root.points.gotoAndStop(int(Math.random()
*_root.points._totalframes)+1)
}
}
now to explain the code:
if(_root.POINTX>this._x){
_x+=6
} else if(_root.POINTX<this._x){
_x-=6
}
this make’s the enemy run after the pointX depending on whether it’s above it or below it.
if(this._x-3>_root.POINTX && this._x+3<_root.POINTX){
_root.points.gotoAndStop(int(Math.random()
*_root.points._totalframes)+1)
}
this part is a little more complex. It checks’ whether the pointX is greater than the enemy’s _x-3 and below the enemy’s _x+3 so that the enemy won’t have to be on the exact x co-ordinates as the point for the point to go to a random frame.
well for the point Y we want it so that if it’s smaller than the enemy’s _y that he will jump. But we don’t want him to continually jump like crazy.
So we’ll set up a variable:
j=int(Math.random()*10)
then we’ll make it so that he will jump:
if(j==6){
//thing’s to do when he jumps
}
now combining that part:

onClipEvent(enterFrame){
j=int(Math.random()*10)
if(j==6){
//jump
}
}

now to incorporate the Y point into it:
onClipEvent(enterFrame){
if(_root.POINTY<this._y){
j=int(Math.random()*10)
if(j==6){
//jump
}
}
}

That simply check’s if the Point Y is below the enemy’s _y that j will be a random integer and if it is 6 that it will jump.
now what do I do?
well you are so close to finishing your artificial intelligence but I’m going to leave you to trial/error your way to customising it. I hope that you learnt something and that you can soon get your brain into an artificial intelligence. Thankyou and goodnight.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

Response to AS: Artificial Thinking 2006-03-19 17:28:34

Nice tutorial. I used this in my Pico day game for random attacks form the enemy. 4 attacks and one standing still position. Worked pretty nicely =) Good job on this.

Response to AS: Artificial Thinking 2006-03-19 17:28:39

is this a tutorial, or are you asking for help?

Response to AS: Artificial Thinking 2006-03-19 17:30:04

At 3/19/06 05:28 PM, pwnageFL8studios wrote: is this a tutorial, or are you asking for help?

hahaha, you obviously don't know what AS: Main is, yes this is a tutorial...

Response to AS: Artificial Thinking 2006-03-19 17:31:42

At 3/19/06 05:25 PM, -Vengeance- wrote: a = int(Math.random()*5)

2 threads on AI, both with stupid methods on random integers.

random(5). simple. effective. easy to write.

snyggys

Response to AS: Artificial Thinking 2006-03-19 17:43:10

At 3/19/06 05:35 PM, SpamBurger wrote:
At 3/19/06 05:31 PM, 2k_rammerizkool wrote:
At 3/19/06 05:25 PM, -Vengeance- wrote: a = int(Math.random()*5)
2 threads on AI, both with stupid methods on random integers.

random(5). simple. effective. easy to write.
random() will be completely deprecated in AS3 ;)

but since it isn't here yet, we're just gonna have to get random integers with random()

:P

snyggys

Response to AS: Artificial Thinking 2006-03-19 17:43:53

At 3/19/06 05:28 PM, True_Darkness wrote: Nice tutorial. I used this in my Pico day game for random attacks form the enemy. 4 attacks and one standing still position. Worked pretty nicely =) Good job on this.

Thanks, i'm using it for a game i'm working on and i thought i may as well share it with the rest of the flash forum community.

At 3/19/06 05:28 PM, pwnageFL8studios wrote: is this a tutorial, or are you asking for help?

if you had read it you would have known that this is a tutorial to teach you how to get into the 'mind' of an artificial intelligence.

At 3/19/06 05:31 PM 2k_rammerizkool wrote:

2 threads on AI, both with stupid methods on random integers.

since when is correct stupid?
random(5). simple. effective. easy to write.
true, but is fully deprecated in AS3.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

Response to AS: Artificial Thinking 2006-03-19 17:47:57

like i said in other one (although this is better)

using random() doesnt make it AI, it makes it the opposite, instead of 'thinking' as the title of the thread says etc. its just a random system, there is no 'thinking'

proper AI requires a decision to be made upon existing knowledge, experiances and current enviromental arrangement and factors. (Although you probably wont have all of these, since this would make a 'perfect' AI where the computer learns)

Response to AS: Artificial Thinking 2006-03-19 17:48:30

At 3/19/06 05:43 PM, -Vengeance- wrote: At 3/19/06 05:31 PM 2k_rammerizkool wrote:
2 threads on AI, both with stupid methods on random integers.
since when is correct stupid?

just because it's correct doesn't mean it's the best way.
2+2=((6-2)/4)+3
that's correct, right? but it isn't the best way, really. you could just say 2+2=4. which is like saying int(Math.random()*20) is the best way to get a random integer. FOR NOW, IN AS 2, random() is the best way.

random(5). simple. effective. easy to write.
true, but is fully deprecated in AS3.

i know >_>

snyggys

Response to AS: Artificial Thinking 2006-03-19 17:51:41

well seaming as AS3 is coming out in a few months and AS:Main seems like it's going to last around for at least a few more years, when AS3 is out and people redirect to the tutorial it wouldn't work and it would get a lot of complaint's.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

Response to AS: Artificial Thinking 2007-02-08 13:17:07

At 3/19/06 05:47 PM, dELtaluca wrote: like i said in other one (although this is better)

using random() doesnt make it AI, it makes it the opposite, instead of 'thinking' as the title of the thread says etc. its just a random system, there is no 'thinking'

proper AI requires a decision to be made upon existing knowledge, experiances and current enviromental arrangement and factors. (Although you probably wont have all of these, since this would make a 'perfect' AI where the computer learns)

You havn't been actually reading it have you? the script ALREADY uses random number selection! It just uses it in a longer way, and all that everyone is saying is that random() is faster than using int(Math.random()), but random() will be removed in AS 3 (i think)

Response to AS: Artificial Thinking 2007-02-08 13:58:18

At 2/8/07 01:17 PM, Hoeloe wrote: You havn't been actually reading it have you? the script ALREADY uses random number selection! It just uses it in a longer way, and all that everyone is saying is that random() is faster than using int(Math.random()), but random() will be removed in AS 3 (i think)

you have, absolutely no idea, what im on about, at all do you?

Response to AS: Artificial Thinking 2007-02-08 15:05:47

Randomization, fuzzy logic, finite state machines and neural networks are usually if not always used in a combination for realistic AI along with several statistical algorithms and methods. Altough you cannot do all of that in flash yet.

So if you just use randomization to dumb the AI down to a unperfect more human level its a nicer use then just have a randomly attacking bot for example, because humans do mistakes and "perfect" AI does not. :)

Response to AS: Artificial Thinking 2007-02-08 15:25:01

At 2/8/07 03:05 PM, LeechmasterB wrote: a

i have made both finite state machines, and neural networks in flash before, although ive never had a try of fuzzy logic

Response to AS: Artificial Thinking 2007-02-08 15:25:18

At 2/8/07 01:17 PM, Hoeloe wrote: You havn't been actually reading it have you? the script ALREADY uses random number selection! It just uses it in a longer way, and all that everyone is saying is that random() is faster than using int(Math.random()), but random() will be removed in AS 3 (i think)

No offense, but you are fucking stupid.
Please re-read delta's post and try ot make better sense of it so you don't look like an idiot next time :P

wtfbbqhax

Response to AS: Artificial Thinking 2007-02-08 15:32:53

Well neural networks just get too slow when having more then 20 entities connected to it. Not that it wasn't possible to do a NN it certainly is and i did it too.... but its not possible using one for a full out AI for a more sophisticated game in flash. Anyhow you mentioned having tried it too... so is there a place where i can check out your experiments with neural networks and see how yours perform?

Response to AS: Artificial Thinking 2007-02-08 15:47:50

i havnt done much in the way of neural networks, only simple ones, i havnt been looking into them for much more than a few weeks.

http://denvish.net/ulf/1170967530_neuronegrap h.php

neural network : feed foward : sigmoid function : 1 input, 6 hidden, 1 output : learning rate = 3

modelling function: y = 0.25*(cos(x.pi)sin(x.pi)+1.5)*(2-x)

using a generic class i made for an n'th layered feed foward neural network with back propogation supervised learning

Response to AS: Artificial Thinking 2007-02-08 15:51:55

Excellent example :)

Response to AS: Artificial Thinking 2007-02-08 16:01:32

well looks really good i will look at it agin wen i will start learning as...=]

Response to AS: Artificial Thinking 2007-02-08 16:10:36

Which reminds me, have you used a more efficent platform than Flash for this stuff? Because objectively it's pretty much the worst choice.
(Gotta admit, I took LB way too low. :D)

lolpatu

Response to AS: Artificial Thinking 2007-02-08 16:34:17

At 2/8/07 04:10 PM, OldGust wrote: Which reminds me, have you used a more efficent platform than Flash for this stuff? Because objectively it's pretty much the worst choice.

nope flash, 100% :p and please, dont start going on your rant about the external as compiler, yes it may compile a little faster, but tbh, no one cares :p not even me with my super heavy applications, if i want optimization that badly, id use Flex or just go to c++

Response to AS: Artificial Thinking 2007-02-09 04:37:56

Well, actually I wanted to suggest Processing. (Simplified Java platform, easier than AS even.)

Response to AS: Artificial Thinking 2007-02-09 05:33:55

WTF AI Flash Movies!

Taste my Walrus.

(it's pronounced 'Jay-Ko')

Response to AS: Artificial Thinking 2007-03-24 18:35:10

awesome tute =D

Response to AS: Artificial Thinking 2007-06-02 13:44:21

Okay, i used the second method and want to make it so that when the hero in my game swings his weapon, and the weapon hits an enemy, the enemy is destroyed. but i'm not sure what to do, so could someone help me?

Response to AS: Artificial Thinking 2007-06-02 14:01:50

not always true, Randomness is a factor in AI. However, if your attempting to create perfect ai, randomness should only be used when there is multiple ways of doin somthing, and all solutions have an equal number of positive factors, making one emthod no better than the other. In this case you should either choose the fastest exectuing, or pick a method randemly, watever.

In a more human attempt to create ai, you will possibly have all the methods you can use to achieve somthing, however, the randomness will be far greater. You wont always want the best method to be picked.

So randomness is very much used in ai.

Response to AS: Artificial Thinking 2007-06-02 14:10:26

oops, sorry, i posted on the wrong tutorial!

Response to AS: Artificial Thinking 2007-06-02 15:43:36