My friend Chang asked me a few days back if I'd do some scripting for N-Pulse. I've been very interested in AI Programming, lately, but the overhead of learning or creating a game engine is very annoying when it comes to learning, so I figured I'd help him out by trying out AI and things of that nature. (Had to practically relearn, GS2, however)
So far these are my accomplishments, they seem to work very efficiently.
Fuzzy Logic
Fuzzy Logic is a logic system that gives value sets the ability to be partially true, or, more accurately, is a "fuzzy" alternative to binary logic. You can read up on it on my blog post about it:
http://coreyschram.wordpress.com/200...7/fuzzy-logic/
PHP Code:
/* GraalScript2 Fuzzy Logic Implementation (2009)
by Corey Schram (*coreys)
This library is free to use by any persons so long as this disclaimer
is not changed or removed.
*/
function onCreated()
{
this.sets = new[0];
}
/* Add a set (flag value, basically) to the
fuzzy logic controller.
Assumes that sets are added in numerical order. */
public function addSet(setName, rangeMin, rangeMax)
{
temp.set = new TStaticVar();
temp.set.setName = setName;
temp.set.rangeMin = rangeMin;
temp.set.rangeMax = rangeMax;
this.sets.add(temp.set);
}
/* Get the degree of truth of a set given a
particular value. */
public function degreeOfTruth(setName, value)
{
for (i = 0; i < this.sets.size(); i++)
{
currSet = this.sets[i];
if (currSet.setName == setName)
{
if (i == 0)
lastSet = NULL;
else lastSet = this.sets[i-1];
if (i == (this.sets.size()-1))
nextSet = NULL;
else nextSet = this.sets[i+1];
if ((value >= currSet.rangeMin) && (value <= currSet.rangeMax))
{
if (lastSet == NULL)
{
if (nextSet == NULL)
return 1;
else if (value > nextSet.rangeMin)
{
v = value - nextSet.rangeMin;
r = currSet.rangeMax - nextSet.rangeMin;
return (1-(v/r));
}
else return 1;
}
else if (value < lastSet.rangeMax)
{
v = value - currSet.rangeMin;
r = lastSet.rangeMax - currSet.rangeMin;
return (v/r);
}
else
{
if (nextSet == NULL)
return 1;
else if (value > nextSet.rangeMin)
{
v = value - nextSet.rangeMin;
r = currSet.rangeMax - nextSet.rangeMin;
return (1-(v/r));
}
else return 1;
}
}
else return 0;
}
}
return NULL;
}
// Get all the true sets given a value.
public function getTrueSets(value)
{
temp.sets = new[0];
for (set: this.sets)
{
if (value >= set.rangeMin && value <= set.rangeMax)
sets.add(set);
}
return sets;
}
/* Gets the best set (highest degree of truth)
given a value. Note that if two sets have the
same degree of truth it will always return the
first in line. */
public function getBestSet(value)
{
temp.sets = this.getTrueSets(value);
temp.best_set = NULL;
temp.best_set_value = 0;
for (temp.set: temp.sets)
{
if (temp.best_set == NULL ||
this.degreeOfTruth(temp.set.setName, value) > temp.best_set_value)
{
temp.best_set = temp.set;
temp.best_set_value = this.degreeOfTruth(temp.best_set.setName, value);
}
}
return temp.best_set;
}
Example Usage:
http://www.npulse-rebirth.com/showpo...4&postcount=18
There are also a couple function for getting the degree of truth, true sets, and best set (set with the highest degree of truth):
PHP Code:
fl.degreeOfTruth(setName, value);
fl.getTrueSets(value);
gl.getBestSet(value);
The post I linked to above also describes how it can be used.
Behavior Trees
http://www.npulse-rebirth.com/showpo...70&postcount=1
PHP Code:
/* Behavior Node class.
Easy, efficient, and widely extensible implementation
of Behavior Trees. */
function onCreated()
{
this.nodeName = "";
this.parentNode = NULL;
this.childNodes = new TDict();
this.behave = NULL;
}
public function treeRoot(behaveFunc)
{
this.nodeName = "treeroot";
this.behave = behaveFunc;
return this;
}
public function addChildNode(nodeName, behaveFunc)
{
n = new TBehaviorNode();
n.nodeName = nodeName;
n.parentNode = this;
n.behave = behaveFunc;
this.childNodes.set(nodeName, n);
return this;
}
public function getParentNode()
{
if (this.nodeName == "treeroot")
return this;
else return this.parentNode;
}
public function getChildNode(nodeName)
{
return this.childNodes.get(nodeName);
}
Example Usage:
PHP Code:
function onCreated()
{
temp.b = new TBehaviorNode();
temp.b.treeRoot(this.treeRoot)
.addChildNode("tree_1_1", this.tree_1_1)
.addChildNode("tree_1_2", this.tree_1_2)
.getChildNode("tree_1_2")
.addChildNode("tree_2_1", this.tree_2_1)
.getParentNode()
.addChildNode("tree_1_3", this.tree_1_3)
.getChildNode("tree_1_3")
.addChildNode("tree_2_2", this.tree_2_2)
.getChildNode("tree_2_2")
.addChildNode("tree_3_1", this.tree_3_1)
;
temp.b.behave(0);
}
public function treeRoot(i)
{
echo("Behavior Tree Root: " @ i);
this.getChildNode("tree_1_3").behave(i+1);
}
public function tree_1_1(i)
{
echo("Behavior Tree Level 1, Node 1: " @ i);
}
public function tree_1_2(i)
{
echo("Behavior Tree Level 1, Node 2: " @ i);
this.getChildNode("tree_2_1").behave(i+1);
}
public function tree_1_3(i)
{
echo("Behavior Tree Level 1, Node 3: " @ i);
this.getChildNode("tree_2_2").behave(i+1);
}
public function tree_2_1(i)
{
echo("Behavior Tree Level 2, Node 1: " @ i);
}
public function tree_2_2(i)
{
echo("Behavior Tree Level 2, Node 2: " @ i);
this.getChildNode("tree_3_1").behave(i+1);
}
public function tree_3_1(i)
{
echo("Behavior Tree Level 3, Node 1: " @ i);
}
Which will print
PHP Code:
Behavior Tree Root: 0
Behavior Tree Level 1, Node 3: 1
Behavior Tree Level 2, Node 2: 2
Behavior Tree Level 3, Node 1: 3
AI is very lacking in Graal, perhaps this can help