name: Mirage
card_text: If a face-up FIRE Fusion Monster(s) you control 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). At the start of the Damage Step, if this card battles an opponent's monster: You can destroy both battling monsters. If this card is destroyed by battle or another card's effect: You can Special Summon 1 Level 7 or lower monster that is "Flame Swordsman" or mentions it, from your Deck or Extra Deck. You can only use each effect of "Mirage Swordsman" once per turn.
external_ids: local CARD_ID_0 = 45231177 --Flame Swordsman
  
lua: local CARD_ID_0 = 45231177 --Flame Swordsman
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--
  	local e0 = aux.AddThisCardInGraveAlreadyCheck(c)
  	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)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_START)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--
  	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:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + o * 2)
  	e3:SetCondition(s.spcon2)
  	e3:SetTarget(s.sptg2)
  	e3:SetOperation(s.spop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp, se)
  	return c:IsPreviousControler(tp)
  		and not c:IsPreviousLocation(LOCATION_SZONE)
  		and c:GetOriginalAttribute() == ATTRIBUTE_FIRE
  		and c:IsType(TYPE_FUSION)
  		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.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local tc = Duel.GetAttacker()
  	if tc == c then
  		tc = Duel.GetAttackTarget()
  	end
  	if chk == 0 then
  		return tc
  	end
  	c:RegisterFlagEffect(id, RESET_CHAIN, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, Group.FromCards(c, tc), 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = c:GetBattleTarget()
  	if c:IsRelateToBattle() and tc:IsRelateToBattle() then
  		Duel.Destroy(Group.FromCards(c, tc), REASON_EFFECT)
  	end
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return bit.band(r, REASON_EFFECT + REASON_BATTLE) ~= 0 and e:GetHandler():GetFlagEffect(id) == 0
  end
  function s.spfilter(c, e, tp)
  	return (c:IsCode(CARD_ID_0) or aux.IsCodeListed(c, CARD_ID_0))
  		and c:IsLevelBelow(7)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
  		and (c:IsLocation(LOCATION_DECK) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Mirage
  If a face-up FIRE Fusion Monster(s) you control 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). At the start of the Damage Step, if this card battles an opponent's monster: You can destroy both battling monsters. If this card is destroyed by battle or another card's effect: You can Special Summon 1 Level 7 or lower monster that is "Flame Swordsman" or mentions it, from your Deck or Extra Deck. You can only use each effect of "Mirage Swordsman" once per turn.
target_text: -- Mirage
  local CARD_ID_0 = 45231177 --Flame Swordsman
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--
  	local e0 = aux.AddThisCardInGraveAlreadyCheck(c)
  	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)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_START)
  	e2:SetCountLimit(1, id + o)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--
  	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:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + o * 2)
  	e3:SetCondition(s.spcon2)
  	e3:SetTarget(s.sptg2)
  	e3:SetOperation(s.spop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp, se)
  	return c:IsPreviousControler(tp)
  		and not c:IsPreviousLocation(LOCATION_SZONE)
  		and c:GetOriginalAttribute() == ATTRIBUTE_FIRE
  		and c:IsType(TYPE_FUSION)
  		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.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local tc = Duel.GetAttacker()
  	if tc == c then
  		tc = Duel.GetAttackTarget()
  	end
  	if chk == 0 then
  		return tc
  	end
  	c:RegisterFlagEffect(id, RESET_CHAIN, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, Group.FromCards(c, tc), 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = c:GetBattleTarget()
  	if c:IsRelateToBattle() and tc:IsRelateToBattle() then
  		Duel.Destroy(Group.FromCards(c, tc), REASON_EFFECT)
  	end
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	return bit.band(r, REASON_EFFECT + REASON_BATTLE) ~= 0 and e:GetHandler():GetFlagEffect(id) == 0
  end
  function s.spfilter(c, e, tp)
  	return (c:IsCode(CARD_ID_0) or aux.IsCodeListed(c, CARD_ID_0))
  		and c:IsLevelBelow(7)
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
  		and (c:IsLocation(LOCATION_DECK) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.sptg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.spop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  