name: Otonari
card_text: If you control 2 or more Thunder monsters and this card is in your hand or GY: You can Special Summon this card, but banish it when it leaves the field. You can target 1 Xyz Monster you control; attach 2 Level 4 LIGHT Thunder monsters from your hand, face-up field, and/or GY to it as material, including this card you control. You can only use each effect of "Otonari Thunder" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--attach
  	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 + o)
  	e2:SetTarget(s.xtg)
  	e2:SetOperation(s.xop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_THUNDER)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 2, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		c:RegisterEffect(e1, true)
  	end
  end
  function s.xfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ)
  end
  function s.xyzfilter(c, tp, e)
  	return c:IsControler(tp) and c:IsFaceupEx() and c:IsLevel(4) and c:IsAttribute(ATTRIBUTE_LIGHT) and c:IsRace(RACE_THUNDER) and c:IsCanOverlay() and not c:IsImmuneToEffect(e)
  end
  function s.xtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.xfilter(chkc) and chkc ~= c
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xfilter, tp, LOCATION_MZONE, 0, 1, c)
  			and s.xyzfilter(c, tp, e)
  			and Duel.IsExistingMatchingCard(s.xyzfilter, tp, LOCATION_MZONE + LOCATION_HAND + LOCATION_GRAVE, 0, 1, c, tp, e)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	Duel.SelectTarget(tp, s.xfilter, tp, LOCATION_MZONE, 0, 1, 1, c)
  end
  function s.xop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or not c:IsLocation(LOCATION_ONFIELD) or not s.xyzfilter(c, tp, e) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.xyzfilter), tp, LOCATION_MZONE + LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, c, tp, e)
  	if g:GetCount() > 0 then
  		local mg = c + g
  		Duel.Overlay(tc, mg)
  	end
  end
  
  
source_text: Otonari
  If you control 2 or more Thunder monsters and this card is in your hand or GY: You can Special Summon this card, but banish it when it leaves the field. You can target 1 Xyz Monster you control; attach 2 Level 4 LIGHT Thunder monsters from your hand, face-up field, and/or GY to it as material, including this card you control. You can only use each effect of "Otonari Thunder" once per turn.
target_text: -- Otonari
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--attach
  	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 + o)
  	e2:SetTarget(s.xtg)
  	e2:SetOperation(s.xop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_THUNDER)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 2, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  		e1:SetValue(LOCATION_REMOVED)
  		e1:SetReset(RESET_EVENT + RESETS_REDIRECT)
  		c:RegisterEffect(e1, true)
  	end
  end
  function s.xfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ)
  end
  function s.xyzfilter(c, tp, e)
  	return c:IsControler(tp) and c:IsFaceupEx() and c:IsLevel(4) and c:IsAttribute(ATTRIBUTE_LIGHT) and c:IsRace(RACE_THUNDER) and c:IsCanOverlay() and not c:IsImmuneToEffect(e)
  end
  function s.xtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.xfilter(chkc) and chkc ~= c
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xfilter, tp, LOCATION_MZONE, 0, 1, c)
  			and s.xyzfilter(c, tp, e)
  			and Duel.IsExistingMatchingCard(s.xyzfilter, tp, LOCATION_MZONE + LOCATION_HAND + LOCATION_GRAVE, 0, 1, c, tp, e)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	Duel.SelectTarget(tp, s.xfilter, tp, LOCATION_MZONE, 0, 1, 1, c)
  end
  function s.xop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or not c:IsLocation(LOCATION_ONFIELD) or not s.xyzfilter(c, tp, e) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if not tc:IsRelateToEffect(e) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.xyzfilter), tp, LOCATION_MZONE + LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, c, tp, e)
  	if g:GetCount() > 0 then
  		local mg = c + g
  		Duel.Overlay(tc, mg)
  	end
  end
  
  