--龙炼装勇士-龙血秘石
local s,id,o=GetID()
function s.initial_effect(c)
    aux.AddFusionProcFun2(c,s.matfilter,aux.FilterBoolFunction(Card.IsFusionSetCard,0xe1),true)
    local e1=Effect.CreateEffect(c)
    e1:SetCategory(CATEGORY_DRAW)
    e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
    e1:SetProperty(EFFECT_FLAG_DELAY)
    e1:SetCode(EVENT_SPSUMMON_SUCCESS)
    e1:SetCountLimit(1,id)
    e1:SetCondition(s.con1)
    e1:SetTarget(s.tg1)
    e1:SetOperation(s.op1)
    c:RegisterEffect(e1)
    local e2=Effect.CreateEffect(c)
    e2:SetCategory(CATEGORY_EQUIP)
    e2:SetType(EFFECT_TYPE_QUICK_O)
    e2:SetCode(EVENT_FREE_CHAIN)
    e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
    e2:SetHintTiming(0,TIMINGS_CHECK_MONSTER+TIMING_MAIN_END)
    e2:SetCountLimit(1)
    e2:SetRange(LOCATION_MZONE)
    e2:SetCondition(s.con2)
    e2:SetTarget(s.tg2)
    e2:SetOperation(s.op2)
    c:RegisterEffect(e2)
end
function s.matfilter(c)
    return c:IsLevelAbove(8) or c:IsLevel(0)
end
function s.con1(e,tp,eg,ep,ev,re,r,rp)
return e:GetHandler():IsSummonLocation(LOCATION_EXTRA)
end
function s.filter1(c,tp)
    return c:IsSetCard(0xe1) and c:IsFaceup()
end
function s.tg1(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then
        local g=Duel.GetMatchingGroup(s.filter1,tp,LOCATION_EXTRA,0,nil)
        local ct=s.count_unique_code(g)
        e:SetLabel(ct)
        return ct>0 and Duel.IsPlayerCanDraw(tp,ct)
    end
    Duel.SetTargetParam(e:GetLabel())
    Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,e:GetLabel())
end
function s.op1(e,tp,eg,ep,ev,re,r,rp)
    local g=Duel.GetMatchingGroup(s.filter1,tp,LOCATION_EXTRA,0,nil)
    local ct=s.count_unique_code(g)
    Duel.Draw(tp,ct,REASON_EFFECT)
end
function s.count_unique_code(g)
    local check={}
    local count=0
    local tc=g:GetFirst()
    while tc do
        for i,code in ipairs({tc:GetCode()}) do
            if not check[code] then
                check[code]=true
                count=count+1
            end
        end
        tc=g:GetNext()
    end
    return count
end
function s.con2(e,tp,eg,ep,ev,re,r,rp)
    local ph=Duel.GetCurrentPhase()
    return ph==PHASE_MAIN1 or ph==PHASE_MAIN2
end
function s.filter2(c,tp)
    return c:IsFaceup() and c:IsAbleToChangeControler()
end
function s.tg2(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
    if chkc then return chkc:IsLocation(LOCATION_MZONE) and s.filter2(chkc,tp) and chkc:IsControler(1-tp) end
    if chk==0 then return Duel.GetLocationCount(tp,LOCATION_SZONE)>0
        and Duel.IsExistingTarget(s.filter2,tp,0,LOCATION_MZONE,1,e:GetHandler(),tp) end
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_EQUIP)
    local g=Duel.SelectTarget(tp,s.filter2,tp,0,LOCATION_MZONE,1,1,e:GetHandler(),tp)
    Duel.SetOperationInfo(0,CATEGORY_EQUIP,g,1,0,0)
end
function s.op2(e,tp,eg,ep,ev,re,r,rp)
    if Duel.GetLocationCount(tp,LOCATION_SZONE)<=0 then return end
    local c=e:GetHandler()
    local tc=Duel.GetFirstTarget()
    if not (tc:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsType(TYPE_EFFECT)) then return end
    if not Duel.Equip(tp,tc,c) then return end
    local e1=Effect.CreateEffect(c)
    e1:SetType(EFFECT_TYPE_EQUIP)
    e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_OWNER_RELATE)
    e1:SetReset(RESET_EVENT+RESETS_STANDARD)
    e1:SetCode(EFFECT_UPDATE_ATTACK)
    e1:SetValue(600)
    tc:RegisterEffect(e1)
    local e2=Effect.CreateEffect(c)
    e2:SetType(EFFECT_TYPE_SINGLE)
    e2:SetProperty(EFFECT_FLAG_COPY_INHERIT+EFFECT_FLAG_OWNER_RELATE)
    e2:SetCode(EFFECT_EQUIP_LIMIT)
    e2:SetValue(s.eqlimit)
    e2:SetReset(RESET_EVENT+RESETS_STANDARD)
    tc:RegisterEffect(e2)
end
function s.eqlimit(e,c)
    return e:GetOwner()==c
end