name: Legendary
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 hand. If this card is Normal or Special Summoned: You can add 1 "Fire King" Spell/Trap from your Deck to your hand. During the next Standby Phase after this card was destroyed and sent to the GY: Add this card from the GY to your hand. You can only use each effect of "Legendary Fire King Ponix" 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)
  	--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_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--to grave
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e4:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetOperation(s.threg)
  	c:RegisterEffect(e4)
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 2))
  	e5:SetCategory(CATEGORY_TOHAND)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e5:SetRange(LOCATION_GRAVE)
  	e5:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e5:SetCountLimit(1, id + o * 2)
  	e5:SetCondition(s.thcon1)
  	e5:SetTarget(s.thtg1)
  	e5:SetOperation(s.thop1)
  	e5:SetLabelObject(e4)
  	c:RegisterEffect(e5)
  end
  function s.cfilter(c, tp)
  	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)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  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 e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 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.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand() and c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if #g ~= 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.threg(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsReason(REASON_DESTROY) then
  		if Duel.GetCurrentPhase() == PHASE_STANDBY then
  			e:SetLabel(Duel.GetTurnCount())
  			c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_STANDBY, 0, 2)
  		else
  			e:SetLabel(0)
  			c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_STANDBY, 0, 1)
  		end
  	end
  end
  function s.thcon1(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabelObject():GetLabel() ~= Duel.GetTurnCount() and e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.thtg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop1(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Legendary
  If your monster(s) that was originally FIRE is destroyed by battle or card effect: You can Special Summon this card from your hand. If this card is Normal or Special Summoned: You can add 1 "Fire King" Spell/Trap from your Deck to your hand. During the next Standby Phase after this card was destroyed and sent to the GY: Add this card from the GY to your hand. You can only use each effect of "Legendary Fire King Ponix" once per turn.
target_text: -- Legendary
  local SETCARD_ID_0 = 0x81 --Fire
  local s, id, o = GetID()
  function s.initial_effect(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_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e1:SetCode(EVENT_DESTROYED)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetCountLimit(1, id + o)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--to grave
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e4:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e4:SetCode(EVENT_TO_GRAVE)
  	e4:SetOperation(s.threg)
  	c:RegisterEffect(e4)
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 2))
  	e5:SetCategory(CATEGORY_TOHAND)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e5:SetRange(LOCATION_GRAVE)
  	e5:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e5:SetCountLimit(1, id + o * 2)
  	e5:SetCondition(s.thcon1)
  	e5:SetTarget(s.thtg1)
  	e5:SetOperation(s.thop1)
  	e5:SetLabelObject(e4)
  	c:RegisterEffect(e5)
  end
  function s.cfilter(c, tp)
  	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)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  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 e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 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.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand() and c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	if #g ~= 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.threg(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsReason(REASON_DESTROY) then
  		if Duel.GetCurrentPhase() == PHASE_STANDBY then
  			e:SetLabel(Duel.GetTurnCount())
  			c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_STANDBY, 0, 2)
  		else
  			e:SetLabel(0)
  			c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_STANDBY, 0, 1)
  		end
  	end
  end
  function s.thcon1(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabelObject():GetLabel() ~= Duel.GetTurnCount() and e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.thtg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop1(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  