void CRecastContainer::Check(uint32 tick)
{
	DSP_DEBUG_BREAK_IF(tick == 0);

    for (uint8 type = 0; type < MAX_RECASTTPE_SIZE; ++type)
    {
        RecastList_t* PRecastList = GetRecastList((RECASTTYPE)type);

        for (uint16 i = 0; i < PRecastList->size(); ++i)
	    {
		    Recast_t* recast = PRecastList->at(i);

		    if (tick >= (recast->TimeStamp + recast->RecastTime))
		    {
                if (type == RECAST_ITEM)
                {
                    CItem* PItem = m_PChar->getStorage(LOC_INVENTORY)->GetItem(recast->ID);

                    m_PChar->pushPacket(new CInventoryItemPacket(PItem, LOC_INVENTORY, recast->ID));
	                m_PChar->pushPacket(new CInventoryFinishPacket());
                }
                if (type == RECAST_ITEM || type == RECAST_MAGIC || type == RECAST_LOOT)
                {
                    PRecastList->erase(PRecastList->begin() + i--);
                    delete recast;
                }
                else
                {
                    recast->RecastTime = 0;
                }
		    }
	    }
    }
}
void CRecastContainer::DeleteByIndex(RECASTTYPE type, uint8 index)
{
	RecastList_t* PRecastList = GetRecastList(type);
    if (type == RECAST_ABILITY)
        Sql_Query(SqlHandle, "DELETE FROM char_recast WHERE charid = %u AND id = %u;", m_PChar->id, PRecastList->at(index)->ID);
	delete PRecastList->at(index);
	PRecastList->erase(PRecastList->begin() + index);
}
void CRecastContainer::DeleteByIndex(RECASTTYPE type, uint8 index)
{
    RecastList_t* PRecastList = GetRecastList(type);
    if (type == RECAST_ABILITY)
    {
        PRecastList->at(index).RecastTime = 0;
    }
    else
    {
        PRecastList->erase(PRecastList->begin() + index);
    }
}
void CRecastContainer::Del(RECASTTYPE type, uint16 id)
{
    RecastList_t* PRecastList = GetRecastList(type);

    for (uint16 i = 0; i < PRecastList->size(); ++i)
	{
        if (PRecastList->at(i)->ID == id)
        {
            delete PRecastList->at(i);
            PRecastList->erase(PRecastList->begin() + i);
            return;
        }
	}
}
void CRecastContainer::Del(RECASTTYPE type, uint16 id)
{
    RecastList_t* PRecastList = GetRecastList(type);

    for (uint16 i = 0; i < PRecastList->size(); ++i)
	{
        if (PRecastList->at(i)->ID == id)
        {
            if (type == RECAST_ABILITY)
                Sql_Query(SqlHandle, "DELETE FROM char_recast WHERE charid = %u AND id = %u;", m_PChar->id, PRecastList->at(i)->ID);
            delete PRecastList->at(i);
            PRecastList->erase(PRecastList->begin() + i);
            return;
        }
	}
}
void CRecastContainer::Del(RECASTTYPE type, uint16 id)
{
    RecastList_t* PRecastList = GetRecastList(type);

    if (type == RECAST_ABILITY)
    {
        if (auto recast = GetRecast(RECAST_ABILITY, id))
        {
            recast->RecastTime = 0;
        }
    }
    else
    {
        PRecastList->erase(std::remove_if(PRecastList->begin(), PRecastList->end(), [&id](auto& recast)
        {
            return recast.ID == id;
        }), PRecastList->end());
    }
}
void CRecastContainer::Check()
{
    for (auto type : {RECAST_MAGIC, RECAST_ABILITY})
    {
        RecastList_t* PRecastList = GetRecastList(type);

        for (uint16 i = 0; i < PRecastList->size(); ++i)
        {
            Recast_t* recast = &PRecastList->at(i);

            if (time(nullptr) >= (recast->TimeStamp + recast->RecastTime))
            {
                if (type == RECAST_MAGIC)
                {
                    PRecastList->erase(PRecastList->begin() + i--);
                }
                else
                {
                    recast->RecastTime = 0;
                }
            }
        }
    }
}
void CRecastContainer::DeleteByIndex(RECASTTYPE type, uint8 index)
{
	RecastList_t* PRecastList = GetRecastList(type);
	delete PRecastList->at(index);
	PRecastList->erase(PRecastList->begin() + index);
}