name: Machina
card_text: Activate 1 of these effects.
  ● Discard 1 card; add 2 "Machina" monsters with different names from each other from your Deck to your hand.
  ● Discard 1 "Machina" card; add 2 "Machina" cards with different names from each other from your Deck to your hand, except "Machina Redeployment".
  You can only activate 1 "Machina Redeployment" per turn.
external_ids: local SETCARD_ID_0 = 0x36 --Machina
  
lua: local SETCARD_ID_0 = 0x36 --Machina
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	if chk == 0 then
  		return true
  	end
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsDiscardable()
  end
  function s.thfilter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thfilter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local count1 = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil):GetClassCount(Card.GetCode)
  	local count2 = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil):GetClassCount(Card.GetCode)
  	local b1 = Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, c) and count1 >= 2
  	local b2 = Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, c) and count2 >= 2
  	if chk == 0 then
  		if e:GetLabel() == 1 then
  			e:SetLabel(0)
  			return b1 or b2
  		else
  			return count1 >= 2 or count2 >= 2
  		end
  	end
  	if e:GetLabel() == 1 then
  		e:SetLabel(0)
  		local op = 0
  		if b1 and b2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  		elseif b1 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  		else
  			op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  		end
  		if op == 0 then
  			Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD, nil)
  		else
  			Duel.DiscardHand(tp, s.costfilter, 1, 1, REASON_COST + REASON_DISCARD, nil)
  		end
  		e:SetLabel(0, op)
  	else
  		local op = 0
  		if count1 >= 2 and count2 >= 2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  		elseif count1 >= 2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  		else
  			op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  		end
  		e:SetLabel(0, op)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local label, op = e:GetLabel()
  	if op == 0 then
  		local g = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local hg = g:SelectSubGroup(tp, aux.dncheck, false, 2, 2)
  		if hg then
  			Duel.SendtoHand(hg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, hg)
  		end
  	else
  		local g = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local hg = g:SelectSubGroup(tp, aux.dncheck, false, 2, 2)
  		if hg then
  			Duel.SendtoHand(hg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, hg)
  		end
  	end
  end
  
  
source_text: Machina
  Activate 1 of these effects.
  ● Discard 1 card; add 2 "Machina" monsters with different names from each other from your Deck to your hand.
  ● Discard 1 "Machina" card; add 2 "Machina" cards with different names from each other from your Deck to your hand, except "Machina Redeployment".
  You can only activate 1 "Machina Redeployment" per turn.
target_text: -- Machina
  local SETCARD_ID_0 = 0x36 --Machina
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	if chk == 0 then
  		return true
  	end
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsDiscardable()
  end
  function s.thfilter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thfilter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	local count1 = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil):GetClassCount(Card.GetCode)
  	local count2 = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil):GetClassCount(Card.GetCode)
  	local b1 = Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, c) and count1 >= 2
  	local b2 = Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, c) and count2 >= 2
  	if chk == 0 then
  		if e:GetLabel() == 1 then
  			e:SetLabel(0)
  			return b1 or b2
  		else
  			return count1 >= 2 or count2 >= 2
  		end
  	end
  	if e:GetLabel() == 1 then
  		e:SetLabel(0)
  		local op = 0
  		if b1 and b2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  		elseif b1 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  		else
  			op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  		end
  		if op == 0 then
  			Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD, nil)
  		else
  			Duel.DiscardHand(tp, s.costfilter, 1, 1, REASON_COST + REASON_DISCARD, nil)
  		end
  		e:SetLabel(0, op)
  	else
  		local op = 0
  		if count1 >= 2 and count2 >= 2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0), aux.Stringid(id, 1))
  		elseif count1 >= 2 then
  			op = Duel.SelectOption(tp, aux.Stringid(id, 0))
  		else
  			op = Duel.SelectOption(tp, aux.Stringid(id, 1)) + 1
  		end
  		e:SetLabel(0, op)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 2, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local label, op = e:GetLabel()
  	if op == 0 then
  		local g = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local hg = g:SelectSubGroup(tp, aux.dncheck, false, 2, 2)
  		if hg then
  			Duel.SendtoHand(hg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, hg)
  		end
  	else
  		local g = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local hg = g:SelectSubGroup(tp, aux.dncheck, false, 2, 2)
  		if hg then
  			Duel.SendtoHand(hg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, hg)
  		end
  	end
  end
  
  