--超融合次元神
local s,id,o=GetID()
function s.initial_effect(c)
	c:EnableReviveLimit()
	aux.AddFusionProcFunRep(c,s.fmfilter,12,false)
	aux.AddContactFusionProcedure(c,Card.IsReleasable,LOCATION_MZONE,LOCATION_MZONE,Duel.Release,POS_FACEUP,REASON_COST+REASON_MATERIAL):SetValue(SUMMON_VALUE_SELF)
	--win
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_DELAY)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetCondition(s.wincon1)
	e1:SetOperation(s.winop)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
	e2:SetCondition(s.wincon2)
	e2:SetOperation(s.winop)
	c:RegisterEffect(e2)
  	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_SINGLE)
	e3:SetCode(EFFECT_MATERIAL_CHECK)
	e3:SetValue(s.mcheck)
	e3:SetLabelObject(e2)
	c:RegisterEffect(e3)
	--remove
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,0))
	e4:SetCategory(CATEGORY_REMOVE)
	e4:SetType(EFFECT_TYPE_QUICK_O)
	e4:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e4:SetCode(EVENT_FREE_CHAIN)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCountLimit(1,EFFECT_COUNT_CODE_CHAIN)
	e4:SetCondition(s.rmcon)
	e4:SetTarget(s.rmtg)
	e4:SetOperation(s.rmop)
	c:RegisterEffect(e4)
	--indes
	local e5=Effect.CreateEffect(c)
	e5:SetType(EFFECT_TYPE_SINGLE)
	e5:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e5:SetRange(LOCATION_MZONE)
	e5:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
	e5:SetValue(1)
	c:RegisterEffect(e5)
	--immune
	local e6=Effect.CreateEffect(c)
	e6:SetType(EFFECT_TYPE_SINGLE)
	e6:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_UNCOPYABLE)
	e6:SetRange(LOCATION_MZONE)
	e6:SetCode(EFFECT_IMMUNE_EFFECT)
	e6:SetValue(s.efilter)
	c:RegisterEffect(e6)
end
function s.fmfilter(c,fc,sub,mg,sg)
	return c:IsLocation(LOCATION_HAND+LOCATION_MZONE) and c:GetLevel()>0 and (not sg or not sg:IsExists(Card.IsLevel,1,c,c:GetLevel()))
end
function s.wincon1(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():GetSummonType()==(SUMMON_TYPE_SPECIAL+SUMMON_VALUE_SELF)
end
function s.wincon2(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_FUSION) and e:GetLabel()==1
end
function s.mcheck(e,c)
	local mg=c:GetMaterial():Filter(Card.IsLocation,nil,LOCATION_MZONE)
	if #mg==12 then e:GetLabelObject():SetLabel(1)
	else e:GetLabelObject():SetLabel(0) end
end
function s.winop(e,tp,eg,ep,ev,re,r,rp)
	local WIN_REASON_CREATORGOD=0x13
	local p=e:GetHandler():GetSummonPlayer()
	Duel.Hint(HINT_CARD,tp,id)
	Duel.Hint(HINT_CARD,1-tp,id)
	Duel.Win(p,WIN_REASON_CREATORGOD)
end
function s.rmfilter(c)
	return c:IsAbleToRemove() and c:IsType(TYPE_MONSTER) and c:GetLevel()>0
end
function s.armfilter(c,lv)
	return c:IsAbleToRemove() and c:IsType(TYPE_MONSTER) and c:GetLevel()==lv
end
function s.rmcon(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():IsLevelAbove(13)
end
function s.rmtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE+LOCATION_GRAVE) and s.rmfilter(chkc) end
	if chk==0 then return Duel.IsExistingTarget(s.rmfilter,tp,LOCATION_MZONE+LOCATION_GRAVE,LOCATION_MZONE+LOCATION_GRAVE,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectTarget(tp,s.rmfilter,tp,LOCATION_MZONE+LOCATION_GRAVE,LOCATION_MZONE+LOCATION_GRAVE,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,1,0,0)
end
function s.rmop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetTargetsRelateToChain()
	if g then
		local lv=g:GetFirst():GetLevel()
		local slg=Duel.GetMatchingGroup(s.armfilter,tp,LOCATION_DECK+LOCATION_EXTRA+LOCATION_HAND+LOCATION_MZONE+LOCATION_GRAVE,LOCATION_DECK+LOCATION_EXTRA+LOCATION_HAND+LOCATION_MZONE+LOCATION_GRAVE,nil,lv)
		g:Merge(slg)
		if Duel.Remove(g,POS_FACEDOWN,REASON_EFFECT)~=0 then
			Duel.Damage(1-tp,lv*300,REASON_EFFECT)
		end
	end
end
function s.qlifilter(e,te)
  if te:IsActiveType(TYPE_MONSTER) and te:IsActivated() then
    local lv=e:GetHandler():GetLevel()
    local ec=te:GetOwner()
    if ec:IsType(TYPE_LINK) then
      return false
    elseif ec:IsType(TYPE_XYZ) then
      return false
    else
      return ec:GetLevel()<lv
    end
  else
    return false
  end
end
function s.efilter(e,te)
	if te:IsActiveType(TYPE_SPELL+TYPE_TRAP) then return true
	else return s.qlifilter(e,te) end
end