local cm,m = GetID()
cm.name = "真龙魔王 分形·化身"
function cm.initial_effect(c)
	aux.EnablePendulumAttribute(c)
	c:EnableReviveLimit()
	aux.EnableChangeCode(c,98287529,LOCATION_HAND)
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_SINGLE)
	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
	e0:SetCode(EFFECT_SPSUMMON_CONDITION)
	e0:SetValue(cm.lim1)
	c:RegisterEffect(e0)
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(m,0))
	e1:SetType(EFEFCT_TYPE_IGNITION)
	e1:SetRange(LOCALTION_PZONE)
	e1:SetCountLimit(1)
	e1:SetCost(cm.cost)
	e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetDescription(aux.Stringid(m,1))
	e2:SetCategory(CATEGORY_SEARCH+CATEGORY_TOHAND)
	e2:SetTarget(cm.tg1)
	e2:SetOperation(cm.op1)
	c:RegisterEffect(e2)
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(m,2))
	e3:SetCategory(CATEGORY_TOGRAVE+CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	e3:SetCondition(cm.con)
	e3:SetTarget(cm.tg2)
	e3:SetOperation(cm.op2)
	c:RegisterEffect(e3)
	local e4=Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_QUICK_O)
	e4:SetCode(EVENT_FREE_CHAIN)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCost(cm.cost)
	e4:SetCountLimit(1)
	e4:SetTarget(cm.tg3)
	e4:SetOperation(cm.op3)
	c:RegisterEffect(e4)
	Duel.AddCustomActivityCounter(m,ACTIVITY_SPSUMMON,cm.f)
end
function cm.f(c)
	return c:IsRace(RACE_DRAGON)
end
function cm.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetCustomActivityCount(m,tp,ACTIVITY_SPSUMMON)==0 end
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e1:SetReset(RESET_PHASE+PHASE_END)
	e1:SetTargetRange(1,0)
	e1:SetTarget(cm.lim)
	Duel.RegisterEffect(e1,tp)
end
function cm.lim(e,c)
	return not c:IsRace(RACE_DRAGON)
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():GetCurrentScale()~=9 end
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if not c:IsRelateToEffect(e) then return false end
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_CHANGE_LSCALE)
	e1:SetValue(9)
	e1:SetReset(RESET_EVENT+RESETS_STANDARD)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EFFECT_CHANGE_RSCALE)
	c:RegisterEffect(e2)
end
function cm.tg1(e,tp,eg,ep,ev,re,r,rp,chk)
	local g=Duel.GetMatchingGroup(Card.IsAbleToHand,tp,LOCATION_DECK,0,nil)
	if chk==0 then return g:CheckSubGroup(cm.ff,2,2) end
end
function cm.op1(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(Card.IsAbleToHand,tp,LOCATION_DECK,0,nil)
	Duel.Hint(3,tp,HINTSMG_ATOHAND)
	g=g:SelectSubGroup(tp,cm.ff,true,2,2)
	if #g==0 then return false end
	Duel.SendtoHand(g,nil,REASON_EFEFCT)
end
function cm.ff(g)
	return cm.chk(g)
end
function cm.chk(g)
	local tc=g:GetFirst()
	local rc=g:GetNext()
	if (tc:IsSetCard(0xe0) and rc:IsSetCard(0xd0)) or (tc:IsSetCard(0xd0) and rc:IsSetCard(0xe0)) then return true end
	return false
end
function cm.lim1(e,se,sp,st)
	return st&SUMMON_TYPE_RITUA==SUMMON_TYPE_RITUA or st&SUMMON_TYPE_PENDULUM==SUMMON_TYPE_PENDULUM 
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
end
function cm.tg2(e,tp,eg,ep,ev,re,r,rp,chk)
	local g=Duel.GetFieldGroup(tp,0,LOCATION_ONFIELD)
	local mg=Duel.GetFieldGroup(tp,0,LOCATION_HAND)
	if chk==0 then return #g>1 or (#g==0 and #mg>0) end
	if #g>1 then Duel.SetOperationInfo(0,CATEGORY_TOGRAVE,g,#g-1,0,0) end
end
function cm.op2(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g=Duel.GetFieldGroup(tp,0,LOCATION_ONFIELD)
	local mg=Duel.GetFieldGroup(tp,0,LOCATION_HAND)
	if #g>1 then
		Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_TOGRAVE)
		local sg=g:Select(1-tp,#g-1,#g-1)
		Duel.SendtoGrave(sg,REASON_RULE)
	elseif #g==0 and #mg>0 then
		Duel.ConfirmCards(tp,mg)
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		local tg=mg:Filter(Card.IsCanBeSpecialSummoned,nil,e,0,tp,false,false,POS_FACEUP)
		if #tg==0 then return end
		if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 and (Duel.SelectOption(tp,aux.Stringid(m,3),aux.Stringid(m,4))==0 or Duel.GetLocationCount(1-tp,LOCATION_MZONE)>0)  then
			Duel.Hint(3,tpHINTMSG_SPSUMMON)
			local tc=tg:Select(tp,1,1,nil):GetFirst()
			Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP)
			local e1 = Effect.CreateEffect(c)
			e1:SetType(EFEFCT_TYPE_SINGLE)
			e1:SetCode(EFFECT_CHANGE_RACE)
			e1:SetRange(LOCATION_MZONE)
			e1:SetValue(RACE_DRAGON)
			e1:SetReset(RESET_EVENT+RESETS_STANDARD)
			tc:RegisterEffect(e1)
			local e3 = e1:Clone()
			e3:SetCode(EFFECT_ADD_SETCODE)
			e3:SetValue(0xd0)
			tc:RegisterEffect(e3)
		else
			Duel.Hint(3,tpHINTMSG_SPSUMMON)
			local tc=tg:Select(tp,1,1,nil):GetFirst()
			Duel.SpecialSummon(tc,0,tp,1-tp,false,false,POS_FACEUP)
			local e1 = Effect.CreateEffect(c)
			e1:SetType(EFEFCT_TYPE_SINGLE)
			e1:SetCode(EFFECT_CHANGE_RACE)
			e1:SetRange(LOCATION_MZONE)
			e1:SetValue(RACE_DRAGON)
			e1:SetReset(RESET_EVENT+RESETS_STANDARD)
			tc:RegisterEffect(e1)
			local e3 = e1:Clone()
			e3:SetCode(EFFECT_ADD_SETCODE)
			e3:SetValue(0xd0)
			tc:RegisterEffect(e3)
		end
	end
end
function cm.ffff(c)
	return c:IsSetCard(0xd0) and c:IsType(TYPE_PENDULUM) and  not c:IsForbidden()
end
function cm.tg3(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return (Duel.CheckLocation(tp,LOCATION_PZONE,0) or Duel.CheckLocation(tp,LOCATION_PZONE,1))
		and Duel.IsExistingMatchingCard(cm.ffff,tp,LOCATION_DECK,0,1,nil) end
end
function cm.op3(e,tp,eg,ep,ev,re,r,rp)
	if not Duel.CheckLocation(tp,LOCATION_PZONE,0) and not Duel.CheckLocation(tp,LOCATION_PZONE,1) then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOFIELD)
	local g=Duel.SelectMatchingCard(tp,cm.ffff,tp,LOCATION_DECK,0,1,1,nil)
	local tc=g:GetFirst()
	if tc then
		Duel.MoveToField(tc,tp,tp,LOCATION_PZONE,POS_FACEUP,true)
	end
end