name: Sacred
card_text: If your monster(s) that was originally FIRE is destroyed by battle or card effect: You can Special Summon this card from your GY (if it was there when the monster was destroyed) or hand (even if not). If this card is Normal or Special Summoned: You can destroy 1 FIRE Beast, Beast-Warrior, or Winged Beast monster in your hand, Deck, or face-up field, except "Sacred Fire King Garunix", and if you do, this card gains ATK equal to half the destroyed monster's ATK it had there, until the end of this turn. You can only use each effect of "Sacred Fire King Garunix" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	local e0 = aux.AddThisCardInGraveAlreadyCheck(c)
  	--special summon
  	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:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetLabelObject(e0)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp, se)
  	return c:IsPreviousControler(tp)
  		and not c:IsPreviousLocation(LOCATION_SZONE)
  		and (c:IsPreviousLocation(LOCATION_MZONE) or c:GetOriginalType() & TYPE_MONSTER ~= 0)
  		and c:GetOriginalAttribute() == ATTRIBUTE_FIRE
  		and c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and (se == nil or c:GetReasonEffect() ~= se)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local se = e:GetLabelObject():GetLabelObject()
  	if c:IsLocation(LOCATION_HAND) then
  		se = nil
  	end
  	return eg:IsExists(s.cfilter, 1, c, tp, se)
  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) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.desfilter(c)
  	return c:IsFaceupEx() and not c:IsCode(id) and c:IsRace(RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST) and c:IsAttribute(ATTRIBUTE_FIRE)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, nil, 1, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectMatchingCard(tp, s.desfilter, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.HintSelection(g)
  		if Duel.Destroy(g, REASON_EFFECT) > 0 then
  			local tc = g:GetFirst()
  			local atk = tc:GetAttack()
  			local c = e:GetHandler()
  			if c:IsFaceup() and c:IsRelateToEffect(e) and atk > 0 then
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_UPDATE_ATTACK)
  				e1:SetValue(atk // 2)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE + RESET_PHASE + PHASE_END)
  				c:RegisterEffect(e1)
  			end
  		end
  	end
  end
  
  
source_text: Sacred
  If your monster(s) that was originally FIRE is destroyed by battle or card effect: You can Special Summon this card from your GY (if it was there when the monster was destroyed) or hand (even if not). If this card is Normal or Special Summoned: You can destroy 1 FIRE Beast, Beast-Warrior, or Winged Beast monster in your hand, Deck, or face-up field, except "Sacred Fire King Garunix", and if you do, this card gains ATK equal to half the destroyed monster's ATK it had there, until the end of this turn. You can only use each effect of "Sacred Fire King Garunix" once per turn.
target_text: -- Sacred
  local s, id, o = GetID()
  function s.initial_effect(c)
  	local e0 = aux.AddThisCardInGraveAlreadyCheck(c)
  	--special summon
  	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:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND + LOCATION_GRAVE)
  	e1:SetLabelObject(e0)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp, se)
  	return c:IsPreviousControler(tp)
  		and not c:IsPreviousLocation(LOCATION_SZONE)
  		and (c:IsPreviousLocation(LOCATION_MZONE) or c:GetOriginalType() & TYPE_MONSTER ~= 0)
  		and c:GetOriginalAttribute() == ATTRIBUTE_FIRE
  		and c:IsReason(REASON_BATTLE + REASON_EFFECT)
  		and (se == nil or c:GetReasonEffect() ~= se)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local se = e:GetLabelObject():GetLabelObject()
  	if c:IsLocation(LOCATION_HAND) then
  		se = nil
  	end
  	return eg:IsExists(s.cfilter, 1, c, tp, se)
  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) then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.desfilter(c)
  	return c:IsFaceupEx() and not c:IsCode(id) and c:IsRace(RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST) and c:IsAttribute(ATTRIBUTE_FIRE)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, nil, 1, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectMatchingCard(tp, s.desfilter, tp, LOCATION_HAND + LOCATION_MZONE + LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.HintSelection(g)
  		if Duel.Destroy(g, REASON_EFFECT) > 0 then
  			local tc = g:GetFirst()
  			local atk = tc:GetAttack()
  			local c = e:GetHandler()
  			if c:IsFaceup() and c:IsRelateToEffect(e) and atk > 0 then
  				local e1 = Effect.CreateEffect(c)
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_UPDATE_ATTACK)
  				e1:SetValue(atk // 2)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE + RESET_PHASE + PHASE_END)
  				c:RegisterEffect(e1)
  			end
  		end
  	end
  end
  
  