name: Graveyard
card_text: If this card is already face-up on the field:
  ● If a monster you control is destroyed by battle and sent to the GY: Special Summon 1 "Fireball Token" (Pyro/FIRE/Level 1/ATK 100/DEF 100).
  ● If a monster(s) in your hand and/or field is sent to the GY by an opponent's card effect: Special Summon as many "Fireball Tokens" as possible, up to the number of sent monsters. You can only use this effect of "Graveyard of Wandering Souls" once per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 23116809 --Fireball Token
  local CARD_ID_2 = 98596597 --Fireball Token
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 23116809 --Fireball Token
  local CARD_ID_2 = 98596597 --Fireball Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--token
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_F)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.spcon1)
  	e2:SetTarget(s.sptg1)
  	e2:SetOperation(s.spop1)
  	c:RegisterEffect(e2)
  	--token
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_QUICK_F)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.spcon2)
  	e3:SetTarget(s.sptg2)
  	e3:SetOperation(s.spop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter1(c, tp)
  	return c:IsReason(REASON_BATTLE) and c:IsLocation(LOCATION_GRAVE) and c:IsPreviousControler(tp)
  end
  function s.spcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter1, 1, nil, tp) and e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED)
  end
  function s.sptg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, 0)
  end
  function s.spop1(e, tp, eg, ep, ev, re, r, rp)
  	if not e:GetHandler():IsRelateToEffect(e) or Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	if Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE) then
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummon(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.cfilter2(c, tp)
  	return c:IsReason(REASON_EFFECT) and c:IsLocation(LOCATION_GRAVE) and c:IsPreviousLocation(LOCATION_HAND + LOCATION_MZONE) and c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp) and e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED)
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE)
  	end
  	local ct = eg:FilterCount(s.cfilter2, nil, tp)
  	e:SetLabel(ct)
  	ct = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), ct)
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ct = 1
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ct, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ct, 0, 0)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), e:GetLabel())
  	if not e:GetHandler():IsRelateToEffect(e) or ct <= 0 or not Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE) then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ct = 1
  	end
  	for i = 1, ct do
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  	Duel.SpecialSummonComplete()
  end
  
  
source_text: Graveyard
  If this card is already face-up on the field:
  ● If a monster you control is destroyed by battle and sent to the GY: Special Summon 1 "Fireball Token" (Pyro/FIRE/Level 1/ATK 100/DEF 100).
  ● If a monster(s) in your hand and/or field is sent to the GY by an opponent's card effect: Special Summon as many "Fireball Tokens" as possible, up to the number of sent monsters. You can only use this effect of "Graveyard of Wandering Souls" once per turn.
target_text: -- Graveyard
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 23116809 --Fireball Token
  local CARD_ID_2 = 98596597 --Fireball Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--token
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_F)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.spcon1)
  	e2:SetTarget(s.sptg1)
  	e2:SetOperation(s.spop1)
  	c:RegisterEffect(e2)
  	--token
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_QUICK_F)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetCondition(s.spcon2)
  	e3:SetTarget(s.sptg2)
  	e3:SetOperation(s.spop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter1(c, tp)
  	return c:IsReason(REASON_BATTLE) and c:IsLocation(LOCATION_GRAVE) and c:IsPreviousControler(tp)
  end
  function s.spcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter1, 1, nil, tp) and e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED)
  end
  function s.sptg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, 0)
  end
  function s.spop1(e, tp, eg, ep, ev, re, r, rp)
  	if not e:GetHandler():IsRelateToEffect(e) or Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	if Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE) then
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummon(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.cfilter2(c, tp)
  	return c:IsReason(REASON_EFFECT) and c:IsLocation(LOCATION_GRAVE) and c:IsPreviousLocation(LOCATION_HAND + LOCATION_MZONE) and c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp) and e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED)
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE)
  	end
  	local ct = eg:FilterCount(s.cfilter2, nil, tp)
  	e:SetLabel(ct)
  	ct = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), ct)
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ct = 1
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ct, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ct, 0, 0)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), e:GetLabel())
  	if not e:GetHandler():IsRelateToEffect(e) or ct <= 0 or not Duel.IsPlayerCanSpecialSummonMonster(tp, CARD_ID_1, 0, TYPES_TOKEN_MONSTER, 100, 100, 1, RACE_PYRO, ATTRIBUTE_FIRE) then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ct = 1
  	end
  	for i = 1, ct do
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  	Duel.SpecialSummonComplete()
  end
  
  