--[决斗编年史]遗言之札
function c20049999.initial_effect(c)
	if not c20049999.reg then
		c20049999.reg = {}
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_CHAIN_SOLVING)
		ge1:SetOperation(c20049999.count)
		Duel.RegisterEffect(ge1,0)
		local ge2=Effect.CreateEffect(c)
		ge2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge2:SetCode(EVENT_CHAIN_SOLVED)
		ge2:SetOperation(c20049999.reset)
		Duel.RegisterEffect(ge2,0)
	end
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(20049999,1))
	e1:SetCategory(CATEGORY_DRAW)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetProperty(EFFECT_FLAG_DELAY)
	e1:SetCode(EVENT_CUSTOM+20049999)
	e1:SetTarget(c20049999.drtg)
	e1:SetOperation(c20049999.drop)
	c:RegisterEffect(e1)
end
function c20049999.count(e,tp,eg,ep,ev,re,r,rp)
	c20049999.reg={}
	local g=Duel.GetFieldGroup(tp,LOCATION_MZONE,LOCATION_MZONE)
	for tc in aux.Next(g) do
		c20049999.reg[tc]=tc:GetAttack()
	end
	--adjust
	local e1=Effect.GlobalEffect()
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e1:SetCode(EVENT_ADJUST)
	e1:SetOperation(c20049999.ChainOperation)
	Duel.RegisterEffect(e1,0)
	c20049999.chain = e1
end
function c20049999.ChainOperation(e,tp,eg,ep,ev,re,r,rp)
	Debug.Message("ChainOperation")
	local g=Duel.GetFieldGroup(tp,LOCATION_MZONE,LOCATION_MZONE)
	for tc in aux.Next(g) do
		local atk = tc:GetAttack()
		if atk==0 and c20049999.reg[tc] and c20049999.reg[tc]>0 then
			Duel.RaiseEvent(tc,EVENT_CUSTOM+20049999,re,REASON_EFFECT,rp,ep,ev)
		end
		c20049999.reg[tc]=atk
	end
end
function c20049999.reset(e,tp,eg,ep,ev,re,r,rp)
	Debug.Message("reset")
	if Duel.GetCurrentChain()>0 then return end
	c20049999.chain:Reset()
end
function c20049999.drtg(e,tp,eg,ep,ev,re,r,rp,chk)
	local cnt=Duel.GetCardsInZone(tp,LOCATION_HAND):GetCount()
	if chk==0 then return cnt<5 and Duel.IsPlayerCanDraw(tp,5-cnt) end
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,5-cnt)
end
function c20049999.drop(e,tp,eg,ep,ev,re,r,rp)
	local _,_,_,p=Duel.GetOperationInfo(0,CATEGORY_DRAW)
	local cnt=5-Duel.GetCardsInZone(tp,LOCATION_HAND):GetCount()
	Duel.Draw(p,cnt,REASON_EFFECT)
end
