name: Ancient
card_text: 2 EARTH Machine monsters
  If this card is Link Summoned: You can add 1 "Ancient Gear" monster or 1 "Geartown" from your Deck to your hand. You can target 1 Spell/Trap you control and 1 face-up monster your opponent controls; destroy that card you control, and if you do, change that opponent's monster's ATK/DEF to 0 until the end of the turn. You can only use each effect of "Ancient Gear Ballista" once per turn.
external_ids: local CARD_ID_1 = 37694547 --Geartown
  local SETCARD_ID_0 = 0x7 --Ancient
  
lua: local CARD_ID_1 = 37694547 --Geartown
  local SETCARD_ID_0 = 0x7 --Ancient
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, s.mfilter, 2, 2)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE + CATEGORY_DEFCHANGE)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.mfilter(c)
  	return c:IsLinkAttribute(ATTRIBUTE_EARTH) and c:IsLinkRace(RACE_MACHINE)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.thfilter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or c:IsCode(CARD_ID_1)) and c:IsAbleToHand()
  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)
  	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)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.desfilter(c)
  	return c:IsFaceup() and (c:IsAttackAbove(1) or c:IsDefenseAbove(1))
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsType, tp, LOCATION_ONFIELD, 0, 1, nil, TYPE_SPELL + TYPE_TRAP) and Duel.IsExistingTarget(s.desfilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	local g1 = Duel.SelectTarget(tp, Card.IsType, tp, LOCATION_ONFIELD, 0, 1, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	e:SetLabelObject(g1:GetFirst())
  	local g2 = Duel.SelectTarget(tp, s.desfilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g1, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = e:GetLabelObject()
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local lc = tg:GetFirst()
  	if lc == tc then
  		lc = tg:GetNext()
  	end
  	if tc:IsRelateToEffect(e) and tc:IsControler(tp) and Duel.Destroy(tc, REASON_EFFECT) ~= 0 and lc:IsRelateToEffect(e) and lc:IsControler(1 - tp) and lc:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetValue(0)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		lc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_SET_DEFENSE_FINAL)
  		lc:RegisterEffect(e2)
  	end
  end
  
  
source_text: Ancient
  2 EARTH Machine monsters
  If this card is Link Summoned: You can add 1 "Ancient Gear" monster or 1 "Geartown" from your Deck to your hand. You can target 1 Spell/Trap you control and 1 face-up monster your opponent controls; destroy that card you control, and if you do, change that opponent's monster's ATK/DEF to 0 until the end of the turn. You can only use each effect of "Ancient Gear Ballista" once per turn.
target_text: -- Ancient
  local CARD_ID_1 = 37694547 --Geartown
  local SETCARD_ID_0 = 0x7 --Ancient
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, s.mfilter, 2, 2)
  	c:EnableReviveLimit()
  	--search
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.thcon)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY + CATEGORY_ATKCHANGE + CATEGORY_DEFCHANGE)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.mfilter(c)
  	return c:IsLinkAttribute(ATTRIBUTE_EARTH) and c:IsLinkRace(RACE_MACHINE)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_LINK)
  end
  function s.thfilter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or c:IsCode(CARD_ID_1)) and c:IsAbleToHand()
  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)
  	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)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.desfilter(c)
  	return c:IsFaceup() and (c:IsAttackAbove(1) or c:IsDefenseAbove(1))
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsType, tp, LOCATION_ONFIELD, 0, 1, nil, TYPE_SPELL + TYPE_TRAP) and Duel.IsExistingTarget(s.desfilter, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	local g1 = Duel.SelectTarget(tp, Card.IsType, tp, LOCATION_ONFIELD, 0, 1, 1, nil, TYPE_SPELL + TYPE_TRAP)
  	e:SetLabelObject(g1:GetFirst())
  	local g2 = Duel.SelectTarget(tp, s.desfilter, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g1, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = e:GetLabelObject()
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local lc = tg:GetFirst()
  	if lc == tc then
  		lc = tg:GetNext()
  	end
  	if tc:IsRelateToEffect(e) and tc:IsControler(tp) and Duel.Destroy(tc, REASON_EFFECT) ~= 0 and lc:IsRelateToEffect(e) and lc:IsControler(1 - tp) and lc:IsFaceup() then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetValue(0)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		lc:RegisterEffect(e1)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_SET_DEFENSE_FINAL)
  		lc:RegisterEffect(e2)
  	end
  end
  
  