name: Chaos
card_text: Each time a monster(s) is banished, place 1 Chaos Counter on this card for each of those monsters. Once per turn: You can remove 4 or more Chaos Counters from your field, then target 1 banished monster with a Level equal to the number removed; Special Summon that target. When this card on the field is sent to the GY by your opponent's card effect: You can add 1 LIGHT or DARK monster from your Deck to your hand, whose Level is less than or equal to the number of Chaos Counters that were on this card.
external_ids: local COUNTER_ID_0 = 0x13 --Chaos
  
lua: local COUNTER_ID_0 = 0x13 --Chaos
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x13)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--add counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_REMOVE)
  	e2:SetOperation(s.ctop)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetCountLimit(1)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  	--search
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e0:SetCode(EVENT_LEAVE_FIELD_P)
  	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e0:SetOperation(s.regop)
  	c:RegisterEffect(e0)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetType(EFFECT_TYPE_TRIGGER_O + EFFECT_TYPE_SINGLE)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	e4:SetLabelObject(e0)
  	c:RegisterEffect(e4)
  end
  function s.ctfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_MONSTER) and not c:IsPreviousLocation(0x80 + LOCATION_SZONE) and not c:IsType(TYPE_TOKEN)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = eg:FilterCount(s.ctfilter, nil)
  	if ct > 0 then
  		e:GetHandler():AddCounter(COUNTER_ID_0, ct)
  	end
  end
  function s.spfilter(c, e, tp)
  	local lv = c:GetLevel()
  	return lv > 3 and c:IsFaceup() and Duel.IsCanRemoveCounter(tp, 1, 0, COUNTER_ID_0, lv, REASON_COST) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_REMOVED) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_REMOVED, LOCATION_REMOVED, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_REMOVED, LOCATION_REMOVED, 1, 1, nil, e, tp)
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, g:GetFirst():GetLevel(), REASON_COST)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetHandler():GetCounter(COUNTER_ID_0)
  	e:SetLabel(ct)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = e:GetLabelObject():GetLabel()
  	e:SetLabel(ct)
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousControler(tp) and ct > 0 and rp == 1 - tp and bit.band(r, REASON_EFFECT) ~= 0
  end
  function s.thfilter(c, lv)
  	return c:IsLevelBelow(lv) and c:IsAttribute(ATTRIBUTE_LIGHT + ATTRIBUTE_DARK) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e:GetLabel())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e:GetLabel())
  	if g:GetCount() ~= 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Chaos
  Each time a monster(s) is banished, place 1 Chaos Counter on this card for each of those monsters. Once per turn: You can remove 4 or more Chaos Counters from your field, then target 1 banished monster with a Level equal to the number removed; Special Summon that target. When this card on the field is sent to the GY by your opponent's card effect: You can add 1 LIGHT or DARK monster from your Deck to your hand, whose Level is less than or equal to the number of Chaos Counters that were on this card.
target_text: -- Chaos
  local COUNTER_ID_0 = 0x13 --Chaos
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x13)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--add counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_REMOVE)
  	e2:SetOperation(s.ctop)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetCountLimit(1)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  	--search
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e0:SetCode(EVENT_LEAVE_FIELD_P)
  	e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e0:SetOperation(s.regop)
  	c:RegisterEffect(e0)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetType(EFFECT_TYPE_TRIGGER_O + EFFECT_TYPE_SINGLE)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	e4:SetLabelObject(e0)
  	c:RegisterEffect(e4)
  end
  function s.ctfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_MONSTER) and not c:IsPreviousLocation(0x80 + LOCATION_SZONE) and not c:IsType(TYPE_TOKEN)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = eg:FilterCount(s.ctfilter, nil)
  	if ct > 0 then
  		e:GetHandler():AddCounter(COUNTER_ID_0, ct)
  	end
  end
  function s.spfilter(c, e, tp)
  	local lv = c:GetLevel()
  	return lv > 3 and c:IsFaceup() and Duel.IsCanRemoveCounter(tp, 1, 0, COUNTER_ID_0, lv, REASON_COST) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_REMOVED) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_REMOVED, LOCATION_REMOVED, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_REMOVED, LOCATION_REMOVED, 1, 1, nil, e, tp)
  	Duel.RemoveCounter(tp, 1, 0, COUNTER_ID_0, g:GetFirst():GetLevel(), REASON_COST)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetHandler():GetCounter(COUNTER_ID_0)
  	e:SetLabel(ct)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ct = e:GetLabelObject():GetLabel()
  	e:SetLabel(ct)
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousControler(tp) and ct > 0 and rp == 1 - tp and bit.band(r, REASON_EFFECT) ~= 0
  end
  function s.thfilter(c, lv)
  	return c:IsLevelBelow(lv) and c:IsAttribute(ATTRIBUTE_LIGHT + ATTRIBUTE_DARK) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e:GetLabel())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e:GetLabel())
  	if g:GetCount() ~= 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  