name: Protection
card_text: When a Spell/Trap Card is activated: Banish 1 face-up "Elemental HERO" monster you control until the End Phase; negate the activation, and if you do, destroy that card. If you control "Elemental HERO Terra Firma": You can banish this card from your GY; Special Summon 1 of your "Elemental HERO" monsters that is banished, or in your hand or GY, ignoring its Summoning conditions. You can only use this effect of "Protection of the Elements" once per turn.
external_ids: local CARD_ID_0 = 74711057 --Elemental HERO Terra Firma
  local SETCARD_ID_0 = 0x3008 --Elemental
  
lua: local CARD_ID_0 = 74711057 --Elemental HERO Terra Firma
  local SETCARD_ID_0 = 0x3008 --Elemental
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddSetNameMonsterList(c, SETCARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.spcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return re:IsHasType(EFFECT_TYPE_ACTIVATE) and Duel.IsChainNegatable(ev)
  end
  function s.costfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.costfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	if Duel.Remove(g, 0, REASON_COST + REASON_TEMPORARY) ~= 0 then
  		local rc = g:GetFirst()
  		if rc:IsType(TYPE_TOKEN) then
  			return
  		end
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		e1:SetLabelObject(rc)
  		e1:SetCountLimit(1)
  		e1:SetOperation(s.retop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.ReturnToField(e:GetLabelObject())
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(aux.AND(Card.IsFaceup, Card.IsCode), tp, LOCATION_ONFIELD, 0, 1, nil, CARD_ID_0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsFaceupEx() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsCanBeSpecialSummoned(e, 0, tp, true, 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 + LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE + LOCATION_REMOVED, 0, nil, e, tp)
  	if ft <= 0 or #g == 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = g:Select(tp, 1, 1, nil)
  	if #sg == 0 then
  		return
  	end
  	Duel.SpecialSummon(sg, 0, tp, tp, true, false, POS_FACEUP)
  end
  
  
source_text: Protection
  When a Spell/Trap Card is activated: Banish 1 face-up "Elemental HERO" monster you control until the End Phase; negate the activation, and if you do, destroy that card. If you control "Elemental HERO Terra Firma": You can banish this card from your GY; Special Summon 1 of your "Elemental HERO" monsters that is banished, or in your hand or GY, ignoring its Summoning conditions. You can only use this effect of "Protection of the Elements" once per turn.
target_text: -- Protection
  local CARD_ID_0 = 74711057 --Elemental HERO Terra Firma
  local SETCARD_ID_0 = 0x3008 --Elemental
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddSetNameMonsterList(c, SETCARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.spcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return re:IsHasType(EFFECT_TYPE_ACTIVATE) and Duel.IsChainNegatable(ev)
  end
  function s.costfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.costfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	if Duel.Remove(g, 0, REASON_COST + REASON_TEMPORARY) ~= 0 then
  		local rc = g:GetFirst()
  		if rc:IsType(TYPE_TOKEN) then
  			return
  		end
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_PHASE + PHASE_END)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		e1:SetLabelObject(rc)
  		e1:SetCountLimit(1)
  		e1:SetOperation(s.retop)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.ReturnToField(e:GetLabelObject())
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(aux.AND(Card.IsFaceup, Card.IsCode), tp, LOCATION_ONFIELD, 0, 1, nil, CARD_ID_0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsFaceupEx() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsCanBeSpecialSummoned(e, 0, tp, true, 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 + LOCATION_GRAVE + LOCATION_REMOVED, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE + LOCATION_REMOVED)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE + LOCATION_REMOVED, 0, nil, e, tp)
  	if ft <= 0 or #g == 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local sg = g:Select(tp, 1, 1, nil)
  	if #sg == 0 then
  		return
  	end
  	Duel.SpecialSummon(sg, 0, tp, tp, true, false, POS_FACEUP)
  end
  
  