#include <StdAfx.h>
#include "baldy.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 BALDY_SHOT_DAMAGE 150
#define BALDY_WALK_TURN (ONE_DEGREE * 3) // = 546
#define BALDY_RUN_TURN (ONE_DEGREE * 6) // = 1092
#define BALDY_WALK_RANGE SQUARE(WALL_L * 4) // = 16777216
#define BALDY_DIE_ANIM 14
#define BALDY_HITPOINTS 200
#define BALDY_RADIUS (WALL_L / 10) // = 102
#define BALDY_SMARTNESS 0x7FFF

static BITE_INFO BaldyGun = EnemyBite[TROFF_BALDY];

enum BALDY_ANIM {
    BALDY_EMPTY = 0,
    BALDY_STOP = 1,
    BALDY_WALK = 2,
    BALDY_RUN = 3,
    BALDY_AIM = 4,
    BALDY_DEATH = 5,
    BALDY_SHOOT = 6,
};

void InitTR1_Baldy(short item_num)
{
    InitialiseCreature(item_num);
}

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

    ITEM_INFO* item = &Ptr_VetItems[item_num];
    CREATURE_INFO* baldy = (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 != BALDY_DEATH) {
            item->current_anim_state = BALDY_DEATH;
            item->anim_number =
                Ptr_VetObjects[item->object_number].anim_index + BALDY_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, baldy->maximum_turn);

        switch (item->current_anim_state) {
        case BALDY_STOP:
			baldy->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 = BALDY_AIM;
            } else if (baldy->mood == MOOD_BORED) {
                item->goal_anim_state = BALDY_WALK;
            } else {
                item->goal_anim_state = BALDY_RUN;
            }
            break;

        case BALDY_WALK:
            baldy->maximum_turn = BALDY_WALK_TURN;
            if (baldy->mood == MOOD_ESCAPE || !info.ahead) {
                item->required_anim_state = BALDY_RUN;
                item->goal_anim_state = BALDY_STOP;
            } else if (Targetable(item, &info)) {
                item->required_anim_state = BALDY_AIM;
                item->goal_anim_state = BALDY_STOP;
            } else if (info.distance > BALDY_WALK_RANGE) {
                item->required_anim_state = BALDY_RUN;
                item->goal_anim_state = BALDY_STOP;
            }
            break;

        case BALDY_RUN:
            baldy->maximum_turn = BALDY_RUN_TURN;
            tilt = angle / 2;
            if (baldy->mood != MOOD_ESCAPE || info.ahead) {
                if (Targetable(item, &info)) {
                    item->required_anim_state = BALDY_AIM;
                    item->goal_anim_state = BALDY_STOP;
                } else if (info.ahead && info.distance < BALDY_WALK_RANGE) {
                    item->required_anim_state = BALDY_WALK;
                    item->goal_anim_state = BALDY_STOP;
                }
            }
            break;

        case BALDY_AIM:
            baldy->flags = 0;
            if (item->required_anim_state) {
                item->goal_anim_state = BALDY_STOP;
            } else if (Targetable(item, &info)) {
                item->goal_anim_state = BALDY_SHOOT;
            } else {
                item->goal_anim_state = BALDY_STOP;
            }
            break;

        case BALDY_SHOOT:
            if (!baldy->flags) {
				info.distance /= 2;
				Utils.EnemyShot(item, &info, &BaldyGun, head, DOUBLE_VALUE_ITEM(dmg1, BALDY_SHOT_DAMAGE));
				item->fired_weapon = 2;
                baldy->flags = 1;
            }
            if (baldy->mood == MOOD_ESCAPE) {
                item->required_anim_state = BALDY_RUN;
            }
            break;
        }
    }

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

void InitialiseTR1_Baldy()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR1_MR_T1 + index].set_name("Mister T");
        tomb[TR1_MR_T1 + index].initialise(TR1_MR_T1 + index, FUNCTION_SHADOW, QUERY_GUNFLASH);
        tomb[TR1_MR_T1 + index].set_shadow_size(128);
        tomb[TR1_MR_T1 + index].set_func_initialise(InitTR1_Baldy);
        tomb[TR1_MR_T1 + index].set_func_collide(CreatureCollision);
        tomb[TR1_MR_T1 + index].set_func_control(CtrlTR1_Baldy);
        tomb[TR1_MR_T1 + index].set_bitoffset(TROFF_BALDY);
        tomb[TR1_MR_T1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR1_MR_T1 + index].set_health(BALDY_HITPOINTS);
        tomb[TR1_MR_T1 + index].set_radius_and_pivot(BALDY_RADIUS, 0);
        tomb[TR1_MR_T1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR1_MR_T1 + index].set_creature(TRUE);
        tomb[TR1_MR_T1 + index].set_rotation_meshtree(0, ROT_Y);
        tomb[TR1_MR_T1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}
