name: Yosen
card_text: Each time a "Yosenju" monster(s) is Normal or Special Summoned, place 1 Yosen Counter on this card. You can remove any number of Yosen Counters from this card; apply this effect, depending on the number of Yosen Counters removed. You can only use this effect of "Yosen Training Grounds" once per turn.
  ● 1 Counter: All "Yosenju" monsters you currently control gain 300 ATK until the end of this turn (even if this card leaves the field).
  ● 3 Counters: Add 1 "Yosenju" card from your Deck or Graveyard to your hand.
external_ids: local SETCARD_ID_0 = 0xb3 --Yosenju
  local COUNTER_ID_0 = 0x33 --Yosen
  
lua: local SETCARD_ID_0 = 0xb3 --Yosenju
  local COUNTER_ID_0 = 0x33 --Yosen
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x33)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.ctcon)
  	e2:SetOperation(s.ctop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--counter
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_IGNITION)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCountLimit(1, id)
  	e4:SetTarget(s.target)
  	e4:SetOperation(s.operation)
  	c:RegisterEffect(e4)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.filter1(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.filter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST) and Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 3, REASON_COST) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_ATKCHANGE)
  		e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  	else
  		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  		e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 3, REASON_COST)
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabel() == 0 then
  		local g = Duel.GetMatchingGroup(s.filter1, tp, LOCATION_MZONE, 0, nil)
  		local tc = g:GetFirst()
  		while tc do
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_ATTACK)
  			e1:SetValue(300)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  			tc = g:GetNext()
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  
  
source_text: Yosen
  Each time a "Yosenju" monster(s) is Normal or Special Summoned, place 1 Yosen Counter on this card. You can remove any number of Yosen Counters from this card; apply this effect, depending on the number of Yosen Counters removed. You can only use this effect of "Yosen Training Grounds" once per turn.
  ● 1 Counter: All "Yosenju" monsters you currently control gain 300 ATK until the end of this turn (even if this card leaves the field).
  ● 3 Counters: Add 1 "Yosenju" card from your Deck or Graveyard to your hand.
target_text: -- Yosen
  local SETCARD_ID_0 = 0xb3 --Yosenju
  local COUNTER_ID_0 = 0x33 --Yosen
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x33)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--counter
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCondition(s.ctcon)
  	e2:SetOperation(s.ctop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--counter
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_IGNITION)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCountLimit(1, id)
  	e4:SetTarget(s.target)
  	e4:SetOperation(s.operation)
  	c:RegisterEffect(e4)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.ctcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil)
  end
  function s.ctop(e, tp, eg, ep, ev, re, r, rp)
  	e:GetHandler():AddCounter(COUNTER_ID_0, 1)
  end
  function s.filter1(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.filter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST) and Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 3, REASON_COST) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_ATKCHANGE)
  		e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
  	else
  		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  		e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 3, REASON_COST)
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabel() == 0 then
  		local g = Duel.GetMatchingGroup(s.filter1, tp, LOCATION_MZONE, 0, nil)
  		local tc = g:GetFirst()
  		while tc do
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_ATTACK)
  			e1:SetValue(300)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  			tc = g:GetNext()
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  
  