Little Fighter Empire - Forums
Beginner's AI - Printable Version

+- Little Fighter Empire - Forums (https://lf-empire.de/forum)
+-- Forum: Little Fighter 2 Zone (https://lf-empire.de/forum/forumdisplay.php?fid=7)
+--- Forum: AI Scripting (https://lf-empire.de/forum/forumdisplay.php?fid=56)
+--- Thread: Beginner's AI (/showthread.php?tid=10240)



Beginner's AI - T.O.R.N.A.D.O - 02-20-2016

So I tried making a simple and basic AI for a character called Naruto from my mod. Here is the code:

Code:
int ego(){
    int rxdist=(target.x-self.x);
    int lxdist=(self.x-target.x);
    int random=rand(100);

    //Run in correct direction
    if (abs(self.x-target.x) > 100)
    {
        if (self.x-target.x > 0)
        {
            left(1,0);
            left(1,0);
        }  
        else if (self.x-target.x < 0)
        {
            right(1,0);
            right(1,0);
        }  
    }

    //Rasenshuriken
    if ((abs(self.z-target.z) < 5) && ((self.x-target.x) < 100) && ((self.x-target.x) > 50) && self.mp > 200 && self.hp<480 && random<=5)
    {
        if (self.x-target.x > 0)
          {
               left(1,0);
               if (self.facing==true)
            DuJ();
          }        
         else if (self.x-target.x < 0)
        {
              right(1,0);
               if (self.facing==false)
               DuJ();
          }
    }
    
    //Shuriken
    if(lxdist<=50 && self.mp>50 && random<=30)
    {
        DlA();
    }
    else if(rxdist<=50 && self.mp>50 && random<=30)
    {
        DrA();
    }

    //Rasengan
    if(lxdist<=30 && self.mp>100 && random<=30)
    {
        DlJ();
    }
    else if(rxdist<=30 && self.mp>100 && random<=30)
    {
        DrJ();
    }

    //Surprise Clone
    if(lxdist<10 || rxdist<10 && self.mp>100 && random<=30)
    {
        A(1,1);
        J();
    }

    //Mass Clone
    if(lxdist>200 || rxdist>200 && self.mp>480 && random<=5)
    {
        DdJ();
    }

    //Frog
    if((lxdist<500 && lxdist>20) || (rxdist<500 && rxdist>20) && self.mp>100 && random<=10 && abs(self.z-target.z)<=10)
    {
        DdA();
    }

    return 0;
}

Now the problem I am getting is - the computer spams J+J+A for no reason. I haven't put anything like that in the ego function and the basic character AI cannot be THAT retarded. So why is he behaving like that? Any thoughts?

PS: The id used here is 1.


RE: Beginner's AI - Ramond - 02-20-2016

Code:
 if(lxdist<10 || rxdist<10 && self.mp>100 && random<=30)
   {
       A(1,1);
       J();
   }

Most likely this causes it. It's always dangerous to use multiple conditional || and && without proper bracketing, so maybe use more brackets to specify the exact conditions.

Currently, seeing how you defined lxdist and rxdist, the following will always hold: lxdist = -rxdist

And you have following two conditions: lxdist<10, rxdist<10

But since lxdist = -rxdist:

lxdist<10, -lxdist<10

Obviously, one of both is non-positive and therefore always fulfilled, so that || doesn't seem to make much sense anyway. Except if you didn't mean to bracket (lxdist<10 || rxdist<10) like that. Perhaps you meant to use rxdist>10, or forgot to use absolute function on the dist values?


RE: Beginner's AI - A-Man - 02-20-2016

For all languages I've encountered, the precedence of `&&` is greater than `||`, so that would be parsed as
(lxdist<10) || (rxdist<10 && self.mp>100 && random<=30)
. What you probably want is
(lxdist<10 || rxdist<10) && self.mp>100 && random<=30



RE: Beginner's AI - T.O.R.N.A.D.O - 02-20-2016

But that doesn't really make any sense. Granted the brackets are wrong there but that is telling the computer to press J while holding A. But computer just spams J+A+A which is other way around.


RE: Beginner's AI - A-Man - 02-20-2016

The order you execute these functions doesn't matter; as all of this is happening in 1 frame. And even if it that wasn't, it also depends on when the character starts responding to inputs. Consider you let your character tap A and D at some random state: 15 frames. If the character's frame control lands on a frame with a hit_d, that will activate, even though there might be a hit_a in the next frame to come.

I am not very knowledgeable about the inner-workings of this, but the fact that you're using 'ego' might as well mean that they do click, or even release buttons from time to time. Guess what holding a button and releasing it continuously would do.


RE: Beginner's AI - T.O.R.N.A.D.O - 02-20-2016

So how do you propose to make it execute that move using AI? I thought that was the best way to execute 2 button moves like AJ, AD, etc.


RE: Beginner's AI - A-Man - 02-20-2016

I suggest you simply let the AI only press the first key when the condition is satisfied, and somewhere else in your code you do the second key click based on a check on your current frame, like so:
if (self.frame == some_frame && (lxdist<10 || rxdist<10) && self.mp>100 && random<=30)


If you need more precision (more characters have a similar move, but not different frames), I think you can go into depths by checking if there is a particular hit_j in a frame:
if (game.objects[self.num].data.frames[self.frame].hit_j == something)