Skip to content

Teching

Tom Sherman edited this page Mar 27, 2017 · 8 revisions

Previous Section: Simple Program

Overview

Example

Now that we have an AI that drops down from the re-spawn platform, let's add the ability to tech. We will create an AI that chooses randomly between no tech/tech in place/tech roll. In the event a tech is missed the AI will also randomly roll/get up/get up attack. After getting up or teching the AI will then randomly choose between spot-dodging and counter-attacking (jab).

Background Knowledge

extern keyword

condition expressions

calling addMove()

AI Behavior when Calling Logic

Basic Structure

The main source file will be called TechingTutorial.c and it will be essentially the same as SimpleProgram.c, at this point the only thing we need to add is more default logic. Other than that the main source file will not change throughout this tutorial.

The first thing we are going to do is create cpuLogic.c and cpuLogic.h. From now on all of our logic will be kept in this file. It is important to have the logic globally accessible since many rules are used throughout the program and we want to avoid duplicate code. Those two files should be pretty self expanatory if you understand the extern keyword. We declare all the logic in the header so that other files can use it, then we define each struct in cpuLogic.c.

The rest of this program will just be defining the logic behind teching.

Teching Logic

We will keep all the teching logic in its own source file, the header file will look like:

void hitTech(AI* ai);
void postTechOption(AI* ai);
void getUpFromGround(AI* ai);

These provide the logic and moves for each of the functions related to teching.

First we have the logic for getting off the ground:

Logic getOffGroundLogic = 
{
    {&fallenDown, .arg1.u = 2},
    {&getUpFromGround, .arg1.p = &cpuPlayer}
};

This tells the AI to call getUpFromGround whenever it has fallen down on the ground:

#define GET_UP_ATTACK_PROB 0.25f

void getUpFromGround(AI* ai)
{
    if (chance(GET_UP_ATTACK_PROB))
    {
        addMove(ai, &_mv_getUpAttack);
    }
    else
    {
        hitTech(ai);
    }
}

When the cpu player has fallen down it will do a get up attack 25% of the time, and the rest of the time it will roll/get-up in place. We take advantage of existing code by calling hitTech to roll or get up in place, since the inputs are roughly the same.

Next we have the logic for hitting a tech:

Logic hitTechLogic = 
{
    {&techSituation, .arg1.u = 2},
    {&hitTech, .arg1.p = &cpuPlayer}
};

This means that whenever player 2 is in a "teching situation" aka heading towards the ground/platform while in hitstun/tumble, the function hitTech will be called:

#define HIT_TECH_PROB 0.8f
#define TECH_ROLL_PROB 0.5f

static float getRandomTechDirection()
{
    if (chance(TECH_ROLL_PROB))
    {
        return chance(0.5f) ? 0.f : 180.f;
    }
    else
    {
        return 90.f;
    }
}

void hitTech(AI* ai)
{
    addLogic(ai, &getOffGroundLogic);
    addLogic(ai, &resetOnHitLogic);

    if (chance(HIT_TECH_PROB))
    {
        SET_TECH_DIR(getRandomTechDirection()); 
        addMove(ai, &_mv_tech);   
        addLogic(ai, &actAfterTechLogic);
    }
}

This function first adds some logic in case it misses the tech. Then addLogic(ai, &resetOnHitLogic) tells the AI to reset if it is hit before in touches the ground. This is important or else the AI will think it is currently in the process of teching when in fact it is being sent in a new direction.

Next, it will hit the tech 80% of the time. It first randomly sets the direction of the tech. Then it adds the move to the AI and finally it adds the logic for acting after the tech. This looks like:

Logic actAfterTechLogic = 
{
    {&actionStateEq, .arg1.u = 2, .arg2.u = _AS_Wait},
    {&postTechOption, .arg1.p = &cpuPlayer}
};

void postTechOption(AI* ai)
{
    if (chance(GET_UP_ATTACK_PROB))
    {
        addMove(ai, &_mv_jab);
    }
    else
    {
        addMove(ai, &_mv_spotDodge);
    }
}

The logic struct calls postTechOption whenever the cpu is in the Wait action state. Since this logic is called before the tech is hit, the first frame of Wait will be the first frame after the tech animation.

At that point, the postTechOption function either jabs or spot dodges. It is not necessary to add any cleanup logic since each of those moves are 2 frames. This means the AI will be clear in 2 frames then receive its default logic once again.

Creating the Program

teching.c contains most of the new code in this program.

The full code is here. Run the standard wiimake command to build the iso.

Next Section

The next important thing our AI needs to do is DI.

Next Section: DI

Clone this wiki locally