#include <StdAfx.h>
#include "bat.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_CreatureEffect CreatureEffect;
extern TYPE2_DoBloodSplat DoBloodSplat_2;
extern TYPE_CreatureAnimation CreatureAnimation;

#define BAT_ATTACK_DAMAGE 2
#define BAT_TURN (20 * ONE_DEGREE) // = 3640
#define BAT_HITPOINTS 1
#define BAT_RADIUS (WALL_L / 10) // = 102
#define BAT_SMARTNESS 0x400

static BITE_INFO BatBite = { 0, 16, 45, 4 };

enum BAT_ANIM {
    BAT_EMPTY = 0,
    BAT_STOP = 1,
    BAT_FLY = 2,
    BAT_ATTACK = 3,
    BAT_FALL = 4,
    BAT_DEATH = 5,
};

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

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

    if (item->hit_points <= 0) {
        if (item->pos.y_pos < item->floor) {
            item->gravity_status = 1;
            item->goal_anim_state = BAT_FALL;
            item->speed = 0;
        } else {
            item->gravity_status = 0;
            item->goal_anim_state = BAT_DEATH;
            item->pos.y_pos = item->floor;
        }
    } else {
        AI_INFO info;

        CreatureAIInfo(item, &info);
        GetCreatureMood(item, &info, VIOLENT);
        CreatureMood(item, &info, VIOLENT);
        angle = CreatureTurn(item, BAT_TURN);

        switch (item->current_anim_state) {
        case BAT_STOP:
            item->goal_anim_state = BAT_FLY;
            break;

        case BAT_FLY:
            if (item->touch_bits) {
                item->goal_anim_state = BAT_ATTACK;
            }
            break;

        case BAT_ATTACK:
            if (item->touch_bits) {
                CreatureEffect(item, &BatBite, DoBloodSplat_2);
                LaraItem->hit_points -= DOUBLE_VALUE_ITEM(dmg1, BAT_ATTACK_DAMAGE);
                LaraItem->hit_status = 1;
            } else {
                item->goal_anim_state = BAT_FLY;
                bat->mood = MOOD_BORED;
            }
            break;
        }
    }

    CreatureAnimation(item_num, angle, 0);
}

void InitialiseTR1_Bat()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR1_BAT1 + index].set_name("Bat");
        tomb[TR1_BAT1 + index].initialise(TR1_BAT1 + index, FUNCTION_ZONE_BEFORE | FUNCTION_SHADOW, 0);
        tomb[TR1_BAT1 + index].set_shadow_size(128);
        tomb[TR1_BAT1 + index].set_func_initialise(InitialiseCreature);
        tomb[TR1_BAT1 + index].set_func_collide(CreatureCollision);
        tomb[TR1_BAT1 + index].set_func_control(CtrlTR1_Bat);
        tomb[TR1_BAT1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR1_BAT1 + index].set_health(BAT_HITPOINTS);
        tomb[TR1_BAT1 + index].set_radius_and_pivot(BAT_RADIUS, 0);
        tomb[TR1_BAT1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR1_BAT1 + index].set_creature(TRUE);
        tomb[TR1_BAT1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}
