Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-10-2009, 08:59 PM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
Some dabbling

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(setNamerangeMinrangeMax)
{
  
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(setNamevalue)
{
  for (
0this.sets.size(); i++)
  {
    
currSet this.sets[i];
    if (
currSet.setName == setName)
    {
      if (
== 0)
        
lastSet NULL;
      else 
lastSet this.sets[i-1];
      if (
== (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)
          {
            
value nextSet.rangeMin;
            
currSet.rangeMax nextSet.rangeMin;
            return (
1-(v/r));
          }
          else return 
1;
        }
        else if (
value lastSet.rangeMax)
        {
          
value currSet.rangeMin;
          
lastSet.rangeMax currSet.rangeMin;
          return (
v/r);
        }
        else
        {
          if (
nextSet == NULL)
            return 
1;
          else if (
value nextSet.rangeMin)
          {
            
value nextSet.rangeMin;
            
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 (
setthis.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.settemp.sets)
  {
    if (
temp.best_set == NULL ||
      
this.degreeOfTruth(temp.set.setNamevalue) > temp.best_set_value)
    {
      
temp.best_set temp.set;
      
temp.best_set_value this.degreeOfTruth(temp.best_set.setNamevalue);
    }
  }
  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(setNamevalue);
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(nodeNamebehaveFunc)
{
  
= new TBehaviorNode();
  
n.nodeName nodeName;
  
n.parentNode this;
  
n.behave behaveFunc;
  
this.childNodes.set(nodeNamen);
  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.= 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 Root0
Behavior Tree Level 1
Node 31
Behavior Tree Level 2
Node 22
Behavior Tree Level 3
Node 1
AI is very lacking in Graal, perhaps this can help
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.