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

extern TYPE_CreatureActive CreatureActive;
extern TYPE_CreatureCollision CreatureCollision;
extern TYPE_InitialiseCreature InitialiseCreature;
extern TYPE_CreatureAnimation CreatureAnimation;
extern TYPE_CreatureJoint CreatureJoint;
extern TYPE_CreatureTilt CreatureTilt;
extern TYPE_CreatureAIInfo CreatureAIInfo;
extern TYPE_CreatureTurn CreatureTurn;
extern TYPE_CreatureMood CreatureMood;
extern TYPE_GetCreatureMood GetCreatureMood;
extern TYPE_CreatureVault CreatureVault;
extern TYPE_DrawAnimatingItem DrawAnimatingItem;
extern TYPE_SoundEffect SoundEffect;
extern TYPE_GetRandomControl GetRandomControl;
extern TYPE_CreatureEffect CreatureEffect;
extern TYPE_DoBloodSplat DoBloodSplat;
extern TYPE_CreateEffect CreateEffect;
extern TYPE_Targetable Targetable;

#define HP_KNIFETHROWER (60)
#define CULT2_WALK_TURN (ONE_DEGREE*3)
#define CULT2_RUN_TURN  (ONE_DEGREE*6)
#define CULT2_WALK_RANGE SQUARE(WALL_L*4)
#define CULT2_KNIFE_RANGE SQUARE(WALL_L*6)
#define CULT2_STOP_RANGE SQUARE(WALL_L*5/2)
#define CULT2_DEATH_ANIM (23)

static BITE_INFO cult2_left = { 0, 0, 0, 5 };
static BITE_INFO cult2_right = { 0, 0, 0, 8 };
static short KnifethrowerSlot;

enum CULT2_STATE {
    ST_CULT2_EMPTY,
    ST_CULT2_STOP,
    ST_CULT2_WALK,
    ST_CULT2_RUN,
    ST_CULT2_AIM1L,
    ST_CULT2_SHOOT1L,
    ST_CULT2_AIM1R,
    ST_CULT2_SHOOT1R,
    ST_CULT2_AIM2,
    ST_CULT2_SHOOT2,
    ST_CULT2_DEATH
};

static short Knife(int x, int y, int z, short speed, short yrot, short room_number)
{
    short fx_number;
    FX_INFO* fx;

    fx_number = CreateEffect(room_number);
    if (fx_number != NO_ITEM)
    {
        fx = &Ptr_VetEffects[fx_number];
        fx->pos.x_pos = x;
        fx->pos.y_pos = y;
        fx->pos.z_pos = z;
        fx->room_number = room_number;
        fx->pos.x_rot = fx->pos.z_rot = 0;
        fx->pos.y_rot = yrot;
        fx->speed = 150;
        fx->frame_number = 0;
        fx->object_number = Utils.getObjects(Utils.getLinks(tsu_slots[KnifethrowerSlot], LNK_OBJECT1));
        fx->shade = 14 * 256;
        Utils.ShootAtLara(fx);
    }
	return fx_number;
}

void CtrlTR2_Knifethrower(short ItemIndex)
{
    if (!CreatureActive(ItemIndex))
        return;

    ITEM_INFO* item;
    CREATURE_INFO* knife;
    AI_INFO info;
    short angle = 0, torso = 0, head = 0, tilt = 0;
    int random = GetRandomControl();

    item = &Ptr_VetItems[ItemIndex];
    knife = (CREATURE_INFO*)item->data;

    if (item->hit_points <= 0)
    {
        if (item->current_anim_state != ST_CULT2_DEATH)
        {
            item->anim_number = Ptr_VetObjects[item->object_number].anim_index + CULT2_DEATH_ANIM;
            item->frame_number = Ptr_VetAnimations[item->anim_number].frame_base;
            item->current_anim_state = ST_CULT2_DEATH;
        }
    }
    else
    {
        CreatureAIInfo(item, &info);
        GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);
        angle = CreatureTurn(item, knife->maximum_turn);

        switch (item->current_anim_state)
        {
            case ST_CULT2_STOP:
                knife->maximum_turn = 0;
                
                if (info.ahead)
                    head = info.angle;

                if (knife->mood == MOOD_ESCAPE)
                {
                    item->goal_anim_state = ST_CULT2_RUN;
                }
                else if (Targetable(item, &info))
                {
                    item->goal_anim_state = ST_CULT2_AIM2;
                }
                else if (knife->mood == MOOD_BORED)
                {
                    if (!info.ahead || info.distance > CULT2_KNIFE_RANGE)
                        item->goal_anim_state = ST_CULT2_WALK;
                }
                else if (info.ahead && info.distance < CULT2_WALK_RANGE)
                {
                    item->goal_anim_state = ST_CULT2_WALK;
                }
                else
                {
                    item->goal_anim_state = ST_CULT2_RUN;
                }
                break;

            case ST_CULT2_WALK:
                knife->maximum_turn = CULT2_WALK_TURN;

                if (info.ahead)
                    head = info.angle;

                if (knife->mood == MOOD_ESCAPE)
                {
                    item->goal_anim_state = ST_CULT2_RUN;
                }
                else if (Targetable(item, &info))
                {
                    if (info.distance < CULT2_STOP_RANGE || info.src_zone != info.target_zone)
                        item->goal_anim_state = ST_CULT2_STOP;
                    else if (GetRandomControl() < 0x4000)
                        item->goal_anim_state = ST_CULT2_AIM1L;
                    else
                        item->goal_anim_state = ST_CULT2_AIM1R;
                }
                else if (knife->mood == MOOD_BORED)
                {
                    if (info.ahead && info.distance < CULT2_KNIFE_RANGE)
                        item->goal_anim_state = ST_CULT2_STOP;
                }
                else if (!info.ahead || info.distance > CULT2_WALK_RANGE)
                {
                    item->goal_anim_state = ST_CULT2_RUN;
                }
                break;

            case ST_CULT2_RUN:
                knife->maximum_turn = CULT2_RUN_TURN;
                tilt = (angle >> 2);

                if (info.ahead)
                    head = info.angle;

                if (knife->mood == MOOD_ESCAPE)
                {
                    break;
                }
                else if (Targetable(item, &info))
                {
                    item->goal_anim_state = ST_CULT2_WALK;
                }
                else if (knife->mood == MOOD_BORED)
                {
                    if (info.ahead && info.distance < CULT2_KNIFE_RANGE)
                        item->goal_anim_state = ST_CULT2_STOP;
                    else
                        item->goal_anim_state = ST_CULT2_WALK;
                }
                else if (info.ahead && info.distance < CULT2_WALK_RANGE)
                {
                    item->goal_anim_state = ST_CULT2_WALK;
                }
                break;

            case ST_CULT2_AIM1L:
                knife->flags = 0;

                if (info.ahead)
                    torso = info.angle;

                if (Targetable(item, &info))
                    item->goal_anim_state = ST_CULT2_SHOOT1L;
                else
                    item->goal_anim_state = ST_CULT2_WALK;
                break;
            
            case ST_CULT2_AIM1R:
                knife->flags = 0;

                if (info.ahead)
                    torso = info.angle;

                if (Targetable(item, &info))
                    item->goal_anim_state = ST_CULT2_SHOOT1R;
                else
                    item->goal_anim_state = ST_CULT2_WALK;
                break;

            case ST_CULT2_AIM2:
                knife->flags = 0;

                if (info.ahead)
                    torso = info.angle;

                if (Targetable(item, &info))
                    item->goal_anim_state = ST_CULT2_SHOOT2;
                else
                    item->goal_anim_state = ST_CULT2_STOP;
                break;

            case ST_CULT2_SHOOT1L:
                if (info.ahead)
                    torso = info.angle;

                if (!knife->flags)
                {
                    KnifethrowerSlot = item->object_number;
                    CreatureEffect(item, &cult2_left, Knife);
                    knife->flags = 1;
                }
                break;

            case ST_CULT2_SHOOT1R:
                if (info.ahead)
                    torso = info.angle;

                if (!knife->flags)
                {
                    KnifethrowerSlot = item->object_number;
                    CreatureEffect(item, &cult2_right, Knife);
                    knife->flags = 1;
                }
                break;

            case ST_CULT2_SHOOT2:
                if (info.ahead)
                    torso = info.angle;

                if (!knife->flags)
                {
                    KnifethrowerSlot = item->object_number;
                    CreatureEffect(item, &cult2_left, Knife);
                    CreatureEffect(item, &cult2_right, Knife);
                    knife->flags = 1;
                }
                break;
        }
    }

    CreatureTilt(item, tilt);
    CreatureJoint(item, 0, torso);
    CreatureJoint(item, 1, head);
    CreatureAnimation(ItemIndex, angle, 0);
}

void InitTR2_Knifethrower(short ItemIndex)
{
    InitialiseCreature(ItemIndex);
}

void InitialiseTR2_Knifethrower()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        short dependency = Utils.getLinks(TR2_KNIFETHROWER1 + index, LNK_OBJECT1);
        if (dependency >= 0 && dependency < TR_OBJECTS_NUMBER && MapToFirst(dependency, SLOTS_NUMBER) == TR2_KNIFE1 && tomb[dependency].get_object())
        {
            tomb[TR2_KNIFETHROWER1 + index].set_name("Knifethrower");
            tomb[TR2_KNIFETHROWER1 + index].initialise(TR2_KNIFETHROWER1 + index, FUNCTION_SHADOW, 0);
            tomb[TR2_KNIFETHROWER1 + index].set_shadow_size(128);
            tomb[TR2_KNIFETHROWER1 + index].set_func_initialise(InitTR2_Knifethrower);
            tomb[TR2_KNIFETHROWER1 + index].set_func_collide(CreatureCollision);
            tomb[TR2_KNIFETHROWER1 + index].set_func_control(CtrlTR2_Knifethrower);
            tomb[TR2_KNIFETHROWER1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
            tomb[TR2_KNIFETHROWER1 + index].set_health(HP_KNIFETHROWER);
            tomb[TR2_KNIFETHROWER1 + index].set_radius_and_pivot(102, 50);
            tomb[TR2_KNIFETHROWER1 + index].set_hit_effect(HIT_BLOOD);
            tomb[TR2_KNIFETHROWER1 + index].set_creature(TRUE);
            tomb[TR2_KNIFETHROWER1 + index].set_rotation_meshtree(0, ROT_Y);
            tomb[TR2_KNIFETHROWER1 + index].set_rotation_meshtree(8, ROT_Y);
            tomb[TR2_KNIFETHROWER1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
        }
    }
}