name: Blast
card_text: When an opponent's monster declares an attack: Destroy the 1 monster your opponent controls with the highest ATK (your choice, if tied), and if you do, inflict damage to your opponent equal to its original ATK, then if "Exchange of the Spirit" is not in your GY, you take damage equal to the damage they took. If this card is sent from the hand or Deck to the GY: You can target 1 Level 4 EARTH Fairy monster in your GY; add it to your hand. You can only use each effect of "Blast Held by Destiny" once per turn.
external_ids: local CARD_ID_1 = 17484499 --Exchange of the Spirit
  
lua: local CARD_ID_1 = 17484499 --Exchange of the Spirit
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	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_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return tp ~= Duel.GetTurnPlayer()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local tg = g:GetMaxGroup(Card.GetAttack)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, tg, 1, 0, 0)
  	local _, dam = tg:GetMaxGroup(Card.GetBaseAttack)
  	if dam > 0 then
  		Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, dam)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	if g:GetCount() > 0 then
  		local tg = g:GetMaxGroup(Card.GetAttack)
  		local dam = 0
  		if tg:GetCount() > 1 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  			local sg = tg:Select(tp, 1, 1, nil)
  			Duel.HintSelection(sg)
  			if Duel.Destroy(sg, REASON_EFFECT) > 0 then
  				dam = sg:GetFirst():GetBaseAttack()
  			end
  		else
  			if Duel.Destroy(tg, REASON_EFFECT) > 0 then
  				dam = tg:GetFirst():GetBaseAttack()
  			end
  		end
  		if dam > 0 then
  			Duel.Damage(1 - tp, dam, REASON_EFFECT)
  			if not Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_1) then
  				Duel.Damage(tp, dam, REASON_EFFECT)
  			end
  		end
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_HAND + LOCATION_DECK)
  end
  function s.thfilter(c)
  	return c:IsRace(RACE_FAIRY) and c:IsAttribute(ATTRIBUTE_EARTH) and c:IsLevel(4) 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
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Blast
  When an opponent's monster declares an attack: Destroy the 1 monster your opponent controls with the highest ATK (your choice, if tied), and if you do, inflict damage to your opponent equal to its original ATK, then if "Exchange of the Spirit" is not in your GY, you take damage equal to the damage they took. If this card is sent from the hand or Deck to the GY: You can target 1 Level 4 EARTH Fairy monster in your GY; add it to your hand. You can only use each effect of "Blast Held by Destiny" once per turn.
target_text: -- Blast
  local CARD_ID_1 = 17484499 --Exchange of the Spirit
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	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_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return tp ~= Duel.GetTurnPlayer()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local tg = g:GetMaxGroup(Card.GetAttack)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, tg, 1, 0, 0)
  	local _, dam = tg:GetMaxGroup(Card.GetBaseAttack)
  	if dam > 0 then
  		Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, dam)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	if g:GetCount() > 0 then
  		local tg = g:GetMaxGroup(Card.GetAttack)
  		local dam = 0
  		if tg:GetCount() > 1 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  			local sg = tg:Select(tp, 1, 1, nil)
  			Duel.HintSelection(sg)
  			if Duel.Destroy(sg, REASON_EFFECT) > 0 then
  				dam = sg:GetFirst():GetBaseAttack()
  			end
  		else
  			if Duel.Destroy(tg, REASON_EFFECT) > 0 then
  				dam = tg:GetFirst():GetBaseAttack()
  			end
  		end
  		if dam > 0 then
  			Duel.Damage(1 - tp, dam, REASON_EFFECT)
  			if not Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_1) then
  				Duel.Damage(tp, dam, REASON_EFFECT)
  			end
  		end
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_HAND + LOCATION_DECK)
  end
  function s.thfilter(c)
  	return c:IsRace(RACE_FAIRY) and c:IsAttribute(ATTRIBUTE_EARTH) and c:IsLevel(4) 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
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  	end
  end
  
  