--Y.H.V.H.
local s,id,o=GetID()
function s.initial_effect(c)
	aux.AddCodeList(c,id)
	-- summon with 3 tribute
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_LIMIT_SUMMON_PROC)
	e1:SetCondition(s.ttcon)
	e1:SetOperation(s.ttop)
	e1:SetValue(SUMMON_TYPE_ADVANCE)
	c:RegisterEffect(e1)
	-- public
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetCode(EVENT_PREDRAW)
	e2:SetRange(LOCATION_HAND)
	e2:SetCountLimit(1,id)
	e2:SetCondition(s.pubcon)
	e2:SetOperation(s.pubop)
	c:RegisterEffect(e2)
	-- banish the world
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,2))
	e4:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_F)
	e4:SetCode(EVENT_SUMMON_SUCCESS)
    e4:SetTarget(s.batarget)
	e4:SetOperation(s.baop)
	c:RegisterEffect(e4)
end
function s.ttcon(e,c,minc)
	if c==nil then return true end
	local t=c:GetFlagEffectLabel(id)
	if t==nil then t=3 end
	return minc<=t and Duel.CheckTribute(c,t)
end
function s.ttop(e,tp,eg,ep,ev,re,r,rp,c)
	local t=c:GetFlagEffectLabel(id)
	if t==nil then t=3 end
	local g=Duel.SelectTribute(tp,c,t,t)
	c:SetMaterial(g)
	Duel.Release(g,REASON_SUMMON+REASON_MATERIAL)
end
function s.pubcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetTurnPlayer()~=tp
end
function s.pubop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if not Duel.SelectYesNo(tp,aux.Stringid(id,1)) then return end
	local fid=c:GetFieldID()
	c:RegisterFlagEffect(id+100000000,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_OPPO_TURN,EFFECT_FLAG_CLIENT_HINT,1,fid,66)
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_PUBLIC)
	e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
	c:RegisterEffect(e1)
	-- no cost
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_LPCOST_CHANGE)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetRange(LOCATION_HAND)
	e2:SetLabel(fid)
	e2:SetLabelObject(c)
	e2:SetTargetRange(1,0)
	e2:SetCondition(s.indcon)
	e2:SetValue(s.costchange)
	e2:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
	Duel.RegisterEffect(e2,tp)
	local e22=Effect.CreateEffect(c)
	e22:SetType(EFFECT_TYPE_FIELD)
	e22:SetCode(EFFECT_DISCARD_COST_CHANGE)
	e22:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e22:SetRange(LOCATION_HAND)
	e22:SetLabel(fid)
	e22:SetLabelObject(c)
	e22:SetTargetRange(1,0)
	e22:SetCondition(s.indcon)
	e22:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
	Duel.RegisterEffect(e22,tp)	
	-- reduce atribute
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e3:SetCode(EVENT_CHAIN_SOLVED)
	e3:SetRange(LOCATION_HAND)
	e3:SetOperation(s.rdtriop)
	e3:SetCondition(s.indcon)
	e3:SetLabel(fid)
	e3:SetLabelObject(c)
	e3:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
	c:RegisterEffect(e3)
	-- reset attribute
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e4:SetCode(EVENT_PHASE+PHASE_END)
	e4:SetCountLimit(1)
	e4:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END+RESET_SELF_TURN)
	e4:SetLabel(fid)
	e4:SetLabelObject(c)
	e4:SetCondition(s.indcon)
	e4:SetOperation(s.resettriop)
	Duel.RegisterEffect(e4,tp)
end

function s.costchange(e,re,rp,val)
	if re and re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:GetHandler():IsType(TYPE_TRAP) and re:GetHandler():IsType(TYPE_COUNTER) then
		return 0
	else
		return val
	end
end

function s.indcon(e)
	local c=e:GetLabelObject()
	return c:GetFlagEffectLabel(id+100000000)==e:GetLabel()
end

function s.resettriop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	c:ResetFlagEffect(id)
end

function s.rdtriop(e,tp,eg,ep,ev,re,r,rp)
    local owner=re:GetOwnerPlayer()
	if re:IsHasType(EFFECT_TYPE_ACTIVATE) and ((re:IsActiveType(TYPE_COUNTER) and owner==tp) or (re:IsActiveType(TYPE_QUICKPLAY) and owner==1-tp)) then 
    else
        return
    end
	Duel.Hint(HINT_CARD,0,id)
	local c=e:GetHandler()
	local t=c:GetFlagEffectLabel(id)
	if t==nil then
		c:RegisterFlagEffect(id,RESET_EVENT,1,3)
		t=3
	end 
	if t>0 then t=t-1 end
	c:SetFlagEffectLabel(id,t)
	c:SetHint(CHINT_TURN,t)
	if t==0 then
		Duel.Summon(tp,c,true,nil)
	end
end
function s.batarget(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsControler(1-tp) and chkc:IsOnField() and chkc:IsFacedown() end
	if chk==0 then return true end
	Duel.SetChainLimit(s.chainlimit)
end
function s.chainlimit(e,rp,tp)
	return false
end
function s.setfilter(c)
	return c:IsType(TYPE_COUNTER) and c:IsSSetable()
end
function s.baop(e,tp,eg,ep,ev,re,r,rp)
	local sg=Duel.GetMatchingGroup(aux.TRUE,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,aux.ExceptThisCard(e))
	if #sg<=0 then return end
    local sc=sg:GetFirst()
	local c=0 
	while sc do
        local id=sc:GetCode()
        local success=Duel.Remove(Group.FromCards(sc),POS_FACEUP,REASON_EFFECT)
        if success then
            c=c+1
			local card=e:GetHandler()
            local ae=Effect.CreateEffect(card)
            ae:SetType(EFFECT_TYPE_FIELD)
            ae:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
			ae:SetRange(LOCATION_MZONE)
            ae:SetCode(EFFECT_CANNOT_ACTIVATE)
            ae:SetReset(RESET_EVENT+RESETS_STANDARD)
            ae:SetTargetRange(1,1)
            ae:SetValue(s.balimit)
            ae:SetLabel(id)
            card:RegisterEffect(ae)
        end
        sc=sg:GetNext()
	end
	local g=Duel.GetMatchingGroup(s.setfilter,tp,LOCATION_DECK,0,nil)
	local ct=math.min(Duel.GetLocationCount(tp,LOCATION_SZONE),c)
	if ct<=0 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SET)
	local sg=g:Select(tp,1,ct,nil)
	Duel.SSet(tp,sg)
end
function s.balimit(e,re,tp)
	return re:GetHandler():IsCode(e:GetLabel())
end