#include <StdAfx.h>
#include "eagle.h"
#include <trng_core.h>
#include "utility.h"
#include "Class/TRClass.h"

extern TYPE_CreatureCollision CreatureCollision;
extern TYPE_InitialiseCreature InitialiseCreature;
extern TYPE_CreatureActive CreatureActive;
extern TYPE_CreatureAnimation CreatureAnimation;
extern TYPE_CreatureAIInfo CreatureAIInfo;
extern TYPE_GetCreatureMood GetCreatureMood;
extern TYPE_CreatureMood CreatureMood;
extern TYPE_CreatureTurn CreatureTurn;
extern TYPE_CreatureEffect CreatureEffect;
extern TYPE2_DoBloodSplat DoBloodSplat_2;
extern TYPE_DrawAnimatingItem DrawAnimatingItem;

#define HP_EAGLE            (20)
#define EAGLE_DAMAGE        (20)
#define EAGLE_TURN_SPEED    (546)
#define EAGLE_ATTACK_RANGE  (0x40000)
#define EAGLE_OCB_TIMID     (0x0)
#define EAGLE_OCB_VIOLENT   (0x1)
#define EAGLE_IDLE_ANIM     (5)

static BITE_INFO eagle_bite = { 15, 46, 21, 6 };

enum EAGLE_STATE {
    ST_EAGLE_EMPTY,
    ST_EAGLE_FLY,
    ST_EAGLE_STOP,
    ST_EAGLE_PLANE,
    ST_EAGLE_DEATH,
    ST_EAGLE_DEATH_END,
    ST_EAGLE_EAT,
    ST_EAGLE_ATTACK
};

void CtrlTR2_Eagle(short item_number)
{
    if (!CreatureActive(item_number))
        return;

    ITEM_INFO* item;
    CREATURE_INFO* eagle;
    AI_INFO info;
    short angle;
    short dmg1;

    item = &Ptr_VetItems[item_number];
    eagle = (CREATURE_INFO*)item->data;
    angle = 0;
    dmg1 = Utils.getObjectAttribute(tsu_slots[item->object_number], ATT_DMG1);

    if (item->hit_points <= 0)
    {
        OBJECT_INFO* slots = &Ptr_VetObjects[item->object_number];

        switch (item->current_anim_state)
        {
            case ST_EAGLE_DEATH:
                if (item->pos.y_pos >= item->floor)
                {
                    item->pos.y_pos = item->floor;
                    item->gravity_status = 0;
                    item->fallspeed = 0;
                    item->goal_anim_state = ST_EAGLE_DEATH_END;
                }
                break;

            case ST_EAGLE_DEATH_END:
                item->pos.y_pos = item->floor;
                break;

            default:
                item->anim_number = Ptr_VetObjects[item->object_number].anim_index + 8;
                item->frame_number = Ptr_VetAnimations[item->anim_number].frame_base;
                item->current_anim_state = ST_EAGLE_DEATH;
                item->gravity_status = 1;
                item->speed = 0;
                break;
        }

        item->pos.x_rot = 0;
    }
    else
    {
        CreatureAIInfo(item, &info);
        GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);
        angle = CreatureTurn(item, EAGLE_TURN_SPEED);

        switch (item->current_anim_state)
        {
            case ST_EAGLE_STOP:
                item->pos.y_pos = item->floor;

                if (eagle->mood != MOOD_BORED)
                    item->goal_anim_state = ST_EAGLE_FLY;
                break;

            case ST_EAGLE_FLY:
                eagle->flags = 0;

                if (item->required_anim_state)
                    item->goal_anim_state = item->required_anim_state;

                if (eagle->mood != MOOD_BORED)
                {
                    if (info.ahead && info.distance < EAGLE_ATTACK_RANGE)
                        item->goal_anim_state = ST_EAGLE_EAT;
                    else
                        item->goal_anim_state = ST_EAGLE_PLANE;
                }
                else
                {
                    item->goal_anim_state = ST_EAGLE_STOP;
                }
                break;

            case ST_EAGLE_PLANE:
                if (eagle->mood == MOOD_BORED)
                {
                    item->required_anim_state = ST_EAGLE_STOP;
                    item->goal_anim_state = ST_EAGLE_FLY;
                }
                else if (info.ahead && info.distance < EAGLE_ATTACK_RANGE)
                {
                    item->goal_anim_state = ST_EAGLE_EAT;
                }
                break;

            case ST_EAGLE_EAT:
                if (!eagle->flags && item->touch_bits)
                {
                    LaraItem->hit_points -= DOUBLE_VALUE_ITEM(dmg1, EAGLE_DAMAGE);
                    LaraItem->hit_status = 1;
                    CreatureEffect(item, &eagle_bite, DoBloodSplat_2);
                    eagle->flags = 1;
                }
                break;

            case ST_EAGLE_ATTACK:
                item->pos.y_pos = item->floor;

                if (eagle->mood != MOOD_BORED)
                    item->goal_anim_state = ST_EAGLE_FLY;
                break;
        }
    }

    CreatureAnimation(item_number, angle, 0);
}

void InitTR2_Eagle(short ItemIndex)
{
    ITEM_INFO* item = &Ptr_VetItems[ItemIndex];
    InitialiseCreature(ItemIndex);
    item->anim_number = Ptr_VetObjects[item->object_number].anim_index + EAGLE_IDLE_ANIM;
    ANIM_STRUCT* anims = &Ptr_VetAnimations[item->anim_number];
    item->frame_number = anims->frame_base;
    item->current_anim_state = anims->current_anim_state;
    item->goal_anim_state = anims->current_anim_state;
}

void InitialiseTR2_Eagle()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR2_EAGLE1 + index].set_name("Eagle");
        tomb[TR2_EAGLE1 + index].initialise(TR2_EAGLE1 + index, FUNCTION_ZONE_BEFORE | FUNCTION_SHADOW, 0);
        tomb[TR2_EAGLE1 + index].set_shadow_size(128);
        tomb[TR2_EAGLE1 + index].set_func_initialise(InitTR2_Eagle);
        tomb[TR2_EAGLE1 + index].set_func_collide(CreatureCollision);
        tomb[TR2_EAGLE1 + index].set_func_control(CtrlTR2_Eagle);
        tomb[TR2_EAGLE1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR2_EAGLE1 + index].set_health(HP_EAGLE);
        tomb[TR2_EAGLE1 + index].set_radius_and_pivot(204, 0);
        tomb[TR2_EAGLE1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR2_EAGLE1 + index].set_creature(TRUE);
        tomb[TR2_EAGLE1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}