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

extern TYPE_InitialiseCreature InitialiseCreature;
extern TYPE_CreatureCollision CreatureCollision;
extern TYPE_CreatureActive CreatureActive;
extern TYPE_CreatureAIInfo CreatureAIInfo;
extern TYPE_GetCreatureMood GetCreatureMood;
extern TYPE_CreatureMood CreatureMood;
extern TYPE_CreatureTurn CreatureTurn;
extern TYPE_Targetable Targetable;
extern TYPE_CreatureTilt CreatureTilt;
extern TYPE_CreatureJoint CreatureJoint;
extern TYPE_CreatureAnimation CreatureAnimation;

#define COWBOY_SHOT_DAMAGE 70
#define COWBOY_WALK_TURN (ONE_DEGREE * 3) // = 546
#define COWBOY_RUN_TURN (ONE_DEGREE * 6) // = 1092
#define COWBOY_WALK_RANGE SQUARE(WALL_L * 3) // = 9437184
#define COWBOY_DIE_ANIM 7
#define COWBOY_HITPOINTS 150
#define COWBOY_RADIUS (WALL_L / 10) // = 102
#define COWBOY_SMARTNESS 0x7FFF

static BITE_INFO CowboyGun1 = EnemyBite[TROFF_COWBOY_LEFT];
static BITE_INFO CowboyGun2 = EnemyBite[TROFF_COWBOY_RIGHT];

enum COWBOY_ANIM {
    COWBOY_EMPTY = 0,
    COWBOY_STOP = 1,
    COWBOY_WALK = 2,
    COWBOY_RUN = 3,
    COWBOY_AIM = 4,
    COWBOY_DEATH = 5,
    COWBOY_SHOOT = 6,
};

void CtrlTR1_Cowboy(short item_num)
{
    if (!CreatureActive(item_num))
        return;

    ITEM_INFO* item = &Ptr_VetItems[item_num];
    CREATURE_INFO* cowboy = (CREATURE_INFO*) item->data;
    short head = 0;
    short angle = 0;
    short tilt = 0;
    short dmg1 = Utils.getObjectAttribute(tsu_slots[item->object_number], ATT_DMG1);

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

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

		GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);

        angle = CreatureTurn(item, cowboy->maximum_turn);

        switch (item->current_anim_state) {
        case COWBOY_STOP:
			cowboy->maximum_turn = 0;
            if (item->required_anim_state) {
                item->goal_anim_state = item->required_anim_state;
            } else if (Targetable(item, &info)) {
                item->goal_anim_state = COWBOY_AIM;
            } else if (cowboy->mood == MOOD_BORED) {
                item->goal_anim_state = COWBOY_WALK;
            } else {
                item->goal_anim_state = COWBOY_RUN;
            }
            break;

        case COWBOY_WALK:
            cowboy->maximum_turn = COWBOY_WALK_TURN;
            if (cowboy->mood == MOOD_ESCAPE || !info.ahead) {
                item->required_anim_state = COWBOY_RUN;
                item->goal_anim_state = COWBOY_STOP;
            } else if (Targetable(item, &info)) {
                item->required_anim_state = COWBOY_AIM;
                item->goal_anim_state = COWBOY_STOP;
            } else if (info.distance > COWBOY_WALK_RANGE) {
                item->required_anim_state = COWBOY_RUN;
                item->goal_anim_state = COWBOY_STOP;
            }
            break;

        case COWBOY_RUN:
            cowboy->maximum_turn = COWBOY_RUN_TURN;
            tilt = angle / 2;
            if (cowboy->mood != MOOD_ESCAPE || info.ahead) {
                if (Targetable(item, &info)) {
                    item->required_anim_state = COWBOY_AIM;
                    item->goal_anim_state = COWBOY_STOP;
                } else if (info.ahead && info.distance < COWBOY_WALK_RANGE) {
                    item->required_anim_state = COWBOY_WALK;
                    item->goal_anim_state = COWBOY_STOP;
                }
            }
            break;

        case COWBOY_AIM:
            cowboy->flags = 0;
            if (item->required_anim_state) {
                item->goal_anim_state = COWBOY_STOP;
            } else if (Targetable(item, &info)) {
                item->goal_anim_state = COWBOY_SHOOT;
            } else {
                item->goal_anim_state = COWBOY_STOP;
            }
            break;

        case COWBOY_SHOOT:
            if (!cowboy->flags) {
				Utils.EnemyShot(item, &info, &CowboyGun1, head, DOUBLE_VALUE_ITEM(dmg1, COWBOY_SHOT_DAMAGE));
				item->fired_weapon = 2;
				item->reserved_1 = 1;
            } else if (cowboy->flags == 6) {
				Utils.EnemyShot(item, &info, &CowboyGun2, head, DOUBLE_VALUE_ITEM(dmg1, COWBOY_SHOT_DAMAGE));
				item->fired_weapon = 2;
				item->reserved_1 = 2;
            }
            cowboy->flags++;

            if (cowboy->mood == MOOD_ESCAPE) {
                item->required_anim_state = COWBOY_RUN;
            }
            break;
        }
    }

    CreatureTilt(item, tilt);
    CreatureJoint(item, 0, head);
    CreatureAnimation(item_num, angle, 0);
}

void InitialiseTR1_Cowboy()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR1_COWBOY1 + index].set_name("Cowboy");
        tomb[TR1_COWBOY1 + index].initialise(TR1_COWBOY1 + index, FUNCTION_SHADOW, QUERY_GUNFLASH);
        tomb[TR1_COWBOY1 + index].set_shadow_size(128);
        tomb[TR1_COWBOY1 + index].set_func_initialise(InitialiseCreature);
        tomb[TR1_COWBOY1 + index].set_func_collide(CreatureCollision);
        tomb[TR1_COWBOY1 + index].set_func_control(CtrlTR1_Cowboy);
        tomb[TR1_COWBOY1 + index].set_bitoffset(TROFF_COWBOY_LEFT | DOUBLE_OFFSET_MUZZLEFLASH);
        tomb[TR1_COWBOY1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR1_COWBOY1 + index].set_health(COWBOY_HITPOINTS);
        tomb[TR1_COWBOY1 + index].set_radius_and_pivot(COWBOY_RADIUS, 0);
        tomb[TR1_COWBOY1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR1_COWBOY1 + index].set_creature(TRUE);
        tomb[TR1_COWBOY1 + index].set_rotation_meshtree(0, ROT_Y);
        tomb[TR1_COWBOY1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}
