-- 因果应报
local s,id,o=GetID()
function s.initial_effect(c)
	--burn
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetType(EFFECT_TYPE_QUICK_O)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetRange(LOCATION_HAND)
	e1:SetHintTiming(0,TIMING_CHAIN_END)
	e1:SetCountLimit(1,EFFECT_COUNT_CODE_CHAIN)
	e1:SetCondition(s.burncon)
	e1:SetTarget(s.burntg)
	e1:SetCost(s.burncost)
	e1:SetOperation(s.burnop)
	c:RegisterEffect(e1)

	if not s.gf then
		s.gf=true
		s.effect_order={}
		s.effect_counter={0,0}
		s.current_order=nil
		s.last_effect=nil
		s.last_effect_turn=0
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DAMAGE_CAL)
		ge1:SetCode(EVENT_CHAINING)
		ge1:SetCondition(s.gecon)
		ge1:SetOperation(s.geop)
		Duel.RegisterEffect(ge1,0)
	end
end

function s.burncon(e,tp,eg,ep,ev,re,r,rp)
	local ph=Duel.GetCurrentPhase()
	return s.effect_counter[2-tp]>=10 and Duel.GetCurrentChain()==0 and s.last_effect_turn==Duel.GetTurnCount()
		and Duel.IsExistingMatchingCard(Card.IsSummonType,tp,0,LOCATION_MZONE,1,nil,SUMMON_TYPE_SPECIAL) 
		and Duel.GetFieldGroupCount(tp,LOCATION_ONFIELD+LOCATION_HAND,0)<Duel.GetFieldGroupCount(tp,0,LOCATION_ONFIELD+LOCATION_HAND)
end

function s.burncost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():IsAbleToGraveAsCost() end
	Duel.SendtoGrave(e:GetHandler(),REASON_COST+REASON_DISCARD)
end

function s.burnop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local ct=Duel.GetMatchingGroupCount(Card.IsSummonType,tp,LOCATION_MZONE,nil,nil,SUMMON_TYPE_SPECIAL)
	Duel.Damage(c:GetOwner(),ct*50,REASON_EFFECT)
end

function s.burntg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	s.current_order={}
	for k,v in pairs(s.effect_order) do
		s.current_order[k]=v
	end
	s.create_chain_effect()
end

function s.chaincon(e,tp,eg,ep,ev,re,r,rp)
	return s.current_order~=nil and e:GetLabel()~=1
end

function s.chaintg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	e:SetLabel(1)
	-------------------------------------------------
	-- ct=#s.current_order
	-- print("[")
	-- for i=1,ct do
	-- 	print(s.current_order[i]:GetCode())
	-- end
	-- print("]")
	-------------------------------------------------
	s.create_chain_effect()
	Duel.SetOperationInfo(0,CATEGORY_DAMAGE,nil,0,tp,50)
end
function s.create_chain_effect()
	while true do
		if #s.current_order==0 then
			s.current_order={}
			return
		end
		local c=table.remove(s.current_order,1)
		local ce=Effect.CreateEffect(c)
		ce:SetDescription(aux.Stringid(id,0))
		ce:SetType(EFFECT_TYPE_QUICK_F)
		ce:SetCode(EVENT_CHAINING)
		ce:SetRange(~0)
		ce:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
		ce:SetCondition(s.chaincon)
		ce:SetTarget(s.chaintg)
		ce:SetOperation(s.burnop)
		ce:SetReset(RESET_EVENT+RESET_CHAIN)
		c:RegisterEffect(ce)
		if ce:IsActivatable(c:GetControler(),true,true) then
			break
		end
		ce:Reset()
	end
end


function s.gecon(e,tp,eg,ep,ev,re,r,rp)
	return s.last_effect~=re
end
function s.geop(e,tp,eg,ep,ev,re,r,rp)
	local turn=Duel.GetTurnCount()
	if turn~=s.last_effect_turn then
		local ct=#s.effect_order
		for i=1,ct do s.effect_order[i]=nil end
		s.effect_counter={0,0}
	end
	table.insert(s.effect_order,re:GetHandler())
	s.effect_counter[rp+1]=s.effect_counter[rp+1]+1
	s.last_effect=re
	s.last_effect_turn=turn
end
