Practical.Artificial.Intelligence.Programming.in.AS3 - yangboz/godpaper GitHub Wiki
Version 0.00, last updated June 11, 2013. by Knight Zhou(Yangbo).
This work is licensed under the Creative Commons Attribution-NoDerivs-NonCommercial License.
To view a copy of this license, visit http://creativecommons.org/licenses/byndnc/1.0
Additional license terms:
This book was written for both professional AS3 programmers and chess game players. I have try to write this book, my first book in English to strive to improve my poor English and in-confidence of my heart(heart innocence to who supported me, inspired me, those code gurus that lighten the road to success).
" To every Jane and Joe programmer,chained to their computer, burning the midnight oil,striving to make a dream come true! "
#Chapter 1.Finite State Machine
FSM is one of the most simplest and most basic AI models.
##Model explanation:
A finite state machine is a device,or a model device,which has a finite number of states it can be in at any given time and can operate on input to either make transitions from one state to another or to cause an output or action to take place.A finite state machine can only be in one state at any moment in time.
##Traditional explanation:
A finite state machine (FSM) or finite state automaton (plural: automata), or simply a state machine, is a model of behavior composed of a finite number of states, transitions between those states, and actions. It is similar to a "flow graph" where we can inspect the way in which the logic runs when certain conditions are met. A finite state machine is an abstract model of a machine with a primitive internal memory.
##Chapter 1.1 Introduction
Games need an AI architecture that supports the straightforward creation and reuse of complex character behaviors.
Since most games rely on custom scripts for non-player characters (NPCs), only plot-essential NPCs are scripted, resulting in repetitive behaviors for other NPCs.
Since collaboration is hard to script, it is rare for NPCs to interact with each other.
Behaviors are hand-scripted, nonresumable, and dependent on the game engine. With each layer of realism, complexity increases and the tools fail.
We identify five features that behaviors should exhibit.
A behavior should be responsive(react quickly to the environment), interruptible(suspendible by other behaviors or events), resumable(continue from the point of interruption), collaborative (initiate and respond to joint behavior requests), and generative (easy to create by nonprogrammers).
We show how our AI architecture supports all five features simultaneously.
This tutorial combines an architectural solution to traditional game AI challenges: interruptible, resumable, responsive, and collaborative behaviors with high-level behavior constructs accessible to game designers with no programming knowledge to significantly reduce the content bottleneck for behavior creation.
##Chapter 1.2 Code snippet:
First off,let's overview this simple ActionScript3-based Finite State Machine System: 1.IState; 2.IFiniteStateMachine; 3.Agent;
Next,how to construct Finite State Machine(FSM) using AS3?
/**
* Each state implements the IState interface.
* Each state implements enter and exit metheod
* for one-off action that the agent takes
* when entering and leaving the state,
* in addition to the update method
* which is run repeatedly while in the state.
* The time parameter in the update method
* is the duration of the frame we're excuting.
*
*/
public interface IState
{
/**
*
* @param value is the description of monster's state.
*
*/
function set description(value:String):void;
function get description():String;
function enter():void;//called on entering the state
function exit():void;//called on leaving the state
function update(time:Number=0):void;//called every frame while in the state
}
IFiniteStateMachine /** * Our FiniteStateMachine must implment this interface. * * */ public interface IFiniteStateMachine { function set globalState(s:IState):void; function get globalState():IState;
function set currentState(s:IState):void;
function get currentState():IState;
function set previousState(s:IState):void;
function get previousState():IState;
function set nextState(s:IState):void;
function get nextState():IState;
}
Agent;
/**
* The agent uses an instance of the FiniteStateMachine class the handle its AI.
* The agent class is a lot more manageable without the FSM implementation
* and all the states inside it. and must have a single FSM class
* that can be used by all agents(who)can even share states across agents too.
*
*
*/
public class Agent
{
private var fsm:FiniteStateMachine;
public var name:String = "unnamed";
public var carrier:SWFLoader;
public var traceTarget:TextArea;
/**
*
* @param the name of agent;
* @param the carrier of agent;
* @param the agen's traceTarget;
*
*/
public function Agent(name:String,carrier:SWFLoader,traceTarget:TextArea=null)
{
fsm = new FiniteStateMachine(this);
this.name = name;
this.carrier = carrier;
this.traceTarget = traceTarget;
}
public function update():void
{
fsm.update();
}
public function getFSM():FiniteStateMachine
{
return fsm;
}
}
##Chapter 1.3 Supplmentary interpretation:
Supplmentary interpretation
Custom state class implement sample:
FiniteStateMachine
/** * The FiniteStateMachine is responsible for * managing the current state. * * Add a number of features to the FSM class to * all chaining of states and returning to previous states. * * @author Knight.zhou * */ public class FiniteStateMachine implements IFiniteStateMachine { private static const LOG:ILogger = LogUtil.getLogger(FiniteStateMachine); private var _globalState:IState;//For globally track monster's state. private var _currentState:IState; private var _previousState:IState; private var _nextState:IState; protected var owner:Agent;
public function FiniteStateMachine( owner:Agent,
currentState:IState=null,
previousState:IState=null,
nextState:IState=null
)
{
this.owner = owner;
this.currentState = currentState;
this.previousState = previousState;
this.nextState = nextState;
}
/**
* Prepare a global state for globally tracking current state.
*
* @param s is anew state;
*
*/
public function set globalState( s:IState ):void
{
_globalState = s ;
}
public function get globalState():IState
{
return _globalState;
}
/**
* Prepare a state for using the current state.
*
* @param s is anew state;
*
*/
public function set currentState( s:IState ):void
{
_currentState = s ;
}
public function get currentState():IState
{
return _currentState;
}
/**
* Prepare a state for use after the current state.
*
* @param s is anew state;
*
*/
public function set nextState( s:IState ):void
{
_nextState = s ;
}
public function get nextState():IState
{
return _nextState;
}
/**
* Prepare a state for use before the current state.
*
* @param s is anew state;
*
*/
public function set previousState( s:IState ):void
{
_previousState = s ;
}
public function get previousState():IState
{
return _previousState;
}
/**
* Update the FSM.
*/
public function update():void
{
if(currentState)
{
currentState.update();
}
}
/**
* Change to another state.
*
* @param s is anew state.
*
*/
public function changeState( s:IState ):void
{
LOG.info("exited:{0}",currentState);
currentState.exit();
previousState = currentState;
globalState = s;
currentState = s;
currentState.enter();
LOG.info("entered:{0}",currentState);
}
/**
* Go to the previous state.
*/
public function goToPreviousState():void
{
changeState( previousState );
}
/**
* Go to the next state.
*/
public function goToNextState():void
{
changeState( nextState );
}
SadLow
/** * Monster's all kinds of talk states * * @author Knight.zhou * */ public class SadLow implements IState { private var agent:Agent; //---------------------------------- // description(native) //---------------------------------- private var _description:String = "STATE_SAD_LOW"; public function get description():String { return _description; } public function set description(value:String):void { _description = value; } public function SadLow(monster:Agent) { this.monster = monster; }
public function enter():void
{
//TODO: implement function
}
public function exit():void
{
//TODO: implement function
}
public function update(time:Number=0):void
{
//TODO: implement function
}
}
##Chapter 1.4 Supple Interpretation: Supple Interpretation
XMLFiniteStateMachine
### Extensions:Ref:http://blog.lookbackon.com/?page_id=301
Ref:http://blog.lookbackon.com/?paged=9
Github:https://github.com/yangboz/as3FiniteStateMachine
#Chapter 2.Fuzzy Logic System
#Chapter 3.Searching
#Chapter 4.Evaluation
#Chapter 5.Artificial Neural Networks
#Chapter 6.Machine Learning
#Bibliography
#Index.