Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function library
#1
-> 
Post your most useful functions in here!
Preferably with comments on each inside the code, a very simple AI using them and an optional example print of each to show what they return in action.

Here is an example:
    C-Code:
void id(){
   clr();
   ego();
   inputs();
   if(danger()){stall();}
   else if(marked()){dodge();}
}
 
void defend(){
   //turn against target and defend
   if(!facing_against()){turn_self();}
   D(1,0);
}
 
void dodge(){
   //walk out of zrange
   if((zdistance()>0&&zborder1()>15)||zborder2()<=15){up(1,0);}else{down(1,0);}
}
 
void inputs(){
   //void inputs
   up(0,0);down(0,0);left(0,0);right(0,0);D(0,0);J(0,0);A(0,0);
}
 
void stall(){
   //flip or defend
   if(self.state==12){J(1,0);}else{defend();}
}
 
void turn_self(){
   //turn around
   if(self.facing){right(1,0);}else{left(1,0);}
}
 
int ego(){
   if(target.num==self.num){return 1;}
   print("target: "+target.num+" id: "+target.id+" type: "+target.type+"\n\n");
 
   print("danger: "+danger()+"\n\n");
 
   print("facing_against : "+facing_against()+"\n");
   print("facing_distance: "+facing_distance()+"\n\n");
 
   print("marked: "+marked()+"\n\n");
 
   print("range(0,100,self.x): "+range(0,100,self.x)+"\n\n");
 
   print("will_collide: "+will_collide()+"\n\n");
 
   print("xdistance     : "+xdistance()+"\n");
   print("xrange(50,150): "+xrange(50,150)+"\n\n");
 
   print("zborder1     : "+zborder1()+"\n");
   print("zborder2     : "+zborder2()+"\n");
   print("zdistance    : "+zdistance()+"\n");
   print("zrange(15,25): "+zrange(15,25)+"\n\n");
 
   print("self_attacking  : "+self_attacking()+"\n");
   print("target_attacking: "+target_attacking()+"\n\n");
 
   print("self_dop  : "+self_dop()+"\n");
   print("target_dop: "+target_dop()+"\n\n");
 
   print("self_facing  : "+self_facing()+"\n");
   print("target_facing: "+target_facing()+"\n\n");
 
   print("self_frame(0,8)  : "+self_frame(0,8)+"\n");
   print("target_frame(0,8): "+target_frame(0,8)+"\n\n");
 
   print("self_ground  : "+self_ground()+"\n");
   print("target_ground: "+target_ground()+"\n\n");
 
   print("self_hitable  : "+self_hitable()+"\n");
   print("target_hitable: "+target_hitable()+"\n\n");
 
   print("self_knockable  : "+self_knockable()+"\n");
   print("target_knockable: "+target_knockable()+"\n\n");
 
   print("self_open  : "+self_open()+"\n");
   print("target_open: "+target_open()+"\n\n");
 
   return 1;
}
 
bool danger(){
   //is there a dangerous object within range?
   int k=target.num;
   for (int i=0;i<400;++i){
      if (loadTarget(i)!=-1&&target.team!=self.team&&zrange(0,15)&&xrange(0,80)&&target_attacking()){
         return true;
      }
   }
   if(loadTarget(k)==-1){loadTarget(self.num);}
   return false;
}
 
bool facing_against(){
   //true if self and target face opposite directions
   return (self.facing!=target.facing)?true:false;
}
 
int facing_distance(){
   //positive: target distance to the front
   return -xdistance()*(2*(self.facing?1:0)-1);
}
 
bool marked(){
   //is there a dangerous object intercepting with self position?
   int k=target.num;
   for (int i=0;i<400;++i){
      if (loadTarget(i)!=-1&&target.team!=self.team&&will_collide()&&target_attacking()){
         return true;
      }
   }
   if(loadTarget(k)==-1){loadTarget(self.num);}
   return false;
}
 
bool range(int min, int max, int value){
   //true if between min and max
   return (value>=min&&value<=max)?true:false;
}
 
bool will_collide(){
   //is there an object within zrange straight flying towards self?
   return((zrange(0,15)&&target.z_velocity==0)&&target.x_velocity*xdistance()<0)?true:false;
}
 
int xdistance(){
   //positive: target distance to the right
   return target.x-self.x;
}
 
bool xrange(int min, int max){
   //true if between min and max
   return range(min,max,abs(xdistance()));
}
 
int zborder1(){
   //distance to the top
   return target.z-bg_zwidth1;
}
 
int zborder2(){
   //distance to the bottom
   return bg_zwidth2-target.z;
}
 
int zdistance(){
   //positive: target distance to the bottom
   return target.z-self.z;
}
 
bool zrange(int min, int max){
   //true if between min and max
   return range(min,max,abs(zdistance()));
}
 
bool self_attacking(){
   //true if self within state: 3* or 18 or 19 or 1002 or 2000
   int x=self.state;
   int i=0;
   do{x/=10;i++;}while(x>0);
   x=1;
   for(i;i>1;--i){x*=10;}
   return (self.state/x==3||self.state==18||self.state==19||target.state==1002||target.state==2000)?true:false;
}
bool target_attacking(){
   //true if target within state: 3* or 18 or 19 or 1002 or 2000
   int x=target.state;
   int i=0;
   do{x/=10;i++;}while(x>0);
   x=1;
   for(i;i>1;--i){x*=10;}
   return (target.state/x==3||target.state==18||target.state==19||target.state==1002||target.state==2000)?true:false;
}
 
bool self_dop(){
   //true if within dance of pain/catchable
   return (self.state==16)?true:false;
}
bool target_dop(){
   //true if within dance of pain/catchable
   return (target.state==16)?true:false;
}
 
bool self_facing(){
   //true if self facing target
   return (facing_distance()>0)?true:false;
}
bool target_facing(){
   //true if target facing self
   return (xdistance()*(2*(target.facing?1:0)-1)>0)?true:false;
}
 
bool self_frame(int min, int max){
   //true if between min and max
   return range(min,max,self.frame);
}
bool target_frame(int min, int max){
   //true if between min and max
   return range(min,max,target.frame);
}
 
bool self_ground(){
   //true if self on ground
   return (self.y==0&&self.state!=4&&self.state!=5&&self.state!=12)?true:false;
}
bool target_ground(){
   //true if target on ground
   return (target.y==0&&target.state!=4&&target.state!=5&&target.state!=12)?true:false;
}
 
bool self_hitable(){
   //true if self hitable
   return (self.blink==0&&self.state!=6&&self.state!=12&&self.state!=14)?true:false;
}
bool target_hitable(){
   //true if target hitable
   return (target.blink==0&&target.state!=6&&target.state!=12&&target.state!=14)?true:false;
}
 
bool self_knockable(){
   //true if self knockable
   return (self.blink==0&&self.state!=6&&self.state!=14)?true:false;
}
bool target_knockable(){
   //true if target knockable
   return (target.blink==0&&target.state!=6&&target.state!=14)?true:false;
}
 
bool self_open(){
   //true if immobile for a longer period
   return (self_dop()||self.state==8||self.state==13||self_frame(130,132))?true:false;
}
bool target_open(){
   //true if immobile for a longer period
   return (target_dop()||target.state==8||target.state==13||target_frame(130,132))?true:false;
}
you can uncomment the ego(); inside the void function to stop the script from printing everything.

after the main id function I have quite a few more void functions that only simplify certain actions (such as defending against the target or turning around)
and after the ego function i have mostly bool functions to help easy decision making with the ego function printing all of them

right now this AI is only evasive and defensive (against all hostile objects)
i have yet to try and dodge diagonal attacks too (only doing straight ones now)
after that i might have a go at target picking and approaching
maybe this can turn into a pretty good basic AI

ALSO I CHALLENGE YOU TO TRY AND GRAB THE ABOVE AI IN A FIGHT (any original characters allowed - LOUIS D^J DOES NOT COUNT)
tell me how you did it if you succeed
Reply
Thanks given by:
#2
Challenge is pretty easy - just throw a baseball ball at him at short distance (but a bit farther than close combat distance) and grab him :) I can record gameplay if u cannot do it.

Unfortunately AI cannot defend from (except of diagonal attacks of course) straight dash and run attacks (I used Deep).

Good job :)
Reply
Thanks given by:
#3
(08-14-2012, 06:29 PM)Shymeck Wrote:  Challenge is pretty easy - just throw a baseball ball at him at short distance (but a bit farther than close combat distance) and grab him :) I can record gameplay if u cannot do it.
i did it with a knife close range
now how about no items?

(08-14-2012, 06:29 PM)Shymeck Wrote:  Unfortunately AI cannot defend from (except of diagonal attacks of course) straight dash and run attacks (I used Deep).
yes but dodge quite easily - and i think on some characters the straight dash attack defense actually works

i will try to include the targets velocity (and possibly self) to time the defense better against fast attacks
Reply
Thanks given by:
#4
Another try, another success - use Dennis' D>J :)
Reply
Thanks given by:
#5
eh pretty difficult

anyway i have written a better collide function
this one is useful to anticipate self and target speed assuming both to be constant

i will use higher times to dodge, mid times to rebound or maybe attack if possible (need new functions for this) and low times for a last minute defend/roll/flip

    C-Code:
int collide_in(){
   //returns time until object and self will collide, -1 for no collision
   int t=-1;
   int xt;int zt;
   int dvx=target.x_velocity-self.x_velocity;
   int dvz=target.z_velocity-self.z_velocity;
 
   if(dvx!=0){xt=abs((abs(xdistance())-80)/dvx);}else{xt=0;}
   if(dvz!=0){zt=abs(zdistance()/dvz);}else{zt=0;}
   if(xt>zt){t=xt+1;}else{t=zt+1;}
 
   int x=target.x+t*dvx;
   int z=target.z+t*dvz;
   if(xrange(0,80)&&zrange(0,15)){t=0;}
   else if(abs(self.x-x)>abs(xdistance())||abs(self.z-z)>abs(zdistance())||!range(0,80,abs(self.x-x))||!range(0,15,abs(self.z-z))){t=-1;}
 
   return t;
}


edit:

here is a function that can read the change of a value from one to the next frame
it will need to be called from the id() function like this:
    C-Code:
void id(){
 
   int change; int value=target.hp;
   print(read_changes(change,value)+" = "+value+"\n");
   change=value;
 
}
 
int read_changes(int change, int value){
   change=value-change;
   return change;
}

in this case the function is used to print the changes of target.hp
if you want to read the changes of a different value just change it
if you want to read more than one kind of value at once you will need to use two new variables for change and value for each inside the id() function
read_changes can remain unchanged
Reply
Thanks given by: A-Man
#6
I see you are using an undocumented bug (or feature ;p ) of AS. Your pseudo-differential function is not too safe in the context of newer versions of AI plug-in ;] Anyway it's a nice idea.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)