name: Cyber
card_text: Add 1 LIGHT Machine monster that cannot be Normal Summoned/Set, or 1 "Cyber Dragon" monster, from your Deck to your hand. If the activation of this card in its owner's possession was negated by your opponent's card effect and sent to your GY: You can discard 1 card; add this card to your hand. You can only activate 1 "Cyber Emergency" per turn.
external_ids: local SETCARD_ID_0 = 0x1093 --Cyber
  
lua: local SETCARD_ID_0 = 0x1093 --Cyber
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCost(s.negreg)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_CHAIN_NEGATED)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCondition(s.thcon)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_CUSTOM + id)
  	e3:SetCondition(s.thcon2)
  	c:RegisterEffect(e3)
  end
  function s.negreg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	if not e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return
  	end
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_CHAIN_NEGATED)
  	e1:SetLabelObject(e)
  	e1:SetOperation(s.negcheck)
  	e1:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.negcheck(e, tp, eg, ep, ev, re, r, rp)
  	local te = e:GetLabelObject()
  	local de, dp = Duel.GetChainInfo(ev, CHAININFO_DISABLE_REASON, CHAININFO_DISABLE_PLAYER)
  	if rp == tp and de and dp == 1 - tp and re == te then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_CHAIN_END)
  		e1:SetOperation(s.negevent)
  		e1:SetLabelObject(te)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.negevent(e, tp, eg, ep, ev, re, r, rp)
  	local te = e:GetLabelObject()
  	Duel.RaiseEvent(te:GetHandler(), EVENT_CUSTOM + id, te, 0, tp, tp, 0)
  	e:Reset()
  end
  function s.filter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or (c:IsAttribute(ATTRIBUTE_LIGHT) and c:IsRace(RACE_MACHINE) and not c:IsSummonableCard()))
  		and c:IsType(TYPE_MONSTER)
  		and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local de, dp = Duel.GetChainInfo(ev, CHAININFO_DISABLE_REASON, CHAININFO_DISABLE_PLAYER)
  	return rp == tp and de and dp == 1 - tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler() == re:GetHandler() and e:GetHandler():GetReasonEffect() == de
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.thtg(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.thop(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
  function s.thcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c == re:GetHandler() and c:GetReasonEffect() == nil
  end
  
  
source_text: Cyber
  Add 1 LIGHT Machine monster that cannot be Normal Summoned/Set, or 1 "Cyber Dragon" monster, from your Deck to your hand. If the activation of this card in its owner's possession was negated by your opponent's card effect and sent to your GY: You can discard 1 card; add this card to your hand. You can only activate 1 "Cyber Emergency" per turn.
target_text: -- Cyber
  local SETCARD_ID_0 = 0x1093 --Cyber
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCost(s.negreg)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_CHAIN_NEGATED)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCondition(s.thcon)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_CUSTOM + id)
  	e3:SetCondition(s.thcon2)
  	c:RegisterEffect(e3)
  end
  function s.negreg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	if not e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return
  	end
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_CHAIN_NEGATED)
  	e1:SetLabelObject(e)
  	e1:SetOperation(s.negcheck)
  	e1:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.negcheck(e, tp, eg, ep, ev, re, r, rp)
  	local te = e:GetLabelObject()
  	local de, dp = Duel.GetChainInfo(ev, CHAININFO_DISABLE_REASON, CHAININFO_DISABLE_PLAYER)
  	if rp == tp and de and dp == 1 - tp and re == te then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e1:SetCode(EVENT_CHAIN_END)
  		e1:SetOperation(s.negevent)
  		e1:SetLabelObject(te)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.negevent(e, tp, eg, ep, ev, re, r, rp)
  	local te = e:GetLabelObject()
  	Duel.RaiseEvent(te:GetHandler(), EVENT_CUSTOM + id, te, 0, tp, tp, 0)
  	e:Reset()
  end
  function s.filter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or (c:IsAttribute(ATTRIBUTE_LIGHT) and c:IsRace(RACE_MACHINE) and not c:IsSummonableCard()))
  		and c:IsType(TYPE_MONSTER)
  		and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local de, dp = Duel.GetChainInfo(ev, CHAININFO_DISABLE_REASON, CHAININFO_DISABLE_PLAYER)
  	return rp == tp and de and dp == 1 - tp and re:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler() == re:GetHandler() and e:GetHandler():GetReasonEffect() == de
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.thtg(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.thop(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
  function s.thcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c == re:GetHandler() and c:GetReasonEffect() == nil
  end
  
  