name: Apple
card_text: Once per turn, if this card is targeted for an attack: You can Special Summon 1 Level 5 or lower Spellcaster-Type monster from your hand, then change the attack target to it, and if you do, the attacking monster's ATK becomes half its current ATK. If this card is destroyed by battle or card effect: You can target up to 3 other "Magician Girl" monsters with different names in your Graveyard; add them to your hand.
external_ids: local SETCARD_ID_0 = 0x20a2 --Magician
  
lua: local SETCARD_ID_0 = 0x20a2 --Magician
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelBelow(5) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  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 Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	if tc and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		local a = Duel.GetAttacker()
  		local ag = a:GetAttackableTarget()
  		if a:IsAttackable() and not a:IsImmuneToEffect(e) and ag:IsContains(tc) then
  			Duel.BreakEffect()
  			Duel.ChangeAttackTarget(tc)
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(math.ceil(a:GetAttack() / 2))
  			a:RegisterEffect(e1)
  		end
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return bit.band(r, REASON_EFFECT + REASON_BATTLE) ~= 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.thfilter(chkc)
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	local g = Duel.GetMatchingGroup(s.thfilter, tp, LOCATION_GRAVE, 0, c):Filter(Card.IsCanBeEffectTarget, nil, e)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local tg = g:SelectSubGroup(tp, aux.dncheck, false, 1, 3)
  	Duel.SetTargetCard(tg)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, tg, tg:GetCount(), 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.SendtoHand(tg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, tg)
  	end
  end
  
  
source_text: Apple
  Once per turn, if this card is targeted for an attack: You can Special Summon 1 Level 5 or lower Spellcaster-Type monster from your hand, then change the attack target to it, and if you do, the attacking monster's ATK becomes half its current ATK. If this card is destroyed by battle or card effect: You can target up to 3 other "Magician Girl" monsters with different names in your Graveyard; add them to your hand.
target_text: -- Apple
  local SETCARD_ID_0 = 0x20a2 --Magician
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelBelow(5) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  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 Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	if tc and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 then
  		local a = Duel.GetAttacker()
  		local ag = a:GetAttackableTarget()
  		if a:IsAttackable() and not a:IsImmuneToEffect(e) and ag:IsContains(tc) then
  			Duel.BreakEffect()
  			Duel.ChangeAttackTarget(tc)
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(math.ceil(a:GetAttack() / 2))
  			a:RegisterEffect(e1)
  		end
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return bit.band(r, REASON_EFFECT + REASON_BATTLE) ~= 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.thfilter(chkc)
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	local g = Duel.GetMatchingGroup(s.thfilter, tp, LOCATION_GRAVE, 0, c):Filter(Card.IsCanBeEffectTarget, nil, e)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local tg = g:SelectSubGroup(tp, aux.dncheck, false, 1, 3)
  	Duel.SetTargetCard(tg)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, tg, tg:GetCount(), 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.SendtoHand(tg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, tg)
  	end
  end
  
  