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!