name: Firewall
card_text: 2+ monsters
  Once while face-up on the field (Quick Effect): You can target monsters on the field and/or GY up to the number of monsters co-linked to this card; return them to the hand. If a monster this card points to is destroyed by battle or sent to the GY: You can Special Summon 1 Cyberse monster from your hand. You can only use each effect of "Firewall Dragon" once per turn.
external_ids: local CARD_ID_0 = 5043011 --Firewall Dragon
  
lua: local CARD_ID_0 = 5043011 --Firewall Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, nil, 2)
  	c:EnableReviveLimit()
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_NO_TURN_RESET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetCondition(s.regcon)
  	e2:SetOperation(s.regop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.regcon2)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_SINGLE_RANGE + EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCode(EVENT_CUSTOM + id)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	local ct = c:GetMutualLinkedGroupCount()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE + LOCATION_GRAVE) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return ct > 0 and Duel.IsExistingTarget(s.thfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, LOCATION_MZONE + LOCATION_GRAVE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, LOCATION_MZONE + LOCATION_GRAVE, 1, ct, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, g:GetCount(), 0, 0)
  	c:RegisterFlagEffect(0, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 2))
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	Duel.SendtoHand(g, nil, REASON_EFFECT)
  end
  function s.cfilter(c, tp, zone)
  	local seq = c:GetPreviousSequence()
  	if c:IsPreviousControler(1 - tp) then
  		seq = seq + 16
  	end
  	return c:IsPreviousLocation(LOCATION_MZONE) and bit.extract(zone, seq) ~= 0
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp, e:GetHandler():GetLinkedZone())
  end
  function s.cfilter2(c, tp, zone)
  	return not c:IsReason(REASON_BATTLE) and s.cfilter(c, tp, zone)
  end
  function s.regcon2(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp, e:GetHandler():GetLinkedZone())
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.RaiseSingleEvent(e:GetHandler(), EVENT_CUSTOM + id, e, 0, tp, 0, 0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_CYBERSE) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  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 Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND)
  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.spfilter, tp, LOCATION_HAND, 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: Firewall
  2+ monsters
  Once while face-up on the field (Quick Effect): You can target monsters on the field and/or GY up to the number of monsters co-linked to this card; return them to the hand. If a monster this card points to is destroyed by battle or sent to the GY: You can Special Summon 1 Cyberse monster from your hand. You can only use each effect of "Firewall Dragon" once per turn.
target_text: -- Firewall
  local CARD_ID_0 = 5043011 --Firewall Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, nil, 2)
  	c:EnableReviveLimit()
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_NO_TURN_RESET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--special summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetCondition(s.regcon)
  	e2:SetOperation(s.regop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.regcon2)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_SINGLE_RANGE + EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCode(EVENT_CUSTOM + id)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	local ct = c:GetMutualLinkedGroupCount()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE + LOCATION_GRAVE) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return ct > 0 and Duel.IsExistingTarget(s.thfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, LOCATION_MZONE + LOCATION_GRAVE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, LOCATION_MZONE + LOCATION_GRAVE, 1, ct, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, g:GetCount(), 0, 0)
  	c:RegisterFlagEffect(0, RESET_EVENT + RESETS_STANDARD, EFFECT_FLAG_CLIENT_HINT, 1, 0, aux.Stringid(id, 2))
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	Duel.SendtoHand(g, nil, REASON_EFFECT)
  end
  function s.cfilter(c, tp, zone)
  	local seq = c:GetPreviousSequence()
  	if c:IsPreviousControler(1 - tp) then
  		seq = seq + 16
  	end
  	return c:IsPreviousLocation(LOCATION_MZONE) and bit.extract(zone, seq) ~= 0
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp, e:GetHandler():GetLinkedZone())
  end
  function s.cfilter2(c, tp, zone)
  	return not c:IsReason(REASON_BATTLE) and s.cfilter(c, tp, zone)
  end
  function s.regcon2(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter2, 1, nil, tp, e:GetHandler():GetLinkedZone())
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.RaiseSingleEvent(e:GetHandler(), EVENT_CUSTOM + id, e, 0, tp, 0, 0)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_CYBERSE) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  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 Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND)
  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.spfilter, tp, LOCATION_HAND, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  