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

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

#define HP_TIGER (20)
#define TIGER_TOUCH (0x7FDC000)
#define TIGER_WALK_TURN (3*ONE_DEGREE)
#define TIGER_RUN_TURN (6*ONE_DEGREE)
#define TIGER_ATTACK1_RANGE SQUARE(WALL_L/3)
#define TIGER_ATTACK2_RANGE SQUARE(WALL_L*3/2)
#define TIGER_ATTACK3_RANGE SQUARE(WALL_L)
#define TIGER_ROAR_CHANCE 0x60
#define TIGER_WALK_CHANCE (TIGER_ROAR_CHANCE + 0x400)
#define TIGER_BITE_DAMAGE 100
#define TIGER_DEAD_ANIM 11

static BITE_INFO TigerBite = { 19, -13, 3, 26 };

enum TIGER_STATE {
    ST_TIGER_EMPTY,
    ST_TIGER_STOP,
    ST_TIGER_WALK,
    ST_TIGER_RUN,
    ST_TIGER_WAIT,
    ST_TIGER_ROAR,
    ST_TIGER_ATTACK1,
    ST_TIGER_ATTACK2,
    ST_TIGER_ATTACK3,
    ST_TIGER_DEATH
};

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

    ITEM_INFO* item;
    CREATURE_INFO* tiger;
    AI_INFO info;
    short angle, head_y, tilt;
    short dmg1, dmg2, dmg3;

    item = &Ptr_VetItems[item_number];
    tiger = (CREATURE_INFO*)item->data;
    angle = head_y = tilt = 0;
    dmg1 = Utils.getObjectAttribute(tsu_slots[item->object_number], ATT_DMG1);
    dmg2 = Utils.getObjectAttribute(tsu_slots[item->object_number], ATT_DMG2);
    dmg3 = Utils.getObjectAttribute(tsu_slots[item->object_number], ATT_DMG3);

    if (item->hit_points <= 0)
    {
        if (item->current_anim_state != ST_TIGER_DEATH)
        {
            item->anim_number = Ptr_VetObjects[item->object_number].anim_index + TIGER_DEAD_ANIM;
            item->frame_number = Ptr_VetAnimations[item->anim_number].frame_base;
            item->current_anim_state = ST_TIGER_DEATH;
        }
    }
    else
    {
        CreatureAIInfo(item, &info);

        if (info.ahead)
        {
            head_y = info.angle;
        }

        GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);
        angle = CreatureTurn(item, tiger->maximum_turn);

        switch (item->current_anim_state)
        {
            case ST_TIGER_STOP:
                tiger->maximum_turn = 0;
                tiger->flags = 0;

                if (tiger->mood == MOOD_ESCAPE)
                {
                    item->goal_anim_state = ST_TIGER_RUN;
                }
                else if (tiger->mood == MOOD_BORED)
                {
                    item->goal_anim_state = ST_TIGER_WALK;
                }
                else if (info.ahead && info.distance < TIGER_ATTACK1_RANGE)
                {
                    item->goal_anim_state = ST_TIGER_ATTACK1;
                }
                else if (info.ahead && info.distance < TIGER_ATTACK3_RANGE)
                {
                    tiger->maximum_turn = TIGER_WALK_TURN;
                    item->goal_anim_state = ST_TIGER_ATTACK3;
                }
                else if (item->required_anim_state)
                {
                    item->goal_anim_state = item->required_anim_state;
                }
                else if (tiger->mood != MOOD_ATTACK && GetRandomControl() < TIGER_ROAR_CHANCE)
                {
                    item->goal_anim_state = ST_TIGER_ROAR;
                }
                else
                {
                    item->goal_anim_state = ST_TIGER_RUN;
                }
                break;
            case ST_TIGER_WALK:
                tiger->maximum_turn = TIGER_WALK_TURN;

                if (tiger->mood != MOOD_BORED || GetRandomControl() < TIGER_ROAR_CHANCE)
                {
                    item->goal_anim_state = ST_TIGER_STOP;
                    item->required_anim_state = ST_TIGER_ROAR;
                }
                break;
            case ST_TIGER_RUN:
                tiger->maximum_turn = TIGER_RUN_TURN;

                if (tiger->mood == MOOD_BORED)
                {
                    item->goal_anim_state = ST_TIGER_STOP;
                }
                else if (tiger->flags)
                {
                    /* after TIGER_ATTACK2, if close to lara, stop and start close attack */
                    if (info.ahead)
                    {
                        item->goal_anim_state = ST_TIGER_STOP;
                    }
                    else if (tiger->mood != MOOD_ATTACK && GetRandomControl() < TIGER_ROAR_CHANCE)
                    {
                        item->goal_anim_state = ST_TIGER_STOP;
                        item->required_anim_state = ST_TIGER_ROAR;
                    }
                }
                else if (info.ahead && info.distance < TIGER_ATTACK2_RANGE)
                {
                    if (LaraItem->speed == 0) // original 0
                        item->goal_anim_state = ST_TIGER_STOP;
                    else
                        item->goal_anim_state = ST_TIGER_ATTACK2;
                }
                else if (tiger->mood != MOOD_ATTACK && GetRandomControl() < TIGER_ROAR_CHANCE)
                {
                    item->goal_anim_state = ST_TIGER_STOP;
                    item->required_anim_state = ST_TIGER_ROAR;
                }
                
                tiger->flags = 0;
                break;
            case ST_TIGER_ATTACK1:
            case ST_TIGER_ATTACK2:
            case ST_TIGER_ATTACK3:
                if (!tiger->flags && (item->touch_bits & TIGER_TOUCH))
                {
                    CreatureEffect(item, &TigerBite, DoBloodSplat_2);
					if (item->current_anim_state == ST_TIGER_ATTACK1)
						LaraItem->hit_points -= DOUBLE_VALUE_ITEM(dmg1, TIGER_BITE_DAMAGE);
					else if (item->current_anim_state == ST_TIGER_ATTACK2)
						LaraItem->hit_points -= DOUBLE_VALUE_ITEM(dmg2, TIGER_BITE_DAMAGE);
					else
						LaraItem->hit_points -= DOUBLE_VALUE_ITEM(dmg3, TIGER_BITE_DAMAGE);
                    LaraItem->hit_status = 1;
                    tiger->flags = 1;
                }
                break;
        }
    }

    CreatureTilt(item, tilt);
    CreatureJoint(item, 0, head_y);
    CreatureAnimation(item_number, angle, 0);
}

void InitialiseTR2_Tiger()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR2_TIGER1 + index].set_name("Tiger");
        tomb[TR2_TIGER1 + index].initialise(TR2_TIGER1 + index, FUNCTION_SHADOW, 0);
        tomb[TR2_TIGER1 + index].set_shadow_size(128);
        tomb[TR2_TIGER1 + index].set_func_initialise(InitialiseCreature);
        tomb[TR2_TIGER1 + index].set_func_collide(CreatureCollision);
        tomb[TR2_TIGER1 + index].set_func_control(CtrlTR2_Tiger);
        tomb[TR2_TIGER1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR2_TIGER1 + index].set_health(HP_TIGER);
        tomb[TR2_TIGER1 + index].set_radius_and_pivot(341, 200);
        tomb[TR2_TIGER1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR2_TIGER1 + index].set_creature(TRUE);
        tomb[TR2_TIGER1 + index].set_rotation_meshtree(21, ROT_Y);
        tomb[TR2_TIGER1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}