#include <StdAfx.h>
#include "wk_shotgun.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_DrawAnimatingItem DrawAnimatingItem;
extern TYPE_SoundEffect SoundEffect;
extern TYPE_GetRandomControl GetRandomControl;
extern TYPE_Targetable Targetable;
extern TYPE_ShotLara ShotLara;

#define HP_WORK1 (25)
#define WORK1_WALK_TURN (546)
#define WORK1_RUN_TURN (910)
#define WORK1_DAMAGE (150)
#define WORK1_RUN_DISTANCE (0x400000)
#define WORK1_SHOOT_DISTANCE (0x900000)
#define WORK1_DEAD_ANIM 18
#define WORK1_IDLE_ANIM 5

static BITE_INFO work1_bite = EnemyBite[TROFF_WKSHOTGUN];

enum WORKER1_STATE {
    ST_WORK1_EMPTY,
    ST_WORK1_WALK,
    ST_WORK1_STOP,
    ST_WORK1_WAIT,
    ST_WORK1_SHOOT1,
    ST_WORK1_RUN,
    ST_WORK1_SHOOT2,
    ST_WORK1_DEATH,
    ST_WORK1_AIM1,
    ST_WORK1_AIM2,
    ST_WORK1_SHOOT3
};

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

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

    item = &Ptr_VetItems[item_number];
    shotgun = (CREATURE_INFO*)item->data;
    angle = head_y = torso_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_WORK1_DEATH)
        {
            item->anim_number = Ptr_VetObjects[item->object_number].anim_index + WORK1_DEAD_ANIM;
            item->frame_number = Ptr_VetAnimations[item->anim_number].frame_base;
            item->current_anim_state = ST_WORK1_DEATH;
        }
    }
    else
    {
        CreatureAIInfo(item, &info);
        GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);
        angle = CreatureTurn(item, shotgun->maximum_turn);

        switch (item->current_anim_state)
        {
            case ST_WORK1_STOP:
                shotgun->flags = 0;
                shotgun->maximum_turn = 0;

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

                if (shotgun->mood == MOOD_ESCAPE)
                {
                    item->goal_anim_state = ST_WORK1_RUN;
                }
                else if (Targetable(item, &info))
                {
                    if (info.distance < WORK1_SHOOT_DISTANCE || info.src_zone != info.target_zone)
                    {
                        item->goal_anim_state = (GetRandomControl() >= 0x4000) ? ST_WORK1_AIM2 : ST_WORK1_AIM1;
                    }
                    else
                    {
                        item->goal_anim_state = ST_WORK1_WALK;
                    }
                }
                else if (shotgun->mood != MOOD_BORED || !info.ahead)
                {
                    item->goal_anim_state = (info.distance <= WORK1_RUN_DISTANCE) ? ST_WORK1_WALK : ST_WORK1_RUN;
                }
                else
                {
                    item->goal_anim_state = ST_WORK1_WAIT;
                }
                break;

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

                if (Targetable(item, &info))
                {
                    item->goal_anim_state = ST_WORK1_SHOOT1;
                }
                else if (shotgun->mood != MOOD_BORED || !info.ahead)
                {
                    item->goal_anim_state = ST_WORK1_STOP;
                }
                break;

            case ST_WORK1_WALK:
                shotgun->flags = 0;
                shotgun->maximum_turn = WORK1_WALK_TURN;

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

                if (shotgun->mood == MOOD_ESCAPE)
                {
                    item->goal_anim_state = ST_WORK1_RUN;
                }
                else if (Targetable(item, &info))
                {
                    if (info.distance < WORK1_SHOOT_DISTANCE || info.src_zone != info.target_zone)
                        item->goal_anim_state = ST_WORK1_STOP;
                    else
                        item->goal_anim_state = ST_WORK1_SHOOT2;
                }
                else if (shotgun->mood != MOOD_BORED || !info.ahead)
                {
                    if (info.distance > WORK1_RUN_DISTANCE)
                        item->goal_anim_state = ST_WORK1_RUN;
                }
                else
                {
                    item->goal_anim_state = ST_WORK1_STOP;
                }
                break;

            case ST_WORK1_RUN:
                shotgun->maximum_turn = WORK1_RUN_TURN;
                tilt = (angle / 2);

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

                if (shotgun->mood != MOOD_ESCAPE)
                {
                    if (Targetable(item, &info))
                    {
                        item->goal_anim_state = ST_WORK1_WALK;
                    }
                    else if (shotgun->mood == MOOD_BORED || shotgun->mood == MOOD_STALK)
                    {
                        item->goal_anim_state = ST_WORK1_WALK;
                    }
                }
                break;

            case ST_WORK1_AIM1:
                shotgun->flags = 0;

                if (info.ahead)
                {
                    torso_y = info.angle;
                }
                break;

            case ST_WORK1_AIM2:
                shotgun->flags = 0;

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

                if (Targetable(item, &info))
                {
                    item->goal_anim_state = ST_WORK1_SHOOT3;
                }
                break;

            case ST_WORK1_SHOOT1:
            case ST_WORK1_SHOOT3:
                if (info.ahead)
                {
                    torso_y = info.angle;
                }

                if (!shotgun->flags)
                {
                    Utils.EnemyShot(item, &info, &work1_bite, torso_y, DOUBLE_VALUE_ANIM(item->current_anim_state, ST_WORK1_SHOOT1, DOUBLE_VALUE_ITEM(dmg1, WORK1_DAMAGE), DOUBLE_VALUE_ITEM(dmg3, WORK1_DAMAGE)));
                    item->fired_weapon = 2;
                    shotgun->flags = 1;
                }

                if (item->goal_anim_state != ST_WORK1_STOP
                && (shotgun->mood == MOOD_ESCAPE || info.distance > WORK1_SHOOT_DISTANCE || !Targetable(item, &info)))
                {
                    item->goal_anim_state = ST_WORK1_STOP;
                }
                break;

            case ST_WORK1_SHOOT2:
                if (info.ahead)
                {
                    torso_y = info.angle;
                }

                if (!shotgun->flags)
                {
                    Utils.EnemyShot(item, &info, &work1_bite, torso_y, DOUBLE_VALUE_ITEM(dmg2, WORK1_DAMAGE));
                    item->fired_weapon = 2;
                    shotgun->flags = 1;
                }
                break;
        }
    }

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

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

void InitialiseTR2_GoonShotgun()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].initialise(TR2_RIG_WORKER_KHAKI_PANTS1 + index, FUNCTION_SHADOW, QUERY_GUNFLASH);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_name("Goon Shotgun");
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_shadow_size(128);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_func_initialise(InitTR2_WKShotgun);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_func_collide(CreatureCollision);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_func_control(CtrlTR2_WKShotgun);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_bitoffset(TROFF_WKSHOTGUN);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_health(HP_WORK1);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_radius_and_pivot(102, 0);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_creature(TRUE);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_rotation_meshtree(4, ROT_Y);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_rotation_meshtree(13, ROT_Y);
        tomb[TR2_RIG_WORKER_KHAKI_PANTS1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}