#include <StdAfx.h>
#include "TRClass.h"
#include "utility.h"

extern TYPE_DrawAnimatingItem DrawAnimatingItem;

// contains all objects from all TR Object
TRClass tomb[TR_OBJECTS_NUMBER];

void TRClass::set_name(LPCSTR setName)
{
    this->name = setName;
}

void TRClass::initialise(int ID, DWORD function, DWORD query)
{
	StrConfiguration configuration;

    this->id = ID;
    this->slot = Utils.getObjects(ID);
    if (tsu_active && this->slot > SLOT_LARA && this->slot < SLOT_NUMBER_OBJECTS && tsu_slots[this->slot] == NO_ITEM)
    {
        this->obj = &Ptr_VetObjects[this->slot];
        if (this->obj->loaded)
        {
			configuration.Slot = this->slot;
			configuration.FunctionSupport = function;
			configuration.QuerySupport = query;
			Trng.pGlobTomb4->pBaseVariableTRNG->Globals.CurrentValue = (int) &configuration;
			PerformFlipeffect(STRING_PLUGIN_OBJECT_HELPER, HELPER_FLIPEFFECT_FUNCTION_QUERY, tsu_handle, 0);
			if (Trng.pGlobTomb4->pBaseVariableTRNG->Globals.CurrentValue)
			{
				/// default value, fix problem when override some slot !
				this->obj->initialise = NULL;
				this->obj->collision = NULL;
				this->obj->control = NULL;
				this->obj->draw = DrawAnimatingItem;
				this->obj->draw_extra = NULL;
				this->obj->floor = NULL;
				this->obj->ceiling = NULL;
				this->obj->hit_effect = HIT_NONE;
				this->obj->hit_points = 0;
				this->obj->non_lot = FALSE;
				this->obj->radius = 0;
				this->obj->pivot_length = 0;
				this->obj->bit_offset = 0;
				this->obj->save_position = FALSE;
				this->obj->save_anim = FALSE;
				this->obj->save_flags = FALSE;
				this->obj->save_hitpoints = FALSE;
				this->obj->save_mesh = FALSE;
				this->obj->shadow_size = 0;
				this->obj->undead = FALSE;
				this->obj->water_creature = FALSE;
				this->obj->semi_transparent = FALSE;
				this->obj->intelligent = FALSE;
				this->obj->explodable_meshbits = 0;
				tsu_slots[this->slot] = this->id;
				return;
			}
        }
    }
    this->obj = nullptr;
}

void TRClass::set_health(short defaultHP)
{
    if (this->obj != nullptr)
    {
        this->hp = Utils.getObjectAttribute(this->id, ATT_HP);

        if (this->hp != NO_ITEM && defaultHP != HP_CANT_BE_TARGETED)
        {
            this->obj->hit_points = this->hp;
        }
        else
        {
            this->obj->hit_points = defaultHP;
        }
    }
}

void TRClass::set_func_initialise(LPVOID initialise)
{
    if (this->obj != nullptr && initialise != NULL)
        this->obj->initialise = initialise;
}

// this function need to be after setItemBitOffset() !!!!
void TRClass::set_func_draw(LPVOID draw, LPVOID draw_extra)
{
    if (this->obj != nullptr)
    {
        if (draw != NULL)
            this->obj->draw = draw;
        if (draw_extra != NULL)
            this->obj->draw_extra = draw_extra;
    }
}

void TRClass::set_func_collide(LPVOID collision)
{
    if (this->obj != nullptr && collision != NULL)
        this->obj->collision = collision;
}

void TRClass::set_func_floor_and_ceiling(LPVOID floor, LPVOID ceiling)
{
    if (this->obj != nullptr)
    {
        if (floor != NULL)
            this->obj->floor = floor;
        if (ceiling != NULL)
            this->obj->ceiling = ceiling;
    }
}

void TRClass::set_func_control(LPVOID control)
{
    if (this->obj != nullptr && control != NULL)
        this->obj->control = control;
}

void TRClass::set_save(BOOL hp, BOOL anim, BOOL flags, BOOL mesh, BOOL position)
{
    if (this->obj != nullptr)
    {
        this->obj->save_hitpoints = hp;
        this->obj->save_anim = anim;
        this->obj->save_flags = flags;
        this->obj->save_mesh = mesh;
        this->obj->save_position = position;
    }
}

void TRClass::set_hit_effect(int HIT_flags)
{
    if (this->obj != nullptr)
        this->obj->hit_effect = HIT_flags;
}

void TRClass::set_shadow_size(int size)
{
    if (this->obj != nullptr)
        this->obj->shadow_size = size;
}

void TRClass::set_radius_and_pivot(int radius, int pivot_length)
{
    if (this->obj != nullptr)
    {
        this->obj->radius = radius;
        this->obj->pivot_length = pivot_length;
    }
}

void TRClass::set_water_creature(BOOL result)
{
    if (this->obj != nullptr && result)
        this->obj->water_creature = TRUE;
}

void TRClass::set_creature(BOOL result)
{
    if (this->obj != nullptr && result)
    {
        this->obj->intelligent = TRUE;
        this->obj->non_lot = FALSE;     // force non_lot to be flase (enabled) for enable the pathfinding !
    }
}

void TRClass::set_transparency(BOOL result)
{
    if (this->obj != nullptr && result)
        this->obj->semi_transparent = TRUE;
}

void TRClass::set_bitoffset(WORD offset)
{
    if (this->obj != nullptr)
        this->obj->bit_offset = offset;
}

void TRClass::set_rotation_meshtree(int mesh_id, short ROT_flags)
{
    if (this->obj != nullptr)
        Ptr_VetAnimationsMeshTree[this->obj->bone_index + mesh_id * 4] |= (ROT_flags);
}

void TRClass::set_explodable_meshbits(int meshbits)
{
    if (this->obj != nullptr && Utils.getObjectOption(this->id, OPT_HEAD) > 0)
		this->obj->explodable_meshbits = meshbits;
}

void TRClass::set_meshswap(int mesh_id)
{
    if (this->obj != nullptr)
	{
		short slot = Utils.getObjectMeshswap(this->id, mesh_id, MSH_SLOT);
		short mesh = Utils.getObjectMeshswap(this->id, mesh_id, MSH_MESH);
		if (slot > SLOT_LARA && slot < SLOT_NUMBER_OBJECTS && mesh >= 0 && mesh < 32)
		{
			OBJECT_INFO* object = &Ptr_VetObjects[slot];
			if (object->loaded)
				Ptr_VetMeshes[this->obj->mesh_index + mesh_id * 2 + 1] = Ptr_VetMeshes[object->mesh_index + mesh * 2];
		}
	}
}

const char* TRClass::get_name()
{
    return this->name;
}

short TRClass::get_id()
{
    return this->id;
}

short TRClass::get_health()
{
    return this->hp;
}

OBJECT_INFO* TRClass::get_object()
{
    return this->obj;
}
