#include <StdAfx.h>
#include "larson.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_GetRandomControl GetRandomControl;
extern TYPE_Targetable Targetable;
extern TYPE_CreatureTilt CreatureTilt;
extern TYPE_CreatureJoint CreatureJoint;
extern TYPE_CreatureAnimation CreatureAnimation;

#define LARSON_POSE_CHANCE 0x60 // = 96
#define LARSON_SHOT_DAMAGE 50
#define LARSON_WALK_TURN (ONE_DEGREE * 3) // = 546
#define LARSON_RUN_TURN (ONE_DEGREE * 6) // = 1092
#define LARSON_WALK_RANGE SQUARE(WALL_L * 3) // = 9437184
#define LARSON_DIE_ANIM 15
#define LARSON_HITPOINTS 50
#define LARSON_RADIUS (WALL_L / 10) // = 102
#define LARSON_SMARTNESS 0x7FFF

static BITE_INFO LarsonGun = EnemyBite[TROFF_LARSON];

enum LARSON_ANIM {
    LARSON_EMPTY = 0,
    LARSON_STOP = 1,
    LARSON_WALK = 2,
    LARSON_RUN = 3,
    LARSON_AIM = 4,
    LARSON_DEATH = 5,
    LARSON_POSE = 6,
    LARSON_SHOOT = 7,
};

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

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

        switch (item->current_anim_state) {
        case LARSON_STOP:
			person->maximum_turn = 0;
            if (item->required_anim_state) {
                item->goal_anim_state = item->required_anim_state;
            } else if (person->mood == MOOD_BORED) {
                item->goal_anim_state = GetRandomControl() < LARSON_POSE_CHANCE
                    ? LARSON_POSE
                    : LARSON_WALK;
            } else if (person->mood == MOOD_ESCAPE) {
                item->goal_anim_state = LARSON_RUN;
            } else {
                item->goal_anim_state = LARSON_WALK;
            }
            break;

        case LARSON_POSE:
            if (person->mood != MOOD_BORED) {
                item->goal_anim_state = LARSON_STOP;
            } else if (GetRandomControl() < LARSON_POSE_CHANCE) {
                item->required_anim_state = LARSON_WALK;
                item->goal_anim_state = LARSON_STOP;
            }
            break;

        case LARSON_WALK:
            person->maximum_turn = LARSON_WALK_TURN;
            if (person->mood == MOOD_BORED
                && GetRandomControl() < LARSON_POSE_CHANCE) {
                item->required_anim_state = LARSON_POSE;
                item->goal_anim_state = LARSON_STOP;
            } else if (person->mood == MOOD_ESCAPE) {
                item->required_anim_state = LARSON_RUN;
                item->goal_anim_state = LARSON_STOP;
            } else if (Targetable(item, &info)) {
                item->required_anim_state = LARSON_AIM;
                item->goal_anim_state = LARSON_STOP;
            } else if (!info.ahead || info.distance > LARSON_WALK_RANGE) {
                item->required_anim_state = LARSON_RUN;
                item->goal_anim_state = LARSON_STOP;
            }
            break;

        case LARSON_RUN:
            person->maximum_turn = LARSON_RUN_TURN;
            tilt = angle / 2;
            if (person->mood == MOOD_BORED
                && GetRandomControl() < LARSON_POSE_CHANCE) {
                item->required_anim_state = LARSON_POSE;
                item->goal_anim_state = LARSON_STOP;
            } else if (Targetable(item, &info)) {
                item->required_anim_state = LARSON_AIM;
                item->goal_anim_state = LARSON_STOP;
            } else if (info.ahead && info.distance < LARSON_WALK_RANGE) {
                item->required_anim_state = LARSON_WALK;
                item->goal_anim_state = LARSON_STOP;
            }
            break;

        case LARSON_AIM:
            if (item->required_anim_state) {
                item->goal_anim_state = item->required_anim_state;
            } else if (Targetable(item, &info)) {
                item->goal_anim_state = LARSON_SHOOT;
            } else {
                item->goal_anim_state = LARSON_STOP;
            }
            break;

        case LARSON_SHOOT:
            if (!item->required_anim_state) {
				Utils.EnemyShot(item, &info, &LarsonGun, head, DOUBLE_VALUE_ITEM(dmg1, LARSON_SHOT_DAMAGE));
				item->fired_weapon = 2;
                item->required_anim_state = LARSON_AIM;
            }
            if (person->mood == MOOD_ESCAPE) {
                item->required_anim_state = LARSON_STOP;
            }
            break;
        }
    }

    CreatureTilt(item, tilt);
    CreatureJoint(item, 0, head);

    CreatureAnimation(item_num, angle, 0);
}

void InitialiseTR1_Larson()
{
    for (int index = 0; index < SLOTS_NUMBER; ++index)
    {
        tomb[TR1_LARSON1 + index].set_name("Larson");
        tomb[TR1_LARSON1 + index].initialise(TR1_LARSON1 + index, FUNCTION_SHADOW, QUERY_GUNFLASH);
        tomb[TR1_LARSON1 + index].set_shadow_size(128);
        tomb[TR1_LARSON1 + index].set_func_initialise(InitialiseCreature);
        tomb[TR1_LARSON1 + index].set_func_collide(CreatureCollision);
        tomb[TR1_LARSON1 + index].set_func_control(CtrlTR1_Larson);
        tomb[TR1_LARSON1 + index].set_bitoffset(TROFF_LARSON);
        tomb[TR1_LARSON1 + index].set_func_draw(DrawAnimatingItemCustom, NULL);
        tomb[TR1_LARSON1 + index].set_health(LARSON_HITPOINTS);
        tomb[TR1_LARSON1 + index].set_radius_and_pivot(LARSON_RADIUS, 0);
        tomb[TR1_LARSON1 + index].set_hit_effect(HIT_BLOOD);
        tomb[TR1_LARSON1 + index].set_creature(TRUE);
        tomb[TR1_LARSON1 + index].set_rotation_meshtree(6, ROT_Y);
        tomb[TR1_LARSON1 + index].set_save(TRUE, TRUE, TRUE, FALSE, TRUE);
    }
}
