name: Dragunity
card_text: You can activate 1 of these effects, but you cannot Special Summon monsters for the rest of this turn, except "Dragunity" monsters.
  ● Target 1 "Dragunity" Monster Card in your Spell & Trap Zone; Special Summon it in Defense Position.
  ● Target 1 "Dragunity" monster you control; equip that target with 1 "Dragunity" monster from your GY.
  You can only use this effect of "Dragunity Legion" once per turn.
external_ids: local SETCARD_ID_0 = 0x29 --Dragunity
  
lua: local SETCARD_ID_0 = 0x29 --Dragunity
  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)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetTarget(s.eftg)
  	e2:SetOperation(s.efop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.eqfilter1(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.eqfilter2, tp, LOCATION_GRAVE, 0, 1, nil, c, tp)
  end
  function s.eqfilter2(c, tc, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsForbidden()
  end
  function s.eftg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		if e:GetLabel() == 0 then
  			return chkc:IsLocation(LOCATION_SZONE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  		else
  			return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.eqfilter1(chkc, tp)
  		end
  	end
  	local b1 = Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_SZONE, 0, 1, nil, e, tp)
  	local b2 = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingTarget(s.eqfilter1, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_SPECIAL_SUMMON)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_SZONE, 0, 1, 1, nil, e, tp)
  		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  	else
  		e:SetCategory(CATEGORY_EQUIP)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		Duel.SelectTarget(tp, s.eqfilter1, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  		Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, nil, 1, tp, 0)
  	end
  end
  function s.efop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabel() == 0 then
  		local tc = Duel.GetFirstTarget()
  		if tc and tc:IsRelateToEffect(e) then
  			Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		end
  	else
  		if Duel.GetLocationCount(tp, LOCATION_SZONE) <= 0 then
  			return
  		end
  		local ec = Duel.GetFirstTarget()
  		if ec:IsRelateToEffect(e) and ec:IsFaceup() then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  			local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.eqfilter2), tp, LOCATION_GRAVE, 0, 1, 1, nil, ec, tp)
  			local tc = g:GetFirst()
  			if not tc or not Duel.Equip(tp, tc, ec) then
  				return
  			end
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(s.eqlimit2)
  			e1:SetLabelObject(ec)
  			tc:RegisterEffect(e1)
  		end
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET + EFFECT_FLAG_OATH)
  	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetTargetRange(1, 0)
  	e1:SetLabelObject(e)
  	e1:SetTarget(s.splimit)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp, se)
  	return not c:IsSetCard(SETCARD_ID_0)
  end
  function s.eqlimit2(e, c)
  	return c == e:GetLabelObject()
  end
  
  
source_text: Dragunity
  You can activate 1 of these effects, but you cannot Special Summon monsters for the rest of this turn, except "Dragunity" monsters.
  ● Target 1 "Dragunity" Monster Card in your Spell & Trap Zone; Special Summon it in Defense Position.
  ● Target 1 "Dragunity" monster you control; equip that target with 1 "Dragunity" monster from your GY.
  You can only use this effect of "Dragunity Legion" once per turn.
target_text: -- Dragunity
  local SETCARD_ID_0 = 0x29 --Dragunity
  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)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetTarget(s.eftg)
  	e2:SetOperation(s.efop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.eqfilter1(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.eqfilter2, tp, LOCATION_GRAVE, 0, 1, nil, c, tp)
  end
  function s.eqfilter2(c, tc, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsForbidden()
  end
  function s.eftg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		if e:GetLabel() == 0 then
  			return chkc:IsLocation(LOCATION_SZONE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  		else
  			return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.eqfilter1(chkc, tp)
  		end
  	end
  	local b1 = Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_SZONE, 0, 1, nil, e, tp)
  	local b2 = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingTarget(s.eqfilter1, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local op = 0
  	if b1 and b2 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1), aux.Stringid(id, 2))
  	elseif b1 then
  		op = Duel.SelectOption(tp, aux.Stringid(id, 1))
  	else
  		op = Duel.SelectOption(tp, aux.Stringid(id, 2)) + 1
  	end
  	e:SetLabel(op)
  	if op == 0 then
  		e:SetCategory(CATEGORY_SPECIAL_SUMMON)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_SZONE, 0, 1, 1, nil, e, tp)
  		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  	else
  		e:SetCategory(CATEGORY_EQUIP)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  		Duel.SelectTarget(tp, s.eqfilter1, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  		Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, nil, 1, tp, 0)
  	end
  end
  function s.efop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if e:GetLabel() == 0 then
  		local tc = Duel.GetFirstTarget()
  		if tc and tc:IsRelateToEffect(e) then
  			Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  		end
  	else
  		if Duel.GetLocationCount(tp, LOCATION_SZONE) <= 0 then
  			return
  		end
  		local ec = Duel.GetFirstTarget()
  		if ec:IsRelateToEffect(e) and ec:IsFaceup() then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  			local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.eqfilter2), tp, LOCATION_GRAVE, 0, 1, 1, nil, ec, tp)
  			local tc = g:GetFirst()
  			if not tc or not Duel.Equip(tp, tc, ec) then
  				return
  			end
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_EQUIP_LIMIT)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  			e1:SetValue(s.eqlimit2)
  			e1:SetLabelObject(ec)
  			tc:RegisterEffect(e1)
  		end
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET + EFFECT_FLAG_OATH)
  	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetTargetRange(1, 0)
  	e1:SetLabelObject(e)
  	e1:SetTarget(s.splimit)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.splimit(e, c, sump, sumtype, sumpos, targetp, se)
  	return not c:IsSetCard(SETCARD_ID_0)
  end
  function s.eqlimit2(e, c)
  	return c == e:GetLabelObject()
  end
  
  