name: Nibiru,
card_text: During the Main Phase, if your opponent Normal or Special Summoned 5 or more monsters this turn (Quick Effect): You can Tribute as many face-up monsters on the field as possible, and if you do, Special Summon this card from your hand, then Special Summon 1 "Primal Being Token" (Rock/LIGHT/Level 11/ATK ?/DEF ?) to your opponent's field. (This Token's ATK/DEF become the combined original ATK/DEF of the Tributed monsters.) You can only use this effect of "Nibiru, the Primal Being" once per turn.
external_ids: local CARD_ID_0 = 27204312 --Primal Being Token
  
lua: local CARD_ID_0 = 27204312 --Primal Being Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_SUMMON_SUCCESS)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  		local ge2 = ge1:Clone()
  		ge2:SetCode(EVENT_SPSUMMON_SUCCESS)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		Duel.RegisterFlagEffect(tc:GetSummonPlayer(), id, RESET_PHASE + PHASE_END, 0, 1)
  		tc = eg:GetNext()
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.IsPlayerAffectedByEffect(tp, EFFECT_KAISER_COLOSSEUM) then
  		local t1 = Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0)
  		local t2 = Duel.GetFieldGroupCount(tp, 0, LOCATION_MZONE)
  		local c1 = Duel.GetMatchingGroupCount(s.relfilter, tp, LOCATION_MZONE, 0, nil)
  		local c2 = Duel.GetMatchingGroupCount(s.relfilter, tp, 0, LOCATION_MZONE, nil)
  		if t1 - c1 >= t2 - c2 then
  			return false
  		end
  	end
  	return Duel.GetFlagEffect(1 - tp, id) >= 5 and (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2)
  end
  function s.relfilter(c)
  	return c:IsFaceup() and c:IsReleasableByEffect()
  end
  function s.adfilter(c, f)
  	return math.max(f(c), 0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if chk == 0 then
  		return g:GetCount() > 0
  			and Duel.GetMZoneCount(tp, g) > 0
  			and Duel.GetMZoneCount(1 - tp, g, tp) > 0
  			and Duel.IsPlayerCanRelease(tp)
  			and Duel.IsPlayerCanSpecialSummonCount(tp, 2)
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsPlayerCanSpecialSummonMonster(
  				tp,
  				id + 1,
  				0,
  				TYPES_TOKEN_MONSTER,
  				g:GetSum(s.adfilter, Card.GetTextAttack),
  				g:GetSum(s.adfilter, Card.GetTextDefense),
  				11,
  				RACE_ROCK,
  				ATTRIBUTE_LIGHT,
  				POS_FACEUP,
  				1 - tp
  			)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 2, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.relfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if g:GetCount() > 0 and Duel.Release(g, REASON_EFFECT) ~= 0 then
  		local og = Duel.GetOperatedGroup()
  		local c = e:GetHandler()
  		if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  			if og:GetCount() == 0 then
  				return
  			end
  			local atk = og:GetSum(s.adfilter, Card.GetTextAttack)
  			local def = og:GetSum(s.adfilter, Card.GetTextDefense)
  			if
  				Duel.GetLocationCount(1 - tp, LOCATION_MZONE, tp) > 0 and Duel.IsPlayerCanSpecialSummonMonster(
  					tp,
  					id + 1,
  					0,
  					TYPES_TOKEN_MONSTER,
  					atk,
  					def,
  					11,
  					RACE_ROCK,
  					ATTRIBUTE_LIGHT,
  					POS_FACEUP,
  					1 - tp
  				)
  			then
  				Duel.BreakEffect()
  				local token = Duel.CreateToken(tp, id + 1)
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_SET_ATTACK)
  				e1:SetValue(atk)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  				token:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(c)
  				e2:SetType(EFFECT_TYPE_SINGLE)
  				e2:SetCode(EFFECT_SET_DEFENSE)
  				e2:SetValue(def)
  				e2:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  				token:RegisterEffect(e2)
  				Duel.SpecialSummon(token, 0, tp, 1 - tp, false, false, POS_FACEUP)
  			end
  		end
  	end
  end
  
  
source_text: Nibiru,
  During the Main Phase, if your opponent Normal or Special Summoned 5 or more monsters this turn (Quick Effect): You can Tribute as many face-up monsters on the field as possible, and if you do, Special Summon this card from your hand, then Special Summon 1 "Primal Being Token" (Rock/LIGHT/Level 11/ATK ?/DEF ?) to your opponent's field. (This Token's ATK/DEF become the combined original ATK/DEF of the Tributed monsters.) You can only use this effect of "Nibiru, the Primal Being" once per turn.
target_text: -- Nibiru,
  local CARD_ID_0 = 27204312 --Primal Being Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_MAIN_END)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_SUMMON_SUCCESS)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  		local ge2 = ge1:Clone()
  		ge2:SetCode(EVENT_SPSUMMON_SUCCESS)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		Duel.RegisterFlagEffect(tc:GetSummonPlayer(), id, RESET_PHASE + PHASE_END, 0, 1)
  		tc = eg:GetNext()
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.IsPlayerAffectedByEffect(tp, EFFECT_KAISER_COLOSSEUM) then
  		local t1 = Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0)
  		local t2 = Duel.GetFieldGroupCount(tp, 0, LOCATION_MZONE)
  		local c1 = Duel.GetMatchingGroupCount(s.relfilter, tp, LOCATION_MZONE, 0, nil)
  		local c2 = Duel.GetMatchingGroupCount(s.relfilter, tp, 0, LOCATION_MZONE, nil)
  		if t1 - c1 >= t2 - c2 then
  			return false
  		end
  	end
  	return Duel.GetFlagEffect(1 - tp, id) >= 5 and (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2)
  end
  function s.relfilter(c)
  	return c:IsFaceup() and c:IsReleasableByEffect()
  end
  function s.adfilter(c, f)
  	return math.max(f(c), 0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if chk == 0 then
  		return g:GetCount() > 0
  			and Duel.GetMZoneCount(tp, g) > 0
  			and Duel.GetMZoneCount(1 - tp, g, tp) > 0
  			and Duel.IsPlayerCanRelease(tp)
  			and Duel.IsPlayerCanSpecialSummonCount(tp, 2)
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsPlayerCanSpecialSummonMonster(
  				tp,
  				id + 1,
  				0,
  				TYPES_TOKEN_MONSTER,
  				g:GetSum(s.adfilter, Card.GetTextAttack),
  				g:GetSum(s.adfilter, Card.GetTextDefense),
  				11,
  				RACE_ROCK,
  				ATTRIBUTE_LIGHT,
  				POS_FACEUP,
  				1 - tp
  			)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 2, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.relfilter, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if g:GetCount() > 0 and Duel.Release(g, REASON_EFFECT) ~= 0 then
  		local og = Duel.GetOperatedGroup()
  		local c = e:GetHandler()
  		if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  			if og:GetCount() == 0 then
  				return
  			end
  			local atk = og:GetSum(s.adfilter, Card.GetTextAttack)
  			local def = og:GetSum(s.adfilter, Card.GetTextDefense)
  			if
  				Duel.GetLocationCount(1 - tp, LOCATION_MZONE, tp) > 0 and Duel.IsPlayerCanSpecialSummonMonster(
  					tp,
  					id + 1,
  					0,
  					TYPES_TOKEN_MONSTER,
  					atk,
  					def,
  					11,
  					RACE_ROCK,
  					ATTRIBUTE_LIGHT,
  					POS_FACEUP,
  					1 - tp
  				)
  			then
  				Duel.BreakEffect()
  				local token = Duel.CreateToken(tp, id + 1)
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_SET_ATTACK)
  				e1:SetValue(atk)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  				token:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(c)
  				e2:SetType(EFFECT_TYPE_SINGLE)
  				e2:SetCode(EFFECT_SET_DEFENSE)
  				e2:SetValue(def)
  				e2:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  				token:RegisterEffect(e2)
  				Duel.SpecialSummon(token, 0, tp, 1 - tp, false, false, POS_FACEUP)
  			end
  		end
  	end
  end
  
  