00:00
00:00
Newgrounds Background Image Theme

jailander1 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: A.i. 2005-06-28 14:18:38


AS: Main

Since these AS threads are very popular today, I decided to make another one. This one is pretty short and will explain the basics of A.I. (artificial intelligence). For all of you who don't know what A.I. is, it is a code that makes something follow something. For example, say you have a character who moves around with the keyboard. You can have an enemy that follows his every move. So lets start! First create a character that you want followed. Then, give him the instance name of blue. Then, create the enemy that you want to follow him. Give that enemy the instance name of red. Then, give him these actions:

onClipEvent (enterFrame) {
if (_root.blue._x>_x) {
_x += 2;
}
}
onClipEvent (enterFrame) {
if (_root.blue._x<_x) {
_x -= 2;
}
}
onClipEvent (enterFrame) {
if (_root.blue._y>_y) {
_y += 2;
}
}
onClipEvent (enterFrame) {
if (_root.blue._y<_y) {
_y -= 2;
}
}

That code does looked a little wacked but I put it into auto format and that is what it gave me! The way the code works is it checks if blue is moving in the _y or _x direction. Then, it sends red into that exact direction. You can change the speed of red by going to the _y and _x's and then changing the 2. If you want blue to be controllable, give him this action:

onClipEvent (enterFrame) {
if(Key.isDown(Key.UP)) {
_y -=5;
}
if(Key.isDown(Key.DOWN)) {
_y +=5;
}
if(Key.isDown(Key.RIGHT)) {
_x +=5;
}
if(Key.isDown(Key.LEFT)) {
_x -=5;
}
}

That is all there is to it!

Response to As: A.i. 2005-06-28 14:34:33


I have a code which does something very similar

The 'hero' MC (controllable) is Instance Named 'player' and has this code:

onClipEvent(load){spd=15;}

onClipEvent(enterFrame) {
_x-=Key.isDown(37)*spd;
_x+=Key.isDown(39)*spd;
_y-=Key.isDown(38)*spd;
_y+=Key.isDown(40)*spd;

_x-=Key.isDown(65)*spd;
_x+=Key.isDown(68)*spd;
_y-=Key.isDown(87)*spd;
_y+=Key.isDown(83)*spd;
}

This allows directional movement (including diagonals) by using either arrow or WASD keys

The 'enemy' MC needs no Instance Name, and has this code on it:

onClipEvent(load){
spd=10; //Set enemy speed
}

onClipEvent(enterFrame){
//Rotate enemy to face player
Xdiff=_parent.player._x-_x;
Ydiff=_parent.player._y-_y;
radAngle=Math.atan2(Ydiff,Xdiff);
_rotation=int((radAngle*360/(2*Math.PI))+90);
updateAfterEvent();

if(this.hitTest(_parent.player)){
//Do attack
}else{
//Move
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
}
}


- - Flash - Music - Images - -

BBS Signature

Response to As: A.i. 2005-06-28 14:39:48


Holy shit Denvish! Your code completely ownz mine! I just got PWND!

Response to As: A.i. 2005-06-28 14:43:02


Time to pwn your AIs :)

AI techniques used in most games

ktnxbai ;)

p.s.

I ususally do array based AI, it's the same as you did except instead of acting it stores the result in array cells named (move left, move right, attack, jump) and so on, then, it checks which array cell is the biggest, and acts

Response to As: A.i. 2005-07-28 11:23:20


Just thought I'd add my AI code.. it's pretty much the same as Denvishes but.. not.

Just add:

onClipEvent (enterFrame) { getX = _root.character._x; getY = _root.character._y; gotoX = (getX-_x)/_root.speed; gotoY = (getY-_y)/_root.speed; _x += gotoX; _y += gotoY;}

to a movie clip and it will follow the MC instance named 'character'. You need to declare a variable somewhere called speed.

http://img38.imagesh...php?image=ai2ll.swf
- example.

Experiment with the speed (textbox) to see how ya like it.


Sup, bitches :)

BBS Signature

Response to As: A.i. 2005-07-28 11:27:34


Lets not forget how to find distance so they know when to shoot or whatever

a = enemy._x - this._x
b = enemy._y - this._y
c = (a*a)+(b*b)
d = Math.sqrt (c)
if (d < 300) {
do whatever the fuck you want
}


- Matt, Rustyarcade.com

Response to As: A.i. 2005-07-28 11:28:09


onClipEvent(enterFrame) {
_x += (Key.isDown(39)-Key.isDown(37)+Key.isDown(
68)-Key.isDown(65))*spd;
_y += (Key.isDown(40)-Key.isDown(38)+Key.isDown(
83)-Key.isDown(87))*spd;
}
i didnt test it but it should work

Response to As: A.i. 2005-12-13 16:09:17


do you have acode for them to shoot at you? sationary and movving enemys?

Response to As: A.i. 2005-12-13 16:14:16


At 12/13/05 04:09 PM, MetroidX51 wrote: do you have acode for them to shoot at you? sationary and movving enemys?

I gave you a link in your thread.


BBS Signature

Response to As: A.i. 2006-01-02 11:12:20


hi every one..umm.. i have been working on this code for a long time now but i still can't get it to become a platform enemy code...my prob is the enemy flipes side ways can i have a code that dos not flip him....plz thanx by

onClipEvent(load){
spd=4; //Set enemy speed
}

onClipEvent(enterFrame){
//Rotate enemy to face player
Xdiff=_parent.player._x-_x;
Ydiff=_parent.player._y-_y;
radAngle=Math.atan2(Ydiff,Xdiff);
_rotation=int((radAngle*360/(2*Math.PI))+9
0);
updateAfterEvent();

if(this.hitTest(_parent.player)){
//Do attack
gotoAndStop(3);
}else{
//Move
gotoAndStop(2);

if(_rotation>360){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
}
}

Response to As: A.i. 2006-01-02 11:16:54


At 1/2/06 11:12 AM, xij wrote: hi every one..umm.. i have been working on this code for a long time now but i still can't get it to become a platform enemy code...my prob is the enemy flipes side ways can i have a code that dos not flip him....plz thanx by

random code and stuffs

May I suggest a simpler alternative? On an AI MC facing right.
onClipEvent(load){
speed = 5
}
onClipEvent(enterFrame){
if(this._x < _root.player._x){
this._xscale = 100
this._x += speed
} else if (this._x > _root.player._x){
this._xscale = -100
this._x -= speed
}
}

Obviously you can expand on that using simple operators. No need to use full rotation code with platformers. I hope that works, for you purpose.


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to As: A.i. 2006-01-02 12:41:31


Writing a good AI is far, far more than any of the stuff posted here (Except maybe the stuff on the link to the game site, I haven't read that yet). It starts from a high level conceptual knowledge of what you want a character/npc to be able to do, moves down into what steps, the works. Actualy writing the actionscript is the very bottom level. Allow me to explain:

So, for example, lets say that you're making a top-down shooter game that takes place inside a building. There are things like walls that can be shot through with a certain number of shots, walls that can't be penetrated, walls that can and can't be seen over, the works. Additionaly, you want to have easy control over all of the enemies and all of your allies from other parts of your actionscript. This calls for a complex AI structure.

It's easiest to break this down into a number of scripts, that each contain they're own code as to how to respond in a situation. This involves checking the unit's status (finding position, figuring out where walls are, calculating what enemies are in range after so many shots, current health and rotation, etcetera). Finding this information gives you a grounds to perform tests on, the fundamental apsect of an AI.

After gathering this information, the unit needs to be able to know how to respond to it, and thats where the coding comes in. In my view, this actualy falls into two levels: The AI script that you define for the unit, and your unit's programmed AI for each script it can be given. Generaly, scripts like this are provided paramaters, or other peices of information that help it do what it's supposed to. Finaly, we're ready to move into the actionscripting of it.

So, in your unit, I generaly like to work within a function that gets called every frame. That keeps code clean in the enterFrame handler. Then, I'll declare the unit's script as an array, where the 0 index contains the script name, and the following indexes contain the peices of information that the script requires:

function script(params){
//Code for the function.
}

The next fundamental part of this function is simple, a line of If/Else statements that will determine which script the unit is opperating under. Then, the code for each different script is determined by the 0 index of the 'params' array:

function script(params){
if(params[0]=="follow"){
//Code for following a target.
} else if(params[0]=="attack"){
//Code for attacking a target.
}
}

There, now you're getting somewhere. This code determines the AI script that you wrote, and acts according to which one you've selected for it. The next step is increasing code flexibility by using paramaters, like we talked about. In this method, the paramaters simply follow the script name in the provided 'params' array. Here's an example of providing a script to a unit:

onClipEvent(load){
myScript = new Array("follow", "player", 200);

function script(params){
if(params[0]=="follow"){
//Code for following a target.
} else if(params[0]=="attack"){
//Code for attacking a target.
}
}
}

onClipEvent(enterFrame){
myScript = new Array("follow", "player", 200);

script(myScript);
}

So thats the basic structure (Excuse the lack of formatting, if you would). Essentialy, you just gave the order to Follow the Player, and keep tracking him until you're within 200 pixels of him. Now, we'll take a little bit deeper look into how you'd power the code that actualy interprets the provided script, 'myScript'.

The function 'script', when it gets 'params', the first thing it does is use the ifelse to check which script is being opperated on (In this case, the 'follow' script). After that, the code still has access to the other array indexes, which hold the other paramaters for the script. From this point, you can use the kind of code that was mentioned earlier in this thread, preferably more complex/functional, though.

function script(params){
if(params[0]=="follow"){

target = eval("_root."+params[1]);
dist = params[2]/2;

if(_y<(target._y - dist)){
_y+=3;
} else if(_y>(target._y + dist){
_y-=3;
}

if(_x<(target._x - dist)){
_x+=3;
} else if(_x>(target._x + dist){
_x-=3;
}

} else if(params[0]=="attack"){
//Code for attacking a target.
}
}
}

...Or, again, prefereably more complex (didn't feel like writing a full scale follow AI). Upon completion, this script should tell the unit how to navitage around obstacles, stay out of the enemy range/line of sight, and follow the target as uniformly as possible.

From this point, you can go and craft all of your other AI scripts, which, again will be equaly or more complex. You may want to implement follow scripts, attack, defendLocation, retreive, scout, etc. The coding flexibility from this point can either have another, higher-level AI control it from another part of the file, or even give control to the user.

In the instance of user control, you could design a system of buttons, or a fun control panel or something that allowed you to control your units. Hell, you might not even have a player, just a control screen for ordering around other units on the battlefield. There are all kinds of ways to present this.

On the other hand, if your going to have your setting of AIs controlled by a higher level AI, (For example, in the case of an enemy team acting in teamwork), the higher level AI should attempt to emulate the player. Provide it with the same interface and the same types of scripts that can be given to the units, and then code it to respond in the same way that your unit does. The biggest difference here is that instead of having it act off of a script thats been set, its set of paramaters will be generated by the state of the field every frame.

This high-level AI will be in charge of monitoring the things that the human player is for the good team- access to information about units within its line of sight, making guesses about where things are based on where they were, etc. Also, like the player, it's in charge of acting on that information. In other words, the High-level AI is now responsible for assigning the AI scripts we just wrote for the units, based on the infromation it gathers tacticaly. However, like all AI, the structure of control is fairly similiar to those used by the units themselves, and theres no need to go over it again.

...So. that is what goes into writing a full-scale AI, and some day, I'll write up a fully coded explination of it. But for now, I'm out of characters, so, next time!

Response to As: A.i. 2006-01-02 13:28:20


...So is anybody going to read that, or what?

Response to As: A.i. 2006-01-02 13:41:07


thanks i've been searching for a as: AI thread for a long time now, this might come in handy! bwhaha!

Response to As: A.i. 2006-01-02 15:31:00


At 6/28/05 02:18 PM, Dancing-Thunder wrote: For all of you who don't know what A.I. is, it is a code that makes something follow something.

AI is not just making something follow something, it extends from movement, to pathfinding, to shooting enemies, to calculating correct trajectories for projjectiles, organizing packs of bots to work together etc etc etc Artificial intelligence, not just mear 2d movement on a simple empty world

Response to As: A.i. 2006-01-02 19:28:48


Agreed. The developement of AI is it's own entire game design concept, sadly, its one that alot of games lack progress in. The AI in my own game, Doomrunner was horrible. I've learned alot in 6 months, and following something is about as boring as it gets.

Response to As: A.i. 2006-03-09 13:05:36


onClipEvent (load) {
speed = 5;
}
onClipEvent (enterFrame) {
if (this._x > _root.youplayer._x+speed) {
this._x-=speed;
} else if (this._x < _root.youplayer._x-speed) {
this._x+=speed;
}
if (this._y > _root.youplayer._y+speed) {
this._y-=speed;
} else if (this._y < _root.youplayer._y-speed) {
this._y+=speed;
}
}

Left to right only:

onClipEvent (load) {
speed = 5;
}
onClipEvent (enterFrame) {
if (this._x > _root.youplayer._x+speed) {
this._x-=speed;
} else if (this._x < _root.youplayer._x-speed) {
this._x+=speed;
}}

Response to As: A.i. 2006-06-04 08:14:39


Thought i'd add a code.

This is an expansion on the first AI. Very simple, but you have a small glitch in the first code, where the AI would vibrate when the x was the same or y was the same.

So i added a "null zone" to it.

onClipEvent (enterFrame) {
if (_root.player._x-10>_x) {
_x += 1;
}
else if (_root.player._x+10<_x) {
_x -= 1;
}
if (_root.player._y-10>_y) {
_y += 1;
}
else if (_root.player._y+10<_y) {
_y -= 1;
}
}


SIG YOINK!

BBS Signature

Response to As: A.i. 2006-06-04 08:57:25


At 1/2/06 01:28 PM, -Defrag- wrote: ...So is anybody going to read that, or what?

I read it all , and I have to say you tought me alot :)
It inspired me to try an AI test in a near future

too much school work right now XD

so.. thanks -Defrag- :)


BBS Signature

Response to As: A.i. 2006-07-15 08:31:07


how do u do instance name? i forgot

Response to As: A.i. 2006-07-15 09:07:24


if u think logically u would figure out this script easily.


.

BBS Signature

Response to As: A.i. 2006-07-15 09:22:44


At 7/15/06 08:31 AM, JackToon wrote: how do u do instance name? i forgot

Voilá.

As: A.i.


BBS Signature

Response to As: A.i. 2006-07-15 10:17:32


If you want something which gives the feel of a certain level of thought, if basic, while still being relatively easy to do in Flash, you should be able to use variables based on environmental and character based factors. These could affect things from reaction times, to type of attacks most used, alignment (defensive / offensive), movement (energetic / stationary), accuracy, even ability to interact with the environment or other AIs.

All of these things can be influenced fairly simply by a few variables such as threat felt form current character, which could be built in a number of ways, current condition, which could be affected by blows by blunt weapons particularly to the head; even environmental factors such as temperature and surface. These are the starting points for a basic AI for organic characters - it isn't all about trigonometery of being able to calculate distance or angles; you also need to introduce ways in which they are influenced by the world around them, and remember: An element of randomness can often give the illusion of complexity.

All in all, AI needs not be that difficult to create in basic ways, but simply following the character robotically and intercepting objects based on trigonometery is far from it.


BBS Signature

Response to As: A.i. 2006-07-27 13:25:24


At 1/2/06 01:28 PM, -Defrag- wrote: ...So is anybody going to read that, or what?

I did. And, phew. Thanks for that Defrag. I wanna start making my own games.hehe

Response to As: A.i. 2006-07-29 16:37:51


Hmmm...This is strange, using the 2nd code, with 'hero' the enemy shoots up off the screen without me doing anything.

I tryed copying and pasting the exact code, but it hasn't worked still. Help?

Response to As: A.i. 2006-08-17 05:20:12


im making a race game, but i need to know how i can get other cars (computer controlled) to drive a round the course... i think this is AI too... :P

Response to As: A.i. 2006-08-31 19:12:19


I need help on this... Im making a platformer where the background is scrolling and the main character stays in the middle of the screen and doesnt move... How do I make a character follow HIM when I press left and right to make the bg scroll

Response to As: A.i. 2006-08-31 19:21:53


Great thread! Just what I was looking for in my upcoming game.

Thank you.