name: Subterror
card_text: Activate 1 of these effects, also, after that, Set this card face-down instead of sending it to the GY.
  ● Change 1 Set "Subterror" monster on the field to face-up Attack or Defense Position.
  ● Change 1 face-up "Subterror" monster on the field to face-down Defense Position.
  ● ATK/DEF of 1 "Subterror" monster on the field become equal to its combined original ATK/DEF until the end of this turn.
  ● This turn, activated effects of "Subterror" cards cannot be negated.
external_ids: local SETCARD_ID_0 = 0xed --Subterror
  
lua: local SETCARD_ID_0 = 0xed --Subterror
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP, TIMING_DAMAGE_STEP + TIMING_END_PHASE)
  	e1:SetCondition(aux.dscon)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsDefenseAbove(0)
  end
  function s.fufilter(c)
  	return c:IsFacedown() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.fdfilter(c)
  	return s.filter(c) and c:IsCanTurnSet()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.IsExistingMatchingCard(s.fufilter, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.IsExistingMatchingCard(s.fdfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	local b3 = Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	local b4 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.GetFlagEffect(tp, id) == 0
  	if chk == 0 then
  		return b1 or b2 or b3 or b4
  	end
  	local off = 1
  	local ops = {}
  	local opval = {}
  	if b1 then
  		ops[off] = aux.Stringid(id, 0)
  		opval[off - 1] = 1
  		off = off + 1
  	end
  	if b2 then
  		ops[off] = aux.Stringid(id, 1)
  		opval[off - 1] = 2
  		off = off + 1
  	end
  	if b3 then
  		ops[off] = aux.Stringid(id, 2)
  		opval[off - 1] = 3
  		off = off + 1
  	end
  	if b4 then
  		ops[off] = aux.Stringid(id, 3)
  		opval[off - 1] = 4
  		off = off + 1
  	end
  	local op = Duel.SelectOption(tp, table.unpack(ops))
  	local sel = opval[op]
  	e:SetLabel(sel)
  	if sel == 1 or sel == 2 then
  		e:SetCategory(CATEGORY_POSITION)
  		Duel.SetOperationInfo(0, CATEGORY_POSITION, nil, 1, 0, 0)
  	elseif sel == 3 then
  		e:SetCategory(CATEGORY_ATKCHANGE + CATEGORY_DEFCHANGE)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local sel = e:GetLabel()
  	if sel == 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, s.fufilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			local tc = g:GetFirst()
  			local pos = Duel.SelectPosition(tp, tc, POS_FACEUP_ATTACK + POS_FACEUP_DEFENSE)
  			Duel.ChangePosition(tc, pos)
  		end
  	elseif sel == 2 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, s.fdfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g, POS_FACEDOWN_DEFENSE)
  		end
  	elseif sel == 3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			local tc = g:GetFirst()
  			local atk = tc:GetBaseAttack() + tc:GetBaseDefense()
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  			e1:SetValue(atk)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  			local e2 = e1:Clone()
  			e2:SetCode(EFFECT_SET_DEFENSE_FINAL)
  			tc:RegisterEffect(e2)
  		end
  	else
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_CANNOT_DISEFFECT)
  		e1:SetValue(s.effectfilter)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  		Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if c:IsRelateToEffect(e) and c:IsCanTurnSet() and e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		Duel.BreakEffect()
  		c:CancelToGrave()
  		Duel.ChangePosition(c, POS_FACEDOWN)
  		Duel.RaiseEvent(c, EVENT_SSET, e, REASON_EFFECT, tp, tp, 0)
  	end
  end
  function s.effectfilter(e, ct)
  	local te = Duel.GetChainInfo(ct, CHAININFO_TRIGGERING_EFFECT)
  	local tc = te:GetHandler()
  	return tc:IsSetCard(SETCARD_ID_0)
  end
  
  
source_text: Subterror
  Activate 1 of these effects, also, after that, Set this card face-down instead of sending it to the GY.
  ● Change 1 Set "Subterror" monster on the field to face-up Attack or Defense Position.
  ● Change 1 face-up "Subterror" monster on the field to face-down Defense Position.
  ● ATK/DEF of 1 "Subterror" monster on the field become equal to its combined original ATK/DEF until the end of this turn.
  ● This turn, activated effects of "Subterror" cards cannot be negated.
target_text: -- Subterror
  local SETCARD_ID_0 = 0xed --Subterror
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP, TIMING_DAMAGE_STEP + TIMING_END_PHASE)
  	e1:SetCondition(aux.dscon)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsDefenseAbove(0)
  end
  function s.fufilter(c)
  	return c:IsFacedown() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.fdfilter(c)
  	return s.filter(c) and c:IsCanTurnSet()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.IsExistingMatchingCard(s.fufilter, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.IsExistingMatchingCard(s.fdfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	local b3 = Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	local b4 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE and Duel.GetFlagEffect(tp, id) == 0
  	if chk == 0 then
  		return b1 or b2 or b3 or b4
  	end
  	local off = 1
  	local ops = {}
  	local opval = {}
  	if b1 then
  		ops[off] = aux.Stringid(id, 0)
  		opval[off - 1] = 1
  		off = off + 1
  	end
  	if b2 then
  		ops[off] = aux.Stringid(id, 1)
  		opval[off - 1] = 2
  		off = off + 1
  	end
  	if b3 then
  		ops[off] = aux.Stringid(id, 2)
  		opval[off - 1] = 3
  		off = off + 1
  	end
  	if b4 then
  		ops[off] = aux.Stringid(id, 3)
  		opval[off - 1] = 4
  		off = off + 1
  	end
  	local op = Duel.SelectOption(tp, table.unpack(ops))
  	local sel = opval[op]
  	e:SetLabel(sel)
  	if sel == 1 or sel == 2 then
  		e:SetCategory(CATEGORY_POSITION)
  		Duel.SetOperationInfo(0, CATEGORY_POSITION, nil, 1, 0, 0)
  	elseif sel == 3 then
  		e:SetCategory(CATEGORY_ATKCHANGE + CATEGORY_DEFCHANGE)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local sel = e:GetLabel()
  	if sel == 1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, s.fufilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			local tc = g:GetFirst()
  			local pos = Duel.SelectPosition(tp, tc, POS_FACEUP_ATTACK + POS_FACEUP_DEFENSE)
  			Duel.ChangePosition(tc, pos)
  		end
  	elseif sel == 2 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, s.fdfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g, POS_FACEDOWN_DEFENSE)
  		end
  	elseif sel == 3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			local tc = g:GetFirst()
  			local atk = tc:GetBaseAttack() + tc:GetBaseDefense()
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  			e1:SetValue(atk)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  			local e2 = e1:Clone()
  			e2:SetCode(EFFECT_SET_DEFENSE_FINAL)
  			tc:RegisterEffect(e2)
  		end
  	else
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_CANNOT_DISEFFECT)
  		e1:SetValue(s.effectfilter)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  		Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if c:IsRelateToEffect(e) and c:IsCanTurnSet() and e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		Duel.BreakEffect()
  		c:CancelToGrave()
  		Duel.ChangePosition(c, POS_FACEDOWN)
  		Duel.RaiseEvent(c, EVENT_SSET, e, REASON_EFFECT, tp, tp, 0)
  	end
  end
  function s.effectfilter(e, ct)
  	local te = Duel.GetChainInfo(ct, CHAININFO_TRIGGERING_EFFECT)
  	local tc = te:GetHandler()
  	return tc:IsSetCard(SETCARD_ID_0)
  end
  
  