cm,m = GetID()
cm.name = "新宇宙起源"
__RegisterEffect = Card.RegisterEffect
cm.etb,cm.egb,cm.erb = { },{ },{ }
function Card.RegisterEffect(c,e)
    if e:GetType()&EFFECT_TYPE_IGNITION==EFFECT_TYPE_IGNITION then
        local t = {c,e}
        table.insert(cm.etb,t)
    end
    return __RegisterEffect(c,e)
end
function cm.initial_effect(c)
	aux.AddCodeList(c,89943723)
	aux.AddSetNameMonsterList(c,0x3008)
    local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCost(cm.cost)
    e1:SetTarget(cm.tg)
	e1:SetOperation(cm.op)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(m,0))
	e2:SetCategory(CATEGORY_DRAW)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetCost(cm.cost2)
	e2:SetTarget(cm.tg2)
	e2:SetOperation(cm.op2)
	c:RegisterEffect(e2)
	Duel.AddCustomActivityCounter(m,ACTIVITY_SPSUMMON,function(c) return not c:IsSummonLocation(LOCATION_EXTRA) or c:IsType(TYPE_FUSION) end)
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.limit)
	Duel.RegisterEffect(e1,tp)
end
function cm.limit(e,c)
	return c:IsLocation(LOCATION_EXTRA) and not c:IsType(TYPE_FUSION)
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsExistingMatchingCard(cm.f,tp,LOCATION_DECK,0,1,nil,tp) and Duel.IsExistingMatchingCard(cm.ff,tp,LOCATION_DECK,0,1,nil,e,tp) and Duel.GetLocationCount(tp,LOCATION_MZONE)>0 end
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK)
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_OPERATECARD)
    local tc = Duel.SelectMatchingCard(tp,cm.f,tp,LOCATION_DECK,0,1,1,nil,tp):GetFirst()
    local te = tc:GetActivateEffect()
    if te:IsActivatable(tp,true,true) or not te then
        local fc = Duel.GetFieldCard(tp,LOCATION_FZONE,0)
        if fc then
			Duel.SendtoGrave(fc,REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(tc,tp,tp,LOCATION_FZONE,POS_FACEUP,true)
		local e1=Effect.CreateEffect(e:GetHandler())
    	e1:SetType(EFFECT_TYPE_FIELD)
    	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
    	e1:SetCode(m)
    	e1:SetRange(LOCATION_FZONE)
    	e1:SetTargetRange(1,0)
    	tc:RegisterEffect(e1)
    	local e2=Effect.CreateEffect(e:GetHandler())
    	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
    	e2:SetCode(EVENT_ADJUST)
    	e2:SetCondition(cm.con)
    	e2:SetOperation(cm.opp)
    	Duel.RegisterEffect(e2,tp)
    	tc:RegisterFlagEffect(m,RESET_EVENT+RESETS_STANDARD,EFFECT_FLAG_CLIENT_HINT,0,0,aux.Stringid(m,1))
		if te then
		    te:UseCountLimit(tp,1,true)
			local tep=tc:GetControler()
			local cost=te:GetCost()
			if cost then cost(te,tep,eg,ep,ev,re,r,rp,1) end
			Duel.RaiseEvent(tc,4179255,te,0,tp,tp,Duel.GetCurrentChain())
		end
		local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
    	if ft<=0 then return end
    	if Duel.IsPlayerAffectedByEffect(tp,59822133) then ft=1 end
    	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    	local g=Duel.SelectMatchingCard(tp,cm.ff,tp,LOCATION_DECK,0,1,ft,nil,e,tp)
    	if g:GetCount()>0 then
    		Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)
    	end
    end
end
function cm.ff(c,e,tp)
	return c:IsSetCard(0x1e) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function cm.f(c,tp)
    return c:IsCode(42015635) and c:GetActivateEffect():IsActivatable(tp,true,true)
end
function cm.con(e,tp,eg,ep,ev,re,r,rp)
    local g = Duel.GetMatchingGroup(cm.f2,tp,LOCATION_MZONE,0,nil)
    return g:FilterCount(cm.f3,nil)~=0 or not Duel.IsPlayerAffectedByEffect(tp,m)
end
function cm.opp(e,tp,eg,ep,ev,re,r,rp)
    Card.RegisterEffect = __RegisterEffect
    if not Duel.IsPlayerAffectedByEffect(tp,m) then
        for k,ea in ipairs(cm.erb) do
            local ek,c = ea:Clone(),ea:GetHandler()
            ea:Reset()
            ek:SetType(EFFECT_TYPE_IGNITION)
            ek:SetCode(0)
            c:RegisterEffect(ek)
        end
        e:Reset()
    else
        local g=Duel.GetMatchingGroup(cm.f2,tp,LOCATION_MZONE,0,nil):Filter(cm.f3,nil)
        for c in aux.Next(g) do
            for k,p in ipairs(cm.etb) do
                local kc,ek = p[1],p[2]
                if kc==c then
                    ea=ek:Clone()
                    ek:Reset()
                    ea:SetType(EFFECT_TYPE_QUICK_O)
                    ea:SetCode(EVENT_FREE_CHAIN)
                    c:RegisterEffect(ea)
                    table.insert(cm.erb,ea)
                end
            end
            table.insert(cm.egb,c)
        end
    end
end
function cm.f2(c)
    return (c:IsType(TYPE_FUSION) or c:IsSummonType(SUMMON_TYPE_FUSION)) and c:GetMaterial():FilterCount(Card.GetCode,nil,89943723)>0
end
function cm.f3(c)
    return cm.ck(cm.egb,c)
end
function cm.ck(t,b)
    for k,c in ipairs(t) do
        if c==b then return false end
    end
    return true
end
function cm.tg2(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsPlayerCanDraw(tp,1) end
	Duel.SetTargetPlayer(tp)
	Duel.SetTargetParam(1)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,1)
end
function cm.op2(e,tp,eg,ep,ev,re,r,rp)
	local p,d=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER,CHAININFO_TARGET_PARAM)
	Duel.Draw(p,d,REASON_EFFECT)
end
function cm.cf(c,tp)
	return c:IsSetCard(0x1e,0x1f) and (c:IsFaceup() or c:IsControler(tp))
end
function cm.cost2(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.CheckReleaseGroup(tp,cm.cf,1,nil,tp) and e:GetHandler():IsAbleToRemoveAsCost() end
	Duel.Remove(e:GetHandler(),POS_FACEUP,REASON_COST)
	local g=Duel.SelectReleaseGroup(tp,cm.cf,1,1,nil,tp)
	Duel.Release(g,REASON_COST)
end