name: Otoshidamashi
card_text: While you control a monster that is not a Tuner, your opponent's monsters cannot target this card for attacks. If a monster(s) is sent from the field to your opponent's GY: Place 1 Otoshidamashi Counter on this card, then Special Summon "Otoshidamashi Tokens" (Beast/EARTH/Level 1/ATK ?/DEF ?) up to the number of Otoshidamashi Counters on this card. Each Token's Level is increased when Summoned by the number of Otoshidamashi Counters on this card, also each Token's ATK/DEF are its Level when Summoned x 500. You can only use this effect of "Otoshidamashi" once per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 14957441 --Otoshidamashi Token
  local COUNTER_ID_0 = 0x59 --Otoshidamashii
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 14957441 --Otoshidamashi Token
  local COUNTER_ID_0 = 0x59 --Otoshidamashii
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x59, LOCATION_MZONE)
  	--cannot be target
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.atklm)
  	e1:SetValue(aux.imval1)
  	c:RegisterEffect(e1)
  	--sp token
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_COUNTER + CATEGORY_TOKEN)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tkcon)
  	e2:SetTarget(s.tktg)
  	e2:SetOperation(s.tkop)
  	c:RegisterEffect(e2)
  end
  function s.atkfilter(c)
  	return c:IsFacedown() or c:IsFaceup() and not c:IsType(TYPE_TUNER)
  end
  function s.atklm(e)
  	return Duel.IsExistingMatchingCard(s.atkfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsType(TYPE_MONSTER)
  end
  function s.tkcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, 1 - tp)
  end
  function s.tktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanAddCounter(COUNTER_ID_0, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COUNTER, nil, 1, 0, 0)
  end
  function s.tkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if e:GetHandler():AddCounter(COUNTER_ID_0, 1) ~= 0 and ft > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, 0, TYPES_TOKEN_MONSTER, 0, 0, 1, RACE_BEAST, ATTRIBUTE_EARTH) then
  		Duel.BreakEffect()
  		local ct = c:GetCounter(COUNTER_ID_0)
  		if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  			ft = 1
  		end
  		if ct > ft then
  			ct = ft
  		end
  		while ct > 0 do
  			local token = Duel.CreateToken(tp, id + 1)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_LEVEL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			e1:SetValue(c:GetCounter(COUNTER_ID_0))
  			token:RegisterEffect(e1)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_SET_ATTACK)
  			e2:SetValue(c:GetCounter(COUNTER_ID_0) * 500)
  			e2:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			token:RegisterEffect(e2)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_SINGLE)
  			e3:SetCode(EFFECT_SET_DEFENSE)
  			e3:SetValue(c:GetCounter(COUNTER_ID_0) * 500)
  			e3:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			token:RegisterEffect(e3)
  			Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP)
  			ct = ct - 1
  			if ct > 0 and not Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  				ct = 0
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  
  
source_text: Otoshidamashi
  While you control a monster that is not a Tuner, your opponent's monsters cannot target this card for attacks. If a monster(s) is sent from the field to your opponent's GY: Place 1 Otoshidamashi Counter on this card, then Special Summon "Otoshidamashi Tokens" (Beast/EARTH/Level 1/ATK ?/DEF ?) up to the number of Otoshidamashi Counters on this card. Each Token's Level is increased when Summoned by the number of Otoshidamashi Counters on this card, also each Token's ATK/DEF are its Level when Summoned x 500. You can only use this effect of "Otoshidamashi" once per turn.
target_text: -- Otoshidamashi
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 14957441 --Otoshidamashi Token
  local COUNTER_ID_0 = 0x59 --Otoshidamashii
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableCounterPermit(0x59, LOCATION_MZONE)
  	--cannot be target
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCondition(s.atklm)
  	e1:SetValue(aux.imval1)
  	c:RegisterEffect(e1)
  	--sp token
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_COUNTER + CATEGORY_TOKEN)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tkcon)
  	e2:SetTarget(s.tktg)
  	e2:SetOperation(s.tkop)
  	c:RegisterEffect(e2)
  end
  function s.atkfilter(c)
  	return c:IsFacedown() or c:IsFaceup() and not c:IsType(TYPE_TUNER)
  end
  function s.atklm(e)
  	return Duel.IsExistingMatchingCard(s.atkfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsType(TYPE_MONSTER)
  end
  function s.tkcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, 1 - tp)
  end
  function s.tktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanAddCounter(COUNTER_ID_0, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COUNTER, nil, 1, 0, 0)
  end
  function s.tkop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if e:GetHandler():AddCounter(COUNTER_ID_0, 1) ~= 0 and ft > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, 0, TYPES_TOKEN_MONSTER, 0, 0, 1, RACE_BEAST, ATTRIBUTE_EARTH) then
  		Duel.BreakEffect()
  		local ct = c:GetCounter(COUNTER_ID_0)
  		if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  			ft = 1
  		end
  		if ct > ft then
  			ct = ft
  		end
  		while ct > 0 do
  			local token = Duel.CreateToken(tp, id + 1)
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_LEVEL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			e1:SetValue(c:GetCounter(COUNTER_ID_0))
  			token:RegisterEffect(e1)
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_SET_ATTACK)
  			e2:SetValue(c:GetCounter(COUNTER_ID_0) * 500)
  			e2:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			token:RegisterEffect(e2)
  			local e3 = Effect.CreateEffect(c)
  			e3:SetType(EFFECT_TYPE_SINGLE)
  			e3:SetCode(EFFECT_SET_DEFENSE)
  			e3:SetValue(c:GetCounter(COUNTER_ID_0) * 500)
  			e3:SetReset(RESET_EVENT + RESETS_STANDARD - RESET_TOFIELD)
  			token:RegisterEffect(e3)
  			Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP)
  			ct = ct - 1
  			if ct > 0 and not Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  				ct = 0
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  
  