--复仇死者的魂醒
function c114514809.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e1)
	--inactivatable
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_CANNOT_INACTIVATE)
	e2:SetRange(LOCATION_SZONE)
	e2:SetValue(c114514809.efilter)
	c:RegisterEffect(e2)
	--act limit
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	e3:SetRange(LOCATION_SZONE)
	e3:SetCondition(c114514809.limcon)
	e3:SetOperation(c114514809.limop)
	c:RegisterEffect(e3)
	local e5=Effect.CreateEffect(c)
	e5:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e5:SetRange(LOCATION_SZONE)
	e5:SetCode(EVENT_CHAIN_END)
	e5:SetOperation(c114514809.limop2)
	c:RegisterEffect(e5)  
	--cannot disable 
	local e2=Effect.CreateEffect(c) 
	e2:SetType(EFFECT_TYPE_FIELD) 
	e2:SetCode(EFFECT_CANNOT_DISABLE) 
	e2:SetRange(LOCATION_SZONE) 
	e2:SetTargetRange(LOCATION_MZONE,0) 
	e2:SetTarget(function(e,c) 
	return c:IsSetCard(0x106) and c:IsType(TYPE_RITUAL) end) 
	c:RegisterEffect(e2) 
	local e2=Effect.CreateEffect(c) 
	e2:SetType(EFFECT_TYPE_FIELD) 
	e2:SetCode(EFFECT_CANNOT_DISEFFECT) 
	e2:SetRange(LOCATION_SZONE)  
	e2:SetValue(function(e,ct) 
	local te=Duel.GetChainInfo(ct,CHAININFO_TRIGGERING_EFFECT) 
	return te:IsActiveType(TYPE_MONSTER) and te:GetHandler():IsType(TYPE_RITUAL) and te:GetHandler():IsSetCard(0x106) and te:GetHandlerPlayer()==e:GetHandlerPlayer() and te:GetHandler():IsOnField() end) 
	c:RegisterEffect(e2)  
	--token 
	local e3=Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_TOKEN)
	e3:SetType(EFFECT_TYPE_IGNITION)
	e3:SetRange(LOCATION_SZONE) 
	e3:SetCountLimit(1,114514809) 
	e3:SetTarget(c114514809.tktg)
	e3:SetOperation(c114514809.tkop)
	c:RegisterEffect(e3)
end
function c114514809.efilter(e,ct)
	local p=e:GetHandlerPlayer()
	local te,tp=Duel.GetChainInfo(ct,CHAININFO_TRIGGERING_EFFECT,CHAININFO_TRIGGERING_PLAYER)
	return p==tp and te:GetHandler():IsType(TYPE_RITUAL) and te:IsActiveType(TYPE_SPELL) and te:IsHasType(EFFECT_TYPE_ACTIVATE) 
end
function c114514809.limfilter(c,tp)
	return c:IsSummonPlayer(tp) and c:IsSummonType(SUMMON_TYPE_RITUAL)
end
function c114514809.limcon(e,tp,eg,ep,ev,re,r,rp)
	return eg:IsExists(c114514809.limfilter,1,nil,tp)
end
function c114514809.limop(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetCurrentChain()==0 then
		Duel.SetChainLimitTillChainEnd(c114514809.chainlm)
	elseif Duel.GetCurrentChain()==1 then
		e:GetHandler():RegisterFlagEffect(114514809,RESET_EVENT+RESETS_STANDARD,0,1)
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		e1:SetCode(EVENT_CHAINING)
		e1:SetOperation(c114514809.resetop)
		Duel.RegisterEffect(e1,tp)
		local e2=e1:Clone()
		e2:SetCode(EVENT_BREAK_EFFECT)
		e2:SetReset(RESET_CHAIN)
		Duel.RegisterEffect(e2,tp)
	end
end
function c114514809.resetop(e,tp,eg,ep,ev,re,r,rp)
	e:GetHandler():ResetFlagEffect(114514809)
	e:Reset()
end
function c114514809.limop2(e,tp,eg,ep,ev,re,r,rp)
	if e:GetHandler():GetFlagEffect(114514809)~=0 then
		Duel.SetChainLimitTillChainEnd(c114514809.chainlm)
	end
	e:GetHandler():ResetFlagEffect(114514809)
end
function c114514809.chainlm(e,rp,tp)
	return tp==rp
end
function c114514809.pbfil(c,tp)
	return c:IsLevelAbove(1) and not c:IsPublic() and Duel.IsPlayerCanSpecialSummonMonster(tp,30650148,0x106,TYPES_TOKEN_MONSTER,0,0,c:GetLevel(),RACE_ZOMBIE,ATTRIBUTE_DARK) 
end
function c114514809.tktg(e,tp,eg,ep,ev,re,r,rp,chk) 
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and Duel.IsExistingMatchingCard(c114514809.pbfil,tp,LOCATION_HAND,0,1,nil,tp) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_OPERATECARD)
	local tc=Duel.SelectMatchingCard(tp,c114514809.pbfil,tp,LOCATION_HAND,0,1,1,nil,tp):GetFirst() 
	e:SetLabelObject(tc) 
	Duel.ConfirmCards(1-tp,tc) 
	Duel.SetOperationInfo(0,CATEGORY_TOKEN,nil,1,0,0)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,0,0)
end
function c114514809.tkop(e,tp,eg,ep,ev,re,r,rp) 
	local c=e:GetHandler()
	local tc=e:GetLabelObject()
	if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then
		local token=Duel.CreateToken(tp,30650148)
		Duel.SpecialSummonStep(token,0,tp,tp,false,false,POS_FACEUP)
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
		e1:SetRange(LOCATION_MZONE)
		e1:SetAbsoluteRange(tp,1,0)
		e1:SetTarget(function(e,c)
		return not c:IsRace(RACE_ZOMBIE) end)
		token:RegisterEffect(e1,true) 
		local e3=Effect.CreateEffect(e:GetHandler())
		e3:SetType(EFFECT_TYPE_SINGLE)
		e3:SetCode(EFFECT_CHANGE_LEVEL)
		e3:SetValue(tc:GetLevel())
		e3:SetReset(RESET_EVENT+RESETS_STANDARD)
		token:RegisterEffect(e3,true)
		Duel.SpecialSummonComplete() 
		if Duel.IsExistingMatchingCard(function(c) return c:IsCode(70491682) end,tp,LOCATION_GRAVE,0,1,nil) and Duel.IsExistingMatchingCard(function(c) return c:IsCode(4388680,94666032) and c:IsAbleToHand() end,tp,LOCATION_GRAVE,0,1,nil) and Duel.SelectYesNo(tp,aux.Stringid(114514809,0)) then 
			Duel.BreakEffect()
			local sg=Duel.SelectMatchingCard(tp,function(c) return c:IsCode(4388680,94666032) and c:IsAbleToHand() end,tp,LOCATION_GRAVE,0,1,1,nil) 
			Duel.SendtoHand(sg,tp,REASON_EFFECT) 
			Duel.ConfirmCards(1-tp,sg)  
		end 
	end 
end

