name: Garunix
card_text: 2+ Level 8 monsters
  If this card is Xyz Summoned: You can destroy all other monsters on the field. You can detach 1 material from this card, then target 1 Spell/Trap on the field; destroy it, and if you do, this card gains 500 ATK. If this card is destroyed while it has material: You can Special Summon "Fire King" monsters from your GY, up to the number of materials this card had. You can only use each effect of "Garunix Eternity, Hyang of the Fire Kings" once per turn.
external_ids: local SETCARD_ID_0 = 0x81 --Fire
  
lua: local SETCARD_ID_0 = 0x81 --Fire
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--material
  	aux.AddXyzProcedure(c, nil, 8, 2, nil, nil, 99)
  	--destroy monsters
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCountLimit(1, id)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCondition(s.dmcon)
  	e1:SetTarget(s.dmtg)
  	e1:SetOperation(s.dmop)
  	c:RegisterEffect(e1)
  	--destroy spell/trap
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCost(s.dscost)
  	e2:SetTarget(s.dstg)
  	e2:SetOperation(s.dsop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + o * 2)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.dmcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
  end
  function s.dmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_MZONE, LOCATION_MZONE, e:GetHandler())
  	if chk == 0 then
  		return #g > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, #g, 0, 0)
  end
  function s.dmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_MZONE, LOCATION_MZONE, aux.ExceptThisCard(e))
  	Duel.Destroy(g, REASON_EFFECT)
  end
  function s.dscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	c:RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.dstg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsType(TYPE_SPELL + TYPE_TRAP)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsType, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, Card.IsType, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.dsop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Destroy(tc, REASON_EFFECT) > 0 and c:IsRelateToEffect(e) and c:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  		e1:SetValue(500)
  		c:RegisterEffect(e1)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_MZONE)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ct = e:GetHandler():GetPreviousOverlayCountOnField()
  	if chk == 0 then
  		return ct > 0 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	e:SetLabel(ct)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  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.filter, tp, LOCATION_GRAVE, 0, 1, e:GetLabel(), nil, e, tp)
  	if #g > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Garunix
  2+ Level 8 monsters
  If this card is Xyz Summoned: You can destroy all other monsters on the field. You can detach 1 material from this card, then target 1 Spell/Trap on the field; destroy it, and if you do, this card gains 500 ATK. If this card is destroyed while it has material: You can Special Summon "Fire King" monsters from your GY, up to the number of materials this card had. You can only use each effect of "Garunix Eternity, Hyang of the Fire Kings" once per turn.
target_text: -- Garunix
  local SETCARD_ID_0 = 0x81 --Fire
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--material
  	aux.AddXyzProcedure(c, nil, 8, 2, nil, nil, 99)
  	--destroy monsters
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCountLimit(1, id)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCondition(s.dmcon)
  	e1:SetTarget(s.dmtg)
  	e1:SetOperation(s.dmop)
  	c:RegisterEffect(e1)
  	--destroy spell/trap
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCost(s.dscost)
  	e2:SetTarget(s.dstg)
  	e2:SetOperation(s.dsop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + o * 2)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.dmcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
  end
  function s.dmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_MZONE, LOCATION_MZONE, e:GetHandler())
  	if chk == 0 then
  		return #g > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, #g, 0, 0)
  end
  function s.dmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_MZONE, LOCATION_MZONE, aux.ExceptThisCard(e))
  	Duel.Destroy(g, REASON_EFFECT)
  end
  function s.dscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	c:RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.dstg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsType(TYPE_SPELL + TYPE_TRAP)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsType, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, Card.IsType, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  end
  function s.dsop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Destroy(tc, REASON_EFFECT) > 0 and c:IsRelateToEffect(e) and c:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  		e1:SetValue(500)
  		c:RegisterEffect(e1)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_MZONE)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ct = e:GetHandler():GetPreviousOverlayCountOnField()
  	if chk == 0 then
  		return ct > 0 and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	e:SetLabel(ct)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE)
  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.filter, tp, LOCATION_GRAVE, 0, 1, e:GetLabel(), nil, e, tp)
  	if #g > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  