--终结与开始 晓美焰
function c60151011.initial_effect(c)
	--xyz summon
    aux.AddXyzProcedure(c,aux.FilterBoolFunction(Card.IsRace,RACE_SPELLCASTER),3,2)
    c:EnableReviveLimit()
    --destroy
    local e10=Effect.CreateEffect(c)
    e10:SetDescription(aux.Stringid(60151011,0))
    e10:SetCategory(CATEGORY_TODECK)
    e10:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
    e10:SetCode(EVENT_BATTLED)
    e10:SetProperty(EFFECT_FLAG_DELAY)
    e10:SetTarget(c60151011.destg)
    e10:SetOperation(c60151011.desop)
    c:RegisterEffect(e10)
	--atkup
    local e2=Effect.CreateEffect(c)
    e2:SetDescription(aux.Stringid(60151011,1))
    e2:SetCategory(CATEGORY_ATKCHANGE)
    e2:SetType(EFFECT_TYPE_IGNITION)
    e2:SetRange(LOCATION_MZONE)
    e2:SetCountLimit(1,60151011)
    e2:SetCost(c60151011.atkcost)
    e2:SetTarget(c60151011.atktg)
    e2:SetOperation(c60151011.atkop)
    c:RegisterEffect(e2)
	--search
    local e3=Effect.CreateEffect(c)
    e3:SetDescription(aux.Stringid(60151011,1))
    e3:SetCategory(CATEGORY_DRAW)
    e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
    e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	e3:SetProperty(EFFECT_FLAG_DELAY+EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
    e3:SetCondition(c60151011.remcon)
	e3:SetTarget(c60151011.target)
    e3:SetOperation(c60151011.activate)
    c:RegisterEffect(e3)
end
function c60151011.discon(e,tp,eg,ep,ev,re,r,rp)
    return not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED)
end
function c60151011.destg(e,tp,eg,ep,ev,re,r,rp,chk)
    local c=e:GetHandler()
    local tc=Duel.GetAttacker()
    if tc==c then tc=Duel.GetAttackTarget() end
    if chk==0 then return tc and tc:IsFaceup() and tc:IsStatus(STATUS_BATTLE_DESTROYED) end
    Duel.SetOperationInfo(0,CATEGORY_TODECK,tc,1,0,0)
end
function c60151011.desop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local tc=Duel.GetAttacker()
    if tc==c then tc=Duel.GetAttackTarget() end
    if tc:IsRelateToBattle() and tc:IsStatus(STATUS_BATTLE_DESTROYED) then
        Duel.SendtoDeck(tc,nil,2,REASON_EFFECT)
    end
end
function c60151011.atkcost(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return e:GetHandler():CheckRemoveOverlayCard(tp,1,REASON_COST) end
    e:GetHandler():RemoveOverlayCard(tp,1,1,REASON_COST)
end
function c60151011.atktg(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.GetFieldGroupCount(tp,LOCATION_HAND,0)>0 end
end
function c60151011.atkop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    if c:IsFaceup() and c:IsRelateToEffect(e) then
        local ct=Duel.GetFieldGroupCount(tp,LOCATION_HAND,0)
        if ct>0 then
            local e1=Effect.CreateEffect(c)
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_UPDATE_ATTACK)
            e1:SetValue(ct*600)
            e1:SetReset(RESET_EVENT+0x1ff0000+RESET_PHASE+PHASE_END)
            c:RegisterEffect(e1)
			local e2=Effect.CreateEffect(c)
            e2:SetType(EFFECT_TYPE_SINGLE)
            e2:SetCode(EFFECT_EXTRA_ATTACK)
            e2:SetValue(ct-1)
            e2:SetReset(RESET_EVENT+0x1ff0000+RESET_PHASE+PHASE_END)
            c:RegisterEffect(e2)
			local e3=Effect.CreateEffect(c)
            e3:SetType(EFFECT_TYPE_SINGLE)
            e3:SetCode(EFFECT_CANNOT_DIRECT_ATTACK)
            e3:SetCondition(c60151011.dircon)
            e3:SetReset(RESET_EVENT+0x1ff0000+RESET_PHASE+PHASE_END)
            c:RegisterEffect(e3)
			local e4=Effect.CreateEffect(c)
            e4:SetType(EFFECT_TYPE_SINGLE)
            e4:SetCode(EFFECT_CANNOT_ATTACK)
            e4:SetCondition(c60151011.atkcon)
            e4:SetReset(RESET_EVENT+0x1ff0000+RESET_PHASE+PHASE_END)
            c:RegisterEffect(e4)
			--double
			local e5=Effect.CreateEffect(c)
			e5:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
			e5:SetCode(EVENT_PRE_BATTLE_DAMAGE)
			e5:SetReset(RESET_EVENT+0x1ff0000+RESET_PHASE+PHASE_END)
			e5:SetCondition(c60151011.damcon)
			e5:SetOperation(c60151011.damop)
			c:RegisterEffect(e5)
        end
    end
end
function c60151011.dircon(e)
    return e:GetHandler():GetAttackAnnouncedCount()>0
end
function c60151011.atkcon(e)
    return e:GetHandler():IsDirectAttacked()
end
function c60151011.remcon(e,tp,eg,ep,ev,re,r,rp)
    local ct1=Duel.GetFieldGroupCount(tp,LOCATION_HAND,0)
    local ct2=Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)
	return e:GetHandler():GetSummonType()==SUMMON_TYPE_XYZ and ct1<ct2
end
function c60151011.target(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsPlayerCanDraw(tp,1) end
    Duel.SetTargetPlayer(tp)
    Duel.SetTargetParam(1)
    Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,1)
end
function c60151011.activate(e,tp,eg,ep,ev,re,r,rp)
    local p,d=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER,CHAININFO_TARGET_PARAM)
    Duel.Draw(p,d,REASON_EFFECT)
end
function c60151011.damcon(e,tp,eg,ep,ev,re,r,rp)
    return ep~=tp and e:GetHandler():GetBattleTarget()~=nil
end
function c60151011.damop(e,tp,eg,ep,ev,re,r,rp)
    Duel.ChangeBattleDamage(ep,ev/2)
end