name: Primula
card_text: If a monster you control is Tributed (except during the Damage Step): You can Special Summon this card from your hand in Defense Position. You can target up to 2 Plant monsters you control; increase their Level(s) by 2 until the end of this turn. You can only use each effect of "Primula the Rikka Fairy" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_CUSTOM + id)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--level
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.lvtg)
  	e2:SetOperation(s.lvop)
  	c:RegisterEffect(e2)
  	if not s.global_check then
  		s.global_check = true
  		local ge2 = Effect.CreateEffect(c)
  		ge2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge2:SetCode(EVENT_RELEASE)
  		ge2:SetCondition(s.regcon)
  		ge2:SetOperation(s.regop)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.spfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	local v = 0
  	if eg:IsExists(s.spfilter, 1, nil, 0) then
  		v = v + 1
  	end
  	if eg:IsExists(s.spfilter, 1, nil, 1) then
  		v = v + 2
  	end
  	if v == 0 then
  		return false
  	end
  	e:SetLabel(({ 0, 1, PLAYER_ALL })[v])
  	return true
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.RaiseEvent(eg, EVENT_CUSTOM + id, re, r, rp, ep, e:GetLabel())
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return ev == tp or ev == PLAYER_ALL
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.lvfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_PLANT) and c:IsLevelAbove(1)
  end
  function s.lvtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.lvfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.lvfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.lvfilter, tp, LOCATION_MZONE, 0, 1, 2, nil)
  end
  function s.cfilter(c, e)
  	return c:IsFaceup() and c:IsRelateToEffect(e)
  end
  function s.lvop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.cfilter, nil, e)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_LEVEL)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetValue(2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  end
  
  
source_text: Primula
  If a monster you control is Tributed (except during the Damage Step): You can Special Summon this card from your hand in Defense Position. You can target up to 2 Plant monsters you control; increase their Level(s) by 2 until the end of this turn. You can only use each effect of "Primula the Rikka Fairy" once per turn.
target_text: -- Primula
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_CUSTOM + id)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--level
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.lvtg)
  	e2:SetOperation(s.lvop)
  	c:RegisterEffect(e2)
  	if not s.global_check then
  		s.global_check = true
  		local ge2 = Effect.CreateEffect(c)
  		ge2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge2:SetCode(EVENT_RELEASE)
  		ge2:SetCondition(s.regcon)
  		ge2:SetOperation(s.regop)
  		Duel.RegisterEffect(ge2, 0)
  	end
  end
  function s.spfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	local v = 0
  	if eg:IsExists(s.spfilter, 1, nil, 0) then
  		v = v + 1
  	end
  	if eg:IsExists(s.spfilter, 1, nil, 1) then
  		v = v + 2
  	end
  	if v == 0 then
  		return false
  	end
  	e:SetLabel(({ 0, 1, PLAYER_ALL })[v])
  	return true
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.RaiseEvent(eg, EVENT_CUSTOM + id, re, r, rp, ep, e:GetLabel())
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return ev == tp or ev == PLAYER_ALL
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  function s.lvfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_PLANT) and c:IsLevelAbove(1)
  end
  function s.lvtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.lvfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.lvfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.lvfilter, tp, LOCATION_MZONE, 0, 1, 2, nil)
  end
  function s.cfilter(c, e)
  	return c:IsFaceup() and c:IsRelateToEffect(e)
  end
  function s.lvop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.cfilter, nil, e)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_LEVEL)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetValue(2)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  end
  
  